def pubkey_to_p2sh_p2wpkh_addr(pubkey_compressed: bytes, version: bytes) -> bytes:
    """ Derives p2sh-segwit (p2sh p2wpkh) address from pubkey """
    pubkey_hash = common_util.sha256(pubkey_compressed)
    rip = common_util.ripemd160(pubkey_hash)
    redeem_script = b'\x00\x14' + rip  # 0x00: OP_0, 0x14: PushData
    redeem_hash = common_util.sha256(redeem_script)
    redeem_rip = common_util.ripemd160(redeem_hash)
    # Base-58 encoding with a checksum
    checksum = common_util.base58_cksum(version + redeem_rip)
    address = base58.b58encode(version + redeem_rip + checksum)
    return address
def pubkey_to_segwit_addr(human_readable_part: str,
                          pubkey: bytes) -> Optional[str]:
    """ Derives bech32 (p2wpkh) address from pubkey """
    witver = 0
    witprog = common_util.ripemd160(common_util.sha256(pubkey))
    addr = encode(human_readable_part, witver, witprog)
    return addr
Example #3
0
def hash160_to_p2sh_p2wpkh_addr(hash160: bytes, version: bytes) -> bytes:
    redeem_script = b'\x00\x14' + hash160  # 0x00: OP_0, 0x14: PushData
    redeem_hash = common_util.sha256(redeem_script)
    redeem_rip = common_util.ripemd160(redeem_hash)
    # Base-58 encoding with a checksum
    checksum = common_util.base58_cksum(version + redeem_rip)
    address = base58.b58encode(version + redeem_rip + checksum)
    return address
Example #4
0
def pubkey_to_p2pkh_addr(pubkey: bytes, version: bytes) -> bytes:
    """ Derives legacy (p2pkh) address from pubkey """
    out1 = common_util.sha256(pubkey)
    out2 = common_util.ripemd160(out1)
    # Base-58 encoding with a checksum
    checksum = common_util.base58_cksum(version + out2)
    address = base58.b58encode(version + out2 + checksum)
    return address
Example #5
0
def pubkey_to_p2sh_p2wpkh_addr(pubkey_compressed: bytes,
                               version: bytes) -> bytes:
    """ Derives p2sh-segwit (p2sh p2wpkh) address from pubkey """
    pubkey_hash = common_util.sha256(pubkey_compressed)
    rip = common_util.ripemd160(pubkey_hash)
    return hash160_to_p2sh_p2wpkh_addr(rip, version)
Example #6
0
def pubkey_to_hash160(pubkey: bytes) -> bytes:
    """ Computes ripemd160(sha256(pubkey)) """
    out1 = common_util.sha256(pubkey)
    out2 = common_util.ripemd160(out1)
    return out2