encodeField chops 2 bytes off UInt8Array bytes type #4096
Unanswered
simontao18
asked this question in
Idea / Feature Request
Replies: 1 comment 4 replies
-
|
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Check existing issues
Viem Version
2.31.7
Current Behavior
In the current
hashStructfunction to create a EIP-712 encoding,hashStructcalls a helper functionencodeFieldin order to encode the data to match the EIP-712 standard. However, the handling of thebytestype is incorrect. Thebytestype can accept both hex strings and UInt8Arrays as input, but the following snippet inencodeFieldimproperly handles UInt8Array values throughvalue.slice(2)and subsequent stringifying:There seems to be an implicit assumption here that the value should be a hex string. When a bytes field is passed as a Uint8Array into
hashStruct, the resulting EIP-712 hash is not equivalent to hashing the raw bytes. Instead, it hashes a coerced string representation. This is also confusing, since viem does support passing in both hex or UInt8Arrays into other functions, such askeccak256.Expected Behavior
The expected behavior should have some type guard / separate logic for properly handling UInt8Array types, instead of slicing the first two bytes off.
Steps To Reproduce
console output:
Link to Minimal Reproducible Example
No response
Anything else?
This bug may also extend to
hashTypedData, as it callshashStructifprimaryType !== 'EIP712Domain'Beta Was this translation helpful? Give feedback.
All reactions