@@ -386,6 +386,45 @@ bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::stri
386
386
return true ;
387
387
}
388
388
389
+ bool LoadCryptedKey (CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
390
+ {
391
+ LOCK (pwallet->cs_wallet );
392
+ try {
393
+ CPubKey vchPubKey;
394
+ ssKey >> vchPubKey;
395
+ if (!vchPubKey.IsValid ())
396
+ {
397
+ strErr = " Error reading wallet database: CPubKey corrupt" ;
398
+ return false ;
399
+ }
400
+ std::vector<unsigned char > vchPrivKey;
401
+ ssValue >> vchPrivKey;
402
+
403
+ // Get the checksum and check it
404
+ bool checksum_valid = false ;
405
+ if (!ssValue.eof ()) {
406
+ uint256 checksum;
407
+ ssValue >> checksum;
408
+ if (!(checksum_valid = Hash (vchPrivKey) == checksum)) {
409
+ strErr = " Error reading wallet database: Encrypted key corrupt" ;
410
+ return false ;
411
+ }
412
+ }
413
+
414
+ if (!pwallet->GetOrCreateLegacyScriptPubKeyMan ()->LoadCryptedKey (vchPubKey, vchPrivKey, checksum_valid))
415
+ {
416
+ strErr = " Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed" ;
417
+ return false ;
418
+ }
419
+ } catch (const std::exception& e) {
420
+ if (strErr.empty ()) {
421
+ strErr = e.what ();
422
+ }
423
+ return false ;
424
+ }
425
+ return true ;
426
+ }
427
+
389
428
static bool
390
429
ReadKeyValue (CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
391
430
CWalletScanState &wss, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr ) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
@@ -493,34 +532,8 @@ ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
493
532
if (pwallet->nMasterKeyMaxID < nID)
494
533
pwallet->nMasterKeyMaxID = nID;
495
534
} else if (strType == DBKeys::CRYPTED_KEY) {
496
- CPubKey vchPubKey;
497
- ssKey >> vchPubKey;
498
- if (!vchPubKey.IsValid ())
499
- {
500
- strErr = " Error reading wallet database: CPubKey corrupt" ;
501
- return false ;
502
- }
503
- std::vector<unsigned char > vchPrivKey;
504
- ssValue >> vchPrivKey;
505
-
506
- // Get the checksum and check it
507
- bool checksum_valid = false ;
508
- if (!ssValue.eof ()) {
509
- uint256 checksum;
510
- ssValue >> checksum;
511
- if (!(checksum_valid = Hash (vchPrivKey) == checksum)) {
512
- strErr = " Error reading wallet database: Encrypted key corrupt" ;
513
- return false ;
514
- }
515
- }
516
-
517
535
wss.nCKeys ++;
518
-
519
- if (!pwallet->GetOrCreateLegacyScriptPubKeyMan ()->LoadCryptedKey (vchPubKey, vchPrivKey, checksum_valid))
520
- {
521
- strErr = " Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed" ;
522
- return false ;
523
- }
536
+ if (!LoadCryptedKey (pwallet, ssKey, ssValue, strErr)) return false ;
524
537
wss.fIsEncrypted = true ;
525
538
} else if (strType == DBKeys::KEYMETA) {
526
539
CPubKey vchPubKey;
0 commit comments