Add GetSecret() and GetKeys() to CKeyStore
[novacoin.git] / src / keystore.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt 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
10 // A virtual base class for key stores
11 class CKeyStore
12 {
13 protected:
14     mutable CCriticalSection cs_KeyStore;
15
16 public:
17     // Add a key to the store.
18     virtual bool AddKey(const CKey& key) =0;
19
20     // Check whether a key corresponding to a given address is present in the store.
21     virtual bool HaveKey(const CBitcoinAddress &address) const =0;
22     virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const 
23     {
24         CSecret vchSecret;
25         if (!GetSecret(address, vchSecret))
26             return false;
27         if (!keyOut.SetSecret(vchSecret))
28             return false;
29         return true;
30     }
31     virtual void GetKeys(std::set<CBitcoinAddress> &setAddress) const =0;
32     virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
33
34     // Generate a new key, and add it to the store
35     virtual std::vector<unsigned char> GenerateNewKey();
36     virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const
37     {
38         CKey key;
39         if (!GetKey(address, key))
40             return false;
41         vchSecret = key.GetSecret();
42         return true;
43     }
44 };
45
46 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
47
48 // Basic key store, that keeps keys in an address->secret map
49 class CBasicKeyStore : public CKeyStore
50 {
51 protected:
52     KeyMap mapKeys;
53
54 public:
55     bool AddKey(const CKey& key);
56     bool HaveKey(const CBitcoinAddress &address) const
57     {
58         bool result;
59         CRITICAL_BLOCK(cs_KeyStore)
60             result = (mapKeys.count(address) > 0);
61         return result;
62     }
63     void GetKeys(std::set<CBitcoinAddress> &setAddress) const
64     {
65         setAddress.clear();
66         CRITICAL_BLOCK(cs_KeyStore)
67         {
68             KeyMap::const_iterator mi = mapKeys.begin();
69             while (mi != mapKeys.end())
70             {
71                 setAddress.insert((*mi).first);
72                 mi++;
73             }
74         }
75     }
76     bool GetSecret(const CBitcoinAddress &address, CSecret &vchSecret) const
77     {
78         CRITICAL_BLOCK(cs_KeyStore)
79         {
80             KeyMap::const_iterator mi = mapKeys.find(address);
81             if (mi != mapKeys.end())
82             {
83                 vchSecret = (*mi).second;
84                 return true;
85             }
86         }
87         return false;
88     }
89 };
90
91 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, 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 class CCryptoKeyStore : public CBasicKeyStore
96 {
97 private:
98     CryptedKeyMap mapCryptedKeys;
99
100     CKeyingMaterial vMasterKey;
101
102     // if fUseCrypto is true, mapKeys must be empty
103     // if fUseCrypto is false, vMasterKey must be empty
104     bool fUseCrypto;
105
106 protected:
107     bool SetCrypted();
108
109     // will encrypt previously unencrypted keys
110     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
111
112     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
113
114 public:
115     CCryptoKeyStore() : fUseCrypto(false)
116     {
117     }
118
119     bool IsCrypted() const
120     {
121         return fUseCrypto;
122     }
123
124     bool IsLocked() const
125     {
126         if (!IsCrypted())
127             return false;
128         bool result;
129         CRITICAL_BLOCK(cs_KeyStore)
130             result = vMasterKey.empty();
131         return result;
132     }
133
134     bool Lock()
135     {
136         if (!SetCrypted())
137             return false;
138
139         CRITICAL_BLOCK(cs_KeyStore)
140             vMasterKey.clear();
141
142         return true;
143     }
144
145     virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
146     std::vector<unsigned char> GenerateNewKey();
147     bool AddKey(const CKey& key);
148     bool HaveKey(const CBitcoinAddress &address) const
149     {
150         CRITICAL_BLOCK(cs_KeyStore)
151         {
152             if (!IsCrypted())
153                 return CBasicKeyStore::HaveKey(address);
154             return mapCryptedKeys.count(address) > 0;
155         }
156         return false;
157     }
158     bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const;
159     bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
160     void GetKeys(std::set<CBitcoinAddress> &setAddress) const
161     {
162         if (!IsCrypted())
163         {
164             CBasicKeyStore::GetKeys(setAddress);
165             return;
166         }
167         setAddress.clear();
168         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
169         while (mi != mapCryptedKeys.end())
170         {
171             setAddress.insert((*mi).first);
172             mi++;
173         }
174     }
175 };
176
177 #endif