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.
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))
87 if (vchSecret.size() != 32)
90 key.SetPubKey(vchPubKey);
91 key.SetSecret(vchSecret);
92 if (key.GetPubKey() == vchPubKey)
96 vMasterKey = vMasterKeyIn;
101 bool CCryptoKeyStore::AddKey(const CKey& key)
103 CRITICAL_BLOCK(cs_KeyStore)
106 return CBasicKeyStore::AddKey(key);
111 std::vector<unsigned char> vchCryptedSecret;
112 std::vector<unsigned char> vchPubKey = key.GetPubKey();
114 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
117 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
124 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
126 CRITICAL_BLOCK(cs_KeyStore)
131 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
136 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
138 CRITICAL_BLOCK(cs_KeyStore)
141 return CBasicKeyStore::GetKey(address, keyOut);
143 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
144 if (mi != mapCryptedKeys.end())
146 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
147 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
149 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
151 if (vchSecret.size() != 32)
153 keyOut.SetPubKey(vchPubKey);
154 keyOut.SetSecret(vchSecret);
161 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
163 CRITICAL_BLOCK(cs_KeyStore)
166 return CKeyStore::GetPubKey(address, vchPubKeyOut);
168 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
169 if (mi != mapCryptedKeys.end())
171 vchPubKeyOut = (*mi).second.first;
178 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
180 CRITICAL_BLOCK(cs_KeyStore)
182 if (!mapCryptedKeys.empty() || IsCrypted())
186 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
189 if (!key.SetSecret(mKey.second.first, mKey.second.second))
191 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
192 std::vector<unsigned char> vchCryptedSecret;
194 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
196 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))