Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating a did:key secp256k1 from private key #307

Open
lemoustachiste opened this issue Sep 6, 2023 · 2 comments
Open

Generating a did:key secp256k1 from private key #307

lemoustachiste opened this issue Sep 6, 2023 · 2 comments

Comments

@lemoustachiste
Copy link

So I may be confused as to what the seed is, as from the bitcoin perspective the seed is the passphrase to get to the private key, but given I already have a private key, can I generate a did:key from it? In that case what would be the expected syntax?

Thanks

@lemoustachiste lemoustachiste changed the title Generating a did:key secp256key1 from private key Generating a did:key secp256k1 from private key Sep 6, 2023
@phoniks
Copy link

phoniks commented Sep 6, 2023

This may help: https://w3c-ccg.github.io/did-method-key/

@lemoustachiste
Copy link
Author

lemoustachiste commented Sep 7, 2023

Ok so this is not the first time I'm using did:key and while I appreciate you sending to the spec which I should probably refresh my mind on, my question is really related to the @transmute/did-key-secp256k1 package.

Here is what I have done before, when I created a key pair and the did:key representation of that key pair:

if (type === SupportedSuites.secp256k1) {
    const seed = crypto.randomBytes(32);
    keyPair = await EcdsaSecp256k1VerificationKey2019.generate({
      seed
    })
    didKey = await didKeySecp256k1.generate({
      secureRandom: () => seed
    });
    didDocument = didKey.didDocument;
    keyPair.controller = didDocument.id;
    keyPair.id = didDocument.verificationMethod[0].id;
  }

However now, since I already have the private key (and no seed), what would the API of the package expect to create a did document with the public representation derived from that private key?

I have a PEM format key generated as follows:

openssl ecparam -name secp256k1 -genkey -noout -out priv_k.pem

That's the one variable I cannot change because that's the way the private is handed to me.

Using keyto, I'm able to convert that key to JWK (private and public). I can confirm using JOSE that the public derived is the same with openssl or with JS, so I expect that the format is valid.

Then I am trying to convert the JWK to https://github.com/transmute-industries/verifiable-data/blob/main/packages/secp256k1-key-pair/src/getPublicKeyFromPublicKeyJwk.ts#L3, not entirely sure what format I should get from that, but basically trying to get back to a base58 format, sort of trying to replicate this behavior: https://github.com/transmute-industries/verifiable-data/blob/main/packages/secp256k1-key-pair/src/Secp256k1KeyPair.ts#L55-L57

However getPublicKeyFromPublicKeyJwk throws an error from secp256k1 level:

Public Key could not be parsed

Due to this condition not passing: https://github.com/cryptocoinjs/secp256k1-node/blob/master/lib/elliptic.js#L39. So I assume something somewhere is not the right math, but I'm not a cryptographer myself.

My ultimate goal is to get the PEM key represented as a did:key:secp256k1 format so that I can reference that public key in the proof.verificationMethod of my VC.

Hence my initial question, can I generate a did:key directly from the private key JWK, and if so how can I do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants