Files

154 lines
4.3 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/>.
*/
2014-08-23 03:35:51 +02:00
#include "script/standard.h"
2018-02-12 14:15:24 +01:00
#include <SettingsDefaults.h>
2021-01-05 22:05:25 +01:00
#include <hash.h>
2023-11-24 18:01:36 +01:00
#include <primitives/PublicKey.h>
#include "primitives/script.h"
2014-08-23 03:35:51 +02:00
#include "util.h"
#include "utilstrencodings.h"
2014-08-23 03:35:51 +02:00
#include <boost/foreach.hpp>
typedef std::vector<unsigned char> valtype;
2014-08-23 03:35:51 +02:00
2018-02-12 14:15:24 +01:00
bool fAcceptDatacarrier = Settings::DefaultAcceptDataCarrier;
2021-03-13 13:47:44 +01:00
int nMaxDatacarrierBytes = Settings::MaxOpReturnRelay;
2014-08-23 03:35:51 +02:00
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
{
std::vector<valtype> vSolutions;
2019-06-06 21:37:49 +02:00
Script::TxnOutType whichType;
if (!Script::solver(scriptPubKey, whichType, vSolutions))
2014-08-23 03:35:51 +02:00
return false;
2019-06-06 21:37:49 +02:00
if (whichType == Script::TX_PUBKEY) {
2022-07-06 21:56:34 +02:00
PublicKey pubKey(vSolutions[0]);
2021-04-19 11:58:00 +02:00
if (!pubKey.isValid())
2014-09-20 16:13:18 -07:00
return false;
2021-04-19 11:58:00 +02:00
addressRet = pubKey.getKeyId();
2014-08-23 03:35:51 +02:00
return true;
}
2019-06-06 21:37:49 +02:00
else if (whichType == Script::TX_PUBKEYHASH) {
2022-07-06 21:52:47 +02:00
addressRet = KeyId(uint160(vSolutions[0]));
2014-08-23 03:35:51 +02:00
return true;
}
2019-06-06 21:37:49 +02:00
else if (whichType == Script::TX_SCRIPTHASH) {
2014-08-23 03:35:51 +02:00
addressRet = CScriptID(uint160(vSolutions[0]));
return true;
}
// Multisig txns have more than one address...
return false;
}
2019-06-06 21:37:49 +02:00
bool ExtractDestinations(const CScript& scriptPubKey, Script::TxnOutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
2014-08-23 03:35:51 +02:00
{
addressRet.clear();
2019-06-06 21:37:49 +02:00
typeRet = Script::TX_NONSTANDARD;
std::vector<valtype> vSolutions;
2019-06-06 21:37:49 +02:00
if (!Script::solver(scriptPubKey, typeRet, vSolutions))
2014-08-23 03:35:51 +02:00
return false;
2019-06-06 21:37:49 +02:00
if (typeRet == Script::TX_NULL_DATA) {
2014-08-23 03:35:51 +02:00
// This is data, not addresses
return false;
}
2019-06-06 21:37:49 +02:00
if (typeRet == Script::TX_MULTISIG)
2014-08-23 03:35:51 +02:00
{
nRequiredRet = vSolutions.front()[0];
for (unsigned int i = 1; i < vSolutions.size()-1; i++)
{
2022-07-06 21:56:34 +02:00
PublicKey pubKey(vSolutions[i]);
2021-04-19 11:58:00 +02:00
if (!pubKey.isValid())
2014-09-20 16:13:18 -07:00
continue;
2021-04-19 11:58:00 +02:00
CTxDestination address = pubKey.getKeyId();
2014-08-23 03:35:51 +02:00
addressRet.push_back(address);
}
2014-09-20 16:13:18 -07:00
if (addressRet.empty())
return false;
2014-08-23 03:35:51 +02:00
}
else
{
nRequiredRet = 1;
CTxDestination address;
if (!ExtractDestination(scriptPubKey, address))
return false;
addressRet.push_back(address);
}
return true;
}
namespace
{
class CScriptVisitor : public boost::static_visitor<bool>
{
private:
CScript *script;
public:
CScriptVisitor(CScript *scriptin) { script = scriptin; }
bool operator()(const CNoDestination &dest) const {
script->clear();
return false;
}
2022-07-06 21:52:47 +02:00
bool operator()(const KeyId &keyID) const {
script->clear();
*script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
return true;
}
bool operator()(const CScriptID &scriptID) const {
script->clear();
*script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
return true;
}
};
}
CScript GetScriptForDestination(const CTxDestination& dest)
{
CScript script;
boost::apply_visitor(CScriptVisitor(&script), dest);
return script;
}
2022-07-06 21:56:34 +02:00
CScript GetScriptForRawPubKey(const PublicKey& pubKey)
{
return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
}
2022-07-06 21:56:34 +02:00
CScript GetScriptForMultisig(int nRequired, const std::vector<PublicKey>& keys)
{
CScript script;
script << CScript::EncodeOP_N(nRequired);
2022-07-06 21:56:34 +02:00
BOOST_FOREACH(const PublicKey& key, keys)
script << ToByteVector(key);
script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
return script;
}