ethereum / eth-account

Account abstraction library for web3.py
http://eth-account.readthedocs.io/
MIT License
269 stars 156 forks source link

encode_structured_data error #231

Closed yuedong1101 closed 1 year ago

yuedong1101 commented 1 year ago

What happened?

I wanted to get the signature by using encode_structured_data but I got two errors:

  1. Invalid Identifier Wallet address in SignIn when I change Wallet address to Walletaddress this error was solved but signature would be wrong

2.I change Wallet address to Walletaddress to test my code then I got the next error: KeyError: 'verifyingContract'

Code that produced the error

from eth_account.messages import encode_defunct, encode_structured_data
def sign_msg(private_key):
    address = Account.privateKeyToAccount(private_key).address
    data = {
        "types": {
            "EIP712Domain": [
                {"name": "name", "type": "string"},
                {"name": "version", "type": "string"},
                {"name": "chainId", "type": "uint256"},
                {"name": "verifyingContract", "type": "address"}
            ],
            "SignIn": [
                { "name": "message", "type": "string" },
                { "name": "Wallet address", "type": "address" }
            ]
        },
        "domain": {
            "name": "SecondLive",
            "version": "1",
            "chainId": 56
        },
        "message": {
            "message": "Welcome",
            "Wallet address": address
        },
        "primaryType": "SignIn",
    } 
    print(json.dumps(data, indent=4))
    message = encode_structured_data(data)

Full error output

ERROR1:
Traceback (most recent call last):
  File ".\click_on.py", line 400, in web_login
    sign = sign_msg(private_key)
  File ".\click_on.py", line 64, in sign_msg
    message = encode_structured_data(data)
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\messages.py", line 152, in encode_structured_data
    hash_domain(structured_data),
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\_utils\structured_data\hashing.py", line 284, in hash_domain
    structured_data["domain"]
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\_utils\structured_data\hashing.py", line 267, in encode_data
    data_types_and_hashes = _encode_data(primaryType, types, data)
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_utils\functional.py", line 45, in inner
    return callback(fn(*args, **kwargs))
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\_utils\structured_data\hashing.py", line 178, in _encode_data
    value = data[field["name"]]
KeyError: 'verifyingContract'

ERROR2:
Traceback (most recent call last):
  File ".\click_on.py", line 400, in web_login
    sign = sign_msg(private_key)
  File ".\click_on.py", line 64, in sign_msg
    message = encode_structured_data(data)
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\messages.py", line 152, in encode_structured_data
    hash_domain(structured_data),
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\_utils\structured_data\hashing.py", line 284, in hash_domain
    structured_data["domain"]
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\_utils\structured_data\hashing.py", line 267, in encode_data
    data_types_and_hashes = _encode_data(primaryType, types, data)
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_utils\functional.py", line 45, in inner
    return callback(fn(*args, **kwargs))
  File "C:\ProgramData\Anaconda3\envs\mytrader\lib\site-packages\eth_account\_utils\structured_data\hashing.py", line 178, in _encode_data
    value = data[field["name"]]
KeyError: 'verifyingContract'

Fill this section in if you know how this could or should be fixed

No response

eth-account Version

0.5.9

Python Version

3.6.13

Operating System

No response

Output from pip freeze

No response

pacrob commented 1 year ago

Regarding the KeyError, in your data dict, types["EIP712Domain"] has name, version, chainId, and verifyingContract, but domain is missing the verifyingContract.

Regarding the Validation Error: Invalid Identifier, it does look like the typing regex doesn't allow for spaces in type names. I don't see why it wouldn't though. I'll get a PR in.

pacrob commented 1 year ago

Update: the current validation is a bit of a mess, so I'm just going to leave it as-is. I'm working on a replacement that will be more in-line with users are used to when signing typed data, should be in soon.