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)
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)
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 id(self): return hash160(self.key.encode(compressed=True))
def id(self): return hash160(self.key.to_public().encode(compressed=True))
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))