Use CBitcoinAddress instead of string/uint160
[novacoin.git] / src / keystore.cpp
1 // Copyright (c) 2009-2011 Satoshi Nakamoto & Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "headers.h"
6 #include "db.h"
7 #include "crypter.h"
8
9 std::vector<unsigned char> CKeyStore::GenerateNewKey()
10 {
11     RandAddSeedPerfmon();
12     CKey key;
13     key.MakeNewKey();
14     if (!AddKey(key))
15         throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
16     return key.GetPubKey();
17 }
18
19 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
20 {
21     CKey key;
22     if (!GetKey(address, key))
23         return false;
24     vchPubKeyOut = key.GetPubKey();
25     return true;
26 }
27
28 bool CBasicKeyStore::AddKey(const CKey& key)
29 {
30     CRITICAL_BLOCK(cs_KeyStore)
31         mapKeys[key.GetAddress()] = key.GetSecret();
32     return true;
33 }
34
35 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
36 {
37     RandAddSeedPerfmon();
38     CKey key;
39     key.MakeNewKey();
40     if (!AddKey(key))
41         throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
42     return key.GetPubKey();
43 }
44
45 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
46 {
47     CRITICAL_BLOCK(cs_vMasterKey)
48     {
49         if (!SetCrypted())
50             return false;
51
52         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
53         for (; mi != mapCryptedKeys.end(); ++mi)
54         {
55             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
56             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
57             CSecret vchSecret;
58             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
59                 return false;
60             CKey key;
61             key.SetSecret(vchSecret);
62             if (key.GetPubKey() == vchPubKey)
63                 break;
64             return false;
65         }
66         vMasterKey = vMasterKeyIn;
67     }
68     return true;
69 }
70
71 bool CCryptoKeyStore::AddKey(const CKey& key)
72 {
73     CRITICAL_BLOCK(cs_KeyStore)
74     CRITICAL_BLOCK(cs_vMasterKey)
75     {
76         if (!IsCrypted())
77             return CBasicKeyStore::AddKey(key);
78
79         if (IsLocked())
80             return false;
81
82         std::vector<unsigned char> vchCryptedSecret;
83         std::vector<unsigned char> vchPubKey = key.GetPubKey();
84         if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
85             return false;
86
87         if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
88             return false;
89     }
90     return true;
91 }
92
93
94 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
95 {
96     CRITICAL_BLOCK(cs_KeyStore)
97     {
98         if (!SetCrypted())
99             return false;
100
101         mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
102     }
103     return true;
104 }
105
106 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
107 {
108     CRITICAL_BLOCK(cs_vMasterKey)
109     {
110         if (!IsCrypted())
111             return CBasicKeyStore::GetKey(address, keyOut);
112
113         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
114         if (mi != mapCryptedKeys.end())
115         {
116             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
117             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
118             CSecret vchSecret;
119             if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
120                 return false;
121             keyOut.SetSecret(vchSecret);
122             return true;
123         }
124     }
125     return false;
126 }
127
128 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
129 {
130     CRITICAL_BLOCK(cs_vMasterKey)
131     {
132         if (!IsCrypted())
133             return CKeyStore::GetPubKey(address, vchPubKeyOut);
134
135         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
136         if (mi != mapCryptedKeys.end())
137         {
138             vchPubKeyOut = (*mi).second.first;
139             return true;
140         }
141     }
142     return false;
143 }
144
145 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
146 {
147     CRITICAL_BLOCK(cs_KeyStore)
148     CRITICAL_BLOCK(cs_vMasterKey)
149     {
150         if (!mapCryptedKeys.empty() || IsCrypted())
151             return false;
152
153         fUseCrypto = true;
154         CKey key;
155         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
156         {
157             if (!key.SetPrivKey(mKey.second))
158                 return false;
159             const std::vector<unsigned char> vchPubKey = key.GetPubKey();
160             std::vector<unsigned char> vchCryptedSecret;
161             if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
162                 return false;
163             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
164                 return false;
165         }
166         mapKeys.clear();
167     }
168     return true;
169 }