def randpair(compressed=False): r_hex = ba.hexlify(get_random(32)) wif = bitcoin.hextowif(r_hex,compressed) addr = bitcoin.privnum2addr(int(r_hex,16),compressed) Vmsg("Key (hex): %s" % r_hex) Vmsg_r("Key (WIF): "); Msg(wif) Vmsg_r("Addr: "); Msg(addr)
def wiftoaddr(s): if s == "": return False compressed = not s[0] == '5' from mmgen.bitcoin import wiftohex,privnum2addr hex_key = wiftohex(s,compressed) if not hex_key: return False return privnum2addr(int(hex_key,16),compressed)
def generate_addrs(seed, addrnums, source="addrgen"): from util import make_chksum_8 seed_id = make_chksum_8(seed) # Must do this before seed gets clobbered if 'a' in opt.gen_what: if opt.no_keyconv or test_for_keyconv() == False: msg("Using (slow) internal ECDSA library for address generation") from mmgen.bitcoin import privnum2addr keyconv = False else: from subprocess import check_output keyconv = "keyconv" addrnums = sorted(set(addrnums)) # don't trust the calling function t_addrs,num,pos,out = len(addrnums),0,0,[] w = { 'ka': ('key/address pair','s'), 'k': ('key','s'), 'a': ('address','es') }[opt.gen_what] from mmgen.addr import AddrInfoEntry,AddrInfo while pos != t_addrs: seed = sha512(seed).digest() num += 1 # round if num != addrnums[pos]: continue pos += 1 qmsg_r("\rGenerating %s #%s (%s of %s)" % (w[0],num,pos,t_addrs)) e = AddrInfoEntry() e.idx = num # Secret key is double sha256 of seed hash round /num/ sec = sha256(sha256(seed).digest()).hexdigest() wif = numtowif(int(sec,16)) if 'a' in opt.gen_what: if keyconv: e.addr = check_output([keyconv, wif]).split()[1] else: e.addr = privnum2addr(int(sec,16)) if 'k' in opt.gen_what: e.wif = wif if opt.b16: e.sec = sec out.append(e) m = w[0] if t_addrs == 1 else w[0]+w[1] qmsg("\r%s: %s %s generated%s" % (seed_id,t_addrs,m," "*15)) a = AddrInfo(has_keys='k' in opt.gen_what, source=source) a.initialize(seed_id,out) return a
def Privhex2addr(privhex, compressed=False, segwit=False): if segwit and not compressed: die(1, 'Segwit address can be generated only from a compressed pubkey') Msg(mmb.privnum2addr(int(privhex, 16), compressed, segwit=segwit))
if opt.system: sys.path.pop(0) from mmgen.addr import test_for_keyconv if not test_for_keyconv(silent=True): msg( "To run this test, you must install 'keyconv' from the vanitygen package.") sys.exit(1) msg(green("Comparing {}'s internally generated addresses against output of 'keyconv'").format(g.proj_name)) from subprocess import check_output for i in range(1,rounds+1): msg_r("\rRound %s/%s " % (i,rounds)) sec = hexlify(os.urandom(32)) wif = hextowif(sec) a = privnum2addr(int(sec,16)) vmsg("\nkey: %s\naddr: %s\n" % (wif,a)) b = check_output(["keyconv", wif]).split()[1] if a != b: msg_r(red("\nERROR: Addresses do not match!")) msg(""" sec key: {} WIF key: {} {pnm}: {} keyconv: {} """.format(sec,wif,a,b,pnm=g.proj_name).rstrip()) sys.exit(3) msg(green("%sOK" % ("" if opt.verbose else "\n")))
def privhex2addr(privkeyhex,compressed=False): Msg(bitcoin.privnum2addr(int(privkeyhex,16),compressed))
def wif2addr(wif,compressed=False): s_enc = bitcoin.wiftohex(wif,compressed) if s_enc == False: die(1,"Invalid address") addr = bitcoin.privnum2addr(int(s_enc,16),compressed) Vmsg_r("Addr: "); Msg(addr)