wallet: Consolidate generation setup callers into one function

This commit is contained in:
Ava Chow
2024-02-20 11:48:33 -05:00
parent 0301c758ea
commit cd912c4e10
2 changed files with 27 additions and 26 deletions

View File

@@ -429,30 +429,18 @@ std::shared_ptr<CWallet> 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> 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<int> 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());

View File

@@ -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;