20
20
sha256 ,
21
21
taproot_construct ,
22
22
)
23
- from .segwit_addr import encode_segwit_address
24
23
from .util import assert_equal
24
+ from test_framework .segwit_addr import (
25
+ decode_segwit_address ,
26
+ encode_segwit_address ,
27
+ )
25
28
26
29
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
27
30
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
@@ -159,6 +162,16 @@ def check_script(script):
159
162
assert False
160
163
161
164
165
+ def bech32_to_bytes (address ):
166
+ hrp = address .split ('1' )[0 ]
167
+ if hrp not in ['bc' , 'tb' , 'bcrt' ]:
168
+ return (None , None )
169
+ version , payload = decode_segwit_address (hrp , address )
170
+ if version is None :
171
+ return (None , None )
172
+ return version , bytearray (payload )
173
+
174
+
162
175
class TestFrameworkScript (unittest .TestCase ):
163
176
def test_base58encodedecode (self ):
164
177
def check_base58 (data , version ):
@@ -176,3 +189,18 @@ def check_base58(data, version):
176
189
check_base58 (bytes .fromhex ('0041c1eaf111802559bad61b60d62b1f897c63928a' ), 0 )
177
190
check_base58 (bytes .fromhex ('000041c1eaf111802559bad61b60d62b1f897c63928a' ), 0 )
178
191
check_base58 (bytes .fromhex ('00000041c1eaf111802559bad61b60d62b1f897c63928a' ), 0 )
192
+
193
+
194
+ def test_bech32_decode (self ):
195
+ def check_bech32_decode (payload , version ):
196
+ hrp = "tb"
197
+ self .assertEqual (bech32_to_bytes (encode_segwit_address (hrp , version , payload )), (version , payload ))
198
+
199
+ check_bech32_decode (bytes .fromhex ('36e3e2a33f328de12e4b43c515a75fba2632ecc3' ), 0 )
200
+ check_bech32_decode (bytes .fromhex ('823e9790fc1d1782321140d4f4aa61aabd5e045b' ), 0 )
201
+ check_bech32_decode (bytes .fromhex ('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' ), 1 )
202
+ check_bech32_decode (bytes .fromhex ('39cf8ebd95134f431c39db0220770bd127f5dd3cc103c988b7dcd577ae34e354' ), 1 )
203
+ check_bech32_decode (bytes .fromhex ('708244006d27c757f6f1fc6f853b6ec26268b727866f7ce632886e34eb5839a3' ), 1 )
204
+ check_bech32_decode (bytes .fromhex ('616211ab00dffe0adcb6ce258d6d3fd8cbd901e2' ), 0 )
205
+ check_bech32_decode (bytes .fromhex ('b6a7c98b482d7fb21c9fa8e65692a0890410ff22' ), 0 )
206
+ check_bech32_decode (bytes .fromhex ('f0c2109cb1008cfa7b5a09cc56f7267cd8e50929' ), 0 )
0 commit comments