1 // Copyright (c) 2009-2016 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"
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>
22 CScript
ParseScript(const std::string
& s
)
26 static std::map
<std::string
, opcodetype
> mapOpNames
;
28 if (mapOpNames
.empty())
30 for (unsigned int op
= 0; op
<= MAX_OPCODE
; op
++)
32 // Allow OP_RESERVED to get into mapOpNames
33 if (op
< OP_NOP
&& op
!= OP_RESERVED
)
36 const char* name
= GetOpName((opcodetype
)op
);
37 if (strcmp(name
, "OP_UNKNOWN") == 0)
39 std::string
strName(name
);
40 mapOpNames
[strName
] = (opcodetype
)op
;
41 // Convenience: OP_ADD and just ADD are both recognized:
42 boost::algorithm::replace_first(strName
, "OP_", "");
43 mapOpNames
[strName
] = (opcodetype
)op
;
47 std::vector
<std::string
> words
;
48 boost::algorithm::split(words
, s
, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on
);
50 for (std::vector
<std::string
>::const_iterator w
= words
.begin(); w
!= words
.end(); ++w
)
54 // Empty string, ignore. (boost::split given '' will return one word)
56 else if (all(*w
, boost::algorithm::is_digit()) ||
57 (boost::algorithm::starts_with(*w
, "-") && all(std::string(w
->begin()+1, w
->end()), boost::algorithm::is_digit())))
60 int64_t n
= atoi64(*w
);
63 else if (boost::algorithm::starts_with(*w
, "0x") && (w
->begin()+2 != w
->end()) && IsHex(std::string(w
->begin()+2, w
->end())))
65 // Raw hex data, inserted NOT pushed onto stack:
66 std::vector
<unsigned char> raw
= ParseHex(std::string(w
->begin()+2, w
->end()));
67 result
.insert(result
.end(), raw
.begin(), raw
.end());
69 else if (w
->size() >= 2 && boost::algorithm::starts_with(*w
, "'") && boost::algorithm::ends_with(*w
, "'"))
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.
73 std::vector
<unsigned char> value(w
->begin()+1, w
->end()-1);
76 else if (mapOpNames
.count(*w
))
78 // opcode, e.g. OP_ADD or ADD:
79 result
<< mapOpNames
[*w
];
83 throw std::runtime_error("script parse error");
90 // Check that all of the input and output scripts of a transaction contains valid opcodes
91 bool CheckTxScriptsSanity(const CMutableTransaction
& tx
)
93 // Check input scripts for non-coinbase txs
94 if (!CTransaction(tx
).IsCoinBase()) {
95 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++) {
96 if (!tx
.vin
[i
].scriptSig
.HasValidOps() || tx
.vin
[i
].scriptSig
.size() > MAX_SCRIPT_SIZE
) {
101 // Check output scripts
102 for (unsigned int i
= 0; i
< tx
.vout
.size(); i
++) {
103 if (!tx
.vout
[i
].scriptPubKey
.HasValidOps() || tx
.vout
[i
].scriptPubKey
.size() > MAX_SCRIPT_SIZE
) {
111 bool DecodeHexTx(CMutableTransaction
& tx
, const std::string
& strHexTx
, bool fTryNoWitness
)
113 if (!IsHex(strHexTx
)) {
117 std::vector
<unsigned char> txData(ParseHex(strHexTx
));
120 CDataStream
ssData(txData
, SER_NETWORK
, PROTOCOL_VERSION
| SERIALIZE_TRANSACTION_NO_WITNESS
);
123 if (ssData
.eof() && CheckTxScriptsSanity(tx
)) {
127 catch (const std::exception
&) {
132 CDataStream
ssData(txData
, SER_NETWORK
, PROTOCOL_VERSION
);
135 if (!ssData
.empty()) {
139 catch (const std::exception
&) {
146 bool DecodeHexBlk(CBlock
& block
, const std::string
& strHexBlk
)
148 if (!IsHex(strHexBlk
))
151 std::vector
<unsigned char> blockData(ParseHex(strHexBlk
));
152 CDataStream
ssBlock(blockData
, SER_NETWORK
, PROTOCOL_VERSION
);
156 catch (const std::exception
&) {
163 uint256
ParseHashUV(const UniValue
& v
, const std::string
& strName
)
167 strHex
= v
.getValStr();
168 return ParseHashStr(strHex
, strName
); // Note: ParseHashStr("") throws a runtime_error
171 uint256
ParseHashStr(const std::string
& strHex
, const std::string
& strName
)
173 if (!IsHex(strHex
)) // Note: IsHex("") is false
174 throw std::runtime_error(strName
+ " must be hexadecimal string (not '" + strHex
+ "')");
177 result
.SetHex(strHex
);
181 std::vector
<unsigned char> ParseHexUV(const UniValue
& v
, const std::string
& strName
)
185 strHex
= v
.getValStr();
187 throw std::runtime_error(strName
+ " must be hexadecimal string (not '" + strHex
+ "')");
188 return ParseHex(strHex
);