MCPcopy Create free account
hub / github.com/LUX-Core/lux / ParseScript

Function ParseScript

src/core_read.cpp:29–81  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27using namespace std;
28
29CScript ParseScript(std::string s)
30{
31 CScript result;
32
33 static map<string, opcodetype> mapOpNames;
34
35 if (mapOpNames.empty()) {
36 for (int op = 0; op <= OP_NOP10; op++) {
37 // Allow OP_RESERVED to get into mapOpNames
38 if (op < OP_NOP && op != OP_RESERVED)
39 continue;
40
41 const char* name = GetOpName((opcodetype)op);
42 if (strcmp(name, "OP_UNKNOWN") == 0)
43 continue;
44 string strName(name);
45 mapOpNames[strName] = (opcodetype)op;
46 // Convenience: OP_ADD and just ADD are both recognized:
47 replace_first(strName, "OP_", "");
48 mapOpNames[strName] = (opcodetype)op;
49 }
50 }
51
52 vector<string> words;
53 split(words, s, is_any_of(" \t\n"), token_compress_on);
54
55 for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w) {
56 if (w->empty()) {
57 // Empty string, ignore. (boost::split given '' will return one word)
58 } else if (all(*w, is_digit()) ||
59 (starts_with(*w, "-") && all(string(w->begin() + 1, w->end()), is_digit()))) {
60 // Number
61 int64_t n = atoi64(*w);
62 result << n;
63 } else if (starts_with(*w, "0x") && (w->begin() + 2 != w->end()) && IsHex(string(w->begin() + 2, w->end()))) {
64 // Raw hex data, inserted NOT pushed onto stack:
65 std::vector<unsigned char> raw = ParseHex(string(w->begin() + 2, w->end()));
66 result.insert(result.end(), raw.begin(), raw.end());
67 } else if (w->size() >= 2 && starts_with(*w, "'") && ends_with(*w, "'")) {
68 // Single-quoted string, pushed as data. NOTE: this is poor-man's
69 // parsing, spaces/tabs/newlines in single-quoted strings won't work.
70 std::vector<unsigned char> value(w->begin() + 1, w->end() - 1);
71 result << value;
72 } else if (mapOpNames.count(*w)) {
73 // opcode, e.g. OP_ADD or ADD:
74 result << mapOpNames[*w];
75 } else {
76 throw runtime_error("script parse error");
77 }
78 }
79
80 return result;
81}
82
83bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx, bool fTryNoWitness)
84{

Callers 3

MutateTxAddOutScriptFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 10

GetOpNameFunction · 0.85
atoi64Function · 0.85
IsHexFunction · 0.85
ParseHexFunction · 0.85
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
insertMethod · 0.45
sizeMethod · 0.45
countMethod · 0.45

Tested by 2

BOOST_AUTO_TEST_CASEFunction · 0.68
BOOST_AUTO_TEST_CASEFunction · 0.68