Skip to content

Commit

Permalink
descriptors: Have GetPrivKey fill keys directly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achow101 committed Jan 21, 2025
1 parent b04bce2 commit 1bfabb4
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ 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;
/** Derive a private key, if private data is available in arg and put it into out. */
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<CPubKey> GetRootPubKey() const = 0;
Expand Down Expand Up @@ -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<CPubKey> GetRootPubKey() const override
{
Expand All @@ -298,6 +298,14 @@ class ConstPubkeyProvider final : public PubkeyProvider
CPubKey m_pubkey;
bool m_xonly;

std::optional<CKey> GetPrivKey(const SigningProvider& arg) const
{
CKey key;
if (!(m_xonly ? arg.GetKeyByXOnly(XOnlyPubKey(m_pubkey), key) :
arg.GetKey(m_pubkey.GetID(), key))) return std::nullopt;
return key;
}

public:
ConstPubkeyProvider(uint32_t exp_index, const CPubKey& pubkey, bool xonly) : PubkeyProvider(exp_index), m_pubkey(pubkey), m_xonly(xonly) {}
std::optional<CPubKey> GetPubKey(int pos, const SigningProvider& arg, FlatSigningProvider& out, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) const override
Expand All @@ -314,20 +322,21 @@ class ConstPubkeyProvider final : public PubkeyProvider
std::string ToString(StringType type) const override { return m_xonly ? HexStr(m_pubkey).substr(2) : HexStr(m_pubkey); }
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
{
CKey key;
if (!GetPrivKey(/*pos=*/0, arg, key)) return false;
ret = EncodeSecret(key);
std::optional<CKey> key = GetPrivKey(arg);
if (!key) return false;
ret = EncodeSecret(*key);
return true;
}
bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
{
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 m_xonly ? arg.GetKeyByXOnly(XOnlyPubKey(m_pubkey), key) :
arg.GetKey(m_pubkey.GetID(), key);
std::optional<CKey> key = GetPrivKey(arg);
if (!key) return;
out.keys.emplace(key->GetPubKey().GetID(), *key);
}
std::optional<CPubKey> GetRootPubKey() const override
{
Expand Down Expand Up @@ -545,15 +554,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<CPubKey> GetRootPubKey() const override
{
Expand Down Expand Up @@ -740,8 +748,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);
Expand Down

0 comments on commit 1bfabb4

Please sign in to comment.