From e3a0e8e4633fc52c553e585a5e805b6f37a8caf1 Mon Sep 17 00:00:00 2001 From: PhilipWafula Date: Thu, 7 Oct 2021 16:48:54 +0300 Subject: [PATCH] Normalizes wallet addresses. --- apps/cic-eth/cic_eth/eth/account.py | 11 ++++++++--- apps/cic-eth/cic_eth/eth/gas.py | 3 ++- apps/cic-eth/cic_eth/eth/nonce.py | 7 ++++--- apps/cic-eth/cic_eth/ext/tx.py | 2 ++ .../cic_eth/runnable/daemons/filters/callback.py | 13 +++++++------ 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/apps/cic-eth/cic_eth/eth/account.py b/apps/cic-eth/cic_eth/eth/account.py index 405b08c0..9f4916c7 100644 --- a/apps/cic-eth/cic_eth/eth/account.py +++ b/apps/cic-eth/cic_eth/eth/account.py @@ -13,7 +13,7 @@ from chainlib.eth.sign import ( new_account, sign_message, ) -from chainlib.eth.address import to_checksum_address +from chainlib.eth.address import to_checksum_address, is_address from chainlib.eth.tx import TxFormat from chainlib.chain import ChainSpec from chainlib.error import JSONRPCException @@ -31,6 +31,7 @@ from cic_eth.eth.gas import ( from cic_eth.db.models.nonce import Nonce from cic_eth.db.models.base import SessionBase from cic_eth.db.models.role import AccountRole +from cic_eth.encode import tx_normalize from cic_eth.error import ( RoleMissingError, SignerError, @@ -176,6 +177,9 @@ def gift(self, account_address, chain_spec_dict): """ chain_spec = ChainSpec.from_dict(chain_spec_dict) + if is_address(account_address): + account_address = tx_normalize.wallet_address(account_address) + logg.debug('gift account address {} to index'.format(account_address)) queue = self.request.delivery_info.get('routing_key') @@ -249,8 +253,9 @@ def have(self, account, chain_spec_dict): @celery_app.task(bind=True, base=CriticalSQLAlchemyTask) def set_role(self, tag, address, chain_spec_dict): - if not to_checksum_address(address): - raise ValueError('invalid checksum address {}'.format(address)) + if not is_address(address): + raise ValueError('invalid address {}'.format(address)) + address = tx_normalize.wallet_address(address) session = SessionBase.create_session() role = AccountRole.set(tag, address, session=session) session.add(role) diff --git a/apps/cic-eth/cic_eth/eth/gas.py b/apps/cic-eth/cic_eth/eth/gas.py index 7e32bf04..86e8c3a5 100644 --- a/apps/cic-eth/cic_eth/eth/gas.py +++ b/apps/cic-eth/cic_eth/eth/gas.py @@ -12,6 +12,7 @@ from chainlib.chain import ChainSpec from chainlib.eth.address import ( is_checksum_address, to_checksum_address, + is_address ) from chainlib.connection import RPCConnection from chainqueue.db.enum import StatusBits @@ -184,7 +185,7 @@ def check_gas(self, tx_hashes_hex, chain_spec_dict, txs_hex=[], address=None, ga """ rpc_format_address = None if address != None: - if not is_checksum_address(address): + if not is_address(address): raise ValueError('invalid address {}'.format(address)) address = tx_normalize.wallet_address(address) address = add_0x(address) diff --git a/apps/cic-eth/cic_eth/eth/nonce.py b/apps/cic-eth/cic_eth/eth/nonce.py index cfc49bba..7d1c1338 100644 --- a/apps/cic-eth/cic_eth/eth/nonce.py +++ b/apps/cic-eth/cic_eth/eth/nonce.py @@ -3,11 +3,12 @@ import logging # external imports import celery -from chainlib.eth.address import is_checksum_address, is_address +from chainlib.eth.address import is_checksum_address, is_address, strip_0x # local imports from cic_eth.db.models.role import AccountRole from cic_eth.db.models.base import SessionBase +from cic_eth.encode import tx_normalize from cic_eth.task import CriticalSQLAlchemyTask from cic_eth.db.models.nonce import ( Nonce, @@ -58,7 +59,7 @@ def reserve_nonce(self, chained_input, chain_spec_dict, signer_address=None): address = chained_input logg.debug('non-explicit address for reserve nonce, using arg head {}'.format(chained_input)) else: - if is_checksum_address(signer_address): + if is_address(signer_address): address = signer_address logg.debug('explicit address for reserve nonce {}'.format(signer_address)) else: @@ -69,7 +70,7 @@ def reserve_nonce(self, chained_input, chain_spec_dict, signer_address=None): raise ValueError('invalid result when resolving address for nonce {}'.format(address)) root_id = self.request.root_id - r = NonceReservation.next(address, root_id, session=session) + r = NonceReservation.next(tx_normalize.wallet_address(address), root_id, session=session) logg.debug('nonce {} reserved for address {} task {}'.format(r[1], address, r[0])) session.commit() diff --git a/apps/cic-eth/cic_eth/ext/tx.py b/apps/cic-eth/cic_eth/ext/tx.py index ebe24fef..62f44d2d 100644 --- a/apps/cic-eth/cic_eth/ext/tx.py +++ b/apps/cic-eth/cic_eth/ext/tx.py @@ -231,6 +231,8 @@ def tx_collate(self, tx_batches, chain_spec_dict, offset, limit, newest_first=Tr except UnknownContractError: logg.error('verify failed on tx {}, skipping'.format(tx['hash'])) continue + tx['recipient'] = tx_normalize.wallet_address(tx['recipient']) + tx['sender'] = tx_normalize.wallet_address(tx['sender']) txs.append(tx) return txs diff --git a/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py b/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py index 4852ab65..c834d053 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/filters/callback.py @@ -21,6 +21,7 @@ from erc20_faucet import Faucet # local imports from .base import SyncFilter from cic_eth.eth.meta import ExtendedTx +from cic_eth.encode import tx_normalize logg = logging.getLogger().getChild(__name__) @@ -42,9 +43,9 @@ class CallbackFilter(SyncFilter): return (None, None) r = ERC20.parse_transfer_request(tx.payload) transfer_data = {} - transfer_data['to'] = r[0] + transfer_data['to'] = tx_normalize.wallet_address(r[0]) transfer_data['value'] = r[1] - transfer_data['from'] = tx.outputs[0] + transfer_data['from'] = tx_normalize.wallet_address(tx.outputs[0]) transfer_data['token_address'] = tx.inputs[0] return ('transfer', transfer_data) @@ -54,8 +55,8 @@ class CallbackFilter(SyncFilter): return (None, None) r = ERC20.parse_transfer_from_request(tx.payload) transfer_data = {} - transfer_data['from'] = r[0] - transfer_data['to'] = r[1] + transfer_data['from'] = tx_normalize.wallet_address(r[0]) + transfer_data['to'] = tx_normalize.wallet_address(r[1]) transfer_data['value'] = r[2] transfer_data['token_address'] = tx.inputs[0] return ('transferfrom', transfer_data) @@ -66,9 +67,9 @@ class CallbackFilter(SyncFilter): return (None, None) r = Faucet.parse_give_to_request(tx.payload) transfer_data = {} - transfer_data['to'] = r[0] + transfer_data['to'] = tx_normalize.wallet_address(r[0]) transfer_data['value'] = tx.value - transfer_data['from'] = tx.outputs[0] + transfer_data['from'] = tx_normalize.wallet_address(tx.outputs[0]) #transfer_data['token_address'] = tx.inputs[0] faucet_contract = tx.inputs[0]