fix(adapters): serialize bigints in viem signTypedData - #242
Open
SgtPooki wants to merge 1 commit into
Open
Conversation
owsToViemAccount().signTypedData ran JSON.stringify over the typed data, which throws "Do not know how to serialize a BigInt" whenever the message holds a uint256 (nonce, id, amount, deadline). viem hands a local account its message with bigints intact, so a real EIP-712 payload never signed. Encode bigints as even-length hex, which the core's parser accepts across the full uint256 range, and add EIP712Domain when the caller omits it (viem's signTypedData action adds it; a direct account call does not). The added test signs a uint256-bearing message and compares the signature to viem's privateKeyToAccount.
|
@SgtPooki is attempting to deploy a commit to the MoonPay Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
owsToViemAccount().signTypedDataserialized the payload withJSON.stringify, which throwsTypeError: Do not know how to serialize a BigIntwhenever the message holds a uint256. viem hands a local account its message with bigints intact (only viem's JSON-RPC path pre-serializes), so an EIP-712 payload that signs an id, nonce, amount, or deadline never signed. That covers EIP-2612 permits and most protocol authorizations.This encodes bigints as even-length hex before handing the JSON to the core. The core's parser enforces two things: decimal uint values above 2^128 are rejected ("exceeds u128 range; use hex encoding"), and hex must have an even number of digits. Even-length hex satisfies both across the full uint256 range.
It also adds
EIP712Domaintotypeswhen the caller omits it. viem'ssignTypedDataaction adds it before calling an account, but a directaccount.signTypedData()call does not, and the core needs it to resolve the domain type.signMessageandsignTransactionare unchanged.How to verify
The added test signs a message carrying
uint256anduint256[]values (0, 2^200, 2^256-1) and asserts the signature is byte-identical to viem'sprivateKeyToAccount. It fails onmainwithDo not know how to serialize a BigIntand passes with this change.Notes
JS-only change under
bindings/node-adapters; no Rust or public API change. Also verified against a Filecoin FEVM integration on the Calibration testnet: AddPieces and SchedulePieceRemovals both signed and confirmed on-chain through this adapter.related filecoin-project/filecoin-pin#458