chainlib-eth

Ethereum implementation of the chainlib interface
Log | Files | Refs | README | LICENSE

commit 567a788588075f1ccb08a70c9e1ed27c53ef69f0
parent f46f4fc128c8723d776485b14775deca72b5ef6d
Author: lash <dev@holbrook.no>
Date:   Thu,  3 Nov 2022 08:52:30 +0000

Enable contract create on eth-gas + easy parse zero-address

Diffstat:
Mchainlib/eth/cli/arg.py | 9++++++++-
Mchainlib/eth/cli/config.py | 4++++
Mchainlib/eth/runnable/encode.py | 3---
Mchainlib/eth/runnable/gas.py | 33+++++++++++++++++++++++++++------
Mchainlib/eth/settings.py | 33++++++++++++++++++++++++++++-----
Msetup.cfg | 2+-
6 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/chainlib/eth/cli/arg.py b/chainlib/eth/cli/arg.py @@ -1,6 +1,13 @@ from chainlib.cli.arg import ( ArgumentParser, - Arg, + Arg as BaseArg, ArgFlag, process_args, ) + + +class Arg(BaseArg): + + def __init__(self, flags): + super(Arg, self).__init__(flags) + self.add('z', 'wallet', typ=bool, help='Send to null address (contract creation)') diff --git a/chainlib/eth/cli/config.py b/chainlib/eth/cli/config.py @@ -31,4 +31,8 @@ def process_config(config, arg, args, flags): ]: raise ValueError('unknown rpc dialect {}'.format(config.get('RPC_DIALECT'))) + if arg.match('wallet', flags): + if getattr(args, 'z'): + config.add(True, '_Z') + return config diff --git a/chainlib/eth/runnable/encode.py b/chainlib/eth/runnable/encode.py @@ -128,9 +128,6 @@ def main(): if mode != 'arg': logg.error('mode tx without contract method signature makes no sense. Use eth-get with --data instead.') sys.exit(1) - if args.format == 'rpc': - logg.error('rpc format with arg put does not make sense') - sys.exit(1) if mode == 'arg': print(strip_0x(code)) diff --git a/chainlib/eth/runnable/gas.py b/chainlib/eth/runnable/gas.py @@ -47,8 +47,31 @@ from chainlib.eth.settings import process_settings logg = logging.getLogger() +def from_data_arg(data): + try: + return strip_0x(data) + except ValueError: + pass + + try: + f = open(data, 'r') + except: + raise ValueError("data not hex and not file we can open") + + data = f.read() + f.close() + + data = data.rstrip() + return strip_0x(data) + + def process_config_local(config, arg, args, flags): - config.add(args.data, '_DATA', False) + data = '' + for data_arg in args.data: + data += from_data_arg(data_arg) + if data == '': + data = None + config.add(data, '_DATA', False) config.add(args.amount, '_VALUE', False) return config @@ -59,7 +82,7 @@ flags = arg_flags.STD_WRITE | arg_flags.WALLET argparser = chainlib.eth.cli.ArgumentParser() argparser = process_args(argparser, arg, flags) -argparser.add_argument('--data', type=str, help='Transaction data') +argparser.add_argument('--data', type=str, action='append', help='Transaction data') argparser.add_argument('amount', type=str, help='Token amount to send') args = argparser.parse_args() @@ -94,9 +117,7 @@ def main(): nonce_oracle=settings.get('NONCE_ORACLE'), ) - recipient = to_checksum_address(config.get('_RECIPIENT')) - if not config.true('_UNSAFE') and not is_checksum_address(recipient): - raise ValueError('invalid checksum address') + recipient = settings.get('RECIPIENT') if logg.isEnabledFor(logging.DEBUG): try: @@ -119,7 +140,7 @@ def main(): settings.get('SENDER_ADDRESS'), settings.get('RECIPIENT'), settings.get('VALUE'), - data=config.get('_DATA'), + data=settings.get('DATA'), id_generator=settings.get('RPC_ID_GENERATOR'), ) diff --git a/chainlib/eth/settings.py b/chainlib/eth/settings.py @@ -10,6 +10,7 @@ from chainlib.block import BlockSpec # local imports import chainlib.eth.cli from chainlib.eth.address import to_checksum_address +from chainlib.eth.constant import ZERO_ADDRESS def process_settings_rpc(settings, config): @@ -58,12 +59,36 @@ def process_settings_blockspec(settings, config): return settings +def __try_zero_address(config, address): + try: + if int(address) == 0: + return ZERO_ADDRESS + except ValueError: + pass + + try: + if int(address, 16) == 0: + return ZERO_ADDRESS + except ValueError: + pass + + recipient = to_checksum_address(address) + if not config.true('_UNSAFE') and recipient != address: + raise ValueError('invalid checksum address: {}'.format(address)) + + return add_0x(recipient) + + def process_settings_wallet(settings, config): wallet = chainlib.eth.cli.Wallet() wallet.from_config(config) settings.set('WALLET', wallet) + if config.get('_Z'): + settings.set('RECIPIENT', None) + return settings + recipient_in = None try: recipient_in = config.get('_RECIPIENT') @@ -77,10 +102,7 @@ def process_settings_wallet(settings, config): recipient_in = wallet.from_address(recipient_in) recipient_in = strip_0x(recipient_in) - recipient = to_checksum_address(recipient_in) - if not config.true('_UNSAFE') and recipient != recipient_in: - raise ValueError('invalid checksum address: {}'.format(recipient_in)) - recipient = add_0x(recipient) + recipient = __try_zero_address(config, recipient_in) settings.set('RECIPIENT', recipient) return settings @@ -105,17 +127,18 @@ def process_settings_contract(settings, config): return settings + def process_settings_data(settings, config): data = None try: data = config.get('_DATA') except KeyError: + settings.set('DATA', None) return settings if data == None: return settings - data = add_0x(data) settings.set('DATA', data) return settings diff --git a/setup.cfg b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = chainlib-eth -version = 0.3.3 +version = 0.3.4 description = Ethereum implementation of the chainlib interface author = Louis Holbrook author_email = dev@holbrook.no