Skip to content

Commit a082434

Browse files
committed
refactor: single method to append new spkm to the wallet
1 parent a13f374 commit a082434

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/wallet/wallet.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,11 @@ LegacyScriptPubKeyMan* CWallet::GetOrCreateLegacyScriptPubKeyMan()
34983498
return GetLegacyScriptPubKeyMan();
34993499
}
35003500

3501+
void CWallet::AddScriptPubKeyMan(const uint256& id, std::unique_ptr<ScriptPubKeyMan> spkm_man)
3502+
{
3503+
m_spk_managers[id] = std::move(spkm_man);
3504+
}
3505+
35013506
void CWallet::SetupLegacyScriptPubKeyMan()
35023507
{
35033508
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()
35093514
m_internal_spk_managers[type] = spk_manager.get();
35103515
m_external_spk_managers[type] = spk_manager.get();
35113516
}
3512-
m_spk_managers[spk_manager->GetID()] = std::move(spk_manager);
3517+
uint256 id = spk_manager->GetID();
3518+
AddScriptPubKeyMan(id, std::move(spk_manager));
35133519
}
35143520

35153521
const CKeyingMaterial& CWallet::GetEncryptionKey() const
@@ -3534,10 +3540,10 @@ void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
35343540
{
35353541
if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
35363542
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size));
3537-
m_spk_managers[id] = std::move(spk_manager);
3543+
AddScriptPubKeyMan(id, std::move(spk_manager));
35383544
} else {
35393545
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size));
3540-
m_spk_managers[id] = std::move(spk_manager);
3546+
AddScriptPubKeyMan(id, std::move(spk_manager));
35413547
}
35423548
}
35433549

@@ -3558,7 +3564,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
35583564
}
35593565
spk_manager->SetupDescriptorGeneration(master_key, t, internal);
35603566
uint256 id = spk_manager->GetID();
3561-
m_spk_managers[id] = std::move(spk_manager);
3567+
AddScriptPubKeyMan(id, std::move(spk_manager));
35623568
AddActiveScriptPubKeyMan(id, t, internal);
35633569
}
35643570
}
@@ -3606,7 +3612,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
36063612
auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, m_keypool_size));
36073613
spk_manager->SetupDescriptor(std::move(desc));
36083614
uint256 id = spk_manager->GetID();
3609-
m_spk_managers[id] = std::move(spk_manager);
3615+
AddScriptPubKeyMan(id, std::move(spk_manager));
36103616
AddActiveScriptPubKeyMan(id, t, internal);
36113617
}
36123618
}
@@ -3723,7 +3729,8 @@ ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const Flat
37233729
spk_man = new_spk_man.get();
37243730

37253731
// Save the descriptor to memory
3726-
m_spk_managers[new_spk_man->GetID()] = std::move(new_spk_man);
3732+
uint256 id = new_spk_man->GetID();
3733+
AddScriptPubKeyMan(id, std::move(new_spk_man));
37273734
}
37283735

37293736
// Add the private keys to the descriptor
@@ -3866,7 +3873,8 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
38663873
error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted.");
38673874
return false;
38683875
}
3869-
m_spk_managers[desc_spkm->GetID()] = std::move(desc_spkm);
3876+
uint256 id = desc_spkm->GetID();
3877+
AddScriptPubKeyMan(id, std::move(desc_spkm));
38703878
}
38713879

38723880
// Remove the LegacyScriptPubKeyMan from disk

src/wallet/wallet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
380380
// ScriptPubKeyMan::GetID. In many cases it will be the hash of an internal structure
381381
std::map<uint256, std::unique_ptr<ScriptPubKeyMan>> m_spk_managers;
382382

383+
// Appends spk managers into the main 'm_spk_managers'.
384+
// Must be the only method adding data to it.
385+
void AddScriptPubKeyMan(const uint256& id, std::unique_ptr<ScriptPubKeyMan> spkm_man);
386+
383387
/**
384388
* Catch wallet up to current chain, scanning new blocks, updating the best
385389
* block locator and m_last_block_processed, and registering for

0 commit comments

Comments
 (0)