Skip to content

Commit 185c7f0

Browse files
committed
Avoid reading the old hd master key during wallet encryption
This makes SetHDMasterKey responsible for maintinaing the CHDChain version instead of always creating it with the latest version and making EncryptWallet responsible for keeping the version from changing.
1 parent f34cdcb commit 185c7f0

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,9 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
637637

638638
// if we are using HD, replace the HD master key (seed) with a new one
639639
if (IsHDEnabled()) {
640-
CKey key;
641-
CPubKey masterPubKey = GenerateNewHDMasterKey();
642-
// preserve the old chains version to not break backward compatibility
643-
CHDChain oldChain = GetHDChain();
644-
if (!SetHDMasterKey(masterPubKey, &oldChain))
640+
if (!SetHDMasterKey(GenerateNewHDMasterKey())) {
645641
return false;
642+
}
646643
}
647644

648645
NewKeyPool();
@@ -1308,17 +1305,14 @@ CPubKey CWallet::GenerateNewHDMasterKey()
13081305
return pubkey;
13091306
}
13101307

1311-
bool CWallet::SetHDMasterKey(const CPubKey& pubkey, CHDChain *possibleOldChain)
1308+
bool CWallet::SetHDMasterKey(const CPubKey& pubkey)
13121309
{
13131310
LOCK(cs_wallet);
13141311
// store the keyid (hash160) together with
13151312
// the child index counter in the database
13161313
// as a hdchain object
13171314
CHDChain newHdChain;
1318-
if (possibleOldChain) {
1319-
// preserve the old chains version
1320-
newHdChain.nVersion = possibleOldChain->nVersion;
1321-
}
1315+
newHdChain.nVersion = CanSupportFeature(FEATURE_HD_SPLIT) ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE;
13221316
newHdChain.masterKeyID = pubkey.GetID();
13231317
SetHDChain(newHdChain, false);
13241318

src/wallet/wallet.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,10 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10561056
CPubKey GenerateNewHDMasterKey();
10571057

10581058
/* Set the current HD master key (will reset the chain child index counters)
1059-
If possibleOldChain is provided, the parameters from the old chain (version)
1060-
will be preserved. */
1061-
bool SetHDMasterKey(const CPubKey& key, CHDChain *possibleOldChain = nullptr);
1059+
Sets the master key's version based on the current wallet version (so the
1060+
caller must ensure the current wallet version is correct before calling
1061+
this function). */
1062+
bool SetHDMasterKey(const CPubKey& key);
10621063
};
10631064

10641065
/** A key allocated from the key pool. */

0 commit comments

Comments
 (0)