Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
keystore.cpp
Go to the documentation of this file.
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 
9 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
10 {
11  CKey key;
12  if (!GetKey(address, key))
13  return false;
14  vchPubKeyOut = key.GetPubKey();
15  return true;
16 }
17 
18 bool CKeyStore::AddKey(const CKey &key) {
19  return AddKeyPubKey(key, key.GetPubKey());
20 }
21 
22 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
23 {
25  mapKeys[pubkey.GetID()] = key;
26  return true;
27 }
28 
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
30 {
32  mapScripts[redeemScript.GetID()] = redeemScript;
33  return true;
34 }
35 
37 {
39  return mapScripts.count(hash) > 0;
40 }
41 
42 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
43 {
45  ScriptMap::const_iterator mi = mapScripts.find(hash);
46  if (mi != mapScripts.end())
47  {
48  redeemScriptOut = (*mi).second;
49  return true;
50  }
51  return false;
52 }
53 
55 {
57  if (fUseCrypto)
58  return true;
59  if (!mapKeys.empty())
60  return false;
61  fUseCrypto = true;
62  return true;
63 }
64 
66 {
67  if (!SetCrypted())
68  return false;
69 
70  {
72  vMasterKey.clear();
73  }
74 
75  NotifyStatusChanged(this);
76  return true;
77 }
78 
79 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
80 {
81  {
83  if (!SetCrypted())
84  return false;
85 
86  CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
87  for (; mi != mapCryptedKeys.end(); ++mi)
88  {
89  const CPubKey &vchPubKey = (*mi).second.first;
90  const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
91  CKeyingMaterial vchSecret;
92  if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
93  return false;
94  if (vchSecret.size() != 32)
95  return false;
96  CKey key;
97  key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
98  if (key.GetPubKey() == vchPubKey)
99  break;
100  return false;
101  }
102  vMasterKey = vMasterKeyIn;
103  }
104  NotifyStatusChanged(this);
105  return true;
106 }
107 
108 bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
109 {
110  {
111  LOCK(cs_KeyStore);
112  if (!IsCrypted())
113  return CBasicKeyStore::AddKeyPubKey(key, pubkey);
114 
115  if (IsLocked())
116  return false;
117 
118  std::vector<unsigned char> vchCryptedSecret;
119  CKeyingMaterial vchSecret(key.begin(), key.end());
120  if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret))
121  return false;
122 
123  if (!AddCryptedKey(pubkey, vchCryptedSecret))
124  return false;
125  }
126  return true;
127 }
128 
129 
130 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
131 {
132  {
133  LOCK(cs_KeyStore);
134  if (!SetCrypted())
135  return false;
136 
137  mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
138  }
139  return true;
140 }
141 
142 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
143 {
144  {
145  LOCK(cs_KeyStore);
146  if (!IsCrypted())
147  return CBasicKeyStore::GetKey(address, keyOut);
148 
149  CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
150  if (mi != mapCryptedKeys.end())
151  {
152  const CPubKey &vchPubKey = (*mi).second.first;
153  const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
154  CKeyingMaterial vchSecret;
155  if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
156  return false;
157  if (vchSecret.size() != 32)
158  return false;
159  keyOut.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
160  return true;
161  }
162  }
163  return false;
164 }
165 
166 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
167 {
168  {
169  LOCK(cs_KeyStore);
170  if (!IsCrypted())
171  return CKeyStore::GetPubKey(address, vchPubKeyOut);
172 
173  CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
174  if (mi != mapCryptedKeys.end())
175  {
176  vchPubKeyOut = (*mi).second.first;
177  return true;
178  }
179  }
180  return false;
181 }
182 
184 {
185  {
186  LOCK(cs_KeyStore);
187  if (!mapCryptedKeys.empty() || IsCrypted())
188  return false;
189 
190  fUseCrypto = true;
191  BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
192  {
193  const CKey &key = mKey.second;
194  CPubKey vchPubKey = key.GetPubKey();
195  CKeyingMaterial vchSecret(key.begin(), key.end());
196  std::vector<unsigned char> vchCryptedSecret;
197  if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), vchCryptedSecret))
198  return false;
199  if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
200  return false;
201  }
202  mapKeys.clear();
203  }
204  return true;
205 }
bool IsCrypted() const
Definition: keystore.h:120
CCriticalSection cs_KeyStore
Definition: keystore.h:18
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)=0
const unsigned char * begin() const
Definition: key.h:222
const unsigned char * end() const
Definition: key.h:223
bool SetCrypted()
Definition: keystore.cpp:54
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:61
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
Definition: keystore.cpp:183
bool IsLocked() const
Definition: keystore.h:125
CKeyingMaterial vMasterKey
Definition: keystore.h:101
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Definition: keystore.cpp:130
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: keystore.cpp:22
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: keystore.cpp:142
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: keystore.cpp:166
virtual bool AddCScript(const CScript &redeemScript)
Definition: keystore.cpp:29
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Definition: keystore.h:172
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: keystore.h:73
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: keystore.cpp:9
CPubKey GetPubKey() const
Definition: key.cpp:312
#define LOCK(cs)
Definition: sync.h:108
An encapsulated public key.
Definition: key.h:40
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
Definition: keystore.cpp:79
ScriptMap mapScripts
Definition: keystore.h:47
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const =0
uint256 GetHash() const
Definition: key.h:134
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: keystore.cpp:108
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:18
void Set(const T pbegin, const T pend, bool fCompressedIn)
Definition: key.h:206
KeyMap mapKeys
Definition: keystore.h:46
bool IsCompressed() const
Definition: key.h:147
CryptedKeyMap mapCryptedKeys
Definition: keystore.h:99
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:244
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:113
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:24
CScriptID GetID() const
Definition: script.h:589
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const
Definition: keystore.cpp:42
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:103
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: key.h:32
An encapsulated private key.
Definition: key.h:172
CKeyID GetID() const
Definition: key.h:129
uint32_t hash
Definition: cache.cc:34
virtual bool HaveCScript(const CScriptID &hash) const
Definition: keystore.cpp:36