Use standard C99 (and Qt) types for 64-bit integers
[novacoin.git] / src / keystore.cpp
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
6 #include <stdint.h>
7
8 #include "headers.h"
9 #include "crypter.h"
10 #include "db.h"
11 #include "script.h"
12
13 std::vector<unsigned char> CKeyStore::GenerateNewKey()
14 {
15     RandAddSeedPerfmon();
16     CKey key;
17     key.MakeNewKey();
18     if (!AddKey(key))
19         throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
20     return key.GetPubKey();
21 }
22
23 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
24 {
25     CKey key;
26     if (!GetKey(address, key))
27         return false;
28     vchPubKeyOut = key.GetPubKey();
29     return true;
30 }
31
32 bool CBasicKeyStore::AddKey(const CKey& key)
33 {
34     CRITICAL_BLOCK(cs_KeyStore)
35         mapKeys[CBitcoinAddress(key.GetPubKey())] = key.GetSecret();
36     return true;
37 }
38
39 bool CBasicKeyStore::AddCScript(const uint160 &hash, const CScript& redeemScript)
40 {
41     CRITICAL_BLOCK(cs_KeyStore)
42         mapScripts[hash] = 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 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
83 {
84     RandAddSeedPerfmon();
85     CKey key;
86     key.MakeNewKey();
87     if (!AddKey(key))
88         throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
89     return key.GetPubKey();
90 }
91
92 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
93 {
94     CRITICAL_BLOCK(cs_KeyStore)
95     {
96         if (!SetCrypted())
97             return false;
98
99         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
100         for (; mi != mapCryptedKeys.end(); ++mi)
101         {
102             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
103             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
104             CSecret vchSecret;
105             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
106                 return false;
107             CKey key;
108             key.SetSecret(vchSecret);
109             if (key.GetPubKey() == vchPubKey)
110                 break;
111             return false;
112         }
113         vMasterKey = vMasterKeyIn;
114     }
115     return true;
116 }
117
118 bool CCryptoKeyStore::AddKey(const CKey& key)
119 {
120     CRITICAL_BLOCK(cs_KeyStore)
121     {
122         if (!IsCrypted())
123             return CBasicKeyStore::AddKey(key);
124
125         if (IsLocked())
126             return false;
127
128         std::vector<unsigned char> vchCryptedSecret;
129         std::vector<unsigned char> vchPubKey = key.GetPubKey();
130         if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
131             return false;
132
133         if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
134             return false;
135     }
136     return true;
137 }
138
139
140 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
141 {
142     CRITICAL_BLOCK(cs_KeyStore)
143     {
144         if (!SetCrypted())
145             return false;
146
147         mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
148     }
149     return true;
150 }
151
152 bool CCryptoKeyStore::GetSecret(const CBitcoinAddress &address, CSecret& vchSecretOut) const
153 {
154     CRITICAL_BLOCK(cs_KeyStore)
155     {
156         if (!IsCrypted())
157             return CBasicKeyStore::GetSecret(address, vchSecretOut);
158
159         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
160         if (mi != mapCryptedKeys.end())
161         {
162             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
163             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
164             return DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecretOut);
165         }
166     }
167     return false;
168 }
169
170 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
171 {
172     CRITICAL_BLOCK(cs_KeyStore)
173     {
174         if (!IsCrypted())
175             return CKeyStore::GetPubKey(address, vchPubKeyOut);
176
177         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
178         if (mi != mapCryptedKeys.end())
179         {
180             vchPubKeyOut = (*mi).second.first;
181             return true;
182         }
183     }
184     return false;
185 }
186
187 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
188 {
189     CRITICAL_BLOCK(cs_KeyStore)
190     {
191         if (!mapCryptedKeys.empty() || IsCrypted())
192             return false;
193
194         fUseCrypto = true;
195         CKey key;
196         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
197         {
198             if (!key.SetSecret(mKey.second))
199                 return false;
200             const std::vector<unsigned char> vchPubKey = key.GetPubKey();
201             std::vector<unsigned char> vchCryptedSecret;
202             if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
203                 return false;
204             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
205                 return false;
206         }
207         mapKeys.clear();
208     }
209     return true;
210 }