-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix EIP712 encoding #458
Fix EIP712 encoding #458
Conversation
Uxio0
commented
Feb 9, 2023
- Fix empty arrays
- Fix arrays with repeating elements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔝
78d51ec
to
8a531f6
Compare
Pull Request Test Coverage Report for Build 4142981802
💛 - Coveralls |
Using the following node script to validate the hashes: const { ethers } = require('ethers')
const typedData = {
"types": {
"Nested": [
{
"name": "nestedString",
"type": "string"
},
{
"name": "nestedAddress",
"type": "address"
},
{
"name": "nestedUint256",
"type": "uint256"
},
{
"name": "nestedUint32",
"type": "uint32"
},
{
"name": "nestedBytes32",
"type": "bytes32"
},
{
"name": "nestedBoolean",
"type": "bool"
}
],
"Example": [
{
"name": "testString",
"type": "string"
},
{
"name": "testAddress",
"type": "address"
},
{
"name": "testUint256",
"type": "uint256"
},
{
"name": "testUint32",
"type": "uint32"
},
{
"name": "testBytes32",
"type": "bytes32"
},
{
"name": "testBoolean",
"type": "bool"
},
{
"name": "testNested",
"type": "Nested"
},
{
"name": "testNestedArray",
"type": "Nested[]"
}
],
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
]
},
"domain": {
"name": "EIP-1271 Example DApp",
"version": "1.0",
"chainId": "5",
"verifyingContract": "0xaecdfd3a19f777f0c03e6bf99aafb59937d6467b"
},
"primaryType": "Example",
"message": {
"testString": "Hello Deeeeeemo",
"testAddress": "0xaecdfd3a19f777f0c03e6bf99aafb59937d6467b",
"testUint256": "115792089237316195423570985008687907853269984665640564039457584007908834671663",
"testUint32": "123",
"testBytes32": "0x00000000000000000000000000000000000000000000000000000000deadbeef",
"testBoolean": true,
"testNested": {
"nestedString": "Hello Deeeeeemo",
"nestedAddress": "0x0000000000000000000000000000000000000002",
"nestedUint256": "0",
"nestedUint32": "1",
"nestedBytes32": "0x000000000000000000000000000000000000000000000000000000000000da7a",
"nestedBoolean": false
},
"testNestedArray": [
{
"nestedString": "Hello Deeeeeemo",
"nestedAddress": "0x0000000000000000000000000000000000000002",
"nestedUint256": "0",
"nestedUint32": "1",
"nestedBytes32": "0x000000000000000000000000000000000000000000000000000000000000da7a",
"nestedBoolean": false
},
{
"nestedString": "Hello Deeeeeemo",
"nestedAddress": "0x0000000000000000000000000000000000000002",
"nestedUint256": "0",
"nestedUint32": "1",
"nestedBytes32": "0x000000000000000000000000000000000000000000000000000000000000da7a",
"nestedBoolean": false
}
]
}
}
const { EIP712Domain: _, ...types } = typedData.types
console.log(ethers.utils._TypedDataEncoder.hash(typedData.domain, types, typedData.message)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good only a suggestion :)
parsed_type = typ[: typ.rindex("[")] | ||
type_value_pairs = [_encode_field(name, parsed_type, v) for v in value] | ||
# Empty array | ||
if value: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to check if value exists before to get the type_value_pairs
you can do something like:
if value:
parsed_type = typ[: typ.rindex("[")]
type_value_pairs = [_encode_field(name, parsed_type, v) for v in value]
data_types, data_hashes = zip(*type_value_pairs)
else:
data_types, data_hashes = [], []
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, more optimal!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
- Fix empty arrays - Fix arrays with repeating elements
8a531f6
to
4c86e32
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