Skip to content

Commit 432ba9e

Browse files
committed
wallet: Store last hardened xpub cache
1 parent d87b544 commit 432ba9e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/wallet/walletdb.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const std::string TX{"tx"};
5252
const std::string VERSION{"version"};
5353
const std::string WALLETDESCRIPTOR{"walletdescriptor"};
5454
const std::string WALLETDESCRIPTORCACHE{"walletdescriptorcache"};
55+
const std::string WALLETDESCRIPTORLHCACHE{"walletdescriptorlhcache"};
5556
const std::string WALLETDESCRIPTORCKEY{"walletdescriptorckey"};
5657
const std::string WALLETDESCRIPTORKEY{"walletdescriptorkey"};
5758
const std::string WATCHMETA{"watchmeta"};
@@ -248,6 +249,13 @@ bool WalletBatch::WriteDescriptorParentCache(const CExtPubKey& xpub, const uint2
248249
return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), key_exp_index), ser_xpub);
249250
}
250251

252+
bool WalletBatch::WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index)
253+
{
254+
std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
255+
xpub.Encode(ser_xpub.data());
256+
return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORLHCACHE, desc_id), key_exp_index), ser_xpub);
257+
}
258+
251259
bool WalletBatch::WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache)
252260
{
253261
for (const auto& parent_xpub_pair : cache.GetCachedParentExtPubKeys()) {
@@ -262,6 +270,11 @@ bool WalletBatch::WriteDescriptorCacheItems(const uint256& desc_id, const Descri
262270
}
263271
}
264272
}
273+
for (const auto& lh_xpub_pair : cache.GetCachedLastHardenedExtPubKeys()) {
274+
if (!WriteDescriptorLastHardenedCache(lh_xpub_pair.second, desc_id, lh_xpub_pair.first)) {
275+
return false;
276+
}
277+
}
265278
return true;
266279
}
267280

@@ -619,6 +632,17 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
619632
} else {
620633
wss.m_descriptor_caches[desc_id].CacheDerivedExtPubKey(key_exp_index, der_index, xpub);
621634
}
635+
} else if (strType == DBKeys::WALLETDESCRIPTORLHCACHE) {
636+
uint256 desc_id;
637+
uint32_t key_exp_index;
638+
ssKey >> desc_id;
639+
ssKey >> key_exp_index;
640+
641+
std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
642+
ssValue >> ser_xpub;
643+
CExtPubKey xpub;
644+
xpub.Decode(ser_xpub.data());
645+
wss.m_descriptor_caches[desc_id].CacheLastHardenedExtPubKey(key_exp_index, xpub);
622646
} else if (strType == DBKeys::WALLETDESCRIPTORKEY) {
623647
uint256 desc_id;
624648
CPubKey pubkey;

src/wallet/walletdb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ class WalletBatch
246246
bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
247247
bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
248248
bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
249+
bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
249250
bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
250251

251252
/// Write destination data key,value tuple to database

0 commit comments

Comments
 (0)