]>
Commit | Line | Data |
---|---|---|
b2120e22 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
2b173d3b | 3 | // Distributed under the MIT software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
51ed9ec9 | 5 | |
e89b9f6a PW |
6 | #ifndef BITCOIN_KEYSTORE_H |
7 | #define BITCOIN_KEYSTORE_H | |
8 | ||
51ed9ec9 | 9 | #include "key.h" |
d2e74c55 | 10 | #include "pubkey.h" |
5a6155cc CF |
11 | #include "script/script.h" |
12 | #include "script/standard.h" | |
7f3ccb59 | 13 | #include "sync.h" |
7c929cf5 | 14 | #include "zcash/Address.hpp" |
02e67455 | 15 | #include "zcash/NoteEncryption.hpp" |
51ed9ec9 | 16 | |
ab1b288f | 17 | #include <boost/signals2/signal.hpp> |
c8988460 | 18 | #include <boost/variant.hpp> |
6b6aaa16 | 19 | |
6b8de05d | 20 | /** A virtual base class for key stores */ |
64c7ee7e PW |
21 | class CKeyStore |
22 | { | |
6cc4a62c | 23 | protected: |
acd65016 | 24 | mutable CCriticalSection cs_KeyStore; |
0d738691 | 25 | mutable CCriticalSection cs_SpendingKeyStore; |
acd65016 | 26 | |
6cc4a62c | 27 | public: |
2e120f28 WL |
28 | virtual ~CKeyStore() {} |
29 | ||
2b173d3b | 30 | //! Add a key to the store. |
dfa23b94 PW |
31 | virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0; |
32 | virtual bool AddKey(const CKey &key); | |
d825e6a3 | 33 | |
2b173d3b | 34 | //! Check whether a key corresponding to a given address is present in the store. |
10254401 PW |
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; | |
d825e6a3 | 39 | |
2b173d3b | 40 | //! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki |
922e8e29 | 41 | virtual bool AddCScript(const CScript& redeemScript) =0; |
10254401 PW |
42 | virtual bool HaveCScript(const CScriptID &hash) const =0; |
43 | virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0; | |
c8988460 | 44 | |
2b173d3b | 45 | //! Support for Watch-only addresses |
d5087d1b | 46 | virtual bool AddWatchOnly(const CScript &dest) =0; |
ccca27a7 | 47 | virtual bool RemoveWatchOnly(const CScript &dest) =0; |
d5087d1b | 48 | virtual bool HaveWatchOnly(const CScript &dest) const =0; |
939ed973 | 49 | virtual bool HaveWatchOnly() const =0; |
7c929cf5 JG |
50 | |
51 | //! Add a spending key to the store. | |
0bfdb962 | 52 | virtual bool AddSpendingKey(const libzcash::SpendingKey &sk) =0; |
7c929cf5 JG |
53 | |
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; | |
b5c06c83 | 56 | virtual bool GetSpendingKey(const libzcash::PaymentAddress &address, libzcash::SpendingKey& skOut) const =0; |
7c929cf5 | 57 | virtual void GetPaymentAddresses(std::set<libzcash::PaymentAddress> &setAddress) const =0; |
acd65016 PW |
58 | }; |
59 | ||
dfa23b94 | 60 | typedef std::map<CKeyID, CKey> KeyMap; |
10254401 | 61 | typedef std::map<CScriptID, CScript > ScriptMap; |
d5087d1b | 62 | typedef std::set<CScript> WatchOnlySet; |
7c929cf5 | 63 | typedef std::map<libzcash::PaymentAddress, libzcash::SpendingKey> SpendingKeyMap; |
02e67455 | 64 | typedef std::map<libzcash::PaymentAddress, ZCNoteDecryption> NoteDecryptorMap; |
4e87d341 | 65 | |
6b8de05d | 66 | /** Basic key store, that keeps keys in an address->secret map */ |
acd65016 PW |
67 | class CBasicKeyStore : public CKeyStore |
68 | { | |
69 | protected: | |
4e87d341 | 70 | KeyMap mapKeys; |
2a45a494 | 71 | ScriptMap mapScripts; |
c8988460 | 72 | WatchOnlySet setWatchOnly; |
7c929cf5 | 73 | SpendingKeyMap mapSpendingKeys; |
02e67455 | 74 | NoteDecryptorMap mapNoteDecryptors; |
acd65016 PW |
75 | |
76 | public: | |
dfa23b94 | 77 | bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey); |
10254401 | 78 | bool HaveKey(const CKeyID &address) const |
64c7ee7e | 79 | { |
6cc4a62c | 80 | bool result; |
f8dcd5ca PW |
81 | { |
82 | LOCK(cs_KeyStore); | |
6cc4a62c | 83 | result = (mapKeys.count(address) > 0); |
f8dcd5ca | 84 | } |
6cc4a62c | 85 | return result; |
64c7ee7e | 86 | } |
10254401 | 87 | void GetKeys(std::set<CKeyID> &setAddress) const |
93db3fce PW |
88 | { |
89 | setAddress.clear(); | |
93db3fce | 90 | { |
f8dcd5ca | 91 | LOCK(cs_KeyStore); |
93db3fce PW |
92 | KeyMap::const_iterator mi = mapKeys.begin(); |
93 | while (mi != mapKeys.end()) | |
94 | { | |
95 | setAddress.insert((*mi).first); | |
96 | mi++; | |
97 | } | |
98 | } | |
99 | } | |
10254401 | 100 | bool GetKey(const CKeyID &address, CKey &keyOut) const |
64c7ee7e | 101 | { |
98705aa5 | 102 | { |
f8dcd5ca | 103 | LOCK(cs_KeyStore); |
6cc4a62c GA |
104 | KeyMap::const_iterator mi = mapKeys.find(address); |
105 | if (mi != mapKeys.end()) | |
106 | { | |
dfa23b94 | 107 | keyOut = mi->second; |
6cc4a62c GA |
108 | return true; |
109 | } | |
98705aa5 PW |
110 | } |
111 | return false; | |
64c7ee7e | 112 | } |
922e8e29 | 113 | virtual bool AddCScript(const CScript& redeemScript); |
10254401 PW |
114 | virtual bool HaveCScript(const CScriptID &hash) const; |
115 | virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const; | |
c8988460 | 116 | |
d5087d1b | 117 | virtual bool AddWatchOnly(const CScript &dest); |
ccca27a7 | 118 | virtual bool RemoveWatchOnly(const CScript &dest); |
d5087d1b | 119 | virtual bool HaveWatchOnly(const CScript &dest) const; |
939ed973 | 120 | virtual bool HaveWatchOnly() const; |
7c929cf5 | 121 | |
0bfdb962 | 122 | bool AddSpendingKey(const libzcash::SpendingKey &sk); |
7c929cf5 JG |
123 | bool HaveSpendingKey(const libzcash::PaymentAddress &address) const |
124 | { | |
125 | bool result; | |
126 | { | |
0d738691 | 127 | LOCK(cs_SpendingKeyStore); |
7c929cf5 JG |
128 | result = (mapSpendingKeys.count(address) > 0); |
129 | } | |
130 | return result; | |
131 | } | |
b5c06c83 | 132 | bool GetSpendingKey(const libzcash::PaymentAddress &address, libzcash::SpendingKey &skOut) const |
7c929cf5 JG |
133 | { |
134 | { | |
0d738691 | 135 | LOCK(cs_SpendingKeyStore); |
7c929cf5 JG |
136 | SpendingKeyMap::const_iterator mi = mapSpendingKeys.find(address); |
137 | if (mi != mapSpendingKeys.end()) | |
138 | { | |
b5c06c83 | 139 | skOut = mi->second; |
7c929cf5 JG |
140 | return true; |
141 | } | |
142 | } | |
143 | return false; | |
144 | } | |
02e67455 JG |
145 | bool GetNoteDecryptor(const libzcash::PaymentAddress &address, ZCNoteDecryption &decOut) const |
146 | { | |
147 | { | |
3fac1020 | 148 | LOCK(cs_SpendingKeyStore); |
02e67455 JG |
149 | NoteDecryptorMap::const_iterator mi = mapNoteDecryptors.find(address); |
150 | if (mi != mapNoteDecryptors.end()) | |
151 | { | |
152 | decOut = mi->second; | |
153 | return true; | |
154 | } | |
155 | } | |
156 | return false; | |
157 | } | |
7c929cf5 JG |
158 | void GetPaymentAddresses(std::set<libzcash::PaymentAddress> &setAddress) const |
159 | { | |
160 | setAddress.clear(); | |
161 | { | |
0d738691 | 162 | LOCK(cs_SpendingKeyStore); |
7c929cf5 JG |
163 | SpendingKeyMap::const_iterator mi = mapSpendingKeys.begin(); |
164 | while (mi != mapSpendingKeys.end()) | |
165 | { | |
166 | setAddress.insert((*mi).first); | |
167 | mi++; | |
168 | } | |
169 | } | |
170 | } | |
acd65016 PW |
171 | }; |
172 | ||
51ed9ec9 | 173 | typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial; |
10254401 | 174 | typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap; |
16d140f4 | 175 | typedef std::map<libzcash::PaymentAddress, std::vector<unsigned char> > CryptedSpendingKeyMap; |
03fbd790 | 176 | |
093303a8 | 177 | #endif // BITCOIN_KEYSTORE_H |