RPC: Add new methods suitable for malleable key pairs management;
[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 CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
74     virtual void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const =0;
75 };
76
77 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
78 typedef std::map<CScriptID, CScript > ScriptMap;
79 typedef std::set<CScript> WatchOnlySet;
80 typedef std::map<CMalleableKeyView, CMalleableKey> MalleableKeyMap;
81
82 /** Basic key store, that keeps keys in an address->secret map */
83 class CBasicKeyStore : public CKeyStore
84 {
85 protected:
86     KeyMap mapKeys;
87     MalleableKeyMap mapMalleableKeys;
88
89     ScriptMap mapScripts;
90     WatchOnlySet setWatchOnly;
91
92 public:
93     bool AddKey(const CKey& key);
94     bool AddMalleableKey(const CMalleableKey& mKey);
95     bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
96     {
97         {
98             LOCK(cs_KeyStore);
99             MalleableKeyMap::const_iterator mi = mapMalleableKeys.find(keyView);
100             if (mi != mapMalleableKeys.end())
101             {
102                 mKey = mi->second;
103                 return true;
104             }
105         }
106         return false;
107     }
108
109     bool HaveKey(const CKeyID &address) const
110     {
111         bool result;
112         {
113             LOCK(cs_KeyStore);
114             result = (mapKeys.count(address) > 0);
115         }
116         return result;
117     }
118     void GetKeys(std::set<CKeyID> &setAddress) const
119     {
120         setAddress.clear();
121         {
122             LOCK(cs_KeyStore);
123             KeyMap::const_iterator mi = mapKeys.begin();
124             while (mi != mapKeys.end())
125             {
126                 setAddress.insert((*mi).first);
127                 mi++;
128             }
129         }
130     }
131     bool GetKey(const CKeyID &address, CKey &keyOut) const
132     {
133         {
134             LOCK(cs_KeyStore);
135             KeyMap::const_iterator mi = mapKeys.find(address);
136             if (mi != mapKeys.end())
137             {
138                 keyOut.Reset();
139                 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
140                 return true;
141             }
142         }
143         return false;
144     }
145     virtual bool AddCScript(const CScript& redeemScript);
146     virtual bool HaveCScript(const CScriptID &hash) const;
147     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
148
149     virtual bool AddWatchOnly(const CScript &dest);
150     virtual bool RemoveWatchOnly(const CScript &dest);
151     virtual bool HaveWatchOnly(const CScript &dest) const;
152     virtual bool HaveWatchOnly() const;
153
154     bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
155     {
156         {
157             LOCK(cs_KeyStore);
158             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
159             {
160                 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
161                     return true;
162             }
163         }
164         return false;
165     }
166
167     bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
168     {
169         {
170             LOCK(cs_KeyStore);
171             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
172             {
173                 if (mi->second.CheckKeyVariant(R, pubKeyVariant, privKey))
174                     return true;
175             }
176         }
177         return false;
178     }
179
180     void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
181     {
182         malleableViewList.clear();
183
184         {
185             LOCK(cs_KeyStore);
186             for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
187                 malleableViewList.push_back(CMalleableKeyView(mi->first));
188         }
189     }
190 };
191
192 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
193
194 /** Keystore which keeps the private keys encrypted.
195  * It derives from the basic key store, which is used if no encryption is active.
196  */
197 class CCryptoKeyStore : public CBasicKeyStore
198 {
199 private:
200     CryptedKeyMap mapCryptedKeys;
201
202     CKeyingMaterial vMasterKey;
203
204     // if fUseCrypto is true, mapKeys must be empty
205     // if fUseCrypto is false, vMasterKey must be empty
206     bool fUseCrypto;
207
208 protected:
209     bool SetCrypted();
210
211     // will encrypt previously unencrypted keys
212     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
213     bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
214
215     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
216
217 public:
218     CCryptoKeyStore() : fUseCrypto(false) { }
219
220     bool IsCrypted() const
221     {
222         return fUseCrypto;
223     }
224
225     bool IsLocked() const
226     {
227         if (!IsCrypted())
228             return false;
229         bool result;
230         {
231             LOCK(cs_KeyStore);
232             result = vMasterKey.empty();
233         }
234         return result;
235     }
236
237     bool Lock();
238
239     virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
240     bool AddKey(const CKey& key);
241     bool HaveKey(const CKeyID &address) const
242     {
243         {
244             LOCK(cs_KeyStore);
245             if (!IsCrypted())
246                 return CBasicKeyStore::HaveKey(address);
247             return mapCryptedKeys.count(address) > 0;
248         }
249     }
250     bool GetKey(const CKeyID &address, CKey& keyOut) const;
251     bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
252     void GetKeys(std::set<CKeyID> &setAddress) const
253     {
254         if (!IsCrypted())
255         {
256             CBasicKeyStore::GetKeys(setAddress);
257             return;
258         }
259         setAddress.clear();
260         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
261         while (mi != mapCryptedKeys.end())
262         {
263             setAddress.insert((*mi).first);
264             mi++;
265         }
266     }
267
268     /* Wallet status (encrypted, locked) changed.
269      * Note: Called without locks held.
270      */
271     boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
272 };
273
274 #endif