Skip to content

Fix normalizeSignature dropping the parameter after ( - #5201

Merged
jxom merged 1 commit into
wevm:mainfrom
ara-stock:fix/normalize-signature-leading-space
Oct 11, 2026
Merged

jxom merged 1 commit into
wevm:mainfrom
ara-stock:fix/normalize-signature-leading-space

Conversation

@ara-stock

Copy link
Copy Markdown
Contributor

normalizeSignature (used by toSignature, toFunctionSelector, toEventSelector, toFunctionHash, toEventHash) silently drops the first parameter of a list when a space directly follows the opening parenthesis. The result is a valid-looking but wrong selector/topic, with no error.

import { parseAbiItem, toEventSelector, toFunctionSelector } from 'viem'

toFunctionSelector('function ownerOf( uint256 tokenId )')
// 0xc2985578  (= keccak("ownerOf()")) -- expected 0x6352211e

toEventSelector('event Transfer( address indexed from, address indexed to, uint256 value)')
// 0x4aa37b32...  (= keccak("Transfer(,address,uint256)")) -- expected 0xddf252ad...

toFunctionSelector(parseAbiItem('function ownerOf( uint256 tokenId )'))
// 0x6352211e  -- abitype parses the same string correctly

Other examples (released viem 2.57.3 → expected, expected computed with ethers v6 FunctionFragment.from(s).format('sighash')):

input before after
function foo( uint256 a ) foo() foo(uint256)
foo( uint256 a, address b) foo(,address) foo(uint256,address)
function foo(( uint256 a, bool b) c) foo((,bool)) foo((uint256,bool))
function foo( ( uint256 a, bool b)[] c) foo((,bool)[]) foo((uint256,bool)[])

Cause: a space inside the parameter list deactivates the parser (to skip the parameter name / indexed / data location) unless it follows a separator. The check only recognised ,, current === ',' and current === ',(', so a space after ( at the start of a list (or after (() was treated as the end of a type, and everything up to the next separator was skipped. The existing trim spaces test passed only because its ( uint104 ... tuple follows a , .

Fix: treat a space as leading whitespace whenever the current section ends with ( or , (this subsumes the three existing conditions), otherwise deactivate as before.

Alternatives considered: routing string inputs through abitype's parseAbiItem would also fix it, but changes what toSignature accepts (e.g. bare foo(uint256) without a function keyword), so I kept the change local to the existing parser.

Tests:

  • Added cases to normalizeSignature.test.ts, toFunctionSelector.test.ts and toEventSelector.test.ts. With the fix reverted, these 3 tests fail; with the fix they pass.
  • vitest run src/utils/hash/ (Node 24, SKIP_GLOBAL_SETUP=true): 41 passed. The one remaining failure, error: invalid signatures, is an inline snapshot that expects [email protected] and receives [email protected] because the global setup was skipped; it fails the same way on main in this setup.
  • biome check on the four changed files: clean.
  • Extra check (not committed): 5,000 random signatures with random spaces around (, , and ), compared with ethers v6 format('sighash'): 2,108 mismatches with viem 2.57.3, 0 with this change.

Changeset added (patch).

I used an AI assistant while preparing this; I reproduced the bug and ran the tests myself.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 7, 2026

Copy link
Copy Markdown

@ara-stock 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 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c94c054

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](https://github.com/ara-stock/viem/new/fix/normalize-signature-leading-space?filename=.changeset/ready-experts-stick.md&value=---%0A%22viem%22%3A%20patch%0A---%0A%0AFix%20%60normalizeSignature%60%20dropping%20the%20parameter%20after%20%60(%20%60%0A)

@jxom
jxom merged commit 0c504ac into wevm:main Oct 11, 2026
2 of 3 checks passed
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