aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAva Chow <github@achow101.com>2023-12-21 18:39:23 -0500
committerAva Chow <github@achow101.com>2024-03-20 16:15:43 -0400
commit8e1a475062e62321e58a0624385cc3fa0885aa12 (patch)
treede0e423e73aa76323430d4ff6279470fa302d1e5 /src
parent85b1fb19dd3a3f3c68da1c5e60a6eb911e1119a6 (diff)
downloadbitcoin-8e1a475062e62321e58a0624385cc3fa0885aa12.tar.xz
wallet: Be able to retrieve single key from descriptors
Adds CWallet::GetKey which retrieves a single key from the descriptors stored in the wallet.
Diffstat (limited to 'src')
-rw-r--r--src/wallet/wallet.cpp15
-rw-r--r--src/wallet/wallet.h4
2 files changed, 19 insertions, 0 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 0dc3da1738..854600b900 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -4507,4 +4507,19 @@ std::set<CExtPubKey> CWallet::GetActiveHDPubKeys() const
}
return active_xpubs;
}
+
+std::optional<CKey> CWallet::GetKey(const CKeyID& keyid) const
+{
+ Assert(IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
+
+ for (const auto& spkm : GetAllScriptPubKeyMans()) {
+ const DescriptorScriptPubKeyMan* desc_spkm = dynamic_cast<DescriptorScriptPubKeyMan*>(spkm);
+ assert(desc_spkm);
+ LOCK(desc_spkm->cs_desc_man);
+ if (std::optional<CKey> 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 83425fca6b..f07db175b4 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<CExtPubKey> 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<CKey> GetKey(const CKeyID& keyid) const;
};
/**