def get_address_from_output_script(bytes): decoded = [x for x in script_GetOp(bytes)] # The Genesis Block, self-payments, # and pay-by-IP-address payments look like: # 65 BYTES:... CHECKSIG match = [opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG] if match_decoded(decoded, match): return public_key_to_bc_address(decoded[0][1]) # Pay-by-Bitcoin-address TxOuts look like: # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG match = [opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG] if match_decoded(decoded, match): return hash_160_to_bc_address(decoded[2][1]) # p2sh match = [opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL] if match_decoded(decoded, match): return hash_160_to_bc_address(decoded[1][1], 5) return "(None)"
def get_address_from_output_script(bytes): decoded = [x for x in script_GetOp(bytes)] # The Genesis Block, self-payments, # and pay-by-IP-address payments look like: # 65 BYTES:... CHECKSIG match = [opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG] if match_decoded(decoded, match): return public_key_to_bc_address(decoded[0][1]) # Pay-by-Bitcoin-address TxOuts look like: # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG match = [opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG] if match_decoded(decoded, match): return hash_160_to_bc_address(decoded[2][1]) # p2sh match = [opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL] if match_decoded(decoded, match): return hash_160_to_bc_address(decoded[1][1], chain.script_version) return "(None)"
def get_address_from_input_script(bytes): decoded = [ x for x in script_GetOp(bytes) ] # non-generated TxIn transactions push a signature # (seventy-something bytes) and then their public key # (65 bytes) onto the stack: match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ] if match_decoded(decoded, match): return public_key_to_bc_address(decoded[1][1]) # p2sh transaction, 2 of n match = [ opcodes.OP_0, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ] if match_decoded(decoded, match): bytes = decoded[3][1] dec2 = [ x for x in script_GetOp(bytes) ] # 2 of 2 match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ] if match_decoded(dec2, match2): pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ] s = multisig_script(pubkeys) return hash_160_to_bc_address(hash_160(s.decode('hex')), 5) # 2 of 3 match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ] if match_decoded(dec2, match2): pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ] s = multisig_script(pubkeys) return hash_160_to_bc_address(hash_160(s.decode('hex')), 5) return "p2sh, unknown" return "(None)"
def get_address_from_input_script(bytes): try: decoded = [x for x in script_GetOp(bytes)] except: # coinbase transactions raise an exception print_error("cannot find address in input script", bytes.encode('hex')) return [], [], "(None)" # non-generated TxIn transactions push a signature # (seventy-something bytes) and then their public key # (65 bytes) onto the stack: match = [opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4] if match_decoded(decoded, match): return ([decoded[1][1].encode('hex')], [decoded[0][1].encode('hex')], public_key_to_bc_address(decoded[1][1])) # p2sh transaction, 2 of n match = [opcodes.OP_0] while len(match) < len(decoded): match.append(opcodes.OP_PUSHDATA4) if match_decoded(decoded, match): redeemScript = decoded[-1][1] signatures = map(lambda x: x[1].encode('hex'), decoded[1:-1]) dec2 = [x for x in script_GetOp(redeemScript)] # 2 of 2 match2 = [opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG] if match_decoded(dec2, match2): pubkeys = [dec2[1][1].encode('hex'), dec2[2][1].encode('hex')] return (pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)) # 2 of 3 match2 = [opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG] if match_decoded(dec2, match2): pubkeys = [dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex')] return (pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5)) print_error("cannot find address in input script", bytes.encode('hex')) return [], [], "(None)"
def get_address_from_input_script(bytes): try: decoded = [x for x in script_GetOp(bytes)] except: # coinbase transactions raise an exception print_error("cannot find address in input script", bytes.encode('hex')) return [], [], "(None)" # non-generated TxIn transactions push a signature # (seventy-something bytes) and then their public key # (65 bytes) onto the stack: match = [opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4] if match_decoded(decoded, match): return ([decoded[1][1].encode('hex')], [decoded[0][1].encode('hex')], public_key_to_bc_address(decoded[1][1])) # p2sh transaction, 2 of n match = [opcodes.OP_0] while len(match) < len(decoded): match.append(opcodes.OP_PUSHDATA4) if match_decoded(decoded, match): redeemScript = decoded[-1][1] signatures = map(lambda x: x[1].encode('hex'), decoded[1:-1]) dec2 = [x for x in script_GetOp(redeemScript)] # 2 of 2 match2 = [opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG] if match_decoded(dec2, match2): pubkeys = [dec2[1][1].encode('hex'), dec2[2][1].encode('hex')] return (pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), chain.script_version)) # 2 of 3 match2 = [opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG] if match_decoded(dec2, match2): pubkeys = [dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex')] return (pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), chain.script_version)) print_error("cannot find address in input script", bytes.encode('hex')) return [], [], "(None)"
def createmultisig(self, num, pubkeys): """Create multisig address""" assert isinstance(pubkeys, list), (type(num), type(pubkeys)) redeem_script = Transaction.multisig_script(pubkeys, num) address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5) return {'address': address, 'redeemScript': redeem_script}
def createmultisig(self, num, pubkeys): """Create multisig address""" assert isinstance(pubkeys, list), (type(num), type(pubkeys)) redeem_script = script.multisig_script(pubkeys, num) address = hash_160_to_bc_address( hash_160(redeem_script.decode('hex')), chainparams.get_active_chain().p2sh_version) return {'address': address, 'redeemScript': redeem_script}
def _on_fetch_stealth(self, data): error = unpack_error(data) raw_rows = self.unpack_table("<33sB20s32s", data, 4) rows = [] for ephemkey, address_version, address_hash, tx_hash in raw_rows: address = bitcoin.hash_160_to_bc_address(address_hash[::-1], address_version) tx_hash = tx_hash[::-1] rows.append((ephemkey, address, tx_hash)) return (error, rows)
def _on_update(self, data): address_version = struct.unpack_from('B', data, 0)[0] address_hash = data[1:21][::-1] address = bitcoin.hash_160_to_bc_address(address_hash, address_version) height = struct.unpack_from('I', data, 21)[0] block_hash = data[25:57] tx = data[57:] if address_hash in self._subscriptions['address']: self._subscriptions['address'][address_hash](address_version, address_hash, height, block_hash, tx)
def _on_update(self, data): address_version = struct.unpack_from("B", data, 0)[0] address_hash = data[1:21][::-1] address = bitcoin.hash_160_to_bc_address(address_hash, address_version) height = struct.unpack_from("I", data, 21)[0] block_hash = data[25:57] tx = data[57:] if address_hash in self._subscriptions["address"]: for update_cb in self._subscriptions["address"][address_hash]: update_cb(address_version, address_hash, height, block_hash, tx)
def get_address_from_input_script(bytes): decoded = [ x for x in script_GetOp(bytes) ] # non-generated TxIn transactions push a signature # (seventy-something bytes) and then their public key # (65 bytes) onto the stack: match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ] if match_decoded(decoded, match): return None, None, public_key_to_bc_address(decoded[1][1]) # p2sh transaction, 2 of n match = [ opcodes.OP_0 ] while len(match) < len(decoded): match.append(opcodes.OP_PUSHDATA4) if match_decoded(decoded, match): redeemScript = decoded[-1][1] num = len(match) - 2 signatures = map(lambda x:x[1].encode('hex'), decoded[1:-1]) dec2 = [ x for x in script_GetOp(redeemScript) ] # 2 of 2 match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ] if match_decoded(dec2, match2): pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ] return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5) # 2 of 3 match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ] if match_decoded(dec2, match2): pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ] return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5) raise BaseException("no match for scriptsig")
def xpubkey_to_address(x_pubkey): if x_pubkey[0:2] == 'fd': addrtype = ord(x_pubkey[2:4].decode('hex')) hash160 = x_pubkey[4:].decode('hex') address = bitcoin.hash_160_to_bc_address(hash160, addrtype) return x_pubkey, address if x_pubkey[0:2] in ['02', '03', '04']: pubkey = x_pubkey elif x_pubkey[0:2] == 'ff': xpub, s = BIP32_KeyStore.parse_xpubkey(x_pubkey) pubkey = BIP32_KeyStore.get_pubkey_from_xpub(xpub, s) elif x_pubkey[0:2] == 'fe': mpk, s = Old_KeyStore.parse_xpubkey(x_pubkey) pubkey = Old_KeyStore.get_pubkey_from_mpk(mpk, s[0], s[1]) else: raise BaseException("Cannot parse pubkey") if pubkey: address = public_key_to_p2pkh(pubkey.decode('hex')) return pubkey, address
def xpubkey_to_address(x_pubkey): if x_pubkey[0:2] == 'fd': addrtype = ord(x_pubkey[2:4].decode('hex')) hash160 = x_pubkey[4:].decode('hex') address = bitcoin.hash_160_to_bc_address(hash160, addrtype) return x_pubkey, address if x_pubkey[0:2] in ['02','03','04']: pubkey = x_pubkey elif x_pubkey[0:2] == 'ff': xpub, s = BIP32_KeyStore.parse_xpubkey(x_pubkey) pubkey = BIP32_KeyStore.get_pubkey_from_xpub(xpub, s) elif x_pubkey[0:2] == 'fe': mpk, s = Old_KeyStore.parse_xpubkey(x_pubkey) pubkey = Old_KeyStore.get_pubkey_from_mpk(mpk, s[0], s[1]) else: raise BaseException("Cannot parse pubkey") if pubkey: address = public_key_to_p2pkh(pubkey.decode('hex')) return pubkey, address
def extract_public_key(bytes): decoded = [ x for x in script_GetOp(bytes) ] # non-generated TxIn transactions push a signature # (seventy-something bytes) and then their public key # (65 bytes) onto the stack: match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ] if match_decoded(decoded, match): return public_key_to_bc_address(decoded[1][1]) # The Genesis Block, self-payments, and pay-by-IP-address payments look like: # 65 BYTES:... CHECKSIG match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ] if match_decoded(decoded, match): return public_key_to_bc_address(decoded[0][1]) # Pay-by-Bitcoin-address TxOuts look like: # DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ] if match_decoded(decoded, match): return hash_160_to_bc_address(decoded[2][1]) return "(None)"
def createmultisig(self, num, pubkeys): """Create multisig address""" assert isinstance(pubkeys, list), (type(num), type(pubkeys)) redeem_script = Transaction.multisig_script(pubkeys, num) address = hash_160_to_bc_address(hash_160(redeem_script.decode("hex")), 5) return {"address": address, "redeemScript": redeem_script}
key2 and chain2 are the results of following this BIP32 derivation path """ (index, is_hardened), path2 = path[0], path[1:] bip32_i = index + bitcoin.BIP32_PRIME if is_hardened else index key2, chain2 = bitcoin.CKD(key, chain, bip32_i) if len(path2) == 0: return key2, chain2 else: return generate(path2, key2, chain2) # An Array of (index,is_hardened) tuples path = [(0, True), (0, False)] # Corresponds to /0h/0 main_key, main_chain = generate(path, k, c) for is_change in [0, 1]: receiving_key, receiving_chain = generate([(is_change, False)], main_key, main_chain) for path, i in [([(i, False)], i) for i in range(5)]: addr_key, _ = generate(path, receiving_key, receiving_chain) pubkey, pubkey_compressed = bitcoin.get_pubkeys_from_secret(addr_key) addr = bitcoin.hash_160_to_bc_address( bitcoin.hash_160(pubkey_compressed)) print "m/0h/0/{}/{} = {}".format(is_change, i, addr) main_pubkey, main_pubkey_compressed = bitcoin.get_pubkeys_from_secret(main_key) print "Writing to file." with open("bitcoin_config.txt", "w") as config_file: config_file.write("master_pubkey: " + main_pubkey.encode('hex') + "\n") config_file.write("master_chain: " + main_chain.encode('hex') + "\n")
def id2addr(id): return bitcoin.hash_160_to_bc_address(toraw(id))
if (pubkey == None) or (chain == None): print "Error loading config." sys.exit(0) def generate_from_pub(path, pubkey, chain): """ generate_from_pub(path, pubkey, chain) -> pubkey2, pubkey2_compressed, chain2 path is a list of tuples like [(index1, is_hardened),(index2, is_hardened)] pubkey is the root pubkey chain is the root chain pubkey2, pubkey2_compressed, and chain2 are the results of following this BIP32 derivation path Because this is a pubkey-based generation, none of the path elements may be hardened. """ (index, is_hardened), path2 = path[0], path[1:] if is_hardened: raise Exception("Error: Trying to derive a hardened address using the pubkey") bip32_i = index key2, key2_comp, chain2 = bitcoin.CKD_prime(pubkey,chain, bip32_i) if len(path2) == 0: return key2, key2_comp, chain2 else: return generate(path2, key2, chain2) for is_change in [0,1]: receiving_pubkey, receiving_pubkey_compressed, receiving_chain = generate_from_pub([(is_change,False)], main_pubkey, main_chain) for path, i in [([(i,False)],i) for i in range(5)]: _, addr_pubkey_compressed, _ = generate_from_pub(path, receiving_pubkey, receiving_chain) addr = bitcoin.hash_160_to_bc_address(bitcoin.hash_160(addr_pubkey_compressed)) print "m/{}/{} = {}".format(is_change, i, addr)
def createmultisig(self, num, pubkeys): assert isinstance(pubkeys, list) redeem_script = Transaction.multisig_script(pubkeys, num) address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), bitcoin.SCRIPT_ADDR) return {'address':address, 'redeemScript':redeem_script}
def createmultisig(self, num, pubkeys): """Create multisig address""" assert isinstance(pubkeys, list), (type(num), type(pubkeys)) redeem_script = script.multisig_script(pubkeys, num) address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), chainparams.get_active_chain().p2sh_version) return {'address':address, 'redeemScript':redeem_script}
def createmultisig(self, num, pubkeys): assert isinstance(pubkeys, list) redeem_script = Transaction.multisig_script(pubkeys, num) address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5) return {'address':address, 'redeemScript':redeem_script}
def to_addr(hash): return bitcoin.hash_160_to_bc_address(hash)