btcsuite / btcd

An alternative full node bitcoin implementation written in Go (golang)
https://github.com/btcsuite/btcd/blob/master/README.md
ISC License
6.11k stars 2.32k forks source link

Request: serialize secp256k1 signatures without DER #1963

Closed decentralgabe closed 1 year ago

decentralgabe commented 1 year ago

I am attempting to build compatibility of secp256k1 signatures between typescript and golang. A prominent TS lib is noble-secp256k1 which exposes an option to sign/encode using DER.

The only way the library exposes transforming a secp256k1 signature into []byte is via the [Serialize method]() which serializes to the DER format.

It would be great if an enhancement could be made to serialize without the DER format, as it's not always needed. More broadly, it appears this functionality is missing in the go ecosystem. This library remains the most authoritative option for secp256k1 usage and rightly so, and it can be improved to enable non-DER serializations.

If this is possible today and I've misunderstood please help me learn how to serialize to bytes without DER.

guggero commented 1 year ago

There is the SignCompact function which I believe returns what you are asking for: https://github.com/decred/dcrd/blob/master/dcrec/secp256k1/ecdsa/signature.go#L765. That returns a byte slice of <compactSigRecoveryCode><32-byte R><32-byte S>. So all you probably need to do is to remove that first byte with the recovery code and it should be compatible with the TS lib you linked to (if I understand their documentation correctly).

decentralgabe commented 1 year ago

thanks @guggero would you be open to a PR to add a new .SerializeCompactHex method here?

guggero commented 1 year ago

I don't think you can do it in this repo, since the Signature struct lives in the decred/dcrd repo and you won't have access to the non-exported r and s values. So I think just using SignCompact is probably your best option.

decentralgabe commented 1 year ago

thanks, you're right. I tried your suggestion and it worked.