From 9382f0425e87b30e4621e0e23a99d6e880ec2200 Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Mon, 27 Mar 2017 09:51:55 +0200 Subject: [PATCH] Do not break backward compatibility during wallet encryption --- src/wallet/wallet.cpp | 10 ++++++++-- src/wallet/wallet.h | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4e625f64d..a99817958 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -639,7 +639,9 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) if (IsHDEnabled()) { CKey key; CPubKey masterPubKey = GenerateNewHDMasterKey(); - if (!SetHDMasterKey(masterPubKey)) + // preserve the old chains version to not break backward compatibility + CHDChain oldChain = GetHDChain(); + if (!SetHDMasterKey(masterPubKey, &oldChain)) return false; } @@ -1306,13 +1308,17 @@ CPubKey CWallet::GenerateNewHDMasterKey() return pubkey; } -bool CWallet::SetHDMasterKey(const CPubKey& pubkey) +bool CWallet::SetHDMasterKey(const CPubKey& pubkey, CHDChain *possibleOldChain) { LOCK(cs_wallet); // store the keyid (hash160) together with // the child index counter in the database // as a hdchain object CHDChain newHdChain; + if (possibleOldChain) { + // preserve the old chains version + newHdChain.nVersion = possibleOldChain->nVersion; + } newHdChain.masterKeyID = pubkey.GetID(); SetHDChain(newHdChain, false); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 0b762cd89..ccede6009 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1055,8 +1055,10 @@ public: /* Generates a new HD master key (will not be activated) */ CPubKey GenerateNewHDMasterKey(); - /* Set the current HD master key (will reset the chain child index counters) */ - bool SetHDMasterKey(const CPubKey& key); + /* Set the current HD master key (will reset the chain child index counters) + If possibleOldChain is provided, the parameters from the old chain (version) + will be preserved. */ + bool SetHDMasterKey(const CPubKey& key, CHDChain *possibleOldChain = nullptr); }; /** A key allocated from the key pool. */