# SPDX-License-Identifier: GPL-3.0-or-later # standard imports import sys import os import json import argparse import logging import select # local imports from chainlib.eth.address import to_checksum from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.tx import count from chainlib.chain import ChainSpec from chainlib.jsonrpc import IntSequenceGenerator from crypto_dev_signer.keystore.dict import DictKeystore from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer from hexathon import add_0x logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() default_eth_provider = os.environ.get('RPC_PROVIDER') if default_eth_provider == None: default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545') def stdin_arg(): h = select.select([sys.stdin], [], [], 0) if len(h[0]) > 0: v = h[0][0].read() return v.rstrip() return None argparser = argparse.ArgumentParser() argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)') argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string') argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing') argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids') argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('address', nargs='?', type=str, default=stdin_arg(), help='Ethereum address of recipient') args = argparser.parse_args() if args.address == None: argparser.error('need first positional argument or value from stdin') if args.vv: logg.setLevel(logging.DEBUG) elif args.v: logg.setLevel(logging.INFO) signer_address = None keystore = DictKeystore() if args.y != None: logg.debug('loading keystore file {}'.format(args.y)) signer_address = keystore.import_keystore_file(args.y, passphrase) logg.debug('now have key for signer address {}'.format(signer_address)) signer = EIP155Signer(keystore) rpc_id_generator = None if args.seq: rpc_id_generator = IntSequenceGenerator() auth = None if os.environ.get('RPC_AUTHENTICATION') == 'basic': from chainlib.auth import BasicAuth auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD']) rpc = EthHTTPConnection(args.p, auth=auth) def main(): recipient = to_checksum(args.address) if not args.u and recipient != add_0x(args.address): raise ValueError('invalid checksum address') o = count(recipient, id_generator=rpc_id_generator) r = rpc.do(o) count_result = None try: count_result = int(r, 16) except ValueError: count_result = int(r, 10) print(count_result) if __name__ == '__main__': main()