17 #include <boost/algorithm/string/classification.hpp>
18 #include <boost/algorithm/string/replace.hpp>
19 #include <boost/algorithm/string/split.hpp>
28 static std::map<std::string, opcodetype> mapOpNames;
30 if (mapOpNames.empty())
32 for (
unsigned int op = 0; op <=
MAX_OPCODE; op++)
38 std::string strName =
GetOpName(static_cast<opcodetype>(op));
39 if (strName ==
"OP_UNKNOWN")
41 mapOpNames[strName] =
static_cast<opcodetype>(op);
43 boost::algorithm::replace_first(strName,
"OP_",
"");
44 mapOpNames[strName] =
static_cast<opcodetype>(op);
48 std::vector<std::string> words;
49 boost::algorithm::split(words, s, boost::algorithm::is_any_of(
" \t\n"), boost::algorithm::token_compress_on);
51 for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
57 else if (std::all_of(w->begin(), w->end(),
::IsDigit) ||
58 (w->front() ==
'-' && w->size() > 1 && std::all_of(w->begin()+1, w->end(),
::IsDigit)))
65 if (n > int64_t{0xffffffff} || n < -1 * int64_t{0xffffffff}) {
66 throw std::runtime_error(
"script parse error: decimal numeric value only allowed in the "
67 "range -0xFFFFFFFF...0xFFFFFFFF");
72 else if (w->substr(0,2) ==
"0x" && w->size() > 2 &&
IsHex(std::string(w->begin()+2, w->end())))
75 std::vector<unsigned char> raw =
ParseHex(std::string(w->begin()+2, w->end()));
76 result.insert(result.end(), raw.begin(), raw.end());
78 else if (w->size() >= 2 && w->front() ==
'\'' && w->back() ==
'\'')
82 std::vector<unsigned char> value(w->begin()+1, w->end()-1);
85 else if (mapOpNames.count(*w))
88 result << mapOpNames[*w];
92 throw std::runtime_error(
"script parse error");
104 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
111 for (
unsigned int i = 0; i < tx.
vout.size(); i++) {
136 bool ok_extended =
false, ok_legacy =
false;
143 ssData >> tx_extended;
144 if (ssData.
empty()) ok_extended =
true;
145 }
catch (
const std::exception&) {
153 tx = std::move(tx_extended);
158 if (try_no_witness) {
162 if (ssData.
empty()) ok_legacy =
true;
163 }
catch (
const std::exception&) {
171 tx = std::move(tx_legacy);
177 tx = std::move(tx_extended);
183 tx = std::move(tx_legacy);
193 if (!
IsHex(hex_tx)) {
197 std::vector<unsigned char> txData(
ParseHex(hex_tx));
198 return DecodeTx(tx, txData, try_no_witness, try_witness);
203 if (!
IsHex(hex_header))
return false;
205 const std::vector<unsigned char> header_data{
ParseHex(hex_header)};
208 ser_header >> header;
209 }
catch (
const std::exception&) {
217 if (!
IsHex(strHexBlk))
220 std::vector<unsigned char> blockData(
ParseHex(strHexBlk));
225 catch (
const std::exception&) {
234 if ((strHex.size() != 64) || !
IsHex(strHex))
247 throw std::runtime_error(strName +
" must be hexadecimal string (not '" + strHex +
"')");
255 static std::map<std::string, int> map_sighash_values = {
263 std::string strHashType = sighash.
get_str();
264 const auto&
it = map_sighash_values.find(strHashType);
265 if (
it != map_sighash_values.end()) {
266 hash_type =
it->second;
268 throw std::runtime_error(strHashType +
" is not a valid sighash parameter.");
bool DecodeHexBlockHeader(CBlockHeader &header, const std::string &hex_header)
const std::string & get_str() const
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...
int ParseSighashString(const UniValue &sighash)
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
static const unsigned int MAX_OPCODE
std::vector< unsigned char > ParseHex(const char *psz)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Double ended buffer combining vector and stream-like interfaces.
opcodetype
Script opcodes.
bool IsHex(const std::string &str)
bool DecodeHexTx(CMutableTransaction &tx, const std::string &hex_tx, bool try_no_witness, bool try_witness)
static const int MAX_SCRIPT_SIZE
std::vector< CTxOut > vout
int64_t atoi64(const std::string &str)
CScript ParseScript(const std::string &s)
const std::string & getValStr() const
Serialized script, used inside transaction inputs and outputs.
static const int PROTOCOL_VERSION
network protocol versioning
std::string GetOpName(opcodetype opcode)
static bool DecodeTx(CMutableTransaction &tx, const std::vector< unsigned char > &tx_data, bool try_no_witness, bool try_witness)
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
A mutable version of CTransaction.
The basic transaction that is broadcasted on the network and contained in blocks. ...
static bool CheckTxScriptsSanity(const CMutableTransaction &tx)
void SetHex(const char *psz)
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)