Remove msvc warnings.
[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
69 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
70 typedef std::map<CScriptID, CScript > ScriptMap;
71 typedef std::set<CScript> WatchOnlySet;
72
73 /** Basic key store, that keeps keys in an address->secret map */
74 class CBasicKeyStore : public CKeyStore
75 {
76 protected:
77     KeyMap mapKeys;
78     ScriptMap mapScripts;
79     WatchOnlySet setWatchOnly;
80
81 public:
82     bool AddKey(const CKey& key);
83     bool HaveKey(const CKeyID &address) const
84     {
85         bool result;
86         {
87             LOCK(cs_KeyStore);
88             result = (mapKeys.count(address) > 0);
89         }
90         return result;
91     }
92     void GetKeys(std::set<CKeyID> &setAddress) const
93     {
94         setAddress.clear();
95         {
96             LOCK(cs_KeyStore);
97             KeyMap::const_iterator mi = mapKeys.begin();
98             while (mi != mapKeys.end())
99             {
100                 setAddress.insert((*mi).first);
101                 mi++;
102             }
103         }
104     }
105     bool GetKey(const CKeyID &address, CKey &keyOut) const
106     {
107         {
108             LOCK(cs_KeyStore);
109             KeyMap::const_iterator mi = mapKeys.find(address);
110             if (mi != mapKeys.end())
111             {
112                 keyOut.Reset();
113                 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
114                 return true;
115             }
116         }
117         return false;
118     }
119     virtual bool AddCScript(const CScript& redeemScript);
120     virtual bool HaveCScript(const CScriptID &hash) const;
121     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
122
123     virtual bool AddWatchOnly(const CScript &dest);
124     virtual bool RemoveWatchOnly(const CScript &dest);
125     virtual bool HaveWatchOnly(const CScript &dest) const;
126     virtual bool HaveWatchOnly() const;
127 };
128
129 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
130
131 /** Keystore which keeps the private keys encrypted.
132  * It derives from the basic key store, which is used if no encryption is active.
133  */
134 class CCryptoKeyStore : public CBasicKeyStore
135 {
136 private:
137     CryptedKeyMap mapCryptedKeys;
138
139     CKeyingMaterial vMasterKey;
140
141     // if fUseCrypto is true, mapKeys must be empty
142     // if fUseCrypto is false, vMasterKey must be empty
143     bool fUseCrypto;
144
145 protected:
146     bool SetCrypted();
147
148     // will encrypt previously unencrypted keys
149     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
150     bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
151
152     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
153
154 public:
155     CCryptoKeyStore() : fUseCrypto(false)
156     {
157     }
158
159     bool IsCrypted() const
160     {
161         return fUseCrypto;
162     }
163
164     bool IsLocked() const
165     {
166         if (!IsCrypted())
167             return false;
168         bool result;
169         {
170             LOCK(cs_KeyStore);
171             result = vMasterKey.empty();
172         }
173         return result;
174     }
175
176     bool Lock();
177
178     virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
179     bool AddKey(const CKey& key);
180     bool HaveKey(const CKeyID &address) const
181     {
182         {
183             LOCK(cs_KeyStore);
184             if (!IsCrypted())
185                 return CBasicKeyStore::HaveKey(address);
186             return mapCryptedKeys.count(address) > 0;
187         }
188     }
189     bool GetKey(const CKeyID &address, CKey& keyOut) const;
190     bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
191     void GetKeys(std::set<CKeyID> &setAddress) const
192     {
193         if (!IsCrypted())
194         {
195             CBasicKeyStore::GetKeys(setAddress);
196             return;
197         }
198         setAddress.clear();
199         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
200         while (mi != mapCryptedKeys.end())
201         {
202             setAddress.insert((*mi).first);
203             mi++;
204         }
205     }
206
207     /* Wallet status (encrypted, locked) changed.
208      * Note: Called without locks held.
209      */
210     boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
211 };
212
213 #endif