@@ -155,14 +155,11 @@ bool CCryptoKeyStore::SetCrypted()
155
155
156
156
bool CCryptoKeyStore::IsLocked () const
157
157
{
158
- if (!IsCrypted ())
158
+ if (!IsCrypted ()) {
159
159
return false ;
160
- bool result;
161
- {
162
- LOCK (cs_KeyStore);
163
- result = vMasterKey.empty ();
164
160
}
165
- return result;
161
+ LOCK (cs_KeyStore);
162
+ return vMasterKey.empty ();
166
163
}
167
164
168
165
bool CCryptoKeyStore::Lock ()
@@ -219,84 +216,79 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
219
216
220
217
bool CCryptoKeyStore::AddKeyPubKey (const CKey& key, const CPubKey &pubkey)
221
218
{
222
- {
223
- LOCK (cs_KeyStore);
224
- if (! IsCrypted ())
225
- return CBasicKeyStore::AddKeyPubKey (key, pubkey);
219
+ LOCK (cs_KeyStore);
220
+ if (! IsCrypted ()) {
221
+ return CBasicKeyStore::AddKeyPubKey (key, pubkey);
222
+ }
226
223
227
- if (IsLocked ())
228
- return false ;
224
+ if (IsLocked ()) {
225
+ return false ;
226
+ }
229
227
230
- std::vector<unsigned char > vchCryptedSecret;
231
- CKeyingMaterial vchSecret (key.begin (), key.end ());
232
- if (!EncryptSecret (vMasterKey, vchSecret, pubkey.GetHash (), vchCryptedSecret))
233
- return false ;
228
+ std::vector<unsigned char > vchCryptedSecret;
229
+ CKeyingMaterial vchSecret (key.begin (), key.end ());
230
+ if (!EncryptSecret (vMasterKey, vchSecret, pubkey.GetHash (), vchCryptedSecret)) {
231
+ return false ;
232
+ }
234
233
235
- if (!AddCryptedKey (pubkey, vchCryptedSecret))
236
- return false ;
234
+ if (!AddCryptedKey (pubkey, vchCryptedSecret)) {
235
+ return false ;
237
236
}
238
237
return true ;
239
238
}
240
239
241
240
242
241
bool CCryptoKeyStore::AddCryptedKey (const CPubKey &vchPubKey, const std::vector<unsigned char > &vchCryptedSecret)
243
242
{
244
- {
245
- LOCK (cs_KeyStore);
246
- if (!SetCrypted ())
247
- return false ;
248
-
249
- mapCryptedKeys[vchPubKey.GetID ()] = make_pair (vchPubKey, vchCryptedSecret);
243
+ LOCK (cs_KeyStore);
244
+ if (!SetCrypted ()) {
245
+ return false ;
250
246
}
247
+
248
+ mapCryptedKeys[vchPubKey.GetID ()] = make_pair (vchPubKey, vchCryptedSecret);
251
249
return true ;
252
250
}
253
251
254
252
bool CCryptoKeyStore::HaveKey (const CKeyID &address) const
255
253
{
256
- {
257
- LOCK (cs_KeyStore);
258
- if (!IsCrypted ()) {
259
- return CBasicKeyStore::HaveKey (address);
260
- }
261
- return mapCryptedKeys.count (address) > 0 ;
254
+ LOCK (cs_KeyStore);
255
+ if (!IsCrypted ()) {
256
+ return CBasicKeyStore::HaveKey (address);
262
257
}
263
- return false ;
258
+ return mapCryptedKeys. count (address) > 0 ;
264
259
}
265
260
266
261
bool CCryptoKeyStore::GetKey (const CKeyID &address, CKey& keyOut) const
267
262
{
268
- {
269
- LOCK (cs_KeyStore);
270
- if (! IsCrypted ())
271
- return CBasicKeyStore::GetKey (address, keyOut);
263
+ LOCK (cs_KeyStore);
264
+ if (! IsCrypted ()) {
265
+ return CBasicKeyStore::GetKey (address, keyOut);
266
+ }
272
267
273
- CryptedKeyMap::const_iterator mi = mapCryptedKeys.find (address);
274
- if (mi != mapCryptedKeys.end ())
275
- {
276
- const CPubKey &vchPubKey = (*mi).second .first ;
277
- const std::vector<unsigned char > &vchCryptedSecret = (*mi).second .second ;
278
- return DecryptKey (vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
279
- }
268
+ CryptedKeyMap::const_iterator mi = mapCryptedKeys.find (address);
269
+ if (mi != mapCryptedKeys.end ())
270
+ {
271
+ const CPubKey &vchPubKey = (*mi).second .first ;
272
+ const std::vector<unsigned char > &vchCryptedSecret = (*mi).second .second ;
273
+ return DecryptKey (vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
280
274
}
281
275
return false ;
282
276
}
283
277
284
278
bool CCryptoKeyStore::GetPubKey (const CKeyID &address, CPubKey& vchPubKeyOut) const
285
279
{
286
- {
287
- LOCK (cs_KeyStore);
288
- if (!IsCrypted ())
289
- return CBasicKeyStore::GetPubKey (address, vchPubKeyOut);
290
-
291
- CryptedKeyMap::const_iterator mi = mapCryptedKeys.find (address);
292
- if (mi != mapCryptedKeys.end ())
293
- {
294
- vchPubKeyOut = (*mi).second .first ;
295
- return true ;
296
- }
297
- // Check for watch-only pubkeys
280
+ LOCK (cs_KeyStore);
281
+ if (!IsCrypted ())
298
282
return CBasicKeyStore::GetPubKey (address, vchPubKeyOut);
283
+
284
+ CryptedKeyMap::const_iterator mi = mapCryptedKeys.find (address);
285
+ if (mi != mapCryptedKeys.end ())
286
+ {
287
+ vchPubKeyOut = (*mi).second .first ;
288
+ return true ;
299
289
}
290
+ // Check for watch-only pubkeys
291
+ return CBasicKeyStore::GetPubKey (address, vchPubKeyOut);
300
292
}
301
293
302
294
std::set<CKeyID> CCryptoKeyStore::GetKeys () const
@@ -314,24 +306,22 @@ std::set<CKeyID> CCryptoKeyStore::GetKeys() const
314
306
315
307
bool CCryptoKeyStore::EncryptKeys (CKeyingMaterial& vMasterKeyIn)
316
308
{
309
+ LOCK (cs_KeyStore);
310
+ if (!mapCryptedKeys.empty () || IsCrypted ())
311
+ return false ;
312
+
313
+ fUseCrypto = true ;
314
+ for (KeyMap::value_type& mKey : mapKeys)
317
315
{
318
- LOCK (cs_KeyStore);
319
- if (!mapCryptedKeys.empty () || IsCrypted ())
316
+ const CKey &key = mKey .second ;
317
+ CPubKey vchPubKey = key.GetPubKey ();
318
+ CKeyingMaterial vchSecret (key.begin (), key.end ());
319
+ std::vector<unsigned char > vchCryptedSecret;
320
+ if (!EncryptSecret (vMasterKeyIn, vchSecret, vchPubKey.GetHash (), vchCryptedSecret))
321
+ return false ;
322
+ if (!AddCryptedKey (vchPubKey, vchCryptedSecret))
320
323
return false ;
321
-
322
- fUseCrypto = true ;
323
- for (KeyMap::value_type& mKey : mapKeys)
324
- {
325
- const CKey &key = mKey .second ;
326
- CPubKey vchPubKey = key.GetPubKey ();
327
- CKeyingMaterial vchSecret (key.begin (), key.end ());
328
- std::vector<unsigned char > vchCryptedSecret;
329
- if (!EncryptSecret (vMasterKeyIn, vchSecret, vchPubKey.GetHash (), vchCryptedSecret))
330
- return false ;
331
- if (!AddCryptedKey (vchPubKey, vchCryptedSecret))
332
- return false ;
333
- }
334
- mapKeys.clear ();
335
324
}
325
+ mapKeys.clear ();
336
326
return true ;
337
327
}
0 commit comments