Skip to content

Commit fe67841

Browse files
committed
descriptor: Be able to get the pubkeys involved in a descriptor
1 parent ef67458 commit fe67841

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/script/descriptor.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ struct PubkeyProvider
212212

213213
/** Derive a private key, if private data is available in arg. */
214214
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
215+
216+
/** Return the non-extended public key for this PubkeyProvider, if it has one. */
217+
virtual std::optional<CPubKey> GetRootPubKey() const = 0;
218+
/** Return the extended public key for this PubkeyProvider, if it has one. */
219+
virtual std::optional<CExtPubKey> GetRootExtPubKey() const = 0;
215220
};
216221

217222
class OriginPubkeyProvider final : public PubkeyProvider
@@ -265,6 +270,14 @@ class OriginPubkeyProvider final : public PubkeyProvider
265270
{
266271
return m_provider->GetPrivKey(pos, arg, key);
267272
}
273+
std::optional<CPubKey> GetRootPubKey() const override
274+
{
275+
return m_provider->GetRootPubKey();
276+
}
277+
std::optional<CExtPubKey> GetRootExtPubKey() const override
278+
{
279+
return m_provider->GetRootExtPubKey();
280+
}
268281
};
269282

270283
/** An object representing a parsed constant public key in a descriptor. */
@@ -310,6 +323,14 @@ class ConstPubkeyProvider final : public PubkeyProvider
310323
{
311324
return arg.GetKey(m_pubkey.GetID(), key);
312325
}
326+
std::optional<CPubKey> GetRootPubKey() const override
327+
{
328+
return m_pubkey;
329+
}
330+
std::optional<CExtPubKey> GetRootExtPubKey() const override
331+
{
332+
return std::nullopt;
333+
}
313334
};
314335

315336
enum class DeriveType {
@@ -525,6 +546,14 @@ class BIP32PubkeyProvider final : public PubkeyProvider
525546
key = extkey.key;
526547
return true;
527548
}
549+
std::optional<CPubKey> GetRootPubKey() const override
550+
{
551+
return std::nullopt;
552+
}
553+
std::optional<CExtPubKey> GetRootExtPubKey() const override
554+
{
555+
return m_root_extkey;
556+
}
528557
};
529558

530559
/** Base class for all Descriptor implementations. */
@@ -720,6 +749,19 @@ class DescriptorImpl : public Descriptor
720749
std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; }
721750

722751
std::optional<int64_t> MaxSatisfactionElems() const override { return {}; }
752+
753+
void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override
754+
{
755+
for (const auto& p : m_pubkey_args) {
756+
std::optional<CPubKey> pub = p->GetRootPubKey();
757+
if (pub) pubkeys.insert(*pub);
758+
std::optional<CExtPubKey> ext_pub = p->GetRootExtPubKey();
759+
if (ext_pub) ext_pubs.insert(*ext_pub);
760+
}
761+
for (const auto& arg : m_subdescriptor_args) {
762+
arg->GetPubKeys(pubkeys, ext_pubs);
763+
}
764+
}
723765
};
724766

725767
/** A parsed addr(A) descriptor. */

src/script/descriptor.h

+7
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ struct Descriptor {
158158

159159
/** Get the maximum size number of stack elements for satisfying this descriptor. */
160160
virtual std::optional<int64_t> MaxSatisfactionElems() const = 0;
161+
162+
/** Return all (extended) public keys for this descriptor, including any from subdescriptors.
163+
*
164+
* @param[out] pubkeys Any public keys
165+
* @param[out] ext_pubs Any extended public keys
166+
*/
167+
virtual void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const = 0;
161168
};
162169

163170
/** Parse a `descriptor` string. Included private keys are put in `out`.

src/wallet/test/walletload_tests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class DummyDescriptor final : public Descriptor {
3434
std::optional<int64_t> ScriptSize() const override { return {}; }
3535
std::optional<int64_t> MaxSatisfactionWeight(bool) const override { return {}; }
3636
std::optional<int64_t> MaxSatisfactionElems() const override { return {}; }
37+
void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const override {}
3738
};
3839

3940
BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup)

0 commit comments

Comments
 (0)