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