1 // Copyright (c) 2009-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "primitives/block.h"
8 #include "primitives/transaction.h"
9 #include "script/script.h"
10 #include "serialize.h"
12 #include "univalue/univalue.h"
14 #include "utilstrencodings.h"
17 #include <boost/algorithm/string/classification.hpp>
18 #include <boost/algorithm/string/predicate.hpp>
19 #include <boost/algorithm/string/replace.hpp>
20 #include <boost/algorithm/string/split.hpp>
21 #include <boost/assign/list_of.hpp>
25 CScript ParseScript(std::string s)
29 static map<string, opcodetype> mapOpNames;
31 if (mapOpNames.empty())
33 for (int op = 0; op <= OP_NOP10; op++)
35 // Allow OP_RESERVED to get into mapOpNames
36 if (op < OP_NOP && op != OP_RESERVED)
39 const char* name = GetOpName((opcodetype)op);
40 if (strcmp(name, "OP_UNKNOWN") == 0)
43 mapOpNames[strName] = (opcodetype)op;
44 // Convenience: OP_ADD and just ADD are both recognized:
45 boost::algorithm::replace_first(strName, "OP_", "");
46 mapOpNames[strName] = (opcodetype)op;
51 boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
53 for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
57 // Empty string, ignore. (boost::split given '' will return one word)
59 else if (all(*w, boost::algorithm::is_digit()) ||
60 (boost::algorithm::starts_with(*w, "-") && all(string(w->begin()+1, w->end()), boost::algorithm::is_digit())))
63 int64_t n = atoi64(*w);
66 else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(string(w->begin()+2, w->end())))
68 // Raw hex data, inserted NOT pushed onto stack:
69 std::vector<unsigned char> raw = ParseHex(string(w->begin()+2, w->end()));
70 result.insert(result.end(), raw.begin(), raw.end());
72 else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'"))
74 // Single-quoted string, pushed as data. NOTE: this is poor-man's
75 // parsing, spaces/tabs/newlines in single-quoted strings won't work.
76 std::vector<unsigned char> value(w->begin()+1, w->end()-1);
79 else if (mapOpNames.count(*w))
81 // opcode, e.g. OP_ADD or ADD:
82 result << mapOpNames[*w];
86 throw runtime_error("script parse error");
93 bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx)
98 vector<unsigned char> txData(ParseHex(strHexTx));
99 CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
103 catch (const std::exception&) {
110 bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
112 if (!IsHex(strHexBlk))
115 std::vector<unsigned char> blockData(ParseHex(strHexBlk));
116 CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
120 catch (const std::exception&) {
127 uint256 ParseHashUV(const UniValue& v, const string& strName)
131 strHex = v.getValStr();
132 return ParseHashStr(strHex, strName); // Note: ParseHashStr("") throws a runtime_error
135 uint256 ParseHashStr(const std::string& strHex, const std::string& strName)
137 if (!IsHex(strHex)) // Note: IsHex("") is false
138 throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
141 result.SetHex(strHex);
145 vector<unsigned char> ParseHexUV(const UniValue& v, const string& strName)
149 strHex = v.getValStr();
151 throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
152 return ParseHex(strHex);