Skip to content

fix(signing): use .toCompactRawBytes() for SECP256K1 signatures#1

Open
amathxbt wants to merge 1 commit into
canopy-network:mainfrom
amathxbt:fix/secp256k1-sign-returns-object
Open

fix(signing): use .toCompactRawBytes() for SECP256K1 signatures#1
amathxbt wants to merge 1 commit into
canopy-network:mainfrom
amathxbt:fix/secp256k1-sign-returns-object

Conversation

@amathxbt

Copy link
Copy Markdown

Bug

secp256k1.sign() in @noble/curves v2 returns a Signature object, not a Uint8Array. The original code cast it as unknown as Uint8Array and passed the object directly to bytesToHex.

bytesToHex iterates bytes of whatever it receives; operating on a JavaScript object instead of typed bytes produces garbage hex for every SECP256K1 and ETHSECP256K1 signature generated by this SDK.

Impact: All transactions signed with SECP256K1 or ETHSECP256K1 keys are rejected by the network — the signature bytes are wrong.

Fix

Call .toCompactRawBytes() on the returned Signature object to obtain the correct 64-byte compact (r, s) representation before encoding.

// Before
return bytesToHex(secp256k1.sign(messageBytes, privKeyBytes) as unknown as Uint8Array);

// After
return bytesToHex(secp256k1.sign(messageBytes, privKeyBytes).toCompactRawBytes());

This matches how @noble/curves is documented and used for ed25519 (which already calls .sign() and gets a Uint8Array directly — ed25519 returns bytes, secp256k1 does not).

In noble/curves v2, secp256k1.sign() returns a Signature object, not a
Uint8Array. The previous code cast it directly as unknown as Uint8Array,
which serialises the object structure rather than the compact (r,s) bytes.

Result: every SECP256K1 / ETHSECP256K1 signature produced by this SDK is
invalid — the node rejects all transactions signed with those curves.

Fix: call .toCompactRawBytes() on the returned Signature object to get
the correct 64-byte compact representation before passing to bytesToHex.
@andrewnguyen22 andrewnguyen22 requested a review from ezeike June 27, 2026 19:27
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

Successfully merging this pull request may close these issues.

1 participant