Bugfix: Unspendable inputs handling
[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 CScript &dest)
67 {
68     LOCK(cs_KeyStore);
69
70     CTxDestination address;
71     if (ExtractDestination(dest, address)) {
72         CKeyID keyID;
73         CBitcoinAddress(address).GetKeyID(keyID);
74         if (HaveKey(keyID))
75             return false;
76     }
77
78     setWatchOnly.insert(dest);
79     return true;
80 }
81
82 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
83 {
84     LOCK(cs_KeyStore);
85     return setWatchOnly.count(dest) > 0;
86 }
87
88 bool CCryptoKeyStore::SetCrypted()
89 {
90     {
91         LOCK(cs_KeyStore);
92         if (fUseCrypto)
93             return true;
94         if (!mapKeys.empty())
95             return false;
96         fUseCrypto = true;
97     }
98     return true;
99 }
100
101 bool CCryptoKeyStore::Lock()
102 {
103     if (!SetCrypted())
104         return false;
105
106     {
107         LOCK(cs_KeyStore);
108         vMasterKey.clear();
109         fWalletUnlockMintOnly = false;
110     }
111
112     NotifyStatusChanged(this);
113     return true;
114 }
115
116 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
117 {
118     {
119         LOCK(cs_KeyStore);
120         if (!SetCrypted())
121             return false;
122
123         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
124         for (; mi != mapCryptedKeys.end(); ++mi)
125         {
126             const CPubKey &vchPubKey = (*mi).second.first;
127             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
128             CSecret vchSecret;
129             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
130                 return false;
131             if (vchSecret.size() != 32)
132                 return false;
133             CKey key;
134             key.SetPubKey(vchPubKey);
135             key.SetSecret(vchSecret);
136             if (key.GetPubKey() == vchPubKey)
137                 break;
138             return false;
139         }
140         vMasterKey = vMasterKeyIn;
141     }
142     NotifyStatusChanged(this);
143     return true;
144 }
145
146 bool CCryptoKeyStore::AddKey(const CKey& key)
147 {
148     {
149         LOCK(cs_KeyStore);
150
151         CScript script;
152         script.SetDestination(key.GetPubKey().GetID());
153
154         if (HaveWatchOnly(script))
155             return false;
156
157         if (!IsCrypted())
158             return CBasicKeyStore::AddKey(key);
159
160         if (IsLocked())
161             return false;
162
163         std::vector<unsigned char> vchCryptedSecret;
164         CPubKey vchPubKey = key.GetPubKey();
165         bool fCompressed;
166         if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
167             return false;
168
169         if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
170             return false;
171     }
172     return true;
173 }
174
175
176 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
177 {
178     {
179         LOCK(cs_KeyStore);
180         if (!SetCrypted())
181             return false;
182
183         mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
184     }
185     return true;
186 }
187
188 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
189 {
190     {
191         LOCK(cs_KeyStore);
192         if (!IsCrypted())
193             return CBasicKeyStore::GetKey(address, keyOut);
194
195         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
196         if (mi != mapCryptedKeys.end())
197         {
198             const CPubKey &vchPubKey = (*mi).second.first;
199             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
200             CSecret vchSecret;
201             if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
202                 return false;
203             if (vchSecret.size() != 32)
204                 return false;
205             keyOut.SetPubKey(vchPubKey);
206             keyOut.SetSecret(vchSecret);
207             return true;
208         }
209     }
210     return false;
211 }
212
213 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
214 {
215     {
216         LOCK(cs_KeyStore);
217         if (!IsCrypted())
218             return CKeyStore::GetPubKey(address, vchPubKeyOut);
219
220         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
221         if (mi != mapCryptedKeys.end())
222         {
223             vchPubKeyOut = (*mi).second.first;
224             return true;
225         }
226     }
227     return false;
228 }
229
230 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
231 {
232     {
233         LOCK(cs_KeyStore);
234         if (!mapCryptedKeys.empty() || IsCrypted())
235             return false;
236
237         fUseCrypto = true;
238         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
239         {
240             CKey key;
241             if (!key.SetSecret(mKey.second.first, mKey.second.second))
242                 return false;
243             const CPubKey vchPubKey = key.GetPubKey();
244             std::vector<unsigned char> vchCryptedSecret;
245             bool fCompressed;
246             if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
247                 return false;
248             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
249                 return false;
250         }
251         mapKeys.clear();
252     }
253     return true;
254 }