7d49735db9d22bdaedbb572f09376ad5acdbabf9
[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     // Check whether a key corresponding to a given address is present in the store.
43     virtual bool HaveKey(const CKeyID &address) const =0;
44     virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
45     virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
46     virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
47
48     // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
49     virtual bool AddCScript(const CScript& redeemScript) =0;
50     virtual bool HaveCScript(const CScriptID &hash) const =0;
51     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
52
53     // Support for Watch-only addresses
54     virtual bool AddWatchOnly(const CScript &dest) =0;
55     virtual bool RemoveWatchOnly(const CScript &dest) =0;
56     virtual bool HaveWatchOnly(const CScript &dest) const =0;
57     virtual bool HaveWatchOnly() const =0;
58
59     virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const
60     {
61         CKey key;
62         if (!GetKey(address, key))
63             return false;
64         vchSecret = key.GetSecret(fCompressed);
65         return true;
66     }
67
68     virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
69     virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
70     virtual void ListMalleablePubKeys(std::list<CMalleablePubKey> &malleablePubKeyList) const =0;
71 };
72
73 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
74 typedef std::map<CScriptID, CScript > ScriptMap;
75 typedef std::set<CScript> WatchOnlySet;
76 typedef std::map<CMalleableKeyView, CMalleableKey> MalleableKeyMap;
77
78 /** Basic key store, that keeps keys in an address->secret map */
79 class CBasicKeyStore : public CKeyStore
80 {
81 protected:
82     KeyMap mapKeys;
83     MalleableKeyMap mapMalleableKeys;
84
85     ScriptMap mapScripts;
86     WatchOnlySet setWatchOnly;
87
88 public:
89     bool AddKey(const CKey& key);
90     bool HaveKey(const CKeyID &address) const
91     {
92         bool result;
93         {
94             LOCK(cs_KeyStore);
95             result = (mapKeys.count(address) > 0);
96         }
97         return result;
98     }
99     void GetKeys(std::set<CKeyID> &setAddress) const
100     {
101         setAddress.clear();
102         {
103             LOCK(cs_KeyStore);
104             KeyMap::const_iterator mi = mapKeys.begin();
105             while (mi != mapKeys.end())
106             {
107                 setAddress.insert((*mi).first);
108                 mi++;
109             }
110         }
111     }
112     bool GetKey(const CKeyID &address, CKey &keyOut) const
113     {
114         {
115             LOCK(cs_KeyStore);
116             KeyMap::const_iterator mi = mapKeys.find(address);
117             if (mi != mapKeys.end())
118             {
119                 keyOut.Reset();
120                 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
121                 return true;
122             }
123         }
124         return false;
125     }
126     virtual bool AddCScript(const CScript& redeemScript);
127     virtual bool HaveCScript(const CScriptID &hash) const;
128     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
129
130     virtual bool AddWatchOnly(const CScript &dest);
131     virtual bool RemoveWatchOnly(const CScript &dest);
132     virtual bool HaveWatchOnly(const CScript &dest) const;
133     virtual bool HaveWatchOnly() const;
134
135     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
136     {
137         {
138             LOCK(cs_KeyStore);
139             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
140             {
141                 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
142                     return true;
143             }
144         }
145         return false;
146     }
147
148     bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
149     {
150         {
151             LOCK(cs_KeyStore);
152             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
153             {
154                 if (mi->second.CheckKeyVariant(R, pubKeyVariant, privKey))
155                     return true;
156             }
157         }
158         return false;
159     }
160
161     void ListMalleablePubKeys(std::list<CMalleablePubKey> &malleablePubKeyList) const
162     {
163         malleablePubKeyList.clear();
164
165         {
166             LOCK(cs_KeyStore);
167             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
168                 malleablePubKeyList.push_back(mi->first.GetMalleablePubKey());
169         }
170     }
171 };
172
173 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
174
175 /** Keystore which keeps the private keys encrypted.
176  * It derives from the basic key store, which is used if no encryption is active.
177  */
178 class CCryptoKeyStore : public CBasicKeyStore
179 {
180 private:
181     CryptedKeyMap mapCryptedKeys;
182
183     CKeyingMaterial vMasterKey;
184
185     // if fUseCrypto is true, mapKeys must be empty
186     // if fUseCrypto is false, vMasterKey must be empty
187     bool fUseCrypto;
188
189 protected:
190     bool SetCrypted();
191
192     // will encrypt previously unencrypted keys
193     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
194     bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
195
196     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
197
198 public:
199     CCryptoKeyStore();
200
201     bool IsCrypted() const
202     {
203         return fUseCrypto;
204     }
205
206     bool IsLocked() const
207     {
208         if (!IsCrypted())
209             return false;
210         bool result;
211         {
212             LOCK(cs_KeyStore);
213             result = vMasterKey.empty();
214         }
215         return result;
216     }
217
218     bool Lock();
219
220     virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
221     bool AddKey(const CKey& key);
222     bool HaveKey(const CKeyID &address) const
223     {
224         {
225             LOCK(cs_KeyStore);
226             if (!IsCrypted())
227                 return CBasicKeyStore::HaveKey(address);
228             return mapCryptedKeys.count(address) > 0;
229         }
230     }
231     bool GetKey(const CKeyID &address, CKey& keyOut) const;
232     bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
233     void GetKeys(std::set<CKeyID> &setAddress) const
234     {
235         if (!IsCrypted())
236         {
237             CBasicKeyStore::GetKeys(setAddress);
238             return;
239         }
240         setAddress.clear();
241         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
242         while (mi != mapCryptedKeys.end())
243         {
244             setAddress.insert((*mi).first);
245             mi++;
246         }
247     }
248
249     /* Wallet status (encrypted, locked) changed.
250      * Note: Called without locks held.
251      */
252     boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
253 };
254
255 #endif