aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorismaelsadeeq <ask4ismailsadiq@gmail.com>2023-03-23 11:59:29 +0100
committerismaelsadeeq <ask4ismailsadiq@gmail.com>2023-03-23 11:59:29 +0100
commitaac8793c7a2ee7630641dd74be6ba2ead50c2aee (patch)
tree882a2e655bd40c70bb5b4a74321a6be358c85568
parent1884b71b1dc1fe463a2f00e61bcc56e4720cbc69 (diff)
downloadbitcoin-aac8793c7a2ee7630641dd74be6ba2ead50c2aee.tar.xz
test: test_bech32_decode in address.py
Adds bech32_to_bytes() which can decode a bech32 address and return the version as an `int` and the payload in bytes. bech32_to_bytes() is used by the test_bech32_decode unit test to test decoding of segwit addresses.
-rw-r--r--test/functional/test_framework/address.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/test/functional/test_framework/address.py b/test/functional/test_framework/address.py
index 959a2a65bd..d1bf186b9d 100644
--- a/test/functional/test_framework/address.py
+++ b/test/functional/test_framework/address.py
@@ -20,8 +20,11 @@ from .script import (
sha256,
taproot_construct,
)
-from .segwit_addr import encode_segwit_address
from .util import assert_equal
+from test_framework.segwit_addr import (
+ decode_segwit_address,
+ encode_segwit_address,
+)
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
@@ -159,6 +162,16 @@ def check_script(script):
assert False
+def bech32_to_bytes(address):
+ hrp = address.split('1')[0]
+ if hrp not in ['bc', 'tb', 'bcrt']:
+ return (None, None)
+ version, payload = decode_segwit_address(hrp, address)
+ if version is None:
+ return (None, None)
+ return version, bytearray(payload)
+
+
class TestFrameworkScript(unittest.TestCase):
def test_base58encodedecode(self):
def check_base58(data, version):
@@ -176,3 +189,18 @@ class TestFrameworkScript(unittest.TestCase):
check_base58(bytes.fromhex('0041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
check_base58(bytes.fromhex('000041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
check_base58(bytes.fromhex('00000041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
+
+
+ def test_bech32_decode(self):
+ def check_bech32_decode(payload, version):
+ hrp = "tb"
+ self.assertEqual(bech32_to_bytes(encode_segwit_address(hrp, version, payload)), (version, payload))
+
+ check_bech32_decode(bytes.fromhex('36e3e2a33f328de12e4b43c515a75fba2632ecc3'), 0)
+ check_bech32_decode(bytes.fromhex('823e9790fc1d1782321140d4f4aa61aabd5e045b'), 0)
+ check_bech32_decode(bytes.fromhex('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'), 1)
+ check_bech32_decode(bytes.fromhex('39cf8ebd95134f431c39db0220770bd127f5dd3cc103c988b7dcd577ae34e354'), 1)
+ check_bech32_decode(bytes.fromhex('708244006d27c757f6f1fc6f853b6ec26268b727866f7ce632886e34eb5839a3'), 1)
+ check_bech32_decode(bytes.fromhex('616211ab00dffe0adcb6ce258d6d3fd8cbd901e2'), 0)
+ check_bech32_decode(bytes.fromhex('b6a7c98b482d7fb21c9fa8e65692a0890410ff22'), 0)
+ check_bech32_decode(bytes.fromhex('f0c2109cb1008cfa7b5a09cc56f7267cd8e50929'), 0)