From cbf4dd3e41661b1e837e52d2d5b6f0b5601bb8cc Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Thu, 4 Jan 2024 14:02:19 -0500 Subject: [PATCH] wallet, rpc: Disallow importing unused() to wallets without privkeys --- src/wallet/rpc/backup.cpp | 3 +++ test/functional/wallet_importdescriptors.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp index 5449ab26455..801baac42d8 100644 --- a/src/wallet/rpc/backup.cpp +++ b/src/wallet/rpc/backup.cpp @@ -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. diff --git a/test/functional/wallet_importdescriptors.py b/test/functional/wallet_importdescriptors.py index c2534617ec0..ea9bd54dd07 100755 --- a/test/functional/wallet_importdescriptors.py +++ b/test/functional/wallet_importdescriptors.py @@ -99,6 +99,20 @@ class ImportDescriptorsTest(BitcoinTestFramework): 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) @@ -808,6 +822,7 @@ class ImportDescriptorsTest(BitcoinTestFramework): self.test_import_unused_key() self.test_import_unused_key_existing() + self.test_import_unused_noprivs() if __name__ == '__main__': ImportDescriptorsTest(__file__).main()