Files

126 lines
3.5 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (c) 2009-2010 Satoshi Nakamoto
* Copyright (c) 2009-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2011-06-01 18:27:05 +02:00
2012-04-15 22:10:54 +02:00
#include "keystore.h"
2026-05-07 21:10:10 +02:00
#include <primitives/ScriptDefines.h> // for MAX_SCRIPT_ELEMENT_SIZE
2013-04-13 00:13:08 -05:00
2022-07-06 22:12:33 +02:00
bool CKeyStore::AddKey(const PrivateKey &key) {
2022-05-11 13:46:15 +02:00
return AddKeyPubKey(key, key.getPubKey());
2013-05-01 06:52:05 +02:00
}
2022-07-06 21:56:34 +02:00
bool CBasicKeyStore::GetPubKey(const KeyId &address, PublicKey &vchPubKeyOut) const
{
2022-07-06 22:12:33 +02:00
PrivateKey key;
if (!GetKey(address, key)) {
WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
if (it != mapWatchKeys.end()) {
vchPubKeyOut = it->second;
return true;
}
return false;
}
2022-05-11 13:46:15 +02:00
vchPubKeyOut = key.getPubKey();
return true;
}
2022-07-06 22:12:33 +02:00
bool CBasicKeyStore::AddKeyPubKey(const PrivateKey& key, const PublicKey &pubkey)
2011-06-01 18:27:05 +02:00
{
2013-05-01 06:52:05 +02:00
LOCK(cs_KeyStore);
2021-04-19 11:58:00 +02:00
mapKeys[pubkey.getKeyId()] = key;
2011-06-25 14:57:32 +02:00
return true;
2011-06-01 18:27:05 +02:00
}
2011-06-01 18:28:20 +02:00
bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
2011-10-03 13:05:43 -04:00
{
2020-10-27 14:28:07 +01:00
if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) {
logCritical(Log::Internals) << "CBasicKeyStore::AddCScript(): redeemScripts >" << MAX_SCRIPT_ELEMENT_SIZE << "bytes are invalid";
return false;
}
2013-05-01 06:52:05 +02:00
LOCK(cs_KeyStore);
mapScripts[CScriptID(redeemScript)] = redeemScript;
2011-10-03 13:05:43 -04:00
return true;
}
bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
2011-10-03 13:05:43 -04:00
{
2013-05-01 06:52:05 +02:00
LOCK(cs_KeyStore);
return mapScripts.count(hash) > 0;
2011-10-03 13:05:43 -04:00
}
bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
2011-10-03 13:05:43 -04:00
{
2013-05-01 06:52:05 +02:00
LOCK(cs_KeyStore);
ScriptMap::const_iterator mi = mapScripts.find(hash);
if (mi != mapScripts.end())
2011-10-03 13:05:43 -04:00
{
2013-05-01 06:52:05 +02:00
redeemScriptOut = (*mi).second;
return true;
2011-10-03 13:05:43 -04:00
}
return false;
}
2022-07-06 21:56:34 +02:00
static bool ExtractPubKey(const CScript &dest, PublicKey& pubKeyOut)
{
//TODO: Use Solver to extract this?
CScript::const_iterator pc = dest.begin();
opcodetype opcode;
std::vector<unsigned char> vch;
2023-11-24 18:01:36 +01:00
if (!dest.GetOp(pc, opcode, vch) ||!PublicKeyUtils::isValidSize(vch))
return false;
2022-07-06 21:56:34 +02:00
pubKeyOut = PublicKey(vch);
2021-04-19 11:58:00 +02:00
if (!pubKeyOut.isFullyValid())
return false;
if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
return false;
return true;
}
bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
2013-07-26 01:06:01 +02:00
{
LOCK(cs_KeyStore);
setWatchOnly.insert(dest);
2022-07-06 21:56:34 +02:00
PublicKey pubKey;
if (ExtractPubKey(dest, pubKey))
2021-04-19 11:58:00 +02:00
mapWatchKeys[pubKey.getKeyId()] = pubKey;
2013-07-26 01:06:01 +02:00
return true;
}
2014-07-26 21:05:11 +02:00
bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
{
LOCK(cs_KeyStore);
setWatchOnly.erase(dest);
2022-07-06 21:56:34 +02:00
PublicKey pubKey;
if (ExtractPubKey(dest, pubKey))
2021-04-19 11:58:00 +02:00
mapWatchKeys.erase(pubKey.getKeyId());
2014-07-26 21:05:11 +02:00
return true;
}
bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
2013-07-26 01:06:01 +02:00
{
LOCK(cs_KeyStore);
return setWatchOnly.count(dest) > 0;
}
bool CBasicKeyStore::HaveWatchOnly() const
{
LOCK(cs_KeyStore);
return (!setWatchOnly.empty());
}