Skip to content

Make secp256k1 sign_digest chain-agnostic, add sign_eth_digest#1048

Merged
0xh3rman merged 1 commit intomainfrom
secp256k1-recovery-id
Apr 3, 2026
Merged

Make secp256k1 sign_digest chain-agnostic, add sign_eth_digest#1048
0xh3rman merged 1 commit intomainfrom
secp256k1-recovery-id

Conversation

@0xh3rman
Copy link
Copy Markdown
Collaborator

@0xh3rman 0xh3rman commented Apr 3, 2026

The Ethereum recovery id offset (27) was hardcoded in the shared secp256k1 sign_digest, producing invalid signatures for non-Ethereum chains (e.g. Bitcoin BIP-137 headers became 66/67 instead of 39/40).

Split into chain-agnostic and Ethereum-specific signing:

  • sign_digest returns raw recovery id (0/1)
  • sign_eth_digest returns Ethereum-style v (27/28)
  • apply_eth_recovery_id for the WalletConnect path (idempotent)

Add Signer::sign_eth_digest and use it in EIP-712, Tron, and message signers. Add BIP-137 header assertion to Bitcoin signer test.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors Secp256k1 signing logic to centralize Ethereum-style recovery ID handling, introducing sign_eth_digest and an idempotent apply_ethereum_recovery_id function. Feedback suggests optimizing sign_eth_digest to avoid unnecessary allocations by pushing the recovery ID directly to the signature vector. Additionally, it is recommended to replace the assert_eq! in apply_ethereum_recovery_id with a safe length check to prevent potential panics in library usage.

@0xh3rman 0xh3rman force-pushed the secp256k1-recovery-id branch 3 times, most recently from 23cb23b to 43c68d6 Compare April 3, 2026 07:09
The Ethereum recovery id offset (27) was hardcoded in the shared
secp256k1 sign_digest, producing invalid signatures for non-Ethereum
chains (e.g. Bitcoin BIP-137 headers became 66/67 instead of 39/40).

Split into chain-agnostic and Ethereum-specific signing:
- sign_digest returns raw recovery id (0/1)
- sign_eth_digest returns Ethereum-style v (27/28)
- apply_ethereum_recovery_id for the WalletConnect path (idempotent)

Add Signer::sign_eth_digest and use it in EIP-712, Tron, and message
signers. Add BIP-137 header assertion to Bitcoin signer test.
@0xh3rman 0xh3rman force-pushed the secp256k1-recovery-id branch from 43c68d6 to d2299e2 Compare April 3, 2026 09:14
@0xh3rman 0xh3rman merged commit 3fc5192 into main Apr 3, 2026
6 checks passed
@0xh3rman 0xh3rman deleted the secp256k1-recovery-id branch April 3, 2026 09:53
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