INNER CODE UNIT · Python
segwit_decode
bee-san/pyWhat · pywhat/bitcoin.py:96
def segwit_decode(address: str) -> Optional[Tuple[str, int, bytes]]:
"""
The human-readable part, the witness version and the witness program of a
SegWit address, or None if it is not one (BIP 173 and BIP 350).
"""
if len(address) > 90 or any(not 33 <= ord(char) <= 126 for char in address):
return None
if address.lower() != address and address.upper() != address:
return None # mixed case
hrp, separator, data_part = address.lower().rpartition("1")
# At least a witness version and the 6 characters of the checksum
if not separator or not hrp or len(data_part) < 7:
return None
if any(char not in BECH32_CHARSET for char in data_part):
return None
data = [BECH32_CHARSET.index(char) for char in data_part]
version = data[0]
if version > 16: