Rehabilitate address declarator runnables

This commit is contained in:
nolash 2021-03-22 17:23:57 +01:00
parent 799c97a7ed
commit 426b0adc79
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 76 additions and 72 deletions

View File

@ -62,6 +62,11 @@ class AddressDeclarator(TxFactory):
return AddressDeclarator.__bytecode return AddressDeclarator.__bytecode
@staticmethod
def gas(code=None):
return 2000000
def constructor(self, sender_address, initial_description): def constructor(self, sender_address, initial_description):
code = AddressDeclarator.bytecode() code = AddressDeclarator.bytecode()
enc = ABIContractEncoder() enc = ABIContractEncoder()

View File

@ -12,18 +12,21 @@ import argparse
import logging import logging
# third-party imports # third-party imports
import web3
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore import DictKeystore from crypto_dev_signer.keystore.dict import DictKeystore
from crypto_dev_signer.eth.helper import EthTxExecutor from crypto_dev_signer.eth.helper import EthTxExecutor
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import RPCGasOracle
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
# local imports
from eth_address_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)
logging.getLogger('urllib3').setLevel(logging.WARNING)
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')
@ -32,13 +35,12 @@ argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed') 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('-ww', action='store_true', help='Wait for every transaction to be confirmed')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string') argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
argparser.add_argument('-r', '--contract-address', dest='r', type=str, help='Address declaration contract address') argparser.add_argument('-a', '--contract-address', dest='a', type=str, help='Address declaration contract address')
argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Accounts declarator owner')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing') argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
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('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('address', type=str, help='Ethereum address to add declaration to') 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') argparser.add_argument('declaration', type=str, help='SHA256 sum of endorsement data to add')
args = argparser.parse_args() args = argparser.parse_args()
@ -47,49 +49,45 @@ if args.vv:
elif args.v: elif args.v:
logg.setLevel(logging.INFO) logg.setLevel(logging.INFO)
passphrase_env = 'ETH_PASSPHRASE'
block_all = args.ww if args.env_prefix != None:
block_last = args.w or block_all passphrase_env = args.env_prefix + '_' + passphrase_env
passphrase = os.environ.get(passphrase_env)
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p)) if passphrase == None:
logg.warning('no passphrase given')
passphrase=''
signer_address = None signer_address = None
keystore = DictKeystore() keystore = DictKeystore()
if args.y != None: if args.y != None:
logg.debug('loading keystore file {}'.format(args.y)) logg.debug('loading keystore file {}'.format(args.y))
signer_address = keystore.import_keystore_file(args.y) signer_address = keystore.import_keystore_file(args.y, password=passphrase)
logg.debug('now have key for signer address {}'.format(signer_address)) logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore) signer = EIP155Signer(keystore)
chain_spec = ChainSpec.from_chain_str(args.i) chain_spec = ChainSpec.from_chain_str(args.i)
chain_id = chain_spec.network_id() chain_id = chain_spec.network_id()
helper = EthTxExecutor( rpc = EthHTTPConnection(args.p)
w3, nonce_oracle = RPCNonceOracle(signer_address, rpc)
signer_address, gas_oracle = RPCGasOracle(rpc, code_callback=AddressDeclarator.gas)
signer,
chain_id, contract_address = args.a
block=args.ww, subject_address = args.subject_address
) declaration = args.declaration
def main(): def main():
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.json'), 'r') c = AddressDeclarator(signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle, chain_id=chain_id)
abi = json.load(f) (tx_hash_hex, o) = c.add_declaration(contract_address, signer_address, subject_address, declaration)
f.close() rpc.do(o)
o = receipt(tx_hash_hex)
r = rpc.do(o)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1)
c = w3.eth.contract(abi=abi, address=args.r) print(tx_hash_hex)
(tx_hash, rcpt) = helper.sign_and_send(
[
c.functions.addDeclaration(args.address, args.declaration).buildTransaction,
],
)
if block_last:
helper.wait_for()
print(tx_hash)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -6,24 +6,28 @@
""" """
# standard imports # standard imports
import sys
import os import os
import json import json
import argparse import argparse
import logging import logging
# third-party imports # third-party imports
import web3
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore import DictKeystore from crypto_dev_signer.keystore.dict import DictKeystore
from crypto_dev_signer.eth.helper import EthTxExecutor from crypto_dev_signer.eth.helper import EthTxExecutor
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import RPCGasOracle
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
# local imports
from eth_address_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)
logging.getLogger('urllib3').setLevel(logging.WARNING)
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')
@ -32,11 +36,10 @@ argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed') 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('-ww', action='store_true', help='Wait for every transaction to be confirmed')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string') argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Accounts declarator owner')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing') argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
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('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose')
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') argparser.add_argument('owner_description_digest', type=str, help='SHA256 of description metadata of contract deployer')
args = argparser.parse_args() args = argparser.parse_args()
@ -48,51 +51,49 @@ elif args.v:
block_last = args.w block_last = args.w
block_all = args.ww block_all = args.ww
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p)) 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=''
signer_address = None signer_address = None
keystore = DictKeystore() keystore = DictKeystore()
if args.y != None: if args.y != None:
logg.debug('loading keystore file {}'.format(args.y)) logg.debug('loading keystore file {}'.format(args.y))
signer_address = keystore.import_keystore_file(args.y) signer_address = keystore.import_keystore_file(args.y, password=passphrase)
logg.debug('now have key for signer address {}'.format(signer_address)) logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore) signer = EIP155Signer(keystore)
chain_spec = ChainSpec.from_chain_str(args.i) chain_spec = ChainSpec.from_chain_str(args.i)
chain_id = chain_spec.network_id() chain_id = chain_spec.network_id()
helper = EthTxExecutor( rpc = EthHTTPConnection(args.p)
w3, nonce_oracle = RPCNonceOracle(signer_address, rpc)
signer_address, gas_oracle = RPCGasOracle(rpc, code_callback=AddressDeclarator.gas)
signer,
chain_id,
block=args.ww,
)
initial_description = args.owner_description_digest
def main(): def main():
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.json'), 'r') c = AddressDeclarator(signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle, chain_id=chain_id)
abi = json.load(f) (tx_hash_hex, o) = c.constructor(signer_address, initial_description)
f.close() rpc.do(o)
o = receipt(tx_hash_hex)
f = open(os.path.join(args.abi_dir, 'AddressDeclarator.bin'), 'r') r = rpc.do(o)
bytecode = f.read() if r['status'] == 0:
f.close() sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1)
c = w3.eth.contract(abi=abi, bytecode=bytecode) # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase)
logg.info('digest {}'.format(args.owner_description_digest)) address = r['contractAddress']
(tx_hash, rcpt) = helper.sign_and_send(
[
c.constructor(args.owner_description_digest).buildTransaction,
],
force_wait=True,
)
if block_last: if block_last:
helper.wait_for() rpc.wait(tx_hash_hex)
print(rcpt.contractAddress) print(address)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':