aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2020-02-26 16:52:03 -0500
committerAndrew Chow <achow101-github@achow101.com>2020-03-07 10:13:47 -0500
commitdeb791c7ba057a3765d09b12bf3e55547a5298e4 (patch)
tree8ed217eb7166a4a171b55ba4c1ebe90bfc736442 /src/test
parentf76733eda5f4c161e9eb47c74b949582ab8f448a (diff)
Only cache xpubs that have a hardened last step
Also adds tests for this: For ranged descriptors with unhardened derivation, we expect to find parent keys in the cache but no child keys. For descriptors containing an xpub but do not have unhardened derivation (i.e. hardened derivation or single xpub with or without derivation), we expect to find all of the keys in the cache, and the same number of keys in the cache as in the SigningProvider. For everything else (no xpub), nothing should be cached at all.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/descriptor_tests.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp
index 7f20a5c022..3154c619d2 100644
--- a/src/test/descriptor_tests.cpp
+++ b/src/test/descriptor_tests.cpp
@@ -149,6 +149,54 @@ void DoCheck(const std::string& prv, const std::string& pub, int flags, const st
BOOST_CHECK(script_provider.scripts == script_provider_cached.scripts);
BOOST_CHECK(script_provider.origins == script_provider_cached.origins);
+ // Check whether keys are in the cache
+ const auto& der_xpub_cache = desc_cache.GetCachedDerivedExtPubKeys();
+ const auto& parent_xpub_cache = desc_cache.GetCachedParentExtPubKeys();
+ if ((flags & RANGE) && !(flags & DERIVE_HARDENED)) {
+ // For ranged, unhardened derivation, None of the keys in origins should appear in the cache but the cache should have parent keys
+ // But we can derive one level from each of those parent keys and find them all
+ BOOST_CHECK(der_xpub_cache.empty());
+ BOOST_CHECK(parent_xpub_cache.size() > 0);
+ std::set<CPubKey> pubkeys;
+ for (const auto& xpub_pair : parent_xpub_cache) {
+ const CExtPubKey& xpub = xpub_pair.second;
+ CExtPubKey der;
+ xpub.Derive(der, i);
+ pubkeys.insert(der.pubkey);
+ }
+ for (const auto& origin_pair : script_provider_cached.origins) {
+ const CPubKey& pk = origin_pair.second.first;
+ BOOST_CHECK(pubkeys.count(pk) > 0);
+ }
+ } else if (pub1.find("xpub") != std::string::npos) {
+ // For ranged, hardened derivation, or not ranged, but has an xpub, all of the keys should appear in the cache
+ BOOST_CHECK(der_xpub_cache.size() + parent_xpub_cache.size() == script_provider_cached.origins.size());
+ // Get all of the derived pubkeys
+ std::set<CPubKey> pubkeys;
+ for (const auto& xpub_map_pair : der_xpub_cache) {
+ for (const auto& xpub_pair : xpub_map_pair.second) {
+ const CExtPubKey& xpub = xpub_pair.second;
+ pubkeys.insert(xpub.pubkey);
+ }
+ }
+ // Derive one level from all of the parents
+ for (const auto& xpub_pair : parent_xpub_cache) {
+ const CExtPubKey& xpub = xpub_pair.second;
+ pubkeys.insert(xpub.pubkey);
+ CExtPubKey der;
+ xpub.Derive(der, i);
+ pubkeys.insert(der.pubkey);
+ }
+ for (const auto& origin_pair : script_provider_cached.origins) {
+ const CPubKey& pk = origin_pair.second.first;
+ BOOST_CHECK(pubkeys.count(pk) > 0);
+ }
+ } else {
+ // No xpub, nothing should be cached
+ BOOST_CHECK(der_xpub_cache.empty());
+ BOOST_CHECK(parent_xpub_cache.empty());
+ }
+
// Make sure we can expand using cached xpubs for unhardened derivation
if (!(flags & DERIVE_HARDENED)) {
// Evaluate the descriptor at i + 1