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
|
2012-05-18 07:02:28 -07:00
|
|
|
// file COPYING or http://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"
|
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
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2012-05-14 14:44:52 -07:00
|
|
|
bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
|
2011-07-05 07:42:32 -07:00
|
|
|
{
|
|
|
|
CKey key;
|
2011-07-05 11:53:43 -07:00
|
|
|
if (!GetKey(address, key))
|
2011-07-05 07:42:32 -07:00
|
|
|
return false;
|
|
|
|
vchPubKeyOut = key.GetPubKey();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:52:05 -07:00
|
|
|
bool CKeyStore::AddKey(const CKey &key) {
|
|
|
|
return AddKeyPubKey(key, key.GetPubKey());
|
|
|
|
}
|
|
|
|
|
2018-08-03 08:51:01 -07:00
|
|
|
bool CBasicKeyStore::SetHDSeed(const HDSeed& seed)
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
2018-09-03 02:45:17 -07:00
|
|
|
if (!hdSeed.IsNull()) {
|
|
|
|
// 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;
|
|
|
|
}
|
2018-08-03 08:51:01 -07:00
|
|
|
hdSeed = seed;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::HaveHDSeed() const
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
return !hdSeed.IsNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::GetHDSeed(HDSeed& seedOut) const
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
if (hdSeed.IsNull()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
seedOut = hdSeed;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-26 12:05:11 -07:00
|
|
|
bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
setWatchOnly.erase(dest);
|
|
|
|
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
|
|
|
{
|
2016-08-16 03:17:33 -07:00
|
|
|
LOCK(cs_SpendingKeyStore);
|
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
|
|
|
|
2018-07-05 14:37:33 -07:00
|
|
|
//! Sapling
|
2018-08-02 14:34:31 -07:00
|
|
|
bool CBasicKeyStore::AddSaplingSpendingKey(
|
2018-08-28 16:07:07 -07:00
|
|
|
const libzcash::SaplingExtendedSpendingKey &sk,
|
2018-09-27 14:44:04 -07:00
|
|
|
const libzcash::SaplingPaymentAddress &defaultAddr)
|
2018-07-05 14:37:33 -07:00
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
2018-08-28 16:07:07 -07:00
|
|
|
auto fvk = sk.expsk.full_viewing_key();
|
2018-08-02 14:34:31 -07:00
|
|
|
|
2018-07-05 14:52:46 -07:00
|
|
|
// if SaplingFullViewingKey is not in SaplingFullViewingKeyMap, add it
|
2018-09-27 14:44:04 -07:00
|
|
|
if (!AddSaplingFullViewingKey(fvk, defaultAddr)) {
|
2018-06-30 12:57:04 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-02 14:34:31 -07:00
|
|
|
|
2018-06-30 12:57:04 -07:00
|
|
|
mapSaplingSpendingKeys[fvk] = sk;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
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(
|
|
|
|
const libzcash::SaplingFullViewingKey &fvk,
|
2018-09-27 14:44:04 -07:00
|
|
|
const libzcash::SaplingPaymentAddress &defaultAddr)
|
2018-07-05 14:52:46 -07:00
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
auto ivk = fvk.in_viewing_key();
|
|
|
|
mapSaplingFullViewingKeys[ivk] = fvk;
|
2018-08-02 14:34:31 -07:00
|
|
|
|
2018-10-05 21:15:08 -07:00
|
|
|
return CBasicKeyStore::AddSaplingIncomingViewingKey(ivk, defaultAddr);
|
2018-10-01 08:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function updates the wallet's internal address->ivk map.
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
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
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
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
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
return mapSaplingFullViewingKeys.count(ivk) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
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
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
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
|
|
|
|
|
|
|
bool CBasicKeyStore::GetSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk,
|
|
|
|
libzcash::SaplingFullViewingKey &fvkOut) const
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
SaplingFullViewingKeyMap::const_iterator mi = mapSaplingFullViewingKeys.find(ivk);
|
|
|
|
if (mi != mapSaplingFullViewingKeys.end()) {
|
|
|
|
fvkOut = mi->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::GetSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr,
|
|
|
|
libzcash::SaplingIncomingViewingKey &ivkOut) const
|
|
|
|
{
|
|
|
|
LOCK(cs_SpendingKeyStore);
|
|
|
|
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
|
|
|
|
|
|
|
bool CBasicKeyStore::GetSaplingExtendedSpendingKey(const libzcash::SaplingPaymentAddress &addr,
|
|
|
|
libzcash::SaplingExtendedSpendingKey &extskOut) const {
|
|
|
|
libzcash::SaplingIncomingViewingKey ivk;
|
|
|
|
libzcash::SaplingFullViewingKey fvk;
|
|
|
|
|
|
|
|
return GetSaplingIncomingViewingKey(addr, ivk) &&
|
|
|
|
GetSaplingFullViewingKey(ivk, fvk) &&
|
|
|
|
GetSaplingSpendingKey(fvk, extskOut);
|
|
|
|
}
|