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 #ifndef BITCOIN_KEYSTORE_H
6 #define BITCOIN_KEYSTORE_H
10 #include <boost/signals2/signal.hpp>
11 #include <boost/variant.hpp>
15 class CNoDestination {
17 friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
18 friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
21 /** A txout script template with a specific destination. It is either:
22 * CNoDestination: no destination set
23 * CKeyID: TX_PUBKEYHASH destination
24 * CScriptID: TX_SCRIPTHASH destination
26 * A CTxDestination is the internal data type encoded in a CBitcoinAddress.
28 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
30 /** A virtual base class for key stores */
34 mutable CCriticalSection cs_KeyStore;
37 virtual ~CKeyStore() {}
39 // Add a key to the store.
40 virtual bool AddKey(const CKey& key) =0;
42 // Add a malleable key to store.
43 virtual bool AddMalleableKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) =0;
44 virtual bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const =0;
46 // Check whether a key corresponding to a given address is present in the store.
47 virtual bool HaveKey(const CKeyID &address) const =0;
48 virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
49 virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
50 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
52 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
53 virtual bool AddCScript(const CScript& redeemScript) =0;
54 virtual bool HaveCScript(const CScriptID &hash) const =0;
55 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
57 // Support for Watch-only addresses
58 virtual bool AddWatchOnly(const CScript &dest) =0;
59 virtual bool RemoveWatchOnly(const CScript &dest) =0;
60 virtual bool HaveWatchOnly(const CScript &dest) const =0;
61 virtual bool HaveWatchOnly() const =0;
63 virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const
66 if (!GetKey(address, key))
68 vchSecret = key.GetSecret(fCompressed);
72 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
73 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const =0;
74 virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
75 virtual void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const =0;
78 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
79 typedef std::map<CScriptID, CScript > ScriptMap;
80 typedef std::set<CScript> WatchOnlySet;
81 typedef std::map<CMalleableKeyView, CSecret> MalleableKeyMap;
83 /** Basic key store, that keeps keys in an address->secret map */
84 class CBasicKeyStore : public CKeyStore
88 MalleableKeyMap mapMalleableKeys;
91 WatchOnlySet setWatchOnly;
94 bool AddKey(const CKey& key);
95 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
96 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
100 MalleableKeyMap::const_iterator mi = mapMalleableKeys.find(keyView);
101 if (mi != mapMalleableKeys.end())
103 mKey = mi->first.GetMalleableKey(mi->second);
110 bool HaveKey(const CKeyID &address) const
115 result = (mapKeys.count(address) > 0);
119 void GetKeys(std::set<CKeyID> &setAddress) const
124 KeyMap::const_iterator mi;
125 for (mi = mapKeys.begin(); mi != mapKeys.end(); ++mi) setAddress.insert((*mi).first);
128 bool GetKey(const CKeyID &address, CKey &keyOut) const
132 KeyMap::const_iterator mi = mapKeys.find(address);
133 if (mi != mapKeys.end())
135 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
141 virtual bool AddCScript(const CScript& redeemScript);
142 virtual bool HaveCScript(const CScriptID &hash) const;
143 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
145 virtual bool AddWatchOnly(const CScript &dest);
146 virtual bool RemoveWatchOnly(const CScript &dest);
147 virtual bool HaveWatchOnly(const CScript &dest) const;
148 virtual bool HaveWatchOnly() const;
150 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
154 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
156 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
163 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
167 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
169 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
179 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
183 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
185 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
187 CMalleableKey mKey = mi->first.GetMalleableKey(mi->second);
188 return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);
195 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
197 malleableViewList.clear();
200 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
201 malleableViewList.push_back(CMalleableKeyView(mi->first));
205 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
207 const CKeyID &mpkID = mpk.GetID();
210 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
211 if (mi->first.GetID() == mpkID)
213 view = CMalleableKeyView(mi->first);
222 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
223 typedef std::map<CMalleableKeyView, std::vector<unsigned char> > CryptedMalleableKeyMap;
225 /** Keystore which keeps the private keys encrypted.
226 * It derives from the basic key store, which is used if no encryption is active.
228 class CCryptoKeyStore : public CBasicKeyStore
231 CryptedKeyMap mapCryptedKeys;
232 CryptedMalleableKeyMap mapCryptedMalleableKeys;
234 CKeyingMaterial vMasterKey;
236 // if fUseCrypto is true, mapKeys must be empty
237 // if fUseCrypto is false, vMasterKey must be empty
243 // will encrypt previously unencrypted keys
244 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
245 bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
247 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
250 CCryptoKeyStore() : fUseCrypto(false) { }
252 bool IsCrypted() const
257 bool IsLocked() const
264 result = vMasterKey.empty();
271 virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
272 virtual bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH);
274 bool AddKey(const CKey& key);
275 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
276 bool HaveKey(const CKeyID &address) const
281 return CBasicKeyStore::HaveKey(address);
282 return mapCryptedKeys.count(address) > 0;
285 bool GetKey(const CKeyID &address, CKey& keyOut) const;
286 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
287 void GetKeys(std::set<CKeyID> &setAddress) const
291 CBasicKeyStore::GetKeys(setAddress);
295 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
296 while (mi != mapCryptedKeys.end())
298 setAddress.insert((*mi).first);
303 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
305 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
310 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R);
311 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
313 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
320 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
325 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R, view);
326 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
328 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
338 bool CheckOwnership(const CMalleablePubKey &mpk)
340 CMalleableKeyView view;
341 return GetMalleableView(mpk, view);
344 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
346 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
348 malleableViewList.clear();
352 return CBasicKeyStore::ListMalleableViews(malleableViewList);
353 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
354 malleableViewList.push_back(CMalleableKeyView(mi->first));
358 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
360 const CKeyID &mpkID = mpk.GetID();
364 return CBasicKeyStore::GetMalleableView(mpk, view);
365 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
366 if (mi->first.GetID() == mpkID)
368 view = CMalleableKeyView(mi->first);
376 /* Wallet status (encrypted, locked) changed.
377 * Note: Called without locks held.
379 boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;