4095535493545aedbd86bc196d084e03870e3e3e
[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 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CMasterKey;
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 std::vector<unsigned char> &vchPubKey) const =0;
16     virtual bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const =0;
17     virtual std::vector<unsigned char> GenerateNewKey();
18 };
19
20 class CBasicKeyStore : public CKeyStore
21 {
22 protected:
23     std::map<std::vector<unsigned char>, CPrivKey> mapKeys;
24
25 public:
26     bool AddKey(const CKey& key);
27     bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
28     {
29         return (mapKeys.count(vchPubKey) > 0);
30     }
31     bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const
32     {
33         std::map<std::vector<unsigned char>, CPrivKey>::const_iterator mi = mapKeys.find(vchPubKey);
34         if (mi != mapKeys.end())
35         {
36             keyOut = (*mi).second;
37             return true;
38         }
39         return false;
40     }
41 };
42
43 class CCryptoKeyStore : public CBasicKeyStore
44 {
45 private:
46     std::map<std::vector<unsigned char>, std::vector<unsigned char> > mapCryptedKeys;
47
48     CMasterKey vMasterKey;
49
50     // if fUseCrypto is true, mapKeys must be empty
51     // if fUseCrypto is false, vMasterKey must be empty
52     bool fUseCrypto;
53
54 protected:
55     bool IsCrypted() const
56     {
57         return fUseCrypto;
58     }
59
60     bool SetCrypted()
61     {
62         if (fUseCrypto)
63             return true;
64         if (!mapKeys.empty())
65             return false;
66         fUseCrypto = true;
67     }
68
69     // will encrypt previously unencrypted keys
70     bool GenerateMasterKey();
71
72     bool GetMasterKey(CMasterKey &vMasterKeyOut) const
73     {
74         if (!IsCrypted())
75             return false;
76         if (IsLocked())
77             return false;
78         vMasterKeyOut = vMasterKey;
79         return true;
80     }
81     bool Unlock(const CMasterKey& vMasterKeyIn);
82
83 public:
84     CCryptoKeyStore() : fUseCrypto(false)
85     {
86     }
87
88     bool IsLocked() const
89     {
90         if (!IsCrypted())
91             return false;
92         return vMasterKey.empty();
93     }
94
95     bool Lock()
96     {
97         if (!SetCrypted())
98             return false;
99         vMasterKey.clear();
100     }
101
102     virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
103     bool AddKey(const CKey& key);
104     bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
105     {
106         if (!IsCrypted())
107             return CBasicKeyStore::HaveKey(vchPubKey);
108         return mapCryptedKeys.count(vchPubKey) > 0;
109     }
110     bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const;
111 };
112
113 #endif