2021-02-17 09:19:42 +01:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# third-party imports
|
|
|
|
import celery
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
from chainlib.connection import RPCConnection
|
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
from cic_eth_registry import CICRegistry
|
|
|
|
from eth_address_declarator import AddressDeclarator
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic_eth.task import BaseTask
|
2021-02-17 09:19:42 +01:00
|
|
|
|
|
|
|
celery_app = celery.current_app
|
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
def translate_address(address, trusted_addresses, chain_spec, sender_address=ZERO_ADDRESS):
|
|
|
|
|
|
|
|
rpc = RPCConnection.connect(chain_spec, 'default')
|
|
|
|
registry = CICRegistry(chain_spec, rpc)
|
|
|
|
|
|
|
|
declarator_address = registry.by_name('AddressDeclarator', sender_address=sender_address)
|
2021-04-06 10:17:43 +02:00
|
|
|
c = AddressDeclarator(chain_spec)
|
2021-03-29 15:27:53 +02:00
|
|
|
|
2021-02-17 09:19:42 +01:00
|
|
|
for trusted_address in trusted_addresses:
|
2021-03-29 15:27:53 +02:00
|
|
|
o = c.declaration(declarator_address, trusted_address, address, sender_address=sender_address)
|
|
|
|
r = rpc.do(o)
|
|
|
|
declaration_hex = AddressDeclarator.parse_declaration(r)
|
|
|
|
declaration_hex = declaration_hex[0].rstrip('0')
|
|
|
|
declaration_bytes = bytes.fromhex(declaration_hex)
|
2021-02-17 09:19:42 +01:00
|
|
|
declaration = None
|
|
|
|
try:
|
|
|
|
declaration = declaration_bytes.decode('utf-8', errors='strict')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
continue
|
|
|
|
return declaration
|
|
|
|
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
@celery_app.task(bind=True, base=BaseTask)
|
|
|
|
def translate_tx_addresses(self, tx, trusted_addresses, chain_spec_dict):
|
2021-02-17 09:19:42 +01:00
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_spec = ChainSpec.from_dict(chain_spec_dict)
|
2021-02-17 09:19:42 +01:00
|
|
|
|
|
|
|
declaration = None
|
|
|
|
if tx['sender_label'] == None:
|
2021-03-29 15:27:53 +02:00
|
|
|
declaration = translate_address(tx['sender'], trusted_addresses, chain_spec, self.call_address)
|
2021-02-17 09:19:42 +01:00
|
|
|
tx['sender_label'] = declaration
|
|
|
|
|
|
|
|
declaration = None
|
|
|
|
if tx['recipient_label'] == None:
|
2021-03-29 15:27:53 +02:00
|
|
|
declaration = translate_address(tx['recipient'], trusted_addresses, chain_spec, self.call_address)
|
2021-02-17 09:19:42 +01:00
|
|
|
tx['recipient_label'] = declaration
|
|
|
|
|
|
|
|
return tx
|