|
| 1 | +# Copyright (c) 2017, 2020 Pieter Wuille |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +"""Reference implementation for Bech32/Bech32m and segwit addresses.""" |
| 22 | + |
| 23 | + |
| 24 | +from enum import Enum |
| 25 | + |
| 26 | +class Encoding(Enum): |
| 27 | + """Enumeration type to list the various supported encodings.""" |
| 28 | + BECH32 = 1 |
| 29 | + BECH32M = 2 |
| 30 | + |
| 31 | +CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l" |
| 32 | +BECH32M_CONST = 0x2bc830a3 |
| 33 | + |
| 34 | +def bech32_polymod(values): |
| 35 | + """Internal function that computes the Bech32 checksum.""" |
| 36 | + generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3] |
| 37 | + chk = 1 |
| 38 | + for value in values: |
| 39 | + top = chk >> 25 |
| 40 | + chk = (chk & 0x1ffffff) << 5 ^ value |
| 41 | + for i in range(5): |
| 42 | + chk ^= generator[i] if ((top >> i) & 1) else 0 |
| 43 | + return chk |
| 44 | + |
| 45 | + |
| 46 | +def bech32_hrp_expand(hrp): |
| 47 | + """Expand the HRP into values for checksum computation.""" |
| 48 | + return [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp] |
| 49 | + |
| 50 | + |
| 51 | +def bech32_verify_checksum(hrp, data): |
| 52 | + """Verify a checksum given HRP and converted data characters.""" |
| 53 | + const = bech32_polymod(bech32_hrp_expand(hrp) + data) |
| 54 | + if const == 1: |
| 55 | + return Encoding.BECH32 |
| 56 | + if const == BECH32M_CONST: |
| 57 | + return Encoding.BECH32M |
| 58 | + return None |
| 59 | + |
| 60 | +def bech32_create_checksum(hrp, data, spec): |
| 61 | + """Compute the checksum values given HRP and data.""" |
| 62 | + values = bech32_hrp_expand(hrp) + data |
| 63 | + const = BECH32M_CONST if spec == Encoding.BECH32M else 1 |
| 64 | + polymod = bech32_polymod(values + [0, 0, 0, 0, 0, 0]) ^ const |
| 65 | + return [(polymod >> 5 * (5 - i)) & 31 for i in range(6)] |
| 66 | + |
| 67 | + |
| 68 | +def bech32_encode(hrp, data, spec): |
| 69 | + """Compute a Bech32 string given HRP and data values.""" |
| 70 | + combined = data + bech32_create_checksum(hrp, data, spec) |
| 71 | + return hrp + '1' + ''.join([CHARSET[d] for d in combined]) |
| 72 | + |
| 73 | +def bech32_decode(bech): |
| 74 | + """Validate a Bech32/Bech32m string, and determine HRP and data.""" |
| 75 | + if ((any(ord(x) < 33 or ord(x) > 126 for x in bech)) or |
| 76 | + (bech.lower() != bech and bech.upper() != bech)): |
| 77 | + return (None, None, None) |
| 78 | + bech = bech.lower() |
| 79 | + pos = bech.rfind('1') |
| 80 | + |
| 81 | + # remove the requirement that bech32m be less than 90 chars |
| 82 | + if pos < 1 or pos + 7 > len(bech): |
| 83 | + return (None, None, None) |
| 84 | + if not all(x in CHARSET for x in bech[pos+1:]): |
| 85 | + return (None, None, None) |
| 86 | + hrp = bech[:pos] |
| 87 | + data = [CHARSET.find(x) for x in bech[pos+1:]] |
| 88 | + spec = bech32_verify_checksum(hrp, data) |
| 89 | + if spec is None: |
| 90 | + return (None, None, None) |
| 91 | + return (hrp, data[:-6], spec) |
| 92 | + |
| 93 | +def convertbits(data, frombits, tobits, pad=True): |
| 94 | + """General power-of-2 base conversion.""" |
| 95 | + acc = 0 |
| 96 | + bits = 0 |
| 97 | + ret = [] |
| 98 | + maxv = (1 << tobits) - 1 |
| 99 | + max_acc = (1 << (frombits + tobits - 1)) - 1 |
| 100 | + for value in data: |
| 101 | + if value < 0 or (value >> frombits): |
| 102 | + return None |
| 103 | + acc = ((acc << frombits) | value) & max_acc |
| 104 | + bits += frombits |
| 105 | + while bits >= tobits: |
| 106 | + bits -= tobits |
| 107 | + ret.append((acc >> bits) & maxv) |
| 108 | + if pad: |
| 109 | + if bits: |
| 110 | + ret.append((acc << (tobits - bits)) & maxv) |
| 111 | + elif bits >= frombits or ((acc << (tobits - bits)) & maxv): |
| 112 | + return None |
| 113 | + return ret |
| 114 | + |
| 115 | + |
| 116 | +def decode(hrp, addr): |
| 117 | + """Decode a segwit address.""" |
| 118 | + hrpgot, data, spec = bech32_decode(addr) |
| 119 | + if hrpgot != hrp: |
| 120 | + return (None, None) |
| 121 | + decoded = convertbits(data[1:], 5, 8, False) |
| 122 | + if decoded is None or len(decoded) < 2: |
| 123 | + return (None, None) |
| 124 | + if data[0] > 16: |
| 125 | + return (None, None) |
| 126 | + return (data[0], decoded) |
| 127 | + |
| 128 | + |
| 129 | +def encode(hrp, witver, witprog): |
| 130 | + """Encode a segwit address.""" |
| 131 | + spec = Encoding.BECH32 if witver == 0 else Encoding.BECH32M |
| 132 | + ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5), spec) |
| 133 | + if decode(hrp, ret) == (None, None): |
| 134 | + return None |
| 135 | + return ret |
0 commit comments