forked from PIVX-Project/PIVX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystore.h
159 lines (130 loc) · 7.06 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
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Copyright (c) 2017-2021 The PIVX Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_KEYSTORE_H
#define BITCOIN_KEYSTORE_H
#include "key.h"
#include "pubkey.h"
#include "sapling/address.h"
#include "sapling/zip32.h"
#include "sync.h"
#include <boost/signals2/signal.hpp>
class CScript;
class CScriptID;
/** A virtual base class for key stores */
class CKeyStore
{
public:
// todo: Make it protected again once we are more advanced in the wallet/spkm decoupling.
mutable RecursiveMutex cs_KeyStore;
virtual ~CKeyStore() {}
//! Add a key to the store.
virtual bool AddKeyPubKey(const CKey& key, const CPubKey& pubkey) = 0;
virtual bool AddKey(const CKey& key);
//! 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 std::set<CKeyID> GetKeys() const = 0;
virtual bool GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) const = 0;
//! Support for BIP 0013 : see https://github.com/bitcoin/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 Sapling
// Add a Sapling spending key to the store.
virtual bool AddSaplingSpendingKey(const libzcash::SaplingExtendedSpendingKey &sk) = 0;
// Check whether a Sapling spending key corresponding to a given Sapling viewing key is present in the store.
virtual bool HaveSaplingSpendingKey(
const libzcash::SaplingExtendedFullViewingKey &extfvk) const = 0;
virtual bool GetSaplingSpendingKey(
const libzcash::SaplingExtendedFullViewingKey &extfvk,
libzcash::SaplingExtendedSpendingKey& skOut) const = 0;
//! Support for Sapling full viewing keys
virtual bool AddSaplingFullViewingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk) = 0;
virtual bool HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const = 0;
virtual bool GetSaplingFullViewingKey(
const libzcash::SaplingIncomingViewingKey &ivk,
libzcash::SaplingExtendedFullViewingKey& extfvkOut) const = 0;
virtual void GetSaplingPaymentAddresses(std::set<libzcash::SaplingPaymentAddress> &setAddress) const = 0;
//! Sapling incoming viewing keys
virtual bool AddSaplingIncomingViewingKey(
const libzcash::SaplingIncomingViewingKey &ivk,
const libzcash::SaplingPaymentAddress &addr) = 0;
virtual bool HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const = 0;
virtual bool GetSaplingIncomingViewingKey(
const libzcash::SaplingPaymentAddress &addr,
libzcash::SaplingIncomingViewingKey& ivkOut) const = 0;
};
typedef std::map<CKeyID, CKey> KeyMap;
typedef std::map<CKeyID, CPubKey> WatchKeyMap;
typedef std::map<CScriptID, CScript> ScriptMap;
typedef std::set<CScript> WatchOnlySet;
// Full viewing key has equivalent functionality to a transparent address
// When encrypting wallet, encrypt SaplingSpendingKeyMap, while leaving SaplingFullViewingKeyMap unencrypted
typedef std::map<
libzcash::SaplingExtendedFullViewingKey,
libzcash::SaplingExtendedSpendingKey> SaplingSpendingKeyMap;
typedef std::map<
libzcash::SaplingIncomingViewingKey,
libzcash::SaplingExtendedFullViewingKey> SaplingFullViewingKeyMap;
// Only maps from default addresses to ivk, may need to be reworked when adding diversified addresses.
typedef std::map<libzcash::SaplingPaymentAddress, libzcash::SaplingIncomingViewingKey> SaplingIncomingViewingKeyMap;
/** Basic key store, that keeps keys in an address->secret map */
class CBasicKeyStore : public CKeyStore
{
protected:
KeyMap mapKeys;
WatchKeyMap mapWatchKeys;
ScriptMap mapScripts;
WatchOnlySet setWatchOnly;
public:
// todo future: Move every Sapling map to the new sspkm box.
SaplingSpendingKeyMap mapSaplingSpendingKeys;
SaplingFullViewingKeyMap mapSaplingFullViewingKeys;
SaplingIncomingViewingKeyMap mapSaplingIncomingViewingKeys;
bool AddKeyPubKey(const CKey& key, const CPubKey& pubkey);
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
bool HaveKey(const CKeyID& address) const;
bool HaveKey(const CExchangeKeyID& address) const;
std::set<CKeyID> GetKeys() const;
bool GetKey(const CKeyID& address, CKey& keyOut) const;
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;
//! Sapling
bool AddSaplingSpendingKey(const libzcash::SaplingExtendedSpendingKey &sk);
bool HaveSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk) const;
bool GetSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, libzcash::SaplingExtendedSpendingKey &skOut) const;
virtual bool AddSaplingFullViewingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk);
virtual bool HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const;
virtual bool GetSaplingFullViewingKey(
const libzcash::SaplingIncomingViewingKey &ivk,
libzcash::SaplingExtendedFullViewingKey& extfvkOut) const;
virtual bool AddSaplingIncomingViewingKey(
const libzcash::SaplingIncomingViewingKey &ivk,
const libzcash::SaplingPaymentAddress &addr);
virtual bool HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const;
virtual bool GetSaplingIncomingViewingKey(
const libzcash::SaplingPaymentAddress &addr,
libzcash::SaplingIncomingViewingKey& ivkOut) const;
bool GetSaplingExtendedSpendingKey(
const libzcash::SaplingPaymentAddress &addr,
libzcash::SaplingExtendedSpendingKey &extskOut) const;
void GetSaplingPaymentAddresses(std::set<libzcash::SaplingPaymentAddress> &setAddress) const;
};
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
//! Sapling
typedef std::map<libzcash::SaplingExtendedFullViewingKey, std::vector<unsigned char> > CryptedSaplingSpendingKeyMap;
#endif // BITCOIN_KEYSTORE_H