paulmillr / noble-secp256k1

Fastest 4KB JS implementation of secp256k1 signatures and ECDH
https://paulmillr.com/noble
MIT License
757 stars 114 forks source link

How do I initialize a Signature with the v2? #111

Closed HRK44 closed 1 year ago

HRK44 commented 1 year ago

I have a signature without having the private key - and I want to verify that it was signed by the public key that claims the signature. I used to do : recoverPublicKey(msg, sig, rec) but now it has changed to sig.recoverPublicKey(msg).

How do I initialize the Signature since it seems that constructor only takes 2 big ints as params (and not a buffer/string).

paulmillr commented 1 year ago

Signature.fromCompact(signatureHex).addRecoveryBit(rec).recoverPublicKey(msg)

HRK44 commented 1 year ago

Ok thanks @paulmillr,

Could I just do:

    const msgHash = bip0322Hash(message);
    const signature = secp.etc.hexToBytes(signatureStr);
    const publicKey = secp.etc.hexToBytes(publicKeyStr);
    const msgHash2 = secp.etc.hexToBytes(msgHash);

    const isValid = secp.verify(signature, msgHash2, publicKey);
    console.log(isValid);

?

paulmillr commented 1 year ago

There is no need to convert from hex to bytes, you can just pass hex.