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 license.txt or http://www.opensource.org/licenses/mit-license.php.
11 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
14 if (!GetKey(address, key))
16 vchPubKeyOut = key.GetPubKey();
20 bool CBasicKeyStore::AddKey(const CKey& key)
22 bool fCompressed = false;
23 CSecret secret = key.GetSecret(fCompressed);
24 CRITICAL_BLOCK(cs_KeyStore)
25 mapKeys[CBitcoinAddress(key.GetPubKey())] = make_pair(secret, fCompressed);
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
31 CRITICAL_BLOCK(cs_KeyStore)
32 mapScripts[Hash160(redeemScript)] = redeemScript;
36 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
39 CRITICAL_BLOCK(cs_KeyStore)
40 result = (mapScripts.count(hash) > 0);
45 bool CBasicKeyStore::GetCScript(const uint160 &hash, CScript& redeemScriptOut) const
47 CRITICAL_BLOCK(cs_KeyStore)
49 ScriptMap::const_iterator mi = mapScripts.find(hash);
50 if (mi != mapScripts.end())
52 redeemScriptOut = (*mi).second;
59 bool CCryptoKeyStore::SetCrypted()
61 CRITICAL_BLOCK(cs_KeyStore)
72 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
74 CRITICAL_BLOCK(cs_KeyStore)
79 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
80 for (; mi != mapCryptedKeys.end(); ++mi)
82 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
83 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
85 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
88 key.SetPubKey(vchPubKey);
89 key.SetSecret(vchSecret);
90 if (key.GetPubKey() == vchPubKey)
94 vMasterKey = vMasterKeyIn;
99 bool CCryptoKeyStore::AddKey(const CKey& key)
101 CRITICAL_BLOCK(cs_KeyStore)
104 return CBasicKeyStore::AddKey(key);
109 std::vector<unsigned char> vchCryptedSecret;
110 std::vector<unsigned char> vchPubKey = key.GetPubKey();
112 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
115 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
122 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
124 CRITICAL_BLOCK(cs_KeyStore)
129 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
134 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
136 CRITICAL_BLOCK(cs_KeyStore)
139 return CBasicKeyStore::GetKey(address, keyOut);
141 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
142 if (mi != mapCryptedKeys.end())
144 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
145 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
147 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
149 keyOut.SetPubKey(vchPubKey);
150 keyOut.SetSecret(vchSecret);
157 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
159 CRITICAL_BLOCK(cs_KeyStore)
162 return CKeyStore::GetPubKey(address, vchPubKeyOut);
164 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
165 if (mi != mapCryptedKeys.end())
167 vchPubKeyOut = (*mi).second.first;
174 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
176 CRITICAL_BLOCK(cs_KeyStore)
178 if (!mapCryptedKeys.empty() || IsCrypted())
182 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
185 if (!key.SetSecret(mKey.second.first, mKey.second.second))
187 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
188 std::vector<unsigned char> vchCryptedSecret;
190 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
192 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))