Add CWallet::ExtractAddress method
[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 CMalleableKey& mKey) =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, CMalleableKey> 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 CMalleableKey& mKey);
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->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.Reset();
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->second.CheckKeyVariant(R, pubKeyVariant, privKey))
187                     return true;
188             }
189         }
190         return false;
191     }
192
193     void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
194     {
195         malleableViewList.clear();
196
197         {
198             LOCK(cs_KeyStore);
199             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
200                 malleableViewList.push_back(CMalleableKeyView(mi->first));
201         }
202     }
203
204     bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
205     {
206         const CKeyID &mpkID = mpk.GetID();
207         {
208             LOCK(cs_KeyStore);
209             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
210                 if (mi->first.GetID() == mpkID)
211                 {
212                     view = CMalleableKeyView(mi->first);
213                     return true;
214                 }
215         }
216
217         return false;
218     }
219 };
220
221 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
222
223 /** Keystore which keeps the private keys encrypted.
224  * It derives from the basic key store, which is used if no encryption is active.
225  */
226 class CCryptoKeyStore : public CBasicKeyStore
227 {
228 private:
229     CryptedKeyMap mapCryptedKeys;
230
231     CKeyingMaterial vMasterKey;
232
233     // if fUseCrypto is true, mapKeys must be empty
234     // if fUseCrypto is false, vMasterKey must be empty
235     bool fUseCrypto;
236
237 protected:
238     bool SetCrypted();
239
240     // will encrypt previously unencrypted keys
241     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
242     bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
243
244     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
245
246 public:
247     CCryptoKeyStore() : fUseCrypto(false) { }
248
249     bool IsCrypted() const
250     {
251         return fUseCrypto;
252     }
253
254     bool IsLocked() const
255     {
256         if (!IsCrypted())
257             return false;
258         bool result;
259         {
260             LOCK(cs_KeyStore);
261             result = vMasterKey.empty();
262         }
263         return result;
264     }
265
266     bool Lock();
267
268     virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
269     bool AddKey(const CKey& key);
270     bool HaveKey(const CKeyID &address) const
271     {
272         {
273             LOCK(cs_KeyStore);
274             if (!IsCrypted())
275                 return CBasicKeyStore::HaveKey(address);
276             return mapCryptedKeys.count(address) > 0;
277         }
278     }
279     bool GetKey(const CKeyID &address, CKey& keyOut) const;
280     bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
281     void GetKeys(std::set<CKeyID> &setAddress) const
282     {
283         if (!IsCrypted())
284         {
285             CBasicKeyStore::GetKeys(setAddress);
286             return;
287         }
288         setAddress.clear();
289         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
290         while (mi != mapCryptedKeys.end())
291         {
292             setAddress.insert((*mi).first);
293             mi++;
294         }
295     }
296
297     /* Wallet status (encrypted, locked) changed.
298      * Note: Called without locks held.
299      */
300     boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
301 };
302
303 #endif