Update License in File Headers
[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 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
10 class CKeyStore
11 {
12 protected:
13     mutable CCriticalSection cs_KeyStore;
14
15 public:
16     virtual ~CKeyStore() {}
17
18     virtual bool AddKey(const CKey& key) =0;
19     virtual bool HaveKey(const CBitcoinAddress &address) const =0;
20     virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
21     virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
22     virtual std::vector<unsigned char> GenerateNewKey();
23 };
24
25 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
26
27 class CBasicKeyStore : public CKeyStore
28 {
29 protected:
30     KeyMap mapKeys;
31
32 public:
33     bool AddKey(const CKey& key);
34     bool HaveKey(const CBitcoinAddress &address) const
35     {
36         bool result;
37         CRITICAL_BLOCK(cs_KeyStore)
38             result = (mapKeys.count(address) > 0);
39         return result;
40     }
41     bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
42     {
43         CRITICAL_BLOCK(cs_KeyStore)
44         {
45             KeyMap::const_iterator mi = mapKeys.find(address);
46             if (mi != mapKeys.end())
47             {
48                 keyOut.SetSecret((*mi).second);
49                 return true;
50             }
51         }
52         return false;
53     }
54 };
55
56 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
57
58 class CCryptoKeyStore : public CBasicKeyStore
59 {
60 private:
61     CryptedKeyMap mapCryptedKeys;
62
63     CKeyingMaterial vMasterKey;
64
65     // if fUseCrypto is true, mapKeys must be empty
66     // if fUseCrypto is false, vMasterKey must be empty
67     bool fUseCrypto;
68
69 protected:
70     bool SetCrypted();
71
72     // will encrypt previously unencrypted keys
73     bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
74
75     bool Unlock(const CKeyingMaterial& vMasterKeyIn);
76
77 public:
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         bool result;
92         CRITICAL_BLOCK(cs_KeyStore)
93             result = vMasterKey.empty();
94         return result;
95     }
96
97     bool Lock()
98     {
99         if (!SetCrypted())
100             return false;
101
102         CRITICAL_BLOCK(cs_KeyStore)
103             vMasterKey.clear();
104
105         return true;
106     }
107
108     virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
109     std::vector<unsigned char> GenerateNewKey();
110     bool AddKey(const CKey& key);
111     bool HaveKey(const CBitcoinAddress &address) const
112     {
113         CRITICAL_BLOCK(cs_KeyStore)
114         {
115             if (!IsCrypted())
116                 return CBasicKeyStore::HaveKey(address);
117             return mapCryptedKeys.count(address) > 0;
118         }
119         return false;
120     }
121     bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
122     bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
123 };
124
125 #endif