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