Skip to content

Commit e6e3fc3

Browse files
committed
Merge #11272: CKeystore/CCrypter: move relevant implementation out of the header
dd9bb25 Fix code style in keystore.cpp/crypter.cpp (Jonas Schnelli) 208fda6 CCrypter: move relevant implementation out of the header (Jonas Schnelli) 3155fd2 CKeystore: move relevant implementation out of the header (Jonas Schnelli) Pull request description: Tree-SHA512: 4ce73cca5609199b74b8ff2614ee2b6af949545a1332a3a0135c6453c98665d2b0da171c1e390c9a2aec6b12b7fad931ec90084bb7c2defe243786bfc70daf60
2 parents 1f4375f + dd9bb25 commit e6e3fc3

File tree

4 files changed

+117
-127
lines changed

4 files changed

+117
-127
lines changed

src/keystore.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
3636
return true;
3737
}
3838

39+
bool CBasicKeyStore::HaveKey(const CKeyID &address) const
40+
{
41+
LOCK(cs_KeyStore);
42+
return mapKeys.count(address) > 0;
43+
}
44+
45+
std::set<CKeyID> CBasicKeyStore::GetKeys() const
46+
{
47+
LOCK(cs_KeyStore);
48+
std::set<CKeyID> set_address;
49+
for (const auto& mi : mapKeys) {
50+
set_address.insert(mi.first);
51+
}
52+
return set_address;
53+
}
54+
55+
bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const
56+
{
57+
LOCK(cs_KeyStore);
58+
KeyMap::const_iterator mi = mapKeys.find(address);
59+
if (mi != mapKeys.end()) {
60+
keyOut = mi->second;
61+
return true;
62+
}
63+
return false;
64+
}
65+
3966
bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
4067
{
4168
if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)

src/keystore.h

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,9 @@ class CBasicKeyStore : public CKeyStore
6262
public:
6363
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
6464
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
65-
bool HaveKey(const CKeyID &address) const override
66-
{
67-
bool result;
68-
{
69-
LOCK(cs_KeyStore);
70-
result = (mapKeys.count(address) > 0);
71-
}
72-
return result;
73-
}
74-
std::set<CKeyID> GetKeys() const override
75-
{
76-
LOCK(cs_KeyStore);
77-
std::set<CKeyID> set_address;
78-
for (const auto& mi : mapKeys) {
79-
set_address.insert(mi.first);
80-
}
81-
return set_address;
82-
}
83-
bool GetKey(const CKeyID &address, CKey &keyOut) const override
84-
{
85-
{
86-
LOCK(cs_KeyStore);
87-
KeyMap::const_iterator mi = mapKeys.find(address);
88-
if (mi != mapKeys.end())
89-
{
90-
keyOut = mi->second;
91-
return true;
92-
}
93-
}
94-
return false;
95-
}
65+
bool HaveKey(const CKeyID &address) const override;
66+
std::set<CKeyID> GetKeys() const override;
67+
bool GetKey(const CKeyID &address, CKey &keyOut) const override;
9668
bool AddCScript(const CScript& redeemScript) override;
9769
bool HaveCScript(const CScriptID &hash) const override;
9870
bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;

src/wallet/crypter.cpp

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ bool CCryptoKeyStore::SetCrypted()
152152
return true;
153153
}
154154

155+
bool CCryptoKeyStore::IsLocked() const
156+
{
157+
if (!IsCrypted()) {
158+
return false;
159+
}
160+
LOCK(cs_KeyStore);
161+
return vMasterKey.empty();
162+
}
163+
155164
bool CCryptoKeyStore::Lock()
156165
{
157166
if (!SetCrypted())
@@ -206,94 +215,112 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
206215

207216
bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
208217
{
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+
}
213222

214-
if (IsLocked())
215-
return false;
223+
if (IsLocked()) {
224+
return false;
225+
}
216226

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+
}
221232

222-
if (!AddCryptedKey(pubkey, vchCryptedSecret))
223-
return false;
233+
if (!AddCryptedKey(pubkey, vchCryptedSecret)) {
234+
return false;
224235
}
225236
return true;
226237
}
227238

228239

229240
bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
230241
{
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;
237245
}
246+
247+
mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
238248
return true;
239249
}
240250

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+
241260
bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
242261
{
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+
}
247266

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);
255273
}
256274
return false;
257275
}
258276

259277
bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
260278
{
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())
261285
{
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+
}
265292

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();
274298
}
299+
std::set<CKeyID> set_address;
300+
for (const auto& mi : mapCryptedKeys) {
301+
set_address.insert(mi.first);
302+
}
303+
return set_address;
275304
}
276305

277306
bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
278307
{
308+
LOCK(cs_KeyStore);
309+
if (!mapCryptedKeys.empty() || IsCrypted())
310+
return false;
311+
312+
fUseCrypto = true;
313+
for (KeyMap::value_type& mKey : mapKeys)
279314
{
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))
282322
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();
297323
}
324+
mapKeys.clear();
298325
return true;
299326
}

src/wallet/crypter.h

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -139,52 +139,16 @@ class CCryptoKeyStore : public CBasicKeyStore
139139
{
140140
}
141141

142-
bool IsCrypted() const
143-
{
144-
return fUseCrypto;
145-
}
146-
147-
bool IsLocked() const
148-
{
149-
if (!IsCrypted())
150-
return false;
151-
bool result;
152-
{
153-
LOCK(cs_KeyStore);
154-
result = vMasterKey.empty();
155-
}
156-
return result;
157-
}
158-
142+
bool IsCrypted() const { return fUseCrypto; }
143+
bool IsLocked() const;
159144
bool Lock();
160145

161146
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
162147
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
163-
bool HaveKey(const CKeyID &address) const override
164-
{
165-
{
166-
LOCK(cs_KeyStore);
167-
if (!IsCrypted()) {
168-
return CBasicKeyStore::HaveKey(address);
169-
}
170-
return mapCryptedKeys.count(address) > 0;
171-
}
172-
return false;
173-
}
148+
bool HaveKey(const CKeyID &address) const override;
174149
bool GetKey(const CKeyID &address, CKey& keyOut) const override;
175150
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
176-
std::set<CKeyID> GetKeys() const override
177-
{
178-
LOCK(cs_KeyStore);
179-
if (!IsCrypted()) {
180-
return CBasicKeyStore::GetKeys();
181-
}
182-
std::set<CKeyID> set_address;
183-
for (const auto& mi : mapCryptedKeys) {
184-
set_address.insert(mi.first);
185-
}
186-
return set_address;
187-
}
151+
std::set<CKeyID> GetKeys() const override;
188152

189153
/**
190154
* Wallet status (encrypted, locked) changed.

0 commit comments

Comments
 (0)