refactor: single method to append new spkm to the wallet

This commit is contained in:
furszy 2023-04-18 11:19:53 -03:00
parent a13f3746dc
commit a082434d12
No known key found for this signature in database
GPG key ID: 5DD23CCC686AA623
2 changed files with 19 additions and 7 deletions

View file

@ -3498,6 +3498,11 @@ LegacyScriptPubKeyMan* CWallet::GetOrCreateLegacyScriptPubKeyMan()
return GetLegacyScriptPubKeyMan(); return GetLegacyScriptPubKeyMan();
} }
void CWallet::AddScriptPubKeyMan(const uint256& id, std::unique_ptr<ScriptPubKeyMan> spkm_man)
{
m_spk_managers[id] = std::move(spkm_man);
}
void CWallet::SetupLegacyScriptPubKeyMan() void CWallet::SetupLegacyScriptPubKeyMan()
{ {
if (!m_internal_spk_managers.empty() || !m_external_spk_managers.empty() || !m_spk_managers.empty() || IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) { if (!m_internal_spk_managers.empty() || !m_external_spk_managers.empty() || !m_spk_managers.empty() || IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
@ -3509,7 +3514,8 @@ void CWallet::SetupLegacyScriptPubKeyMan()
m_internal_spk_managers[type] = spk_manager.get(); m_internal_spk_managers[type] = spk_manager.get();
m_external_spk_managers[type] = spk_manager.get(); m_external_spk_managers[type] = spk_manager.get();
} }
m_spk_managers[spk_manager->GetID()] = std::move(spk_manager); uint256 id = spk_manager->GetID();
AddScriptPubKeyMan(id, std::move(spk_manager));
} }
const CKeyingMaterial& CWallet::GetEncryptionKey() const const CKeyingMaterial& CWallet::GetEncryptionKey() const
@ -3534,10 +3540,10 @@ void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
{ {
if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) { if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size)); auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size));
m_spk_managers[id] = std::move(spk_manager); AddScriptPubKeyMan(id, std::move(spk_manager));
} else { } else {
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size)); auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size));
m_spk_managers[id] = std::move(spk_manager); AddScriptPubKeyMan(id, std::move(spk_manager));
} }
} }
@ -3558,7 +3564,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
} }
spk_manager->SetupDescriptorGeneration(master_key, t, internal); spk_manager->SetupDescriptorGeneration(master_key, t, internal);
uint256 id = spk_manager->GetID(); uint256 id = spk_manager->GetID();
m_spk_managers[id] = std::move(spk_manager); AddScriptPubKeyMan(id, std::move(spk_manager));
AddActiveScriptPubKeyMan(id, t, internal); AddActiveScriptPubKeyMan(id, t, internal);
} }
} }
@ -3606,7 +3612,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, m_keypool_size)); auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, m_keypool_size));
spk_manager->SetupDescriptor(std::move(desc)); spk_manager->SetupDescriptor(std::move(desc));
uint256 id = spk_manager->GetID(); uint256 id = spk_manager->GetID();
m_spk_managers[id] = std::move(spk_manager); AddScriptPubKeyMan(id, std::move(spk_manager));
AddActiveScriptPubKeyMan(id, t, internal); AddActiveScriptPubKeyMan(id, t, internal);
} }
} }
@ -3723,7 +3729,8 @@ ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const Flat
spk_man = new_spk_man.get(); spk_man = new_spk_man.get();
// Save the descriptor to memory // Save the descriptor to memory
m_spk_managers[new_spk_man->GetID()] = std::move(new_spk_man); uint256 id = new_spk_man->GetID();
AddScriptPubKeyMan(id, std::move(new_spk_man));
} }
// Add the private keys to the descriptor // Add the private keys to the descriptor
@ -3866,7 +3873,8 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted."); error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted.");
return false; return false;
} }
m_spk_managers[desc_spkm->GetID()] = std::move(desc_spkm); uint256 id = desc_spkm->GetID();
AddScriptPubKeyMan(id, std::move(desc_spkm));
} }
// Remove the LegacyScriptPubKeyMan from disk // Remove the LegacyScriptPubKeyMan from disk

View file

@ -380,6 +380,10 @@ private:
// ScriptPubKeyMan::GetID. In many cases it will be the hash of an internal structure // ScriptPubKeyMan::GetID. In many cases it will be the hash of an internal structure
std::map<uint256, std::unique_ptr<ScriptPubKeyMan>> m_spk_managers; std::map<uint256, std::unique_ptr<ScriptPubKeyMan>> m_spk_managers;
// Appends spk managers into the main 'm_spk_managers'.
// Must be the only method adding data to it.
void AddScriptPubKeyMan(const uint256& id, std::unique_ptr<ScriptPubKeyMan> spkm_man);
/** /**
* Catch wallet up to current chain, scanning new blocks, updating the best * Catch wallet up to current chain, scanning new blocks, updating the best
* block locator and m_last_block_processed, and registering for * block locator and m_last_block_processed, and registering for