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