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.
11 #include <boost/foreach.hpp>
13 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
16 if (!GetKey(address, key))
18 vchPubKeyOut = key.GetPubKey();
22 bool CKeyStore::AddKey(const CKey &key) {
23 return AddKeyPubKey(key, key.GetPubKey());
26 bool CBasicKeyStore::SetHDSeed(const HDSeed& seed)
28 LOCK(cs_SpendingKeyStore);
29 if (!hdSeed.IsNull()) {
30 // Don't allow an existing seed to be changed. We can maybe relax this
31 // restriction later once we have worked out the UX implications.
38 bool CBasicKeyStore::HaveHDSeed() const
40 LOCK(cs_SpendingKeyStore);
41 return !hdSeed.IsNull();
44 bool CBasicKeyStore::GetHDSeed(HDSeed& seedOut) const
46 LOCK(cs_SpendingKeyStore);
47 if (hdSeed.IsNull()) {
55 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
58 mapKeys[pubkey.GetID()] = key;
62 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
64 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
65 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
68 mapScripts[CScriptID(redeemScript)] = redeemScript;
72 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
75 return mapScripts.count(hash) > 0;
78 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
81 ScriptMap::const_iterator mi = mapScripts.find(hash);
82 if (mi != mapScripts.end())
84 redeemScriptOut = (*mi).second;
90 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
93 setWatchOnly.insert(dest);
97 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
100 setWatchOnly.erase(dest);
104 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
107 return setWatchOnly.count(dest) > 0;
110 bool CBasicKeyStore::HaveWatchOnly() const
113 return (!setWatchOnly.empty());
116 bool CBasicKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk)
118 LOCK(cs_SpendingKeyStore);
119 auto address = sk.address();
120 mapSproutSpendingKeys[address] = sk;
121 mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(sk.receiving_key())));
126 bool CBasicKeyStore::AddSaplingSpendingKey(
127 const libzcash::SaplingExtendedSpendingKey &sk,
128 const libzcash::SaplingPaymentAddress &defaultAddr)
130 LOCK(cs_SpendingKeyStore);
131 auto fvk = sk.expsk.full_viewing_key();
133 // if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
134 if (!AddSaplingFullViewingKey(fvk, defaultAddr)) {
138 mapSaplingSpendingKeys[fvk] = sk;
143 bool CBasicKeyStore::AddSproutViewingKey(const libzcash::SproutViewingKey &vk)
145 LOCK(cs_SpendingKeyStore);
146 auto address = vk.address();
147 mapSproutViewingKeys[address] = vk;
148 mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(vk.sk_enc)));
152 bool CBasicKeyStore::AddSaplingFullViewingKey(
153 const libzcash::SaplingFullViewingKey &fvk,
154 const libzcash::SaplingPaymentAddress &defaultAddr)
156 LOCK(cs_SpendingKeyStore);
157 auto ivk = fvk.in_viewing_key();
158 mapSaplingFullViewingKeys[ivk] = fvk;
160 return CBasicKeyStore::AddSaplingIncomingViewingKey(ivk, defaultAddr);
163 // This function updates the wallet's internal address->ivk map.
164 // If we add an address that is already in the map, the map will
165 // remain unchanged as each address only has one ivk.
166 bool CBasicKeyStore::AddSaplingIncomingViewingKey(
167 const libzcash::SaplingIncomingViewingKey &ivk,
168 const libzcash::SaplingPaymentAddress &addr)
170 LOCK(cs_SpendingKeyStore);
172 // Add addr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap
173 mapSaplingIncomingViewingKeys[addr] = ivk;
178 bool CBasicKeyStore::RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk)
180 LOCK(cs_SpendingKeyStore);
181 mapSproutViewingKeys.erase(vk.address());
185 bool CBasicKeyStore::HaveSproutViewingKey(const libzcash::SproutPaymentAddress &address) const
187 LOCK(cs_SpendingKeyStore);
188 return mapSproutViewingKeys.count(address) > 0;
191 bool CBasicKeyStore::HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const
193 LOCK(cs_SpendingKeyStore);
194 return mapSaplingFullViewingKeys.count(ivk) > 0;
197 bool CBasicKeyStore::HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const
199 LOCK(cs_SpendingKeyStore);
200 return mapSaplingIncomingViewingKeys.count(addr) > 0;
203 bool CBasicKeyStore::GetSproutViewingKey(
204 const libzcash::SproutPaymentAddress &address,
205 libzcash::SproutViewingKey &vkOut) const
207 LOCK(cs_SpendingKeyStore);
208 SproutViewingKeyMap::const_iterator mi = mapSproutViewingKeys.find(address);
209 if (mi != mapSproutViewingKeys.end()) {
216 bool CBasicKeyStore::GetSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk,
217 libzcash::SaplingFullViewingKey &fvkOut) const
219 LOCK(cs_SpendingKeyStore);
220 SaplingFullViewingKeyMap::const_iterator mi = mapSaplingFullViewingKeys.find(ivk);
221 if (mi != mapSaplingFullViewingKeys.end()) {
228 bool CBasicKeyStore::GetSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr,
229 libzcash::SaplingIncomingViewingKey &ivkOut) const
231 LOCK(cs_SpendingKeyStore);
232 SaplingIncomingViewingKeyMap::const_iterator mi = mapSaplingIncomingViewingKeys.find(addr);
233 if (mi != mapSaplingIncomingViewingKeys.end()) {
240 bool CBasicKeyStore::GetSaplingExtendedSpendingKey(const libzcash::SaplingPaymentAddress &addr,
241 libzcash::SaplingExtendedSpendingKey &extskOut) const {
242 libzcash::SaplingIncomingViewingKey ivk;
243 libzcash::SaplingFullViewingKey fvk;
245 return GetSaplingIncomingViewingKey(addr, ivk) &&
246 GetSaplingFullViewingKey(ivk, fvk) &&
247 GetSaplingSpendingKey(fvk, extskOut);