From 1a21ce2bce00aca4b63dbeedd2dfe1b8fe5de9de Mon Sep 17 00:00:00 2001 From: MMGen Date: Wed, 30 May 2018 14:45:10 +0000 Subject: [PATCH] Testnet-specific addrlists --- mmgen/addr.py | 18 ++++++++++++++---- mmgen/protocol.py | 4 ++++ mmgen/tw.py | 5 ++--- mmgen/tx.py | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index 75089245..51a3b6db 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -462,15 +462,17 @@ Removed {{}} duplicate WIF key{{}} from keylist (also in {pnm} key-address file def scramble_seed(self,seed): is_btcfork = g.proto.base_coin == 'BTC' - if is_btcfork and self.al_id.mmtype == 'L': + if is_btcfork and self.al_id.mmtype == 'L' and not g.proto.is_testnet(): dmsg_sc('str','(none)') return seed if g.proto.base_coin == 'ETH': scramble_key = g.coin.lower() else: scramble_key = (g.coin.lower()+':','')[is_btcfork] + self.al_id.mmtype.name - dmsg_sc('str',scramble_key) from mmgen.crypto import scramble_seed + if g.proto.is_testnet(): + scramble_key += ':testnet' + dmsg_sc('str',scramble_key) return scramble_seed(seed,scramble_key,self.scramble_hash_rounds) def encrypt(self,desc='new key list'): @@ -479,7 +481,8 @@ Removed {{}} duplicate WIF key{{}} from keylist (also in {pnm} key-address file self.ext += '.'+g.mmenc_ext def write_to_file(self,ask_tty=True,ask_write_default_yes=False,binary=False,desc=None): - fn = u'{}{x}.{}'.format(self.id_str,self.ext,x=u'-α' if g.debug_utf8 else '') + tn = ('','.testnet')[g.proto.is_testnet()] + fn = u'{}{x}{}.{}'.format(self.id_str,tn,self.ext,x=u'-α' if g.debug_utf8 else '') ask_tty = self.has_keys and not opt.quiet write_data_to_file(fn,self.fmt_data,desc or self.file_desc,ask_tty=ask_tty,binary=binary) @@ -570,7 +573,8 @@ Removed {{}} duplicate WIF key{{}} from keylist (also in {pnm} key-address file bc,mt = g.proto.base_coin,self.al_id.mmtype l_coin = [] if bc == 'BTC' else [g.coin] if bc == 'ETH' else [bc] l_type = [] if mt in ('L','E') else [mt.name.upper()] - lbl_p2 = ':'.join(l_coin+l_type) + l_tn = [] if not g.proto.is_testnet() else ['TESTNET'] + lbl_p2 = ':'.join(l_coin+l_type+l_tn) lbl = self.al_id.sid + ('',' ')[bool(lbl_p2)] + lbl_p2 dmsg_sc('lbl',lbl[9:]) @@ -643,6 +647,12 @@ Removed {{}} duplicate WIF key{{}} from keylist (also in {pnm} key-address file 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() diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 0e3f2bd9..7dde6745 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -91,6 +91,10 @@ class BitcoinProtocol(MMGenObject): witness_vernum = int(witness_vernum_hex,16) bech32_hrp = 'bc' + @classmethod + def is_testnet(cls): + return cls.__name__[-15:] == 'TestnetProtocol' + @staticmethod def get_protocol_by_chain(chain): return CoinProtocol(g.coin,{'mainnet':False,'testnet':True,'regtest':True}[chain]) diff --git a/mmgen/tw.py b/mmgen/tw.py index c12e9a82..b62605ed 100755 --- a/mmgen/tw.py +++ b/mmgen/tw.py @@ -177,8 +177,7 @@ watch-only wallet using '{}-addrimport' and then re-run this program. b.skip = (k,'addr')[k=='twmmid'] out = [self.hdr_fmt.format(' '.join(self.sort_info()),g.coin,self.total.hl())] - if g.chain in ('testnet','regtest'): - out += [green('Chain: {}'.format(g.chain.upper()))] + if g.chain != 'mainnet': out += ['Chain: '+green(g.chain.upper())] if self.show_txid: fs = u' {n:%s} {t:%s} {v:2} {a} {A} {c:<}' % (col1_w,tx_w) else: @@ -417,7 +416,7 @@ class TwAddrList(MMGenDict): self[label.mmid]['addr'] = CoinAddr(addr_arr[0]) def format(self,showbtcaddrs,sort,show_age,show_days): - out = ([],[green('Chain: {}'.format(g.chain.upper()))])[g.chain in ('testnet','regtest')] + out = ['Chain: '+green(g.chain.upper())] if g.chain != 'mainnet' else [] fs = u'{{mid}}{} {{cmt}} {{amt}}{}'.format(('',' {addr}')[showbtcaddrs],('',' {age}')[show_age]) mmaddrs = [k for k in self.keys() if k.type == 'mmgen'] max_mmid_len = max(len(k) for k in mmaddrs) + 2 if mmaddrs else 10 diff --git a/mmgen/tx.py b/mmgen/tx.py index 9e07a4be..c793b253 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -1050,7 +1050,7 @@ class MMGenTX(MMGenObject): s=self.marked_signed(color=True), l=(green('None'),orange(strfmt_locktime(self.locktime,terse=True)))[bool(self.locktime)]) - if self.chain in ('testnet','regtest'): + if self.chain != 'mainnet': out += green('Chain: {}\n'.format(self.chain.upper())) if self.coin_txid: out += '{} TxID: {}\n'.format(g.coin,self.coin_txid.hl())