Skip to content

Commit 6b91f7d

Browse files
committed
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.
1 parent 151fd9d commit 6b91f7d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/script/descriptor.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ struct PubkeyProvider
212212
virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const = 0;
213213

214214
/** Derive a private key, if private data is available in arg. */
215-
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
215+
virtual void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const = 0;
216216

217217
/** Return the non-extended public key for this PubkeyProvider, if it has one. */
218218
virtual std::optional<CPubKey> GetRootPubKey() const = 0;
@@ -274,9 +274,9 @@ class OriginPubkeyProvider final : public PubkeyProvider
274274
}
275275
return true;
276276
}
277-
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
277+
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
278278
{
279-
return m_provider->GetPrivKey(pos, arg, key);
279+
m_provider->GetPrivKey(pos, arg, out);
280280
}
281281
std::optional<CPubKey> GetRootPubKey() const override
282282
{
@@ -332,9 +332,12 @@ class ConstPubkeyProvider final : public PubkeyProvider
332332
ret = ToString(StringType::PUBLIC);
333333
return true;
334334
}
335-
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
335+
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
336336
{
337-
return arg.GetKey(m_pubkey.GetID(), key);
337+
CKeyID id = m_pubkey.GetID();
338+
CKey key;
339+
if (!arg.GetKey(id, key)) return;
340+
out.keys.emplace(id, key);
338341
}
339342
std::optional<CPubKey> GetRootPubKey() const override
340343
{
@@ -552,15 +555,14 @@ class BIP32PubkeyProvider final : public PubkeyProvider
552555
}
553556
return true;
554557
}
555-
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
558+
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
556559
{
557560
CExtKey extkey;
558561
CExtKey dummy;
559-
if (!GetDerivedExtKey(arg, extkey, dummy)) return false;
560-
if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return false;
561-
if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return false;
562-
key = extkey.key;
563-
return true;
562+
if (!GetDerivedExtKey(arg, extkey, dummy)) return;
563+
if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return;
564+
if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return;
565+
out.keys.emplace(extkey.key.GetPubKey().GetID(), extkey.key);
564566
}
565567
std::optional<CPubKey> GetRootPubKey() const override
566568
{
@@ -747,8 +749,7 @@ class DescriptorImpl : public Descriptor
747749
{
748750
for (const auto& p : m_pubkey_args) {
749751
CKey key;
750-
if (!p->GetPrivKey(pos, provider, key)) continue;
751-
out.keys.emplace(key.GetPubKey().GetID(), key);
752+
p->GetPrivKey(pos, provider, out);
752753
}
753754
for (const auto& arg : m_subdescriptor_args) {
754755
arg->ExpandPrivate(pos, provider, out);

0 commit comments

Comments
 (0)