Skip to content

Commit fa6a259

Browse files
committed
desc spkm: Add functions to retrieve specific private keys
1 parent fe67841 commit fa6a259

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <script/sign.h>
1212
#include <script/solver.h>
1313
#include <util/bip32.h>
14+
#include <util/check.h>
1415
#include <util/strencodings.h>
1516
#include <util/string.h>
1617
#include <util/time.h>
@@ -2143,6 +2144,36 @@ std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const
21432144
return m_map_keys;
21442145
}
21452146

2147+
bool DescriptorScriptPubKeyMan::HasPrivKey(const CKeyID& keyid) const
2148+
{
2149+
AssertLockHeld(cs_desc_man);
2150+
return m_map_keys.contains(keyid) || m_map_crypted_keys.contains(keyid);
2151+
}
2152+
2153+
std::optional<CKey> DescriptorScriptPubKeyMan::GetKey(const CKeyID& keyid) const
2154+
{
2155+
AssertLockHeld(cs_desc_man);
2156+
if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
2157+
const auto& it = m_map_crypted_keys.find(keyid);
2158+
if (it == m_map_crypted_keys.end()) {
2159+
return std::nullopt;
2160+
}
2161+
const std::vector<unsigned char>& crypted_secret = it->second.second;
2162+
CKey key;
2163+
if (!Assume(m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
2164+
return DecryptKey(encryption_key, crypted_secret, it->second.first, key);
2165+
}))) {
2166+
return std::nullopt;
2167+
}
2168+
return key;
2169+
}
2170+
const auto& it = m_map_keys.find(keyid);
2171+
if (it == m_map_keys.end()) {
2172+
return std::nullopt;
2173+
}
2174+
return it->second;
2175+
}
2176+
21462177
bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
21472178
{
21482179
WalletBatch batch(m_storage.GetDatabase());

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,9 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
633633
bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);
634634

635635
bool HavePrivateKeys() const override;
636+
bool HasPrivKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
637+
//! Retrieve the particular key if it is available. Returns nullopt if the key is not in the wallet, or if the wallet is locked.
638+
std::optional<CKey> GetKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
636639

637640
std::optional<int64_t> GetOldestKeyPoolTime() const override;
638641
unsigned int GetKeyPoolSize() const override;

0 commit comments

Comments
 (0)