forked from navcoin/navcoin-core
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeystore.h
325 lines (270 loc) · 11.6 KB
/
keystore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef NAVCOIN_KEYSTORE_H
#define NAVCOIN_KEYSTORE_H
#include <blsct/key.h>
#include <key.h>
#include <pubkey.h>
#include <script/script.h>
#include <script/standard.h>
#include <sync.h>
#include <util.h>
#include <boost/signals2/signal.hpp>
#include <boost/variant.hpp>
/** A virtual base class for key stores */
class CKeyStore
{
protected:
mutable CCriticalSection cs_KeyStore;
public:
virtual ~CKeyStore() {}
//! Add a key to the store.
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
virtual bool AddKey(const CKey &key);
virtual bool AddBLSCTBlindingKeyPubKey(const blsctKey& key, const blsctPublicKey &pubkey) =0;
virtual bool AddBLSCTBlindingKey(const blsctKey &key) = 0;
virtual bool AddBLSCTSubAddress(const CKeyID &hashId, const std::pair<uint64_t, uint64_t>& index) =0;
//! Check whether a key corresponding to a given address is present in the store.
virtual bool HaveKey(const CKeyID &address) const =0;
virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const =0;
virtual bool HaveBLSCTBlindingKey(const blsctPublicKey &pk) const =0;
virtual bool GetBLSCTHashId(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, CKeyID& hashId) const =0;
virtual bool HaveBLSCTSubAddress(const CKeyID &hashId) const =0;
virtual bool HaveBLSCTSubAddress(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey) const =0;
virtual bool GetBLSCTBlindingKey(const blsctPublicKey &pk, blsctKey &k) const =0;
virtual bool GetBLSCTSubAddressIndex(const CKeyID &hashId, std::pair<uint64_t, uint64_t>& index) const =0;
virtual bool GetBLSCTSubAddressIndex(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, std::pair<uint64_t, uint64_t>& index) const =0;
virtual bool GetBLSCTSubAddressPublicKeys(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, blsctDoublePublicKey& pk) const =0;
virtual bool GetBLSCTSubAddressPublicKeys(const CKeyID &hashId, blsctDoublePublicKey& pk) const =0;
virtual bool GetBLSCTSubAddressPublicKeys(const std::pair<uint64_t, uint64_t>& index, blsctDoublePublicKey& pk) const =0;
virtual bool GetBLSCTSubAddressSpendingKeyForOutput(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, blsctKey& k) const =0;
virtual bool GetBLSCTSubAddressSpendingKeyForOutput(const CKeyID &hashId, const std::vector<unsigned char>& outputKey, blsctKey& k) const =0;
virtual bool GetBLSCTSubAddressSpendingKeyForOutput(const std::pair<uint64_t, uint64_t>& index, const std::vector<unsigned char>& outputKey,blsctKey& k) const =0;
//! Support for BIP 0013 : see https://github.com/navcoin/bips/blob/master/bip-0013.mediawiki
virtual bool AddCScript(const CScript& redeemScript) =0;
virtual bool HaveCScript(const CScriptID &hash) const =0;
virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
//! Support for Watch-only addresses
virtual bool AddWatchOnly(const CScript &dest) =0;
virtual bool RemoveWatchOnly(const CScript &dest) =0;
virtual bool HaveWatchOnly(const CScript &dest) const =0;
virtual bool HaveWatchOnly() const =0;
//! Support for BLSCT address
virtual bool GetBLSCTViewKey(blsctKey& k) const =0;
virtual bool GetBLSCTSpendKey(blsctKey& k) const =0;
virtual bool GetBLSCTDoublePublicKey(blsctDoublePublicKey& k) const =0;
virtual bool GetBLSCTBlindingMasterKey(blsctKey& k) const =0;
virtual bool SetBLSCTViewKey(const blsctKey& v) =0;
virtual bool SetBLSCTSpendKey(const blsctKey& v) =0;
virtual bool SetBLSCTDoublePublicKey(const blsctDoublePublicKey& k) =0;
virtual bool SetBLSCTBlindingMasterKey(const blsctKey& k) =0;
};
typedef std::map<CKeyID, CKey> KeyMap;
typedef std::map<CKeyID, blsctKey> BLSCTBlindingKeyMap;
typedef std::map<CKeyID, std::pair<uint64_t, uint64_t>> BLSCTSubAddressMap;
typedef std::map<CKeyID, CPubKey> WatchKeyMap;
typedef std::map<CScriptID, CScript > ScriptMap;
typedef std::set<CScript> WatchOnlySet;
/** Basic key store, that keeps keys in an address->secret map */
class CBasicKeyStore : public CKeyStore
{
protected:
KeyMap mapKeys;
BLSCTBlindingKeyMap mapBLSCTBlindingKeys;
BLSCTSubAddressMap mapBLSCTSubAddresses;
WatchKeyMap mapWatchKeys;
ScriptMap mapScripts;
WatchOnlySet setWatchOnly;
blsctKey privateBlsViewKey;
blsctKey privateBlsSpendKey;
blsctDoublePublicKey publicBlsKey;
blsctKey privateBlsBlindingKey;
public:
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
bool AddBLSCTBlindingKeyPubKey(const blsctKey& key, const blsctPublicKey &pubkey);
bool AddBLSCTBlindingKey(const blsctKey &key);
bool AddBLSCTSubAddress(const CKeyID &hashId, const std::pair<uint64_t, uint64_t>& index);
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
bool HaveKey(const CKeyID &address) const
{
bool result;
{
LOCK(cs_KeyStore);
result = (mapKeys.count(address) > 0);
}
return result;
}
bool HaveBLSCTBlindingKey(const blsctPublicKey &address) const;
bool HaveBLSCTSubAddress(const CKeyID &hashId) const
{
{
LOCK(cs_KeyStore);
for(auto& it: mapBLSCTSubAddresses)
{
if (it.first == hashId) return true;
}
}
return false;
}
bool HaveBLSCTSubAddress(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey) const
{
CKeyID hashId;
if (!GetBLSCTHashId(outputKey, spendingKey, hashId))
return false;
return HaveBLSCTSubAddress(hashId);
}
bool GetBLSCTHashId(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, CKeyID& hashId) const;
void GetKeys(std::set<CKeyID> &setAddress) const
{
setAddress.clear();
{
LOCK(cs_KeyStore);
KeyMap::const_iterator mi = mapKeys.begin();
while (mi != mapKeys.end())
{
setAddress.insert((*mi).first);
mi++;
}
}
}
bool GetKey(const CKeyID &address, CKey &keyOut) const
{
{
LOCK(cs_KeyStore);
KeyMap::const_iterator mi = mapKeys.find(address);
if (mi != mapKeys.end())
{
keyOut = mi->second;
return true;
}
}
return false;
}
bool GetBLSCTBlindingKey(const blsctPublicKey &address, blsctKey &keyOut) const
{
{
LOCK(cs_KeyStore);
BLSCTBlindingKeyMap::const_iterator mi = mapBLSCTBlindingKeys.find(address.GetID());
if (mi != mapBLSCTBlindingKeys.end())
{
keyOut = mi->second;
return true;
}
}
return false;
}
bool GetBLSCTSubAddressIndex(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, std::pair<uint64_t, uint64_t>& index) const
{
CKeyID hashId;
if (!GetBLSCTHashId(outputKey, spendingKey, hashId))
return false;
return GetBLSCTSubAddressIndex(hashId, index);
}
bool GetBLSCTSubAddressIndex(const CKeyID &hashId, std::pair<uint64_t, uint64_t>& index) const
{
{
LOCK(cs_KeyStore);
for(auto& mi: mapBLSCTSubAddresses)
{
if (mi.first == hashId)
{
index = mi.second;
return true;
}
}
}
return false;
}
bool GetBLSCTSubAddressSpendingKeyForOutput(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, blsctKey& k) const
{
CKeyID hashId;
if (!GetBLSCTHashId(outputKey, spendingKey, hashId))
return false;
return GetBLSCTSubAddressSpendingKeyForOutput(hashId, outputKey, k);
}
bool GetBLSCTSubAddressSpendingKeyForOutput(const CKeyID &hashId, const std::vector<unsigned char>& outputKey, blsctKey& k) const
{
std::pair<uint64_t, uint64_t> index;
if (!GetBLSCTSubAddressIndex(hashId, index))
return false;
return GetBLSCTSubAddressSpendingKeyForOutput(index, outputKey, k);
}
bool GetBLSCTSubAddressSpendingKeyForOutput(const std::pair<uint64_t, uint64_t>& index, const std::vector<unsigned char>& outputKey, blsctKey& k) const;
bool GetBLSCTSubAddressPublicKeys(const std::vector<unsigned char>& outputKey, const std::vector<unsigned char>& spendingKey, blsctDoublePublicKey& pk) const
{
CKeyID hashId;
if (!GetBLSCTHashId(outputKey, spendingKey, hashId))
return false;
return GetBLSCTSubAddressPublicKeys(hashId, pk);
}
bool GetBLSCTSubAddressPublicKeys(const CKeyID &hashId, blsctDoublePublicKey& pk) const
{
std::pair<uint64_t, uint64_t> index;
if (!GetBLSCTSubAddressIndex(hashId, index))
return false;
return GetBLSCTSubAddressPublicKeys(index, pk);
}
bool GetBLSCTSubAddressPublicKeys(const std::pair<uint64_t, uint64_t>& index, blsctDoublePublicKey& pk) const;
bool GetBLSCTViewKey(blsctKey& zk) const {
if(!privateBlsViewKey.IsValid())
return false;
zk = privateBlsViewKey;
return true;
}
bool GetBLSCTBlindingMasterKey(blsctKey& zk) const {
if(!privateBlsBlindingKey.IsValid())
return false;
zk = privateBlsBlindingKey;
return true;
}
bool GetBLSCTSpendKey(blsctKey& zk) const {
if(!privateBlsSpendKey.IsValid())
return false;
zk = privateBlsSpendKey;
return true;
}
bool GetBLSCTDoublePublicKey(blsctDoublePublicKey& zk) const {
if(!publicBlsKey.IsValid())
return false;
zk = publicBlsKey;
return true;
}
bool SetBLSCTViewKey(const blsctKey& v) {
if(!v.IsValid())
return false;
privateBlsViewKey = v;
return true;
}
bool SetBLSCTBlindingMasterKey(const blsctKey& v) {
if(!v.IsValid())
return false;
privateBlsBlindingKey = v;
return true;
}
bool SetBLSCTSpendKey(const blsctKey& s) {
if(!s.IsValid())
return false;
privateBlsSpendKey = s;
return true;
}
bool SetBLSCTDoublePublicKey(const blsctDoublePublicKey &k) {
if (!k.IsValid())
return false;
publicBlsKey = k;
return true;
}
virtual bool AddCScript(const CScript& redeemScript);
virtual bool HaveCScript(const CScriptID &hash) const;
virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
virtual bool AddWatchOnly(const CScript &dest);
virtual bool RemoveWatchOnly(const CScript &dest);
virtual bool HaveWatchOnly(const CScript &dest) const;
virtual bool HaveWatchOnly() const;
};
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
#endif // NAVCOIN_KEYSTORE_H