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.
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>
23 using namespace boost;
24 using namespace boost::algorithm;
27 CScript ParseScript(std::string s)
31 static map<string, opcodetype> mapOpNames;
33 if (mapOpNames.empty())
35 for (int op = 0; op <= OP_NOP10; op++)
37 // Allow OP_RESERVED to get into mapOpNames
38 if (op < OP_NOP && op != OP_RESERVED)
41 const char* name = GetOpName((opcodetype)op);
42 if (strcmp(name, "OP_UNKNOWN") == 0)
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;
53 split(words, s, is_any_of(" \t\n"), token_compress_on);
55 for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
59 // Empty string, ignore. (boost::split given '' will return one word)
61 else if (all(*w, is_digit()) ||
62 (starts_with(*w, "-") && all(string(w->begin()+1, w->end()), is_digit())))
65 int64_t n = atoi64(*w);
68 else if (starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(string(w->begin()+2, w->end())))
70 // Raw hex data, inserted NOT pushed onto stack:
71 std::vector<unsigned char> raw = ParseHex(string(w->begin()+2, w->end()));
72 result.insert(result.end(), raw.begin(), raw.end());
74 else if (w->size() >= 2 && starts_with(*w, "'") && ends_with(*w, "'"))
76 // Single-quoted string, pushed as data. NOTE: this is poor-man's
77 // parsing, spaces/tabs/newlines in single-quoted strings won't work.
78 std::vector<unsigned char> value(w->begin()+1, w->end()-1);
81 else if (mapOpNames.count(*w))
83 // opcode, e.g. OP_ADD or ADD:
84 result << mapOpNames[*w];
88 throw runtime_error("script parse error");
95 bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx)
100 vector<unsigned char> txData(ParseHex(strHexTx));
101 CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
105 catch (const std::exception &) {
112 bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
114 if (!IsHex(strHexBlk))
117 std::vector<unsigned char> blockData(ParseHex(strHexBlk));
118 CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
122 catch (const std::exception &) {
129 uint256 ParseHashUV(const UniValue& v, const string& strName)
133 strHex = v.getValStr();
134 if (!IsHex(strHex)) // Note: IsHex("") is false
135 throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
138 result.SetHex(strHex);
142 vector<unsigned char> ParseHexUV(const UniValue& v, const string& strName)
146 strHex = v.getValStr();
148 throw runtime_error(strName+" must be hexadecimal string (not '"+strHex+"')");
149 return ParseHex(strHex);