diff options
author | Andrew Chow <achow101-github@achow101.com> | 2020-02-25 18:28:05 -0500 |
---|---|---|
committer | Andrew Chow <achow101-github@achow101.com> | 2020-03-07 10:13:47 -0500 |
commit | f76733eda5f4c161e9eb47c74b949582ab8f448a (patch) | |
tree | ca3ff2eb6bc4496b7d61d2d3a87f752f099fc85a /src/script/descriptor.cpp | |
parent | 58f54b686f663e4c46a2cf7a64560409007c7eb3 (diff) |
Cache the immediate derivation parent xpub
If unhardened derivation is used, cache the immediate derivation
parent xpub and use it for unhardened derivation
Diffstat (limited to 'src/script/descriptor.cpp')
-rw-r--r-- | src/script/descriptor.cpp | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index ac872c8cd2..30384fbd06 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -313,21 +313,30 @@ public: // Derive keys or fetch them from cache CExtPubKey final_extkey = m_root_extkey; + CExtPubKey parent_extkey = m_root_extkey; bool der = true; if (read_cache) { - if (!read_cache->GetCachedDerivedExtPubKey(m_expr_index, pos, final_extkey)) return false; + if (!read_cache->GetCachedDerivedExtPubKey(m_expr_index, pos, final_extkey)) { + if (m_derive == DeriveType::HARDENED) return false; + // Try to get the derivation parent + if (!read_cache->GetCachedParentExtPubKey(m_expr_index, parent_extkey)) return false; + final_extkey = parent_extkey; + if (m_derive == DeriveType::UNHARDENED) der = parent_extkey.Derive(final_extkey, pos); + } } else if (IsHardened()) { CExtKey xprv; if (!GetDerivedExtKey(arg, xprv)) return false; + parent_extkey = xprv.Neuter(); if (m_derive == DeriveType::UNHARDENED) der = xprv.Derive(xprv, pos); if (m_derive == DeriveType::HARDENED) der = xprv.Derive(xprv, pos | 0x80000000UL); final_extkey = xprv.Neuter(); } else { for (auto entry : m_path) { - der = final_extkey.Derive(final_extkey, entry); + der = parent_extkey.Derive(parent_extkey, entry); assert(der); } - if (m_derive == DeriveType::UNHARDENED) der = final_extkey.Derive(final_extkey, pos); + final_extkey = parent_extkey; + if (m_derive == DeriveType::UNHARDENED) der = parent_extkey.Derive(final_extkey, pos); assert(m_derive != DeriveType::HARDENED); } assert(der); @@ -337,6 +346,10 @@ public: if (write_cache) { write_cache->CacheDerivedExtPubKey(m_expr_index, pos, final_extkey); + // Only cache parent if there is any unhardened derivation + if (m_derive != DeriveType::HARDENED) { + write_cache->CacheParentExtPubKey(m_expr_index, parent_extkey); + } } return true; |