chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit 494cb1e0aff331bbfdfae5e99c4b269aa3d6d60c
parent 5bac222e9cb81d3ea9f572553ceea47591085d57
Author: nolash <dev@holbrook.no>
Date:   Fri, 11 Jun 2021 23:00:20 +0200

Add stdin input to replace single positional argument

Diffstat:
MMANIFEST.in | 2+-
Mchainlib/eth/runnable/checksum.py | 20+++++++++++++++++++-
Mchainlib/eth/runnable/count.py | 17++++++++++++++---
Mchainlib/eth/runnable/decode.py | 22++++++++++++++++++----
Mchainlib/eth/runnable/get.py | 15+++++++++++++--
5 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/MANIFEST.in b/MANIFEST.in @@ -1 +1 @@ -include requirements.txt +include *requirements.txt LICENSE diff --git a/chainlib/eth/runnable/checksum.py b/chainlib/eth/runnable/checksum.py @@ -1,5 +1,6 @@ # standard imports import sys +import select # external imports from hexathon import strip_0x @@ -7,8 +8,25 @@ from hexathon import strip_0x # local imports from chainlib.eth.address import to_checksum_address +v = None +if len(sys.argv) > 1: + v = sys.argv[1] +else: + h = select.select([sys.stdin], [], [], 0) + if len(h[0]) > 0: + v = h[0][0].read() + v = v.rstrip() + +if v == None: + sys.stderr.write('input missing\n') + sys.exit(1) + def main(): - print(to_checksum_address(strip_0x(sys.argv[1]))) + try: + print(to_checksum_address(strip_0x(v))) + except ValueError as e: + sys.stderr.write('invalid input: {}\n'.format(e)) + sys.exit(1) if __name__ == '__main__': diff --git a/chainlib/eth/runnable/count.py b/chainlib/eth/runnable/count.py @@ -6,6 +6,7 @@ import os import json import argparse import logging +import select # local imports from chainlib.eth.address import to_checksum @@ -14,12 +15,20 @@ from chainlib.eth.tx import count from chainlib.chain import ChainSpec 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('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='http://localhost:8545', 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') @@ -28,15 +37,17 @@ argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFI argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') 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', type=str, help='Ethereum address of recipient') +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: @@ -52,7 +63,7 @@ def main(): if not args.u and recipient != add_0x(args.address): raise ValueError('invalid checksum address') - o = count(args.address) + o = count(recipient) print(rpc.do(o)) diff --git a/chainlib/eth/runnable/decode.py b/chainlib/eth/runnable/decode.py @@ -15,8 +15,9 @@ import os import json import argparse import logging +import select -# third-party imports +# external imports from chainlib.eth.tx import unpack from chainlib.chain import ChainSpec @@ -30,14 +31,27 @@ logg = logging.getLogger() default_abi_dir = os.environ.get('ETH_ABI_DIR', '/usr/share/local/cic/solidity/abi') 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('-v', action='store_true', help='Be verbose') argparser.add_argument('-i', '--chain-id', dest='i', default='evm:ethereum:1', type=str, help='Numeric network id') -argparser.add_argument('tx', type=str, help='hex-encoded signed raw transaction') +argparser.add_argument('tx', type=str, nargs='?', default=stdin_arg(), help='hex-encoded signed raw transaction') +argparser.add_argument('-v', action='store_true', help='Be verbose') +argparser.add_argument('-vv', action='store_true', help='Be more verbose') args = argparser.parse_args() -if args.v: +if args.tx == 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) chain_spec = ChainSpec.from_chain_str(args.i) diff --git a/chainlib/eth/runnable/get.py b/chainlib/eth/runnable/get.py @@ -16,6 +16,7 @@ import json import argparse import logging import enum +import select # external imports from hexathon import ( @@ -43,7 +44,14 @@ logg = logging.getLogger() default_abi_dir = os.environ.get('ETH_ABI_DIR', '/usr/share/local/cic/solidity/abi') default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545') -argparser = argparse.ArgumentParser() +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('eth-get', description='display information about an Ethereum address or transaction', epilog='address/transaction can be provided as an argument or from standard input') 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('-t', '--token-address', dest='t', type=str, help='Token address. If not set, will return gas balance') @@ -51,9 +59,12 @@ argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Au argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=default_abi_dir, help='Directory containing bytecode and abi (default {})'.format(default_abi_dir)) argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose') -argparser.add_argument('item', type=str, help='Item to get information for (address og transaction)') +argparser.add_argument('item', nargs='?', default=stdin_arg(), type=str, help='Item to get information for (address og transaction)') args = argparser.parse_args() +if args.item == None: + argparser.error('need first positional argument or value from stdin') + if args.vv: logg.setLevel(logging.DEBUG) elif args.v: