Skip to content

Commit e9379f1

Browse files
committed
rpc, wallet: Include information about blank flag
This allows us to test that the blank flag is being set appropriately.
1 parent da9f62f commit e9379f1

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

src/wallet/rpc/wallet.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static RPCHelpMan getwalletinfo()
6868
}, /*skip_type_check=*/true},
6969
{RPCResult::Type::BOOL, "descriptors", "whether this wallet uses descriptors for scriptPubKey management"},
7070
{RPCResult::Type::BOOL, "external_signer", "whether this wallet is configured to use an external signer such as a hardware wallet"},
71+
{RPCResult::Type::BOOL, "blank", "Whether this wallet intentionally does not contain any keys, scripts, or descriptors"},
7172
RESULT_LAST_PROCESSED_BLOCK,
7273
}},
7374
},
@@ -130,6 +131,7 @@ static RPCHelpMan getwalletinfo()
130131
}
131132
obj.pushKV("descriptors", pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
132133
obj.pushKV("external_signer", pwallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
134+
obj.pushKV("blank", pwallet->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
133135

134136
AppendLastProcessedBlock(obj, *pwallet);
135137
return obj;

test/functional/test_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@
166166
'p2p_compactblocks_blocksonly.py',
167167
'wallet_hd.py --legacy-wallet',
168168
'wallet_hd.py --descriptors',
169+
'wallet_blank.py --legacy-wallet',
170+
'wallet_blank.py --descriptors',
169171
'wallet_keypool_topup.py --legacy-wallet',
170172
'wallet_keypool_topup.py --descriptors',
171173
'wallet_fast_rescan.py --descriptors',

test/functional/wallet_blank.py

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2022 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
5+
6+
import os
7+
8+
from test_framework.test_framework import BitcoinTestFramework
9+
from test_framework.address import (
10+
ADDRESS_BCRT1_UNSPENDABLE,
11+
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR,
12+
)
13+
from test_framework.key import ECKey
14+
from test_framework.util import (
15+
assert_equal,
16+
)
17+
from test_framework.wallet_util import bytes_to_wif
18+
19+
20+
class WalletBlankTest(BitcoinTestFramework):
21+
def set_test_params(self):
22+
self.num_nodes = 1
23+
24+
def skip_test_if_missing_module(self):
25+
self.skip_if_no_wallet()
26+
27+
def add_options(self, options):
28+
self.add_wallet_options(options)
29+
30+
def test_importaddress(self):
31+
if self.options.descriptors:
32+
return
33+
self.log.info("Test that importaddress unsets the blank flag")
34+
self.nodes[0].createwallet(wallet_name="iaddr", disable_private_keys=True, blank=True)
35+
wallet = self.nodes[0].get_wallet_rpc("iaddr")
36+
info = wallet.getwalletinfo()
37+
assert_equal(info["descriptors"], False)
38+
assert_equal(info["blank"], True)
39+
wallet.importaddress(ADDRESS_BCRT1_UNSPENDABLE)
40+
assert_equal(wallet.getwalletinfo()["blank"], False)
41+
42+
def test_importpubkey(self):
43+
if self.options.descriptors:
44+
return
45+
self.log.info("Test that importpubkey unsets the blank flag")
46+
for i, comp in enumerate([True, False]):
47+
self.nodes[0].createwallet(wallet_name=f"ipub{i}", disable_private_keys=True, blank=True)
48+
wallet = self.nodes[0].get_wallet_rpc(f"ipub{i}")
49+
info = wallet.getwalletinfo()
50+
assert_equal(info["descriptors"], False)
51+
assert_equal(info["blank"], True)
52+
53+
eckey = ECKey()
54+
eckey.generate(compressed=comp)
55+
56+
wallet.importpubkey(eckey.get_pubkey().get_bytes().hex())
57+
assert_equal(wallet.getwalletinfo()["blank"], False)
58+
59+
def test_importprivkey(self):
60+
if self.options.descriptors:
61+
return
62+
self.log.info("Test that importprivkey unsets the blank flag")
63+
for i, comp in enumerate([True, False]):
64+
self.nodes[0].createwallet(wallet_name=f"ipriv{i}", blank=True)
65+
wallet = self.nodes[0].get_wallet_rpc(f"ipriv{i}")
66+
info = wallet.getwalletinfo()
67+
assert_equal(info["descriptors"], False)
68+
assert_equal(info["blank"], True)
69+
70+
eckey = ECKey()
71+
eckey.generate(compressed=comp)
72+
wif = bytes_to_wif(eckey.get_bytes(), eckey.is_compressed)
73+
74+
wallet.importprivkey(wif)
75+
# FIXME: A bug results in blank remaining set
76+
assert_equal(wallet.getwalletinfo()["blank"], True)
77+
78+
def test_importmulti(self):
79+
if self.options.descriptors:
80+
return
81+
self.log.info("Test that importmulti unsets the blank flag")
82+
self.nodes[0].createwallet(wallet_name="imulti", disable_private_keys=True, blank=True)
83+
wallet = self.nodes[0].get_wallet_rpc("imulti")
84+
info = wallet.getwalletinfo()
85+
assert_equal(info["descriptors"], False)
86+
assert_equal(info["blank"], True)
87+
wallet.importmulti([{
88+
"desc": ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR,
89+
"timestamp": "now",
90+
}])
91+
assert_equal(wallet.getwalletinfo()["blank"], False)
92+
93+
def test_importdescriptors(self):
94+
if not self.options.descriptors:
95+
return
96+
self.log.info("Test that importdescriptors preserves the blank flag")
97+
self.nodes[0].createwallet(wallet_name="idesc", disable_private_keys=True, blank=True)
98+
wallet = self.nodes[0].get_wallet_rpc("idesc")
99+
info = wallet.getwalletinfo()
100+
assert_equal(info["descriptors"], True)
101+
assert_equal(info["blank"], True)
102+
wallet.importdescriptors([{
103+
"desc": ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR,
104+
"timestamp": "now",
105+
}])
106+
assert_equal(wallet.getwalletinfo()["blank"], True)
107+
108+
def test_importwallet(self):
109+
if self.options.descriptors:
110+
return
111+
self.log.info("Test that importwallet unsets the blank flag")
112+
def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
113+
114+
self.nodes[0].createwallet(wallet_name="iwallet", blank=True)
115+
wallet = self.nodes[0].get_wallet_rpc("iwallet")
116+
info = wallet.getwalletinfo()
117+
assert_equal(info["descriptors"], False)
118+
assert_equal(info["blank"], True)
119+
120+
wallet_dump_path = os.path.join(self.nodes[0].datadir, "wallet.dump")
121+
def_wallet.dumpwallet(wallet_dump_path)
122+
123+
wallet.importwallet(wallet_dump_path)
124+
# FIXME: A bug results in blank remaining set
125+
assert_equal(wallet.getwalletinfo()["blank"], True)
126+
127+
def test_encrypt_legacy(self):
128+
if self.options.descriptors:
129+
return
130+
self.log.info("Test that encrypting a blank legacy wallet preserves the blank flag and does not generate a seed")
131+
self.nodes[0].createwallet(wallet_name="encblanklegacy", blank=True)
132+
wallet = self.nodes[0].get_wallet_rpc("encblanklegacy")
133+
134+
info = wallet.getwalletinfo()
135+
assert_equal(info["descriptors"], False)
136+
assert_equal(info["blank"], True)
137+
assert "hdseedid" not in info
138+
139+
wallet.encryptwallet("pass")
140+
info = wallet.getwalletinfo()
141+
assert_equal(info["blank"], True)
142+
assert "hdseedid" not in info
143+
144+
def test_encrypt_descriptors(self):
145+
if not self.options.descriptors:
146+
return
147+
self.log.info("Test that encrypting a blank descriptor wallet preserves the blank flag and descriptors remain the same")
148+
self.nodes[0].createwallet(wallet_name="encblankdesc", blank=True)
149+
wallet = self.nodes[0].get_wallet_rpc("encblankdesc")
150+
151+
info = wallet.getwalletinfo()
152+
assert_equal(info["descriptors"], True)
153+
assert_equal(info["blank"], True)
154+
descs = wallet.listdescriptors()
155+
156+
wallet.encryptwallet("pass")
157+
assert_equal(wallet.getwalletinfo()["blank"], True)
158+
assert_equal(descs, wallet.listdescriptors())
159+
160+
def run_test(self):
161+
self.test_importaddress()
162+
self.test_importpubkey()
163+
self.test_importprivkey()
164+
self.test_importmulti()
165+
self.test_importdescriptors()
166+
self.test_importwallet()
167+
self.test_encrypt_legacy()
168+
self.test_encrypt_descriptors()
169+
170+
171+
if __name__ == '__main__':
172+
WalletBlankTest().main()

0 commit comments

Comments
 (0)