]> Git Repo - VerusCoin.git/blob - src/keystore.cpp
Auto merge of #3324 - ebfull:sapling-note-encryption, r=ebfull
[VerusCoin.git] / src / keystore.cpp
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.
5
6 #include "keystore.h"
7
8 #include "key.h"
9 #include "util.h"
10
11 #include <boost/foreach.hpp>
12
13 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
14 {
15     CKey key;
16     if (!GetKey(address, key))
17         return false;
18     vchPubKeyOut = key.GetPubKey();
19     return true;
20 }
21
22 bool CKeyStore::AddKey(const CKey &key) {
23     return AddKeyPubKey(key, key.GetPubKey());
24 }
25
26 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
27 {
28     LOCK(cs_KeyStore);
29     mapKeys[pubkey.GetID()] = key;
30     return true;
31 }
32
33 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
34 {
35     if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
36         return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
37
38     LOCK(cs_KeyStore);
39     mapScripts[CScriptID(redeemScript)] = redeemScript;
40     return true;
41 }
42
43 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
44 {
45     LOCK(cs_KeyStore);
46     return mapScripts.count(hash) > 0;
47 }
48
49 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
50 {
51     LOCK(cs_KeyStore);
52     ScriptMap::const_iterator mi = mapScripts.find(hash);
53     if (mi != mapScripts.end())
54     {
55         redeemScriptOut = (*mi).second;
56         return true;
57     }
58     return false;
59 }
60
61 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
62 {
63     LOCK(cs_KeyStore);
64     setWatchOnly.insert(dest);
65     return true;
66 }
67
68 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
69 {
70     LOCK(cs_KeyStore);
71     setWatchOnly.erase(dest);
72     return true;
73 }
74
75 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
76 {
77     LOCK(cs_KeyStore);
78     return setWatchOnly.count(dest) > 0;
79 }
80
81 bool CBasicKeyStore::HaveWatchOnly() const
82 {
83     LOCK(cs_KeyStore);
84     return (!setWatchOnly.empty());
85 }
86
87 bool CBasicKeyStore::AddSpendingKey(const libzcash::SproutSpendingKey &sk)
88 {
89     LOCK(cs_SpendingKeyStore);
90     auto address = sk.address();
91     mapSpendingKeys[address] = sk;
92     mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(sk.receiving_key())));
93     return true;
94 }
95
96 //! Sapling 
97 bool CBasicKeyStore::AddSaplingSpendingKey(const libzcash::SaplingSpendingKey &sk)
98 {
99     LOCK(cs_SpendingKeyStore);
100     auto fvk = sk.full_viewing_key();
101     
102     // if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
103     if (!AddSaplingFullViewingKey(fvk)){
104         return false;
105     }
106     
107     mapSaplingSpendingKeys[fvk] = sk;
108
109     // Add addr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap
110     auto ivk = fvk.in_viewing_key();
111     auto addr = sk.default_address();
112     mapSaplingIncomingViewingKeys[addr] = ivk;
113     
114     return true;
115 }
116
117 bool CBasicKeyStore::AddViewingKey(const libzcash::SproutViewingKey &vk)
118 {
119     LOCK(cs_SpendingKeyStore);
120     auto address = vk.address();
121     mapViewingKeys[address] = vk;
122     mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(vk.sk_enc)));
123     return true;
124 }
125
126 bool CBasicKeyStore::AddSaplingFullViewingKey(const libzcash::SaplingFullViewingKey &fvk)
127 {
128     LOCK(cs_SpendingKeyStore);
129     auto ivk = fvk.in_viewing_key();
130     mapSaplingFullViewingKeys[ivk] = fvk;
131     
132     //! TODO: Note decryptors for Sapling
133     
134     return true;
135 }
136
137 bool CBasicKeyStore::RemoveViewingKey(const libzcash::SproutViewingKey &vk)
138 {
139     LOCK(cs_SpendingKeyStore);
140     mapViewingKeys.erase(vk.address());
141     return true;
142 }
143
144 bool CBasicKeyStore::HaveViewingKey(const libzcash::SproutPaymentAddress &address) const
145 {
146     LOCK(cs_SpendingKeyStore);
147     return mapViewingKeys.count(address) > 0;
148 }
149
150 bool CBasicKeyStore::HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const
151 {
152     LOCK(cs_SpendingKeyStore);
153     return mapSaplingFullViewingKeys.count(ivk) > 0;
154 }
155
156 bool CBasicKeyStore::HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const
157 {
158     LOCK(cs_SpendingKeyStore);
159     return mapSaplingIncomingViewingKeys.count(addr) > 0;
160 }
161
162 bool CBasicKeyStore::GetViewingKey(const libzcash::SproutPaymentAddress &address,
163                                    libzcash::SproutViewingKey &vkOut) const
164 {
165     LOCK(cs_SpendingKeyStore);
166     ViewingKeyMap::const_iterator mi = mapViewingKeys.find(address);
167     if (mi != mapViewingKeys.end()) {
168         vkOut = mi->second;
169         return true;
170     }
171     return false;
172 }
173
174 bool CBasicKeyStore::GetSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk,
175                                    libzcash::SaplingFullViewingKey &fvkOut) const
176 {
177     LOCK(cs_SpendingKeyStore);
178     SaplingFullViewingKeyMap::const_iterator mi = mapSaplingFullViewingKeys.find(ivk);
179     if (mi != mapSaplingFullViewingKeys.end()) {
180         fvkOut = mi->second;
181         return true;
182     }
183     return false;
184 }
185
186 bool CBasicKeyStore::GetSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr,
187                                    libzcash::SaplingIncomingViewingKey &ivkOut) const
188 {
189     LOCK(cs_SpendingKeyStore);
190     SaplingIncomingViewingKeyMap::const_iterator mi = mapSaplingIncomingViewingKeys.find(addr);
191     if (mi != mapSaplingIncomingViewingKeys.end()) {
192         ivkOut = mi->second;
193         return true;
194     }
195     return false;
196 }
This page took 0.032198 seconds and 4 git commands to generate.