wallet, rpc: add listdescriptors private option

This commit is contained in:
S3RK 2021-06-24 08:35:23 +02:00
parent 088b348dbe
commit bb822a7af8
6 changed files with 52 additions and 7 deletions

View File

@ -142,6 +142,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "importmulti", 0, "requests" }, { "importmulti", 0, "requests" },
{ "importmulti", 1, "options" }, { "importmulti", 1, "options" },
{ "importdescriptors", 0, "requests" }, { "importdescriptors", 0, "requests" },
{ "listdescriptors", 0, "private" },
{ "verifychain", 0, "checklevel" }, { "verifychain", 0, "checklevel" },
{ "verifychain", 1, "nblocks" }, { "verifychain", 1, "nblocks" },
{ "getblockstats", 0, "hash_or_height" }, { "getblockstats", 0, "hash_or_height" },

View File

@ -1755,8 +1755,10 @@ RPCHelpMan listdescriptors()
{ {
return RPCHelpMan{ return RPCHelpMan{
"listdescriptors", "listdescriptors",
"\nList descriptors imported into a descriptor-enabled wallet.", "\nList descriptors imported into a descriptor-enabled wallet.\n",
{}, {
{"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private descriptors."}
},
RPCResult{RPCResult::Type::OBJ, "", "", { RPCResult{RPCResult::Type::OBJ, "", "", {
{RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"}, {RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
{RPCResult::Type::ARR, "descriptors", "Array of descriptor objects", {RPCResult::Type::ARR, "descriptors", "Array of descriptor objects",
@ -1776,6 +1778,7 @@ RPCHelpMan listdescriptors()
}}, }},
RPCExamples{ RPCExamples{
HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "") HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "")
+ HelpExampleCli("listdescriptors", "true") + HelpExampleRpc("listdescriptors", "true")
}, },
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
@ -1786,6 +1789,11 @@ RPCHelpMan listdescriptors()
throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets"); throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets");
} }
const bool priv = !request.params[0].isNull() && request.params[0].get_bool();
if (priv) {
EnsureWalletIsUnlocked(*wallet);
}
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
UniValue descriptors(UniValue::VARR); UniValue descriptors(UniValue::VARR);
@ -1799,8 +1807,9 @@ RPCHelpMan listdescriptors()
LOCK(desc_spk_man->cs_desc_man); LOCK(desc_spk_man->cs_desc_man);
const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor(); const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor();
std::string descriptor; std::string descriptor;
if (!desc_spk_man->GetDescriptorString(descriptor)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Can't get normalized descriptor string."); if (!desc_spk_man->GetDescriptorString(descriptor, priv)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Can't get descriptor string.");
} }
spk.pushKV("desc", descriptor); spk.pushKV("desc", descriptor);
spk.pushKV("timestamp", wallet_descriptor.creation_time); spk.pushKV("timestamp", wallet_descriptor.creation_time);

View File

@ -3872,7 +3872,7 @@ RPCHelpMan getaddressinfo()
DescriptorScriptPubKeyMan* desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(pwallet->GetScriptPubKeyMan(scriptPubKey)); DescriptorScriptPubKeyMan* desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(pwallet->GetScriptPubKeyMan(scriptPubKey));
if (desc_spk_man) { if (desc_spk_man) {
std::string desc_str; std::string desc_str;
if (desc_spk_man->GetDescriptorString(desc_str)) { if (desc_spk_man->GetDescriptorString(desc_str, /* priv */ false)) {
ret.pushKV("parent_desc", desc_str); ret.pushKV("parent_desc", desc_str);
} }
} }

View File

@ -2258,13 +2258,20 @@ const std::vector<CScript> DescriptorScriptPubKeyMan::GetScriptPubKeys() const
return script_pub_keys; return script_pub_keys;
} }
bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out) const bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out, const bool priv) const
{ {
LOCK(cs_desc_man); LOCK(cs_desc_man);
FlatSigningProvider provider; FlatSigningProvider provider;
provider.keys = GetKeys(); provider.keys = GetKeys();
if (priv) {
// For the private version, always return the master key to avoid
// exposing child private keys. The risk implications of exposing child
// private keys together with the parent xpub may be non-obvious for users.
return m_wallet_descriptor.descriptor->ToPrivateString(provider, out);
}
return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache); return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache);
} }

View File

@ -621,7 +621,7 @@ public:
const WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); const WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
const std::vector<CScript> GetScriptPubKeys() const; const std::vector<CScript> GetScriptPubKeys() const;
bool GetDescriptorString(std::string& out) const; bool GetDescriptorString(std::string& out, const bool priv) const;
void UpgradeDescriptorCache(); void UpgradeDescriptorCache();
}; };

View File

@ -71,11 +71,39 @@ class ListDescriptorsTest(BitcoinTestFramework):
], ],
} }
assert_equal(expected, wallet.listdescriptors()) assert_equal(expected, wallet.listdescriptors())
assert_equal(expected, wallet.listdescriptors(False))
self.log.info('Test list private descriptors')
expected_private = {
'wallet_name': 'w2',
'descriptors': [
{'desc': descsum_create('wpkh(' + xprv + hardened_path + '/0/*)'),
'timestamp': 1296688602,
'active': False,
'range': [0, 0],
'next': 0},
],
}
assert_equal(expected_private, wallet.listdescriptors(True))
self.log.info("Test listdescriptors with encrypted wallet") self.log.info("Test listdescriptors with encrypted wallet")
wallet.encryptwallet("pass") wallet.encryptwallet("pass")
assert_equal(expected, wallet.listdescriptors()) assert_equal(expected, wallet.listdescriptors())
self.log.info('Test list private descriptors with encrypted wallet')
assert_raises_rpc_error(-13, 'Please enter the wallet passphrase with walletpassphrase first.', wallet.listdescriptors, True)
wallet.walletpassphrase(passphrase="pass", timeout=1000000)
assert_equal(expected_private, wallet.listdescriptors(True))
self.log.info('Test list private descriptors with watch-only wallet')
node.createwallet(wallet_name='watch-only', descriptors=True, disable_private_keys=True)
watch_only_wallet = node.get_wallet_rpc('watch-only')
watch_only_wallet.importdescriptors([{
'desc': descsum_create('wpkh(' + xpub_acc + ')'),
'timestamp': 1296688602,
}])
assert_raises_rpc_error(-4, 'Can\'t get descriptor string', watch_only_wallet.listdescriptors, True)
self.log.info('Test non-active non-range combo descriptor') self.log.info('Test non-active non-range combo descriptor')
node.createwallet(wallet_name='w4', blank=True, descriptors=True) node.createwallet(wallet_name='w4', blank=True, descriptors=True)
wallet = node.get_wallet_rpc('w4') wallet = node.get_wallet_rpc('w4')