From d91026a6f31caee5688245e62d61e14ce42d9285 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Mon, 21 Aug 2023 17:15:05 -0400 Subject: [PATCH] wallet: Setup new autogenerated descriptors on construction Instead of having a caller use SetupDescriptorGeneration, just have a constructor that takes those arguments and sets up the descriptor with the autogenerated key. --- .../external_signer_scriptpubkeyman.cpp | 2 +- src/wallet/scriptpubkeyman.cpp | 7 +++++++ src/wallet/scriptpubkeyman.h | 19 +++++++++++-------- src/wallet/wallet.cpp | 3 +-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/wallet/external_signer_scriptpubkeyman.cpp b/src/wallet/external_signer_scriptpubkeyman.cpp index acae066f0f9ea..8944650b0ee2a 100644 --- a/src/wallet/external_signer_scriptpubkeyman.cpp +++ b/src/wallet/external_signer_scriptpubkeyman.cpp @@ -17,7 +17,7 @@ namespace wallet { ExternalSignerScriptPubKeyMan::ExternalSignerScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size, std::unique_ptr desc) - : DescriptorScriptPubKeyMan(storage, keypool_size) + : DescriptorScriptPubKeyMan(storage, keypool_size) { LOCK(cs_desc_man); assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 6d6f43dec34a5..0b769568e2c60 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -2001,6 +2001,13 @@ DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, con SetCache(m_wallet_descriptor.cache); } +DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal) + : ScriptPubKeyMan(storage), + m_keypool_size(keypool_size) +{ + SetupDescriptorGeneration(master_key, addr_type, internal); +} + util::Result DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type) { // Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index c822143f13108..bf374fc2a0405 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -584,18 +584,24 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan void AddDescriptorKey(const CKey& key, const CPubKey &pubkey); void UpdateWithSigningProvider(const FlatSigningProvider& signing_provider); + //! Setup descriptors based on the given CExtkey + bool SetupDescriptorGeneration(const CExtKey& master_key, OutputType addr_type, bool internal); + protected: - WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man); + WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man); + + DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size) + : ScriptPubKeyMan(storage), + m_keypool_size(keypool_size) + {} public: //! Create a new DescriptorScriptPubKeyMan from an existing descriptor (i.e. from an import) DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider); //! Create a DescriptorScriptPubKeyMan from existing data (i.e. during loading) DescriptorScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys); - DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size) - : ScriptPubKeyMan(storage), - m_keypool_size(keypool_size) - {} + //! Create an automatically generated DescriptorScriptPubKeyMan + DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal); mutable RecursiveMutex cs_desc_man; @@ -618,9 +624,6 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan bool IsHDEnabled() const override; - //! Setup descriptors based on the given CExtkey - bool SetupDescriptorGeneration(const CExtKey& master_key, OutputType addr_type, bool internal); - bool HavePrivateKeys() const override; std::optional GetOldestKeyPoolTime() const override; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 741b156248e7d..487e8fbbeafe6 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3560,7 +3560,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key) for (bool internal : {false, true}) { for (OutputType t : OUTPUT_TYPES) { - auto spk_manager = std::unique_ptr(new DescriptorScriptPubKeyMan(*this, m_keypool_size)); + auto spk_manager = std::unique_ptr(new DescriptorScriptPubKeyMan(*this, m_keypool_size, master_key, t, internal)); if (IsCrypted()) { if (IsLocked()) { throw std::runtime_error(std::string(__func__) + ": Wallet is locked, cannot setup new descriptors"); @@ -3569,7 +3569,6 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key) throw std::runtime_error(std::string(__func__) + ": Could not encrypt new descriptors"); } } - spk_manager->SetupDescriptorGeneration(master_key, t, internal); uint256 id = spk_manager->GetID(); AddScriptPubKeyMan(id, std::move(spk_manager)); AddActiveScriptPubKeyMan(id, t, internal);