Refactor: Move SetupGeneration code out of CWallet

This commit does not change behavior.
This commit is contained in:
Andrew Chow committed 2019-11-01 22:58:05 -04:00
1 parent f45d12b36c
commit 0eac7088ab
3 files changed
+39 -16

No files matched your search

+13
View File
@@ -339,6 +339,19 @@ void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
} }
} }
bool LegacyScriptPubKeyMan::SetupGeneration(bool force)
{
if ((CanGenerateKeys() && !force) || m_storage.IsLocked()) {
return false;
}
SetHDSeed(GenerateNewSeed());
if (!NewKeyPool()) {
return false;
}
return true;
}
bool LegacyScriptPubKeyMan::IsHDEnabled() const bool LegacyScriptPubKeyMan::IsHDEnabled() const
{ {
return !hdChain.seed_id.IsNull(); return !hdChain.seed_id.IsNull();
+8
View File
@@ -159,6 +159,12 @@ public:
//! Mark unused addresses as being used //! Mark unused addresses as being used
virtual void MarkUnusedAddresses(const CScript& script) {} virtual void MarkUnusedAddresses(const CScript& script) {}
/** Sets up the key generation stuff, i.e. generates new HD seeds and sets them as active.
* Returns false if already setup or setup fails, true if setup is successful
* Set force=true to make it re-setup if already setup, used for upgrades
*/
virtual bool SetupGeneration(bool force = false) { return false; }
/* Returns true if HD is enabled */ /* Returns true if HD is enabled */
virtual bool IsHDEnabled() const { return false; } virtual bool IsHDEnabled() const { return false; }
@@ -276,6 +282,8 @@ public:
bool IsHDEnabled() const override; bool IsHDEnabled() const override;
bool SetupGeneration(bool force = false) override;
bool Upgrade(int prev_version, std::string& error) override; bool Upgrade(int prev_version, std::string& error) override;
bool HavePrivateKeys() const override; bool HavePrivateKeys() const override;
+18 -16
View File
@@ -210,9 +210,14 @@ WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString&
} }
// Set a seed for the wallet // Set a seed for the wallet
CPubKey master_pub_key = wallet->m_spk_man->GenerateNewSeed(); {
wallet->m_spk_man->SetHDSeed(master_pub_key); if (auto spk_man = wallet->m_spk_man.get()) {
wallet->m_spk_man->NewKeyPool(); if (!spk_man->SetupGeneration()) {
error = "Unable to generate initial keys";
return WalletCreationStatus::CREATION_FAILED;
}
}
}
// Relock the wallet // Relock the wallet
wallet->Lock(); wallet->Lock();
@@ -565,11 +570,11 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
Unlock(strWalletPassphrase); Unlock(strWalletPassphrase);
// if we are using HD, replace the HD seed with a new one // if we are using HD, replace the HD seed with a new one
if (m_spk_man->IsHDEnabled()) { if (auto spk_man = m_spk_man.get()) {
m_spk_man->SetHDSeed(m_spk_man->GenerateNewSeed()); if (spk_man->IsHDEnabled()) {
spk_man->SetupGeneration(true);
}
} }
m_spk_man->NewKeyPool();
Lock(); Lock();
// Need to completely rewrite the wallet file; if we don't, bdb might keep // 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,
walletInstance->SetWalletFlags(wallet_creation_flags, false); walletInstance->SetWalletFlags(wallet_creation_flags, false);
if (!(wallet_creation_flags & (WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET))) { if (!(wallet_creation_flags & (WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET))) {
// generate a new seed if (auto spk_man = walletInstance->m_spk_man.get()) {
CPubKey seed = walletInstance->m_spk_man->GenerateNewSeed(); if (!spk_man->SetupGeneration()) {
walletInstance->m_spk_man->SetHDSeed(seed); error = _("Unable to generate initial keys").translated;
} return nullptr;
}
// Top up the keypool }
if (walletInstance->m_spk_man->CanGenerateKeys() && !walletInstance->m_spk_man->TopUp()) {
error = _("Unable to generate initial keys").translated;
return nullptr;
} }
auto locked_chain = chain.lock(); auto locked_chain = chain.lock();