Skip to content

fix(erc7739): order the wrapped contents type like the TypedDataSign type - #5487

Closed
erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/erc7739-sorted-contents-type
Closed

erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/erc7739-sorted-contents-type

Conversation

@erkancamli

Copy link
Copy Markdown
Contributor

wrapTypedDataSignature appends contentsType built with encodeType({ primaryType, types }), so the contents struct comes first and its dependencies follow. The owner signs the TypedDataSign typed data from erc7739/hashTypedData, and in that encoding the contents struct is a dependency of TypedDataSign, so it is sorted by name together with its own dependencies. An ERC-7739 verifier rebuilds the typehash as "TypedDataSign(" ‖ contentsName ‖ " contents,...)" ‖ contentsType from what was appended, so the two only match when the primary type also sorts first.

When a nested type sorts before the primary type, the verifier hashes something else and isValidSignature fails. Permit2 PermitSingle is an example: PermitDetails sorts first, so the signed type ends in PermitDetails(...)PermitSingle(...) while the appended one is PermitSingle(...)PermitDetails(...). The ERC covers this case: "When the contents structure contains nested types, EIP-712 lexicographical sorting can result in the contentsName not being positioned exactly at the start of the contentsType. As such, we need the explicit mode." Solady's ERC1271 reads both modes.

This reaches toSoladySmartAccount().signTypedData, the ERC-7739 signTypedData action and erc7739Actions.

The change builds contentsType in the order of the TypedDataSign encoding and uses explicit mode (contentsType ‖ contentsName) only when the primary type is not first. When it is first (the Mail fixtures, for example) the output is byte for byte the same as before.

Tests

In wrapTypedDataSignature.test.ts, a helper rebuilds the hash a verifier checks from the wrapped signature (implicit and explicit modes, per the ERC and Solady's parsing) and compares it with erc7739/hashTypedData:

  • default (Mail): unchanged snapshot, and the hashes match before and after
  • behavior: nested type sorts before the contents type (PermitSingle): fails on main (417f2eb) and passes with the change

The erc7739, signature and typed data suites give the same results before and after apart from the new test (the anvil tests could not run in my environment, and fail the same way on both). tsc and biome are clean. A changeset is included. I did not run an on-chain isValidSignature check against a deployed account.

The v3 branch copy in src/erc4337/internal/erc7739.ts builds contentsType the same way.

@vercel

vercel Bot commented Nov 11, 2026

Copy link
Copy Markdown

@erkancamli 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 Nov 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2fa23e9

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

…type

wrapTypedDataSignature put the contents struct first in contentsDescription,
but the signed TypedDataSign type sorts it with its dependencies. An
ERC-7739 verifier rebuilds the type from contentsDescription, so a typed
data signature whose nested type sorts before the primary type (for
example Permit2 PermitDetails and PermitSingle) did not verify.

Send the contents type in the sorted order and use explicit mode when the
primary type is not first.
@erkancamli
erkancamli force-pushed the fix/erc7739-sorted-contents-type branch from 5297606 to 2fa23e9 Compare November 17, 2026 17:13
@erkancamli

Copy link
Copy Markdown
Contributor Author

Rebased onto main to clear the package.json conflict. Main already raised the viem/account-abstraction limit to 63 kB, so I dropped my separate size bump: with this change the bundle measures 62.98 kB (62.93 kB on main). The fix and its test are unchanged.

jxom commented Nov 18, 2026

Copy link
Copy Markdown
Member

Thanks @erkancamli, the fix is spot on! I carried your commit over to #5583 with a few small review fixes (one-sentence changeset, snapshot-based tests, and an on-chain PermitSingle check), so I'm closing this in favour of it.

@jxom jxom closed this Nov 18, 2026
@erkancamli

Copy link
Copy Markdown
Contributor Author

Thanks @jxom, the review fixes look good to me. Appreciate you carrying it over!

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