Skip to content

Commit fa25668

Browse files
author
MarcoFalke
committed
test: Test p2sh-witness and bech32 in wallet_import_rescan
1 parent fa79af2 commit fa25668

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

test/functional/test_framework/address.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Encode and decode BASE58, P2PKH and P2SH addresses."""
66

7+
import enum
8+
79
from .script import hash256, hash160, sha256, CScript, OP_0
810
from .util import hex_str_to_bytes
911

1012
from . import segwit_addr
1113

1214
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
1315

16+
17+
class AddressType(enum.Enum):
18+
bech32 = 'bech32'
19+
p2sh_segwit = 'p2sh-segwit'
20+
legacy = 'legacy' # P2PKH
21+
22+
1423
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
1524

1625

test/functional/wallet_import_rescan.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"""
2121

2222
from test_framework.test_framework import BitcoinTestFramework
23+
from test_framework.address import AddressType
2324
from test_framework.util import (
2425
connect_nodes,
2526
assert_equal,
@@ -37,13 +38,20 @@
3738
Rescan = enum.Enum("Rescan", "no yes late_timestamp")
3839

3940

40-
class Variant(collections.namedtuple("Variant", "call data rescan prune")):
41+
class Variant(collections.namedtuple("Variant", "call data address_type rescan prune")):
4142
"""Helper for importing one key and verifying scanned transactions."""
4243

4344
def do_import(self, timestamp):
4445
"""Call one key import RPC."""
4546
rescan = self.rescan == Rescan.yes
4647

48+
assert_equal(self.address["solvable"], True)
49+
assert_equal(self.address["isscript"], self.address_type == AddressType.p2sh_segwit)
50+
assert_equal(self.address["iswitness"], self.address_type == AddressType.bech32)
51+
if self.address["isscript"]:
52+
assert_equal(self.address["embedded"]["isscript"], False)
53+
assert_equal(self.address["embedded"]["iswitness"], True)
54+
4755
if self.call == Call.single:
4856
if self.data == Data.address:
4957
response = self.node.importaddress(address=self.address["address"], label=self.label, rescan=rescan)
@@ -54,7 +62,7 @@ def do_import(self, timestamp):
5462
assert_equal(response, None)
5563

5664
elif self.call in (Call.multiaddress, Call.multiscript):
57-
response = self.node.importmulti([{
65+
request = {
5866
"scriptPubKey": {
5967
"address": self.address["address"]
6068
} if self.call == Call.multiaddress else self.address["scriptPubKey"],
@@ -63,7 +71,14 @@ def do_import(self, timestamp):
6371
"keys": [self.key] if self.data == Data.priv else [],
6472
"label": self.label,
6573
"watchonly": self.data != Data.priv
66-
}], {"rescan": self.rescan in (Rescan.yes, Rescan.late_timestamp)})
74+
}
75+
if self.address_type == AddressType.p2sh_segwit and self.data != Data.address:
76+
# We need solving data when providing a pubkey or privkey as data
77+
request.update({"redeemscript": self.address['embedded']['scriptPubKey']})
78+
response = self.node.importmulti(
79+
requests=[request],
80+
options={"rescan": self.rescan in (Rescan.yes, Rescan.late_timestamp)},
81+
)
6782
assert_equal(response, [{"success": True}])
6883

6984
def check(self, txid=None, amount=None, confirmation_height=None):
@@ -105,7 +120,7 @@ def check(self, txid=None, amount=None, confirmation_height=None):
105120

106121

107122
# List of Variants for each way a key or address could be imported.
108-
IMPORT_VARIANTS = [Variant(*variants) for variants in itertools.product(Call, Data, Rescan, (False, True))]
123+
IMPORT_VARIANTS = [Variant(*variants) for variants in itertools.product(Call, Data, AddressType, Rescan, (False, True))]
109124

110125
# List of nodes to import keys to. Half the nodes will have pruning disabled,
111126
# half will have it enabled. Different nodes will be used for imports that are
@@ -135,12 +150,12 @@ def skip_test_if_missing_module(self):
135150
self.skip_if_no_wallet()
136151

137152
def setup_network(self):
138-
extra_args = [["-addresstype=legacy"] for _ in range(self.num_nodes)]
153+
self.extra_args = [[]] * self.num_nodes
139154
for i, import_node in enumerate(IMPORT_NODES, 2):
140155
if import_node.prune:
141-
extra_args[i] += ["-prune=1"]
156+
self.extra_args[i] += ["-prune=1"]
142157

143-
self.add_nodes(self.num_nodes, extra_args=extra_args)
158+
self.add_nodes(self.num_nodes, extra_args=self.extra_args)
144159

145160
# Import keys with pruning disabled
146161
self.start_nodes(extra_args=[[]] * self.num_nodes)
@@ -157,7 +172,10 @@ def run_test(self):
157172
# each possible type of wallet import RPC.
158173
for i, variant in enumerate(IMPORT_VARIANTS):
159174
variant.label = "label {} {}".format(i, variant)
160-
variant.address = self.nodes[1].getaddressinfo(self.nodes[1].getnewaddress(variant.label))
175+
variant.address = self.nodes[1].getaddressinfo(self.nodes[1].getnewaddress(
176+
label=variant.label,
177+
address_type=variant.address_type.value,
178+
))
161179
variant.key = self.nodes[1].dumpprivkey(variant.address["address"])
162180
variant.initial_amount = get_rand_amount()
163181
variant.initial_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.initial_amount)

0 commit comments

Comments
 (0)