From cd912c4e10848baf7fc1d661b533cf2fb806d696 Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Tue, 20 Feb 2024 11:48:33 -0500 Subject: [PATCH] wallet: Consolidate generation setup callers into one function --- src/wallet/wallet.cpp | 50 +++++++++++++++++++++---------------------- src/wallet/wallet.h | 3 +++ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 3724c3f2b3d..ade5b48fa51 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -429,30 +429,18 @@ std::shared_ptr CreateWallet(WalletContext& context, const std::string& return nullptr; } + // Unset the blank flag if not specified by the user + if (!create_blank) { + wallet->UnsetWalletFlag(WALLET_FLAG_BLANK_WALLET); + } + // Encrypt the wallet - if (!passphrase.empty() && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { + if (!passphrase.empty()) { if (!wallet->EncryptWallet(passphrase)) { error = Untranslated("Error: Wallet created but failed to encrypt."); status = DatabaseStatus::FAILED_ENCRYPT; return nullptr; } - if (!create_blank) { - // Unlock the wallet - if (!wallet->Unlock(passphrase)) { - error = Untranslated("Error: Wallet was encrypted but could not be unlocked"); - status = DatabaseStatus::FAILED_ENCRYPT; - return nullptr; - } - - // Set a seed for the wallet - { - LOCK(wallet->cs_wallet); - wallet->SetupDescriptorScriptPubKeyMans(); - } - - // Relock the wallet - wallet->Lock(); - } } WITH_LOCK(wallet->cs_wallet, wallet->LogStats()); @@ -873,12 +861,12 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) encrypted_batch = nullptr; Lock(); - Unlock(strWalletPassphrase); - - // Make new descriptors with a new seed - if (!IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET)) { - SetupDescriptorScriptPubKeyMans(); + if (!Unlock(strWalletPassphrase)) { + return false; } + + SetupWalletGeneration(); + Lock(); // Need to completely rewrite the wallet file; if we don't, the database might keep @@ -3101,9 +3089,7 @@ std::shared_ptr CWallet::CreateNew(WalletContext& context, const std::s // Only descriptor wallets can be created assert(walletInstance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)); - if ((wallet_creation_flags & WALLET_FLAG_EXTERNAL_SIGNER) || !(wallet_creation_flags & (WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET))) { - walletInstance->SetupDescriptorScriptPubKeyMans(); - } + walletInstance->SetupWalletGeneration(); if (chain) { std::optional tip_height = chain->getHeight(); @@ -3670,6 +3656,18 @@ void CWallet::SetupDescriptorScriptPubKeyMans() } } +void CWallet::SetupWalletGeneration() +{ + AssertLockHeld(cs_wallet); + // Skip setup for non-external-signer wallets that are either blank + // or have private keys disabled (not having private keys implies blank). + if (!IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER) && + (IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET) || IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS))) { + return; + } + SetupDescriptorScriptPubKeyMans(); +} + void CWallet::AddActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal) { WalletBatch batch(GetDatabase()); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 81d40ec8938..9a1b4fbc5b0 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1033,6 +1033,9 @@ public: //! Create new seed and default DescriptorScriptPubKeyMans for this wallet void SetupOwnDescriptorScriptPubKeyMans(WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + //! Setup new descriptors or seed for new address generation + void SetupWalletGeneration() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + //! Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet DescriptorScriptPubKeyMan* GetDescriptorScriptPubKeyMan(const WalletDescriptor& desc) const;