ConsenSysMesh / py-eip712-structs

EIP712 data structure management for python
MIT License
34 stars 26 forks source link

signable_bytes() and to_message_json() throw exceptions #14

Open anomit opened 5 years ago

anomit commented 5 years ago

Code in question:

from eip712_structs import EIP712Struct, Address, String, Uint

class Identity(EIP712Struct):
    userId = Uint(256)
    wallet = Address()

class Message(EIP712Struct):
    actionType = String()
    timestamp = Uint(256)
    authorizer = Identity

class EIP712Domain(EIP712Struct):
    name = String()
    version = String()
    chainId = Uint(256)
    verifyingContract = Address()

verifying_contract_domain = EIP712Domain(
    name='VerifierApp101',
    version='1',
    chainId=8995,
    verifyingContract='0x8c1eD7e19abAa9f23c476dA86Dc1577F1Ef401f5'
)

user_identification = Identity(userId=123, wallet='0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC')
msg = Message(actionType='Action7440', timestamp=1570112162, authorizer=Identity)

print(msg.to_message_json(verifying_contract_domain))
print(msg.signable_bytes(verifying_contract_domain))

msg.to_message_json(verifying_contract_domain) throws the following exception:

Traceback (most recent call last):
  File "submit_proof.py", line 32, in <module>
    print(msg.to_message_json(verifying_contract_domain))
  File "/Users/anomit/.pyenv/versions/ev-cli/lib/python3.6/site-packages/eip712_structs/struct.py", line 188, in to_message_json
    return json.dumps(message, cls=BytesJSONEncoder)
  File "/Users/anomit/.pyenv/versions/3.6.5/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/Users/anomit/.pyenv/versions/3.6.5/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/anomit/.pyenv/versions/3.6.5/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/anomit/.pyenv/versions/ev-cli/lib/python3.6/site-packages/eip712_structs/types.py", line 244, in default
    return super(BytesJSONEncoder, self).default(o)
  File "/Users/anomit/.pyenv/versions/3.6.5/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'OrderedAttributesMeta' is not JSON serializable

msg.signable_bytes(verifying_contract_domain) throws:

Traceback (most recent call last):
  File "submit_proof.py", line 32, in <module>
    print(msg.signable_bytes(verifying_contract_domain))
  File "/Users/anomit/.pyenv/versions/ev-cli/lib/python3.6/site-packages/eip712_structs/struct.py", line 200, in signable_bytes
    result = b'\x19\x01' + domain.hash_struct() + self.hash_struct()
  File "/Users/anomit/.pyenv/versions/ev-cli/lib/python3.6/site-packages/eip712_structs/struct.py", line 132, in hash_struct
    return keccak(b''.join([self.type_hash(), self.encode_value()]))
  File "/Users/anomit/.pyenv/versions/ev-cli/lib/python3.6/site-packages/eip712_structs/struct.py", line 61, in encode_value
    encoded_values.append(sub_struct.hash_struct())
TypeError: hash_struct() missing 1 required positional argument: 'self'
BoboTiG commented 8 months ago

Actually, the issue relies in your code: you do not use the proper authorizer object. To fix it:

- msg = Message(actionType='Action7440', timestamp=1570112162, authorizer=Identity)
+ msg = Message(actionType='Action7440', timestamp=1570112162, authorizer=user_identification)