765144a9b76526588227104501cdb3900def0750
[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
8 std::vector<unsigned char> CKeyStore::GenerateNewKey()
9 {
10     RandAddSeedPerfmon();
11     CKey key;
12     key.MakeNewKey();
13     if (!AddKey(key))
14         throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
15     return key.GetPubKey();
16 }
17
18 bool CBasicKeyStore::AddKey(const CKey& key)
19 {
20     CRITICAL_BLOCK(cs_KeyStore)
21     {
22         mapKeys[key.GetPubKey()] = key.GetPrivKey();
23         mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey();
24     }
25     return true;
26 }
27
28 bool CCryptoKeyStore::Unlock(const CMasterKey& vMasterKeyIn)
29 {
30     if (!SetCrypted())
31         return false;
32
33     std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.begin();
34     for (; mi != mapCryptedKeys.end(); ++mi)
35     {
36         const std::vector<unsigned char> &vchPubKey = (*mi).first;
37         const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
38         CSecret vchSecret;
39         // decrypt vchCryptedSecret using vMasterKeyIn, into vchSecret
40         CKey key;
41         key.SetSecret(vchSecret);
42         if (key.GetPubKey() == vchPubKey)
43             break;
44         return false;
45     }
46     vMasterKey = vMasterKeyIn;
47     return true;
48 }
49
50 bool CCryptoKeyStore::AddKey(const CKey& key)
51 {
52     CRITICAL_BLOCK(cs_KeyStore)
53     {
54         if (!IsCrypted())
55             return CBasicKeyStore::AddKey(key);
56
57         if (IsLocked())
58             return false;
59
60         CSecret vchSecret = key.GetSecret();
61
62         std::vector<unsigned char> vchCryptedSecret;
63         // encrypt vchSecret using vMasterKey, into vchCryptedSecret
64
65         AddCryptedKey(key.GetPubKey(), vchCryptedSecret);
66     }
67     return true;
68 }
69
70
71 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
72 {
73     CRITICAL_BLOCK(cs_KeyStore)
74     {
75         if (!SetCrypted())
76             return false;
77
78         mapCryptedKeys[vchPubKey] = vchCryptedSecret;
79         mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
80     }
81     return true;
82 }
83
84 bool CCryptoKeyStore::GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const
85 {
86     if (!IsCrypted())
87         return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut);
88
89     std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.find(vchPubKey);
90     if (mi != mapCryptedKeys.end())
91     {
92         const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
93         CSecret vchSecret;
94         // decrypt vchCryptedSecret using vMasterKey into vchSecret;
95         CKey key;
96         key.SetSecret(vchSecret);
97         keyOut = key.GetPrivKey();
98         return true;
99     }
100     return false;
101 }
102
103 bool CCryptoKeyStore::GenerateMasterKey()
104 {
105     if (!mapCryptedKeys.empty())
106         return false;
107
108     RandAddSeedPerfmon();
109
110     vMasterKey.resize(32);
111     RAND_bytes(&vMasterKey[0], 32);
112
113     if (!IsCrypted())
114     {
115         // upgrade wallet
116         fUseCrypto = true;
117     }
118
119     return true;
120 }