@@ -44,6 +44,7 @@ const std::string SETTINGS{"settings"};
4444const std::string TX{" tx" };
4545const std::string VERSION{" version" };
4646const std::string WALLETDESCRIPTOR{" walletdescriptor" };
47+ const std::string WALLETDESCRIPTORCACHE{" walletdescriptorcache" };
4748const std::string WATCHMETA{" watchmeta" };
4849const std::string WATCHS{" watchs" };
4950} // namespace DBKeys
@@ -200,6 +201,7 @@ class CWalletScanState {
200201 std::vector<uint256> vWalletUpgrade;
201202 std::map<OutputType, uint256> m_active_external_spks;
202203 std::map<OutputType, uint256> m_active_internal_spks;
204+ std::map<uint256, DescriptorCache> m_descriptor_caches;
203205
204206 CWalletScanState () {
205207 }
@@ -433,7 +435,38 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
433435 ssKey >> id;
434436 WalletDescriptor desc;
435437 ssValue >> desc;
438+ if (wss.m_descriptor_caches .count (id) == 0 ) {
439+ wss.m_descriptor_caches [id] = DescriptorCache ();
440+ }
436441 pwallet->LoadDescriptorScriptPubKeyMan (id, desc);
442+ } else if (strType == DBKeys::WALLETDESCRIPTORCACHE) {
443+ bool parent = true ;
444+ uint256 desc_id;
445+ uint32_t key_exp_index;
446+ uint32_t der_index;
447+ ssKey >> desc_id;
448+ ssKey >> key_exp_index;
449+
450+ // if the der_index exists, it's a derived xpub
451+ try
452+ {
453+ ssKey >> der_index;
454+ parent = false ;
455+ }
456+ catch (...) {}
457+
458+ std::vector<unsigned char > ser_xpub (BIP32_EXTKEY_SIZE);
459+ ssValue >> ser_xpub;
460+ CExtPubKey xpub;
461+ xpub.Decode (ser_xpub.data ());
462+ if (wss.m_descriptor_caches .count (desc_id)) {
463+ wss.m_descriptor_caches [desc_id] = DescriptorCache ();
464+ }
465+ if (parent) {
466+ wss.m_descriptor_caches [desc_id].CacheParentExtPubKey (key_exp_index, xpub);
467+ } else {
468+ wss.m_descriptor_caches [desc_id].CacheDerivedExtPubKey (key_exp_index, der_index, xpub);
469+ }
437470 } else if (strType != DBKeys::BESTBLOCK && strType != DBKeys::BESTBLOCK_NOMERKLE &&
438471 strType != DBKeys::MINVERSION && strType != DBKeys::ACENTRY &&
439472 strType != DBKeys::VERSION && strType != DBKeys::SETTINGS) {
@@ -535,6 +568,13 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
535568 pwallet->SetActiveScriptPubKeyMan (spk_man_pair.second , spk_man_pair.first , /* internal */ true , /* memonly */ true );
536569 }
537570
571+ // Set the descriptor caches
572+ for (auto desc_cache_pair : wss.m_descriptor_caches ) {
573+ auto spk_man = pwallet->GetScriptPubKeyMan (desc_cache_pair.first );
574+ assert (spk_man);
575+ ((DescriptorScriptPubKeyMan*)spk_man)->SetCache (desc_cache_pair.second );
576+ }
577+
538578 if (fNoncriticalErrors && result == DBErrors::LOAD_OK)
539579 result = DBErrors::NONCRITICAL_ERROR;
540580
0 commit comments