Skip to content
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

Merged
merged 2 commits into from
Feb 10, 2023
Merged

Fix EIP712 encoding #458

merged 2 commits into from
Feb 10, 2023

Conversation

Uxio0
Copy link
Member

@Uxio0 Uxio0 commented Feb 9, 2023

  • Fix empty arrays
  • Fix arrays with repeating elements

@Uxio0 Uxio0 requested a review from a team as a code owner February 9, 2023 17:13
@Uxio0 Uxio0 requested review from fmrsabino, moisses89 and hectorgomezv and removed request for a team February 9, 2023 17:13
Copy link
Member

@mmv08 mmv08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔝

@coveralls
Copy link

coveralls commented Feb 10, 2023

Pull Request Test Coverage Report for Build 4142981802

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage remained the same at 90.634%

Files with Coverage Reduction New Missed Lines %
gnosis/eth/ethereum_client.py 1 86.74%
Totals Coverage Status
Change from base Build 4112569321: 0.0%
Covered Lines: 3774
Relevant Lines: 4164

💛 - Coveralls

@Uxio0
Copy link
Member Author

Uxio0 commented Feb 10, 2023

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))

Copy link
Member

@moisses89 moisses89 left a 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:
Copy link
Member

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 = [], []

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, more optimal!

Copy link
Member Author

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
Copy link
Member

@moisses89 moisses89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@Uxio0 Uxio0 merged commit 4e5c5f3 into master Feb 10, 2023
@Uxio0 Uxio0 deleted the fix-eip712-encoding branch February 10, 2023 10:42
@github-actions github-actions bot locked and limited conversation to collaborators Feb 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants