65398a179eafd57938509e88c85003b444798a8a
[novacoin.git] / src / keystore.h
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 #ifndef BITCOIN_KEYSTORE_H
6 #define BITCOIN_KEYSTORE_H
7
8 #include "crypter.h"
9 #include "sync.h"
10 #include <boost/signals2/signal.hpp>
11 #include <boost/variant.hpp>
12
13 class CScript;
14
15 class CNoDestination {
16 public:
17     friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
18     friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
19 };
20
21 /** A txout script template with a specific destination. It is either:
22   * CNoDestination: no destination set
23   * CKeyID: TX_PUBKEYHASH destination
24   * CScriptID: TX_SCRIPTHASH destination
25   *
26   * A CTxDestination is the internal data type encoded in a CBitcoinAddress.
27   */
28 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
29
30 /** A virtual base class for key stores */
31 class CKeyStore
32 {
33 protected:
34     mutable CCriticalSection cs_KeyStore;
35
36 public:
37     virtual ~CKeyStore() {}
38
39     // Add a key to the store.
40     virtual bool AddKey(const CKey& key) =0;
41
42     // Add a malleable key to store.
43     virtual bool AddMalleableKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) =0;
44     virtual bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const =0;
45
46     // Check whether a key corresponding to a given address is present in the store.
47     virtual bool HaveKey(const CKeyID &address) const =0;
48     virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
49     virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
50     virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
51
52     // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
53     virtual bool AddCScript(const CScript& redeemScript) =0;
54     virtual bool HaveCScript(const CScriptID &hash) const =0;
55     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
56
57     // Support for Watch-only addresses
58     virtual bool AddWatchOnly(const CScript &dest) =0;
59     virtual bool RemoveWatchOnly(const CScript &dest) =0;
60     virtual bool HaveWatchOnly(const CScript &dest) const =0;
61     virtual bool HaveWatchOnly() const =0;
62
63     virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const
64     {
65         CKey key;
66         if (!GetKey(address, key))
67             return false;
68         vchSecret = key.GetSecret(fCompressed);
69         return true;
70     }
71
72     virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
73     virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const =0;
74     virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
75     virtual void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const =0;
76 };
77
78 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
79 typedef std::map<CScriptID, CScript > ScriptMap;
80 typedef std::set<CScript> WatchOnlySet;
81 typedef std::map<CMalleableKeyView, CSecret> MalleableKeyMap;
82
83 /** Basic key store, that keeps keys in an address->secret map */
84 class CBasicKeyStore : public CKeyStore
85 {
86 protected:
87     KeyMap mapKeys;
88     MalleableKeyMap mapMalleableKeys;
89
90     ScriptMap mapScripts;
91     WatchOnlySet setWatchOnly;
92
93 public:
94     bool AddKey(const CKey& key);
95     bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
96     bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
97     {
98         {
99             LOCK(cs_KeyStore);
100             MalleableKeyMap::const_iterator mi = mapMalleableKeys.find(keyView);
101             if (mi != mapMalleableKeys.end())
102             {
103                 mKey = mi->first.GetMalleableKey(mi->second);
104                 return true;
105             }
106         }
107         return false;
108     }
109
110     bool HaveKey(const CKeyID &address) const
111     {
112         bool result;
113         {
114             LOCK(cs_KeyStore);
115             result = (mapKeys.count(address) > 0);
116         }
117         return result;
118     }
119     void GetKeys(std::set<CKeyID> &setAddress) const
120     {
121         setAddress.clear();
122         {
123             LOCK(cs_KeyStore);
124             KeyMap::const_iterator mi;
125             for (mi = mapKeys.begin(); mi != mapKeys.end(); ++mi) setAddress.insert((*mi).first);
126         }
127     }
128     bool GetKey(const CKeyID &address, CKey &keyOut) const
129     {
130         {
131             LOCK(cs_KeyStore);
132             KeyMap::const_iterator mi = mapKeys.find(address);
133             if (mi != mapKeys.end())
134             {
135                 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
136                 return true;
137             }
138         }
139         return false;
140     }
141     virtual bool AddCScript(const CScript& redeemScript);
142     virtual bool HaveCScript(const CScriptID &hash) const;
143     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
144
145     virtual bool AddWatchOnly(const CScript &dest);
146     virtual bool RemoveWatchOnly(const CScript &dest);
147     virtual bool HaveWatchOnly(const CScript &dest) const;
148     virtual bool HaveWatchOnly() const;
149
150     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
151     {
152         {
153             LOCK(cs_KeyStore);
154             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
155             {
156                 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
157                     return true;
158             }
159         }
160         return false;
161     }
162
163     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
164     {
165         {
166             LOCK(cs_KeyStore);
167             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
168             {
169                 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
170                 {
171                     view = mi->first;
172                     return true;
173                 }
174             }
175         }
176         return false;
177     }
178
179     bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
180     {
181         {
182             LOCK(cs_KeyStore);
183             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
184             {
185                 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
186                 {
187                     CMalleableKey mKey = mi->first.GetMalleableKey(mi->second);
188                     return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);
189                 }
190             }
191         }
192         return false;
193     }
194
195     void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
196     {
197         malleableViewList.clear();
198         {
199             LOCK(cs_KeyStore);
200             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
201                 malleableViewList.push_back(CMalleableKeyView(mi->first));
202         }
203     }
204
205     bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
206     {
207         const CKeyID &mpkID = mpk.GetID();
208         {
209             LOCK(cs_KeyStore);
210             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
211                 if (mi->first.GetID() == mpkID)
212                 {
213                     view = CMalleableKeyView(mi->first);
214                     return true;
215                 }
216         }
217
218         return false;
219     }
220 };
221
222 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
223 typedef std::map<CMalleableKeyView, std::vector<unsigned char> > CryptedMalleableKeyMap;
224
225 /** Keystore which keeps the private keys encrypted.
226  * It derives from the basic key store, which is used if no encryption is active.
227  */
228 class CCryptoKeyStore : public CBasicKeyStore
229 {
230 private:
231     CryptedKeyMap mapCryptedKeys;
232     CryptedMalleableKeyMap mapCryptedMalleableKeys;
233
234     CKeyingMaterial vMasterKey;
235
236     // if fUseCrypto is true, mapKeys must be empty
237     // if fUseCrypto is false, vMasterKey must be empty
238     bool fUseCrypto;
239
240     // keeps track of whether Unlock has run a thorough check before
241     bool fDecryptionThoroughlyChecked;
242
243 protected:
244     bool SetCrypted();
245
246     // will encrypt previously unencrypted keys
247     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
248     bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
249
250     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
251
252 public:
253     CCryptoKeyStore() : fUseCrypto(false), fDecryptionThoroughlyChecked(false) { }
254
255     bool IsCrypted() const
256     {
257         return fUseCrypto;
258     }
259
260     bool IsLocked() const
261     {
262         if (!IsCrypted())
263             return false;
264         bool result;
265         {
266             LOCK(cs_KeyStore);
267             result = vMasterKey.empty();
268         }
269         return result;
270     }
271
272     bool Lock();
273
274     virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
275     virtual bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char>  &vchCryptedSecretH);
276
277     bool AddKey(const CKey& key);
278     bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
279     bool HaveKey(const CKeyID &address) const
280     {
281         {
282             LOCK(cs_KeyStore);
283             if (!IsCrypted())
284                 return CBasicKeyStore::HaveKey(address);
285             return mapCryptedKeys.count(address) > 0;
286         }
287     }
288     bool GetKey(const CKeyID &address, CKey& keyOut) const;
289     bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
290     void GetKeys(std::set<CKeyID> &setAddress) const
291     {
292         if (!IsCrypted())
293         {
294             CBasicKeyStore::GetKeys(setAddress);
295             return;
296         }
297         setAddress.clear();
298         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
299         while (mi != mapCryptedKeys.end())
300         {
301             setAddress.insert((*mi).first);
302             mi++;
303         }
304     }
305
306     bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
307
308     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
309     {
310         {
311             LOCK(cs_KeyStore);
312             if (!IsCrypted())
313                 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R);
314             for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
315             {
316                 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
317                     return true;
318             }
319         }
320         return false;
321     }
322
323     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
324     {
325         {
326             LOCK(cs_KeyStore);
327             if (!IsCrypted())
328                 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R, view);
329             for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
330             {
331                 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
332                 {
333                     view = mi->first;
334                     return true;
335                 }
336             }
337         }
338         return false;
339     }
340
341     bool CheckOwnership(const CMalleablePubKey &mpk)
342     {
343         CMalleableKeyView view;
344         return GetMalleableView(mpk, view);
345     }
346
347     bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
348
349     void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
350     {
351         malleableViewList.clear();
352         {
353             LOCK(cs_KeyStore);
354             if (!IsCrypted())
355                 return CBasicKeyStore::ListMalleableViews(malleableViewList);
356             for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
357                 malleableViewList.push_back(CMalleableKeyView(mi->first));
358         }
359     }
360
361     bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
362     {
363         const CKeyID &mpkID = mpk.GetID();
364         {
365             LOCK(cs_KeyStore);
366             if (!IsCrypted())
367                 return CBasicKeyStore::GetMalleableView(mpk, view);
368             for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
369                 if (mi->first.GetID() == mpkID)
370                 {
371                     view = CMalleableKeyView(mi->first);
372                     return true;
373                 }
374         }
375
376         return false;
377     }
378
379     /* Wallet status (encrypted, locked) changed.
380      * Note: Called without locks held.
381      */
382     boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
383 };
384
385 #endif