MCPcopy Index your code
hub / github.com/petertodd/python-bitcoinlib / bech32_decode

Function bech32_decode

bitcoin/segwit_addr.py:61–76  ·  view source on GitHub ↗

Validate a Bech32 string, and determine HRP and data.

(bech)

Source from the content-addressed store, hash-verified

59
60
61def 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
79def convertbits(data, frombits, tobits, pad=True):

Callers 1

decodeFunction · 0.85

Calls 1

bech32_verify_checksumFunction · 0.85

Tested by

no test coverage detected