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