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