Skip to content

Commit 5b62f09

Browse files
committed
wallet: Refactor SetupDescSPKMs to take CExtKey
Refactors SetupDescSPKMs so that the DescSPKM loops are in their own function. This allows us to call it later during migration with a key that was already generated.
1 parent 22401f1 commit 5b62f09

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/wallet/wallet.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3433,6 +3433,29 @@ void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
34333433
}
34343434
}
34353435

3436+
void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
3437+
{
3438+
AssertLockHeld(cs_wallet);
3439+
3440+
for (bool internal : {false, true}) {
3441+
for (OutputType t : OUTPUT_TYPES) {
3442+
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this));
3443+
if (IsCrypted()) {
3444+
if (IsLocked()) {
3445+
throw std::runtime_error(std::string(__func__) + ": Wallet is locked, cannot setup new descriptors");
3446+
}
3447+
if (!spk_manager->CheckDecryptionKey(vMasterKey) && !spk_manager->Encrypt(vMasterKey, nullptr)) {
3448+
throw std::runtime_error(std::string(__func__) + ": Could not encrypt new descriptors");
3449+
}
3450+
}
3451+
spk_manager->SetupDescriptorGeneration(master_key, t, internal);
3452+
uint256 id = spk_manager->GetID();
3453+
m_spk_managers[id] = std::move(spk_manager);
3454+
AddActiveScriptPubKeyMan(id, t, internal);
3455+
}
3456+
}
3457+
}
3458+
34363459
void CWallet::SetupDescriptorScriptPubKeyMans()
34373460
{
34383461
AssertLockHeld(cs_wallet);
@@ -3448,23 +3471,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
34483471
CExtKey master_key;
34493472
master_key.SetSeed(seed_key);
34503473

3451-
for (bool internal : {false, true}) {
3452-
for (OutputType t : OUTPUT_TYPES) {
3453-
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this));
3454-
if (IsCrypted()) {
3455-
if (IsLocked()) {
3456-
throw std::runtime_error(std::string(__func__) + ": Wallet is locked, cannot setup new descriptors");
3457-
}
3458-
if (!spk_manager->CheckDecryptionKey(vMasterKey) && !spk_manager->Encrypt(vMasterKey, nullptr)) {
3459-
throw std::runtime_error(std::string(__func__) + ": Could not encrypt new descriptors");
3460-
}
3461-
}
3462-
spk_manager->SetupDescriptorGeneration(master_key, t, internal);
3463-
uint256 id = spk_manager->GetID();
3464-
m_spk_managers[id] = std::move(spk_manager);
3465-
AddActiveScriptPubKeyMan(id, t, internal);
3466-
}
3467-
}
3474+
SetupDescriptorScriptPubKeyMans(master_key);
34683475
} else {
34693476
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
34703477

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
907907
void DeactivateScriptPubKeyMan(uint256 id, OutputType type, bool internal);
908908

909909
//! Create new DescriptorScriptPubKeyMans and add them to the wallet
910+
void SetupDescriptorScriptPubKeyMans(const CExtKey& master_key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
910911
void SetupDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
911912

912913
//! Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet

0 commit comments

Comments
 (0)