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