def do_compare_test(n, trounds, in_bytes): global last_t if opt.verbose or time.time() - last_t >= 0.1: qmsg_r('\rRound {}/{} '.format(i + 1, trounds)) last_t = time.time() sec = PrivKey(proto, in_bytes, compressed=addr_type.compressed, pubkey_type=addr_type.pubkey_type) a_ph = kg_a.to_pubhex(sec) a_addr = ag.to_addr(a_ph) a_vk = None tinfo = (in_bytes, sec, sec.wif, kg_a.desc, kg_b.desc) if isinstance(kg_b, GenTool): b = kg_b.run_tool(sec) test_equal('WIF keys', sec.wif, b.wif, *tinfo) test_equal('addresses', a_addr, b.addr, *tinfo) if b.vk: a_vk = ag.to_viewkey(a_ph) test_equal('view keys', a_vk, b.vk, *tinfo) else: b_addr = ag.to_addr(kg_b.to_pubhex(sec)) test_equal('addresses', a_addr, b_addr, *tinfo) vmsg(fs.format(b=in_bytes.hex(), k=sec.wif, v=a_vk, a=a_addr)) qmsg_r('\rRound {}/{} '.format(n + 1, trounds))
def test_randpair(t): wif, addr = t.randpair() wif_chk = PrivKey(proto=t.proto, wif=wif).wif check_equal(wif, wif_chk) msg('\n === randwif ===') msg(' wif:', wif) msg(' addr:', CoinAddr(proto=t.proto, addr=addr))
def test_wif2addr(t, wif_chk, addr_chk, key_idx): key_bytes = bytes.fromhex(keys[key_idx]) wif = PrivKey(proto=t.proto, s=key_bytes, compressed=t.addrtype.compressed, pubkey_type=t.addrtype.pubkey_type).wif addr = t.wif2addr(wif) msg('\n === wif2addr ===') msg(' wif:', PrivKey(proto=t.proto, wif=wif).wif) msg(' addr:', CoinAddr(proto=t.proto, addr=addr)) addr_ph = t.privhex2addr(key_bytes.hex()) check_equal(addr, addr_ph) check_equal(wif, wif_chk) check_equal(addr, addr_chk)
def compare_test(): for k in ('segwit', 'compressed'): if b == 'ext' and addr_type.name == k and g.coin not in ci.external_tests_segwit_compressed[ k]: m = 'skipping - external program does not support {} for coin {}' msg(m.format(addr_type.name.capitalize(), g.coin)) return if 'ext_lib' in globals(): if g.coin not in ci.external_tests[('mainnet', 'testnet')[g.testnet]][ext_lib]: msg("Coin '{}' incompatible with external generator '{}'".format( g.coin, ext_lib)) return m = "Comparing address generators '{}' and '{}' for coin {}" last_t = time.time() A = kg_a.desc B = ext_lib if b == 'ext' else kg_b.desc if A == B: msg('skipping - generation methods A and B are the same ({})'.format( A)) return qmsg(green(m.format(A, B, g.coin))) for i in range(rounds): if opt.verbose or time.time() - last_t >= 0.1: qmsg_r('\rRound {}/{} '.format(i + 1, rounds)) last_t = time.time() sec = PrivKey(os.urandom(32), compressed=addr_type.compressed, pubkey_type=addr_type.pubkey_type) ph = kg_a.to_pubhex(sec) a_addr = ag.to_addr(ph) if addr_type.name == 'zcash_z': a_vk = ag.to_viewkey(ph) if b == 'ext': if addr_type.name == 'zcash_z': b_wif, b_addr, b_vk = ext_sec2addr(sec) vmsg_r('\nvkey: {}'.format(b_vk)) if b_vk != a_vk: match_error(sec, sec.wif, a_vk, b_vk, a, b) else: b_wif, b_addr = ext_sec2addr(sec) if b_wif != sec.wif: match_error(sec, sec.wif, sec.wif, b_wif, a, b) else: b_addr = ag.to_addr(kg_b.to_pubhex(sec)) vmsg('\nkey: {}\naddr: {}\n'.format(sec.wif, a_addr)) if a_addr != b_addr: match_error(sec, sec.wif, a_addr, b_addr, a, ext_lib if b == 'ext' else b) qmsg_r('\rRound {}/{} '.format(i + 1, rounds)) qmsg(green(('\n', '')[bool(opt.verbose)] + 'OK'))
def dump_test(): m = "Comparing output of address generator '{}' against wallet dump '{}'" qmsg(green(m.format(kg_a.desc,cmd_args[1]))) for n,[wif,a_addr] in enumerate(dump,1): qmsg_r('\rKey {}/{} '.format(n,len(dump))) try: sec = PrivKey(wif=wif) except: die(2,'\nInvalid {}net WIF address in dump file: {}'.format(('main','test')[g.testnet],wif)) b_addr = ag.to_addr(kg_a.to_pubhex(sec)) vmsg('\nwif: {}\naddr: {}\n'.format(wif,b_addr)) if a_addr != b_addr: match_error(sec,wif,a_addr,b_addr,3,a) qmsg(green(('\n','')[bool(opt.verbose)] + 'OK'))
def _create_fake_unspent_data(self, adata, tx_data, non_mmgen_input='', non_mmgen_input_compressed=True): out = [] for d in tx_data.values(): al = adata.addrlist(al_id=d['al_id']) for n, (idx, coinaddr) in enumerate(al.addrpairs()): lbl = get_label(do_shuffle=True) out.append( self._create_fake_unspent_entry(coinaddr, d['al_id'], idx, lbl, segwit=d['segwit'])) if n == 0: # create a duplicate address. This means addrs_per_wallet += 1 out.append( self._create_fake_unspent_entry(coinaddr, d['al_id'], idx, lbl, segwit=d['segwit'])) if non_mmgen_input: from mmgen.obj import PrivKey privkey = PrivKey(self.proto, os.urandom(32), compressed=non_mmgen_input_compressed, pubkey_type='std') from mmgen.addr import AddrGenerator, KeyGenerator rand_coinaddr = AddrGenerator(self.proto, 'compressed').to_addr( KeyGenerator(self.proto, 'std').to_pubhex(privkey)) of = joinpath(self.cfgs[non_mmgen_input]['tmpdir'], non_mmgen_fn) write_data_to_file(outfile=of, data=privkey.wif + '\n', desc=f'compressed {self.proto.name} key', quiet=True, ignore_opt_outdir=True) out.append( self._create_fake_unspent_entry(rand_coinaddr, non_mmgen=True, segwit=False)) return out
def speed_test(): m = "Testing speed of address generator '{}' for coin {}" qmsg(green(m.format(kg_a.desc,g.coin))) from struct import pack,unpack seed = os.urandom(28) print 'Incrementing key with each round' print 'Starting key:', hexlify(seed+pack('I',0)) import time start = last_t = time.time() for i in range(rounds): if time.time() - last_t >= 0.1: qmsg_r('\rRound {}/{} '.format(i+1,rounds)) last_t = time.time() sec = PrivKey(seed+pack('I',i),compressed=addr_type.compressed,pubkey_type=addr_type.pubkey_type) a_addr = ag.to_addr(kg_a.to_pubhex(sec)) vmsg('\nkey: {}\naddr: {}\n'.format(sec.wif,a_addr)) qmsg_r('\rRound {}/{} '.format(i+1,rounds)) qmsg('\n{} addresses generated in {:.2f} seconds'.format(rounds,time.time()-start))
def dump_test(kg,ag,fh): dump = [[*(e.split()[0] for e in line.split('addr='))] for line in fh.readlines() if 'addr=' in line] if not dump: die(1,'File {!r} appears not to be a wallet dump'.format(fh.name)) m = 'Comparing output of address generator {!r} against wallet dump {!r}' qmsg(green(m.format(kg.desc,fh.name))) for count,(b_wif,b_addr) in enumerate(dump,1): qmsg_r('\rKey {}/{} '.format(count,len(dump))) try: b_sec = PrivKey(wif=b_wif) except: die(2,'\nInvalid {} WIF address in dump file: {}'.format(g.proto.network,b_wif)) a_addr = ag.to_addr(kg.to_pubhex(b_sec)) vmsg('\nwif: {}\naddr: {}\n'.format(b_wif,b_addr)) tinfo = (bytes.fromhex(b_sec),b_sec,b_wif,kg.desc,fh.name) test_equal('addresses',a_addr,b_addr,*tinfo) qmsg(green(('\n','')[bool(opt.verbose)] + 'OK'))
def _make_txcreate_cmdline(self, tx_data): from mmgen.obj import PrivKey privkey = PrivKey(self.proto, os.urandom(32), compressed=True, pubkey_type='std') t = ('compressed', 'segwit')['S' in self.proto.mmtypes] from mmgen.addr import AddrGenerator, KeyGenerator rand_coinaddr = AddrGenerator(self.proto, t).to_addr( KeyGenerator(self.proto, 'std').to_pubhex(privkey)) # total of two outputs must be < 10 BTC (<1000 LTC) mods = { 'btc': (6, 4), 'bch': (6, 4), 'ltc': (600, 400) }[self.proto.coin.lower()] for k in self.cfgs: self.cfgs[k]['amts'] = [None, None] for idx, mod in enumerate(mods): self.cfgs[k]['amts'][idx] = '{}.{}'.format( getrandnum(4) % mod, str(getrandnum(4))[:5]) cmd_args = ['--outdir=' + self.tmpdir] for num in tx_data: s = tx_data[num] cmd_args += [ '{}:{},{}'.format(s['al_id'], s['addr_idxs'][0], self.cfgs[num]['amts'][0]), ] # + one change address and one BTC address if num is list(tx_data.keys())[-1]: cmd_args += ['{}:{}'.format(s['al_id'], s['addr_idxs'][1])] cmd_args += [ '{},{}'.format(rand_coinaddr, self.cfgs[num]['amts'][1]) ] return cmd_args + [tx_data[num]['addrfile'] for num in tx_data]
from mmgen.obj import PrivKey ag = AddrGenerator(('p2pkh', 'segwit')[bool(opt.segwit)]) if a and b: m = "Comparing address generators '{}' and '{}'" qmsg(green(m.format(g.key_generators[a - 1], g.key_generators[b - 1]))) last_t = time.time() kg_a = KeyGenerator(a) kg_b = KeyGenerator(b) for i in range(rounds): if time.time() - last_t >= 0.1: qmsg_r('\rRound %s/%s ' % (i + 1, rounds)) last_t = time.time() sec = PrivKey(os.urandom(32), compressed) a_addr = ag.to_addr(kg_a.to_pubhex(sec)) b_addr = ag.to_addr(kg_b.to_pubhex(sec)) vmsg('\nkey: %s\naddr: %s\n' % (sec.wif, a_addr)) if a_addr != b_addr: match_error(sec, sec.wif, a_addr, b_addr, a, b) if not opt.segwit: compressed = not compressed qmsg_r('\rRound %s/%s ' % (i + 1, rounds)) qmsg(green(('\n', '')[bool(opt.verbose)] + 'OK')) elif a and not fh: m = "Testing speed of address generator '{}'" qmsg(green(m.format(g.key_generators[a - 1]))) from struct import pack, unpack seed = os.urandom(28)