Skip to content

Commit 0eac708

Browse files
committed
Refactor: Move SetupGeneration code out of CWallet
This commit does not change behavior.
1 parent f45d12b commit 0eac708

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,19 @@ void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
339339
}
340340
}
341341

342+
bool LegacyScriptPubKeyMan::SetupGeneration(bool force)
343+
{
344+
if ((CanGenerateKeys() && !force) || m_storage.IsLocked()) {
345+
return false;
346+
}
347+
348+
SetHDSeed(GenerateNewSeed());
349+
if (!NewKeyPool()) {
350+
return false;
351+
}
352+
return true;
353+
}
354+
342355
bool LegacyScriptPubKeyMan::IsHDEnabled() const
343356
{
344357
return !hdChain.seed_id.IsNull();

src/wallet/scriptpubkeyman.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ class ScriptPubKeyMan
159159
//! Mark unused addresses as being used
160160
virtual void MarkUnusedAddresses(const CScript& script) {}
161161

162+
/** Sets up the key generation stuff, i.e. generates new HD seeds and sets them as active.
163+
* Returns false if already setup or setup fails, true if setup is successful
164+
* Set force=true to make it re-setup if already setup, used for upgrades
165+
*/
166+
virtual bool SetupGeneration(bool force = false) { return false; }
167+
162168
/* Returns true if HD is enabled */
163169
virtual bool IsHDEnabled() const { return false; }
164170

@@ -276,6 +282,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
276282

277283
bool IsHDEnabled() const override;
278284

285+
bool SetupGeneration(bool force = false) override;
286+
279287
bool Upgrade(int prev_version, std::string& error) override;
280288

281289
bool HavePrivateKeys() const override;

src/wallet/wallet.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,14 @@ WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString&
210210
}
211211

212212
// Set a seed for the wallet
213-
CPubKey master_pub_key = wallet->m_spk_man->GenerateNewSeed();
214-
wallet->m_spk_man->SetHDSeed(master_pub_key);
215-
wallet->m_spk_man->NewKeyPool();
213+
{
214+
if (auto spk_man = wallet->m_spk_man.get()) {
215+
if (!spk_man->SetupGeneration()) {
216+
error = "Unable to generate initial keys";
217+
return WalletCreationStatus::CREATION_FAILED;
218+
}
219+
}
220+
}
216221

217222
// Relock the wallet
218223
wallet->Lock();
@@ -565,11 +570,11 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
565570
Unlock(strWalletPassphrase);
566571

567572
// if we are using HD, replace the HD seed with a new one
568-
if (m_spk_man->IsHDEnabled()) {
569-
m_spk_man->SetHDSeed(m_spk_man->GenerateNewSeed());
573+
if (auto spk_man = m_spk_man.get()) {
574+
if (spk_man->IsHDEnabled()) {
575+
spk_man->SetupGeneration(true);
576+
}
570577
}
571-
572-
m_spk_man->NewKeyPool();
573578
Lock();
574579

575580
// Need to completely rewrite the wallet file; if we don't, bdb might keep
@@ -3630,15 +3635,12 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
36303635

36313636
walletInstance->SetWalletFlags(wallet_creation_flags, false);
36323637
if (!(wallet_creation_flags & (WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET))) {
3633-
// generate a new seed
3634-
CPubKey seed = walletInstance->m_spk_man->GenerateNewSeed();
3635-
walletInstance->m_spk_man->SetHDSeed(seed);
3636-
}
3637-
3638-
// Top up the keypool
3639-
if (walletInstance->m_spk_man->CanGenerateKeys() && !walletInstance->m_spk_man->TopUp()) {
3640-
error = _("Unable to generate initial keys").translated;
3641-
return nullptr;
3638+
if (auto spk_man = walletInstance->m_spk_man.get()) {
3639+
if (!spk_man->SetupGeneration()) {
3640+
error = _("Unable to generate initial keys").translated;
3641+
return nullptr;
3642+
}
3643+
}
36423644
}
36433645

36443646
auto locked_chain = chain.lock();

0 commit comments

Comments
 (0)