436053a9e3fa9c9147980c159d488e2cf5a8d739
[novacoin.git] / src / keystore.h
1 // Copyright (c) 2009-2011 Satoshi Nakamoto & Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef BITCOIN_KEYSTORE_H
5 #define BITCOIN_KEYSTORE_H
6
7 #include "crypter.h"
8
9 class CKeyStore
10 {
11 public:
12     mutable CCriticalSection cs_KeyStore;
13
14     virtual bool AddKey(const CKey& key) =0;
15     virtual bool HaveKey(const CBitcoinAddress &address) const =0;
16     virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
17     virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
18     virtual std::vector<unsigned char> GenerateNewKey();
19 };
20
21 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
22
23 class CBasicKeyStore : public CKeyStore
24 {
25 protected:
26     KeyMap mapKeys;
27
28 public:
29     bool AddKey(const CKey& key);
30     bool HaveKey(const CBitcoinAddress &address) const
31     {
32         return (mapKeys.count(address) > 0);
33     }
34     bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
35     {
36         KeyMap::const_iterator mi = mapKeys.find(address);
37         if (mi != mapKeys.end())
38         {
39             keyOut.SetSecret((*mi).second);
40             return true;
41         }
42         return false;
43     }
44 };
45
46 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
47
48 class CCryptoKeyStore : public CBasicKeyStore
49 {
50 private:
51     CryptedKeyMap mapCryptedKeys;
52
53     CKeyingMaterial vMasterKey;
54
55     // if fUseCrypto is true, mapKeys must be empty
56     // if fUseCrypto is false, vMasterKey must be empty
57     bool fUseCrypto;
58
59 protected:
60     bool SetCrypted()
61     {
62         if (fUseCrypto)
63             return true;
64         if (!mapKeys.empty())
65             return false;
66         fUseCrypto = true;
67         return true;
68     }
69
70     // will encrypt previously unencrypted keys
71     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
72
73     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
74
75 public:
76     mutable CCriticalSection cs_vMasterKey; //No guarantees master key wont get locked before you can use it, so lock this first
77
78     CCryptoKeyStore() : fUseCrypto(false)
79     {
80     }
81
82     bool IsCrypted() const
83     {
84         return fUseCrypto;
85     }
86
87     bool IsLocked() const
88     {
89         if (!IsCrypted())
90             return false;
91         return vMasterKey.empty();
92     }
93
94     bool Lock()
95     {
96         CRITICAL_BLOCK(cs_vMasterKey)
97         {
98             if (!SetCrypted())
99                 return false;
100
101             vMasterKey.clear();
102         }
103         return true;
104     }
105
106     virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
107     std::vector<unsigned char> GenerateNewKey();
108     bool AddKey(const CKey& key);
109     bool HaveKey(const CBitcoinAddress &address) const
110     {
111         if (!IsCrypted())
112             return CBasicKeyStore::HaveKey(address);
113         return mapCryptedKeys.count(address) > 0;
114     }
115     bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
116     bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
117 };
118
119 #endif