Accept none value in build raw for to address

This commit is contained in:
nolash 2021-02-24 22:38:56 +01:00
parent 17431dc000
commit 1f19aecd0e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 63 additions and 4 deletions

View File

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

View File

@ -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)

View File

@ -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__':

View File

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

View File

@ -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

35
tests/test_address.py Normal file
View File

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