Validate a Bech32 string, and determine HRP and data.
(bech)
| 59 | |
| 60 | |
| 61 | def bech32_decode(bech): |
| 62 | """Validate a Bech32 string, and determine HRP and data.""" |
| 63 | if ((any(ord(x) < 33 or ord(x) > 126 for x in bech)) or |
| 64 | (bech.lower() != bech and bech.upper() != bech)): |
| 65 | return (None, None) |
| 66 | bech = bech.lower() |
| 67 | pos = bech.rfind('1') |
| 68 | if pos < 1 or pos + 7 > len(bech) or len(bech) > 90: |
| 69 | return (None, None) |
| 70 | if not all(x in CHARSET for x in bech[pos+1:]): |
| 71 | return (None, None) |
| 72 | hrp = bech[:pos] |
| 73 | data = [CHARSET.find(x) for x in bech[pos+1:]] |
| 74 | if not bech32_verify_checksum(hrp, data): |
| 75 | return (None, None) |
| 76 | return (hrp, data[:-6]) |
| 77 | |
| 78 | |
| 79 | def convertbits(data, frombits, tobits, pad=True): |
no test coverage detected