Skip to content

Commit

Permalink
wallet, rpc: Disallow importing unused() to wallets without privkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
achow101 committed Sep 10, 2024
1 parent b9ad563 commit f4e9929
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/wallet/rpc/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,9 @@ static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, c

// If this is an unused(KEY) descriptor, check that the wallet doesn't already have other descriptors with this key
if (!parsed_desc->HasScripts()) {
if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import unused() to wallet without private keys enabled");
}
// Unused descriptors must contain a single key.
// Earlier checks will have enforced that this key is either a private key when private keys are enabled,
// or that this key is a public key when private keys are disabled.
Expand Down
15 changes: 15 additions & 0 deletions test/functional/wallet_importdescriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ def test_import_unused_key_existing(self):
wallet=wallet)
wallet.unloadwallet()

def test_import_unused_noprivs(self):
self.log.info("Test import of unused(KEY) to wallet without privkeys")
self.nodes[0].createwallet(wallet_name="import_unused_noprivs", disable_private_keys=True)
wallet = self.nodes[0].get_wallet_rpc("import_unused_noprivs")

xpub = "tpubD6NzVbkrYhZ4YNXVQbNhMK1WqguFsUXceaVJKbmno2aZ3B6QfbMeraaYvnBSGpV3vxLyTTK9DYT1yoEck4XUScMzXoQ2U2oSmE2JyMedq3H"
self.test_importdesc({"timestamp": "now", "desc": descsum_create(f"unused({xpub})")},
success=False,
error_code=-4,
error_message="Cannot import unused() to wallet without private keys enabled",
wallet=wallet)
wallet.unloadwallet()


def run_test(self):
self.log.info('Setting up wallets')
self.nodes[0].createwallet(wallet_name='w0', disable_private_keys=False, descriptors=True)
Expand Down Expand Up @@ -795,6 +809,7 @@ def run_test(self):

self.test_import_unused_key()
self.test_import_unused_key_existing()
self.test_import_unused_noprivs()

if __name__ == '__main__':
ImportDescriptorsTest(__file__).main()

0 comments on commit f4e9929

Please sign in to comment.