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>
16 class CNoDestination {
18 friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
19 friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
22 /** A txout script template with a specific destination. It is either:
23 * CNoDestination: no destination set
24 * CKeyID: TX_PUBKEYHASH destination
25 * CScriptID: TX_SCRIPTHASH destination
27 * A CTxDestination is the internal data type encoded in a CBitcoinAddress.
29 using CTxDestination = std::variant<CNoDestination, CKeyID, CScriptID>;
31 /** A virtual base class for key stores */
35 mutable CCriticalSection cs_KeyStore;
38 virtual ~CKeyStore() {}
40 // Add a key to the store.
41 virtual bool AddKey(const CKey& key) =0;
43 // Add a malleable key to store.
44 virtual bool AddMalleableKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) =0;
45 virtual bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const =0;
47 // Check whether a key corresponding to a given address is present in the store.
48 virtual bool HaveKey(const CKeyID &address) const =0;
49 virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
50 virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
51 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
53 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
54 virtual bool AddCScript(const CScript& redeemScript) =0;
55 virtual bool HaveCScript(const CScriptID &hash) const =0;
56 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
58 // Support for Watch-only addresses
59 virtual bool AddWatchOnly(const CScript &dest) =0;
60 virtual bool RemoveWatchOnly(const CScript &dest) =0;
61 virtual bool HaveWatchOnly(const CScript &dest) const =0;
62 virtual bool HaveWatchOnly() const =0;
64 virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const
67 if (!GetKey(address, key))
69 vchSecret = key.GetSecret(fCompressed);
73 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
74 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const =0;
75 virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
76 virtual void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const =0;
79 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
80 typedef std::map<CScriptID, CScript > ScriptMap;
81 typedef std::set<CScript> WatchOnlySet;
82 typedef std::map<CMalleableKeyView, CSecret> MalleableKeyMap;
84 /** Basic key store, that keeps keys in an address->secret map */
85 class CBasicKeyStore : public CKeyStore
89 MalleableKeyMap mapMalleableKeys;
92 WatchOnlySet setWatchOnly;
95 bool AddKey(const CKey& key);
96 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
97 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
101 MalleableKeyMap::const_iterator mi = mapMalleableKeys.find(keyView);
102 if (mi != mapMalleableKeys.end())
104 mKey = mi->first.GetMalleableKey(mi->second);
111 bool HaveKey(const CKeyID &address) const
116 result = (mapKeys.count(address) > 0);
120 void GetKeys(std::set<CKeyID> &setAddress) const
125 KeyMap::const_iterator mi;
126 for (mi = mapKeys.begin(); mi != mapKeys.end(); ++mi) setAddress.insert((*mi).first);
129 bool GetKey(const CKeyID &address, CKey &keyOut) const
133 KeyMap::const_iterator mi = mapKeys.find(address);
134 if (mi != mapKeys.end())
136 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
142 virtual bool AddCScript(const CScript& redeemScript);
143 virtual bool HaveCScript(const CScriptID &hash) const;
144 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
146 virtual bool AddWatchOnly(const CScript &dest);
147 virtual bool RemoveWatchOnly(const CScript &dest);
148 virtual bool HaveWatchOnly(const CScript &dest) const;
149 virtual bool HaveWatchOnly() const;
151 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
155 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
157 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
164 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
168 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
170 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
180 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
184 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
186 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
188 CMalleableKey mKey = mi->first.GetMalleableKey(mi->second);
189 return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);
196 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
198 malleableViewList.clear();
201 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
202 malleableViewList.push_back(CMalleableKeyView(mi->first));
206 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
208 const CKeyID &mpkID = mpk.GetID();
211 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
212 if (mi->first.GetID() == mpkID)
214 view = CMalleableKeyView(mi->first);
223 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
224 typedef std::map<CMalleableKeyView, std::vector<unsigned char> > CryptedMalleableKeyMap;
226 /** Keystore which keeps the private keys encrypted.
227 * It derives from the basic key store, which is used if no encryption is active.
229 class CCryptoKeyStore : public CBasicKeyStore
232 CryptedKeyMap mapCryptedKeys;
233 CryptedMalleableKeyMap mapCryptedMalleableKeys;
235 CKeyingMaterial vMasterKey;
237 // if fUseCrypto is true, mapKeys must be empty
238 // if fUseCrypto is false, vMasterKey must be empty
244 // will encrypt previously unencrypted keys
245 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
246 bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
248 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
251 CCryptoKeyStore() : fUseCrypto(false) { }
253 bool IsCrypted() const
258 bool IsLocked() const
265 result = vMasterKey.empty();
272 virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
273 virtual bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH);
275 bool AddKey(const CKey& key);
276 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
277 bool HaveKey(const CKeyID &address) const
282 return CBasicKeyStore::HaveKey(address);
283 return mapCryptedKeys.count(address) > 0;
286 bool GetKey(const CKeyID &address, CKey& keyOut) const;
287 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
288 void GetKeys(std::set<CKeyID> &setAddress) const
292 CBasicKeyStore::GetKeys(setAddress);
296 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
297 while (mi != mapCryptedKeys.end())
299 setAddress.insert((*mi).first);
304 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
306 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
311 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R);
312 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
314 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
321 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
326 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R, view);
327 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
329 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
339 bool CheckOwnership(const CMalleablePubKey &mpk)
341 CMalleableKeyView view;
342 return GetMalleableView(mpk, view);
345 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
347 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
349 malleableViewList.clear();
353 return CBasicKeyStore::ListMalleableViews(malleableViewList);
354 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
355 malleableViewList.push_back(CMalleableKeyView(mi->first));
359 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
361 const CKeyID &mpkID = mpk.GetID();
365 return CBasicKeyStore::GetMalleableView(mpk, view);
366 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
367 if (mi->first.GetID() == mpkID)
369 view = CMalleableKeyView(mi->first);
377 /* Wallet status (encrypted, locked) changed.
378 * Note: Called without locks held.
380 boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;