2011-08-09 04:27:58 -07:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-16 17:47:57 -08:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-10-26 01:35:06 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2011-06-01 09:27:05 -07:00
|
|
|
|
2012-04-15 13:10:54 -07:00
|
|
|
#include "keystore.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
|
|
|
|
#include "key.h"
|
2015-06-10 01:04:08 -07:00
|
|
|
#include "pubkey.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 07:11:09 -07:00
|
|
|
#include "util.h"
|
2011-06-01 09:27:05 -07:00
|
|
|
|
2013-04-12 22:13:08 -07:00
|
|
|
|
2015-06-10 01:04:08 -07:00
|
|
|
bool CKeyStore::AddKey(const CKey &key) {
|
|
|
|
return AddKeyPubKey(key, key.GetPubKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
|
2011-07-05 07:42:32 -07:00
|
|
|
{
|
|
|
|
CKey key;
|
2015-06-10 01:04:08 -07:00
|
|
|
if (!GetKey(address, key)) {
|
2016-04-19 14:03:42 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2015-06-10 01:04:08 -07:00
|
|
|
WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
|
|
|
|
if (it != mapWatchKeys.end()) {
|
|
|
|
vchPubKeyOut = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-05 07:42:32 -07:00
|
|
|
return false;
|
2015-06-10 01:04:08 -07:00
|
|
|
}
|
2011-07-05 07:42:32 -07:00
|
|
|
vchPubKeyOut = key.GetPubKey();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:32:24 -07:00
|
|
|
bool CBasicKeyStore::SetMnemonicSeed(const MnemonicSeed& seed)
|
2018-08-03 08:51:01 -07:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2021-08-19 14:32:24 -07:00
|
|
|
if (mnemonicSeed.has_value()) {
|
2018-09-03 02:45:17 -07:00
|
|
|
// Don't allow an existing seed to be changed. We can maybe relax this
|
|
|
|
// restriction later once we have worked out the UX implications.
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-19 14:32:24 -07:00
|
|
|
mnemonicSeed = seed;
|
2018-08-03 08:51:01 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:32:24 -07:00
|
|
|
bool CBasicKeyStore::HaveMnemonicSeed() const
|
2018-08-03 08:51:01 -07:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2021-08-19 14:32:24 -07:00
|
|
|
return mnemonicSeed.has_value();
|
2018-08-03 08:51:01 -07:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:32:24 -07:00
|
|
|
std::optional<MnemonicSeed> CBasicKeyStore::GetMnemonicSeed() const
|
2018-08-03 08:51:01 -07:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2021-08-19 14:32:24 -07:00
|
|
|
return mnemonicSeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::SetLegacyHDSeed(const HDSeed& seed)
|
|
|
|
{
|
2021-10-28 12:55:18 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2021-08-19 14:32:24 -07:00
|
|
|
if (legacySeed.has_value()) {
|
|
|
|
// Don't allow an existing seed to be changed.
|
2018-08-03 08:51:01 -07:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-19 14:32:24 -07:00
|
|
|
legacySeed = seed;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<HDSeed> CBasicKeyStore::GetLegacyHDSeed() const
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return legacySeed;
|
2018-08-03 08:51:01 -07:00
|
|
|
}
|
|
|
|
|
2013-04-30 21:52:05 -07:00
|
|
|
bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
|
2011-06-01 09:27:05 -07:00
|
|
|
{
|
2013-04-30 21:52:05 -07:00
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
mapKeys[pubkey.GetID()] = key;
|
2011-06-25 05:57:32 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-04 18:40:52 -08:00
|
|
|
bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
|
2011-10-03 10:05:43 -07:00
|
|
|
{
|
2014-03-10 19:43:15 -07:00
|
|
|
if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
|
2015-01-08 02:44:25 -08:00
|
|
|
return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
|
2014-03-10 19:43:15 -07:00
|
|
|
|
2013-04-30 21:52:05 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2014-09-24 19:24:46 -07:00
|
|
|
mapScripts[CScriptID(redeemScript)] = redeemScript;
|
2011-10-03 10:05:43 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-14 14:44:52 -07:00
|
|
|
bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
|
2011-10-03 10:05:43 -07:00
|
|
|
{
|
2013-04-30 21:52:05 -07:00
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return mapScripts.count(hash) > 0;
|
2011-10-03 10:05:43 -07:00
|
|
|
}
|
|
|
|
|
2012-05-14 14:44:52 -07:00
|
|
|
bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
|
2011-10-03 10:05:43 -07:00
|
|
|
{
|
2013-04-30 21:52:05 -07:00
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
ScriptMap::const_iterator mi = mapScripts.find(hash);
|
|
|
|
if (mi != mapScripts.end())
|
2011-10-03 10:05:43 -07:00
|
|
|
{
|
2013-04-30 21:52:05 -07:00
|
|
|
redeemScriptOut = (*mi).second;
|
|
|
|
return true;
|
2011-10-03 10:05:43 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-10 01:04:08 -07:00
|
|
|
static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
|
|
|
|
{
|
|
|
|
//TODO: Use Solver to extract this?
|
|
|
|
CScript::const_iterator pc = dest.begin();
|
|
|
|
opcodetype opcode;
|
|
|
|
std::vector<unsigned char> vch;
|
|
|
|
if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
|
|
|
|
return false;
|
|
|
|
pubKeyOut = CPubKey(vch);
|
|
|
|
if (!pubKeyOut.IsFullyValid())
|
|
|
|
return false;
|
|
|
|
if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-09 12:11:59 -07:00
|
|
|
bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
|
2013-07-25 16:06:01 -07:00
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
setWatchOnly.insert(dest);
|
2015-06-10 01:04:08 -07:00
|
|
|
CPubKey pubKey;
|
|
|
|
if (ExtractPubKey(dest, pubKey))
|
|
|
|
mapWatchKeys[pubKey.GetID()] = pubKey;
|
2013-07-25 16:06:01 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-26 12:05:11 -07:00
|
|
|
bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
setWatchOnly.erase(dest);
|
2015-06-10 01:04:08 -07:00
|
|
|
CPubKey pubKey;
|
|
|
|
if (ExtractPubKey(dest, pubKey))
|
|
|
|
mapWatchKeys.erase(pubKey.GetID());
|
2014-07-26 12:05:11 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-09 12:11:59 -07:00
|
|
|
bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
|
2013-07-25 16:06:01 -07:00
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return setWatchOnly.count(dest) > 0;
|
|
|
|
}
|
2014-07-26 12:05:11 -07:00
|
|
|
|
|
|
|
bool CBasicKeyStore::HaveWatchOnly() const
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return (!setWatchOnly.empty());
|
|
|
|
}
|
2016-08-08 18:34:58 -07:00
|
|
|
|
2018-08-03 02:10:26 -07:00
|
|
|
bool CBasicKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk)
|
2016-08-08 18:34:58 -07:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2016-08-23 20:50:45 -07:00
|
|
|
auto address = sk.address();
|
2018-08-02 17:47:30 -07:00
|
|
|
mapSproutSpendingKeys[address] = sk;
|
2017-02-23 10:27:41 -08:00
|
|
|
mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(sk.receiving_key())));
|
2016-08-08 18:34:58 -07:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-23 19:01:00 -08:00
|
|
|
|
2021-08-19 14:32:24 -07:00
|
|
|
//! Sapling
|
2018-08-02 14:34:31 -07:00
|
|
|
bool CBasicKeyStore::AddSaplingSpendingKey(
|
2020-02-19 04:16:32 -08:00
|
|
|
const libzcash::SaplingExtendedSpendingKey &sk)
|
2018-07-05 14:37:33 -07:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2020-02-18 11:12:15 -08:00
|
|
|
auto extfvk = sk.ToXFVK();
|
2018-08-02 14:34:31 -07:00
|
|
|
|
2020-02-18 11:12:15 -08:00
|
|
|
// if extfvk is not in SaplingFullViewingKeyMap, add it
|
2020-02-19 06:09:09 -08:00
|
|
|
if (!CBasicKeyStore::AddSaplingFullViewingKey(extfvk)) {
|
2018-06-30 12:57:04 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-02 14:34:31 -07:00
|
|
|
|
2020-02-18 11:12:15 -08:00
|
|
|
mapSaplingSpendingKeys[extfvk] = sk;
|
2018-06-30 12:57:04 -07:00
|
|
|
|
2018-07-05 14:37:33 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:16:42 -07:00
|
|
|
bool CBasicKeyStore::AddSproutViewingKey(const libzcash::SproutViewingKey &vk)
|
2017-02-23 19:01:00 -08:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2017-02-23 19:01:00 -08:00
|
|
|
auto address = vk.address();
|
2018-08-02 18:16:42 -07:00
|
|
|
mapSproutViewingKeys[address] = vk;
|
2017-02-23 19:01:00 -08:00
|
|
|
mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(vk.sk_enc)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:34:31 -07:00
|
|
|
bool CBasicKeyStore::AddSaplingFullViewingKey(
|
2020-02-19 04:16:32 -08:00
|
|
|
const libzcash::SaplingExtendedFullViewingKey &extfvk)
|
2018-07-05 14:52:46 -07:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2020-02-18 11:12:15 -08:00
|
|
|
auto ivk = extfvk.fvk.in_viewing_key();
|
|
|
|
mapSaplingFullViewingKeys[ivk] = extfvk;
|
2018-08-02 14:34:31 -07:00
|
|
|
|
2020-02-19 04:16:32 -08:00
|
|
|
return CBasicKeyStore::AddSaplingIncomingViewingKey(ivk, extfvk.DefaultAddress());
|
2018-10-01 08:34:25 -07:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:32:24 -07:00
|
|
|
// This function updates the wallet's internal address->ivk map.
|
2018-10-01 08:34:25 -07:00
|
|
|
// If we add an address that is already in the map, the map will
|
|
|
|
// remain unchanged as each address only has one ivk.
|
|
|
|
bool CBasicKeyStore::AddSaplingIncomingViewingKey(
|
|
|
|
const libzcash::SaplingIncomingViewingKey &ivk,
|
|
|
|
const libzcash::SaplingPaymentAddress &addr)
|
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-10-01 08:34:25 -07:00
|
|
|
|
|
|
|
// Add addr -> SaplingIncomingViewing to SaplingIncomingViewingKeyMap
|
|
|
|
mapSaplingIncomingViewingKeys[addr] = ivk;
|
2018-09-27 14:44:04 -07:00
|
|
|
|
2018-07-05 14:52:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:16:42 -07:00
|
|
|
bool CBasicKeyStore::RemoveSproutViewingKey(const libzcash::SproutViewingKey &vk)
|
2017-02-23 19:01:00 -08:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-08-02 18:16:42 -07:00
|
|
|
mapSproutViewingKeys.erase(vk.address());
|
2017-02-23 19:01:00 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:16:42 -07:00
|
|
|
bool CBasicKeyStore::HaveSproutViewingKey(const libzcash::SproutPaymentAddress &address) const
|
2017-02-23 19:01:00 -08:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-08-02 18:16:42 -07:00
|
|
|
return mapSproutViewingKeys.count(address) > 0;
|
2017-02-23 19:01:00 -08:00
|
|
|
}
|
|
|
|
|
2018-07-05 14:52:46 -07:00
|
|
|
bool CBasicKeyStore::HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const
|
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-07-05 14:52:46 -07:00
|
|
|
return mapSaplingFullViewingKeys.count(ivk) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const
|
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-07-05 14:52:46 -07:00
|
|
|
return mapSaplingIncomingViewingKeys.count(addr) > 0;
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:16:42 -07:00
|
|
|
bool CBasicKeyStore::GetSproutViewingKey(
|
|
|
|
const libzcash::SproutPaymentAddress &address,
|
|
|
|
libzcash::SproutViewingKey &vkOut) const
|
2017-02-23 19:01:00 -08:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-08-02 18:16:42 -07:00
|
|
|
SproutViewingKeyMap::const_iterator mi = mapSproutViewingKeys.find(address);
|
|
|
|
if (mi != mapSproutViewingKeys.end()) {
|
2017-02-23 19:01:00 -08:00
|
|
|
vkOut = mi->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-05 14:52:46 -07:00
|
|
|
|
2020-02-18 11:12:15 -08:00
|
|
|
bool CBasicKeyStore::GetSaplingFullViewingKey(
|
|
|
|
const libzcash::SaplingIncomingViewingKey &ivk,
|
|
|
|
libzcash::SaplingExtendedFullViewingKey &extfvkOut) const
|
2018-07-05 14:52:46 -07:00
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-07-05 14:52:46 -07:00
|
|
|
SaplingFullViewingKeyMap::const_iterator mi = mapSaplingFullViewingKeys.find(ivk);
|
|
|
|
if (mi != mapSaplingFullViewingKeys.end()) {
|
2020-02-18 11:12:15 -08:00
|
|
|
extfvkOut = mi->second;
|
2018-07-05 14:52:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::GetSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr,
|
|
|
|
libzcash::SaplingIncomingViewingKey &ivkOut) const
|
|
|
|
{
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-07-05 14:52:46 -07:00
|
|
|
SaplingIncomingViewingKeyMap::const_iterator mi = mapSaplingIncomingViewingKeys.find(addr);
|
|
|
|
if (mi != mapSaplingIncomingViewingKeys.end()) {
|
|
|
|
ivkOut = mi->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-12 04:01:08 -07:00
|
|
|
|
2021-08-19 14:32:24 -07:00
|
|
|
bool CBasicKeyStore::GetSaplingExtendedSpendingKey(const libzcash::SaplingPaymentAddress &addr,
|
2018-09-12 04:01:08 -07:00
|
|
|
libzcash::SaplingExtendedSpendingKey &extskOut) const {
|
|
|
|
libzcash::SaplingIncomingViewingKey ivk;
|
2020-02-18 11:12:15 -08:00
|
|
|
libzcash::SaplingExtendedFullViewingKey extfvk;
|
2018-09-12 04:01:08 -07:00
|
|
|
|
2019-04-01 09:15:16 -07:00
|
|
|
LOCK(cs_KeyStore);
|
2018-09-12 04:01:08 -07:00
|
|
|
return GetSaplingIncomingViewingKey(addr, ivk) &&
|
2020-02-18 11:12:15 -08:00
|
|
|
GetSaplingFullViewingKey(ivk, extfvk) &&
|
|
|
|
GetSaplingSpendingKey(extfvk, extskOut);
|
2018-09-12 04:01:08 -07:00
|
|
|
}
|
2021-11-30 07:30:50 -08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Unified Keys
|
|
|
|
//
|
|
|
|
|
|
|
|
bool CBasicKeyStore::AddUnifiedFullViewingKey(
|
|
|
|
const libzcash::ZcashdUnifiedFullViewingKey &ufvk)
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
|
2021-12-14 09:09:35 -08:00
|
|
|
// Add the Sapling component of the UFVK to the wallet.
|
2021-11-30 07:30:50 -08:00
|
|
|
auto saplingKey = ufvk.GetSaplingKey();
|
|
|
|
if (saplingKey.has_value()) {
|
|
|
|
auto ivk = saplingKey.value().fvk.in_viewing_key();
|
2021-12-22 12:26:32 -08:00
|
|
|
mapSaplingKeyUnified.insert(std::make_pair(ivk, ufvk.GetKeyID()));
|
2021-11-30 07:30:50 -08:00
|
|
|
}
|
|
|
|
|
2021-12-14 09:09:35 -08:00
|
|
|
// We can't reasonably add the transparent component here, because
|
|
|
|
// of the way that transparent addresses are generated from the
|
2021-12-29 08:12:44 -08:00
|
|
|
// P2PKH part of the unified address. Instead, whenever a new
|
2021-12-14 09:09:35 -08:00
|
|
|
// unified address is generated, the keys associated with the
|
|
|
|
// transparent part of the address must be added to the keystore.
|
|
|
|
|
|
|
|
// Add the UFVK by key identifier.
|
2021-12-22 12:26:32 -08:00
|
|
|
mapUnifiedFullViewingKeys.insert({ufvk.GetKeyID(), ufvk});
|
2021-11-30 07:30:50 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-08 17:13:28 -08:00
|
|
|
|
2022-01-03 14:46:28 -08:00
|
|
|
bool CBasicKeyStore::AddTransparentReceiverForUnifiedAddress(
|
2021-12-08 17:13:28 -08:00
|
|
|
const libzcash::UFVKId& keyId,
|
2021-12-22 17:20:53 -08:00
|
|
|
const libzcash::diversifier_index_t& diversifierIndex,
|
|
|
|
const libzcash::UnifiedAddress& ua)
|
2021-12-08 17:13:28 -08:00
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
|
|
|
|
// It's only necessary to add p2pkh and p2sh components of
|
|
|
|
// the UA; all other lookups of the associated UFVK will be
|
|
|
|
// made via the protocol-specific viewing key that is used
|
|
|
|
// to trial-decrypt a transaction.
|
2021-12-22 17:20:53 -08:00
|
|
|
auto addrEntry = std::make_pair(keyId, diversifierIndex);
|
2021-12-08 17:13:28 -08:00
|
|
|
|
2021-12-22 17:20:53 -08:00
|
|
|
auto p2pkhReceiver = ua.GetP2PKHReceiver();
|
2021-12-08 17:13:28 -08:00
|
|
|
if (p2pkhReceiver.has_value()) {
|
2021-12-13 14:11:29 -08:00
|
|
|
mapP2PKHUnified.insert(std::make_pair(p2pkhReceiver.value(), addrEntry));
|
2021-12-08 17:13:28 -08:00
|
|
|
}
|
|
|
|
|
2021-12-22 17:20:53 -08:00
|
|
|
auto p2shReceiver = ua.GetP2SHReceiver();
|
2021-12-08 17:13:28 -08:00
|
|
|
if (p2shReceiver.has_value()) {
|
2021-12-13 14:11:29 -08:00
|
|
|
mapP2SHUnified.insert(std::make_pair(p2shReceiver.value(), addrEntry));
|
2021-12-08 17:13:28 -08:00
|
|
|
}
|
2021-12-22 17:20:53 -08:00
|
|
|
|
|
|
|
return true;
|
2021-12-08 17:13:28 -08:00
|
|
|
}
|
|
|
|
|
2021-12-09 17:34:04 -08:00
|
|
|
std::optional<libzcash::ZcashdUnifiedFullViewingKey> CBasicKeyStore::GetUnifiedFullViewingKey(
|
2021-12-13 14:11:29 -08:00
|
|
|
const libzcash::UFVKId& keyId) const
|
2021-12-09 17:34:04 -08:00
|
|
|
{
|
|
|
|
auto mi = mapUnifiedFullViewingKeys.find(keyId);
|
|
|
|
if (mi != mapUnifiedFullViewingKeys.end()) {
|
|
|
|
return mi->second;
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 14:11:29 -08:00
|
|
|
|
|
|
|
std::optional<std::pair<libzcash::UFVKId, std::optional<libzcash::diversifier_index_t>>>
|
|
|
|
CBasicKeyStore::GetUFVKMetadataForReceiver(const libzcash::Receiver& receiver) const
|
|
|
|
{
|
|
|
|
return std::visit(FindUFVKId(*this), receiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::pair<libzcash::UFVKId, std::optional<libzcash::diversifier_index_t>>>
|
|
|
|
FindUFVKId::operator()(const libzcash::SaplingPaymentAddress& saplingAddr) const {
|
2022-01-03 14:46:28 -08:00
|
|
|
const auto saplingIvk = keystore.mapSaplingIncomingViewingKeys.find(saplingAddr);
|
|
|
|
if (saplingIvk != keystore.mapSaplingIncomingViewingKeys.end()) {
|
|
|
|
const auto ufvkId = keystore.mapSaplingKeyUnified.find(saplingIvk->second);
|
|
|
|
if (ufvkId != keystore.mapSaplingKeyUnified.end()) {
|
|
|
|
return std::make_pair(ufvkId->second, std::nullopt);
|
2021-12-13 14:11:29 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::optional<std::pair<libzcash::UFVKId, std::optional<libzcash::diversifier_index_t>>>
|
|
|
|
FindUFVKId::operator()(const CScriptID& scriptId) const {
|
2022-01-03 14:46:28 -08:00
|
|
|
const auto metadata = keystore.mapP2SHUnified.find(scriptId);
|
|
|
|
if (metadata != keystore.mapP2SHUnified.end()) {
|
|
|
|
return metadata->second;
|
2021-12-13 14:11:29 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::optional<std::pair<libzcash::UFVKId, std::optional<libzcash::diversifier_index_t>>>
|
|
|
|
FindUFVKId::operator()(const CKeyID& keyId) const {
|
2022-01-03 14:46:28 -08:00
|
|
|
const auto metadata = keystore.mapP2PKHUnified.find(keyId);
|
|
|
|
if (metadata != keystore.mapP2PKHUnified.end()) {
|
|
|
|
return metadata->second;
|
2021-12-13 14:11:29 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::optional<std::pair<libzcash::UFVKId, std::optional<libzcash::diversifier_index_t>>>
|
|
|
|
FindUFVKId::operator()(const libzcash::UnknownReceiver& receiver) const {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|