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