Skip to content

feat(signatures): sign P256 entries in signFrameTx - #6

Closed
ayushsingh82 wants to merge 1 commit into
JustaLab-co:mainfrom
ayushsingh82:feat/p256-signing
Closed

ayushsingh82 wants to merge 1 commit into
JustaLab-co:mainfrom
ayushsingh82:feat/p256-signing

Conversation

@ayushsingh82

Copy link
Copy Markdown
Contributor

signFrameTx only ever signed SECP256K1 entries. A P256 entry had to be assembled and normalized by hand — the README said as much — even though DESIGN §3 already spells out the low-s rule scheme 2 needs, and the reason it needs it: P256VERIFY accepts high s, so WebAuthn and passkey signers (which routinely emit it) produce transactions the chain rejects unless someone folds s down first.

So this teaches signFrameTx to do that fold, and to sign P256 entries.

normalizeP256Signature(sig) — new, exported. Takes r || s || qx || qy (128 bytes, any s) and rewrites it to low-s: s → n - s when s > n/2. ECDSA is malleable, so (r, n - s) verifies against the same key — the embedded qx || qy is untouched — and it's the form consensus wants. Structurally malformed input (wrong length, r or s outside (0, n)) throws rather than being silently repaired. Also useful standalone for anyone building the bytes by hand.

FrameAccount.signP256 — a new optional field, a raw-digest signer returning the 128-byte tuple. signFrameTx signs empty-msg P256 entries with it, normalizes s, and refuses on a resolved-signer mismatch exactly as it does for SECP256K1. A P256 entry with no signP256 available is left untouched, same as an ARBITRARY entry.

signFrameTx no longer requires sign unconditionally. An account with only signP256 — a passkey wrapper — can now sign a P256 transaction. A SECP256K1 entry with no sign method fails per-entry with a specific message instead of up front. An account with neither is still refused. The existing /raw 32-byte digest/ error path is unchanged.

signFrameTx does not verify a P256 signature — scheme 2 has no address recovery — so the signer owns r, s and the embedded key; the library owns the wire form only. That's the same line the rest of the P256 path already draws (assertCanonicalSignature range-checks scheme 2 but never touches the curve).

Tests (test/signatures.test.ts)

  • normalizeP256Signature: low-s untouched byte for byte, high-s folded to n - s, the n/2 boundary and the n/2 + 1 fold-back, the flipped result passes the canonical check, the public key survives, malformed input rejected. Bounds pinned to SECP256R1_N.
  • signFrameTx with a signP256: fills and normalizes a scheme-2 entry, a P256-only account works, resolved-signer mismatch refused, a P256-only account refuses a SECP256K1 entry, neither-signer refused, explicit-msg P256 left untouched, P256 left untouched when the account has no signP256, and a SECP256K1 + P256 pair signed in one pass.

bun run typecheck, bun run test (247 pass), bun run build, bunx @arethetypeswrong/cli --pack . all clean.

signFrameTx only signed SECP256K1 entries; a P256 entry had to be built
and normalized by hand (README:70), even though DESIGN §3 already
spells out the low-s rule scheme 2 needs.

- add normalizeP256Signature(sig): rewrite r||s||qx||qy to low-s form,
  s -> n - s when s > n/2. ECDSA is malleable, so (r, n - s) is a valid
  signature against the same key; it is also the form consensus
  requires, and WebAuthn / passkey signers routinely emit high s.
  Exported for building the bytes by hand. Malformed input (wrong
  length, r or s outside (0, n)) throws rather than being repaired.
- FrameAccount gains an optional signP256 returning r||s||qx||qy for a
  raw digest. signFrameTx signs empty-msg P256 entries with it,
  normalizing s, and refuses on a resolved-signer mismatch the same way
  it does for SECP256K1. A P256 entry with no signP256 available is
  left untouched, like an ARBITRARY entry.
- signFrameTx no longer requires `sign` unconditionally: an account
  with only signP256 can now sign a P256 transaction, and a SECP256K1
  entry without a `sign` method fails per-entry rather than up front.

signFrameTx does not verify a P256 signature — scheme 2 has no address
recovery — so the signer owns r, s and the embedded key; the library
owns the wire form. Bounds in the tests are pinned to the published
SECP256R1 group order.
@ayushsingh82

Copy link
Copy Markdown
Contributor Author

Rebased onto main (past #4 / #5 / v0.0.2). Conflicts were the DESIGN status line and the P256 test block that #5 also added — kept #5's assertCanonicalSignature: P256 block, folded my normalizeP256Signature block and signFrameTx with a P256 signer alongside it, deduped the shared resolveSigner/recoverFrameSigner/assertValidFrameTx P256 cases. 295 tests green.

@Ghadi8

Ghadi8 commented Oct 8, 2026

Copy link
Copy Markdown
Member

Thanks for this, and sorry it sat for so long. P-256 signing landed in #50 (released in v0.5.0): signFrameTx and signFrameSignature now sign scheme-2 entries from a P-256 owner, folding s to low form and verifying locally before encoding r || s || qx || qy. Since that covers what this PR set out to do and the branch now conflicts with main, I'm closing it. Your n/2 boundary cases were a good idea, and we'll keep them in mind for the signing tests.

@Ghadi8 Ghadi8 closed this Oct 8, 2026
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.

2 participants