From 1fcf9e6e81ea8299fad958b32777c36b696090ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A8le=20Oul=C3=A8s?= Date: Sat, 17 Sep 2022 21:38:35 +0200 Subject: [PATCH 1/2] rpc: Allow importmulti watchonly imports with locked wallet --- src/wallet/rpc/backup.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp index 35df151e84f..09c74ea2daa 100644 --- a/src/wallet/rpc/backup.cpp +++ b/src/wallet/rpc/backup.cpp @@ -1363,7 +1363,18 @@ RPCHelpMan importmulti() UniValue response(UniValue::VARR); { LOCK(pwallet->cs_wallet); - EnsureWalletIsUnlocked(*pwallet); + + // Check all requests are watchonly + bool is_watchonly{true}; + for (size_t i = 0; i < requests.size(); ++i) { + const UniValue& request = requests[i]; + if (!request.exists("watchonly") || !request["watchonly"].get_bool()) { + is_watchonly = false; + break; + } + } + // Wallet does not need to be unlocked if all requests are watchonly + if (!is_watchonly) EnsureWalletIsUnlocked(wallet); // Verify all timestamps are present before importing any keys. CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nLowestTimestamp).mtpTime(now))); From 2c03465dfa18ee615f76b6e507a65ef451ce1b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A8le=20Oul=C3=A8s?= Date: Sat, 17 Sep 2022 22:22:24 +0200 Subject: [PATCH 2/2] test: Test watchonly imports with passphrase-locked wallet --- test/functional/wallet_importmulti.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/functional/wallet_importmulti.py b/test/functional/wallet_importmulti.py index 39538514910..62a1a3341d1 100755 --- a/test/functional/wallet_importmulti.py +++ b/test/functional/wallet_importmulti.py @@ -874,6 +874,25 @@ class ImportMultiTest(BitcoinTestFramework): addr = wrpc.getnewaddress('', 'bech32') assert_equal(addr, addresses[i]) + # Create wallet with passphrase + self.log.info('Test watchonly imports on a wallet with a passphrase, without unlocking') + self.nodes[1].createwallet(wallet_name='w1', blank=True, passphrase='pass') + wrpc = self.nodes[1].get_wallet_rpc('w1') + assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first.", + wrpc.importmulti, [{ + 'desc': descsum_create('wpkh(' + pub1 + ')'), + "timestamp": "now", + }]) + + result = wrpc.importmulti( + [{ + 'desc': descsum_create('wpkh(' + pub1 + ')'), + "timestamp": "now", + "watchonly": True, + }] + ) + assert result[0]['success'] + if __name__ == '__main__': ImportMultiTest().main()