desc spkm: Add functions to retrieve specific private keys

This commit is contained in:
Ava Chow 2023-12-21 17:27:51 -05:00
parent fe67841464
commit fa6a259985
2 changed files with 34 additions and 0 deletions

View File

@ -11,6 +11,7 @@
#include <script/sign.h>
#include <script/solver.h>
#include <util/bip32.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/time.h>
@ -2143,6 +2144,36 @@ std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const
return m_map_keys;
}
bool DescriptorScriptPubKeyMan::HasPrivKey(const CKeyID& keyid) const
{
AssertLockHeld(cs_desc_man);
return m_map_keys.contains(keyid) || m_map_crypted_keys.contains(keyid);
}
std::optional<CKey> DescriptorScriptPubKeyMan::GetKey(const CKeyID& keyid) const
{
AssertLockHeld(cs_desc_man);
if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
const auto& it = m_map_crypted_keys.find(keyid);
if (it == m_map_crypted_keys.end()) {
return std::nullopt;
}
const std::vector<unsigned char>& crypted_secret = it->second.second;
CKey key;
if (!Assume(m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
return DecryptKey(encryption_key, crypted_secret, it->second.first, key);
}))) {
return std::nullopt;
}
return key;
}
const auto& it = m_map_keys.find(keyid);
if (it == m_map_keys.end()) {
return std::nullopt;
}
return it->second;
}
bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
{
WalletBatch batch(m_storage.GetDatabase());

View File

@ -633,6 +633,9 @@ public:
bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);
bool HavePrivateKeys() const override;
bool HasPrivKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
//! Retrieve the particular key if it is available. Returns nullopt if the key is not in the wallet, or if the wallet is locked.
std::optional<CKey> GetKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
std::optional<int64_t> GetOldestKeyPoolTime() const override;
unsigned int GetKeyPoolSize() const override;