|
| 1 | +# Copyright (C) 2015-2016 The bitcoin-blockchain-parser developers |
| 2 | +# |
| 3 | +# This file is part of bitcoin-blockchain-parser. |
| 4 | +# |
| 5 | +# It is subject to the license terms in the LICENSE file found in the top-level |
| 6 | +# directory of this distribution. |
| 7 | +# |
| 8 | +# No part of bitcoin-blockchain-parser, including this file, may be copied, |
| 9 | +# modified, propagated, or distributed except according to the terms contained |
| 10 | +# in the LICENSE file. |
| 11 | +# |
| 12 | +# Encoding/Decoding written by Pieter Wuille (2017) |
| 13 | +# and adapted by Anton Wahrstätter (2022) |
| 14 | +# https://github.com/Bytom/python-bytomlib/blob/master/pybtmsdk/segwit_addr.py |
| 15 | + |
| 16 | +from enum import Enum |
| 17 | + |
| 18 | + |
| 19 | +class Encoding(Enum): |
| 20 | + """Enumeration type to list the various supported encodings.""" |
| 21 | + BECH32 = 1 |
| 22 | + BECH32M = 2 |
| 23 | + |
| 24 | + |
| 25 | +CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l" |
| 26 | +BECH32M_CONST = 0x2bc830a3 |
| 27 | + |
| 28 | + |
| 29 | +def bech32_polymod(values): |
| 30 | + """Internal function that computes the Bech32 checksum.""" |
| 31 | + generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3] |
| 32 | + chk = 1 |
| 33 | + for value in values: |
| 34 | + top = chk >> 25 |
| 35 | + chk = (chk & 0x1ffffff) << 5 ^ value |
| 36 | + for i in range(5): |
| 37 | + chk ^= generator[i] if ((top >> i) & 1) else 0 |
| 38 | + return chk |
| 39 | + |
| 40 | + |
| 41 | +def bech32_hrp_expand(hrp): |
| 42 | + """Expand the HRP into values for checksum computation.""" |
| 43 | + return [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp] |
| 44 | + |
| 45 | + |
| 46 | +def bech32_verify_checksum(hrp, data): |
| 47 | + """Verify a checksum given HRP and converted data characters.""" |
| 48 | + const = bech32_polymod(bech32_hrp_expand(hrp) + data) |
| 49 | + if const == 1: |
| 50 | + return Encoding.BECH32 |
| 51 | + if const == BECH32M_CONST: |
| 52 | + return Encoding.BECH32M |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def bech32_create_checksum(hrp, data, spec): |
| 57 | + """Compute the checksum values given HRP and data.""" |
| 58 | + values = bech32_hrp_expand(hrp) + data |
| 59 | + const = BECH32M_CONST if spec == Encoding.BECH32M else 1 |
| 60 | + polymod = bech32_polymod(values + [0, 0, 0, 0, 0, 0]) ^ const |
| 61 | + return [(polymod >> 5 * (5 - i)) & 31 for i in range(6)] |
| 62 | + |
| 63 | + |
| 64 | +def bech32_encode(hrp, data, spec): |
| 65 | + """Compute a Bech32 string given HRP and data values.""" |
| 66 | + combined = data + bech32_create_checksum(hrp, data, spec) |
| 67 | + return hrp + '1' + ''.join([CHARSET[d] for d in combined]) |
| 68 | + |
| 69 | + |
| 70 | +def bech32_decode(bech): |
| 71 | + """Validate a Bech32/Bech32m string, and determine HRP and data.""" |
| 72 | + if ((any(ord(x) < 33 or ord(x) > 126 for x in bech)) or |
| 73 | + (bech.lower() != bech and bech.upper() != bech)): |
| 74 | + return (None, None, None) |
| 75 | + bech = bech.lower() |
| 76 | + pos = bech.rfind('1') |
| 77 | + if pos < 1 or pos + 7 > len(bech) or len(bech) > 90: |
| 78 | + return (None, None, None) |
| 79 | + if not all(x in CHARSET for x in bech[pos+1:]): |
| 80 | + return (None, None, None) |
| 81 | + hrp = bech[:pos] |
| 82 | + data = [CHARSET.find(x) for x in bech[pos+1:]] |
| 83 | + spec = bech32_verify_checksum(hrp, data) |
| 84 | + if spec is None: |
| 85 | + return (None, None, None) |
| 86 | + return (hrp, data[:-6], spec) |
| 87 | + |
| 88 | + |
| 89 | +def convertbits(data, frombits, tobits, pad=True): |
| 90 | + """General power-of-2 base conversion.""" |
| 91 | + acc = 0 |
| 92 | + bits = 0 |
| 93 | + ret = [] |
| 94 | + maxv = (1 << tobits) - 1 |
| 95 | + max_acc = (1 << (frombits + tobits - 1)) - 1 |
| 96 | + for value in data: |
| 97 | + if value < 0 or (value >> frombits): |
| 98 | + return None |
| 99 | + acc = ((acc << frombits) | value) & max_acc |
| 100 | + bits += frombits |
| 101 | + while bits >= tobits: |
| 102 | + bits -= tobits |
| 103 | + ret.append((acc >> bits) & maxv) |
| 104 | + if pad: |
| 105 | + if bits: |
| 106 | + ret.append((acc << (tobits - bits)) & maxv) |
| 107 | + elif bits >= frombits or ((acc << (tobits - bits)) & maxv): |
| 108 | + return None |
| 109 | + return ret |
| 110 | + |
| 111 | + |
| 112 | +def decode(hrp, addr): |
| 113 | + """Decode a segwit address.""" |
| 114 | + hrpgot, data, spec = bech32_decode(addr) |
| 115 | + if hrpgot != hrp: |
| 116 | + return (None, None) |
| 117 | + decoded = convertbits(data[1:], 5, 8, False) |
| 118 | + if decoded is None or len(decoded) < 2 or len(decoded) > 40: |
| 119 | + return (None, None) |
| 120 | + if data[0] > 16: |
| 121 | + return (None, None) |
| 122 | + if data[0] == 0 and len(decoded) != 20 and len(decoded) != 32: |
| 123 | + return (None, None) |
| 124 | + if data[0] == 0 and spec != Encoding.BECH32 \ |
| 125 | + or data[0] != 0 and spec != Encoding.BECH32M: |
| 126 | + return (None, None) |
| 127 | + return (data[0], decoded) |
| 128 | + |
| 129 | + |
| 130 | +def encode(witprog): |
| 131 | + hrp, witver = "bc", 1 |
| 132 | + """Encode a segwit address.""" |
| 133 | + spec = Encoding.BECH32M |
| 134 | + ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5), spec) |
| 135 | + if decode(hrp, ret) == (None, None): |
| 136 | + return None |
| 137 | + return ret |
| 138 | + |
| 139 | + |
| 140 | +def from_taproot(tpk): |
| 141 | + """Input Tweaked Public Key.""" |
| 142 | + tpk = [int(tpk[i:i+2], 16) for i in range(0, len(tpk), 2)] |
| 143 | + return encode(tpk) |
0 commit comments