Skip to content

Commit 8fa03c4

Browse files
committed
Merge bitcoin/bitcoin#21500: wallet, rpc: add an option to list private descriptors
bb822a7 wallet, rpc: add listdescriptors private option (S3RK) Pull request description: Rationale: make it possible to backup your wallet with `listdescriptors` command * The default behaviour is still to show public version * For private version only the root xprv is returned Example use-case: ``` > bitcoin-cli -regtest -named createwallet wallet_name=old descriptors=true > bitcoin-cli -regtest -rpcwallet=old listdescriptors true | jq '.descriptors' > descriptors.txt > bitcoin-cli -regtest -named createwallet wallet_name=new descriptors=true blank=true > bitcoin-cli -regtest -rpcwallet=new importdescriptors "$(cat descriptors.txt)" ``` In case of watch-only wallet without private keys there will be following output: ``` error code: -4 error message: Can't get descriptor string. ``` ACKs for top commit: achow101: re-ACK bb822a7 Rspigler: tACK bb822a7 jonatack: ACK bb822a7 per `git diff 2854ddc bb822a7` prayank23: tACK bitcoin/bitcoin@bb822a7 meshcollider: Code review ACK bb822a7 Tree-SHA512: f6dddc72a74e5667071ccd77f8dce578382e8e29e7ed6a0834ac2e114a6d3918b59c2f194f4079b3259e13d9ba3b4f405619940c3ecb7a1a0344615aed47c43d
2 parents db94d74 + bb822a7 commit 8fa03c4

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
@@ -1760,8 +1760,10 @@ RPCHelpMan listdescriptors()
17601760
{
17611761
return RPCHelpMan{
17621762
"listdescriptors",
1763-
"\nList descriptors imported into a descriptor-enabled wallet.",
1764-
{},
1763+
"\nList descriptors imported into a descriptor-enabled wallet.\n",
1764+
{
1765+
{"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private descriptors."}
1766+
},
17651767
RPCResult{RPCResult::Type::OBJ, "", "", {
17661768
{RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
17671769
{RPCResult::Type::ARR, "descriptors", "Array of descriptor objects",
@@ -1781,6 +1783,7 @@ RPCHelpMan listdescriptors()
17811783
}},
17821784
RPCExamples{
17831785
HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "")
1786+
+ HelpExampleCli("listdescriptors", "true") + HelpExampleRpc("listdescriptors", "true")
17841787
},
17851788
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
17861789
{
@@ -1791,6 +1794,11 @@ RPCHelpMan listdescriptors()
17911794
throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets");
17921795
}
17931796

1797+
const bool priv = !request.params[0].isNull() && request.params[0].get_bool();
1798+
if (priv) {
1799+
EnsureWalletIsUnlocked(*wallet);
1800+
}
1801+
17941802
LOCK(wallet->cs_wallet);
17951803

17961804
UniValue descriptors(UniValue::VARR);
@@ -1804,8 +1812,9 @@ RPCHelpMan listdescriptors()
18041812
LOCK(desc_spk_man->cs_desc_man);
18051813
const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor();
18061814
std::string descriptor;
1807-
if (!desc_spk_man->GetDescriptorString(descriptor)) {
1808-
throw JSONRPCError(RPC_WALLET_ERROR, "Can't get normalized descriptor string.");
1815+
1816+
if (!desc_spk_man->GetDescriptorString(descriptor, priv)) {
1817+
throw JSONRPCError(RPC_WALLET_ERROR, "Can't get descriptor string.");
18091818
}
18101819
spk.pushKV("desc", descriptor);
18111820
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
@@ -3875,7 +3875,7 @@ RPCHelpMan getaddressinfo()
38753875
DescriptorScriptPubKeyMan* desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(pwallet->GetScriptPubKeyMan(scriptPubKey));
38763876
if (desc_spk_man) {
38773877
std::string desc_str;
3878-
if (desc_spk_man->GetDescriptorString(desc_str)) {
3878+
if (desc_spk_man->GetDescriptorString(desc_str, /* priv */ false)) {
38793879
ret.pushKV("parent_desc", desc_str);
38803880
}
38813881
}

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
@@ -72,11 +72,39 @@ def run_test(self):
7272
],
7373
}
7474
assert_equal(expected, wallet.listdescriptors())
75+
assert_equal(expected, wallet.listdescriptors(False))
76+
77+
self.log.info('Test list private descriptors')
78+
expected_private = {
79+
'wallet_name': 'w2',
80+
'descriptors': [
81+
{'desc': descsum_create('wpkh(' + xprv + hardened_path + '/0/*)'),
82+
'timestamp': 1296688602,
83+
'active': False,
84+
'range': [0, 0],
85+
'next': 0},
86+
],
87+
}
88+
assert_equal(expected_private, wallet.listdescriptors(True))
7589

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

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

0 commit comments

Comments
 (0)