def test_bip32_descend(): master = btc.bip32_master_key('\x07'*32) end_key = btc.bip32_descend(master, [2, 3, 10000]) assert end_key=="6856ef965940a1a7b1311dc041050ac0013e326c7ff4e2c677a7694b4f0405c901" end_key = btc.bip32_descend(master, 2, 5, 4, 5) assert end_key=="d2d816b6485103c0d7ff95482788f0e8e73fa11817079e006d47979d8196c4b101"
def derive_keypairs(hd_key, keys=3): keypairs = [] for i in range(keys): privkey = encode_privkey(bip32_descend(hd_key, [i]), 'hex') addr = decode_addr(ethereum.keys.privtoaddr(privkey)).decode('utf-8') keypairs.append((privkey, addr)) return keypairs
def seed2pvkey(seed, path, bip44=True): if bip44: master_xprv = bitcoin.bip32_master_key(seed, b'\x04\x88\xAD\xE4') path_list = path2list(path) return bitcoin.bip32_descend(master_xprv, path_list) else: return seed + "00"
def address(args): c = 1 if args.change else 0 if (args.index < 0): unspents = bitcoin.BlockchainInfo.unspent_xpub(args.xpub) index = check_outputs_max_index(unspents, c) else: index = args.index address = bitcoin.pubtoaddr(bitcoin.bip32_descend(args.xpub, c, index)) print(address)
def address(args): c = 1 if args.change else 0 if args.index < 0: unspents = bitcoin.BlockchainInfo.unspent_xpub(args.xpub) index = check_outputs_max_index(unspents, c) else: index=args.index address = bitcoin.pubtoaddr(bitcoin.bip32_descend(args.xpub, c, index)) print(address)
def generate_new_address(self, index): """ Generate new bitcoin address from a hd public master key based on a particlar index Address can be generated sequentially like in the case of electrum :param index: Index to use to generate address :return: Generated address """ address = btc.pubkey_to_address( btc.bip32_descend(self.public_key, [0, index])) return address
def address(args): c = 1 if args.change else 0 if (args.index < 0): unspents = bitcoin.BlockchainInfo.unspent_xpub(args.xpub) index = check_outputs_max_index(unspents, c) else: index = args.index if (coin_arg_parse(args.coin) != coin_arg_parse('BTC')): print("Error: not known how to build address for coin \"" + args.coin + "\"") address = bitcoin.pubtoaddr(bitcoin.bip32_descend(args.xpub, c, index)) print(address)
def send(args): if (len(args.outputs) % 2 != 0): raise Exception( "When sending, there must be an even number of arguments for the outputs (address,price)" ) unspents = bitcoin.BlockchainInfo.unspent_xpub(args.xpub) def btctosatoshi(vs): return int(float(vs) * 100000000.0) fee = btctosatoshi(args.fee) if (fee < 0): fee = int( 0.0001 * 100000000 ) #todo do something to estimated fee...make it negative or something though... DONT outaddrval = [(args.outputs[2 * i], btctosatoshi(args.outputs[2 * i + 1])) for i in range(len(args.outputs) // 2)] outtotalval = sum([o[1] for o in outaddrval]) changeindex = check_outputs_max_index(unspents, 1) changeaddress = bitcoin.pubtoaddr( bitcoin.bip32_descend(args.xpub, 1, changeindex)) unspents = bitcoin.select(unspents, outtotalval + fee) #unspents cull using outtotalval unspenttotalval = sum([u['value'] for u in unspents]) changeamount = unspenttotalval - (outtotalval + abs(fee)) if (changeamount < 0): raise Exception( "There is unlikely to be enough unspent outputs to cover the transaction and fees" ) out = {} outs = outaddrval if (changeamount > 0): outs += [[changeaddress, changeamount]] #print(unspents) #print(outs) otx = [{'address': o[0], 'value': o[1]} for o in outs] tx = bitcoin.mktx(unspents, otx) #compute all the underlying addresses and pubkeys into a string hash #estimate the fee #build the transaction out['tx'] = tx u['xpub']['value'] = u['value'] out['keys'] = dict([(u['output'], u['xpub']) for u in unspents]) #print(bitcoin.deserialize(tx)) json.dump(out, sys.stdout)
def sign(args): master_key = get_master_key() input_transaction = json.load(args.input_file) privs = input_transaction['keys'] tx = input_transaction['tx'] for k, p in privs.items(): pstr = bitcoin.bip32_path_from_string(p['path']) xpubk = p['m'] a = 0 priv_key = bitcoin.hd_lookup(master_key, account=a) while (bitcoin.bip32_privtopub(priv_key) != xpubk): priv_key = bitcoin.hd_lookup(master_key, account=a) a += 1 privs[k] = bitcoin.bip32_descend(priv_key, pstr[0], pstr[1]) print(bitcoin.signall(str(tx), privs))
def sign(args): master_key = get_master_key() input_transaction = json.load(args.input_file) privs = input_transaction['keys'] tx = input_transaction['tx'] for k, p in privs.items(): pstr = bitcoin.bip32_path_from_string(p['path']) xpubk = p['m'] a = 0 priv_key = bitcoin.hd_lookup(master_key, account = a) while bitcoin.bip32_privtopub(priv_key) != xpubk: priv_key = bitcoin.hd_lookup(master_key, account = a) a += 1 privs[k] = bitcoin.bip32_descend(priv_key, pstr[0], pstr[1]) print(bitcoin.signall(str(tx), privs))
def send(args): if len(args.outputs) % 2 != 0: raise Exception("When sending, there must be an even number of arguments " + "for the outputs (address, price)") unspents = bitcoin.BlockchainInfo.unspent_xpub(args.xpub) def btctosatoshi(vs): return int(float(vs)*100000000.0) fee = btctosatoshi(args.fee) if fee < 0: fee = int(0.0001*100000000) #todo do something to estimated fee...make it negative or something though... DONT outaddrval = [(args.outputs[2*i],btctosatoshi(args.outputs[2*i+1])) for i in range(len(args.outputs)//2)] outtotalval = sum([o[1] for o in outaddrval]) changeindex = check_outputs_max_index(unspents,1) changeaddress = bitcoin.pubtoaddr(bitcoin.bip32_descend(args.xpub, 1, changeindex)) unspents = bitcoin.select(unspents, outtotalval+fee) #unspents cull using outtotalval unspenttotalval = sum([u['value'] for u in unspents]) changeamount = unspenttotalval - (outtotalval + abs(fee)) if changeamount < 0: raise Exception("There is unlikely to be enough unspent outputs to cover the transaction and fees") out = {} outs = outaddrval if changeamount > 0: outs += [[changeaddress, changeamount]] #print(unspents) #print(outs) otx = [{'address':o[0], 'value':o[1]} for o in outs] tx = bitcoin.mktx(unspents, otx) #compute all the underlying addresses and pubkeys into a string hash #estimate the fee #build the transaction out['tx'] = tx out['keys'] = dict([(u['output'], u['xpub']) for u in unspents]) #print(bitcoin.deserialize(tx)) json.dump(out, sys.stdout)
import bitcoin from bitcoin.deterministic import bip32_harden as h mnemonic = 'saddle observe obtain scare burger nerve electric alone minute east walnut motor omit coyote time' seed = bitcoin.mnemonic_to_seed(mnemonic) mpriv = bitcoin.bip32_master_key(seed) accountroot = mpriv accountroot = bitcoin.bip32_ckd(accountroot, h(44)) accountroot = bitcoin.bip32_ckd(accountroot, h(0)) accountroot = bitcoin.bip32_ckd(accountroot, h(0)) for i in range(19): dkey = bitcoin.bip32_descend(accountroot, 0, i) print(bitcoin.privtoaddr(dkey))
import bitcoin from bitcoin.deterministic import bip32_harden as h mnemonic='saddle observe obtain scare burger nerve electric alone minute east walnut motor omit coyote time' seed=bitcoin.mnemonic_to_seed(mnemonic) mpriv=bitcoin.bip32_master_key(seed) accountroot=mpriv accountroot=bitcoin.bip32_ckd(accountroot,h(44)) accountroot=bitcoin.bip32_ckd(accountroot,h(0)) accountroot=bitcoin.bip32_ckd(accountroot,h(0)) for i in range(19): dkey=bitcoin.bip32_descend(accountroot,0,i) print(bitcoin.privtoaddr(dkey))