Files

118 lines
5.4 KiB
C++
Raw Permalink Normal View History

2017-07-24 22:40:28 +02:00
/*
2017-11-09 19:34:51 +01:00
* This file is part of the Flowee project
2015-12-13 14:51:43 +01:00
// Copyright (c) 2009-2015 The Bitcoin Core developers
2021-06-20 22:44:44 +02:00
* Copyright (C) 2017 Tom Zander <tom@flowee.org>
2017-07-24 22:40:28 +02:00
*
* 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/>.
*/
#include "core_io.h"
2014-09-14 12:43:56 +02:00
2019-04-11 11:33:27 +02:00
#include <primitives/transaction.h>
#include <primitives/script.h>
2014-08-23 03:35:51 +02:00
#include "script/standard.h"
#include "serialize.h"
#include "streaming/streams.h"
2015-09-04 16:11:34 +02:00
#include <univalue.h>
#include "util.h"
2014-08-21 16:11:09 +02:00
#include "utilmoneystr.h"
#include "utilstrencodings.h"
2022-07-06 22:50:53 +02:00
#include <BitcoinVersion.h>
2015-07-30 19:56:00 -04:00
#include <boost/assign/list_of.hpp>
2014-08-23 05:09:47 +02:00
#include <boost/foreach.hpp>
2017-06-21 22:08:07 -04:00
const std::map<unsigned char, std::string> mapSigHashTypes =
2015-07-30 19:56:00 -04:00
boost::assign::map_list_of
(static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL"))
(static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY"))
(static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE"))
(static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY"))
(static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE"))
(static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY"))
2026-05-09 08:29:15 +02:00
(static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_UTXOS), std::string("ALL|UTXOS"))
(static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_UTXOS), std::string("NONE|UTXOS"))
(static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_UTXOS), std::string("SINGLE|UTXOS"))
2015-07-30 19:56:00 -04:00
;
/**
* Create the assembly string representation of a CScript object.
* @param[in] script CScript object to convert into the asm string representation.
* @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format
* of a signature. Only pass true for scripts you believe could contain signatures. For example,
* pass false, or omit the this argument (defaults to false), for scriptPubKeys.
*/
std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
2015-07-30 19:56:00 -04:00
{
std::string str;
2015-07-30 19:56:00 -04:00
opcodetype opcode;
std::vector<unsigned char> vch;
2015-07-30 19:56:00 -04:00
CScript::const_iterator pc = script.begin();
while (pc < script.end()) {
if (!str.empty()) {
str += " ";
}
if (!script.GetOp(pc, opcode, vch)) {
str += "[error]";
return str;
}
if (0 <= opcode && opcode <= OP_PUSHDATA4) {
if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
2015-07-30 19:56:00 -04:00
str += strprintf("%d", CScriptNum(vch, false).getint());
} else {
// the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
if (fAttemptSighashDecode && !script.IsUnspendable()) {
2017-06-22 10:39:38 +02:00
std::string strSigHashDecode;
2015-07-30 19:56:00 -04:00
// goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
// this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
// the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
// checks in CheckSignatureEncoding.
2017-07-24 22:40:28 +02:00
uint32_t flags = SCRIPT_VERIFY_STRICTENC;
if (vch.back() & SIGHASH_FORKID) {
// If the transaction is using SIGHASH_FORKID, we need
// to set the apropriate flag.
// TODO: Remove after the Hard Fork.
flags |= SCRIPT_ENABLE_SIGHASH_FORKID;
}
2026-05-09 08:29:15 +02:00
if (vch.back() & SIGHASH_UTXOS)
flags |= SCRIPT_ENABLE_SIGHASH_UTXOS;
2020-04-11 18:35:43 +02:00
Script::State state(flags);
if (Script::checkTransactionSignatureEncoding(vch, state)) {
2017-07-24 22:40:28 +02:00
const uint8_t chSigHashType = vch.back() & 0xBF;
2015-07-30 19:56:00 -04:00
if (mapSigHashTypes.count(chSigHashType)) {
2017-07-24 22:40:28 +02:00
const bool forkIdSet = (vch.back() & SIGHASH_FORKID) == SIGHASH_FORKID;
strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second
+ (forkIdSet ? "|FORKID]" : "]");
2015-07-30 19:56:00 -04:00
vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
}
}
str += HexStr(vch) + strSigHashDecode;
} else {
str += HexStr(vch);
}
}
} else {
str += GetOpName(opcode);
}
}
return str;
}
std::string EncodeHexTx(const CTransaction& tx)
{
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << tx;
return HexStr(ssTx.begin(), ssTx.end());
}