commit c77c0a615f0235baa0c1a7155a471f643f5a4bd8
parent 6b768b41571898f9281fa97dd143a3c3082760ca
Author: lash <dev@holbrook.no>
Date: Wed, 29 Mar 2023 10:43:14 +0100
Add chainlib generated tuple abi test
Diffstat:
1 file changed, 74 insertions(+), 1 deletion(-)
diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py
@@ -67,7 +67,7 @@ class TestERC712(TestERC712Base):
print(r.hex())
- def test_mail(self):
+ def test_mail_manual(self):
a = os.urandom(20)
b = os.urandom(20)
mail_from_name = 'Pinky Inky'
@@ -145,5 +145,78 @@ class TestERC712(TestERC712Base):
self.assertEqual(int(r, 16), 1)
+ def test_mail_chainlib_abi(self):
+ a = os.urandom(20)
+ b = os.urandom(20)
+ mail_from_name = 'Pinky Inky'
+ mail_from_address = a.hex()
+ mail_to_name = 'Clyde Blinky'
+ mail_to_address = b.hex()
+ mail_contents = 'barbarbar'
+
+ mail = ExampleMail(
+ mail_from_name,
+ mail_from_address,
+ mail_to_name,
+ mail_to_address,
+ mail_contents,
+ self.domain,
+ )
+ sig = self.signer.sign_typed_message(self.accounts[0], mail.get_domain(), mail.get_data_hash())
+ sig = sig[:64] + (sig[64] + 27).to_bytes(1, byteorder='big')
+ logg.debug('message is:\n{}\nsigned by {}'.format(mail, self.accounts[0]))
+
+ logg.debug('encode data from')
+ enc_from = ABIContractEncoder()
+ enc_from.typ(ABIContractType.STRING)
+ enc_from.typ(ABIContractType.ADDRESS)
+ enc_from.string(mail_from_name)
+ enc_from.address(mail_from_address)
+
+ logg.debug('encode data to')
+ enc_to = ABIContractEncoder()
+ enc_to.typ(ABIContractType.STRING)
+ enc_to.typ(ABIContractType.ADDRESS)
+ enc_to.string(mail_to_name)
+ enc_to.address(mail_to_address)
+
+ logg.debug('encode data contents')
+ enc_mail = ABIContractEncoder()
+ enc_mail.typ(enc_from)
+ enc_mail.typ(enc_to)
+ enc_mail.typ(ABIContractType.STRING)
+ enc_mail.tuple(enc_from)
+ enc_mail.tuple(enc_to)
+ enc_mail.string(mail_contents)
+
+ logg.debug('encode complete calldata')
+ c = TxFactory(self.chain_spec)
+ j = JSONRPCRequest()
+ o = j.template()
+ o['method'] = 'eth_call'
+ enc = ABIContractEncoder()
+ enc.method('verify')
+ enc.typ(enc_mail)
+ enc.typ(ABIContractType.UINT8)
+ enc.typ(ABIContractType.BYTES32)
+ enc.typ(ABIContractType.BYTES32)
+ enc.tuple(enc_mail)
+ enc.uintn(sig[64], 8)
+ enc.bytes32(sig[:32])
+ enc.bytes32(sig[32:64])
+ data = enc.get()
+ for i in range(8, len(data), 64):
+ logg.info('calldata {} {}'.format((i-8).to_bytes(2, byteorder='big').hex(), data[i:i+64]))
+ data = add_0x(data)
+ tx = c.template(self.accounts[0], self.address)
+ tx = c.set_code(tx, data)
+ o['params'].append(c.normalize(tx))
+ o['params'].append('latest')
+ o = j.finalize(o)
+ r = self.rpc.do(o)
+ r = strip_0x(r)
+ self.assertEqual(int(r, 16), 1)
+
+
if __name__ == '__main__':
unittest.main()