Add removeaddress RPC call
[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 COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "keystore.h"
7 #include "script.h"
8 #include "base58.h"
9
10 extern bool fWalletUnlockMintOnly;
11
12 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
13 {
14     CKey key;
15     if (!GetKey(address, key))
16         return false;
17     vchPubKeyOut = key.GetPubKey();
18     return true;
19 }
20
21 bool CBasicKeyStore::AddKey(const CKey& key)
22 {
23     bool fCompressed = false;
24     CSecret secret = key.GetSecret(fCompressed);
25     {
26         LOCK(cs_KeyStore);
27         mapKeys[key.GetPubKey().GetID()] = make_pair(secret, fCompressed);
28     }
29     return true;
30 }
31
32 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
33 {
34     {
35         LOCK(cs_KeyStore);
36         mapScripts[redeemScript.GetID()] = redeemScript;
37     }
38     return true;
39 }
40
41 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
42 {
43     bool result;
44     {
45         LOCK(cs_KeyStore);
46         result = (mapScripts.count(hash) > 0);
47     }
48     return result;
49 }
50
51
52 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
53 {
54     {
55         LOCK(cs_KeyStore);
56         ScriptMap::const_iterator mi = mapScripts.find(hash);
57         if (mi != mapScripts.end())
58         {
59             redeemScriptOut = (*mi).second;
60             return true;
61         }
62     }
63     return false;
64 }
65
66 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
67 {
68     LOCK(cs_KeyStore);
69
70     CTxDestination address;
71     if (ExtractDestination(dest, address)) {
72         CKeyID keyID;
73         CBitcoinAddress(address).GetKeyID(keyID);
74         if (HaveKey(keyID))
75             return false;
76     }
77
78     setWatchOnly.insert(dest);
79     return true;
80 }
81
82
83 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
84 {
85     LOCK(cs_KeyStore);
86     setWatchOnly.erase(dest);
87     return true;
88 }
89
90 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
91 {
92     LOCK(cs_KeyStore);
93     return setWatchOnly.count(dest) > 0;
94 }
95
96 bool CBasicKeyStore::HaveWatchOnly() const
97 {
98     LOCK(cs_KeyStore);
99     return (!setWatchOnly.empty());
100 }
101
102 bool CCryptoKeyStore::SetCrypted()
103 {
104     {
105         LOCK(cs_KeyStore);
106         if (fUseCrypto)
107             return true;
108         if (!mapKeys.empty())
109             return false;
110         fUseCrypto = true;
111     }
112     return true;
113 }
114
115 bool CCryptoKeyStore::Lock()
116 {
117     if (!SetCrypted())
118         return false;
119
120     {
121         LOCK(cs_KeyStore);
122         vMasterKey.clear();
123         fWalletUnlockMintOnly = false;
124     }
125
126     NotifyStatusChanged(this);
127     return true;
128 }
129
130 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
131 {
132     {
133         LOCK(cs_KeyStore);
134         if (!SetCrypted())
135             return false;
136
137         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
138         for (; mi != mapCryptedKeys.end(); ++mi)
139         {
140             const CPubKey &vchPubKey = (*mi).second.first;
141             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
142             CSecret vchSecret;
143             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
144                 return false;
145             if (vchSecret.size() != 32)
146                 return false;
147             CKey key;
148             key.SetPubKey(vchPubKey);
149             key.SetSecret(vchSecret);
150             if (key.GetPubKey() == vchPubKey)
151                 break;
152             return false;
153         }
154         vMasterKey = vMasterKeyIn;
155     }
156     NotifyStatusChanged(this);
157     return true;
158 }
159
160 bool CCryptoKeyStore::AddKey(const CKey& key)
161 {
162     {
163         LOCK(cs_KeyStore);
164
165         CScript script;
166         script.SetDestination(key.GetPubKey().GetID());
167
168         if (HaveWatchOnly(script))
169             return false;
170
171         if (!IsCrypted())
172             return CBasicKeyStore::AddKey(key);
173
174         if (IsLocked())
175             return false;
176
177         std::vector<unsigned char> vchCryptedSecret;
178         CPubKey vchPubKey = key.GetPubKey();
179         bool fCompressed;
180         if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
181             return false;
182
183         if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
184             return false;
185     }
186     return true;
187 }
188
189
190 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
191 {
192     {
193         LOCK(cs_KeyStore);
194         if (!SetCrypted())
195             return false;
196
197         mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
198     }
199     return true;
200 }
201
202 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
203 {
204     {
205         LOCK(cs_KeyStore);
206         if (!IsCrypted())
207             return CBasicKeyStore::GetKey(address, keyOut);
208
209         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
210         if (mi != mapCryptedKeys.end())
211         {
212             const CPubKey &vchPubKey = (*mi).second.first;
213             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
214             CSecret vchSecret;
215             if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
216                 return false;
217             if (vchSecret.size() != 32)
218                 return false;
219             keyOut.SetPubKey(vchPubKey);
220             keyOut.SetSecret(vchSecret);
221             return true;
222         }
223     }
224     return false;
225 }
226
227 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
228 {
229     {
230         LOCK(cs_KeyStore);
231         if (!IsCrypted())
232             return CKeyStore::GetPubKey(address, vchPubKeyOut);
233
234         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
235         if (mi != mapCryptedKeys.end())
236         {
237             vchPubKeyOut = (*mi).second.first;
238             return true;
239         }
240     }
241     return false;
242 }
243
244 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
245 {
246     {
247         LOCK(cs_KeyStore);
248         if (!mapCryptedKeys.empty() || IsCrypted())
249             return false;
250
251         fUseCrypto = true;
252         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
253         {
254             CKey key;
255             if (!key.SetSecret(mKey.second.first, mKey.second.second))
256                 return false;
257             const CPubKey vchPubKey = key.GetPubKey();
258             std::vector<unsigned char> vchCryptedSecret;
259             bool fCompressed;
260             if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
261                 return false;
262             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
263                 return false;
264         }
265         mapKeys.clear();
266     }
267     return true;
268 }