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