paulmillr / noble-secp256k1

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

what is the process to transform the noble-secp256k1 signature output to a node:crypto signature? #125

Closed lemoustachiste closed 3 weeks ago

lemoustachiste commented 3 weeks ago

Hi,

so I'm trying to use @noble/secp256k1 as a replacement/polyfill for the node:crypto (and crypto.subtle) methods to sign and verify.

However the output of both sign methods are different.

Noble yields the low-level r + s signature as such:

Signature {
  r: 30673915999826815067112358723830053709740609731848351883723761154187753271507n,
  s: 27924747462559541784234137714889274701124087642782423432899060127035916103299n,
  recovery: 1
}

And the crypto/webcrypto world returns a Buffer as follows:

ArrayBuffer {
  [Uint8Contents]: <26 af be f2 d1 9b 09 fb be 20 3b ce ca 09 33 f4 c5 ed c5 24 ae 77 b7 fc 5c 4b 69 7d bd c6 a5 aa a1 12 9f 07 ca a8 62 9d 5b bd 6a b4 c0 a2 70 9d b0 f2 5f ea ce f2 b9 b4 91 d5 17 de 8f d6 88 b6>,
  byteLength: 64
}

I don't know how to transform (and the inverse) the noble signature to be following the same format as the crypto one. Can you point me in the right direction?

Thanks

paulmillr commented 3 weeks ago

use Signature#toCompactRawBytes

lemoustachiste commented 3 weeks ago

Perfect, thanks for the swift answer