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 xpub_to_hash(xpub: str):
    xpub_raw = Base58.check_decode(xpub)
    if xpub_raw[0:4] in (b'\x02\xfe\x52\xcc', b'\x04\x88\xb2\x1e'):  # remove xpub prefix
        xpub_raw = xpub_raw[4:]
    xpub_hash = bitcoin.bin_sha256(xpub_raw)
    xpub_hash = base64.b64encode(xpub_hash)
    return xpub_hash
def xpub_to_hash(xpub: str):
    xpub_raw = Base58.check_decode(xpub)
    if xpub_raw[0:4] in (b'\x03\xe2\x5d\x7e'):  # remove ppub prefix
        xpub_raw = xpub_raw[4:]
    xpub_hash = bitcoin.bin_sha256(xpub_raw)
    xpub_hash = base64.b64encode(xpub_hash)
    return xpub_hash
Example #4
0
def xpub_to_pk(xpub):
    """
    Derive a compressed public key from an xpub.
    :param xpub: Base58 encoded xpub.
    :return: Hex string compressed public key.
    """
    # Last 33 bytes of xpub are the compressed public key
    return xpub_bytes_to_field_bytes(Base58.check_decode(xpub), 'key').hex()
Example #5
0
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))
Example #6
0
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))
Example #7
0
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
Example #8
0
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
Example #9
0
def pk_to_p2wpkh_as_p2sh_addr(pk_hash):
    return Base58.check_encode(b"\x05" +
                               hash160_bytes(bytes.fromhex("0014") + pk_hash))