@@ -107,8 +107,7 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
107107 return true ;
108108}
109109
110-
111- static bool EncryptSecret (const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char > &vchCiphertext)
110+ bool EncryptSecret (const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char > &vchCiphertext)
112111{
113112 CCrypter cKeyCrypter;
114113 std::vector<unsigned char > chIV (WALLET_CRYPTO_IV_SIZE);
@@ -118,7 +117,7 @@ static bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMateri
118117 return cKeyCrypter.Encrypt (*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
119118}
120119
121- static bool DecryptSecret (const CKeyingMaterial& vMasterKey, const std::vector<unsigned char >& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
120+ bool DecryptSecret (const CKeyingMaterial& vMasterKey, const std::vector<unsigned char >& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
122121{
123122 CCrypter cKeyCrypter;
124123 std::vector<unsigned char > chIV (WALLET_CRYPTO_IV_SIZE);
@@ -128,7 +127,7 @@ static bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<u
128127 return cKeyCrypter.Decrypt (vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
129128}
130129
131- static bool DecryptKey (const CKeyingMaterial& vMasterKey, const std::vector<unsigned char >& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
130+ bool DecryptKey (const CKeyingMaterial& vMasterKey, const std::vector<unsigned char >& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
132131{
133132 CKeyingMaterial vchSecret;
134133 if (!DecryptSecret (vMasterKey, vchCryptedSecret, vchPubKey.GetHash (), vchSecret))
@@ -140,188 +139,3 @@ static bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsi
140139 key.Set (vchSecret.begin (), vchSecret.end (), vchPubKey.IsCompressed ());
141140 return key.VerifyPubKey (vchPubKey);
142141}
143-
144- bool CCryptoKeyStore::SetCrypted ()
145- {
146- LOCK (cs_KeyStore);
147- if (fUseCrypto )
148- return true ;
149- if (!mapKeys.empty ())
150- return false ;
151- fUseCrypto = true ;
152- return true ;
153- }
154-
155- bool CCryptoKeyStore::IsLocked () const
156- {
157- if (!IsCrypted ()) {
158- return false ;
159- }
160- LOCK (cs_KeyStore);
161- return vMasterKey.empty ();
162- }
163-
164- bool CCryptoKeyStore::Lock ()
165- {
166- if (!SetCrypted ())
167- return false ;
168-
169- {
170- LOCK (cs_KeyStore);
171- vMasterKey.clear ();
172- }
173-
174- NotifyStatusChanged (this );
175- return true ;
176- }
177-
178- bool CCryptoKeyStore::Unlock (const CKeyingMaterial& vMasterKeyIn, bool accept_no_keys)
179- {
180- {
181- LOCK (cs_KeyStore);
182- if (!SetCrypted ())
183- return false ;
184-
185- bool keyPass = mapCryptedKeys.empty (); // Always pass when there are no encrypted keys
186- bool keyFail = false ;
187- CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin ();
188- for (; mi != mapCryptedKeys.end (); ++mi)
189- {
190- const CPubKey &vchPubKey = (*mi).second .first ;
191- const std::vector<unsigned char > &vchCryptedSecret = (*mi).second .second ;
192- CKey key;
193- if (!DecryptKey (vMasterKeyIn, vchCryptedSecret, vchPubKey, key))
194- {
195- keyFail = true ;
196- break ;
197- }
198- keyPass = true ;
199- if (fDecryptionThoroughlyChecked )
200- break ;
201- }
202- if (keyPass && keyFail)
203- {
204- LogPrintf (" The wallet is probably corrupted: Some keys decrypt but not all.\n " );
205- throw std::runtime_error (" Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt." );
206- }
207- if (keyFail || (!keyPass && !accept_no_keys))
208- return false ;
209- vMasterKey = vMasterKeyIn;
210- fDecryptionThoroughlyChecked = true ;
211- }
212- NotifyStatusChanged (this );
213- return true ;
214- }
215-
216- bool CCryptoKeyStore::AddKeyPubKey (const CKey& key, const CPubKey &pubkey)
217- {
218- LOCK (cs_KeyStore);
219- if (!IsCrypted ()) {
220- return FillableSigningProvider::AddKeyPubKey (key, pubkey);
221- }
222-
223- if (IsLocked ()) {
224- return false ;
225- }
226-
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- }
232-
233- if (!AddCryptedKey (pubkey, vchCryptedSecret)) {
234- return false ;
235- }
236- return true ;
237- }
238-
239-
240- bool CCryptoKeyStore::AddCryptedKey (const CPubKey &vchPubKey, const std::vector<unsigned char > &vchCryptedSecret)
241- {
242- LOCK (cs_KeyStore);
243- if (!SetCrypted ()) {
244- return false ;
245- }
246-
247- mapCryptedKeys[vchPubKey.GetID ()] = make_pair (vchPubKey, vchCryptedSecret);
248- ImplicitlyLearnRelatedKeyScripts (vchPubKey);
249- return true ;
250- }
251-
252- bool CCryptoKeyStore::HaveKey (const CKeyID &address) const
253- {
254- LOCK (cs_KeyStore);
255- if (!IsCrypted ()) {
256- return FillableSigningProvider::HaveKey (address);
257- }
258- return mapCryptedKeys.count (address) > 0 ;
259- }
260-
261- bool CCryptoKeyStore::GetKey (const CKeyID &address, CKey& keyOut) const
262- {
263- LOCK (cs_KeyStore);
264- if (!IsCrypted ()) {
265- return FillableSigningProvider::GetKey (address, keyOut);
266- }
267-
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);
274- }
275- return false ;
276- }
277-
278- bool CCryptoKeyStore::GetPubKey (const CKeyID &address, CPubKey& vchPubKeyOut) const
279- {
280- LOCK (cs_KeyStore);
281- if (!IsCrypted ())
282- return FillableSigningProvider::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 ;
289- }
290- // Check for watch-only pubkeys
291- return FillableSigningProvider::GetPubKey (address, vchPubKeyOut);
292- }
293-
294- std::set<CKeyID> CCryptoKeyStore::GetKeys () const
295- {
296- LOCK (cs_KeyStore);
297- if (!IsCrypted ()) {
298- return FillableSigningProvider::GetKeys ();
299- }
300- std::set<CKeyID> set_address;
301- for (const auto & mi : mapCryptedKeys) {
302- set_address.insert (mi.first );
303- }
304- return set_address;
305- }
306-
307- bool CCryptoKeyStore::EncryptKeys (CKeyingMaterial& vMasterKeyIn)
308- {
309- LOCK (cs_KeyStore);
310- if (!mapCryptedKeys.empty () || IsCrypted ())
311- return false ;
312-
313- fUseCrypto = true ;
314- for (const KeyMap::value_type& mKey : mapKeys)
315- {
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))
323- return false ;
324- }
325- mapKeys.clear ();
326- return true ;
327- }
0 commit comments