Skip to content

Commit 72694b8

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. This will be necessary for descriptors such as musig() where there are private keys that need to be added to the FlatSigningProvider but do not directly appear in any resulting scripts. GetPrivKey is now changed to void as the caller no longer cares whether it succeeds or fails.
1 parent 08d22b4 commit 72694b8

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/script/descriptor.cpp

+25-19
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ struct PubkeyProvider
211211
*/
212212
virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const = 0;
213213

214-
/** Derive a private key, if private data is available in arg. */
215-
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
214+
/** Derive a private key, if private data is available in arg and put it into out. */
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
{
@@ -298,6 +298,14 @@ class ConstPubkeyProvider final : public PubkeyProvider
298298
CPubKey m_pubkey;
299299
bool m_xonly;
300300

301+
std::optional<CKey> GetPrivKey(const SigningProvider& arg) const
302+
{
303+
CKey key;
304+
if (!(m_xonly ? arg.GetKeyByXOnly(XOnlyPubKey(m_pubkey), key) :
305+
arg.GetKey(m_pubkey.GetID(), key))) return std::nullopt;
306+
return key;
307+
}
308+
301309
public:
302310
ConstPubkeyProvider(uint32_t exp_index, const CPubKey& pubkey, bool xonly) : PubkeyProvider(exp_index), m_pubkey(pubkey), m_xonly(xonly) {}
303311
std::optional<CPubKey> GetPubKey(int pos, const SigningProvider& arg, FlatSigningProvider& out, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) const override
@@ -314,20 +322,21 @@ class ConstPubkeyProvider final : public PubkeyProvider
314322
std::string ToString(StringType type) const override { return m_xonly ? HexStr(m_pubkey).substr(2) : HexStr(m_pubkey); }
315323
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
316324
{
317-
CKey key;
318-
if (!GetPrivKey(/*pos=*/0, arg, key)) return false;
319-
ret = EncodeSecret(key);
325+
std::optional<CKey> key = GetPrivKey(arg);
326+
if (!key) return false;
327+
ret = EncodeSecret(*key);
320328
return true;
321329
}
322330
bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
323331
{
324332
ret = ToString(StringType::PUBLIC);
325333
return true;
326334
}
327-
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
335+
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
328336
{
329-
return m_xonly ? arg.GetKeyByXOnly(XOnlyPubKey(m_pubkey), key) :
330-
arg.GetKey(m_pubkey.GetID(), key);
337+
std::optional<CKey> key = GetPrivKey(arg);
338+
if (!key) return;
339+
out.keys.emplace(key->GetPubKey().GetID(), *key);
331340
}
332341
std::optional<CPubKey> GetRootPubKey() const override
333342
{
@@ -545,15 +554,14 @@ class BIP32PubkeyProvider final : public PubkeyProvider
545554
}
546555
return true;
547556
}
548-
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
557+
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
549558
{
550559
CExtKey extkey;
551560
CExtKey dummy;
552-
if (!GetDerivedExtKey(arg, extkey, dummy)) return false;
553-
if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return false;
554-
if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return false;
555-
key = extkey.key;
556-
return true;
561+
if (!GetDerivedExtKey(arg, extkey, dummy)) return;
562+
if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return;
563+
if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return;
564+
out.keys.emplace(extkey.key.GetPubKey().GetID(), extkey.key);
557565
}
558566
std::optional<CPubKey> GetRootPubKey() const override
559567
{
@@ -739,9 +747,7 @@ class DescriptorImpl : public Descriptor
739747
void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
740748
{
741749
for (const auto& p : m_pubkey_args) {
742-
CKey key;
743-
if (!p->GetPrivKey(pos, provider, key)) continue;
744-
out.keys.emplace(key.GetPubKey().GetID(), key);
750+
p->GetPrivKey(pos, provider, out);
745751
}
746752
for (const auto& arg : m_subdescriptor_args) {
747753
arg->ExpandPrivate(pos, provider, out);

0 commit comments

Comments
 (0)