Skip to content

Commit 474ea3b

Browse files
committed
Introduce DescriptorCache struct which caches xpubs
1 parent c3b4715 commit 474ea3b

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/script/descriptor.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,42 @@ std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const Signing
10501050
{
10511051
return InferScript(script, ParseScriptContext::TOP, provider);
10521052
}
1053+
1054+
void DescriptorCache::CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub)
1055+
{
1056+
m_parent_xpubs[key_exp_pos] = xpub;
1057+
}
1058+
1059+
void DescriptorCache::CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey& xpub)
1060+
{
1061+
auto& xpubs = m_derived_xpubs[key_exp_pos];
1062+
xpubs[der_index] = xpub;
1063+
}
1064+
1065+
bool DescriptorCache::GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const
1066+
{
1067+
const auto& it = m_parent_xpubs.find(key_exp_pos);
1068+
if (it == m_parent_xpubs.end()) return false;
1069+
xpub = it->second;
1070+
return true;
1071+
}
1072+
1073+
bool DescriptorCache::GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey& xpub) const
1074+
{
1075+
const auto& key_exp_it = m_derived_xpubs.find(key_exp_pos);
1076+
if (key_exp_it == m_derived_xpubs.end()) return false;
1077+
const auto& der_it = key_exp_it->second.find(der_index);
1078+
if (der_it == key_exp_it->second.end()) return false;
1079+
xpub = der_it->second;
1080+
return true;
1081+
}
1082+
1083+
const ExtPubKeyMap DescriptorCache::GetCachedParentExtPubKeys() const
1084+
{
1085+
return m_parent_xpubs;
1086+
}
1087+
1088+
const std::unordered_map<uint32_t, ExtPubKeyMap> DescriptorCache::GetCachedDerivedExtPubKeys() const
1089+
{
1090+
return m_derived_xpubs;
1091+
}

src/script/descriptor.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@
1313

1414
#include <vector>
1515

16+
using ExtPubKeyMap = std::unordered_map<uint32_t, CExtPubKey>;
17+
18+
/** Cache for single descriptor's derived extended pubkeys */
19+
class DescriptorCache {
20+
private:
21+
/** Map key expression index -> map of (key derivation index -> xpub) */
22+
std::unordered_map<uint32_t, ExtPubKeyMap> m_derived_xpubs;
23+
/** Map key expression index -> parent xpub */
24+
ExtPubKeyMap m_parent_xpubs;
25+
26+
public:
27+
/** Cache a parent xpub
28+
*
29+
* @param[in] key_exp_pos Position of the key expression within the descriptor
30+
* @param[in] xpub The CExtPubKey to cache
31+
*/
32+
void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub);
33+
/** Retrieve a cached parent xpub
34+
*
35+
* @param[in] key_exp_pos Position of the key expression within the descriptor
36+
* @param[in] xpub The CExtPubKey to get from cache
37+
*/
38+
bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const;
39+
/** Cache an xpub derived at an index
40+
*
41+
* @param[in] key_exp_pos Position of the key expression within the descriptor
42+
* @param[in] der_index Derivation index of the xpub
43+
* @param[in] xpub The CExtPubKey to cache
44+
*/
45+
void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey& xpub);
46+
/** Retrieve a cached xpub derived at an index
47+
*
48+
* @param[in] key_exp_pos Position of the key expression within the descriptor
49+
* @param[in] der_index Derivation index of the xpub
50+
* @param[in] xpub The CExtPubKey to get from cache
51+
*/
52+
bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey& xpub) const;
53+
54+
/** Retrieve all cached parent xpubs */
55+
const ExtPubKeyMap GetCachedParentExtPubKeys() const;
56+
/** Retrieve all cached derived xpubs */
57+
const std::unordered_map<uint32_t, ExtPubKeyMap> GetCachedDerivedExtPubKeys() const;
58+
};
1659

1760
/** \brief Interface for parsed descriptor objects.
1861
*

0 commit comments

Comments
 (0)