accounts-index

Accounts index evm contract tooling with permissioned writes
Log | Files | Refs

list.py (3037B)


      1 """Query account index state
      2 
      3 .. moduleauthor:: Louis Holbrook <dev@holbrook.no>
      4 .. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 
      5 
      6 """
      7 
      8 # standard imports
      9 import sys
     10 import os
     11 import json
     12 import argparse
     13 import logging
     14 
     15 # external imports
     16 import chainlib.eth.cli
     17 from chainlib.chain import ChainSpec
     18 from chainlib.eth.connection import EthHTTPConnection
     19 from chainlib.eth.tx import receipt
     20 from chainlib.eth.constant import ZERO_CONTENT
     21 from chainlib.error import JSONRPCException
     22 from chainlib.eth.cli.arg import (
     23         Arg,
     24         ArgFlag,
     25         process_args,
     26         )
     27 from chainlib.eth.cli.config import (
     28         Config,
     29         process_config,
     30         )
     31 from chainlib.eth.cli.log import process_log
     32 from chainlib.eth.settings import process_settings
     33 from chainlib.settings import ChainSettings
     34 from chainlib.eth.address import to_checksum_address
     35 
     36 # local imports
     37 from eth_accounts_index import AccountsIndex
     38 from eth_accounts_index.registry import AccountRegistry
     39 
     40 logging.basicConfig(level=logging.WARNING)
     41 logg = logging.getLogger()
     42 
     43 
     44 def process_config_local(config, arg, args, flags):
     45     address = config.get('_POSARG')
     46     if address != None:
     47         address = to_checksum_address(address)
     48     config.add(address, '_ADDRESS')
     49     return config
     50 
     51 
     52 arg_flags = ArgFlag()
     53 arg = Arg(arg_flags)
     54 flags = arg_flags.STD_READ | arg_flags.EXEC
     55 
     56 argparser = chainlib.eth.cli.ArgumentParser()
     57 argparser = process_args(argparser, arg, flags)
     58 argparser.add_argument('address', type=str, help='Address to add to registry')
     59 args = argparser.parse_args()
     60 
     61 logg = process_log(args, logg)
     62 
     63 config = Config()
     64 config = process_config(config, arg, args, flags, positional_name='address')
     65 config = process_config_local(config, arg, args, flags)
     66 logg.debug('config loaded:\n{}'.format(config))
     67 
     68 settings = ChainSettings()
     69 settings = process_settings(settings, config)
     70 logg.debug('settings loaded:\n{}'.format(settings))
     71 
     72 
     73 def out_element(e, w=sys.stdout):
     74     w.write(str(e[1]) + '\n')
     75 
     76 
     77 def element(ifc, conn, contract_address, address, w=sys.stdout):
     78     o = ifc.have(contract_address, address)
     79     r =  conn.do(o)
     80     have = ifc.parse_have(r)
     81     out_element((0, address), w)
     82 
     83 
     84 def ls(ifc, conn, contract_address, w=sys.stdout):
     85     i = 0
     86     while True:
     87         o = ifc.entry(contract_address, i)
     88         try:
     89             r = conn.do(o)
     90             account = ifc.parse_account(r)
     91             out_element((i, account), w)
     92             i += 1
     93         except JSONRPCException as e:
     94             break
     95 
     96 
     97 def main():
     98     conn = settings.get('CONN')
     99     address = config.get('_ADDRESS')
    100     c = AccountsIndex(
    101             settings.get('CHAIN_SPEC')
    102             )
    103     if address != None:
    104         element(
    105                 c,
    106                 conn,
    107                 settings.get('EXEC'),
    108                 address,
    109                 w=sys.stdout,
    110                 )
    111     else:
    112         ls(
    113                 c,
    114                 conn,
    115                 settings.get('EXEC'),
    116                 w=sys.stdout,
    117                 )
    118 
    119 
    120 if __name__ == '__main__':
    121     main()