Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

core_read.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2015 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 <serialize.h>
11 #include <streams.h>
12 #include <univalue.h>
13 #include <util.h>
14 #include <utilstrencodings.h>
15 #include <version.h>
16 
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 
22 CScript ParseScript(const std::string& s)
23 {
24  CScript result;
25 
26  static std::map<std::string, opcodetype> mapOpNames;
27 
28  if (mapOpNames.empty())
29  {
30  for (unsigned int op = 0; op <= MAX_OPCODE; op++)
31  {
32  // Allow OP_RESERVED to get into mapOpNames
33  if (op < OP_NOP && op != OP_RESERVED)
34  continue;
35 
36  const char* name = GetOpName((opcodetype)op);
37  if (strcmp(name, "OP_UNKNOWN") == 0)
38  continue;
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;
44  }
45  }
46 
47  std::vector<std::string> words;
48  boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
49 
50  for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
51  {
52  if (w->empty())
53  {
54  // Empty string, ignore. (boost::split given '' will return one word)
55  }
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())))
58  {
59  // Number
60  int64_t n = atoi64(*w);
61  result << n;
62  }
63  else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(std::string(w->begin()+2, w->end())))
64  {
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());
68  }
69  else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'"))
70  {
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);
74  result << value;
75  }
76  else if (mapOpNames.count(*w))
77  {
78  // opcode, e.g. OP_ADD or ADD:
79  result << mapOpNames[*w];
80  }
81  else
82  {
83  throw std::runtime_error("script parse error");
84  }
85  }
86 
87  return result;
88 }
89 
90 bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx)
91 {
92  if (!IsHex(strHexTx))
93  return false;
94 
95  std::vector<unsigned char> txData(ParseHex(strHexTx));
96  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
97  try {
98  ssData >> tx;
99  if (!ssData.empty())
100  return false;
101  }
102  catch (const std::exception&) {
103  return false;
104  }
105 
106  return true;
107 }
108 
109 bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
110 {
111  if (!IsHex(strHexBlk))
112  return false;
113 
114  std::vector<unsigned char> blockData(ParseHex(strHexBlk));
115  CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
116  try {
117  ssBlock >> block;
118  }
119  catch (const std::exception&) {
120  return false;
121  }
122 
123  return true;
124 }
125 
126 uint256 ParseHashUV(const UniValue& v, const std::string& strName)
127 {
128  std::string strHex;
129  if (v.isStr())
130  strHex = v.getValStr();
131  return ParseHashStr(strHex, strName); // Note: ParseHashStr("") throws a runtime_error
132 }
133 
134 uint256 ParseHashStr(const std::string& strHex, const std::string& strName)
135 {
136  if (!IsHex(strHex)) // Note: IsHex("") is false
137  throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
138 
139  uint256 result;
140  result.SetHex(strHex);
141  return result;
142 }
143 
144 std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
145 {
146  std::string strHex;
147  if (v.isStr())
148  strHex = v.getValStr();
149  if (!IsHex(strHex))
150  throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
151  return ParseHex(strHex);
152 }
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:144
iterator insert(iterator pos, const T &value)
Definition: prevector.h:375
static const unsigned int MAX_OPCODE
Definition: script.h:195
Definition: block.h:72
bool isStr() const
Definition: univalue.h:82
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
bool empty() const
Definition: streams.h:195
const std::string & getValStr() const
Definition: univalue.h:66
iterator end()
Definition: prevector.h:320
opcodetype
Script opcodes.
Definition: script.h:48
const char * name
Definition: rest.cpp:36
bool IsHex(const std::string &str)
Definition: script.h:77
uint256 ParseHashStr(const std::string &strHex, const std::string &strName)
Definition: core_read.cpp:134
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:10
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:22
256-bit opaque blob.
Definition: uint256.h:123
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:14
int64_t atoi64(const char *psz)
bool DecodeHexTx(CMutableTransaction &tx, const std::string &strHexTx)
Definition: core_read.cpp:90
A mutable version of CTransaction.
Definition: transaction.h:291
void SetHex(const char *psz)
Definition: uint256.cpp:27
uint256 ParseHashUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:126
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)
Definition: core_read.cpp:109
std::vector< unsigned char > ParseHex(const char *psz)
Released under the MIT license