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.
10 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
13 if (!GetKey(address, key))
15 vchPubKeyOut = key.GetPubKey();
19 bool CBasicKeyStore::AddKey(const CKey& key)
21 bool fCompressed = false;
22 CSecret secret = key.GetSecret(fCompressed);
25 mapKeys[CBitcoinAddress(key.GetPubKey())] = make_pair(secret, fCompressed);
30 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
34 mapScripts[Hash160(redeemScript)] = redeemScript;
39 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
44 result = (mapScripts.count(hash) > 0);
50 bool CBasicKeyStore::GetCScript(const uint160 &hash, CScript& redeemScriptOut) const
54 ScriptMap::const_iterator mi = mapScripts.find(hash);
55 if (mi != mapScripts.end())
57 redeemScriptOut = (*mi).second;
64 bool CCryptoKeyStore::SetCrypted()
77 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
84 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
85 for (; mi != mapCryptedKeys.end(); ++mi)
87 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
88 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
90 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
92 if (vchSecret.size() != 32)
95 key.SetPubKey(vchPubKey);
96 key.SetSecret(vchSecret);
97 if (key.GetPubKey() == vchPubKey)
101 vMasterKey = vMasterKeyIn;
106 bool CCryptoKeyStore::AddKey(const CKey& key)
111 return CBasicKeyStore::AddKey(key);
116 std::vector<unsigned char> vchCryptedSecret;
117 std::vector<unsigned char> vchPubKey = key.GetPubKey();
119 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
122 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
129 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
136 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
141 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
146 return CBasicKeyStore::GetKey(address, keyOut);
148 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
149 if (mi != mapCryptedKeys.end())
151 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
152 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
154 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
156 if (vchSecret.size() != 32)
158 keyOut.SetPubKey(vchPubKey);
159 keyOut.SetSecret(vchSecret);
166 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
171 return CKeyStore::GetPubKey(address, vchPubKeyOut);
173 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
174 if (mi != mapCryptedKeys.end())
176 vchPubKeyOut = (*mi).second.first;
183 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
187 if (!mapCryptedKeys.empty() || IsCrypted())
191 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
194 if (!key.SetSecret(mKey.second.first, mKey.second.second))
196 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
197 std::vector<unsigned char> vchCryptedSecret;
199 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
201 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))