From b7f7c8bad909c25a04b021c1b95639a9233cd2fc Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 30 Jul 2021 20:12:26 +0200 Subject: [PATCH] implement chainlib cli util --- python/MANIFEST.in | 2 +- python/eth_address_declarator/runnable/add.py | 123 +++++++--------- .../eth_address_declarator/runnable/deploy.py | 111 +++++--------- .../eth_address_declarator/runnable/view.py | 135 +++++++++--------- python/requirements.txt | 8 +- python/setup.cfg | 2 +- 6 files changed, 164 insertions(+), 217 deletions(-) diff --git a/python/MANIFEST.in b/python/MANIFEST.in index ebc3162..4168d50 100644 --- a/python/MANIFEST.in +++ b/python/MANIFEST.in @@ -1 +1 @@ -include **/data/*.json **/data/*.bin +include **/data/*.json **/data/*.bin *requirements.txt diff --git a/python/eth_address_declarator/runnable/add.py b/python/eth_address_declarator/runnable/add.py index de316cc..dc84064 100644 --- a/python/eth_address_declarator/runnable/add.py +++ b/python/eth_address_declarator/runnable/add.py @@ -11,20 +11,16 @@ import json import argparse import logging -# third-party imports -from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer -from crypto_dev_signer.keystore.dict import DictKeystore +# external imports +import chainlib.eth.cli from chainlib.chain import ChainSpec -from chainlib.eth.nonce import ( - RPCNonceOracle, - OverrideNonceOracle, - ) -from chainlib.eth.gas import ( - RPCGasOracle, - OverrideGasOracle, - ) from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.tx import receipt +from chainlib.eth.address import to_checksum_address +from hexathon import ( + add_0x, + strip_0x, + ) # local imports from eth_address_declarator.declarator import AddressDeclarator @@ -35,84 +31,63 @@ logg = logging.getLogger() script_dir = os.path.dirname(__file__) data_dir = os.path.join(script_dir, '..', 'data') -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('-w', action='store_true', help='Wait for the last transaction to be confirmed') -argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed') -argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string') -argparser.add_argument('-a', '--contract-address', dest='a', type=str, help='Address declaration contract address') -argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing') -argparser.add_argument('-v', action='store_true', help='Be verbose') -argparser.add_argument('-vv', action='store_true', help='Be more verbose') -argparser.add_argument('-d', action='store_true', help='Dump RPC calls to terminal and do not send') -argparser.add_argument('--gas-price', type=int, dest='gas_price', help='Override gas price') -argparser.add_argument('--nonce', type=int, help='Override transaction nonce') -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('subject_address', type=str, help='Ethereum address to add declaration to') -argparser.add_argument('declaration', type=str, help='SHA256 sum of endorsement data to add') +arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC +argparser = chainlib.eth.cli.ArgumentParser(arg_flags) +argparser.add_argument('-a', '--address', type=str, help='Address to add declaration for') +argparser.add_positional('declaration', type=str, help='SHA256 sum of endorsement data to add') args = argparser.parse_args() -if args.vv: - logg.setLevel(logging.DEBUG) -elif args.v: - logg.setLevel(logging.INFO) +extra_args = { + 'address': None, + 'declaration': None, + } +config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AddressDeclarator.gas()) -block_last = args.w -block_all = args.ww +wallet = chainlib.eth.cli.Wallet() +wallet.from_config(config) -passphrase_env = 'ETH_PASSPHRASE' -if args.env_prefix != None: - passphrase_env = args.env_prefix + '_' + passphrase_env -passphrase = os.environ.get(passphrase_env) -if passphrase == None: - logg.warning('no passphrase given') - passphrase='' +rpc = chainlib.eth.cli.Rpc(wallet=wallet) +conn = rpc.connect_by_config(config) -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, password=passphrase) - logg.debug('now have key for signer address {}'.format(signer_address)) -signer = EIP155Signer(keystore) - -chain_spec = ChainSpec.from_chain_str(args.i) - -rpc = EthHTTPConnection(args.p) -nonce_oracle = None -if args.nonce != None: - nonce_oracle = OverrideNonceOracle(signer_address, args.nonce) -else: - nonce_oracle = RPCNonceOracle(signer_address, rpc) - -gas_oracle = None -if args.gas_price !=None: - gas_oracle = OverrideGasOracle(price=args.gas_price, conn=rpc, code_callback=AddressDeclarator.gas) -else: - gas_oracle = RPCGasOracle(rpc, code_callback=AddressDeclarator.gas) - -dummy = args.d - -contract_address = args.a -subject_address = args.subject_address -declaration = args.declaration +chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) def main(): + signer = rpc.get_signer() + signer_address = rpc.get_sender_address() + + gas_oracle = rpc.get_gas_oracle() + nonce_oracle = rpc.get_nonce_oracle() + c = AddressDeclarator(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) + + subject_address = to_checksum_address(config.get('_ADDRESS')) + if not config.true('_UNSAFE') and subject_address != add_0x(config.get('_ADDRESS')): + raise ValueError('invalid checksum address for subject_address') + + contract_address = to_checksum_address(config.get('_EXEC_ADDRESS')) + if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')): + raise ValueError('invalid checksum address for contract') + + declaration = config.get('_DECLARATION') + declaration_bytes = bytes.fromhex(strip_0x(declaration)) + if len(declaration_bytes) != 32: + raise ValueError('declaration hash must be 32 bytes') + declaration = add_0x(declaration) + (tx_hash_hex, o) = c.add_declaration(contract_address, signer_address, subject_address, declaration) - rpc.do(o) - if dummy: - print(tx_hash_hex) - print(o) - else: - if block_last: - r = rpc.wait(tx_hash_hex) + + if config.get('_RPC_SEND'): + conn.do(o) + if config.get('_WAIT'): + r = conn.wait(tx_hash_hex) if r['status'] == 0: sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you') sys.exit(1) print(tx_hash_hex) + else: + print(o) if __name__ == '__main__': diff --git a/python/eth_address_declarator/runnable/deploy.py b/python/eth_address_declarator/runnable/deploy.py index ff9e054..f112d09 100644 --- a/python/eth_address_declarator/runnable/deploy.py +++ b/python/eth_address_declarator/runnable/deploy.py @@ -11,19 +11,14 @@ import os import json import argparse import logging +from hexathon import ( + add_0x, + strip_0x, + ) -# third-party imports -from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer -from crypto_dev_signer.keystore.dict import DictKeystore +# external imports +import chainlib.eth.cli from chainlib.chain import ChainSpec -from chainlib.eth.nonce import ( - RPCNonceOracle, - OverrideNonceOracle, - ) -from chainlib.eth.gas import ( - RPCGasOracle, - OverrideGasOracle, - ) from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.tx import receipt @@ -33,77 +28,45 @@ from eth_address_declarator.declarator import AddressDeclarator logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() -script_dir = os.path.dirname(__file__) -data_dir = os.path.join(script_dir, '..', 'data') - -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('-w', action='store_true', help='Wait for the last transaction to be confirmed') -argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed') -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('-v', action='store_true', help='Be verbose') -argparser.add_argument('-vv', action='store_true', help='Be more verbose') -argparser.add_argument('-d', action='store_true', help='Dump RPC calls to terminal and do not send') -argparser.add_argument('--gas-price', type=int, dest='gas_price', help='Override gas price') -argparser.add_argument('--nonce', type=int, help='Override transaction nonce') -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('owner_description_digest', type=str, help='SHA256 of description metadata of contract deployer') +arg_flags = chainlib.eth.cli.argflag_std_write +argparser = chainlib.eth.cli.ArgumentParser(arg_flags) +argparser.add_argument('owner_description_hash', type=str, help='SHA256 of description metadata of contract deployer') args = argparser.parse_args() -if args.vv: - logg.setLevel(logging.DEBUG) -elif args.v: - logg.setLevel(logging.INFO) +extra_args = { + 'owner_description_hash': None, + } +config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AddressDeclarator.gas()) -block_all = args.ww -block_last = args.w or block_all +wallet = chainlib.eth.cli.Wallet() +wallet.from_config(config) -passphrase_env = 'ETH_PASSPHRASE' -if args.env_prefix != None: - passphrase_env = args.env_prefix + '_' + passphrase_env -passphrase = os.environ.get(passphrase_env) -if passphrase == None: - logg.warning('no passphrase given') - passphrase='' +rpc = chainlib.eth.cli.Rpc(wallet=wallet) +conn = rpc.connect_by_config(config) -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, password=passphrase) - logg.debug('now have key for signer address {}'.format(signer_address)) -signer = EIP155Signer(keystore) +chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) -chain_spec = ChainSpec.from_chain_str(args.i) - -rpc = EthHTTPConnection(args.p) -nonce_oracle = None -if args.nonce != None: - nonce_oracle = OverrideNonceOracle(signer_address, args.nonce) -else: - nonce_oracle = RPCNonceOracle(signer_address, rpc) - -gas_oracle = None -if args.gas_price !=None: - gas_oracle = OverrideGasOracle(price=args.gas_price, conn=rpc, code_callback=AddressDeclarator.gas) -else: - gas_oracle = RPCGasOracle(rpc, code_callback=AddressDeclarator.gas) - -dummy = args.d - -initial_description = args.owner_description_digest def main(): + signer = rpc.get_signer() + signer_address = rpc.get_sender_address() + + gas_oracle = rpc.get_gas_oracle() + nonce_oracle = rpc.get_nonce_oracle() + c = AddressDeclarator(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) - (tx_hash_hex, o) = c.constructor(signer_address, initial_description) - rpc.do(o) - if dummy: - print(tx_hash_hex) - print(o) - else: - if block_last: - r = rpc.wait(tx_hash_hex) + + owner_description_hash = config.get('_OWNER_DESCRIPTION_HASH') + owner_description_hash_bytes = bytes.fromhex(strip_0x(owner_description_hash)) + if len(owner_description_hash_bytes) != 32: + raise ValueError('chain config hash must be 32 bytes') + owner_description_hash = add_0x(owner_description_hash) + + (tx_hash_hex, o) = c.constructor(signer_address, owner_description_hash) + if config.get('_RPC_SEND'): + conn.do(o) + if config.get('_WAIT'): + r = conn.wait(tx_hash_hex) if r['status'] == 0: sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you') sys.exit(1) @@ -113,6 +76,8 @@ def main(): print(address) else: print(tx_hash_hex) + else: + print(o) if __name__ == '__main__': diff --git a/python/eth_address_declarator/runnable/view.py b/python/eth_address_declarator/runnable/view.py index 9075049..eed070d 100644 --- a/python/eth_address_declarator/runnable/view.py +++ b/python/eth_address_declarator/runnable/view.py @@ -11,90 +11,97 @@ import os import json import argparse import logging +import sys -# third-party imports -import web3 -from crypto_dev_signer.keystore import DictKeystore +# external imports +import chainlib.eth.cli +from chainlib.chain import ChainSpec +from chainlib.error import JSONRPCException +from chainlib.eth.address import to_checksum_address +from hexathon import ( + add_0x, + strip_0x, + ) + +# local imports +from eth_address_declarator import Declarator +from eth_address_declarator.declarator import AddressDeclarator logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() -logging.getLogger('web3').setLevel(logging.WARNING) -logging.getLogger('urllib3').setLevel(logging.WARNING) - -script_dir = os.path.dirname(__file__) -data_dir = os.path.join(script_dir, '..', 'data') - -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('-r', '--contract-address', dest='r', type=str, help='Address declaration contract address') -argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string') -argparser.add_argument('-a', '-declarator-address', dest='a', type=str, help='Signing address for the declaration') -argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing') -argparser.add_argument('--resolve', action='store_true', help='Attempt to resolve the hashes to actual content') -argparser.add_argument('--resolve-http', dest='resolve_http', type=str, help='Base url to look up content hashes') -argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_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('address', type=str, help='Ethereum declaration address to look up') +#argparser.add_argument('--resolve', action='store_true', help='Attempt to resolve the hashes to actual content') +#argparser.add_argument('--resolve-http', dest='resolve_http', type=str, help='Base url to look up content hashes') +arg_flags = chainlib.eth.cli.argflag_std_read | chainlib.eth.cli.Flag.EXEC +argparser = chainlib.eth.cli.ArgumentParser(arg_flags) +argparser.add_argument('--declarator-address', required=True, type=str, help='Declarator of address') +argparser.add_positional('address', type=str, help='Ethereum declaration address to look up') args = argparser.parse_args() -if args.vv: - logg.setLevel(logging.DEBUG) -elif args.v: - logg.setLevel(logging.INFO) +extra_args = { + 'declarator_address': None, + 'address': None, + } +config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AddressDeclarator.gas()) -w3 = web3.Web3(web3.Web3.HTTPProvider(args.p)) +wallet = chainlib.eth.cli.Wallet() +wallet.from_config(config) -def try_sha256(s): - r = urllib.request.urlopen(os.path.join(args.resolve_http, s.hex())) - return r.read().decode('utf-8') +rpc = chainlib.eth.cli.Rpc() +conn = rpc.connect_by_config(config) -def try_utf8(s): - return s.decode('utf-8') +chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) -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) - logg.debug('now have key for signer address {}'.format(signer_address)) + +def out_element(e, w=sys.stdout): + w.write(e[1] + '\n') + + +def ls(ifc, conn, contract_address, declarator_address, subject_address, w=sys.stdout): + o = ifc.declaration(contract_address, declarator_address, subject_address) + r = conn.do(o) + declarations = ifc.parse_declaration(r) + + for i, d in enumerate(declarations): + out_element((i, d), w) def main(): - f = open(os.path.join(args.abi_dir, 'AddressDeclarator.json'), 'r') - abi = json.load(f) - f.close() + c = Declarator(chain_spec) - declarator_address = signer_address - if declarator_address == None: - declarator_address = args.a - if declarator_address == None: - sys.stderr.write('missing declarator address, specify -y or -a\n') - sys.exit(1) + contract_address = to_checksum_address(config.get('_EXEC_ADDRESS')) + if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')): + raise ValueError('invalid checksum address for contract') - logg.debug('declarator address {}'.format(declarator_address)) - c = w3.eth.contract(abi=abi, address=args.r) + declarator_address = to_checksum_address(config.get('_DECLARATOR_ADDRESS')) + if not config.true('_UNSAFE') and declarator_address != add_0x(config.get('_DECLARATOR_ADDRESS')): + raise ValueError('invalid checksum address for declarator') - declarations = c.functions.declaration(declarator_address, args.address).call() + subject_address = to_checksum_address(config.get('_ADDRESS')) + if not config.true('_UNSAFE') and subject_address != add_0x(config.get('_ADDRESS')): + raise ValueError('invalid checksum address for subject') - for d in declarations: - if not args.resolve: - print(d.hex()) - continue - if args.resolve_http: - try: - r = try_sha256(d) - print(r) - continue - except urllib.error.HTTPError: - pass - try: - print(try_utf8(d)) - except UnicodeDecodeError: - pass + ls(c, conn, contract_address, declarator_address, subject_address) + + declarations = [] + +# for d in declarations: +# if not args.resolve: +# print(d.hex()) +# continue +# if args.resolve_http: +# try: +# r = try_sha256(d) +# print(r) +# continue +# except urllib.error.HTTPError: +# pass +# try: +# print(try_utf8(d)) +# except UnicodeDecodeError: +# pass if __name__ == '__main__': diff --git a/python/requirements.txt b/python/requirements.txt index aede0db..6783a95 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1,4 +1,4 @@ -confini~=0.3.6rc3 -crypto-dev-signer~=0.4.14b6 -chainlib-eth~=0.0.5a1 -eth_erc20~=0.0.10a1 +confini>=0.3.6rc3,<0.5.0 +crypto-dev-signer>=0.4.14b7,<=0.4.14 +chainlib-eth>=0.0.5a2,<=0.1.0 +eth_erc20>=0.0.12a1,<=0.1.0 diff --git a/python/setup.cfg b/python/setup.cfg index 629ec47..6bcb354 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = eth-address-index -version = 0.1.2a1 +version = 0.1.4a1 description = Signed metadata declarations for ethereum addresses author = Louis Holbrook author_email = dev@holbrook.no