1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_KEYSTORE_H
7 #define BITCOIN_KEYSTORE_H
11 #include "script/script.h"
12 #include "script/standard.h"
14 #include "zcash/Address.hpp"
15 #include "zcash/NoteEncryption.hpp"
17 #include <boost/signals2/signal.hpp>
18 #include <boost/variant.hpp>
20 /** A virtual base class for key stores */
24 mutable CCriticalSection cs_KeyStore;
25 mutable CCriticalSection cs_SpendingKeyStore;
28 virtual ~CKeyStore() {}
30 //! Add a key to the store.
31 virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
32 virtual bool AddKey(const CKey &key);
34 //! Check whether a key corresponding to a given address is present in the store.
35 virtual bool HaveKey(const CKeyID &address) const =0;
36 virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
37 virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
38 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
40 //! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
41 virtual bool AddCScript(const CScript& redeemScript) =0;
42 virtual bool HaveCScript(const CScriptID &hash) const =0;
43 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
45 //! Support for Watch-only addresses
46 virtual bool AddWatchOnly(const CScript &dest) =0;
47 virtual bool RemoveWatchOnly(const CScript &dest) =0;
48 virtual bool HaveWatchOnly(const CScript &dest) const =0;
49 virtual bool HaveWatchOnly() const =0;
51 //! Add a spending key to the store.
52 virtual bool AddSpendingKey(const libzcash::SpendingKey &sk) =0;
54 //! Check whether a spending key corresponding to a given payment address is present in the store.
55 virtual bool HaveSpendingKey(const libzcash::PaymentAddress &address) const =0;
56 virtual bool GetSpendingKey(const libzcash::PaymentAddress &address, libzcash::SpendingKey& skOut) const =0;
57 virtual void GetPaymentAddresses(std::set<libzcash::PaymentAddress> &setAddress) const =0;
60 typedef std::map<CKeyID, CKey> KeyMap;
61 typedef std::map<CScriptID, CScript > ScriptMap;
62 typedef std::set<CScript> WatchOnlySet;
63 typedef std::map<libzcash::PaymentAddress, libzcash::SpendingKey> SpendingKeyMap;
64 typedef std::map<libzcash::PaymentAddress, ZCNoteDecryption> NoteDecryptorMap;
66 /** Basic key store, that keeps keys in an address->secret map */
67 class CBasicKeyStore : public CKeyStore
72 WatchOnlySet setWatchOnly;
73 SpendingKeyMap mapSpendingKeys;
74 NoteDecryptorMap mapNoteDecryptors;
77 bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
78 bool HaveKey(const CKeyID &address) const
83 result = (mapKeys.count(address) > 0);
87 void GetKeys(std::set<CKeyID> &setAddress) const
92 KeyMap::const_iterator mi = mapKeys.begin();
93 while (mi != mapKeys.end())
95 setAddress.insert((*mi).first);
100 bool GetKey(const CKeyID &address, CKey &keyOut) const
104 KeyMap::const_iterator mi = mapKeys.find(address);
105 if (mi != mapKeys.end())
113 virtual bool AddCScript(const CScript& redeemScript);
114 virtual bool HaveCScript(const CScriptID &hash) const;
115 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
117 virtual bool AddWatchOnly(const CScript &dest);
118 virtual bool RemoveWatchOnly(const CScript &dest);
119 virtual bool HaveWatchOnly(const CScript &dest) const;
120 virtual bool HaveWatchOnly() const;
122 bool AddSpendingKey(const libzcash::SpendingKey &sk);
123 bool HaveSpendingKey(const libzcash::PaymentAddress &address) const
127 LOCK(cs_SpendingKeyStore);
128 result = (mapSpendingKeys.count(address) > 0);
132 bool GetSpendingKey(const libzcash::PaymentAddress &address, libzcash::SpendingKey &skOut) const
135 LOCK(cs_SpendingKeyStore);
136 SpendingKeyMap::const_iterator mi = mapSpendingKeys.find(address);
137 if (mi != mapSpendingKeys.end())
145 bool GetNoteDecryptor(const libzcash::PaymentAddress &address, ZCNoteDecryption &decOut) const
148 LOCK(cs_SpendingKeyStore);
149 NoteDecryptorMap::const_iterator mi = mapNoteDecryptors.find(address);
150 if (mi != mapNoteDecryptors.end())
158 void GetPaymentAddresses(std::set<libzcash::PaymentAddress> &setAddress) const
162 LOCK(cs_SpendingKeyStore);
163 SpendingKeyMap::const_iterator mi = mapSpendingKeys.begin();
164 while (mi != mapSpendingKeys.end())
166 setAddress.insert((*mi).first);
173 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
174 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
175 typedef std::map<libzcash::PaymentAddress, std::vector<unsigned char> > CryptedSpendingKeyMap;
177 #endif // BITCOIN_KEYSTORE_H