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 of ethsignTypedData error. TypeError: Value of `chainId` (137) in the struct `EIP712Domain` is of the type `<class 'str'>`, but expected uint256 value #220

Closed snarflakes closed 1 year ago

snarflakes commented 1 year ago

What was wrong?

signature formation for other simpler ethsignTypedData sig requests (wallet queries) are working fine with the same code.

TypeError: Value of chainId (137) in the struct EIP712Domain is of the type <class 'str'>, but expected uint256 value

Code that produced the error

                            if "eth_signTypedData" == method:

                                parameters = parameters[1]
                                print(type(parameters))

parameters = parameters[2:]

                                print(parameters)
                                print("load json")
                                parameters = json.loads(parameters)
                                print(type(parameters))
                                print(parameters)
                                print(parameters['types']['EIP712Domain'])

                                print("eth_signTypedData")
                                message = messages.encode_structured_data(primitive=parameters)
                                print(message)
                                print(type(message))
                                signed_message = Account.sign_message(message, private_key=secretkey)
                                print(signed_message)

print(parameters[0])

                                result = signed_message.signature.hex()

                                print(result)
                                print(type(result))
                                wclient.reply(id_request, result)

CODE_TO_REPRODUCE 

Full error output

<---- Received WalletConnect wallet query : (1678237973673491, 'eth_signTypedData', ['0x827d3e01191de9206ce85197d1e1f2b91ed7251d', '{"types":{"SafeTx":[{"type":"address","name":"to"},{"type":"uint256","name":"value"},{"type":"bytes","name":"data"},{"type":"uint8","name":"operation"},{"type":"uint256","name":"safeTxGas"},{"type":"uint256","name":"baseGas"},{"type":"uint256","name":"gasPrice"},{"type":"address","name":"gasToken"},{"type":"address","name":"refundReceiver"},{"type":"uint256","name":"nonce"}],"EIP712Domain":[{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}]},"domain":{"chainId":"137","verifyingContract":"0x9135a7832db16190475f409b649771532bb6fa7f"},"primaryType":"SafeTx","message":{"to":"0x9135a7832db16190475f409b649771532bb6fa7f","value":"0","data":"0x694e80c30000000000000000000000000000000000000000000000000000000000000001","operation":"0","safeTxGas":"0","baseGas":"0","gasPrice":"0","gasToken":"0x0000000000000000000000000000000000000000","refundReceiver":"0x0000000000000000000000000000000000000000","nonce":"0"}}']) 1678237973673491 <class 'tuple'> <class 'int'> 1678237973673491 eth_signTypedData <class 'list'> ['0x827d3e01191de9206ce85197d1e1f2b91ed7251d', '{"types":{"SafeTx":[{"type":"address","name":"to"},{"type":"uint256","name":"value"},{"type":"bytes","name":"data"},{"type":"uint8","name":"operation"},{"type":"uint256","name":"safeTxGas"},{"type":"uint256","name":"baseGas"},{"type":"uint256","name":"gasPrice"},{"type":"address","name":"gasToken"},{"type":"address","name":"refundReceiver"},{"type":"uint256","name":"nonce"}],"EIP712Domain":[{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}]},"domain":{"chainId":"137","verifyingContract":"0x9135a7832db16190475f409b649771532bb6fa7f"},"primaryType":"SafeTx","message":{"to":"0x9135a7832db16190475f409b649771532bb6fa7f","value":"0","data":"0x694e80c30000000000000000000000000000000000000000000000000000000000000001","operation":"0","safeTxGas":"0","baseGas":"0","gasPrice":"0","gasToken":"0x0000000000000000000000000000000000000000","refundReceiver":"0x0000000000000000000000000000000000000000","nonce":"0"}}'] <class 'str'> {"types":{"SafeTx":[{"type":"address","name":"to"},{"type":"uint256","name":"value"},{"type":"bytes","name":"data"},{"type":"uint8","name":"operation"},{"type":"uint256","name":"safeTxGas"},{"type":"uint256","name":"baseGas"},{"type":"uint256","name":"gasPrice"},{"type":"address","name":"gasToken"},{"type":"address","name":"refundReceiver"},{"type":"uint256","name":"nonce"}],"EIP712Domain":[{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}]},"domain":{"chainId":"137","verifyingContract":"0x9135a7832db16190475f409b649771532bb6fa7f"},"primaryType":"SafeTx","message":{"to":"0x9135a7832db16190475f409b649771532bb6fa7f","value":"0","data":"0x694e80c30000000000000000000000000000000000000000000000000000000000000001","operation":"0","safeTxGas":"0","baseGas":"0","gasPrice":"0","gasToken":"0x0000000000000000000000000000000000000000","refundReceiver":"0x0000000000000000000000000000000000000000","nonce":"0"}} load json <class 'dict'> {'types': {'SafeTx': [{'type': 'address', 'name': 'to'}, {'type': 'uint256', 'name': 'value'}, {'type': 'bytes', 'name': 'data'}, {'type': 'uint8', 'name': 'operation'}, {'type': 'uint256', 'name': 'safeTxGas'}, {'type': 'uint256', 'name': 'baseGas'}, {'type': 'uint256', 'name': 'gasPrice'}, {'type': 'address', 'name': 'gasToken'}, {'type': 'address', 'name': 'refundReceiver'}, {'type': 'uint256', 'name': 'nonce'}], 'EIP712Domain': [{'name': 'chainId', 'type': 'uint256'}, {'name': 'verifyingContract', 'type': 'address'}]}, 'domain': {'chainId': '137', 'verifyingContract': '0x9135a7832db16190475f409b649771532bb6fa7f'}, 'primaryType': 'SafeTx', 'message': {'to': '0x9135a7832db16190475f409b649771532bb6fa7f', 'value': '0', 'data': '0x694e80c30000000000000000000000000000000000000000000000000000000000000001', 'operation': '0', 'safeTxGas': '0', 'baseGas': '0', 'gasPrice': '0', 'gasToken': '0x0000000000000000000000000000000000000000', 'refundReceiver': '0x0000000000000000000000000000000000000000', 'nonce': '0'}} [{'name': 'chainId', 'type': 'uint256'}, {'name': 'verifyingContract', 'type': 'address'}] eth_signTypedData Traceback (most recent call last): File "imageidopen.py", line 1008, in qr_capture message = messages.encode_structured_data(primitive=parameters) File "/usr/local/lib/python3.7/dist-packages/eth_account/messages.py", line 152, in encode_structured_data hash_domain(structured_data), File "/usr/local/lib/python3.7/dist-packages/eth_account/_utils/structured_data/hashing.py", line 284, in hash_domain structured_data["domain"] File "/usr/local/lib/python3.7/dist-packages/eth_account/_utils/structured_data/hashing.py", line 267, in encode_data data_types_and_hashes = _encode_data(primaryType, types, data) File "/home/pi/.local/lib/python3.7/site-packages/eth_utils/functional.py", line 45, in inner return callback(fn(*args, **kwargs)) File "/usr/local/lib/python3.7/dist-packages/eth_account/_utils/structured_data/hashing.py", line 261, in _encode_data field["type"], TypeError: Value of chainId (137) in the struct EIP712Domain is of the type <class 'str'>, but expected uint256 value

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/local/lib/python3.7/dist-packages/gpiozero/pins/rpigpio.py", line 223, in _call_when_changed super(RPiGPIOPin, self)._call_when_changed() File "/usr/local/lib/python3.7/dist-packages/gpiozero/pins/local.py", line 130, in _call_when_changed self.state if state is None else state) File "/usr/local/lib/python3.7/dist-packages/gpiozero/pins/pi.py", line 298, in _call_when_changed method(ticks, state) File "/usr/local/lib/python3.7/dist-packages/gpiozero/input_devices.py", line 182, in _pin_changed self._fire_events(ticks, bool(self._state_to_value(state))) File "/usr/local/lib/python3.7/dist-packages/gpiozero/mixins.py", line 401, in _fire_events self._fire_activated() File "/usr/local/lib/python3.7/dist-packages/gpiozero/mixins.py", line 447, in _fire_activated super(HoldMixin, self)._fire_activated() File "/usr/local/lib/python3.7/dist-packages/gpiozero/mixins.py", line 364, in _fire_activated self.when_activated() File "imageidopen.py", line 317, in push_button2 qr_capture() File "imageidopen.py", line 1075, in qr_capture except pywalletconnect.client.WCClientException: NameError: name 'pywalletconnect' is not defined

