From 8e1a475062e62321e58a0624385cc3fa0885aa12 Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Thu, 21 Dec 2023 18:39:23 -0500 Subject: [PATCH] wallet: Be able to retrieve single key from descriptors Adds CWallet::GetKey which retrieves a single key from the descriptors stored in the wallet. --- src/wallet/wallet.cpp | 15 +++++++++++++++ src/wallet/wallet.h | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 0dc3da17389..854600b900b 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4507,4 +4507,19 @@ std::set CWallet::GetActiveHDPubKeys() const } return active_xpubs; } + +std::optional CWallet::GetKey(const CKeyID& keyid) const +{ + Assert(IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); + + for (const auto& spkm : GetAllScriptPubKeyMans()) { + const DescriptorScriptPubKeyMan* desc_spkm = dynamic_cast(spkm); + assert(desc_spkm); + LOCK(desc_spkm->cs_desc_man); + if (std::optional key = desc_spkm->GetKey(keyid)) { + return key; + } + } + return std::nullopt; +} } // namespace wallet diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 83425fca6b5..f07db175b48 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1059,6 +1059,10 @@ public: //! Retrieve the xpubs in use by the active descriptors std::set GetActiveHDPubKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + + //! Find the private key for the given key id from the wallet's descriptors, if available + //! Returns nullopt when no descriptor has the key or if the wallet is locked. + std::optional GetKey(const CKeyID& keyid) const; }; /**