diff --git a/src/wallet/external_signer_scriptpubkeyman.cpp b/src/wallet/external_signer_scriptpubkeyman.cpp index 758be582ac4..1020ef6f667 100644 --- a/src/wallet/external_signer_scriptpubkeyman.cpp +++ b/src/wallet/external_signer_scriptpubkeyman.cpp @@ -28,17 +28,16 @@ std::unique_ptr ExternalSignerScriptPubKeyMan::Lo std::unique_ptr ExternalSignerScriptPubKeyMan::CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::unique_ptr desc) { - auto spkm = std::unique_ptr(new ExternalSignerScriptPubKeyMan(storage, keypool_size)); - - LOCK(spkm->cs_desc_man); - assert(storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); - assert(storage.IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)); - int64_t creation_time = GetTime(); // Make the descriptor WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0); - spkm->m_wallet_descriptor = w_desc; + + auto spkm = std::unique_ptr(new ExternalSignerScriptPubKeyMan(storage, w_desc, keypool_size)); + + LOCK(spkm->cs_desc_man); + assert(storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); + assert(storage.IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)); // Store the descriptor if (!batch.WriteDescriptor(spkm->GetID(), spkm->m_wallet_descriptor)) { diff --git a/src/wallet/external_signer_scriptpubkeyman.h b/src/wallet/external_signer_scriptpubkeyman.h index 8a3ae3df7a2..ea17ca1a063 100644 --- a/src/wallet/external_signer_scriptpubkeyman.h +++ b/src/wallet/external_signer_scriptpubkeyman.h @@ -16,14 +16,7 @@ namespace wallet { class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan { private: - //! Create an ExternalSPKM from existing wallet data - ExternalSignerScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys) - : DescriptorScriptPubKeyMan(storage, descriptor, keypool_size, keys, ckeys) - {} - - ExternalSignerScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size) - : DescriptorScriptPubKeyMan(storage, keypool_size) - {} + using DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan; public: static std::unique_ptr LoadFromStorage(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys); diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 30742753602..a7d7bc68cad 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -866,8 +866,30 @@ std::unique_ptr DescriptorScriptPubKeyMan::LoadFromSt std::unique_ptr DescriptorScriptPubKeyMan::GenerateNewSingleSig(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal) { - auto spkm = std::unique_ptr(new DescriptorScriptPubKeyMan(storage, keypool_size)); - spkm->SetupDescriptorGeneration(batch, master_key, addr_type, internal); + WalletDescriptor desc = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal); + + auto spkm = std::unique_ptr(new DescriptorScriptPubKeyMan(storage, desc, keypool_size)); + + LOCK(spkm->cs_desc_man); + Assert(spkm->m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); + + // Store the master private key, and descriptor + if (!spkm->AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) { + throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed"); + } + if (!batch.WriteDescriptor(spkm->GetID(), spkm->m_wallet_descriptor)) { + throw std::runtime_error(std::string(__func__) + ": writing descriptor failed"); + } + + // Set m_decryption_thoroughly_checked for encrypted wallets + if (spkm->m_storage.HasEncryptionKeys()) { + spkm->m_decryption_thoroughly_checked = true; + } + + // TopUp + spkm->TopUpWithDB(batch); + + spkm->m_storage.UnsetBlankWalletFlag(batch); return spkm; } @@ -1214,33 +1236,6 @@ bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch& batch, const } } -void DescriptorScriptPubKeyMan::SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal) -{ - LOCK(cs_desc_man); - Assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); - Assert(!m_wallet_descriptor.descriptor); - - m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal); - - // Store the master private key, and descriptor - if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) { - throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed"); - } - if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) { - throw std::runtime_error(std::string(__func__) + ": writing descriptor failed"); - } - - // Set m_decryption_thoroughly_checked for encrypted wallets - if (m_storage.HasEncryptionKeys()) { - m_decryption_thoroughly_checked = true; - } - - // TopUp - TopUpWithDB(batch); - - m_storage.UnsetBlankWalletFlag(batch); -} - bool DescriptorScriptPubKeyMan::IsHDEnabled() const { LOCK(cs_desc_man); diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index 5c977b12d40..0c3dcc6c4d5 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -301,13 +301,6 @@ private: */ mutable std::map m_musig2_secnonces; - //! Create a new DescriptorScriptPubKeyMan from an existing descriptor (i.e. from an import) - DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size) - : ScriptPubKeyMan(storage), - m_keypool_size(keypool_size), - m_wallet_descriptor(descriptor) - {} - bool AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); @@ -324,16 +317,15 @@ private: void AddDescriptorKey(const CKey& key, const CPubKey &pubkey); void UpdateWithSigningProvider(WalletBatch& batch, const FlatSigningProvider& signing_provider) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man); - //! Setup descriptors based on the given CExtKey - void SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal); - protected: //! Create a DescriptorScriptPubKeyMan from existing data (i.e. during loading) DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys); - DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size) + //! Create a new DescriptorScriptPubKeyMan from a descriptor (e.g. from an import, newly generated) + DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size) : ScriptPubKeyMan(storage), - m_keypool_size(keypool_size) + m_keypool_size(keypool_size), + m_wallet_descriptor(descriptor) {} WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);