@@ -155,14 +155,11 @@ bool CCryptoKeyStore::SetCrypted()
155155
156156bool  CCryptoKeyStore::IsLocked () const 
157157{
158-     if  (!IsCrypted ())
158+     if  (!IsCrypted ()) { 
159159        return  false ;
160-     bool  result;
161-     {
162-         LOCK (cs_KeyStore);
163-         result = vMasterKey.empty ();
164160    }
165-     return  result;
161+     LOCK (cs_KeyStore);
162+     return  vMasterKey.empty ();
166163}
167164
168165bool  CCryptoKeyStore::Lock ()
@@ -219,84 +216,79 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
219216
220217bool  CCryptoKeyStore::AddKeyPubKey (const  CKey& key, const  CPubKey &pubkey)
221218{
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+     } 
226223
227-         if  (IsLocked ())
228-             return  false ;
224+     if  (IsLocked ()) {
225+         return  false ;
226+     }
229227
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+     }
234233
235-          if  (!AddCryptedKey (pubkey, vchCryptedSecret))
236-              return  false ;
234+     if  (!AddCryptedKey (pubkey, vchCryptedSecret)) { 
235+         return  false ;
237236    }
238237    return  true ;
239238}
240239
241240
242241bool  CCryptoKeyStore::AddCryptedKey (const  CPubKey &vchPubKey, const  std::vector<unsigned  char > &vchCryptedSecret)
243242{
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 ;
250246    }
247+ 
248+     mapCryptedKeys[vchPubKey.GetID ()] = make_pair (vchPubKey, vchCryptedSecret);
251249    return  true ;
252250}
253251
254252bool  CCryptoKeyStore::HaveKey (const  CKeyID &address) const 
255253{
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);
262257    }
263-     return  false ;
258+     return  mapCryptedKeys. count (address) >  0 ;
264259}
265260
266261bool  CCryptoKeyStore::GetKey (const  CKeyID &address, CKey& keyOut) const 
267262{
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+     } 
272267
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);
280274    }
281275    return  false ;
282276}
283277
284278bool  CCryptoKeyStore::GetPubKey (const  CKeyID &address, CPubKey& vchPubKeyOut) const 
285279{
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 ())
298282        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 ;
299289    }
290+     //  Check for watch-only pubkeys
291+     return  CBasicKeyStore::GetPubKey (address, vchPubKeyOut);
300292}
301293
302294std::set<CKeyID> CCryptoKeyStore::GetKeys () const 
@@ -314,24 +306,22 @@ std::set<CKeyID> CCryptoKeyStore::GetKeys() const
314306
315307bool  CCryptoKeyStore::EncryptKeys (CKeyingMaterial& vMasterKeyIn)
316308{
309+     LOCK (cs_KeyStore);
310+     if  (!mapCryptedKeys.empty () || IsCrypted ())
311+         return  false ;
312+ 
313+     fUseCrypto  = true ;
314+     for  (KeyMap::value_type& mKey  : mapKeys)
317315    {
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))
320323            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 ();
335324    }
325+     mapKeys.clear ();
336326    return  true ;
337327}
0 commit comments