Merge bitcoin/bitcoin#26116: rpc: Allow importmulti watchonly imports with locked wallet

2c03465dfa test: Test watchonly imports with passphrase-locked wallet (Aurèle Oulès)
1fcf9e6e81 rpc: Allow importmulti watchonly imports with locked wallet (Aurèle Oulès)

Pull request description:

  Allows watch-only imports on locked wallets with `importmulti`.
  Also adds a test.

  Fixes #17867.

ACKs for top commit:
  achow101:
    ACK 2c03465dfa
  kristapsk:
    re-ACK 2c03465dfa
  theStack:
    re-ACK 2c03465dfa

Tree-SHA512: 9978d6e59a230c0d160efd312c671cf59458797387d6622b6bf5c9e0681c1fcfebedb3d834fa9314dc5a1eda97e3295696352eacbeab9b43a46b942990087035
This commit is contained in:
Andrew Chow 2022-09-20 11:54:17 -04:00
commit fc4017552c
No known key found for this signature in database
GPG key ID: 17565732E08E5E41
2 changed files with 31 additions and 1 deletions

View file

@ -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)));

View file

@ -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()