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;
65 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
66 virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const =0;
67 virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
68 virtual void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const =0;
71 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
72 typedef std::map<CScriptID, CScript > ScriptMap;
73 typedef std::set<CScript> WatchOnlySet;
74 typedef std::map<CMalleableKeyView, CSecret> MalleableKeyMap;
76 /** Basic key store, that keeps keys in an address->secret map */
77 class CBasicKeyStore : public CKeyStore
81 MalleableKeyMap mapMalleableKeys;
84 WatchOnlySet setWatchOnly;
87 bool AddKey(const CKey& key);
88 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
89 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
90 bool HaveKey(const CKeyID &address) const;
91 void GetKeys(std::set<CKeyID> &setAddress) const;
92 bool GetKey(const CKeyID &address, CKey &keyOut) const;
93 virtual bool AddCScript(const CScript& redeemScript);
94 virtual bool HaveCScript(const CScriptID &hash) const;
95 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
97 virtual bool AddWatchOnly(const CScript &dest);
98 virtual bool RemoveWatchOnly(const CScript &dest);
99 virtual bool HaveWatchOnly(const CScript &dest) const;
100 virtual bool HaveWatchOnly() const;
102 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const;
103 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const;
104 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
105 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const;
106 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view);
109 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
110 typedef std::map<CMalleableKeyView, std::vector<unsigned char> > CryptedMalleableKeyMap;
112 /** Keystore which keeps the private keys encrypted.
113 * It derives from the basic key store, which is used if no encryption is active.
115 class CCryptoKeyStore : public CBasicKeyStore
118 CryptedKeyMap mapCryptedKeys;
119 CryptedMalleableKeyMap mapCryptedMalleableKeys;
121 CKeyingMaterial vMasterKey;
123 // if fUseCrypto is true, mapKeys must be empty
124 // if fUseCrypto is false, vMasterKey must be empty
127 // keeps track of whether Unlock has run a thorough check before
128 bool fDecryptionThoroughlyChecked;
133 // will encrypt previously unencrypted keys
134 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
135 bool DecryptKeys(const CKeyingMaterial& vMasterKeyIn);
137 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
142 bool IsCrypted() const;
143 bool IsLocked() const;
146 virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
147 virtual bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH);
149 bool AddKey(const CKey& key);
150 bool AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH);
151 bool HaveKey(const CKeyID &address) const;
152 bool GetKey(const CKeyID &address, CKey& keyOut) const;
153 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
154 void GetKeys(std::set<CKeyID> &setAddress) const;
155 bool GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const;
156 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const;
157 bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R, CMalleableKeyView &view) const;
158 bool CheckOwnership(const CMalleablePubKey &mpk);
159 bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const;
160 void ListMalleableViews(std::list<CMalleableKeyView> &malleableViewList) const;
161 bool GetMalleableView(const CMalleablePubKey &mpk, CMalleableKeyView &view);
162 /* Wallet status (encrypted, locked) changed.
163 * Note: Called without locks held.
165 boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;