]> Git Repo - VerusCoin.git/blob - src/keystore.cpp
Auto merge of #2674 - str4d:2469-ci-workers-macos, r=str4d
[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::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk)
88 {
89     LOCK(cs_SpendingKeyStore);
90     auto address = sk.address();
91     mapSproutSpendingKeys[address] = sk;
92     mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(sk.receiving_key())));
93     return true;
94 }
95
96 //! Sapling 
97 bool CBasicKeyStore::AddSaplingSpendingKey(
98     const libzcash::SaplingSpendingKey &sk,
99     const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr)
100 {
101     LOCK(cs_SpendingKeyStore);
102     auto fvk = sk.full_viewing_key();
103
104     // if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
105     if (!AddSaplingFullViewingKey(fvk, defaultAddr)){
106         return false;
107     }
108
109     mapSaplingSpendingKeys[fvk] = sk;
110
111     return true;
112 }
113
114 bool CBasicKeyStore::AddSproutViewingKey(const libzcash::SproutViewingKey &vk)
115 {
116     LOCK(cs_SpendingKeyStore);
117     auto address = vk.address();
118     mapSproutViewingKeys[address] = vk;
119     mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(vk.sk_enc)));
120     return true;
121 }
122
123 bool CBasicKeyStore::AddSaplingFullViewingKey(
124     const libzcash::SaplingFullViewingKey &fvk,
125     const boost::optional<libzcash::SaplingPaymentAddress> &defaultAddr)
126 {
127     LOCK(cs_SpendingKeyStore);
128     auto ivk = fvk.in_viewing_key();
129     mapSaplingFullViewingKeys[ivk] = fvk;
130
131     if (defaultAddr) {
132         // Add defaultAddr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap
133         mapSaplingIncomingViewingKeys[defaultAddr.get()] = ivk;
134     }
135     
136     return true;
137 }
138
139 bool CBasicKeyStore::RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk)
140 {
141     LOCK(cs_SpendingKeyStore);
142     mapSproutViewingKeys.erase(vk.address());
143     return true;
144 }
145
146 bool CBasicKeyStore::HaveSproutViewingKey(const libzcash::SproutPaymentAddress &address) const
147 {
148     LOCK(cs_SpendingKeyStore);
149     return mapSproutViewingKeys.count(address) > 0;
150 }
151
152 bool CBasicKeyStore::HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const
153 {
154     LOCK(cs_SpendingKeyStore);
155     return mapSaplingFullViewingKeys.count(ivk) > 0;
156 }
157
158 bool CBasicKeyStore::HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const
159 {
160     LOCK(cs_SpendingKeyStore);
161     return mapSaplingIncomingViewingKeys.count(addr) > 0;
162 }
163
164 bool CBasicKeyStore::GetSproutViewingKey(
165     const libzcash::SproutPaymentAddress &address,
166     libzcash::SproutViewingKey &vkOut) const
167 {
168     LOCK(cs_SpendingKeyStore);
169     SproutViewingKeyMap::const_iterator mi = mapSproutViewingKeys.find(address);
170     if (mi != mapSproutViewingKeys.end()) {
171         vkOut = mi->second;
172         return true;
173     }
174     return false;
175 }
176
177 bool CBasicKeyStore::GetSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk,
178                                    libzcash::SaplingFullViewingKey &fvkOut) const
179 {
180     LOCK(cs_SpendingKeyStore);
181     SaplingFullViewingKeyMap::const_iterator mi = mapSaplingFullViewingKeys.find(ivk);
182     if (mi != mapSaplingFullViewingKeys.end()) {
183         fvkOut = mi->second;
184         return true;
185     }
186     return false;
187 }
188
189 bool CBasicKeyStore::GetSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr,
190                                    libzcash::SaplingIncomingViewingKey &ivkOut) const
191 {
192     LOCK(cs_SpendingKeyStore);
193     SaplingIncomingViewingKeyMap::const_iterator mi = mapSaplingIncomingViewingKeys.find(addr);
194     if (mi != mapSaplingIncomingViewingKeys.end()) {
195         ivkOut = mi->second;
196         return true;
197     }
198     return false;
199 }
This page took 0.036702 seconds and 4 git commands to generate.