Skip to content

Commit 54e74f4

Browse files
committed
wallet: Refactor function for single DescSPKM setup
We will need access to a function that sets up a singular DescriptorSPKM, so refactor this out of the multiple DescriptorSPKM setup function.
1 parent 3b09d0e commit 54e74f4

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,6 +3628,26 @@ DescriptorScriptPubKeyMan& CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, Wa
36283628
return *spk_manager;
36293629
}
36303630

3631+
DescriptorScriptPubKeyMan& CWallet::SetupDescriptorScriptPubKeyMan(WalletBatch& batch, const CExtKey& master_key, const OutputType& output_type, bool internal)
3632+
{
3633+
AssertLockHeld(cs_wallet);
3634+
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, m_keypool_size));
3635+
if (IsCrypted()) {
3636+
if (IsLocked()) {
3637+
throw std::runtime_error(std::string(__func__) + ": Wallet is locked, cannot setup new descriptors");
3638+
}
3639+
if (!spk_manager->CheckDecryptionKey(vMasterKey) && !spk_manager->Encrypt(vMasterKey, &batch)) {
3640+
throw std::runtime_error(std::string(__func__) + ": Could not encrypt new descriptors");
3641+
}
3642+
}
3643+
spk_manager->SetupDescriptorGeneration(batch, master_key, output_type, internal);
3644+
DescriptorScriptPubKeyMan* out = spk_manager.get();
3645+
uint256 id = spk_manager->GetID();
3646+
AddScriptPubKeyMan(id, std::move(spk_manager));
3647+
AddActiveScriptPubKeyManWithDb(batch, id, output_type, internal);
3648+
return *out;
3649+
}
3650+
36313651
void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
36323652
{
36333653
AssertLockHeld(cs_wallet);
@@ -3638,19 +3658,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
36383658

36393659
for (bool internal : {false, true}) {
36403660
for (OutputType t : OUTPUT_TYPES) {
3641-
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, m_keypool_size));
3642-
if (IsCrypted()) {
3643-
if (IsLocked()) {
3644-
throw std::runtime_error(std::string(__func__) + ": Wallet is locked, cannot setup new descriptors");
3645-
}
3646-
if (!spk_manager->CheckDecryptionKey(vMasterKey) && !spk_manager->Encrypt(vMasterKey, &batch)) {
3647-
throw std::runtime_error(std::string(__func__) + ": Could not encrypt new descriptors");
3648-
}
3649-
}
3650-
spk_manager->SetupDescriptorGeneration(batch, master_key, t, internal);
3651-
uint256 id = spk_manager->GetID();
3652-
AddScriptPubKeyMan(id, std::move(spk_manager));
3653-
AddActiveScriptPubKeyManWithDb(batch, id, t, internal);
3661+
SetupDescriptorScriptPubKeyMan(batch, master_key, t, internal);
36543662
}
36553663
}
36563664

src/wallet/wallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10181018
//! @param[in] internal Whether this ScriptPubKeyMan provides change addresses
10191019
void DeactivateScriptPubKeyMan(uint256 id, OutputType type, bool internal);
10201020

1021+
//! Create new DescriptorScriptPubKeyMan and add it to the wallet
1022+
DescriptorScriptPubKeyMan& SetupDescriptorScriptPubKeyMan(WalletBatch& batch, const CExtKey& master_key, const OutputType& output_type, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10211023
//! Create new DescriptorScriptPubKeyMans and add them to the wallet
10221024
void SetupDescriptorScriptPubKeyMans(const CExtKey& master_key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10231025
void SetupDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

0 commit comments

Comments
 (0)