I have a problem with the recoverPublicKey() method in the latest version of js-ethereum-cryptography. I can get the values of { r, s, recovery }, but I cannot recover the public key because I get an undefined error in the browser. Thanks for your help!
app.post("/send", (req, res) => {
const { sender, recipient, amount, signature } = req.body;
const { r, s, recovery } = signature;
const publicKey = secp.secp256k1.recoverPublicKey({ r, s, recovery }, sender);
console.log(publicKey.toString());
//...
});
From the Upgrading section of js-ethereum-cryptography oficial repository:
And from Upgrading section of noble-curves:
So, you should first recreate the
signatureobject from theJSONdata, and then use it to recover the public key. Note that depending on how you serialised the (BigInt)rands, you may need to convert them back toBigIntfirst.Also, even in the old version of the library, recovery is done from the
signatureand the hash of thetransactiondata, not from the sender (as in your code).Finally, depending on what you need to do with the public key, you can use the
toRawBytes()ortoHex()methods, and even get the uncompressed version by specifying theisCompressed = falseparameter.