Skip to content

Commit 5c50e93

Browse files
committed
Allow -upgradewallet to upgradewallets to HD
Changes the maximum upgradewallet version to the latest wallet version number, 159900. Non-HD wallets will be upgraded to use HD derivation. Non HD chain split wallets will be upgraded to HD chain split. If a non-HD wallet is upgraded to HD, the keypool will be entirely regenerated. Since upgradewallet is effectively run during a first run, all of the first run initial setup stuff is combined with the upgrade to HD
1 parent 2bcf2b5 commit 5c50e93

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/wallet/wallet.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4025,14 +4025,43 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
40254025
walletInstance->SetMaxVersion(nMaxVersion);
40264026
}
40274027

4028+
// Upgrade to HD if explicit upgrade
4029+
if (gArgs.GetBoolArg("-upgradewallet", false)) {
4030+
LOCK(walletInstance->cs_wallet);
4031+
bool hd_upgrade = false;
4032+
if (walletInstance->CanSupportFeature(FEATURE_HD) && !walletInstance->IsHDEnabled()) {
4033+
LogPrintf("Upgrading wallet to HD\n");
4034+
walletInstance->SetMinVersion(FEATURE_HD);
4035+
4036+
// generate a new master key
4037+
CPubKey masterPubKey = walletInstance->GenerateNewHDMasterKey();
4038+
if (!walletInstance->SetHDMasterKey(masterPubKey)) {
4039+
throw std::runtime_error(std::string(__func__) + ": Storing master key failed");
4040+
}
4041+
hd_upgrade = true;
4042+
}
4043+
// Upgrade to HD chain split if necessary
4044+
if (walletInstance->CanSupportFeature(FEATURE_HD_SPLIT)) {
4045+
LogPrintf("Upgrading wallet to use HD chain split\n");
4046+
walletInstance->SetMinVersion(FEATURE_HD_SPLIT);
4047+
}
4048+
// Regenerate the keypool if upgraded to HD
4049+
if (hd_upgrade) {
4050+
if (!walletInstance->NewKeyPool()) {
4051+
InitError(_("Unable to generate keys") += "\n");
4052+
return nullptr;
4053+
}
4054+
}
4055+
}
4056+
40284057
if (fFirstRun)
40294058
{
40304059
// ensure this wallet.dat can only be opened by clients supporting HD with chain split and expects no default key
40314060
if (!gArgs.GetBoolArg("-usehd", true)) {
40324061
InitError(strprintf(_("Error creating %s: You can't create non-HD wallets with this version."), walletFile));
40334062
return nullptr;
40344063
}
4035-
walletInstance->SetMinVersion(FEATURE_NO_DEFAULT_KEY);
4064+
walletInstance->SetMinVersion(FEATURE_LATEST);
40364065

40374066
// generate a new master key
40384067
CPubKey masterPubKey = walletInstance->GenerateNewHDMasterKey();

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ enum WalletFeature
8989

9090
FEATURE_NO_DEFAULT_KEY = 159900, // Wallet without a default key written
9191

92-
FEATURE_LATEST = FEATURE_COMPRPUBKEY // HD is optional, use FEATURE_COMPRPUBKEY as latest version
92+
FEATURE_LATEST = FEATURE_NO_DEFAULT_KEY
9393
};
9494

9595
enum class OutputType {

0 commit comments

Comments
 (0)