Skip to content

Commit 873a506

Browse files
committed
Merge bitcoin/bitcoin#27269: test: Support decoding segwit address in address_to_scriptpubkey()
d178082 test: add bech32 decoding support to address_to_scriptpubkey() (ismaelsadeeq) aac8793 test: test_bech32_decode in address.py (ismaelsadeeq) Pull request description: [rpc_scantxoutset.py](https://github.com/bitcoin/bitcoin/blob/e695d8536e534f1e59de4a62a0d87a93e7a73456/test/functional/rpc_scantxoutset.py#L26) sendtodestination only sends to legacy addresses and scriptPubkeys because [wallet.py](https://github.com/bitcoin/bitcoin/blob/e695d8536e534f1e59de4a62a0d87a93e7a73456/test/functional/test_framework/wallet.py#L415) address_to_scriptpubkey does not support conversion of segwit address. This update enables address_to_scriptpubkey to support the conversion of testnet segwit addresses to scriptPubkeys. This change will enable [rpc_scantxoutset.py](https://github.com/bitcoin/bitcoin/blob/e695d8536e534f1e59de4a62a0d87a93e7a73456/test/functional/rpc_scantxoutset.py#L22) ScantxoutsetTest to have more test coverage by adding more sendtodestination calls with bech32 and bech32m testnet addresses, then test the bech32 and bech32m derivation subsets UTXO amount in [Test extended key derivation](https://github.com/bitcoin/bitcoin/blob/e695d8536e534f1e59de4a62a0d87a93e7a73456/test/functional/rpc_scantxoutset.py#L84). I will add the test coverage in a subsequent Pull request. ACKs for top commit: josibake: ACK bitcoin/bitcoin@d178082 theStack: ACK d178082 ✔️ willcl-ark: ACK d178082 Tree-SHA512: 312c20ce192c648faf7dd178622700c9b871d755db56c246250e25508c3c19e7b02c0ae901dda11a1794629b9a9429c877168c05e1c4c1dbf41493316e30e7e9
2 parents 630756c + d178082 commit 873a506

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

test/functional/test_framework/address.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
sha256,
2121
taproot_construct,
2222
)
23-
from .segwit_addr import encode_segwit_address
2423
from .util import assert_equal
24+
from test_framework.segwit_addr import (
25+
decode_segwit_address,
26+
encode_segwit_address,
27+
)
2528

2629
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
2730
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
@@ -159,6 +162,16 @@ def check_script(script):
159162
assert False
160163

161164

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+
162175
class TestFrameworkScript(unittest.TestCase):
163176
def test_base58encodedecode(self):
164177
def check_base58(data, version):
@@ -176,3 +189,18 @@ def check_base58(data, version):
176189
check_base58(bytes.fromhex('0041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
177190
check_base58(bytes.fromhex('000041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
178191
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)

test/functional/test_framework/wallet.py

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from test_framework.address import (
1616
base58_to_byte,
17+
bech32_to_bytes,
1718
create_deterministic_address_bcrt1_p2tr_op_true,
1819
key_to_p2pkh,
1920
key_to_p2sh_p2wpkh,
@@ -49,6 +50,7 @@
4950
key_to_p2sh_p2wpkh_script,
5051
key_to_p2wpkh_script,
5152
keyhash_to_p2pkh_script,
53+
program_to_witness_script,
5254
scripthash_to_p2sh_script,
5355
)
5456
from test_framework.util import (
@@ -414,6 +416,9 @@ def getnewdestination(address_type='bech32m'):
414416

415417
def address_to_scriptpubkey(address):
416418
"""Converts a given address to the corresponding output script (scriptPubKey)."""
419+
version, payload = bech32_to_bytes(address)
420+
if version is not None:
421+
return program_to_witness_script(version, payload) # testnet segwit scriptpubkey
417422
payload, version = base58_to_byte(address)
418423
if version == 111: # testnet pubkey hash
419424
return keyhash_to_p2pkh_script(payload)

0 commit comments

Comments
 (0)