2021-02-01 18:12:51 +01:00
|
|
|
# standard imports
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# third-party imports
|
|
|
|
import confini
|
|
|
|
import celery
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
from chainlib.eth.address import is_checksum_address
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
# local imports
|
2021-07-24 14:35:38 +02:00
|
|
|
import cic_eth.cli
|
2021-07-07 09:45:21 +02:00
|
|
|
from cic_eth.api.admin import AdminApi
|
2021-02-01 18:12:51 +01:00
|
|
|
from cic_eth.db.enum import LockEnum
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
2021-07-24 14:35:38 +02:00
|
|
|
arg_flags = cic_eth.cli.argflag_std_read
|
|
|
|
local_arg_flags = cic_eth.cli.argflag_local_task | cic_eth.cli.argflag_local_chain
|
|
|
|
argparser = cic_eth.cli.ArgumentParser(arg_flags)
|
|
|
|
argparser.add_argument('--no-register', dest='no_register', action='store_true', help='Do not register new account in on-chain accounts index')
|
|
|
|
argparser.process_local_flags(local_arg_flags)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
def process_lock_args(argparser):
|
|
|
|
argparser.add_argument('flags', type=str, help='Flags to manipulate')
|
2021-03-29 15:27:53 +02:00
|
|
|
argparser.add_argument('address', default=ZERO_ADDRESS, nargs='?', type=str, help='Ethereum address to unlock,')
|
2021-02-01 18:12:51 +01:00
|
|
|
|
2021-07-24 14:35:38 +02:00
|
|
|
sub = argparser.add_subparsers(help='')
|
2021-02-01 18:12:51 +01:00
|
|
|
sub.dest = "command"
|
|
|
|
sub_lock = sub.add_parser('lock', help='Set or reset locks')
|
|
|
|
sub_unlock = sub.add_parser('unlock', help='Set or reset locks')
|
|
|
|
process_lock_args(sub_lock)
|
|
|
|
process_lock_args(sub_unlock)
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
2021-07-24 14:35:38 +02:00
|
|
|
config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags)
|
|
|
|
|
|
|
|
celery_app = cic_eth.cli.CeleryApp.from_config(config)
|
|
|
|
|
|
|
|
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
|
|
|
|
2021-03-29 15:27:53 +02:00
|
|
|
admin_api = AdminApi(None)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def lock_names_to_flag(s):
|
|
|
|
flagstrings = s.split(',')
|
|
|
|
flagstrings = map(lambda s: s.upper(), flagstrings)
|
|
|
|
flagsvalue = 0
|
|
|
|
for s in flagstrings:
|
|
|
|
v = getattr(LockEnum, s)
|
|
|
|
flagsvalue |= v
|
|
|
|
return flagsvalue
|
|
|
|
|
|
|
|
# TODO: move each command to submodule
|
|
|
|
def main():
|
2021-04-23 23:02:51 +02:00
|
|
|
chain_spec_dict = None
|
|
|
|
if chain_spec != None:
|
|
|
|
chain_spec_dict = chain_spec.asdict()
|
2021-02-01 18:12:51 +01:00
|
|
|
if args.command == 'unlock':
|
|
|
|
flags = lock_names_to_flag(args.flags)
|
2021-03-29 15:27:53 +02:00
|
|
|
if not is_checksum_address(args.address):
|
2021-02-01 18:12:51 +01:00
|
|
|
raise ValueError('Invalid checksum address {}'.format(args.address))
|
|
|
|
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.admin.ctrl.unlock',
|
|
|
|
[
|
|
|
|
None,
|
2021-04-23 23:02:51 +02:00
|
|
|
chain_spec_dict,
|
2021-02-01 18:12:51 +01:00
|
|
|
args.address,
|
|
|
|
flags,
|
|
|
|
],
|
2021-07-24 14:35:38 +02:00
|
|
|
queue=config.get('CELERY_QUEUE'),
|
2021-02-01 18:12:51 +01:00
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
logg.debug('unlock {} on {} task {}'.format(flags, args.address, t))
|
|
|
|
|
|
|
|
|
|
|
|
if args.command == 'lock':
|
|
|
|
flags = lock_names_to_flag(args.flags)
|
2021-03-29 15:27:53 +02:00
|
|
|
if not is_checksum_address(args.address):
|
2021-02-01 18:12:51 +01:00
|
|
|
raise ValueError('Invalid checksum address {}'.format(args.address))
|
|
|
|
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.admin.ctrl.lock',
|
|
|
|
[
|
|
|
|
None,
|
2021-04-23 23:02:51 +02:00
|
|
|
chain_spec_dict,
|
2021-02-01 18:12:51 +01:00
|
|
|
args.address,
|
|
|
|
flags,
|
|
|
|
],
|
2021-07-24 14:35:38 +02:00
|
|
|
queue=config.get('CELERY_QUEUE'),
|
2021-02-01 18:12:51 +01:00
|
|
|
)
|
|
|
|
t = s.apply_async()
|
|
|
|
logg.debug('lock {} on {} task {}'.format(flags, args.address, t))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|