py-eip712-structs icon indicating copy to clipboard operation
py-eip712-structs copied to clipboard

signable_bytes() and to_message_json() throw exceptions

Open anomit opened this issue 6 years ago • 1 comments

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'

anomit avatar Oct 03 '19 14:10 anomit

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)

BoboTiG avatar Jan 12 '24 08:01 BoboTiG