chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit 1f19aecd0e827da1a045b123b779d83745b4ad34
parent 17431dc0008c7c6355cd0d127a24e20299203dcc
Author: nolash <dev@holbrook.no>
Date:   Wed, 24 Feb 2021 22:38:56 +0100

Accept none value in build raw for to address

Diffstat:
Mchainlib/eth/address.py | 20++++++++++++++++++++
Mchainlib/eth/connection.py | 1+
Mchainlib/eth/runnable/get.py | 7++++---
Mchainlib/eth/tx.py | 2++
Msetup.cfg | 2+-
Atests/test_address.py | 35+++++++++++++++++++++++++++++++++++
6 files changed, 63 insertions(+), 4 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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()