bech32_to_bytes will be updated to handle all segwit addresses and check if hrp is valid.
def bech32_to_bytes(address):
hrp = address.split('1')[0]
assert hrp in ['bc', 'tb', 'bcrt'], f"hrp should be 'bc', 'tb', or 'bcrt', not '{hrp}'"
version, payload = decode_segwit_address(hrp, address)
if version is None:
return (None, None)
return version, bytearray(payload)
So calling address_to_scriptpubkey with a base58 address will assert to false, because the hrp is invalid.
def address_to_scriptpubkey(address):
"""Converts a given address to the corresponding output script (scriptPubKey)."""
version, payload = bech32_to_bytes(address)
if version is not None:
return program_to_witness_script(version, payload) # testnet segwit scriptpubkey
payload, version = base58_to_byte(address)
if version == 111: # testnet pubkey hash
return keyhash_to_p2pkh_script(payload)
elif version == 196: # testnet script hash
return scripthash_to_p2sh_script(payload)
# TODO: also support other address formats
else:
assert False
address_to_scriptpubkey will be updated to check whether an address is in base58 before calling bech32_to_bytes
def address_to_scriptpubkey(address):
"""Converts a given address to the corresponding output script (scriptPubKey)."""
prefix = address[0]
if prefix in ['m','n','2']:
payload, version = base58_to_byte(address)
if version == 111: # testnet pubkey hash
return keyhash_to_p2pkh_script(payload)
elif version == 196: # testnet script hash
return scripthash_to_p2sh_script(payload)
version, payload = bech32_to_bytes(address)
return program_to_witness_script(version, payload) #segwit scriptpubkey