def bech32_decode(bech: str, *, ignore_long_length=False) -> DecodedBech32:
    """Validate a Bech32/Bech32m string, and determine HRP and data."""
    bech_lower = bech.lower()
    if bech_lower != bech and bech.upper() != bech:
        return DecodedBech32(None, None, None)
    pos = bech.rfind('1')
    if pos < 1 or pos + 7 > len(bech) or (not ignore_long_length and len(bech) > 90):
        return DecodedBech32(None, None, None)
    # check that HRP only consists of sane ASCII chars
    if any(ord(x) < 33 or ord(x) > 126 for x in bech[:pos+1]):
        return DecodedBech32(None, None, None)
    bech = bech_lower
    hrp = bech[:pos]
    try:
        data = [CHARSET_INVERSE[x] for x in bech[pos + 1:]]
    except KeyError:
        return DecodedBech32(None, None, None)
    encoding = bech32_verify_checksum(hrp, data)
    if encoding is None:
        return DecodedBech32(None, None, None)
    return DecodedBech32(encoding=encoding, hrp=hrp, data=data[:-6])
Validate a Bech32/Bech32m string, and determine HRP and data.
electrum/segwit_addr.py