You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core_read.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (c) 2009-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "core_io.h"
  5. #include "primitives/block.h"
  6. #include "primitives/transaction.h"
  7. #include "script/script.h"
  8. #include "serialize.h"
  9. #include "streams.h"
  10. #include <univalue.h>
  11. #include "util.h"
  12. #include "utilstrencodings.h"
  13. #include "version.h"
  14. #include <boost/algorithm/string/classification.hpp>
  15. #include <boost/algorithm/string/predicate.hpp>
  16. #include <boost/algorithm/string/replace.hpp>
  17. #include <boost/algorithm/string/split.hpp>
  18. CScript ParseScript(const std::string& s)
  19. {
  20. CScript result;
  21. static std::map<std::string, opcodetype> mapOpNames;
  22. if (mapOpNames.empty())
  23. {
  24. for (unsigned int op = 0; op <= MAX_OPCODE; op++)
  25. {
  26. // Allow OP_RESERVED to get into mapOpNames
  27. if (op < OP_NOP && op != OP_RESERVED)
  28. continue;
  29. const char* name = GetOpName((opcodetype)op);
  30. if (strcmp(name, "OP_UNKNOWN") == 0)
  31. continue;
  32. std::string strName(name);
  33. mapOpNames[strName] = (opcodetype)op;
  34. // Convenience: OP_ADD and just ADD are both recognized:
  35. boost::algorithm::replace_first(strName, "OP_", "");
  36. mapOpNames[strName] = (opcodetype)op;
  37. }
  38. }
  39. std::vector<std::string> words;
  40. boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
  41. for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
  42. {
  43. if (w->empty())
  44. {
  45. // Empty string, ignore. (boost::split given '' will return one word)
  46. }
  47. else if (all(*w, boost::algorithm::is_digit()) ||
  48. (boost::algorithm::starts_with(*w, "-") && all(std::string(w->begin()+1, w->end()), boost::algorithm::is_digit())))
  49. {
  50. // Number
  51. int64_t n = atoi64(*w);
  52. result << n;
  53. }
  54. else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(std::string(w->begin()+2, w->end())))
  55. {
  56. // Raw hex data, inserted NOT pushed onto stack:
  57. std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end()));
  58. result.insert(result.end(), raw.begin(), raw.end());
  59. }
  60. else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'"))
  61. {
  62. // Single-quoted string, pushed as data. NOTE: this is poor-man's
  63. // parsing, spaces/tabs/newlines in single-quoted strings won't work.
  64. std::vector<unsigned char> value(w->begin()+1, w->end()-1);
  65. result << value;
  66. }
  67. else if (mapOpNames.count(*w))
  68. {
  69. // opcode, e.g. OP_ADD or ADD:
  70. result << mapOpNames[*w];
  71. }
  72. else
  73. {
  74. throw std::runtime_error("script parse error");
  75. }
  76. }
  77. return result;
  78. }
  79. // Check that all of the input and output scripts of a transaction contains valid opcodes
  80. bool CheckTxScriptsSanity(const CMutableTransaction& tx)
  81. {
  82. // Check input scripts for non-coinbase txs
  83. if (!CTransaction(tx).IsCoinBase()) {
  84. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  85. if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
  86. return false;
  87. }
  88. }
  89. }
  90. // Check output scripts
  91. for (unsigned int i = 0; i < tx.vout.size(); i++) {
  92. if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx, bool fTryNoWitness)
  99. {
  100. if (!IsHex(strHexTx)) {
  101. return false;
  102. }
  103. std::vector<unsigned char> txData(ParseHex(strHexTx));
  104. if (fTryNoWitness) {
  105. CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
  106. try {
  107. ssData >> tx;
  108. if (ssData.eof() && CheckTxScriptsSanity(tx)) {
  109. return true;
  110. }
  111. }
  112. catch (const std::exception&) {
  113. // Fall through.
  114. }
  115. }
  116. CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
  117. try {
  118. ssData >> tx;
  119. if (!ssData.empty()) {
  120. return false;
  121. }
  122. }
  123. catch (const std::exception&) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
  129. {
  130. if (!IsHex(strHexBlk))
  131. return false;
  132. std::vector<unsigned char> blockData(ParseHex(strHexBlk));
  133. CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
  134. try {
  135. ssBlock >> block;
  136. }
  137. catch (const std::exception&) {
  138. return false;
  139. }
  140. return true;
  141. }
  142. uint256 ParseHashUV(const UniValue& v, const std::string& strName)
  143. {
  144. std::string strHex;
  145. if (v.isStr())
  146. strHex = v.getValStr();
  147. return ParseHashStr(strHex, strName); // Note: ParseHashStr("") throws a runtime_error
  148. }
  149. uint256 ParseHashStr(const std::string& strHex, const std::string& strName)
  150. {
  151. if (!IsHex(strHex)) // Note: IsHex("") is false
  152. throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
  153. uint256 result;
  154. result.SetHex(strHex);
  155. return result;
  156. }
  157. std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
  158. {
  159. std::string strHex;
  160. if (v.isStr())
  161. strHex = v.getValStr();
  162. if (!IsHex(strHex))
  163. throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
  164. return ParseHex(strHex);
  165. }