Merge #712: fix len check in blech32 decode() in test framework

522a147be Add blech32 roundtrip test for python implementation (Gregory Sanders)
32564b0a9 fix len check in blech32 decode() in test framework (Dmitry Petukhov)

Pull request description:

  I used the code from test/functional/test_framework/liquid_addr.py with the same length check adjustments in python-elementstx to decode Liquid's blech32 addresses, and it seems to be working fine.

Tree-SHA512: 4ac4589e0cdbeb7397a4f3c9000b8fdb91516ca257b757cddbfb6df2d75df4587ddcee37b8f9f1d0364d5e78515ea2e1f4270eecca42ea9a59b8f28578d7ab1c
This commit is contained in:
Gregory Sanders 2019-09-13 14:55:55 -04:00
commit 3c8aacee72
No known key found for this signature in database
GPG key ID: F3F68E2D86A48FDB
2 changed files with 20 additions and 3 deletions

View file

@ -28,6 +28,11 @@ from test_framework.util import (
import os
import re
from test_framework.liquid_addr import (
encode,
decode,
)
class CTTest (BitcoinTestFramework):
def set_test_params(self):
@ -104,6 +109,15 @@ class CTTest (BitcoinTestFramework):
print("Testing wallet secret recovery")
self.test_wallet_recovery()
print("Test blech32 python roundtrip")
# blech/bech are aliased, both are blech32
for addrtype in ["bech32", "blech32"]:
addr_to_rt = self.nodes[0].getnewaddress("", addrtype)
hrp = addr_to_rt[:2]
assert_equal(hrp, "el")
(witver, witprog) = decode(hrp, addr_to_rt)
assert_equal(encode(hrp, witver, witprog), addr_to_rt)
# Test that "blech32" gives a blinded segwit address.
blech32_addr = self.nodes[0].getnewaddress("", "blech32")
blech32_addr_info = self.nodes[0].getaddressinfo(blech32_addr)

View file

@ -84,16 +84,19 @@ def convertbits(data, frombits, tobits, pad=True):
def decode(hrp, addr):
"""Decode a segwit address."""
"""Decode a segwit confidential address.
Its payload is longer than the payload for unconfidential address
by 33 bytes (the length of blinding pubkey)"""
hrpgot, data = blech32_decode(addr)
if hrpgot != hrp:
return (None, None)
decoded = convertbits(data[1:], 5, 8, False)
if decoded is None or len(decoded) < 2 or len(decoded) > 40:
if decoded is None or len(decoded) < 2 or len(decoded) > 40+33:
return (None, None)
if data[0] > 16:
return (None, None)
if data[0] == 0 and len(decoded) != 20 and len(decoded) != 32:
if data[0] == 0 and len(decoded) != 20+33 and len(decoded) != 32+33:
return (None, None)
return (data[0], decoded)