Remove headers.h
[novacoin.git] / src / keystore.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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
6 #include "keystore.h"
7 #include "crypter.h"
8 #include "script.h"
9
10 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
11 {
12     CKey key;
13     if (!GetKey(address, key))
14         return false;
15     vchPubKeyOut = key.GetPubKey();
16     return true;
17 }
18
19 bool CBasicKeyStore::AddKey(const CKey& key)
20 {
21     bool fCompressed = false;
22     CSecret secret = key.GetSecret(fCompressed);
23     {
24         LOCK(cs_KeyStore);
25         mapKeys[CBitcoinAddress(key.GetPubKey())] = make_pair(secret, fCompressed);
26     }
27     return true;
28 }
29
30 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
31 {
32     {
33         LOCK(cs_KeyStore);
34         mapScripts[Hash160(redeemScript)] = redeemScript;
35     }
36     return true;
37 }
38
39 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
40 {
41     bool result;
42     {
43         LOCK(cs_KeyStore);
44         result = (mapScripts.count(hash) > 0);
45     }
46     return result;
47 }
48
49
50 bool CBasicKeyStore::GetCScript(const uint160 &hash, CScript& redeemScriptOut) const
51 {
52     {
53         LOCK(cs_KeyStore);
54         ScriptMap::const_iterator mi = mapScripts.find(hash);
55         if (mi != mapScripts.end())
56         {
57             redeemScriptOut = (*mi).second;
58             return true;
59         }
60     }
61     return false;
62 }
63
64 bool CCryptoKeyStore::SetCrypted()
65 {
66     {
67         LOCK(cs_KeyStore);
68         if (fUseCrypto)
69             return true;
70         if (!mapKeys.empty())
71             return false;
72         fUseCrypto = true;
73     }
74     return true;
75 }
76
77 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
78 {
79     {
80         LOCK(cs_KeyStore);
81         if (!SetCrypted())
82             return false;
83
84         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
85         for (; mi != mapCryptedKeys.end(); ++mi)
86         {
87             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
88             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
89             CSecret vchSecret;
90             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
91                 return false;
92             if (vchSecret.size() != 32)
93                 return false;
94             CKey key;
95             key.SetPubKey(vchPubKey);
96             key.SetSecret(vchSecret);
97             if (key.GetPubKey() == vchPubKey)
98                 break;
99             return false;
100         }
101         vMasterKey = vMasterKeyIn;
102     }
103     return true;
104 }
105
106 bool CCryptoKeyStore::AddKey(const CKey& key)
107 {
108     {
109         LOCK(cs_KeyStore);
110         if (!IsCrypted())
111             return CBasicKeyStore::AddKey(key);
112
113         if (IsLocked())
114             return false;
115
116         std::vector<unsigned char> vchCryptedSecret;
117         std::vector<unsigned char> vchPubKey = key.GetPubKey();
118         bool fCompressed;
119         if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
120             return false;
121
122         if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
123             return false;
124     }
125     return true;
126 }
127
128
129 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
130 {
131     {
132         LOCK(cs_KeyStore);
133         if (!SetCrypted())
134             return false;
135
136         mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
137     }
138     return true;
139 }
140
141 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
142 {
143     {
144         LOCK(cs_KeyStore);
145         if (!IsCrypted())
146             return CBasicKeyStore::GetKey(address, keyOut);
147
148         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
149         if (mi != mapCryptedKeys.end())
150         {
151             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
152             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
153             CSecret vchSecret;
154             if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
155                 return false;
156             if (vchSecret.size() != 32)
157                 return false;
158             keyOut.SetPubKey(vchPubKey);
159             keyOut.SetSecret(vchSecret);
160             return true;
161         }
162     }
163     return false;
164 }
165
166 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
167 {
168     {
169         LOCK(cs_KeyStore);
170         if (!IsCrypted())
171             return CKeyStore::GetPubKey(address, vchPubKeyOut);
172
173         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
174         if (mi != mapCryptedKeys.end())
175         {
176             vchPubKeyOut = (*mi).second.first;
177             return true;
178         }
179     }
180     return false;
181 }
182
183 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
184 {
185     {
186         LOCK(cs_KeyStore);
187         if (!mapCryptedKeys.empty() || IsCrypted())
188             return false;
189
190         fUseCrypto = true;
191         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
192         {
193             CKey key;
194             if (!key.SetSecret(mKey.second.first, mKey.second.second))
195                 return false;
196             const std::vector<unsigned char> vchPubKey = key.GetPubKey();
197             std::vector<unsigned char> vchCryptedSecret;
198             bool fCompressed;
199             if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
200                 return false;
201             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
202                 return false;
203         }
204         mapKeys.clear();
205     }
206     return true;
207 }