wallet: add GetExtKey helper

Reconstruct a descriptor's extended private key from its xpub by looking
up the corresponding private key. This is the extended-key analog of
GetKey() and is used by the derivehdkey RPC in the following commit.
This commit is contained in:
Sjors Provoost
2026-06-26 16:44:48 +02:00
parent aaf1548475
commit 62da9f9614
2 changed files with 12 additions and 0 deletions

View File

@@ -4572,6 +4572,14 @@ std::optional<CKey> CWallet::GetKey(const CKeyID& keyid) const
return std::nullopt;
}
std::optional<CExtKey> CWallet::GetExtKey(const CExtPubKey& xpub) const
{
if (std::optional<CKey> key = GetKey(xpub.pubkey.GetID())) {
return CExtKey{xpub, *key};
}
return std::nullopt;
}
void CWallet::WriteBestBlock() const
{
AssertLockHeld(cs_wallet);

View File

@@ -1091,6 +1091,10 @@ public:
//! Returns nullopt when no descriptor has the key or if the wallet is locked.
std::optional<CKey> GetKey(const CKeyID& keyid) const;
//! Reconstruct the extended private key for an HD xpub. Returns nullopt when
//! no descriptor has the private key, or the wallet is locked.
std::optional<CExtKey> GetExtKey(const CExtPubKey& xpub) const;
//! Disconnect chain notifications and wait for all notifications to be processed
void DisconnectChainNotifications();
};