MCPcopy Create free account
hub / github.com/ElementsProject/elements / ParseScript

Function ParseScript

src/core_read.cpp:65–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63} // namespace
64
65CScript ParseScript(const std::string& s)
66{
67 CScript result;
68
69 std::vector<std::string> words;
70 boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
71
72 for (const std::string& w : words) {
73 if (w.empty()) {
74 // Empty string, ignore. (boost::split given '' will return one word)
75 } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
76 (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
77 {
78 // Number
79 const auto num{ToIntegral<int64_t>(w)};
80
81 // limit the range of numbers ParseScript accepts in decimal
82 // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
83 if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
84 throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
85 "range -0xFFFFFFFF...0xFFFFFFFF");
86 }
87
88 result << num.value();
89 } else if (w.substr(0, 2) == "0x" && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
90 // Raw hex data, inserted NOT pushed onto stack:
91 std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
92 result.insert(result.end(), raw.begin(), raw.end());
93 } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
94 // Single-quoted string, pushed as data. NOTE: this is poor-man's
95 // parsing, spaces/tabs/newlines in single-quoted strings won't work.
96 std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
97 result << value;
98 } else {
99 // opcode, e.g. OP_ADD or ADD:
100 result << ParseOpCode(w);
101 }
102 }
103
104 return result;
105}
106
107// Check that all of the input and output scripts of a transaction contains valid opcodes
108static bool CheckTxScriptsSanity(const CMutableTransaction& tx)

Callers 1

MutateTxAddOutScriptFunction · 0.70

Calls 9

IsHexFunction · 0.85
ParseHexFunction · 0.85
ParseOpCodeFunction · 0.85
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45
valueMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected