From 6b91f7df96ddefc32a193161339b20c79c5e4eae Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Mon, 12 Feb 2024 17:06:01 -0500 Subject: [PATCH] descriptors: Have GetPrivKey fill keys directly Instead of GetPrivKey returning a key and having the caller fill the FlatSigningProvider, have GetPrivKey take the FlatSigningProvider and fill it by itself. GetPrivKey is now changed to void as the caller no longer cares whether it succeeds or fails. --- src/script/descriptor.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 70294629670b02..e20677c05ff62e 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -212,7 +212,7 @@ struct PubkeyProvider virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const = 0; /** Derive a private key, if private data is available in arg. */ - virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0; + virtual void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const = 0; /** Return the non-extended public key for this PubkeyProvider, if it has one. */ virtual std::optional GetRootPubKey() const = 0; @@ -274,9 +274,9 @@ class OriginPubkeyProvider final : public PubkeyProvider } return true; } - bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override + void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override { - return m_provider->GetPrivKey(pos, arg, key); + m_provider->GetPrivKey(pos, arg, out); } std::optional GetRootPubKey() const override { @@ -332,9 +332,12 @@ class ConstPubkeyProvider final : public PubkeyProvider ret = ToString(StringType::PUBLIC); return true; } - bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override + void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override { - return arg.GetKey(m_pubkey.GetID(), key); + CKeyID id = m_pubkey.GetID(); + CKey key; + if (!arg.GetKey(id, key)) return; + out.keys.emplace(id, key); } std::optional GetRootPubKey() const override { @@ -552,15 +555,14 @@ class BIP32PubkeyProvider final : public PubkeyProvider } return true; } - bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override + void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override { CExtKey extkey; CExtKey dummy; - if (!GetDerivedExtKey(arg, extkey, dummy)) return false; - if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return false; - if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return false; - key = extkey.key; - return true; + if (!GetDerivedExtKey(arg, extkey, dummy)) return; + if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return; + if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return; + out.keys.emplace(extkey.key.GetPubKey().GetID(), extkey.key); } std::optional GetRootPubKey() const override { @@ -747,8 +749,7 @@ class DescriptorImpl : public Descriptor { for (const auto& p : m_pubkey_args) { CKey key; - if (!p->GetPrivKey(pos, provider, key)) continue; - out.keys.emplace(key.GetPubKey().GetID(), key); + p->GetPrivKey(pos, provider, out); } for (const auto& arg : m_subdescriptor_args) { arg->ExpandPrivate(pos, provider, out);