eth-address-index/python/eth_address_declarator/runnable/view.py

111 lines
2.8 KiB
Python
Raw Normal View History

2021-01-05 14:22:34 +01:00
"""View address declaration, and attempt to resolve contents
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
import os
import logging
2021-07-30 20:12:26 +02:00
import sys
# external imports
import chainlib.eth.cli
from chainlib.chain import ChainSpec
from chainlib.error import JSONRPCException
from chainlib.eth.address import to_checksum_address
2023-02-25 21:27:46 +01:00
from chainlib.eth.constant import ZERO_ADDRESS
2021-07-30 20:12:26 +02:00
from hexathon import (
add_0x,
strip_0x,
)
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
from chainlib.eth.cli.log import process_log
from chainlib.eth.settings import process_settings
from chainlib.settings import ChainSettings
2021-07-30 20:12:26 +02:00
# local imports
from eth_address_declarator import Declarator
from eth_address_declarator.declarator import AddressDeclarator
2021-01-05 14:22:34 +01:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
def process_config_local(config, arg, args, flags):
a = strip_0x(config.get('_POSARG'))
ac = to_checksum_address(a)
if config.true('_UNSAFE'):
a = ac
else:
if a != ac:
raise ValueError('declarator is not a valid checksum address')
config.add(a, '_DECLARATOR')
return config
arg_flags = ArgFlag()
arg = Arg(arg_flags)
2023-02-25 21:27:46 +01:00
flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.EXEC | arg_flags.SENDER
2021-07-30 20:12:26 +02:00
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
argparser.add_argument('declarator', type=str, help='Ethereum declaration address to look up')
args = argparser.parse_args()
2021-07-30 20:12:26 +02:00
logg = process_log(args, logg)
2021-01-05 14:22:34 +01:00
config = Config()
config = process_config(config, arg, args, flags, positional_name='declarator')
config = process_config_local(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
2021-01-05 14:22:34 +01:00
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
2022-04-20 23:23:35 +02:00
2021-01-05 14:22:34 +01:00
2021-07-30 20:12:26 +02:00
def out_element(e, w=sys.stdout):
w.write(e[1] + '\n')
2021-01-05 14:22:34 +01:00
2021-07-30 20:12:26 +02:00
2023-02-25 21:27:46 +01:00
def ls(ifc, conn, contract_address, declarator_address, subject_address, w=sys.stdout, sender_address=ZERO_ADDRESS):
o = ifc.declaration(contract_address, declarator_address, subject_address, sender_address=sender_address)
2021-07-30 20:12:26 +02:00
r = conn.do(o)
declarations = ifc.parse_declaration(r)
for i, d in enumerate(declarations):
out_element((i, d), w)
2021-01-10 22:03:26 +01:00
2021-01-05 14:22:34 +01:00
def main():
c = Declarator(
settings.get('CHAIN_SPEC')
)
2021-07-30 20:12:26 +02:00
ls(
c,
settings.get('CONN'),
settings.get('EXEC'),
config.get('_DECLARATOR'),
settings.get('RECIPIENT'),
2023-02-25 21:27:46 +01:00
sender_address=settings.get('SENDER_ADDRESS'),
)
2021-07-30 20:12:26 +02:00
declarations = []
2021-01-05 14:22:34 +01:00
if __name__ == '__main__':
main()