示例#1
0
def pubkey_to_bech32(pub: PublicKey, witver: int) -> str:
    """https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program"""
    witprog = hash160(pub.encode(compressed=True))
    return bech32.encode(network('hrp'), witver, witprog)
示例#2
0
def legacy_address(pub_or_script: Union[bytes, PublicKey], version_byte: bytes) -> str:
    """https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses"""
    bts = pub_or_script.encode(compressed=False) if isinstance(pub_or_script, PublicKey) else pub_or_script
    hashed = hash160(bts)
    payload = version_byte + hashed
    return hashed_payload_to_address(payload)
示例#3
0
def script_to_bech32(script: bytes, witver: int) -> str:
    """https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program"""
    witprog = sha256(script)
    return bech32.encode(network('hrp'), witver, witprog)


def pubkey_to_bech32(pub: PublicKey, witver: int) -> str:
    """https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program"""
    witprog = hash160(pub.encode(compressed=True))
    return bech32.encode(network('hrp'), witver, witprog)


key_to_addr_versions = {
    ADDRESS.P2PKH: lambda pub: legacy_address(pub, version_byte=network('keyhash')),
    # 'P2WPKH': partial(pubkey_to_p2wpkh, version_byte=0x06, witver=0x00),  # WAS REPLACED BY BIP 173
    ADDRESS.P2WPKH_P2SH: lambda pub: legacy_address(witness_byte(witver=0) + push(hash160(pub.encode(compressed=True))), version_byte=network('scripthash')),
    ADDRESS.P2WPKH: partial(pubkey_to_bech32, witver=0x00),
}

script_to_addr_versions = {
    ADDRESS.P2SH: lambda script: legacy_address(script, version_byte=network('scripthash')),
    # 'P2WSH': partial(script_to_p2wsh, version_byte=0x0A, witver=0x00),  # WAS REPLACED BY BIP 173
    ADDRESS.P2WSH_P2SH: lambda script: legacy_address(witness_byte(witver=0) + push(sha256(script)), version_byte=network('scripthash')),
    ADDRESS.P2WSH: partial(script_to_bech32, witver=0x00),
}


def pubkey_to_address(pub: PublicKey, version='P2PKH') -> str:
    converter = key_to_addr_versions[ADDRESS(version.upper())]
    return converter(pub)
示例#4
0
 def OP_HASH160(self):
     """ The input is hashed twice: first with SHA-256 and then with RIPEMD-160."""
     item = self.pop()
     self.push(hash160(item))
示例#5
0
 def id(self):
     return hash160(self.key.encode(compressed=True))
示例#6
0
 def id(self):
     return hash160(self.key.to_public().encode(compressed=True))