implement chainlib cli util

This commit is contained in:
nolash 2021-07-30 20:12:26 +02:00
parent 7634e5566d
commit b7f7c8bad9
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 164 additions and 217 deletions

View File

@ -1 +1 @@
include **/data/*.json **/data/*.bin include **/data/*.json **/data/*.bin *requirements.txt

View File

@ -11,20 +11,16 @@ import json
import argparse import argparse
import logging import logging
# third-party imports # external imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer import chainlib.eth.cli
from crypto_dev_signer.keystore.dict import DictKeystore
from chainlib.chain import ChainSpec 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.connection import EthHTTPConnection
from chainlib.eth.tx import receipt from chainlib.eth.tx import receipt
from chainlib.eth.address import to_checksum_address
from hexathon import (
add_0x,
strip_0x,
)
# local imports # local imports
from eth_address_declarator.declarator import AddressDeclarator from eth_address_declarator.declarator import AddressDeclarator
@ -35,84 +31,63 @@ logg = logging.getLogger()
script_dir = os.path.dirname(__file__) script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '..', 'data') data_dir = os.path.join(script_dir, '..', 'data')
argparser = argparse.ArgumentParser() arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)') argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed') argparser.add_argument('-a', '--address', type=str, help='Address to add declaration for')
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed') argparser.add_positional('declaration', type=str, help='SHA256 sum of endorsement data to add')
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')
args = argparser.parse_args() args = argparser.parse_args()
if args.vv: extra_args = {
logg.setLevel(logging.DEBUG) 'address': None,
elif args.v: 'declaration': None,
logg.setLevel(logging.INFO) }
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AddressDeclarator.gas())
block_last = args.w wallet = chainlib.eth.cli.Wallet()
block_all = args.ww wallet.from_config(config)
passphrase_env = 'ETH_PASSPHRASE' rpc = chainlib.eth.cli.Rpc(wallet=wallet)
if args.env_prefix != None: conn = rpc.connect_by_config(config)
passphrase_env = args.env_prefix + '_' + passphrase_env
passphrase = os.environ.get(passphrase_env)
if passphrase == None:
logg.warning('no passphrase given')
passphrase=''
signer_address = None chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
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
def main(): 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) 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) (tx_hash_hex, o) = c.add_declaration(contract_address, signer_address, subject_address, declaration)
rpc.do(o)
if dummy: if config.get('_RPC_SEND'):
print(tx_hash_hex) conn.do(o)
print(o) if config.get('_WAIT'):
else: r = conn.wait(tx_hash_hex)
if block_last:
r = rpc.wait(tx_hash_hex)
if r['status'] == 0: if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you') sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1) sys.exit(1)
print(tx_hash_hex) print(tx_hash_hex)
else:
print(o)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -11,19 +11,14 @@ import os
import json import json
import argparse import argparse
import logging import logging
from hexathon import (
add_0x,
strip_0x,
)
# third-party imports # external imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer import chainlib.eth.cli
from crypto_dev_signer.keystore.dict import DictKeystore
from chainlib.chain import ChainSpec 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.connection import EthHTTPConnection
from chainlib.eth.tx import receipt from chainlib.eth.tx import receipt
@ -33,77 +28,45 @@ from eth_address_declarator.declarator import AddressDeclarator
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
script_dir = os.path.dirname(__file__) arg_flags = chainlib.eth.cli.argflag_std_write
data_dir = os.path.join(script_dir, '..', 'data') argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_argument('owner_description_hash', type=str, help='SHA256 of description metadata of contract deployer')
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')
args = argparser.parse_args() args = argparser.parse_args()
if args.vv: extra_args = {
logg.setLevel(logging.DEBUG) 'owner_description_hash': None,
elif args.v: }
logg.setLevel(logging.INFO) config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AddressDeclarator.gas())
block_all = args.ww wallet = chainlib.eth.cli.Wallet()
block_last = args.w or block_all wallet.from_config(config)
passphrase_env = 'ETH_PASSPHRASE' rpc = chainlib.eth.cli.Rpc(wallet=wallet)
if args.env_prefix != None: conn = rpc.connect_by_config(config)
passphrase_env = args.env_prefix + '_' + passphrase_env
passphrase = os.environ.get(passphrase_env)
if passphrase == None:
logg.warning('no passphrase given')
passphrase=''
signer_address = None chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
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
initial_description = args.owner_description_digest
def main(): 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) 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) owner_description_hash = config.get('_OWNER_DESCRIPTION_HASH')
if dummy: owner_description_hash_bytes = bytes.fromhex(strip_0x(owner_description_hash))
print(tx_hash_hex) if len(owner_description_hash_bytes) != 32:
print(o) raise ValueError('chain config hash must be 32 bytes')
else: owner_description_hash = add_0x(owner_description_hash)
if block_last:
r = rpc.wait(tx_hash_hex) (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: if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you') sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1) sys.exit(1)
@ -113,6 +76,8 @@ def main():
print(address) print(address)
else: else:
print(tx_hash_hex) print(tx_hash_hex)
else:
print(o)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -11,90 +11,97 @@ import os
import json import json
import argparse import argparse
import logging import logging
import sys
# third-party imports # external imports
import web3 import chainlib.eth.cli
from crypto_dev_signer.keystore import DictKeystore 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) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.WARNING) #argparser.add_argument('--resolve', action='store_true', help='Attempt to resolve the hashes to actual content')
logging.getLogger('urllib3').setLevel(logging.WARNING) #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
script_dir = os.path.dirname(__file__) argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
data_dir = os.path.join(script_dir, '..', 'data') 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')
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')
args = argparser.parse_args() args = argparser.parse_args()
if args.vv: extra_args = {
logg.setLevel(logging.DEBUG) 'declarator_address': None,
elif args.v: 'address': None,
logg.setLevel(logging.INFO) }
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): rpc = chainlib.eth.cli.Rpc()
r = urllib.request.urlopen(os.path.join(args.resolve_http, s.hex())) conn = rpc.connect_by_config(config)
return r.read().decode('utf-8')
def try_utf8(s): chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
return s.decode('utf-8')
signer_address = None
keystore = DictKeystore() def out_element(e, w=sys.stdout):
if args.y != None: w.write(e[1] + '\n')
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 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(): def main():
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.json'), 'r') c = Declarator(chain_spec)
abi = json.load(f)
f.close()
declarator_address = signer_address contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
if declarator_address == None: if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
declarator_address = args.a raise ValueError('invalid checksum address for contract')
if declarator_address == None:
sys.stderr.write('missing declarator address, specify -y or -a\n')
sys.exit(1)
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: ls(c, conn, contract_address, declarator_address, subject_address)
if not args.resolve:
print(d.hex()) declarations = []
continue
if args.resolve_http: # for d in declarations:
try: # if not args.resolve:
r = try_sha256(d) # print(d.hex())
print(r) # continue
continue # if args.resolve_http:
except urllib.error.HTTPError: # try:
pass # r = try_sha256(d)
try: # print(r)
print(try_utf8(d)) # continue
except UnicodeDecodeError: # except urllib.error.HTTPError:
pass # pass
# try:
# print(try_utf8(d))
# except UnicodeDecodeError:
# pass
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,4 +1,4 @@
confini~=0.3.6rc3 confini>=0.3.6rc3,<0.5.0
crypto-dev-signer~=0.4.14b6 crypto-dev-signer>=0.4.14b7,<=0.4.14
chainlib-eth~=0.0.5a1 chainlib-eth>=0.0.5a2,<=0.1.0
eth_erc20~=0.0.10a1 eth_erc20>=0.0.12a1,<=0.1.0

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = eth-address-index name = eth-address-index
version = 0.1.2a1 version = 0.1.4a1
description = Signed metadata declarations for ethereum addresses description = Signed metadata declarations for ethereum addresses
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no