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.
8 std::vector<unsigned char> CKeyStore::GenerateNewKey()
14 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
15 return key.GetPubKey();
18 bool CBasicKeyStore::AddKey(const CKey& key)
20 CRITICAL_BLOCK(cs_KeyStore)
22 mapKeys[key.GetPubKey()] = key.GetPrivKey();
23 mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey();
28 bool CCryptoKeyStore::Unlock(const CMasterKey& vMasterKeyIn)
33 std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.begin();
34 for (; mi != mapCryptedKeys.end(); ++mi)
36 const std::vector<unsigned char> &vchPubKey = (*mi).first;
37 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
39 // decrypt vchCryptedSecret using vMasterKeyIn, into vchSecret
41 key.SetSecret(vchSecret);
42 if (key.GetPubKey() == vchPubKey)
46 vMasterKey = vMasterKeyIn;
50 bool CCryptoKeyStore::AddKey(const CKey& key)
52 CRITICAL_BLOCK(cs_KeyStore)
55 return CBasicKeyStore::AddKey(key);
60 CSecret vchSecret = key.GetSecret();
62 std::vector<unsigned char> vchCryptedSecret;
63 // encrypt vchSecret using vMasterKey, into vchCryptedSecret
65 AddCryptedKey(key.GetPubKey(), vchCryptedSecret);
71 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
73 CRITICAL_BLOCK(cs_KeyStore)
78 mapCryptedKeys[vchPubKey] = vchCryptedSecret;
79 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
84 bool CCryptoKeyStore::GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const
87 return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut);
89 std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.find(vchPubKey);
90 if (mi != mapCryptedKeys.end())
92 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
94 // decrypt vchCryptedSecret using vMasterKey into vchSecret;
96 key.SetSecret(vchSecret);
97 keyOut = key.GetPrivKey();
103 bool CCryptoKeyStore::GenerateMasterKey()
105 if (!mapCryptedKeys.empty())
108 RandAddSeedPerfmon();
110 vMasterKey.resize(32);
111 RAND_bytes(&vMasterKey[0], 32);