Skip to content

Commit b18351e

Browse files
laanwjknst
authored andcommitted
Merge bitcoin#20153: wallet: do not import a descriptor with hardened 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
1 parent c995e5d commit b18351e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/wallet/rpcdump.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,9 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
16921692
// Need to ExpandPrivate to check if private keys are available for all pubkeys
16931693
FlatSigningProvider expand_keys;
16941694
std::vector<CScript> scripts;
1695-
parsed_desc->Expand(0, keys, scripts, expand_keys);
1695+
if (!parsed_desc->Expand(0, keys, scripts, expand_keys)) {
1696+
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot expand descriptor. Probably because of hardened derivations without private keys provided");
1697+
}
16961698
parsed_desc->ExpandPrivate(0, keys, expand_keys);
16971699

16981700
// Check if all private keys are provided

src/wallet/wallet.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5637,7 +5637,10 @@ ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const Flat
56375637
}
56385638

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

56425645
// Apply the label if necessary
56435646
// Note: we disable labels for ranged descriptors

test/functional/wallet_importdescriptors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ def run_test(self):
205205
success=False,
206206
error_code=-4,
207207
error_message='Cannot import private keys to a wallet with private keys disabled')
208+
209+
self.log.info("Should not import a descriptor with hardened derivations when private keys are disabled")
210+
self.test_importdesc({"desc": descsum_create("pkh(" + xpub + "/1h/*)"),
211+
"timestamp": "now",
212+
"range": 1},
213+
success=False,
214+
error_code=-4,
215+
error_message='Cannot expand descriptor. Probably because of hardened derivations without private keys provided')
216+
208217
for address in addresses:
209218
test_address(w1,
210219
address,

0 commit comments

Comments
 (0)