Skip to content

Commit bb822a7

Browse files
committed
wallet, rpc: add listdescriptors private option
1 parent 088b348 commit bb822a7

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
142142
{ "importmulti", 0, "requests" },
143143
{ "importmulti", 1, "options" },
144144
{ "importdescriptors", 0, "requests" },
145+
{ "listdescriptors", 0, "private" },
145146
{ "verifychain", 0, "checklevel" },
146147
{ "verifychain", 1, "nblocks" },
147148
{ "getblockstats", 0, "hash_or_height" },

src/wallet/rpcdump.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,8 +1755,10 @@ RPCHelpMan listdescriptors()
17551755
{
17561756
return RPCHelpMan{
17571757
"listdescriptors",
1758-
"\nList descriptors imported into a descriptor-enabled wallet.",
1759-
{},
1758+
"\nList descriptors imported into a descriptor-enabled wallet.\n",
1759+
{
1760+
{"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private descriptors."}
1761+
},
17601762
RPCResult{RPCResult::Type::OBJ, "", "", {
17611763
{RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
17621764
{RPCResult::Type::ARR, "descriptors", "Array of descriptor objects",
@@ -1776,6 +1778,7 @@ RPCHelpMan listdescriptors()
17761778
}},
17771779
RPCExamples{
17781780
HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "")
1781+
+ HelpExampleCli("listdescriptors", "true") + HelpExampleRpc("listdescriptors", "true")
17791782
},
17801783
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
17811784
{
@@ -1786,6 +1789,11 @@ RPCHelpMan listdescriptors()
17861789
throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets");
17871790
}
17881791

1792+
const bool priv = !request.params[0].isNull() && request.params[0].get_bool();
1793+
if (priv) {
1794+
EnsureWalletIsUnlocked(*wallet);
1795+
}
1796+
17891797
LOCK(wallet->cs_wallet);
17901798

17911799
UniValue descriptors(UniValue::VARR);
@@ -1799,8 +1807,9 @@ RPCHelpMan listdescriptors()
17991807
LOCK(desc_spk_man->cs_desc_man);
18001808
const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor();
18011809
std::string descriptor;
1802-
if (!desc_spk_man->GetDescriptorString(descriptor)) {
1803-
throw JSONRPCError(RPC_WALLET_ERROR, "Can't get normalized descriptor string.");
1810+
1811+
if (!desc_spk_man->GetDescriptorString(descriptor, priv)) {
1812+
throw JSONRPCError(RPC_WALLET_ERROR, "Can't get descriptor string.");
18041813
}
18051814
spk.pushKV("desc", descriptor);
18061815
spk.pushKV("timestamp", wallet_descriptor.creation_time);

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3872,7 +3872,7 @@ RPCHelpMan getaddressinfo()
38723872
DescriptorScriptPubKeyMan* desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(pwallet->GetScriptPubKeyMan(scriptPubKey));
38733873
if (desc_spk_man) {
38743874
std::string desc_str;
3875-
if (desc_spk_man->GetDescriptorString(desc_str)) {
3875+
if (desc_spk_man->GetDescriptorString(desc_str, /* priv */ false)) {
38763876
ret.pushKV("parent_desc", desc_str);
38773877
}
38783878
}

src/wallet/scriptpubkeyman.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2258,13 +2258,20 @@ const std::vector<CScript> DescriptorScriptPubKeyMan::GetScriptPubKeys() const
22582258
return script_pub_keys;
22592259
}
22602260

2261-
bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out) const
2261+
bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out, const bool priv) const
22622262
{
22632263
LOCK(cs_desc_man);
22642264

22652265
FlatSigningProvider provider;
22662266
provider.keys = GetKeys();
22672267

2268+
if (priv) {
2269+
// For the private version, always return the master key to avoid
2270+
// exposing child private keys. The risk implications of exposing child
2271+
// private keys together with the parent xpub may be non-obvious for users.
2272+
return m_wallet_descriptor.descriptor->ToPrivateString(provider, out);
2273+
}
2274+
22682275
return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache);
22692276
}
22702277

src/wallet/scriptpubkeyman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
621621
const WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
622622
const std::vector<CScript> GetScriptPubKeys() const;
623623

624-
bool GetDescriptorString(std::string& out) const;
624+
bool GetDescriptorString(std::string& out, const bool priv) const;
625625

626626
void UpgradeDescriptorCache();
627627
};

test/functional/wallet_listdescriptors.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,39 @@ def run_test(self):
7171
],
7272
}
7373
assert_equal(expected, wallet.listdescriptors())
74+
assert_equal(expected, wallet.listdescriptors(False))
75+
76+
self.log.info('Test list private descriptors')
77+
expected_private = {
78+
'wallet_name': 'w2',
79+
'descriptors': [
80+
{'desc': descsum_create('wpkh(' + xprv + hardened_path + '/0/*)'),
81+
'timestamp': 1296688602,
82+
'active': False,
83+
'range': [0, 0],
84+
'next': 0},
85+
],
86+
}
87+
assert_equal(expected_private, wallet.listdescriptors(True))
7488

7589
self.log.info("Test listdescriptors with encrypted wallet")
7690
wallet.encryptwallet("pass")
7791
assert_equal(expected, wallet.listdescriptors())
7892

93+
self.log.info('Test list private descriptors with encrypted wallet')
94+
assert_raises_rpc_error(-13, 'Please enter the wallet passphrase with walletpassphrase first.', wallet.listdescriptors, True)
95+
wallet.walletpassphrase(passphrase="pass", timeout=1000000)
96+
assert_equal(expected_private, wallet.listdescriptors(True))
97+
98+
self.log.info('Test list private descriptors with watch-only wallet')
99+
node.createwallet(wallet_name='watch-only', descriptors=True, disable_private_keys=True)
100+
watch_only_wallet = node.get_wallet_rpc('watch-only')
101+
watch_only_wallet.importdescriptors([{
102+
'desc': descsum_create('wpkh(' + xpub_acc + ')'),
103+
'timestamp': 1296688602,
104+
}])
105+
assert_raises_rpc_error(-4, 'Can\'t get descriptor string', watch_only_wallet.listdescriptors, True)
106+
79107
self.log.info('Test non-active non-range combo descriptor')
80108
node.createwallet(wallet_name='w4', blank=True, descriptors=True)
81109
wallet = node.get_wallet_rpc('w4')

0 commit comments

Comments
 (0)