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
11 #include <boost/signals2/signal.hpp>
17 class CNoDestination {
19 friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
20 friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
23 /** A txout script template with a specific destination. It is either:
24 * CNoDestination: no destination set
25 * CKeyID: TX_PUBKEYHASH destination
26 * CScriptID: TX_SCRIPTHASH destination
28 * A CTxDestination is the internal data type encoded in a CBitcoinAddress.
30 using CTxDestination = std::variant<CNoDestination, CKeyID, CScriptID>;
32 /** A virtual base class for key stores */
36 mutable CCriticalSection cs_KeyStore;
39 virtual ~CKeyStore() {}
41 // Add a key to the store.
42 virtual bool AddKey(const CKey& key) =0;
44 // Add a malleable key to store.
45 virtual bool AddMalleableKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) =0;
46 virtual bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const =0;
48 // Check whether a key corresponding to a given address is present in the store.
49 virtual bool HaveKey(const CKeyID &address) const =0;
50 virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
51 virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
52 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
54 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
55 virtual bool AddCScript(const CScript& redeemScript) =0;
56 virtual bool HaveCScript(const CScriptID &hash) const =0;
57 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
59 // Support for Watch-only addresses
60 virtual bool AddWatchOnly(const CScript &dest) =0;
61 virtual bool RemoveWatchOnly(const CScript &dest) =0;
62 virtual bool HaveWatchOnly(const CScript &dest) const =0;
63 virtual bool HaveWatchOnly() const =0;
65 virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const
68 if (!GetKey(address, key))
70 vchSecret = key.GetSecret(fCompressed);
74 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
75 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const =0;
76 virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
77 virtual void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const =0;
80 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
81 typedef std::map<CScriptID, CScript > ScriptMap;
82 typedef std::set<CScript> WatchOnlySet;
83 typedef std::map<CMalleableKeyView, CSecret> MalleableKeyMap;
85 /** Basic key store, that keeps keys in an address->secret map */
86 class CBasicKeyStore : public CKeyStore
90 MalleableKeyMap mapMalleableKeys;
93 WatchOnlySet setWatchOnly;
96 bool AddKey(const CKey& key);
97 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
98 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
102 MalleableKeyMap::const_iterator mi = mapMalleableKeys.find(keyView);
103 if (mi != mapMalleableKeys.end())
105 mKey = mi->first.GetMalleableKey(mi->second);
112 bool HaveKey(const CKeyID &address) const
117 result = (mapKeys.count(address) > 0);
121 void GetKeys(std::set<CKeyID> &setAddress) const
126 KeyMap::const_iterator mi;
127 for (mi = mapKeys.begin(); mi != mapKeys.end(); ++mi) setAddress.insert((*mi).first);
130 bool GetKey(const CKeyID &address, CKey &keyOut) const
134 KeyMap::const_iterator mi = mapKeys.find(address);
135 if (mi != mapKeys.end())
137 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
143 virtual bool AddCScript(const CScript& redeemScript);
144 virtual bool HaveCScript(const CScriptID &hash) const;
145 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
147 virtual bool AddWatchOnly(const CScript &dest);
148 virtual bool RemoveWatchOnly(const CScript &dest);
149 virtual bool HaveWatchOnly(const CScript &dest) const;
150 virtual bool HaveWatchOnly() const;
152 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
156 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
158 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
165 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
169 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
171 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
181 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
185 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
187 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
189 CMalleableKey mKey = mi->first.GetMalleableKey(mi->second);
190 return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);
197 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
199 malleableViewList.clear();
202 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
203 malleableViewList.push_back(CMalleableKeyView(mi->first));
207 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
209 const CKeyID &mpkID = mpk.GetID();
212 for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
213 if (mi->first.GetID() == mpkID)
215 view = CMalleableKeyView(mi->first);
224 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
225 typedef std::map<CMalleableKeyView, std::vector<unsigned char> > CryptedMalleableKeyMap;
227 /** Keystore which keeps the private keys encrypted.
228 * It derives from the basic key store, which is used if no encryption is active.
230 class CCryptoKeyStore : public CBasicKeyStore
233 CryptedKeyMap mapCryptedKeys;
234 CryptedMalleableKeyMap mapCryptedMalleableKeys;
236 CKeyingMaterial vMasterKey;
238 // if fUseCrypto is true, mapKeys must be empty
239 // if fUseCrypto is false, vMasterKey must be empty
245 // will encrypt previously unencrypted keys
246 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
247 bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
249 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
252 CCryptoKeyStore() : fUseCrypto(false) { }
254 bool IsCrypted() const
259 bool IsLocked() const
266 result = vMasterKey.empty();
273 virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
274 virtual bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH);
276 bool AddKey(const CKey& key);
277 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
278 bool HaveKey(const CKeyID &address) const
283 return CBasicKeyStore::HaveKey(address);
284 return mapCryptedKeys.count(address) > 0;
287 bool GetKey(const CKeyID &address, CKey& keyOut) const;
288 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
289 void GetKeys(std::set<CKeyID> &setAddress) const
293 CBasicKeyStore::GetKeys(setAddress);
297 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
298 while (mi != mapCryptedKeys.end())
300 setAddress.insert((*mi).first);
305 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
307 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
312 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R);
313 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
315 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
322 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const
327 return CBasicKeyStore::CheckOwnership(pubKeyVariant, R, view);
328 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
330 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
340 bool CheckOwnership(const CMalleablePubKey &mpk)
342 CMalleableKeyView view;
343 return GetMalleableView(mpk, view);
346 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
348 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const
350 malleableViewList.clear();
354 return CBasicKeyStore::ListMalleableViews(malleableViewList);
355 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
356 malleableViewList.push_back(CMalleableKeyView(mi->first));
360 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view)
362 const CKeyID &mpkID = mpk.GetID();
366 return CBasicKeyStore::GetMalleableView(mpk, view);
367 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
368 if (mi->first.GetID() == mpkID)
370 view = CMalleableKeyView(mi->first);
378 /* Wallet status (encrypted, locked) changed.
379 * Note: Called without locks held.
381 boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;