MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ParseScript

Function ParseScript

src/core_io.cpp:92–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90} // namespace
91
92CScript ParseScript(const std::string& s)
93{
94 CScript result;
95
96 std::vector<std::string> words = SplitString(s, " \t\n");
97
98 for (const std::string& w : words) {
99 if (w.empty()) {
100 // Empty string, ignore. (SplitString doesn't combine multiple separators)
101 } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
102 (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
103 {
104 // Number
105 const auto num{ToIntegral<int64_t>(w)};
106
107 // limit the range of numbers ParseScript accepts in decimal
108 // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
109 if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
110 throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
111 "range -0xFFFFFFFF...0xFFFFFFFF");
112 }
113
114 result << num.value();
115 } else if (w.starts_with("0x") && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
116 // Raw hex data, inserted NOT pushed onto stack:
117 std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
118 result.insert(result.end(), raw.begin(), raw.end());
119 } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
120 // Single-quoted string, pushed as data. NOTE: this is poor-man's
121 // parsing, spaces/tabs/newlines in single-quoted strings won't work.
122 std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
123 result << value;
124 } else {
125 // opcode, e.g. OP_ADD or ADD:
126 result << ParseOpCode(w);
127 }
128 }
129
130 return result;
131}
132
133/// Check that all of the input and output scripts of a transaction contain valid opcodes
134static bool CheckTxScriptsSanity(const CMutableTransaction& tx)

Callers 1

MutateTxAddOutScriptFunction · 0.70

Calls 13

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

Tested by

no test coverage detected