def convert_dash_xpub(xpub, dest_prefix: str): if dest_prefix != xpub[0:4]: if dest_prefix == 'xpub': raw = Base58.check_decode(xpub) raw = bytes.fromhex('0488b21e') + raw[4:] xpub = Base58.check_encode(raw) elif dest_prefix == 'drkp': raw = Base58.check_decode(xpub) raw = bytes.fromhex('02fe52cc') + raw[4:] xpub = Base58.check_encode(raw) return xpub
def pk_to_p2pkh_addr(pk, testnet=False): """ Compressed public key (hex string) -> p2pkh address. 'Legacy Bitcoin address.' """ pk_bytes = bytes.fromhex(pk) assert is_compressed_pk( pk_bytes), "Only use compressed public keys please." return Base58.check_encode( _prefix_bytes('p2pkh', testnet=testnet) + hash160_bytes(pk_bytes))
def pks_to_p2sh_multisig_addr(m, *pks, testnet=False): """ :param m: The 'm' in m-of-n; the number of required signatures. :param pks: The public keys involved in the multisig. `len(pks) == n` in m-of-n. :return: A base 58 encoded address. (The p2sh address prefix + the hash of the redeem script.) """ redeem_script = _p2sh_multisig_script(m, *pks) assert len( redeem_script ) <= 500, "Spending script is at most 500 bytes (it is valid /and/ standard)" return Base58.check_encode( _prefix_bytes("p2sh", testnet=testnet) + hash160_bytes(redeem_script))
def pk_to_p2wpkh_in_p2sh_addr(pk, testnet=False): """ Compressed public key (hex string) -> p2wpkh nested in p2sh address. 'SegWit address.' """ pk_bytes = bytes.fromhex(pk) assert is_compressed_pk(pk_bytes), \ "Only compressed public keys are compatible with p2sh-p2wpkh addresses. See BIP49." # Script sig is just 0 + PUSH(20){hash160(cpk)} script_sig = OP_0 + push_bytes(hash160_bytes(pk_bytes)) # Address is then prefix + hash160(script_sig) address = Base58.check_encode( _prefix_bytes('p2sh', testnet=testnet) + hash160_bytes(script_sig)) return address
def get_xpub(client, bip32_path): bip32_path = clean_bip32_path(bip32_path) bip32_path.strip() if bip32_path.lower().find('m/') >= 0: bip32_path = bip32_path[2:] path_n = bip32_path_string_to_n(bip32_path) parent_bip32_path = bip32_path_n_to_string(path_n[:-1]) depth = len(path_n) index = path_n[-1] nodedata = client.getWalletPublicKey(bip32_path) pubkey = compress(nodedata.get('publicKey')) chaincode = nodedata.get('chainCode') parent_nodedata = client.getWalletPublicKey(parent_bip32_path) parent_pubkey = compress(parent_nodedata['publicKey']) parent_fingerprint = bin_hash160(parent_pubkey)[:4] xpub_raw = bytes.fromhex('0488b21e') + depth.to_bytes(1, 'big') + parent_fingerprint + index.to_bytes(4, 'big') + \ chaincode + pubkey xpub = Base58.check_encode(xpub_raw) return xpub
def pk_to_p2wpkh_as_p2sh_addr(pk_hash): return Base58.check_encode(b"\x05" + hash160_bytes(bytes.fromhex("0014") + pk_hash))