ERROR_HERE 

Expected Result

valid signature

(valid signature is created with this walletconnect query eth_signedTypeData using same code)

{'types': {'Alias': [{'name': 'from', 'type': 'address'}, {'name': 'alias', 'type': 'address'}], 'EIP712Domain': [{'name': 'name', 'type': 'string'}, {'name': 'version', 'type': 'string'}]}, 'domain': {'name': 'snapshot', 'version': '0.1.4'}, 'primaryType': 'Alias', 'message': {'from': '0x827d3e01191de9206ce85197d1e1f2b91ed7251d', 'alias': '0xea6f8c18d8dee98346eb1328f30c6406d28dee7a'}} [{'name': 'name', 'type': 'string'}, {'name': 'version', 'type': 'string'}]

EXPECTED_RESULT

Environment

version 0.5.9

# run this:
$ python -m eth_utils

# then copy the output here:
OUTPUT_HERE

How can it be fixed?

some encoding error?

fselmo commented 1 year ago

This is the error

TypeError: Value of chainId (137) in the struct EIP712Domain is of the type <class 'str'>, but expected uint256 value

It is expecting a python int for the chainid.

fselmo commented 1 year ago

I closed this for now as it doesn't seem to be an issue with the expected behavior. If I missed something or there's a particular feature request (a request to modify the expected behavior) in here somewhere please feel free to clarify / start a conversation on that instead. Best of luck!

snarflakes commented 1 year ago

Thanks for the help! I got rid of that one, and a new one popped up (below) for another piece of that same dictionary. Am I missing a strategy of building most of these dapp delivered ethsignedTypedData signatures request easily? Or do I have to manually correct each of these fields for the signature to be successfully encoded for the dapp to accept? The one I've been working with here is a gnosis Safe signature, perhaps these are more complicated sigs.

new error TypeError: Value of value (0) is not encodable as type uint256. If the base type is correct, verify that the value does not exceed the specified size for the type.

snarflakes commented 1 year ago

actually I think this PR is solving my exact issues with ethsignedtypeddata signatures!!!

"add TypedDataConvert to message #221"