How do I use the _TypedDataEncoder hash function for EIP712? #2738
-
Describe the bug
...I've checked and domain, types or values arent undefined. Reproduction steps `
Console output:
Environment: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
This problem can be reproduced with following script: const domain = {
name: 'N21_FACTORY',
version: '1',
chainId: 1,
verifyingContract: ethers.constants.AddressZero
}
const types = {
Create: [{
name: 'hashId',
type: 'bytes32'
},
{
name: 'account',
type: 'address'
},
{
name: 'value',
type: 'uint256'
},
{
name: 'currency',
type: 'bytes32'
},
{
name: 'startDate',
type: 'uint256'
},
{
name: 'endDate',
type: 'uint256'
},
{
name: 'uri',
type: 'bytes32'
},
{
name: 'deadline',
type: 'uint256'
}
]
}
const value = {
hashId: ethers.constants.HashZero,
account: ethers.constants.AddressZero,
value: 1,
currency: ethers.constants.HashZero,
startDate: 1,
endDate: 1,
uri: ethers.constants.HashZero,
deadline: 1
}
const typedDataEncoder = new ethers.utils._TypedDataEncoder.from(types)
const structHash = typedDataEncoder.hash(domain, types, value) // this gives error |
Beta Was this translation helpful? Give feedback.
-
Thanks @zemse, I’ll look into this shortly. |
Beta Was this translation helpful? Give feedback.
-
I found the issue. There are two separate APIs, one that operates on an instance and one that is static. You are using the static API on an instance; passing too many parameters in (the instance method You likely want just this: // Remove this line:
// const typedDataEncoder = ...
// And use the static method directly; no new, no instance
const structHash = ethers.utils._TypedDataEncoder.hash(domain, types, value) The instance APIs are designed for more low-level modifications to the encoding process, for debugging and for reusing parts of the encoding. Let me know if that helps. :) |
Beta Was this translation helpful? Give feedback.
I found the issue. There are two separate APIs, one that operates on an instance and one that is static. You are using the static API on an instance; passing too many parameters in (the instance method
.hash
only access one parameter,value
), so it is trying to evaluate thedomain
as the value.You likely want just this:
The instance APIs are designed for more low-level modifications to the encoding process, for debugging and for reusing parts of the encoding.
Let me know if that helps. :)