def parse_addrfile_label( lbl): # we must maintain backwards compat, so parse is tricky al_coin, al_mmtype = None, None tn = lbl[-8:] == ':TESTNET' if tn: assert g.proto.is_testnet( ), '{} file is testnet but protocol is mainnet!'.format( self.data_desc) lbl = lbl[:-8] else: assert not g.proto.is_testnet( ), '{} file is mainnet but protocol is testnet!'.format( self.data_desc) lbl = lbl.split(':', 1) if len(lbl) == 2: al_coin, al_mmtype = lbl[0], lbl[1].lower() else: if lbl[0].lower() in MMGenAddrType.get_names(): al_mmtype = lbl[0].lower() else: al_coin = lbl[0] # this block fails if al_mmtype is invalid for g.coin if not al_mmtype: mmtype = MMGenAddrType('E' if al_coin in ('ETH', 'ETC') else 'L', on_fail='raise') else: mmtype = MMGenAddrType(al_mmtype, on_fail='raise') from mmgen.protocol import CoinProtocol base_coin = CoinProtocol(al_coin or 'BTC', testnet=False).base_coin return base_coin, mmtype
def override_from_cfg_file(cfg_data): from mmgen.util import die, strip_comments, set_for_type import re from mmgen.protocol import CoinProtocol for n, l in enumerate(cfg_data.splitlines(), 1): # DOS-safe l = strip_comments(l) if l == '': continue m = re.match(r'(\w+)\s+(\S+)$', l) if not m: die(2, "Parse error in file '{}', line {}".format(g.cfg_file, n)) name, val = m.groups() if name in g.cfg_file_opts: pfx, cfg_var = name.split('_', 1) if pfx in CoinProtocol.coins: tn = False cv1, cv2 = cfg_var.split('_', 1) if cv1 in ('mainnet', 'testnet'): tn, cfg_var = (cv1 == 'testnet'), cv2 cls, attr = CoinProtocol(pfx, tn), cfg_var else: cls, attr = g, name setattr( cls, attr, set_for_type(val, getattr(cls, attr), attr, src=g.cfg_file)) else: die(2, "'{}': unrecognized option in '{}'".format(name, g.cfg_file))
def fork(coin): coin = coin.upper() from mmgen.protocol import CoinProtocol forks = CoinProtocol(coin,False).forks if not [f for f in forks if f[2] == g.coin.lower() and f[3] == True]: die(1,"Coin {} is not a replayable fork of coin {}".format(g.coin,coin)) gmsg('Creating fork from coin {} to coin {}'.format(coin,g.coin)) source_data_dir = os.path.join(g.data_dir_root,'regtest',coin.lower()) try: os.stat(source_data_dir) except: die(1,"Source directory '{}' does not exist!".format(source_data_dir)) # stop the other daemon global rpc_port,data_dir rpc_port_save,data_dir_save = rpc_port,data_dir rpc_port = rpc_ports[coin.lower()] data_dir = os.path.join(g.data_dir_root,'regtest',coin.lower()) if test_daemon() != 'stopped': stop_and_wait(silent=True,stop_silent=True) rpc_port,data_dir = rpc_port_save,data_dir_save try: os.makedirs(data_dir) except: pass # stop our daemon if test_daemon() != 'stopped': stop_and_wait(silent=True,stop_silent=True) create_data_dir() os.rmdir(data_dir) shutil.copytree(source_data_dir,data_dir,symlinks=True) start_and_wait('miner',reindex=True,silent=True) stop_and_wait(silent=True,stop_silent=True) gmsg('Fork {} successfully created'.format(g.coin))
def addrimport(self,user,sid=None,addr_range='1-5',num_addrs=5,mmtypes=[]): id_strs = { 'legacy':'', 'compressed':'-C', 'segwit':'-S', 'bech32':'-B' } if not sid: sid = self._user_sid(user) from mmgen.addr import MMGenAddrType for mmtype in mmtypes or g.proto.mmtypes: desc = MMGenAddrType.mmtypes[mmtype]['name'] addrfile = joinpath(self._user_dir(user), '{}{}{}[{}]{x}.testnet.addrs'.format( sid,self.altcoin_pfx,id_strs[desc],addr_range, x='-α' if g.debug_utf8 else '')) if mmtype == g.proto.mmtypes[0] and user == 'bob': psave = g.proto g.proto = CoinProtocol(g.coin,True) self._add_comments_to_addr_file(addrfile,addrfile,use_labels=True) g.proto = psave t = self.spawn( 'mmgen-addrimport', ['--quiet', '--'+user, '--batch', addrfile], extra_desc='({})'.format(desc)) if g.debug: t.expect("Type uppercase 'YES' to confirm: ",'YES\n') t.expect('Importing') t.expect('{} addresses imported'.format(num_addrs)) ok_msg() t.skip_ok = True return t
def check_daemons_running(): if opt.coin: die( 1, '--coin option not supported with this command. Use --coins instead' ) if opt.coins: coins = opt.coins.upper().split(',') else: ymsg('Warning: no coins specified, so defaulting to BTC only') coins = ['BTC'] for coin in coins: if coin in no_daemon_coins: continue g.proto = CoinProtocol(coin, g.testnet) vmsg('Checking {} daemon'.format(coin)) try: rpc_init(reinit=True) g.rpch.getbalance() except SystemExit as e: if e[0] != 0: ydie( 1, '{} daemon not running or not listening on port {}'.format( coin, g.proto.rpc_port))
def get_addr_from_addrlist(self,user,sid,mmtype,idx,addr_range='1-5'): id_str = { 'L':'', 'S':'-S', 'C':'-C', 'B':'-B' }[mmtype] ext = '{}{}{}[{}]{x}.testnet.addrs'.format( sid,self.altcoin_pfx,id_str,addr_range,x='-α' if g.debug_utf8 else '') addrfile = get_file_with_ext(self._user_dir(user),ext,no_dot=True) psave = g.proto g.proto = CoinProtocol(g.coin,True) if hasattr(g.proto,'bech32_hrp_rt'): g.proto.bech32_hrp = g.proto.bech32_hrp_rt silence() addr = AddrList(addrfile).data[idx].addr end_silence() g.proto = psave return addr
def parse_addrfile_label( lbl): # we must maintain backwards compat, so parse is tricky al_coin, al_mmtype = None, None lbl = lbl.split(':', 1) if len(lbl) == 2: al_coin, al_mmtype = lbl[0], lbl[1].lower() else: if lbl[0].lower() in MMGenAddrType.get_names(): al_mmtype = lbl[0].lower() else: al_coin = lbl[0] # this block fails if al_mmtype is invalid for g.coin if not al_mmtype: mmtype = MMGenAddrType('E' if al_coin in ('ETH', 'ETC') else 'L', on_fail='raise') else: mmtype = MMGenAddrType(al_mmtype, on_fail='raise') from mmgen.protocol import CoinProtocol base_coin = CoinProtocol(al_coin or 'BTC', testnet=False).base_coin return base_coin, mmtype
def init(opts_f, add_opts=[], opt_filter=None): from mmgen.protocol import CoinProtocol, BitcoinProtocol, init_genonly_altcoins g.proto = BitcoinProtocol # this must be initialized to something before opts_f is called # most, but not all, of these set the corresponding global var common_opts_data = """ --, --accept-defaults Accept defaults at all prompts --, --coin=c Choose coin unit. Default: {cu_dfl}. Options: {cu_all} --, --color=0|1 Disable or enable color output --, --force-256-color Force 256-color output when color is enabled --, --daemon-data-dir=d Specify coin daemon data directory location 'd' --, --data-dir=d Specify {pnm} data directory location 'd' --, --no-license Suppress the GPL license prompt --, --rpc-host=h Communicate with {dn} running on host 'h' --, --rpc-port=p Communicate with {dn} listening on port 'p' --, --rpc-user=user Override 'rpcuser' in {pn}.conf --, --rpc-password=pass Override 'rpcpassword' in {pn}.conf --, --regtest=0|1 Disable or enable regtest mode --, --testnet=0|1 Disable or enable testnet --, --skip-cfg-file Skip reading the configuration file --, --version Print version information and exit --, --bob Switch to user "Bob" in MMGen regtest setup --, --alice Switch to user "Alice" in MMGen regtest setup """.format(pnm=g.proj_name, pn=g.proto.name, dn=g.proto.daemon_name, cu_dfl=g.coin, cu_all=' '.join(CoinProtocol.coins)) opts_data = opts_f() opts_data['long_options'] = common_opts_data version_info = """ {pgnm_uc} version {g.version} Part of the {pnm} suite, an online/offline cryptocoin wallet for the command line. Copyright (C) {g.Cdates} {g.author} {g.email} """.format(pnm=g.proj_name, g=g, pgnm_uc=g.prog_name.upper()).strip() uopts,args,short_opts,long_opts,skipped_opts,do_help = \ mmgen.share.Opts.parse_opts(sys.argv,opts_data,opt_filter=opt_filter,skip_help=True) if g.debug_opts: opt_preproc_debug(short_opts, long_opts, skipped_opts, uopts, args) # Save this for usage() global usage_txt usage_txt = opts_data['usage'] # Transfer uopts into opt, setting program's opts + required opts to None if not set by user for o in tuple([s.rstrip('=') for s in long_opts] + add_opts + skipped_opts) + \ g.required_opts + g.common_opts: setattr(opt, o, uopts[o] if o in uopts else None) if opt.version: Die(0, version_info) from mmgen.common import set_debug_all set_debug_all() # === Interaction with global vars begins here === # NB: user opt --data-dir is actually g.data_dir_root # cfg file is in g.data_dir_root, wallet and other data are in g.data_dir # We must set g.data_dir_root and g.cfg_file from cmdline before processing cfg file set_data_dir_root() if not opt.skip_cfg_file: override_from_cfg_file(get_data_from_cfg_file()) override_from_env() # User opt sets global var - do these here, before opt is set from g.global_sets_opt for k in g.common_opts: val = getattr(opt, k) if val != None: setattr(g, k, set_for_type(val, getattr(g, k), '--' + k)) if g.regtest: g.testnet = True # These are equivalent for now altcoin_trust_level = init_genonly_altcoins(opt.coin) # g.testnet is set, so we can set g.proto g.proto = CoinProtocol(g.coin, g.testnet) # global sets proto if g.daemon_data_dir: g.proto.daemon_data_dir = g.daemon_data_dir # g.proto is set, so we can set g.data_dir g.data_dir = os.path.normpath( os.path.join(g.data_dir_root, g.proto.data_subdir)) # If user opt is set, convert its type based on value in mmgen.globalvars (g) # If unset, set it to default value in mmgen.globalvars (g) setattr(opt, 'set_by_user', []) for k in g.global_sets_opt: if k in opt.__dict__ and getattr(opt, k) != None: setattr(opt, k, set_for_type(getattr(opt, k), getattr(g, k), '--' + k)) opt.set_by_user.append(k) else: setattr(opt, k, g.__dict__[k]) if opt.show_hash_presets: _show_hash_presets() sys.exit(0) if opt.verbose: opt.quiet = None die_on_incompatible_opts(g.incompatible_opts) opt_postproc_initializations() if do_help: # print help screen only after global vars are initialized opts_data = opts_f() opts_data['long_options'] = common_opts_data if g.debug_utf8: for k in opts_data: if type(opts_data[k]) in (str, unicode): opts_data[k] += u'-α' mmgen.share.Opts.parse_opts(sys.argv, opts_data, opt_filter=opt_filter) if g.bob or g.alice: g.testnet = True g.regtest = True g.proto = CoinProtocol(g.coin, g.testnet) g.data_dir = os.path.join(g.data_dir_root, 'regtest', g.coin.lower(), ('alice', 'bob')[g.bob]) check_or_create_dir(g.data_dir) import regtest as rt g.rpc_host = 'localhost' g.rpc_port = rt.rpc_port g.rpc_user = rt.rpc_user g.rpc_password = rt.rpc_password if g.regtest and hasattr(g.proto, 'bech32_hrp_rt'): g.proto.bech32_hrp = g.proto.bech32_hrp_rt # Check user-set opts without modifying them if not check_opts(uopts): sys.exit(1) if hasattr(g, 'cfg_options_changed'): ymsg("Warning: config file options have changed! See '{}' for details". format(g.cfg_file + '.sample')) my_raw_input('Hit ENTER to continue: ') if g.debug and g.prog_name != 'test.py': opt.verbose, opt.quiet = True, None if g.debug_opts: opt_postproc_debug() # We don't need this data anymore del mmgen.share.Opts del opts_f for k in ('prog_name', 'desc', 'usage', 'options', 'notes'): if k in opts_data: del opts_data[k] g.altcoin_data_dir = os.path.join(g.data_dir, 'altcoins') warn_altcoins(altcoin_trust_level) return args
assert 1 <= b <= len(g.key_generators),'{}: invalid key generator'.format(b) assert a != b,'Key generators are the same!' except Exception as e: die(1,'{}\n{}: invalid generator argument'.format(e[0],cmd_args[0])) from mmgen.addr import KeyGenerator,AddrGenerator from mmgen.obj import PrivKey kg_a = KeyGenerator(addr_type,a) ag = AddrGenerator(addr_type) if a and b: if opt.all: from mmgen.protocol import init_coin,init_genonly_altcoins,CoinProtocol init_genonly_altcoins('btc',trust_level=0) mmgen_supported = CoinProtocol.get_valid_coins(upcase=True) for coin in ci.external_tests[('mainnet','testnet')[g.testnet]][ext_lib]: if coin not in mmgen_supported: continue init_coin(coin) tmp_addr_type = addr_type if addr_type in g.proto.mmtypes else MMGenAddrType(g.proto.dfl_mmtype) kg_a = KeyGenerator(tmp_addr_type,a) ag = AddrGenerator(tmp_addr_type) compare_test() else: if b != 'ext': kg_b = KeyGenerator(addr_type,b) b_desc = kg_b.desc compare_test() elif a and not fh: speed_test() elif a and dump:
g.key_generators), '{}: invalid key generator'.format(b) assert a != b, 'Key generators are the same!' except Exception as e: die(1, '{}\n{}: invalid generator argument'.format(e[0], cmd_args[0])) from mmgen.addr import KeyGenerator, AddrGenerator from mmgen.obj import PrivKey kg_a = KeyGenerator(addr_type, a) ag = AddrGenerator(addr_type) if a and b: if opt.all: from mmgen.protocol import init_coin, init_genonly_altcoins, CoinProtocol init_genonly_altcoins('btc', trust_level=0) mmgen_supported = CoinProtocol.get_valid_coins(upcase=True) for coin in ci.external_tests[('mainnet', 'testnet')[g.testnet]][ext_lib]: if coin not in mmgen_supported: continue init_coin(coin) tmp_addr_type = addr_type if addr_type in g.proto.mmtypes else MMGenAddrType( g.proto.dfl_mmtype) kg_a = KeyGenerator(tmp_addr_type, a) ag = AddrGenerator(tmp_addr_type) compare_test() else: if b != 'ext': kg_b = KeyGenerator(addr_type, b) b_desc = kg_b.desc compare_test() elif a and not fh:
def alice_add_label_badaddr4(self): addr = CoinProtocol(g.coin, True).pubhash2addr('00' * 20, False) # testnet zero address return self.alice_add_label_badaddr( addr, "Address '{}' not found in tracking wallet".format(addr))
def init(opts_data, add_opts=[], opt_filter=None, parse_only=False): opts_data['text']['long_options'] = common_opts_data['text'] uopts,args,short_opts,long_opts,skipped_opts = \ mmgen.share.Opts.parse_opts(opts_data,opt_filter=opt_filter,parse_only=parse_only) if parse_only: return uopts, args, short_opts, long_opts, skipped_opts if g.debug_opts: opt_preproc_debug(short_opts, long_opts, skipped_opts, uopts, args) # Save this for usage() global usage_txt usage_txt = opts_data['text']['usage'] # Transfer uopts into opt, setting program's opts + required opts to None if not set by user for o in (tuple([s.rstrip('=') for s in long_opts] + add_opts + skipped_opts) + g.required_opts + g.common_opts): setattr(opt, o, uopts[o] if o in uopts else None) if opt.version: Die( 0, """ {pn} version {g.version} Part of the {g.proj_name} suite, an online/offline cryptocoin wallet for the command line. Copyright (C) {g.Cdates} {g.author} {g.email} """.format(g=g, pn=g.prog_name.upper()).lstrip('\n').rstrip()) if os.getenv('MMGEN_DEBUG_ALL'): for name in g.env_opts: if name[:11] == 'MMGEN_DEBUG': os.environ[name] = '1' # === Interaction with global vars begins here === # NB: user opt --data-dir is actually g.data_dir_root # cfg file is in g.data_dir_root, wallet and other data are in g.data_dir # We must set g.data_dir_root and g.cfg_file from cmdline before processing cfg file set_data_dir_root() if not opt.skip_cfg_file: override_from_cfg_file(get_data_from_cfg_file()) override_from_env() # User opt sets global var - do these here, before opt is set from g.global_sets_opt for k in (g.common_opts + g.opt_sets_global): if hasattr(opt, k): val = getattr(opt, k) if val != None: setattr(g, k, set_for_type(val, getattr(g, k), '--' + k)) if g.regtest: g.testnet = True # These are equivalent for now from mmgen.protocol import init_genonly_altcoins, CoinProtocol altcoin_trust_level = init_genonly_altcoins(opt.coin) # g.testnet is set, so we can set g.proto g.proto = CoinProtocol(g.coin, g.testnet) # global sets proto if g.daemon_data_dir: g.proto.daemon_data_dir = g.daemon_data_dir # g.proto is set, so we can set g.data_dir g.data_dir = os.path.normpath( os.path.join(g.data_dir_root, g.proto.data_subdir)) # If user opt is set, convert its type based on value in mmgen.globalvars (g) # If unset, set it to default value in mmgen.globalvars (g) setattr(opt, 'set_by_user', []) for k in g.global_sets_opt: if k in opt.__dict__ and getattr(opt, k) != None: setattr(opt, k, set_for_type(getattr(opt, k), getattr(g, k), '--' + k)) opt.set_by_user.append(k) else: setattr(opt, k, g.__dict__[k]) if opt.show_hash_presets: _show_hash_presets() sys.exit(0) if opt.verbose: opt.quiet = None die_on_incompatible_opts(g.incompatible_opts) opt_postproc_initializations() if opts_data[ 'do_help']: # print help screen only after global vars are initialized if not 'code' in opts_data: opts_data['code'] = {} opts_data['code']['long_options'] = common_opts_data['code'] if g.debug_utf8: for k in opts_data: if type(opts_data[k]) == str: opts_data[k] += '-α' mmgen.share.Opts.print_help(opts_data, opt_filter) # exits if g.bob or g.alice: g.testnet = True g.regtest = True g.proto = CoinProtocol(g.coin, g.testnet) g.data_dir = os.path.join(g.data_dir_root, 'regtest', g.coin.lower(), ('alice', 'bob')[g.bob]) from . import regtest as rt g.rpc_host = 'localhost' g.rpc_port = rt.rpc_port g.rpc_user = rt.rpc_user g.rpc_password = rt.rpc_password check_or_create_dir( g.data_dir) # g.data_dir is finalized, so now we can do this if g.regtest and hasattr(g.proto, 'bech32_hrp_rt'): g.proto.bech32_hrp = g.proto.bech32_hrp_rt # Check user-set opts without modifying them if not check_opts(uopts): die(1, 'Options checking failed') if hasattr(g, 'cfg_options_changed'): ymsg("Warning: config file options have changed! See '{}' for details". format(g.cfg_file + '.sample')) from mmgen.util import my_raw_input my_raw_input('Hit ENTER to continue: ') if g.debug and g.prog_name != 'test.py': opt.verbose, opt.quiet = (True, None) if g.debug_opts: opt_postproc_debug() g.altcoin_data_dir = os.path.join(g.data_dir_root, 'altcoins') warn_altcoins(altcoin_trust_level) # We don't need this data anymore del mmgen.share.Opts, opts_data return args