Bitcoin Core  0.19.99
P2P Digital Currency
core_read.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2019 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.
4 
5 #include <core_io.h>
6 
7 #include <primitives/block.h>
9 #include <script/script.h>
10 #include <script/sign.h>
11 #include <serialize.h>
12 #include <streams.h>
13 #include <univalue.h>
14 #include <util/strencodings.h>
15 #include <version.h>
16 
17 #include <boost/algorithm/string/classification.hpp>
18 #include <boost/algorithm/string/replace.hpp>
19 #include <boost/algorithm/string/split.hpp>
20 
21 #include <algorithm>
22 
23 CScript ParseScript(const std::string& s)
24 {
25  CScript result;
26 
27  static std::map<std::string, opcodetype> mapOpNames;
28 
29  if (mapOpNames.empty())
30  {
31  for (unsigned int op = 0; op <= MAX_OPCODE; op++)
32  {
33  // Allow OP_RESERVED to get into mapOpNames
34  if (op < OP_NOP && op != OP_RESERVED)
35  continue;
36 
37  const char* name = GetOpName(static_cast<opcodetype>(op));
38  if (strcmp(name, "OP_UNKNOWN") == 0)
39  continue;
40  std::string strName(name);
41  mapOpNames[strName] = static_cast<opcodetype>(op);
42  // Convenience: OP_ADD and just ADD are both recognized:
43  boost::algorithm::replace_first(strName, "OP_", "");
44  mapOpNames[strName] = static_cast<opcodetype>(op);
45  }
46  }
47 
48  std::vector<std::string> words;
49  boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
50 
51  for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
52  {
53  if (w->empty())
54  {
55  // Empty string, ignore. (boost::split given '' will return one word)
56  }
57  else if (std::all_of(w->begin(), w->end(), ::IsDigit) ||
58  (w->front() == '-' && w->size() > 1 && std::all_of(w->begin()+1, w->end(), ::IsDigit)))
59  {
60  // Number
61  int64_t n = atoi64(*w);
62  result << n;
63  }
64  else if (w->substr(0,2) == "0x" && w->size() > 2 && IsHex(std::string(w->begin()+2, w->end())))
65  {
66  // Raw hex data, inserted NOT pushed onto stack:
67  std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end()));
68  result.insert(result.end(), raw.begin(), raw.end());
69  }
70  else if (w->size() >= 2 && w->front() == '\'' && w->back() == '\'')
71  {
72  // Single-quoted string, pushed as data. NOTE: this is poor-man's
73  // parsing, spaces/tabs/newlines in single-quoted strings won't work.
74  std::vector<unsigned char> value(w->begin()+1, w->end()-1);
75  result << value;
76  }
77  else if (mapOpNames.count(*w))
78  {
79  // opcode, e.g. OP_ADD or ADD:
80  result << mapOpNames[*w];
81  }
82  else
83  {
84  throw std::runtime_error("script parse error");
85  }
86  }
87 
88  return result;
89 }
90 
91 // Check that all of the input and output scripts of a transaction contains valid opcodes
93 {
94  // Check input scripts for non-coinbase txs
95  if (!CTransaction(tx).IsCoinBase()) {
96  for (unsigned int i = 0; i < tx.vin.size(); i++) {
97  if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
98  return false;
99  }
100  }
101  }
102  // Check output scripts
103  for (unsigned int i = 0; i < tx.vout.size(); i++) {
104  if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
105  return false;
106  }
107  }
108 
109  return true;
110 }
111 
112 bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
113 {
114  if (!IsHex(hex_tx)) {
115  return false;
116  }
117 
118  std::vector<unsigned char> txData(ParseHex(hex_tx));
119 
120  if (try_no_witness) {
122  try {
123  ssData >> tx;
124  if (ssData.eof() && (!try_witness || CheckTxScriptsSanity(tx))) {
125  return true;
126  }
127  } catch (const std::exception&) {
128  // Fall through.
129  }
130  }
131 
132  if (try_witness) {
133  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
134  try {
135  ssData >> tx;
136  if (ssData.empty()) {
137  return true;
138  }
139  } catch (const std::exception&) {
140  // Fall through.
141  }
142  }
143 
144  return false;
145 }
146 
147 bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
148 {
149  if (!IsHex(hex_header)) return false;
150 
151  const std::vector<unsigned char> header_data{ParseHex(hex_header)};
152  CDataStream ser_header(header_data, SER_NETWORK, PROTOCOL_VERSION);
153  try {
154  ser_header >> header;
155  } catch (const std::exception&) {
156  return false;
157  }
158  return true;
159 }
160 
161 bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
162 {
163  if (!IsHex(strHexBlk))
164  return false;
165 
166  std::vector<unsigned char> blockData(ParseHex(strHexBlk));
167  CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
168  try {
169  ssBlock >> block;
170  }
171  catch (const std::exception&) {
172  return false;
173  }
174 
175  return true;
176 }
177 
178 bool ParseHashStr(const std::string& strHex, uint256& result)
179 {
180  if ((strHex.size() != 64) || !IsHex(strHex))
181  return false;
182 
183  result.SetHex(strHex);
184  return true;
185 }
186 
187 std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
188 {
189  std::string strHex;
190  if (v.isStr())
191  strHex = v.getValStr();
192  if (!IsHex(strHex))
193  throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
194  return ParseHex(strHex);
195 }
196 
197 int ParseSighashString(const UniValue& sighash)
198 {
199  int hash_type = SIGHASH_ALL;
200  if (!sighash.isNull()) {
201  static std::map<std::string, int> map_sighash_values = {
202  {std::string("ALL"), int(SIGHASH_ALL)},
203  {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
204  {std::string("NONE"), int(SIGHASH_NONE)},
205  {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
206  {std::string("SINGLE"), int(SIGHASH_SINGLE)},
207  {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
208  };
209  std::string strHashType = sighash.get_str();
210  const auto& it = map_sighash_values.find(strHashType);
211  if (it != map_sighash_values.end()) {
212  hash_type = it->second;
213  } else {
214  throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
215  }
216  }
217  return hash_type;
218 }
bool DecodeHexBlockHeader(CBlockHeader &header, const std::string &hex_header)
Definition: core_read.cpp:147
static const int SERIALIZE_TRANSACTION_NO_WITNESS
Definition: transaction.h:15
int ParseSighashString(const UniValue &sighash)
Definition: core_read.cpp:197
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:187
iterator insert(iterator pos, const T &value)
Definition: prevector.h:347
static const unsigned int MAX_OPCODE
Definition: script.h:194
Definition: block.h:72
std::vector< CTxIn > vin
Definition: transaction.h:368
std::vector< unsigned char > ParseHex(const char *psz)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:69
const std::string & get_str() const
bool isStr() const
Definition: univalue.h:81
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:201
bool empty() const
Definition: streams.h:293
const std::string & getValStr() const
Definition: univalue.h:65
iterator end()
Definition: prevector.h:292
opcodetype
Script opcodes.
Definition: script.h:54
const char * name
Definition: rest.cpp:40
bool IsHex(const std::string &str)
Definition: script.h:83
bool DecodeHexTx(CMutableTransaction &tx, const std::string &hex_tx, bool try_no_witness, bool try_witness)
Definition: core_read.cpp:112
static const int MAX_SCRIPT_SIZE
Definition: script.h:32
std::vector< CTxOut > vout
Definition: transaction.h:369
bool isNull() const
Definition: univalue.h:77
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:10
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:23
256-bit opaque blob.
Definition: uint256.h:120
int64_t atoi64(const char *psz)
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
Definition: core_read.cpp:178
A mutable version of CTransaction.
Definition: transaction.h:366
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
static bool CheckTxScriptsSanity(const CMutableTransaction &tx)
Definition: core_read.cpp:92
auto it
Definition: validation.cpp:362
void SetHex(const char *psz)
Definition: uint256.cpp:26
bool eof() const
Definition: streams.h:388
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)
Definition: core_read.cpp:161
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20