]> Git Repo - VerusCoin.git/blob - src/keystore.h
Merge pull request #3117 from gavinandresen/debuglockprint
[VerusCoin.git] / src / keystore.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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
12 class CScript;
13
14 /** A virtual base class for key stores */
15 class CKeyStore
16 {
17 protected:
18     mutable CCriticalSection cs_KeyStore;
19
20 public:
21     virtual ~CKeyStore() {}
22
23     // Add a key to the store.
24     virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
25     virtual bool AddKey(const CKey &key);
26
27     // Check whether a key corresponding to a given address is present in the store.
28     virtual bool HaveKey(const CKeyID &address) const =0;
29     virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
30     virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
31     virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
32
33     // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
34     virtual bool AddCScript(const CScript& redeemScript) =0;
35     virtual bool HaveCScript(const CScriptID &hash) const =0;
36     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
37 };
38
39 typedef std::map<CKeyID, CKey> KeyMap;
40 typedef std::map<CScriptID, CScript > ScriptMap;
41
42 /** Basic key store, that keeps keys in an address->secret map */
43 class CBasicKeyStore : public CKeyStore
44 {
45 protected:
46     KeyMap mapKeys;
47     ScriptMap mapScripts;
48
49 public:
50     bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
51     bool HaveKey(const CKeyID &address) const
52     {
53         bool result;
54         {
55             LOCK(cs_KeyStore);
56             result = (mapKeys.count(address) > 0);
57         }
58         return result;
59     }
60     void GetKeys(std::set<CKeyID> &setAddress) const
61     {
62         setAddress.clear();
63         {
64             LOCK(cs_KeyStore);
65             KeyMap::const_iterator mi = mapKeys.begin();
66             while (mi != mapKeys.end())
67             {
68                 setAddress.insert((*mi).first);
69                 mi++;
70             }
71         }
72     }
73     bool GetKey(const CKeyID &address, CKey &keyOut) const
74     {
75         {
76             LOCK(cs_KeyStore);
77             KeyMap::const_iterator mi = mapKeys.find(address);
78             if (mi != mapKeys.end())
79             {
80                 keyOut = mi->second;
81                 return true;
82             }
83         }
84         return false;
85     }
86     virtual bool AddCScript(const CScript& redeemScript);
87     virtual bool HaveCScript(const CScriptID &hash) const;
88     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
89 };
90
91 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
92
93 /** Keystore which keeps the private keys encrypted.
94  * It derives from the basic key store, which is used if no encryption is active.
95  */
96 class CCryptoKeyStore : public CBasicKeyStore
97 {
98 private:
99     CryptedKeyMap mapCryptedKeys;
100
101     CKeyingMaterial vMasterKey;
102
103     // if fUseCrypto is true, mapKeys must be empty
104     // if fUseCrypto is false, vMasterKey must be empty
105     bool fUseCrypto;
106
107 protected:
108     bool SetCrypted();
109
110     // will encrypt previously unencrypted keys
111     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
112
113     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
114
115 public:
116     CCryptoKeyStore() : fUseCrypto(false)
117     {
118     }
119
120     bool IsCrypted() const
121     {
122         return fUseCrypto;
123     }
124
125     bool IsLocked() const
126     {
127         if (!IsCrypted())
128             return false;
129         bool result;
130         {
131             LOCK(cs_KeyStore);
132             result = vMasterKey.empty();
133         }
134         return result;
135     }
136
137     bool Lock();
138
139     virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
140     bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
141     bool HaveKey(const CKeyID &address) const
142     {
143         {
144             LOCK(cs_KeyStore);
145             if (!IsCrypted())
146                 return CBasicKeyStore::HaveKey(address);
147             return mapCryptedKeys.count(address) > 0;
148         }
149         return false;
150     }
151     bool GetKey(const CKeyID &address, CKey& keyOut) const;
152     bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
153     void GetKeys(std::set<CKeyID> &setAddress) const
154     {
155         if (!IsCrypted())
156         {
157             CBasicKeyStore::GetKeys(setAddress);
158             return;
159         }
160         setAddress.clear();
161         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
162         while (mi != mapCryptedKeys.end())
163         {
164             setAddress.insert((*mi).first);
165             mi++;
166         }
167     }
168
169     /* Wallet status (encrypted, locked) changed.
170      * Note: Called without locks held.
171      */
172     boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
173 };
174
175 #endif
This page took 0.033493 seconds and 4 git commands to generate.