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