24 std::map<std::string, opcodetype> mapOpNames;
29 for (
unsigned int op = 0; op <=
MAX_OPCODE; ++op) {
36 if (strName ==
"OP_UNKNOWN") {
39 mapOpNames[strName] =
static_cast<opcodetype>(op);
41 if (strName.compare(0, 3,
"OP_") == 0) {
42 mapOpNames[strName.substr(3)] =
static_cast<opcodetype>(op);
48 auto it = mapOpNames.find(s);
49 if (it == mapOpNames.end())
throw std::runtime_error(
"script parse error: unknown opcode");
56 static const OpCodeParser ocp;
66 std::vector<std::string> words =
SplitString(s,
" \t\n");
68 for (
const std::string& w : words) {
71 }
else if (std::all_of(w.begin(), w.end(), ::
IsDigit) ||
72 (w.front() ==
'-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::
IsDigit)))
75 const auto num{ToIntegral<int64_t>(w)};
79 if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
80 throw std::runtime_error(
"script parse error: decimal numeric value only allowed in the "
81 "range -0xFFFFFFFF...0xFFFFFFFF");
84 result << num.value();
85 }
else if (w.substr(0, 2) ==
"0x" && w.size() > 2 &&
IsHex(std::string(w.begin() + 2, w.end()))) {
87 std::vector<unsigned char> raw =
ParseHex(std::string(w.begin() + 2, w.end()));
88 result.
insert(result.
end(), raw.begin(), raw.end());
89 }
else if (w.size() >= 2 && w.front() ==
'\'' && w.back() ==
'\'') {
92 std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
96 result << ParseOpCode(w);
108 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
115 for (
unsigned int i = 0; i < tx.
vout.size(); i++) {
140 bool ok_extended =
false, ok_legacy =
false;
147 ssData >> tx_extended;
148 if (ssData.
empty()) ok_extended =
true;
149 }
catch (
const std::exception&) {
157 tx = std::move(tx_extended);
162 if (try_no_witness) {
166 if (ssData.
empty()) ok_legacy =
true;
167 }
catch (
const std::exception&) {
175 tx = std::move(tx_legacy);
181 tx = std::move(tx_extended);
187 tx = std::move(tx_legacy);
197 if (!
IsHex(hex_tx)) {
201 std::vector<unsigned char> txData(
ParseHex(hex_tx));
202 return DecodeTx(tx, txData, try_no_witness, try_witness);
207 if (!
IsHex(hex_header))
return false;
209 const std::vector<unsigned char> header_data{
ParseHex(hex_header)};
212 ser_header >> header;
213 }
catch (
const std::exception&) {
221 if (!
IsHex(strHexBlk))
224 std::vector<unsigned char> blockData(
ParseHex(strHexBlk));
229 catch (
const std::exception&) {
238 if ((strHex.size() != 64) || !
IsHex(strHex))
251 throw std::runtime_error(strName +
" must be hexadecimal string (not '" + strHex +
"')");
259 static std::map<std::string, int> map_sighash_values = {
268 const std::string& strHashType = sighash.
get_str();
269 const auto& it = map_sighash_values.find(strHashType);
270 if (it != map_sighash_values.end()) {
271 hash_type = it->second;
273 throw std::runtime_error(strHashType +
" is not a valid sighash parameter.");
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.
const std::string & get_str() const
const std::string & getValStr() const
void SetHex(const char *psz)
iterator insert(iterator pos, const T &value)
static UniValue Parse(std::string_view raw)
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)
int ParseSighashString(const UniValue &sighash)
bool DecodeHexBlockHeader(CBlockHeader &header, const std::string &hex_header)
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
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.
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
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.
std::vector< std::string > SplitString(std::string_view str, char sep)
A mutable version of CTransaction.
std::vector< CTxOut > vout
bool IsHex(std::string_view str)
static const int PROTOCOL_VERSION
network protocol versioning