chainlib-eth

Ethereum implementation of the chainlib interface
Log | Files | Refs | README | LICENSE

commit 43ebc8d99206dbaf21c4f34c908ff3e86b2749de
parent c0cc324aca7cc0193adb8e4ad18c6f2da2e78c6d
Author: lash <dev@holbrook.no>
Date:   Sat, 18 Feb 2023 18:52:46 +0000

Add bytes encoding

Diffstat:
MCHANGELOG | 2++
Mchainlib/eth/contract.py | 16+++++++++++++---
Msetup.cfg | 2+-
Mtests/test_abi.py | 16++++++++++++++++
4 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +- 0.4.16 + * Add dynamic bytes encoding in contracts module - 0.4.15 * Enable setting of unsigned sender address for contract calls - 0.4.14 diff --git a/chainlib/eth/contract.py b/chainlib/eth/contract.py @@ -429,11 +429,21 @@ class ABIContractEncoder(ABIMethodEncoder): :type v: str """ b = v.encode('utf-8') - l = len(b) + return self._bytes(b, pad=True) + + + def bytes(self, v): + b = bytes.fromhex(v) + return self._bytes(b, pad=True) + + + def _bytes(self, v, pad=False): + l = len(v) contents = l.to_bytes(32, 'big') - contents += b + contents += v padlen = 32 - (l % 32) - contents += padlen * b'\x00' + if pad: + contents += padlen * b'\x00' self.bytes_fixed(len(contents), contents) self.types.append(ABIContractType.STRING) self.__log_latest(v) diff --git a/setup.cfg b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = chainlib-eth -version = 0.4.15 +version = 0.4.16 description = Ethereum implementation of the chainlib interface author = Louis Holbrook author_email = dev@holbrook.no diff --git a/tests/test_abi.py b/tests/test_abi.py @@ -50,5 +50,21 @@ class TestContract(unittest.TestCase): self.assertEqual(e.get(), '5e260038000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000029a0000000000000000000000000000000000000000000000000000000000000539') + def test_abi_bytes(self): + e = ABIContractEncoder() + e.bytes('deadbeef') + e.method('foo') + e.typ(ABIContractType.BYTES) + self.assertEqual(e.get(), '30c8d1da00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004deadbeef00000000000000000000000000000000000000000000000000000000') + + + def test_abi_string(self): + e = ABIContractEncoder() + e.string('deadbeef') + e.method('foo') + e.typ(ABIContractType.STRING) + self.assertEqual(e.get(), 'f31a6969000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000086465616462656566000000000000000000000000000000000000000000000000') + + if __name__ == '__main__': unittest.main()