e0cf805a19ea3e492c3e6070971ddfd4425bd701
[novacoin.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 CBasicKeyStore::AddKey(const CKey& key)
19 {
20     bool fCompressed = false;
21     CSecret secret = key.GetSecret(fCompressed);
22     {
23         LOCK(cs_KeyStore);
24         mapKeys[key.GetPubKey().GetID()] = make_pair(secret, fCompressed);
25     }
26     return true;
27 }
28
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
30 {
31     {
32         LOCK(cs_KeyStore);
33         mapScripts[redeemScript.GetID()] = redeemScript;
34     }
35     return true;
36 }
37
38 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
39 {
40     bool result;
41     {
42         LOCK(cs_KeyStore);
43         result = (mapScripts.count(hash) > 0);
44     }
45     return result;
46 }
47
48
49 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
50 {
51     {
52         LOCK(cs_KeyStore);
53         ScriptMap::const_iterator mi = mapScripts.find(hash);
54         if (mi != mapScripts.end())
55         {
56             redeemScriptOut = (*mi).second;
57             return true;
58         }
59     }
60     return false;
61 }
62
63 bool CCryptoKeyStore::SetCrypted()
64 {
65     {
66         LOCK(cs_KeyStore);
67         if (fUseCrypto)
68             return true;
69         if (!mapKeys.empty())
70             return false;
71         fUseCrypto = true;
72     }
73     return true;
74 }
75
76 bool CCryptoKeyStore::Lock()
77 {
78     if (!SetCrypted())
79         return false;
80
81     {
82         LOCK(cs_KeyStore);
83         vMasterKey.clear();
84     }
85
86     NotifyStatusChanged(this);
87     return true;
88 }
89
90 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
91 {
92     {
93         LOCK(cs_KeyStore);
94         if (!SetCrypted())
95             return false;
96
97         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
98         for (; mi != mapCryptedKeys.end(); ++mi)
99         {
100             const CPubKey &vchPubKey = (*mi).second.first;
101             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
102             CSecret vchSecret;
103             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
104                 return false;
105             if (vchSecret.size() != 32)
106                 return false;
107             CKey key;
108             key.SetPubKey(vchPubKey);
109             key.SetSecret(vchSecret);
110             if (key.GetPubKey() == vchPubKey)
111                 break;
112             return false;
113         }
114         vMasterKey = vMasterKeyIn;
115     }
116     NotifyStatusChanged(this);
117     return true;
118 }
119
120 bool CCryptoKeyStore::AddKey(const CKey& key)
121 {
122     {
123         LOCK(cs_KeyStore);
124         if (!IsCrypted())
125             return CBasicKeyStore::AddKey(key);
126
127         if (IsLocked())
128             return false;
129
130         std::vector<unsigned char> vchCryptedSecret;
131         CPubKey vchPubKey = key.GetPubKey();
132         bool fCompressed;
133         if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
134             return false;
135
136         if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
137             return false;
138     }
139     return true;
140 }
141
142
143 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
144 {
145     {
146         LOCK(cs_KeyStore);
147         if (!SetCrypted())
148             return false;
149
150         mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
151     }
152     return true;
153 }
154
155 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
156 {
157     {
158         LOCK(cs_KeyStore);
159         if (!IsCrypted())
160             return CBasicKeyStore::GetKey(address, keyOut);
161
162         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
163         if (mi != mapCryptedKeys.end())
164         {
165             const CPubKey &vchPubKey = (*mi).second.first;
166             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
167             CSecret vchSecret;
168             if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
169                 return false;
170             if (vchSecret.size() != 32)
171                 return false;
172             keyOut.SetPubKey(vchPubKey);
173             keyOut.SetSecret(vchSecret);
174             return true;
175         }
176     }
177     return false;
178 }
179
180 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
181 {
182     {
183         LOCK(cs_KeyStore);
184         if (!IsCrypted())
185             return CKeyStore::GetPubKey(address, vchPubKeyOut);
186
187         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
188         if (mi != mapCryptedKeys.end())
189         {
190             vchPubKeyOut = (*mi).second.first;
191             return true;
192         }
193     }
194     return false;
195 }
196
197 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
198 {
199     {
200         LOCK(cs_KeyStore);
201         if (!mapCryptedKeys.empty() || IsCrypted())
202             return false;
203
204         fUseCrypto = true;
205         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
206         {
207             CKey key;
208             if (!key.SetSecret(mKey.second.first, mKey.second.second))
209                 return false;
210             const CPubKey vchPubKey = key.GetPubKey();
211             std::vector<unsigned char> vchCryptedSecret;
212             bool fCompressed;
213             if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
214                 return false;
215             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
216                 return false;
217         }
218         mapKeys.clear();
219     }
220     return true;
221 }