]> Git Repo - VerusCoin.git/blob - src/keystore.cpp
Merge pull request #2747 from luke-jr/getblock_verbose0
[VerusCoin.git] / src / keystore.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "keystore.h"
7 #include "script.h"
8
9 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
10 {
11     CKey key;
12     if (!GetKey(address, key))
13         return false;
14     vchPubKeyOut = key.GetPubKey();
15     return true;
16 }
17
18 bool CKeyStore::AddKey(const CKey &key) {
19     return AddKeyPubKey(key, key.GetPubKey());
20 }
21
22 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
23 {
24     LOCK(cs_KeyStore);
25     mapKeys[pubkey.GetID()] = key;
26     return true;
27 }
28
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
30 {
31     LOCK(cs_KeyStore);
32     mapScripts[redeemScript.GetID()] = redeemScript;
33     return true;
34 }
35
36 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
37 {
38     LOCK(cs_KeyStore);
39     return mapScripts.count(hash) > 0;
40 }
41
42 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
43 {
44     LOCK(cs_KeyStore);
45     ScriptMap::const_iterator mi = mapScripts.find(hash);
46     if (mi != mapScripts.end())
47     {
48         redeemScriptOut = (*mi).second;
49         return true;
50     }
51     return false;
52 }
53
54 bool CCryptoKeyStore::SetCrypted()
55 {
56     LOCK(cs_KeyStore);
57     if (fUseCrypto)
58         return true;
59     if (!mapKeys.empty())
60         return false;
61     fUseCrypto = true;
62     return true;
63 }
64
65 bool CCryptoKeyStore::Lock()
66 {
67     if (!SetCrypted())
68         return false;
69
70     {
71         LOCK(cs_KeyStore);
72         vMasterKey.clear();
73     }
74
75     NotifyStatusChanged(this);
76     return true;
77 }
78
79 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
80 {
81     {
82         LOCK(cs_KeyStore);
83         if (!SetCrypted())
84             return false;
85
86         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
87         for (; mi != mapCryptedKeys.end(); ++mi)
88         {
89             const CPubKey &vchPubKey = (*mi).second.first;
90             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
91             CKeyingMaterial vchSecret;
92             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
93                 return false;
94             if (vchSecret.size() != 32)
95                 return false;
96             CKey key;
97             key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
98             if (key.GetPubKey() == vchPubKey)
99                 break;
100             return false;
101         }
102         vMasterKey = vMasterKeyIn;
103     }
104     NotifyStatusChanged(this);
105     return true;
106 }
107
108 bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
109 {
110     {
111         LOCK(cs_KeyStore);
112         if (!IsCrypted())
113             return CBasicKeyStore::AddKeyPubKey(key, pubkey);
114
115         if (IsLocked())
116             return false;
117
118         std::vector<unsigned char> vchCryptedSecret;
119         CKeyingMaterial vchSecret(key.begin(), key.end());
120         if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret))
121             return false;
122
123         if (!AddCryptedKey(pubkey, vchCryptedSecret))
124             return false;
125     }
126     return true;
127 }
128
129
130 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
131 {
132     {
133         LOCK(cs_KeyStore);
134         if (!SetCrypted())
135             return false;
136
137         mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
138     }
139     return true;
140 }
141
142 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
143 {
144     {
145         LOCK(cs_KeyStore);
146         if (!IsCrypted())
147             return CBasicKeyStore::GetKey(address, keyOut);
148
149         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
150         if (mi != mapCryptedKeys.end())
151         {
152             const CPubKey &vchPubKey = (*mi).second.first;
153             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
154             CKeyingMaterial vchSecret;
155             if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
156                 return false;
157             if (vchSecret.size() != 32)
158                 return false;
159             keyOut.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
160             return true;
161         }
162     }
163     return false;
164 }
165
166 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
167 {
168     {
169         LOCK(cs_KeyStore);
170         if (!IsCrypted())
171             return CKeyStore::GetPubKey(address, vchPubKeyOut);
172
173         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
174         if (mi != mapCryptedKeys.end())
175         {
176             vchPubKeyOut = (*mi).second.first;
177             return true;
178         }
179     }
180     return false;
181 }
182
183 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
184 {
185     {
186         LOCK(cs_KeyStore);
187         if (!mapCryptedKeys.empty() || IsCrypted())
188             return false;
189
190         fUseCrypto = true;
191         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
192         {
193             const CKey &key = mKey.second;
194             CPubKey vchPubKey = key.GetPubKey();
195             CKeyingMaterial vchSecret(key.begin(), key.end());
196             std::vector<unsigned char> vchCryptedSecret;
197             if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), vchCryptedSecret))
198                 return false;
199             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
200                 return false;
201         }
202         mapKeys.clear();
203     }
204     return true;
205 }
This page took 0.035858 seconds and 4 git commands to generate.