Skip to content

Commit

Permalink
Merge bitcoin#20153: wallet: do not import a descriptor with hardened…
Browse files Browse the repository at this point in the history
… derivations into a watch-only wallet

538be42 wallet: fix importdescriptor silent fail (Ivan Metlushko)

Pull request description:

  Currently `importdescriptor` command will successfully import a descriptor with hardened derivations into a watch-only wallet while silently failing to expand the descriptor to fill the cache. This leads to a broken wallet state and failure to load such wallet due to missing cache on subsequent restart.

ACKs for top commit:
  laanwj:
    Code review ACK 538be42
  achow101:
    ACK 538be42
  meshcollider:
    utACK 538be42

Tree-SHA512: 4bdd0ab4437d55b3f1a79c3a300a0b186089155c020fe220a73d0cce274de47d90371d88918d39fd795f9fccf8db328f1e322d29a6062f9ce94a1c254398f004
  • Loading branch information
laanwj authored and knst committed Mar 6, 2024
1 parent c995e5d commit b18351e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,9 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
// Need to ExpandPrivate to check if private keys are available for all pubkeys
FlatSigningProvider expand_keys;
std::vector<CScript> scripts;
parsed_desc->Expand(0, keys, scripts, expand_keys);
if (!parsed_desc->Expand(0, keys, scripts, expand_keys)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot expand descriptor. Probably because of hardened derivations without private keys provided");
}
parsed_desc->ExpandPrivate(0, keys, expand_keys);

// Check if all private keys are provided
Expand Down
5 changes: 4 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5637,7 +5637,10 @@ ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const Flat
}

// Top up key pool, the manager will generate new scriptPubKeys internally
new_spk_man->TopUp();
if (!new_spk_man->TopUp()) {
WalletLogPrintf("Could not top up scriptPubKeys\n");
return nullptr;
}

// Apply the label if necessary
// Note: we disable labels for ranged descriptors
Expand Down
9 changes: 9 additions & 0 deletions test/functional/wallet_importdescriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ def run_test(self):
success=False,
error_code=-4,
error_message='Cannot import private keys to a wallet with private keys disabled')

self.log.info("Should not import a descriptor with hardened derivations when private keys are disabled")
self.test_importdesc({"desc": descsum_create("pkh(" + xpub + "/1h/*)"),
"timestamp": "now",
"range": 1},
success=False,
error_code=-4,
error_message='Cannot expand descriptor. Probably because of hardened derivations without private keys provided')

for address in addresses:
test_address(w1,
address,
Expand Down

0 comments on commit b18351e

Please sign in to comment.