]>
Commit | Line | Data |
---|---|---|
2b600992 PK |
1 | // Copyright (c) 2009-2014 The Bitcoin developers |
2 | // Distributed under the MIT/X11 software license, see the accompanying | |
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | |
ae775b5b | 4 | |
ae775b5b JG |
5 | #include "core_io.h" |
6 | #include "core.h" | |
7 | #include "serialize.h" | |
cbd22a50 | 8 | #include "script/script.h" |
cbe39a38 | 9 | #include "util.h" |
b2aeaa79 JG |
10 | |
11 | #include <boost/assign/list_of.hpp> | |
12 | #include <boost/algorithm/string/classification.hpp> | |
13 | #include <boost/algorithm/string/predicate.hpp> | |
14 | #include <boost/algorithm/string/split.hpp> | |
15 | #include <boost/algorithm/string/replace.hpp> | |
cbe39a38 | 16 | #include "univalue/univalue.h" |
ae775b5b JG |
17 | |
18 | using namespace std; | |
b2aeaa79 JG |
19 | using namespace boost; |
20 | using namespace boost::algorithm; | |
21 | ||
22 | CScript ParseScript(std::string s) | |
23 | { | |
24 | CScript result; | |
25 | ||
26 | static map<string, opcodetype> mapOpNames; | |
27 | ||
2a584009 | 28 | if (mapOpNames.empty()) |
b2aeaa79 JG |
29 | { |
30 | for (int op = 0; op <= OP_NOP10; op++) | |
31 | { | |
32 | // Allow OP_RESERVED to get into mapOpNames | |
33 | if (op < OP_NOP && op != OP_RESERVED) | |
34 | continue; | |
35 | ||
36 | const char* name = GetOpName((opcodetype)op); | |
37 | if (strcmp(name, "OP_UNKNOWN") == 0) | |
38 | continue; | |
39 | string strName(name); | |
40 | mapOpNames[strName] = (opcodetype)op; | |
41 | // Convenience: OP_ADD and just ADD are both recognized: | |
42 | replace_first(strName, "OP_", ""); | |
43 | mapOpNames[strName] = (opcodetype)op; | |
44 | } | |
45 | } | |
46 | ||
47 | vector<string> words; | |
48 | split(words, s, is_any_of(" \t\n"), token_compress_on); | |
49 | ||
2a584009 | 50 | for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w) |
b2aeaa79 | 51 | { |
2a584009 | 52 | if (w->empty()) |
b2aeaa79 JG |
53 | { |
54 | // Empty string, ignore. (boost::split given '' will return one word) | |
55 | } | |
2a584009 CF |
56 | else if (all(*w, is_digit()) || |
57 | (starts_with(*w, "-") && all(string(w->begin()+1, w->end()), is_digit()))) | |
b2aeaa79 JG |
58 | { |
59 | // Number | |
2a584009 | 60 | int64_t n = atoi64(*w); |
b2aeaa79 JG |
61 | result << n; |
62 | } | |
2a584009 | 63 | else if (starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(string(w->begin()+2, w->end()))) |
b2aeaa79 JG |
64 | { |
65 | // Raw hex data, inserted NOT pushed onto stack: | |
2a584009 | 66 | std::vector<unsigned char> raw = ParseHex(string(w->begin()+2, w->end())); |
b2aeaa79 JG |
67 | result.insert(result.end(), raw.begin(), raw.end()); |
68 | } | |
2a584009 | 69 | else if (w->size() >= 2 && starts_with(*w, "'") && ends_with(*w, "'")) |
b2aeaa79 JG |
70 | { |
71 | // Single-quoted string, pushed as data. NOTE: this is poor-man's | |
72 | // parsing, spaces/tabs/newlines in single-quoted strings won't work. | |
2a584009 | 73 | std::vector<unsigned char> value(w->begin()+1, w->end()-1); |
b2aeaa79 JG |
74 | result << value; |
75 | } | |
2a584009 | 76 | else if (mapOpNames.count(*w)) |
b2aeaa79 JG |
77 | { |
78 | // opcode, e.g. OP_ADD or ADD: | |
2a584009 | 79 | result << mapOpNames[*w]; |
b2aeaa79 JG |
80 | } |
81 | else | |
82 | { | |
83 | throw runtime_error("script parse error"); | |
84 | } | |
85 | } | |
86 | ||
87 | return result; | |
88 | } | |
ae775b5b JG |
89 | |
90 | bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx) | |
91 | { | |
92 | if (!IsHex(strHexTx)) | |
93 | return false; | |
94 | ||
95 | vector<unsigned char> txData(ParseHex(strHexTx)); | |
96 | CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION); | |
97 | try { | |
98 | ssData >> tx; | |
99 | } | |
100 | catch (std::exception &e) { | |
101 | return false; | |
102 | } | |
103 | ||
104 | return true; | |
105 | } | |
106 | ||
cbe39a38 JG |
107 | uint256 ParseHashUV(const UniValue& v, const string& strName) |
108 | { | |
109 | string strHex; | |
110 | if (v.isStr()) | |
111 | strHex = v.getValStr(); | |
112 | if (!IsHex(strHex)) // Note: IsHex("") is false | |
113 | throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')"); | |
114 | ||
115 | uint256 result; | |
116 | result.SetHex(strHex); | |
117 | return result; | |
118 | } | |
119 | ||
120 | vector<unsigned char> ParseHexUV(const UniValue& v, const string& strName) | |
121 | { | |
122 | string strHex; | |
123 | if (v.isStr()) | |
124 | strHex = v.getValStr(); | |
125 | if (!IsHex(strHex)) | |
126 | throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')"); | |
127 | return ParseHex(strHex); | |
128 | } |