35 std::map<std::string, opcodetype> mapOpNames;
40 for (
unsigned int op = 0; op <=
MAX_OPCODE; ++op) {
47 if (strName ==
"OP_UNKNOWN") {
50 mapOpNames[strName] =
static_cast<opcodetype>(op);
52 if (strName.starts_with(
"OP_")) {
53 mapOpNames[strName.substr(3)] =
static_cast<opcodetype>(op);
59 auto it = mapOpNames.find(
s);
60 if (it == mapOpNames.end())
throw std::runtime_error(
"script parse error: unknown opcode");
67 static const OpCodeParser ocp;
77 std::vector<std::string> words =
SplitString(
s,
" \t\n");
79 for (
const std::string& w : words) {
82 }
else if (std::all_of(w.begin(), w.end(),
::IsDigit) ||
83 (w.front() ==
'-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(),
::IsDigit)))
86 const auto num{ToIntegral<int64_t>(w)};
90 if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
91 throw std::runtime_error(
"script parse error: decimal numeric value only allowed in the "
92 "range -0xFFFFFFFF...0xFFFFFFFF");
95 result << num.value();
96 }
else if (w.starts_with(
"0x") && w.size() > 2 &&
IsHex(std::string(w.begin() + 2, w.end()))) {
98 std::vector<unsigned char> raw =
ParseHex(std::string(w.begin() + 2, w.end()));
99 result.
insert(result.
end(), raw.begin(), raw.end());
100 }
else if (w.size() >= 2 && w.front() ==
'\'' && w.back() ==
'\'') {
103 std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
107 result << ParseOpCode(w);
119 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
126 for (
unsigned int i = 0; i < tx.
vout.size(); i++) {
151 bool ok_extended =
false, ok_legacy =
false;
159 if (ssData.
empty()) ok_extended =
true;
160 }
catch (
const std::exception&) {
168 tx = std::move(tx_extended);
173 if (try_no_witness) {
177 if (ssData.
empty()) ok_legacy =
true;
178 }
catch (
const std::exception&) {
186 tx = std::move(tx_legacy);
192 tx = std::move(tx_extended);
198 tx = std::move(tx_legacy);
208 if (!
IsHex(hex_tx)) {
212 std::vector<unsigned char> txData(
ParseHex(hex_tx));
213 return DecodeTx(tx, txData, try_no_witness, try_witness);
218 if (!
IsHex(hex_header))
return false;
220 const std::vector<unsigned char> header_data{
ParseHex(hex_header)};
223 ser_header >> header;
224 }
catch (
const std::exception&) {
232 if (!
IsHex(strHexBlk))
235 std::vector<unsigned char> blockData(
ParseHex(strHexBlk));
240 catch (
const std::exception&) {
249 static const std::map<std::string, int> map_sighash_values = {
258 const auto& it = map_sighash_values.find(sighash);
259 if (it != map_sighash_values.end()) {
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
Double ended buffer combining vector and stream-like interfaces.
iterator insert(iterator pos, const T &value)
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
CScript ParseScript(const std::string &s)
static bool CheckTxScriptsSanity(const CMutableTransaction &tx)
bool DecodeHexTx(CMutableTransaction &tx, const std::string &hex_tx, bool try_no_witness, bool try_witness)
bool DecodeHexBlockHeader(CBlockHeader &header, const std::string &hex_header)
util::Result< int > SighashFromStr(const std::string &sighash)
static bool DecodeTx(CMutableTransaction &tx, const std::vector< unsigned char > &tx_data, bool try_no_witness, bool try_witness)
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)
@ SIGHASH_DEFAULT
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.
std::vector< std::string > SplitString(std::string_view str, char sep)
static constexpr TransactionSerParams TX_NO_WITNESS
static constexpr TransactionSerParams TX_WITH_WITNESS
std::string GetOpName(opcodetype opcode)
static const unsigned int MAX_OPCODE
static const int MAX_SCRIPT_SIZE
opcodetype
Script opcodes.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
A mutable version of CTransaction.
std::vector< CTxOut > vout
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
bool IsHex(std::string_view str)