From 813a16a463326079472366aed42f5218a57db63b Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Tue, 24 Oct 2023 17:44:46 -0400 Subject: [PATCH] wallet: Add HasCryptedKeys --- src/wallet/scriptpubkeyman.cpp | 12 ++++++++++++ src/wallet/scriptpubkeyman.h | 3 +++ src/wallet/wallet.cpp | 8 ++++++++ src/wallet/wallet.h | 1 + 4 files changed, 24 insertions(+) diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 46ec5dc1acc..a37981ff658 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -525,6 +525,12 @@ bool LegacyScriptPubKeyMan::HavePrivateKeys() const return !mapKeys.empty() || !mapCryptedKeys.empty(); } +bool LegacyScriptPubKeyMan::HaveCryptedKeys() const +{ + LOCK(cs_KeyStore); + return !mapCryptedKeys.empty(); +} + void LegacyScriptPubKeyMan::RewriteDB() { LOCK(cs_KeyStore); @@ -2392,6 +2398,12 @@ bool DescriptorScriptPubKeyMan::HavePrivateKeys() const return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0; } +bool DescriptorScriptPubKeyMan::HaveCryptedKeys() const +{ + LOCK(cs_desc_man); + return !m_map_crypted_keys.empty(); +} + std::optional DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() const { // This is only used for getwalletinfo output and isn't relevant to descriptor wallets. diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index cf7b7eaf31d..e0601785b03 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -221,6 +221,7 @@ public: virtual bool Upgrade(int prev_version, int new_version, bilingual_str& error) { return true; } virtual bool HavePrivateKeys() const { return false; } + virtual bool HaveCryptedKeys() const { return false; } //! The action to do when the DB needs rewrite virtual void RewriteDB() {} @@ -472,6 +473,7 @@ public: bool Upgrade(int prev_version, int new_version, bilingual_str& error) override; bool HavePrivateKeys() const override; + bool HaveCryptedKeys() const override; void RewriteDB() override; @@ -659,6 +661,7 @@ public: 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 GetKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); + bool HaveCryptedKeys() const override; std::optional GetOldestKeyPoolTime() const override; unsigned int GetKeyPoolSize() const override; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index de565102cc2..bef42cbdc22 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3695,6 +3695,14 @@ bool CWallet::HasEncryptionKeys() const return !mapMasterKeys.empty(); } +bool CWallet::HaveCryptedKeys() const +{ + for (const auto& spkm : GetAllScriptPubKeyMans()) { + if (spkm->HaveCryptedKeys()) return true; + } + return false; +} + void CWallet::ConnectScriptPubKeyManNotifiers() { for (const auto& spk_man : GetActiveScriptPubKeyMans()) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index d3a7208b15e..88e4778660b 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -969,6 +969,7 @@ public: bool WithEncryptionKey(std::function cb) const override; bool HasEncryptionKeys() const override; + bool HaveCryptedKeys() const; /** Get last block processed height */ int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)