-
Notifications
You must be signed in to change notification settings - Fork 175
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
Add EIP712 support for bytes and uint formatted as string #427
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,14 @@ def _encode_field(name, typ, value): | |
if value is None: | ||
raise Exception(f"Missing value for field {name} of type {type}") | ||
|
||
# Accept string bytes | ||
if "bytes" in typ and isinstance(value, str): | ||
value = bytes.fromhex(value.replace("0x", "")) | ||
|
||
# Accept string uint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should support any kind of integer, not only
|
||
if "uint" in typ and isinstance(value, str): | ||
value = int(value) | ||
|
||
if typ == "bytes": | ||
return ["bytes32", fast_keccak(value)] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,43 @@ | |
|
||
|
||
class TestEIP712(TestCase): | ||
address = "0x8e12f01dae5fe7f1122dc42f2cb084f2f9e8aa03" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use checksummed addresses unless we are testing not checksummed addresses:
|
||
types = { | ||
"EIP712Domain": [ | ||
{"name": "name", "type": "string"}, | ||
{"name": "version", "type": "string"}, | ||
{"name": "chainId", "type": "uint256"}, | ||
{"name": "verifyingContract", "type": "address"}, | ||
], | ||
"Mailbox": [ | ||
{"name": "owner", "type": "address"}, | ||
{"name": "messages", "type": "Message[]"}, | ||
], | ||
"Message": [ | ||
{"name": "sender", "type": "address"}, | ||
{"name": "subject", "type": "string"}, | ||
{"name": "isSpam", "type": "bool"}, | ||
{"name": "body", "type": "string"}, | ||
], | ||
} | ||
|
||
msgs = [ | ||
{ | ||
"sender": address, | ||
"subject": "Hello World", | ||
"body": "The sparrow flies at midnight.", | ||
"isSpam": False, | ||
}, | ||
{ | ||
"sender": address, | ||
"subject": "You may have already Won! :dumb-emoji:", | ||
"body": "Click here for sweepstakes!", | ||
"isSpam": True, | ||
}, | ||
] | ||
|
||
mailbox = {"owner": address, "messages": msgs} | ||
|
||
def test_eip712_encode_hash(self): | ||
for value in [ | ||
{}, | ||
|
@@ -12,8 +49,6 @@ def test_eip712_encode_hash(self): | |
with self.assertRaises(ValueError): | ||
eip712_encode_hash(value) | ||
|
||
address = "0x8e12f01dae5fe7f1122dc42f2cb084f2f9e8aa03" | ||
|
||
wrong_types = { | ||
"EIP712Domain": [ | ||
{"name": "name", "type": "stringa"}, | ||
|
@@ -33,59 +68,69 @@ def test_eip712_encode_hash(self): | |
], | ||
} | ||
|
||
types = { | ||
"EIP712Domain": [ | ||
{"name": "name", "type": "string"}, | ||
{"name": "version", "type": "string"}, | ||
{"name": "chainId", "type": "uint256"}, | ||
{"name": "verifyingContract", "type": "address"}, | ||
], | ||
"Mailbox": [ | ||
{"name": "owner", "type": "address"}, | ||
{"name": "messages", "type": "Message[]"}, | ||
], | ||
"Message": [ | ||
{"name": "sender", "type": "address"}, | ||
{"name": "subject", "type": "string"}, | ||
{"name": "isSpam", "type": "bool"}, | ||
{"name": "body", "type": "string"}, | ||
], | ||
} | ||
|
||
msgs = [ | ||
{ | ||
"sender": address, | ||
"subject": "Hello World", | ||
"body": "The sparrow flies at midnight.", | ||
"isSpam": False, | ||
}, | ||
{ | ||
"sender": address, | ||
"subject": "You may have already Won! :dumb-emoji:", | ||
"body": "Click here for sweepstakes!", | ||
"isSpam": True, | ||
}, | ||
] | ||
|
||
mailbox = {"owner": address, "messages": msgs} | ||
|
||
payload = { | ||
"types": wrong_types, | ||
"primaryType": "Mailbox", | ||
"domain": { | ||
"name": "MyDApp", | ||
"version": "3.0", | ||
"chainId": 41, | ||
"verifyingContract": address, | ||
"verifyingContract": self.address, | ||
}, | ||
"message": mailbox, | ||
"message": self.mailbox, | ||
} | ||
|
||
with self.assertRaises(ValueError): | ||
eip712_encode_hash(payload) | ||
|
||
payload["types"] = types | ||
payload["types"] = self.types | ||
self.assertEqual( | ||
eip712_encode_hash(payload), | ||
b"\xd5N\xcbf7\xfa\x99\n\xae\x02\x86\xd4 \xacpe\x8d\xb9\x95\xaem\t\xcc\x9b\xb1\xda\xcf6J\x14\x17\xd0", | ||
) | ||
|
||
def test_eip712_encode_hash_string_uint(self): | ||
# test string uint (chainId) | ||
payload = { | ||
"types": self.types, | ||
"primaryType": "Mailbox", | ||
"domain": { | ||
"name": "MyDApp", | ||
"version": "3.0", | ||
"chainId": "41", | ||
"verifyingContract": self.address, | ||
}, | ||
"message": self.mailbox, | ||
} | ||
|
||
self.assertEqual( | ||
eip712_encode_hash(payload), | ||
b"\xd5N\xcbf7\xfa\x99\n\xae\x02\x86\xd4 \xacpe\x8d\xb9\x95\xaem\t\xcc\x9b\xb1\xda\xcf6J\x14\x17\xd0", | ||
) | ||
|
||
def test_eip712_encode_hash_string_bytes(self): | ||
types_with_bytes = { | ||
"EIP712Domain": [ | ||
{"name": "name", "type": "string"}, | ||
], | ||
"Message": [ | ||
{"name": "oneByte", "type": "bytes1"}, | ||
{"name": "maxByte", "type": "bytes32"}, | ||
], | ||
} | ||
payload = { | ||
"types": types_with_bytes, | ||
"primaryType": "Message", | ||
"domain": { | ||
"name": "MyDApp", | ||
}, | ||
"message": { | ||
"oneByte": "0x01", | ||
"maxByte": "0x6214da6089b8d8aaa6e6268977746aa0af19fd1ef5d56e225bb3390a697c3ec1", | ||
}, | ||
} | ||
|
||
self.assertEqual( | ||
eip712_encode_hash(payload).hex(), | ||
"2950cf06416c6c20059f24a965e3baf51a24f4ef49a1e7b1a47ee13ee08cde1f", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Your suggestions sounds good for me, then I add the following PR #437 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, thanks |
||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value = HexBytes(value)