Normalizes wallet addresses.

This commit is contained in:
PhilipWafula 2021-10-07 16:48:54 +03:00
parent 2babcbdbdb
commit e3a0e8e463
Signed by untrusted user: mango-habanero
GPG Key ID: B00CE9034DA19FB7
5 changed files with 23 additions and 13 deletions

View File

@ -13,7 +13,7 @@ from chainlib.eth.sign import (
new_account, new_account,
sign_message, 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.eth.tx import TxFormat
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
from chainlib.error import JSONRPCException 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.nonce import Nonce
from cic_eth.db.models.base import SessionBase from cic_eth.db.models.base import SessionBase
from cic_eth.db.models.role import AccountRole from cic_eth.db.models.role import AccountRole
from cic_eth.encode import tx_normalize
from cic_eth.error import ( from cic_eth.error import (
RoleMissingError, RoleMissingError,
SignerError, SignerError,
@ -176,6 +177,9 @@ def gift(self, account_address, chain_spec_dict):
""" """
chain_spec = ChainSpec.from_dict(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)) logg.debug('gift account address {} to index'.format(account_address))
queue = self.request.delivery_info.get('routing_key') 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) @celery_app.task(bind=True, base=CriticalSQLAlchemyTask)
def set_role(self, tag, address, chain_spec_dict): def set_role(self, tag, address, chain_spec_dict):
if not to_checksum_address(address): if not is_address(address):
raise ValueError('invalid checksum address {}'.format(address)) raise ValueError('invalid address {}'.format(address))
address = tx_normalize.wallet_address(address)
session = SessionBase.create_session() session = SessionBase.create_session()
role = AccountRole.set(tag, address, session=session) role = AccountRole.set(tag, address, session=session)
session.add(role) session.add(role)

View File

@ -12,6 +12,7 @@ from chainlib.chain import ChainSpec
from chainlib.eth.address import ( from chainlib.eth.address import (
is_checksum_address, is_checksum_address,
to_checksum_address, to_checksum_address,
is_address
) )
from chainlib.connection import RPCConnection from chainlib.connection import RPCConnection
from chainqueue.db.enum import StatusBits 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 rpc_format_address = None
if address != None: if address != None:
if not is_checksum_address(address): if not is_address(address):
raise ValueError('invalid address {}'.format(address)) raise ValueError('invalid address {}'.format(address))
address = tx_normalize.wallet_address(address) address = tx_normalize.wallet_address(address)
address = add_0x(address) address = add_0x(address)

View File

@ -3,11 +3,12 @@ import logging
# external imports # external imports
import celery 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 # local imports
from cic_eth.db.models.role import AccountRole from cic_eth.db.models.role import AccountRole
from cic_eth.db.models.base import SessionBase from cic_eth.db.models.base import SessionBase
from cic_eth.encode import tx_normalize
from cic_eth.task import CriticalSQLAlchemyTask from cic_eth.task import CriticalSQLAlchemyTask
from cic_eth.db.models.nonce import ( from cic_eth.db.models.nonce import (
Nonce, Nonce,
@ -58,7 +59,7 @@ def reserve_nonce(self, chained_input, chain_spec_dict, signer_address=None):
address = chained_input address = chained_input
logg.debug('non-explicit address for reserve nonce, using arg head {}'.format(chained_input)) logg.debug('non-explicit address for reserve nonce, using arg head {}'.format(chained_input))
else: else:
if is_checksum_address(signer_address): if is_address(signer_address):
address = signer_address address = signer_address
logg.debug('explicit address for reserve nonce {}'.format(signer_address)) logg.debug('explicit address for reserve nonce {}'.format(signer_address))
else: 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)) raise ValueError('invalid result when resolving address for nonce {}'.format(address))
root_id = self.request.root_id 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])) logg.debug('nonce {} reserved for address {} task {}'.format(r[1], address, r[0]))
session.commit() session.commit()

View File

@ -231,6 +231,8 @@ def tx_collate(self, tx_batches, chain_spec_dict, offset, limit, newest_first=Tr
except UnknownContractError: except UnknownContractError:
logg.error('verify failed on tx {}, skipping'.format(tx['hash'])) logg.error('verify failed on tx {}, skipping'.format(tx['hash']))
continue continue
tx['recipient'] = tx_normalize.wallet_address(tx['recipient'])
tx['sender'] = tx_normalize.wallet_address(tx['sender'])
txs.append(tx) txs.append(tx)
return txs return txs

View File

@ -21,6 +21,7 @@ from erc20_faucet import Faucet
# local imports # local imports
from .base import SyncFilter from .base import SyncFilter
from cic_eth.eth.meta import ExtendedTx from cic_eth.eth.meta import ExtendedTx
from cic_eth.encode import tx_normalize
logg = logging.getLogger().getChild(__name__) logg = logging.getLogger().getChild(__name__)
@ -42,9 +43,9 @@ class CallbackFilter(SyncFilter):
return (None, None) return (None, None)
r = ERC20.parse_transfer_request(tx.payload) r = ERC20.parse_transfer_request(tx.payload)
transfer_data = {} transfer_data = {}
transfer_data['to'] = r[0] transfer_data['to'] = tx_normalize.wallet_address(r[0])
transfer_data['value'] = r[1] 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] transfer_data['token_address'] = tx.inputs[0]
return ('transfer', transfer_data) return ('transfer', transfer_data)
@ -54,8 +55,8 @@ class CallbackFilter(SyncFilter):
return (None, None) return (None, None)
r = ERC20.parse_transfer_from_request(tx.payload) r = ERC20.parse_transfer_from_request(tx.payload)
transfer_data = {} transfer_data = {}
transfer_data['from'] = r[0] transfer_data['from'] = tx_normalize.wallet_address(r[0])
transfer_data['to'] = r[1] transfer_data['to'] = tx_normalize.wallet_address(r[1])
transfer_data['value'] = r[2] transfer_data['value'] = r[2]
transfer_data['token_address'] = tx.inputs[0] transfer_data['token_address'] = tx.inputs[0]
return ('transferfrom', transfer_data) return ('transferfrom', transfer_data)
@ -66,9 +67,9 @@ class CallbackFilter(SyncFilter):
return (None, None) return (None, None)
r = Faucet.parse_give_to_request(tx.payload) r = Faucet.parse_give_to_request(tx.payload)
transfer_data = {} transfer_data = {}
transfer_data['to'] = r[0] transfer_data['to'] = tx_normalize.wallet_address(r[0])
transfer_data['value'] = tx.value 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] #transfer_data['token_address'] = tx.inputs[0]
faucet_contract = tx.inputs[0] faucet_contract = tx.inputs[0]