wallet: Be able to retrieve single key from descriptors

Adds CWallet::GetKey which retrieves a single key from the descriptors
stored in the wallet.
This commit is contained in:
Ava Chow 2023-12-21 18:39:23 -05:00
parent 85b1fb19dd
commit 8e1a475062
2 changed files with 19 additions and 0 deletions

View File

@ -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

View File

@ -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;
};
/**