Skip to content

Commit 9382f04

Browse files
committed
Do not break backward compatibility during wallet encryption
1 parent 1df08d1 commit 9382f04

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/wallet/wallet.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,9 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
639639
if (IsHDEnabled()) {
640640
CKey key;
641641
CPubKey masterPubKey = GenerateNewHDMasterKey();
642-
if (!SetHDMasterKey(masterPubKey))
642+
// preserve the old chains version to not break backward compatibility
643+
CHDChain oldChain = GetHDChain();
644+
if (!SetHDMasterKey(masterPubKey, &oldChain))
643645
return false;
644646
}
645647

@@ -1306,13 +1308,17 @@ CPubKey CWallet::GenerateNewHDMasterKey()
13061308
return pubkey;
13071309
}
13081310

1309-
bool CWallet::SetHDMasterKey(const CPubKey& pubkey)
1311+
bool CWallet::SetHDMasterKey(const CPubKey& pubkey, CHDChain *possibleOldChain)
13101312
{
13111313
LOCK(cs_wallet);
13121314
// store the keyid (hash160) together with
13131315
// the child index counter in the database
13141316
// as a hdchain object
13151317
CHDChain newHdChain;
1318+
if (possibleOldChain) {
1319+
// preserve the old chains version
1320+
newHdChain.nVersion = possibleOldChain->nVersion;
1321+
}
13161322
newHdChain.masterKeyID = pubkey.GetID();
13171323
SetHDChain(newHdChain, false);
13181324

src/wallet/wallet.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,10 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10551055
/* Generates a new HD master key (will not be activated) */
10561056
CPubKey GenerateNewHDMasterKey();
10571057

1058-
/* Set the current HD master key (will reset the chain child index counters) */
1059-
bool SetHDMasterKey(const CPubKey& key);
1058+
/* 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);
10601062
};
10611063

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

0 commit comments

Comments
 (0)