Skip to content

fix(signatureToCompactSignature): pad s and derive yParity from legacy v - #5235

Closed
pucedoteth wants to merge 1 commit into
wevm:mainfrom
pucedoteth:fix-compact-signature-short-s
Closed

pucedoteth wants to merge 1 commit into
wevm:mainfrom
pucedoteth:fix-compact-signature-short-s

Conversation

@pucedoteth

Copy link
Copy Markdown
Contributor

signatureToCompactSignature returns a wrong EIP-2098 signature in two cases. Both come up with transactions read over RPC.

s shorter than 32 bytes. The yParity bit was set on hexToBytes(s)[0], which is only the top byte when s is exactly 32 bytes. Nodes return r/s as quantities with leading zeros stripped, so about 1 in 256 signatures has a shorter s, and the bit lands inside the value. In the new test, s = 0xc3d1… (31 bytes) already has its top bit set, so the output is unchanged and the parity is lost. compactSignatureToSignature then reads the result as a different s, and recoverAddress returns the wrong address. s is now left-padded to 32 bytes first. The size option of hexToBytes pads on the right, so this uses pad.

Legacy v with a chain id. yParity was computed as v - 27n, so v = 38n (chain 1, recovery 1) became 11 and the bit was never set. formatTransaction drops yParity for legacy transactions, so v is all a caller has there. The yParity logic now matches serializeSignature: 27/28/>= 35 by parity, otherwise the same Invalid \v` or `yParity` valueerror. Before, an unsupportedv` silently produced no bit.

The two new tests fail without the change. The short-s test uses a real signature from accounts[0] and checks that recoverAddress returns that account after a round trip through the compact form. All src/utils/signature tests pass (50), and biome and tsc are clean.

Written with AI assistance (Claude Code).

🤖 Generated with Claude Code

…acy `v`

Set the yParity bit on `s` padded to 32 bytes, so an `s` with a leading zero
byte keeps its value, and derive yParity from an EIP-155 `v` the same way
`serializeSignature` does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 15, 2026

Copy link
Copy Markdown

@pucedoteth is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Oct 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2ade2ef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/viem@5235

commit: 2ade2ef

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