@@ -202,12 +202,11 @@ isminetype LegacyScriptPubKeyMan::IsMine(const CScript& script) const
202
202
assert (false );
203
203
}
204
204
205
- bool CWallet::Unlock (const CKeyingMaterial& vMasterKeyIn , bool accept_no_keys)
205
+ bool LegacyScriptPubKeyMan::CheckDecryptionKey (const CKeyingMaterial& master_key , bool accept_no_keys)
206
206
{
207
207
{
208
208
LOCK (cs_KeyStore);
209
- if (!SetCrypted ())
210
- return false ;
209
+ assert (mapKeys.empty ());
211
210
212
211
bool keyPass = mapCryptedKeys.empty (); // Always pass when there are no encrypted keys
213
212
bool keyFail = false ;
@@ -217,7 +216,7 @@ bool CWallet::Unlock(const CKeyingMaterial& vMasterKeyIn, bool accept_no_keys)
217
216
const CPubKey &vchPubKey = (*mi).second .first ;
218
217
const std::vector<unsigned char > &vchCryptedSecret = (*mi).second .second ;
219
218
CKey key;
220
- if (!DecryptKey (vMasterKeyIn , vchCryptedSecret, vchPubKey, key))
219
+ if (!DecryptKey (master_key , vchCryptedSecret, vchPubKey, key))
221
220
{
222
221
keyFail = true ;
223
222
break ;
@@ -233,32 +232,39 @@ bool CWallet::Unlock(const CKeyingMaterial& vMasterKeyIn, bool accept_no_keys)
233
232
}
234
233
if (keyFail || (!keyPass && !accept_no_keys))
235
234
return false ;
236
- vMasterKey = vMasterKeyIn;
237
235
fDecryptionThoroughlyChecked = true ;
238
236
}
239
- NotifyStatusChanged (this );
240
237
return true ;
241
238
}
242
239
243
- bool LegacyScriptPubKeyMan::EncryptKeys ( CKeyingMaterial& vMasterKeyIn )
240
+ bool LegacyScriptPubKeyMan::Encrypt ( const CKeyingMaterial& master_key, WalletBatch* batch )
244
241
{
242
+ AssertLockHeld (cs_wallet);
245
243
LOCK (cs_KeyStore);
246
- if (!mapCryptedKeys.empty () || IsCrypted ())
244
+ encrypted_batch = batch;
245
+ if (!mapCryptedKeys.empty ()) {
246
+ encrypted_batch = nullptr ;
247
247
return false ;
248
+ }
248
249
249
- fUseCrypto = true ;
250
- for (const KeyMap::value_type& mKey : mapKeys)
250
+ KeyMap keys_to_encrypt;
251
+ keys_to_encrypt.swap (mapKeys); // Clear mapKeys so AddCryptedKeyInner will succeed.
252
+ for (const KeyMap::value_type& mKey : keys_to_encrypt)
251
253
{
252
254
const CKey &key = mKey .second ;
253
255
CPubKey vchPubKey = key.GetPubKey ();
254
256
CKeyingMaterial vchSecret (key.begin (), key.end ());
255
257
std::vector<unsigned char > vchCryptedSecret;
256
- if (!EncryptSecret (vMasterKeyIn, vchSecret, vchPubKey.GetHash (), vchCryptedSecret))
258
+ if (!EncryptSecret (master_key, vchSecret, vchPubKey.GetHash (), vchCryptedSecret)) {
259
+ encrypted_batch = nullptr ;
257
260
return false ;
258
- if (!AddCryptedKey (vchPubKey, vchCryptedSecret))
261
+ }
262
+ if (!AddCryptedKey (vchPubKey, vchCryptedSecret)) {
263
+ encrypted_batch = nullptr ;
259
264
return false ;
265
+ }
260
266
}
261
- mapKeys. clear () ;
267
+ encrypted_batch = nullptr ;
262
268
return true ;
263
269
}
264
270
@@ -543,7 +549,7 @@ bool LegacyScriptPubKeyMan::AddKeyPubKeyWithDB(WalletBatch& batch, const CKey& s
543
549
RemoveWatchOnly (script);
544
550
}
545
551
546
- if (!IsCrypted ()) {
552
+ if (!m_storage. HasEncryptionKeys ()) {
547
553
return batch.WriteKey (pubkey,
548
554
secret.GetPrivKey (),
549
555
mapKeyMetadata[pubkey.GetID ()]);
@@ -584,7 +590,7 @@ void LegacyScriptPubKeyMan::LoadScriptMetadata(const CScriptID& script_id, const
584
590
bool LegacyScriptPubKeyMan::AddKeyPubKeyInner (const CKey& key, const CPubKey &pubkey)
585
591
{
586
592
LOCK (cs_KeyStore);
587
- if (!IsCrypted ()) {
593
+ if (!m_storage. HasEncryptionKeys ()) {
588
594
return FillableSigningProvider::AddKeyPubKey (key, pubkey);
589
595
}
590
596
@@ -594,7 +600,7 @@ bool LegacyScriptPubKeyMan::AddKeyPubKeyInner(const CKey& key, const CPubKey &pu
594
600
595
601
std::vector<unsigned char > vchCryptedSecret;
596
602
CKeyingMaterial vchSecret (key.begin (), key.end ());
597
- if (!EncryptSecret (vMasterKey , vchSecret, pubkey.GetHash (), vchCryptedSecret)) {
603
+ if (!EncryptSecret (m_storage. GetEncryptionKey () , vchSecret, pubkey.GetHash (), vchCryptedSecret)) {
598
604
return false ;
599
605
}
600
606
@@ -612,9 +618,7 @@ bool LegacyScriptPubKeyMan::LoadCryptedKey(const CPubKey &vchPubKey, const std::
612
618
bool LegacyScriptPubKeyMan::AddCryptedKeyInner (const CPubKey &vchPubKey, const std::vector<unsigned char > &vchCryptedSecret)
613
619
{
614
620
LOCK (cs_KeyStore);
615
- if (!SetCrypted ()) {
616
- return false ;
617
- }
621
+ assert (mapKeys.empty ());
618
622
619
623
mapCryptedKeys[vchPubKey.GetID ()] = make_pair (vchPubKey, vchCryptedSecret);
620
624
ImplicitlyLearnRelatedKeyScripts (vchPubKey);
@@ -741,7 +745,7 @@ void LegacyScriptPubKeyMan::SetHDChain(const CHDChain& chain, bool memonly)
741
745
bool LegacyScriptPubKeyMan::HaveKey (const CKeyID &address) const
742
746
{
743
747
LOCK (cs_KeyStore);
744
- if (!IsCrypted ()) {
748
+ if (!m_storage. HasEncryptionKeys ()) {
745
749
return FillableSigningProvider::HaveKey (address);
746
750
}
747
751
return mapCryptedKeys.count (address) > 0 ;
@@ -750,7 +754,7 @@ bool LegacyScriptPubKeyMan::HaveKey(const CKeyID &address) const
750
754
bool LegacyScriptPubKeyMan::GetKey (const CKeyID &address, CKey& keyOut) const
751
755
{
752
756
LOCK (cs_KeyStore);
753
- if (!IsCrypted ()) {
757
+ if (!m_storage. HasEncryptionKeys ()) {
754
758
return FillableSigningProvider::GetKey (address, keyOut);
755
759
}
756
760
@@ -759,7 +763,7 @@ bool LegacyScriptPubKeyMan::GetKey(const CKeyID &address, CKey& keyOut) const
759
763
{
760
764
const CPubKey &vchPubKey = (*mi).second .first ;
761
765
const std::vector<unsigned char > &vchCryptedSecret = (*mi).second .second ;
762
- return DecryptKey (vMasterKey , vchCryptedSecret, vchPubKey, keyOut);
766
+ return DecryptKey (m_storage. GetEncryptionKey () , vchCryptedSecret, vchPubKey, keyOut);
763
767
}
764
768
return false ;
765
769
}
@@ -797,7 +801,7 @@ bool LegacyScriptPubKeyMan::GetWatchPubKey(const CKeyID &address, CPubKey &pubke
797
801
bool LegacyScriptPubKeyMan::GetPubKey (const CKeyID &address, CPubKey& vchPubKeyOut) const
798
802
{
799
803
LOCK (cs_KeyStore);
800
- if (!IsCrypted ()) {
804
+ if (!m_storage. HasEncryptionKeys ()) {
801
805
if (!FillableSigningProvider::GetPubKey (address, vchPubKeyOut)) {
802
806
return GetWatchPubKey (address, vchPubKeyOut);
803
807
}
@@ -1383,7 +1387,7 @@ bool LegacyScriptPubKeyMan::ImportScriptPubKeys(const std::set<CScript>& script_
1383
1387
std::set<CKeyID> LegacyScriptPubKeyMan::GetKeys () const
1384
1388
{
1385
1389
LOCK (cs_KeyStore);
1386
- if (!IsCrypted ()) {
1390
+ if (!m_storage. HasEncryptionKeys ()) {
1387
1391
return FillableSigningProvider::GetKeys ();
1388
1392
}
1389
1393
std::set<CKeyID> set_address;
@@ -1397,13 +1401,8 @@ std::set<CKeyID> LegacyScriptPubKeyMan::GetKeys() const
1397
1401
LegacyScriptPubKeyMan::LegacyScriptPubKeyMan (CWallet& wallet)
1398
1402
: ScriptPubKeyMan(wallet),
1399
1403
m_wallet(wallet),
1400
- cs_wallet(wallet.cs_wallet),
1401
- vMasterKey(wallet.vMasterKey),
1402
- fUseCrypto(wallet.fUseCrypto ),
1403
- fDecryptionThoroughlyChecked(wallet.fDecryptionThoroughlyChecked ) {}
1404
+ cs_wallet(wallet.cs_wallet) {}
1404
1405
1405
- bool LegacyScriptPubKeyMan::SetCrypted () { return m_wallet.SetCrypted (); }
1406
- bool LegacyScriptPubKeyMan::IsCrypted () const { return m_wallet.IsCrypted (); }
1407
1406
void LegacyScriptPubKeyMan::NotifyWatchonlyChanged (bool fHaveWatchOnly ) const { return m_wallet.NotifyWatchonlyChanged (fHaveWatchOnly ); }
1408
1407
void LegacyScriptPubKeyMan::NotifyCanGetAddressesChanged () const { return m_wallet.NotifyCanGetAddressesChanged (); }
1409
1408
template <typename ... Params> void LegacyScriptPubKeyMan::WalletLogPrintf (const std::string& fmt, const Params&... parameters) const { return m_wallet.WalletLogPrintf (fmt, parameters...); }
0 commit comments