@@ -218,14 +218,14 @@ bool LegacyScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key
218218 if (keyFail) {
219219 return false ;
220220 }
221- if (!keyPass && !accept_no_keys && cryptedHDChain .IsNull ()) {
221+ if (!keyPass && !accept_no_keys && (hdChain .IsNull () || !hdChain. IsNull () && !hdChain. IsCrypted () )) {
222222 return false ;
223223 }
224224
225- if (!cryptedHDChain .IsNull ()) {
225+ if (!hdChain .IsNull () && !hdChain. IsCrypted ()) {
226226 // try to decrypt seed and make sure it matches
227227 CHDChain hdChainTmp;
228- if (!DecryptHDChain (master_key, hdChainTmp) || (cryptedHDChain .GetID () != hdChainTmp.GetSeedHash ())) {
228+ if (!DecryptHDChain (master_key, hdChainTmp) || (hdChain .GetID () != hdChainTmp.GetSeedHash ())) {
229229 return false ;
230230 }
231231 }
@@ -267,7 +267,8 @@ bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBat
267267 }
268268
269269 if (!hdChainCurrent.IsNull ()) {
270- assert (EncryptAndSetHDChain (master_key));
270+ assert (EncryptHDChain (master_key, hdChain));
271+ assert (SetHDChain (hdChain));
271272
272273 CHDChain hdChainCrypted;
273274 assert (GetHDChain (hdChainCrypted));
@@ -396,7 +397,11 @@ void LegacyScriptPubKeyMan::GenerateNewCryptedHDChain(const SecureString& secure
396397 hdChainTmp.AddAccount ();
397398 hdChainTmp.Debug (__func__);
398399
399- bool res = EncryptAndSetHDChain (vMasterKey, hdChainTmp);
400+ // We need to safe chain for validation further
401+ CHDChain hdChainPrev = hdChainTmp;
402+ bool res = EncryptHDChain (vMasterKey, hdChainTmp);
403+ assert (res);
404+ res = SetHDChain (hdChainTmp);
400405 assert (res);
401406
402407 CHDChain hdChainCrypted;
@@ -408,10 +413,9 @@ void LegacyScriptPubKeyMan::GenerateNewCryptedHDChain(const SecureString& secure
408413 tfm::format (std::cout, " GenerateNewCryptedHDChain -- crypted seed: '%s'\n " , HexStr (hdChainCrypted.GetSeed ()));
409414 );
410415
411-
412416 // ids should match, seed hashes should not
413- assert (hdChainTmp .GetID () == hdChainCrypted.GetID ());
414- assert (hdChainTmp .GetSeedHash () != hdChainCrypted.GetSeedHash ());
417+ assert (hdChainPrev .GetID () == hdChainCrypted.GetID ());
418+ assert (hdChainPrev .GetSeedHash () != hdChainCrypted.GetSeedHash ());
415419
416420 hdChainCrypted.Debug (__func__);
417421
@@ -494,62 +498,52 @@ bool LegacyScriptPubKeyMan::GetDecryptedHDChain(CHDChain& hdChainRet)
494498 return true ;
495499}
496500
497- bool LegacyScriptPubKeyMan::EncryptAndSetHDChain (const CKeyingMaterial& vMasterKeyIn, const CHDChain& chain)
501+ bool LegacyScriptPubKeyMan::EncryptHDChain (const CKeyingMaterial& vMasterKeyIn, CHDChain& chain)
498502{
499503 LOCK (cs_KeyStore);
500504 // should call EncryptKeys first
501505 if (!m_storage.HasEncryptionKeys ())
502506 return false ;
503507
504- if (!cryptedHDChain.IsNull ())
505- return true ;
506-
507- if (cryptedHDChain.IsCrypted ())
508+ if (chain.IsCrypted ())
508509 return true ;
509510
510- if (hdChain.IsNull () && !chain.IsNull ()) {
511- // Encrypting a new HDChain for an already encrypted non-HD wallet
512- hdChain = chain;
513- }
514-
515511 // make sure seed matches this chain
516- if (hdChain .GetID () != hdChain .GetSeedHash ())
512+ if (chain .GetID () != chain .GetSeedHash ())
517513 return false ;
518514
519515 std::vector<unsigned char > vchCryptedSeed;
520- if (!EncryptSecret (vMasterKeyIn, hdChain .GetSeed (), hdChain .GetID (), vchCryptedSeed))
516+ if (!EncryptSecret (vMasterKeyIn, chain .GetSeed (), chain .GetID (), vchCryptedSeed))
521517 return false ;
522518
523- hdChain. Debug (__func__) ;
524- cryptedHDChain = hdChain ;
525- cryptedHDChain .SetCrypted (true );
519+ CHDChain cryptedChain = chain ;
520+ cryptedChain. Debug (__func__) ;
521+ cryptedChain .SetCrypted (true );
526522
527523 SecureVector vchSecureCryptedSeed (vchCryptedSeed.begin (), vchCryptedSeed.end ());
528- if (!cryptedHDChain .SetSeed (vchSecureCryptedSeed, false ))
524+ if (!cryptedChain .SetSeed (vchSecureCryptedSeed, false ))
529525 return false ;
530526
531527 SecureVector vchMnemonic;
532528 SecureVector vchMnemonicPassphrase;
533529
534530 // it's ok to have no mnemonic if wallet was initialized via hdseed
535- if (hdChain .GetMnemonic (vchMnemonic, vchMnemonicPassphrase)) {
531+ if (chain .GetMnemonic (vchMnemonic, vchMnemonicPassphrase)) {
536532 std::vector<unsigned char > vchCryptedMnemonic;
537533 std::vector<unsigned char > vchCryptedMnemonicPassphrase;
538534
539- if (!vchMnemonic.empty () && !EncryptSecret (vMasterKeyIn, vchMnemonic, hdChain .GetID (), vchCryptedMnemonic))
535+ if (!vchMnemonic.empty () && !EncryptSecret (vMasterKeyIn, vchMnemonic, chain .GetID (), vchCryptedMnemonic))
540536 return false ;
541- if (!vchMnemonicPassphrase.empty () && !EncryptSecret (vMasterKeyIn, vchMnemonicPassphrase, hdChain .GetID (), vchCryptedMnemonicPassphrase))
537+ if (!vchMnemonicPassphrase.empty () && !EncryptSecret (vMasterKeyIn, vchMnemonicPassphrase, chain .GetID (), vchCryptedMnemonicPassphrase))
542538 return false ;
543539
544540 SecureVector vchSecureCryptedMnemonic (vchCryptedMnemonic.begin (), vchCryptedMnemonic.end ());
545541 SecureVector vchSecureCryptedMnemonicPassphrase (vchCryptedMnemonicPassphrase.begin (), vchCryptedMnemonicPassphrase.end ());
546- if (!cryptedHDChain .SetMnemonic (vchSecureCryptedMnemonic, vchSecureCryptedMnemonicPassphrase, false ))
542+ if (!cryptedChain .SetMnemonic (vchSecureCryptedMnemonic, vchSecureCryptedMnemonicPassphrase, false ))
547543 return false ;
548544 }
549545
550- if (!hdChain.SetNull ())
551- return false ;
552-
546+ chain = cryptedChain;
553547 return true ;
554548}
555549
@@ -559,40 +553,40 @@ bool LegacyScriptPubKeyMan::DecryptHDChain(const CKeyingMaterial& vMasterKeyIn,
559553 if (!m_storage.HasEncryptionKeys ())
560554 return true ;
561555
562- if (cryptedHDChain .IsNull ())
556+ if (hdChain .IsNull ())
563557 return false ;
564558
565- if (!cryptedHDChain .IsCrypted ())
559+ if (!hdChain .IsCrypted ())
566560 return false ;
567561
568562 SecureVector vchSecureSeed;
569- SecureVector vchSecureCryptedSeed = cryptedHDChain .GetSeed ();
563+ SecureVector vchSecureCryptedSeed = hdChain .GetSeed ();
570564 std::vector<unsigned char > vchCryptedSeed (vchSecureCryptedSeed.begin (), vchSecureCryptedSeed.end ());
571- if (!DecryptSecret (vMasterKeyIn, vchCryptedSeed, cryptedHDChain .GetID (), vchSecureSeed))
565+ if (!DecryptSecret (vMasterKeyIn, vchCryptedSeed, hdChain .GetID (), vchSecureSeed))
572566 return false ;
573567
574- hdChainRet = cryptedHDChain ;
568+ hdChainRet = hdChain ;
575569 if (!hdChainRet.SetSeed (vchSecureSeed, false ))
576570 return false ;
577571
578572 // hash of decrypted seed must match chain id
579- if (hdChainRet.GetSeedHash () != cryptedHDChain .GetID ())
573+ if (hdChainRet.GetSeedHash () != hdChain .GetID ())
580574 return false ;
581575
582576 SecureVector vchSecureCryptedMnemonic;
583577 SecureVector vchSecureCryptedMnemonicPassphrase;
584578
585579 // it's ok to have no mnemonic if wallet was initialized via hdseed
586- if (cryptedHDChain .GetMnemonic (vchSecureCryptedMnemonic, vchSecureCryptedMnemonicPassphrase)) {
580+ if (hdChain .GetMnemonic (vchSecureCryptedMnemonic, vchSecureCryptedMnemonicPassphrase)) {
587581 SecureVector vchSecureMnemonic;
588582 SecureVector vchSecureMnemonicPassphrase;
589583
590584 std::vector<unsigned char > vchCryptedMnemonic (vchSecureCryptedMnemonic.begin (), vchSecureCryptedMnemonic.end ());
591585 std::vector<unsigned char > vchCryptedMnemonicPassphrase (vchSecureCryptedMnemonicPassphrase.begin (), vchSecureCryptedMnemonicPassphrase.end ());
592586
593- if (!vchCryptedMnemonic.empty () && !DecryptSecret (vMasterKeyIn, vchCryptedMnemonic, cryptedHDChain .GetID (), vchSecureMnemonic))
587+ if (!vchCryptedMnemonic.empty () && !DecryptSecret (vMasterKeyIn, vchCryptedMnemonic, hdChain .GetID (), vchSecureMnemonic))
594588 return false ;
595- if (!vchCryptedMnemonicPassphrase.empty () && !DecryptSecret (vMasterKeyIn, vchCryptedMnemonicPassphrase, cryptedHDChain .GetID (), vchSecureMnemonicPassphrase))
589+ if (!vchCryptedMnemonicPassphrase.empty () && !DecryptSecret (vMasterKeyIn, vchCryptedMnemonicPassphrase, hdChain .GetID (), vchSecureMnemonicPassphrase))
596590 return false ;
597591
598592 if (!hdChainRet.SetMnemonic (vchSecureMnemonic, vchSecureMnemonicPassphrase, false ))
@@ -1123,11 +1117,7 @@ bool LegacyScriptPubKeyMan::SetHDChain(const CHDChain& chain)
11231117
11241118 if (m_storage.HasEncryptionKeys () != chain.IsCrypted ()) return false ;
11251119
1126- if (chain.IsCrypted ()) {
1127- cryptedHDChain = chain;
1128- } else {
1129- hdChain = chain;
1130- }
1120+ hdChain = chain;
11311121 return true ;
11321122}
11331123
@@ -1786,11 +1776,6 @@ std::set<CKeyID> LegacyScriptPubKeyMan::GetKeys() const
17861776bool LegacyScriptPubKeyMan::GetHDChain (CHDChain& hdChainRet) const
17871777{
17881778 LOCK (cs_KeyStore);
1789- if (m_storage.HasEncryptionKeys () && !cryptedHDChain.IsNull ()) {
1790- hdChainRet = cryptedHDChain;
1791- return true ;
1792- }
1793-
17941779 hdChainRet = hdChain;
17951780 return !hdChain.IsNull ();
17961781}
0 commit comments