From 1f19aecd0e827da1a045b123b779d83745b4ad34 Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 24 Feb 2021 22:38:56 +0100 Subject: [PATCH] Accept none value in build raw for to address --- chainlib/eth/address.py | 20 ++++++++++++++++++++ chainlib/eth/connection.py | 1 + chainlib/eth/runnable/get.py | 7 ++++--- chainlib/eth/tx.py | 2 ++ setup.cfg | 2 +- tests/test_address.py | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/test_address.py diff --git a/chainlib/eth/address.py b/chainlib/eth/address.py index d49ae32..ed5a8cf 100644 --- a/chainlib/eth/address.py +++ b/chainlib/eth/address.py @@ -6,9 +6,29 @@ from hexathon import ( ) +def is_address(address_hex): + try: + address_hex = strip_0x(address_hex) + except ValueError: + return False + return len(address_hex) == 40 + + +def is_checksum_address(address_hex): + hx = None + try: + hx = to_checksum(address_hex) + except ValueError: + return False + print('{} {}'.format(hx, address_hex)) + return hx == address_hex + + def to_checksum(address_hex): address_hex = strip_0x(address_hex) address_hex = uniform(address_hex) + if len(address_hex) != 40: + raise ValueError('Invalid address length') h = sha3.keccak_256() h.update(address_hex.encode('utf-8')) z = h.digest() diff --git a/chainlib/eth/connection.py b/chainlib/eth/connection.py index 2aac2d1..0c21847 100644 --- a/chainlib/eth/connection.py +++ b/chainlib/eth/connection.py @@ -44,6 +44,7 @@ class HTTPConnection: logg.debug('(HTTP) send {}'.format(data)) res = urlopen(req, data=data.encode('utf-8')) o = json.load(res) + logg.debug('(HTTP) recv {}'.format(o)) return jsonrpc_result(o, error_parser) diff --git a/chainlib/eth/runnable/get.py b/chainlib/eth/runnable/get.py index 79e41b4..d926cb4 100644 --- a/chainlib/eth/runnable/get.py +++ b/chainlib/eth/runnable/get.py @@ -73,6 +73,7 @@ def main(): tx = None status = -1 + rcpt = None if tx_src['blockHash'] != None: o = jsonrpc_template() o['method'] = 'eth_getBlockByHash' @@ -88,13 +89,13 @@ def main(): o['method'] = 'eth_getTransactionReceipt' o['params'].append(tx_hash) rcpt = conn.do(o) - status = int(strip_0x(rcpt['status']), 16) + #status = int(strip_0x(rcpt['status']), 16) if tx == None: tx = Tx(tx_src) + if rcpt != None: + tx.apply_receipt(rcpt) print(tx) - status_name = Status(status).name - print('status {}'.format(status_name)) if __name__ == '__main__': diff --git a/chainlib/eth/tx.py b/chainlib/eth/tx.py index 28ff167..ca1b134 100644 --- a/chainlib/eth/tx.py +++ b/chainlib/eth/tx.py @@ -112,6 +112,8 @@ class TxFactory: def build_raw(self, tx): + if tx['to'] == None or tx['to'] == '': + tx['to'] = '0x' txe = EIP155Transaction(tx, tx['nonce'], tx['chainId']) self.signer.signTransaction(txe) tx_raw = txe.rlp_serialize() diff --git a/setup.cfg b/setup.cfg index 0721d6d..c8478e0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = chainlib -version = 0.0.1a18 +version = 0.0.1a19 description = Generic blockchain access library and tooling author = Louis Holbrook author_email = dev@holbrook.no diff --git a/tests/test_address.py b/tests/test_address.py new file mode 100644 index 0000000..693f603 --- /dev/null +++ b/tests/test_address.py @@ -0,0 +1,35 @@ +import unittest + +from chainlib.eth.address import ( + is_address, + is_checksum_address, + to_checksum, + ) + +from tests.base import TestBase + + +class TestChain(TestBase): + + def test_chain_spec(self): + checksum_address = '0xEb3907eCad74a0013c259D5874AE7f22DcBcC95C' + plain_address = checksum_address.lower() + + self.assertEqual(checksum_address, to_checksum(checksum_address)) + + self.assertTrue(is_address(plain_address)) + self.assertFalse(is_checksum_address(plain_address)) + self.assertTrue(is_checksum_address(checksum_address)) + + self.assertFalse(is_address(plain_address + "00")) + self.assertFalse(is_address(plain_address[:len(plain_address)-2])) + + with self.assertRaises(ValueError): + to_checksum(plain_address + "00") + + with self.assertRaises(ValueError): + to_checksum(plain_address[:len(plain_address)-2]) + + +if __name__ == '__main__': + unittest.main()