Skip to content

Commit 0accd53

Browse files
committed
bech32+lnaddr: Add Support for Segwit Fallback Adressess
1 parent 5167500 commit 0accd53

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

bech32.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,33 @@ def bech32_hrp_expand(hrp):
4141
return [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp]
4242

4343

44-
def bech32_verify_checksum(hrp, data):
44+
def bech32_verify_checksum(hrp, data, segwit_version):
4545
"""Verify a checksum given HRP and converted data characters."""
46-
return bech32_polymod(bech32_hrp_expand(hrp) + data) == 1
46+
if segwit_version == 0:
47+
const = 1 # Bech32
48+
else:
49+
const = 0x2bc830a3 # Bech32m
50+
return bech32_polymod(bech32_hrp_expand(hrp) + data) == const
4751

4852

49-
def bech32_create_checksum(hrp, data):
53+
def bech32_create_checksum(hrp, data, segwit_version):
5054
"""Compute the checksum values given HRP and data."""
5155
values = bech32_hrp_expand(hrp) + data
52-
polymod = bech32_polymod(values + [0, 0, 0, 0, 0, 0]) ^ 1
56+
if segwit_version == 0:
57+
const = 1 # Bech32
58+
else:
59+
const = 0x2bc830a3 # Bech32m
60+
polymod = bech32_polymod(values + [0, 0, 0, 0, 0, 0]) ^ const
5361
return [(polymod >> 5 * (5 - i)) & 31 for i in range(6)]
5462

5563

56-
def bech32_encode(hrp, data):
64+
def bech32_encode(hrp, data, segwit_version=0):
5765
"""Compute a Bech32 string given HRP and data values."""
58-
combined = data + bech32_create_checksum(hrp, data)
66+
combined = data + bech32_create_checksum(hrp, data, segwit_version)
5967
return hrp + '1' + ''.join([CHARSET[d] for d in combined])
6068

6169

62-
def bech32_decode(bech):
70+
def bech32_decode(bech, segwit_version_verify=False):
6371
"""Validate a Bech32 string, and determine HRP and data."""
6472
if ((any(ord(x) < 33 or ord(x) > 126 for x in bech)) or
6573
(bech.lower() != bech and bech.upper() != bech)):
@@ -72,7 +80,7 @@ def bech32_decode(bech):
7280
return (None, None)
7381
hrp = bech[:pos]
7482
data = [CHARSET.find(x) for x in bech[pos+1:]]
75-
if not bech32_verify_checksum(hrp, data):
83+
if not bech32_verify_checksum(hrp, data, data[0] if segwit_version_verify else 0):
7684
return (None, None)
7785
return (hrp, data[:-6])
7886

lnaddr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def encode_fallback(fallback, currency):
7878
""" Encode all supported fallback addresses.
7979
"""
8080
if currency == 'bc' or currency == 'tb' or currency == 'tbs' or currency == 'bcrt':
81-
fbhrp, witness = bech32_decode(fallback)
81+
fbhrp, witness = bech32_decode(fallback, segwit_version_verify=True)
8282
if fbhrp:
8383
if fbhrp != currency:
8484
raise ValueError("Not a bech32 address for this currency")
@@ -109,7 +109,7 @@ def parse_fallback(fallback, currency):
109109
addr=base58.b58encode_check(bytes([base58_prefix_map[currency][1]])
110110
+ fallback[5:].tobytes())
111111
elif wver <= 16:
112-
addr=bech32_encode(currency, bitarray_to_u5(fallback))
112+
addr=bech32_encode(currency, bitarray_to_u5(fallback), wver)
113113
else:
114114
return None
115115
else:

0 commit comments

Comments
 (0)