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
|
2023-01-23 10:31:54 -08:00
|
|
|
// Copyright (c) 2016-2023 The Zcash 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"
|
2018-10-22 15:51:11 -07:00
|
|
|
#include "util/system.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());
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:14:08 -08:00
|
|
|
std::optional<AddressUFVKMetadata> CKeyStore::GetUFVKMetadataForAddress(
|
|
|
|
const CTxDestination& address) const
|
|
|
|
{
|
|
|
|
auto self = this;
|
2023-04-04 13:26:39 -07:00
|
|
|
return examine(address, match {
|
2023-03-08 17:14:08 -08:00
|
|
|
[](const CNoDestination&) -> std::optional<AddressUFVKMetadata> { return std::nullopt; },
|
|
|
|
[&](const auto& addr) { return self->GetUFVKMetadataForReceiver(addr); }
|
2023-04-04 13:26:39 -07:00
|
|
|
});
|
2023-03-08 17:14:08 -08:00
|
|
|
}
|
|
|
|
|
2015-06-10 01:04:08 -07:00
|
|
|
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);
|
2022-01-30 16:14:03 -08:00
|
|
|
auto ivk = extfvk.ToIncomingViewingKey();
|
2020-02-18 11:12:15 -08:00
|
|
|
mapSaplingFullViewingKeys[ivk] = extfvk;
|
2018-08-02 14:34:31 -07:00
|
|
|
|
2022-01-30 10:21:56 -08:00
|
|
|
return true;
|
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.
|
2022-01-30 16:19:49 -08:00
|
|
|
bool CBasicKeyStore::AddSaplingPaymentAddress(
|
2018-10-01 08:34:25 -07:00
|
|
|
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
|
|
|
|
2021-10-26 14:10:09 -07:00
|
|
|
//
|
|
|
|
// Sapling Keys
|
|
|
|
//
|
|
|
|
|
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-12-17 11:59:30 -08:00
|
|
|
bool CBasicKeyStore::GetSaplingExtendedSpendingKey(
|
|
|
|
const libzcash::SaplingPaymentAddress &addr,
|
|
|
|
libzcash::SaplingExtendedSpendingKey &extskOut) const {
|
2018-09-12 04:01:08 -07:00
|
|
|
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
|
|
|
|
2021-12-17 11:59:30 -08:00
|
|
|
bool CBasicKeyStore::HaveSaplingSpendingKeyForAddress(
|
|
|
|
const libzcash::SaplingPaymentAddress &addr) const {
|
|
|
|
libzcash::SaplingIncomingViewingKey ivk;
|
|
|
|
libzcash::SaplingExtendedFullViewingKey extfvk;
|
|
|
|
|
|
|
|
return GetSaplingIncomingViewingKey(addr, ivk) &&
|
|
|
|
GetSaplingFullViewingKey(ivk, extfvk) &&
|
|
|
|
HaveSaplingSpendingKey(extfvk);
|
|
|
|
}
|
2022-01-12 15:54:12 -08:00
|
|
|
|
2021-11-30 07:30:50 -08:00
|
|
|
//
|
|
|
|
// Unified Keys
|
|
|
|
//
|
|
|
|
|
|
|
|
bool CBasicKeyStore::AddUnifiedFullViewingKey(
|
|
|
|
const libzcash::ZcashdUnifiedFullViewingKey &ufvk)
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
|
2022-02-18 16:41:38 -08:00
|
|
|
auto ufvkId = ufvk.GetKeyID();
|
|
|
|
|
|
|
|
// Add the Orchard component of the UFVK to the wallet.
|
|
|
|
auto orchardKey = ufvk.GetOrchardKey();
|
|
|
|
if (orchardKey.has_value()) {
|
|
|
|
auto ivk = orchardKey.value().ToIncomingViewingKey();
|
|
|
|
mapOrchardKeyUnified.insert(std::make_pair(ivk, ufvkId));
|
|
|
|
|
|
|
|
auto ivkInternal = orchardKey.value().ToInternalIncomingViewingKey();
|
|
|
|
mapOrchardKeyUnified.insert(std::make_pair(ivkInternal, ufvkId));
|
|
|
|
}
|
|
|
|
|
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()) {
|
2022-01-30 16:14:03 -08:00
|
|
|
auto ivk = saplingKey.value().ToIncomingViewingKey();
|
2022-02-18 16:41:38 -08:00
|
|
|
mapSaplingKeyUnified.insert(std::make_pair(ivk, ufvkId));
|
2022-01-30 16:14:03 -08:00
|
|
|
|
|
|
|
auto changeIvk = saplingKey.value().GetChangeIVK();
|
2022-02-18 16:41:38 -08:00
|
|
|
mapSaplingKeyUnified.insert(std::make_pair(changeIvk, ufvkId));
|
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.
|
2022-02-18 16:41:38 -08:00
|
|
|
mapUnifiedFullViewingKeys.insert({ufvkId, 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
|
|
|
|
2022-03-18 10:09:35 -07:00
|
|
|
std::optional<AddressUFVKMetadata>
|
2021-12-13 14:11:29 -08:00
|
|
|
CBasicKeyStore::GetUFVKMetadataForReceiver(const libzcash::Receiver& receiver) const
|
|
|
|
{
|
|
|
|
return std::visit(FindUFVKId(*this), receiver);
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:14:08 -08:00
|
|
|
std::optional<libzcash::UFVKId>
|
|
|
|
CBasicKeyStore::GetUFVKIdForAddress(const libzcash::UnifiedAddress& addr) const
|
2022-03-16 16:04:09 -07:00
|
|
|
{
|
|
|
|
std::optional<libzcash::UFVKId> ufvkId;
|
|
|
|
for (const auto& receiver : addr) {
|
2022-03-20 11:13:24 -07:00
|
|
|
auto rmeta = GetUFVKMetadataForReceiver(receiver);
|
|
|
|
if (rmeta.has_value()) {
|
2022-03-18 10:09:35 -07:00
|
|
|
// We should never generate unified addresses with internal receivers
|
2022-03-20 11:13:24 -07:00
|
|
|
assert(rmeta.value().IsExternalAddress());
|
2022-03-18 10:09:35 -07:00
|
|
|
|
2022-03-20 11:13:24 -07:00
|
|
|
if (ufvkId.has_value()) {
|
2022-03-16 16:30:55 -07:00
|
|
|
// If the unified address contains receivers that are associated with
|
|
|
|
// different UFVKs, we cannot return a singular value.
|
2022-03-18 10:09:35 -07:00
|
|
|
if (rmeta.value().GetUFVKId() != ufvkId.value()) {
|
2022-03-16 16:30:55 -07:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-03-20 11:13:24 -07:00
|
|
|
} else {
|
2022-03-18 10:09:35 -07:00
|
|
|
ufvkId = rmeta.value().GetUFVKId();
|
2022-03-16 16:04:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:14:08 -08:00
|
|
|
return ufvkId;
|
2022-03-16 16:04:09 -07:00
|
|
|
}
|
|
|
|
|
2022-02-09 12:49:36 -08:00
|
|
|
std::optional<libzcash::UFVKId> CBasicKeyStore::GetUFVKIdForViewingKey(const libzcash::ViewingKey& vk) const
|
|
|
|
{
|
|
|
|
std::optional<libzcash::UFVKId> result;
|
2023-04-04 13:26:39 -07:00
|
|
|
examine(vk, match {
|
2022-02-09 12:49:36 -08:00
|
|
|
[&](const libzcash::SproutViewingKey& vk) {},
|
|
|
|
[&](const libzcash::SaplingExtendedFullViewingKey& extfvk) {
|
|
|
|
const auto saplingIvk = extfvk.ToIncomingViewingKey();
|
|
|
|
const auto ufvkId = mapSaplingKeyUnified.find(saplingIvk);
|
|
|
|
if (ufvkId != mapSaplingKeyUnified.end()) {
|
|
|
|
result = ufvkId->second;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[&](const libzcash::UnifiedFullViewingKey& ufvk) {
|
2022-02-18 16:41:38 -08:00
|
|
|
const auto orchardFvk = ufvk.GetOrchardKey();
|
|
|
|
if (orchardFvk.has_value()) {
|
|
|
|
const auto orchardIvk = orchardFvk.value().ToIncomingViewingKey();
|
|
|
|
const auto ufvkId = mapOrchardKeyUnified.find(orchardIvk);
|
|
|
|
if (ufvkId != mapOrchardKeyUnified.end()) {
|
|
|
|
result = ufvkId->second;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 12:49:36 -08:00
|
|
|
const auto saplingDfvk = ufvk.GetSaplingKey();
|
|
|
|
if (saplingDfvk.has_value()) {
|
|
|
|
const auto saplingIvk = saplingDfvk.value().ToIncomingViewingKey();
|
|
|
|
const auto ufvkId = mapSaplingKeyUnified.find(saplingIvk);
|
|
|
|
if (ufvkId != mapSaplingKeyUnified.end()) {
|
|
|
|
result = ufvkId->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 13:26:39 -07:00
|
|
|
});
|
2022-02-09 12:49:36 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:09:35 -07:00
|
|
|
//
|
|
|
|
// FindUFVKId :: (KeyStore, Receiver) -> std::optional<AddressUFVKMetadata>
|
|
|
|
//
|
|
|
|
|
|
|
|
std::optional<AddressUFVKMetadata> FindUFVKId::operator()(const libzcash::OrchardRawAddress& orchardAddr) const {
|
2022-02-18 16:41:38 -08:00
|
|
|
for (const auto& [k, v] : keystore.mapUnifiedFullViewingKeys) {
|
|
|
|
auto fvk = v.GetOrchardKey();
|
|
|
|
if (fvk.has_value()) {
|
2022-03-20 11:13:24 -07:00
|
|
|
auto d_idx = fvk.value().DecryptDiversifier(orchardAddr);
|
2022-02-18 16:41:38 -08:00
|
|
|
if (d_idx.has_value()) {
|
2022-03-20 11:13:24 -07:00
|
|
|
return AddressUFVKMetadata(k, d_idx->first, d_idx->second);
|
2022-02-18 16:41:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 15:18:40 -08:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-03-18 10:09:35 -07:00
|
|
|
std::optional<AddressUFVKMetadata> 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()) {
|
2022-02-22 18:23:45 -08:00
|
|
|
// We have either generated this as a receiver via `z_getaddressforaccount` or a
|
|
|
|
// legacy Sapling address via `z_getnewaddress`, or we have previously detected
|
|
|
|
// this via trial-decryption of a note.
|
2022-01-03 14:46:28 -08:00
|
|
|
const auto ufvkId = keystore.mapSaplingKeyUnified.find(saplingIvk->second);
|
|
|
|
if (ufvkId != keystore.mapSaplingKeyUnified.end()) {
|
2022-03-20 11:13:24 -07:00
|
|
|
// We know that we have a UFVK, and that it has a Sapling key that
|
|
|
|
// produced this address, so decrypt the diversifier to determine
|
|
|
|
// whether it was an internal or external address
|
|
|
|
auto ufvk = keystore.GetUnifiedFullViewingKey(ufvkId->second).value();
|
|
|
|
auto saplingKey = ufvk.GetSaplingKey().value();
|
|
|
|
auto d_idx = saplingKey.DecryptDiversifier(saplingAddr).value();
|
|
|
|
return AddressUFVKMetadata(ufvkId->second, d_idx.first, d_idx.second);
|
2021-12-13 14:11:29 -08:00
|
|
|
}
|
2022-03-20 11:13:24 -07:00
|
|
|
|
|
|
|
// If we have the addr -> ivk map entry but not the ivk -> UFVK map entry,
|
|
|
|
// then this is definitely a legacy Sapling address.
|
|
|
|
return std::nullopt;
|
2021-12-13 14:11:29 -08:00
|
|
|
}
|
2022-02-22 18:23:45 -08:00
|
|
|
|
|
|
|
// We haven't generated this receiver via `z_getaddressforaccount` (or this is a
|
|
|
|
// recovery from a backed-up mnemonic which doesn't store receiver types selected by
|
|
|
|
// users). Trial-decrypt the diversifier of the Sapling address with every UFVK in the
|
|
|
|
// wallet, to check directly if it belongs to any of them.
|
|
|
|
for (const auto& [k, v] : keystore.mapUnifiedFullViewingKeys) {
|
|
|
|
auto dfvk = v.GetSaplingKey();
|
|
|
|
if (dfvk.has_value()) {
|
2022-03-20 11:13:24 -07:00
|
|
|
auto d_idx = dfvk.value().DecryptDiversifier(saplingAddr);
|
|
|
|
if (d_idx.has_value()) {
|
|
|
|
return AddressUFVKMetadata(k, d_idx->first, d_idx->second);
|
2022-02-22 18:23:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We definitely don't know of any UFVK linked to this Sapling address.
|
|
|
|
return std::nullopt;
|
2021-12-13 14:11:29 -08:00
|
|
|
}
|
2022-03-18 10:09:35 -07:00
|
|
|
std::optional<AddressUFVKMetadata> 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()) {
|
2022-03-18 10:09:35 -07:00
|
|
|
// At present we never generate transparent internal addresses, so this
|
|
|
|
// must be an external address
|
2022-03-20 11:13:24 -07:00
|
|
|
return AddressUFVKMetadata(metadata->second.first, metadata->second.second, true);
|
2021-12-13 14:11:29 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 10:09:35 -07:00
|
|
|
std::optional<AddressUFVKMetadata> 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()) {
|
2022-03-18 10:09:35 -07:00
|
|
|
// At present we never generate transparent internal addresses, so this
|
|
|
|
// must be an external address
|
2022-03-20 11:13:24 -07:00
|
|
|
return AddressUFVKMetadata(metadata->second.first, metadata->second.second, true);
|
2021-12-13 14:11:29 -08:00
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 10:09:35 -07:00
|
|
|
std::optional<AddressUFVKMetadata> FindUFVKId::operator()(const libzcash::UnknownReceiver& receiver) const {
|
2021-12-13 14:11:29 -08:00
|
|
|
return std::nullopt;
|
|
|
|
}
|