LCOV - code coverage report
Current view: top level - src - core_read.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 113 119 95.0 %
Date: 2020-09-26 01:30:44 Functions: 8 8 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2020 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>
       8             : #include <primitives/transaction.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             : #include <string>
      23             : 
      24        2707 : CScript ParseScript(const std::string& s)
      25             : {
      26        2707 :     CScript result;
      27             : 
      28        2707 :     static std::map<std::string, opcodetype> mapOpNames;
      29             : 
      30        2707 :     if (mapOpNames.empty())
      31             :     {
      32        3553 :         for (unsigned int op = 0; op <= MAX_OPCODE; op++)
      33             :         {
      34             :             // Allow OP_RESERVED to get into mapOpNames
      35        3534 :             if (op < OP_NOP && op != OP_RESERVED)
      36             :                 continue;
      37             : 
      38        1710 :             std::string strName = GetOpName(static_cast<opcodetype>(op));
      39        1710 :             if (strName == "OP_UNKNOWN")
      40           0 :                 continue;
      41        1710 :             mapOpNames[strName] = static_cast<opcodetype>(op);
      42             :             // Convenience: OP_ADD and just ADD are both recognized:
      43        1710 :             boost::algorithm::replace_first(strName, "OP_", "");
      44        1710 :             mapOpNames[strName] = static_cast<opcodetype>(op);
      45        1710 :         }
      46          19 :     }
      47             : 
      48        2707 :     std::vector<std::string> words;
      49        2707 :     boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
      50             : 
      51       14787 :     for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
      52             :     {
      53       12080 :         if (w->empty())
      54             :         {
      55             :             // Empty string, ignore. (boost::split given '' will return one word)
      56             :         }
      57       19314 :         else if (std::all_of(w->begin(), w->end(), ::IsDigit) ||
      58        7398 :             (w->front() == '-' && w->size() > 1 && std::all_of(w->begin()+1, w->end(), ::IsDigit)))
      59             :         {
      60             :             // Number
      61        4598 :             int64_t n = atoi64(*w);
      62             : 
      63             :             //limit the range of numbers ParseScript accepts in decimal
      64             :             //since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
      65        4598 :             if (n > int64_t{0xffffffff} || n < -1 * int64_t{0xffffffff}) {
      66           4 :                 throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
      67             :                                          "range -0xFFFFFFFF...0xFFFFFFFF");
      68             :             }
      69             : 
      70        4594 :             result << n;
      71        4598 :         }
      72        7318 :         else if (w->substr(0,2) == "0x" && w->size() > 2 && IsHex(std::string(w->begin()+2, w->end())))
      73             :         {
      74             :             // Raw hex data, inserted NOT pushed onto stack:
      75        2200 :             std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end()));
      76        2200 :             result.insert(result.end(), raw.begin(), raw.end());
      77        2200 :         }
      78        5118 :         else if (w->size() >= 2 && w->front() == '\'' && w->back() == '\'')
      79             :         {
      80             :             // Single-quoted string, pushed as data. NOTE: this is poor-man's
      81             :             // parsing, spaces/tabs/newlines in single-quoted strings won't work.
      82        1244 :             std::vector<unsigned char> value(w->begin()+1, w->end()-1);
      83        1244 :             result << value;
      84        1244 :         }
      85        3874 :         else if (mapOpNames.count(*w))
      86             :         {
      87             :             // opcode, e.g. OP_ADD or ADD:
      88        3873 :             result << mapOpNames[*w];
      89             :         }
      90             :         else
      91             :         {
      92           1 :             throw std::runtime_error("script parse error");
      93             :         }
      94             :     }
      95             : 
      96             :     return result;
      97        2712 : }
      98             : 
      99             : // Check that all of the input and output scripts of a transaction contains valid opcodes
     100        1990 : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
     101             : {
     102             :     // Check input scripts for non-coinbase txs
     103        1990 :     if (!CTransaction(tx).IsCoinBase()) {
     104       11054 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
     105        9064 :             if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
     106           0 :                 return false;
     107             :             }
     108             :         }
     109             :     }
     110             :     // Check output scripts
     111       58133 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     112       56144 :         if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
     113           1 :             return false;
     114             :         }
     115             :     }
     116             : 
     117        1989 :     return true;
     118        1990 : }
     119             : 
     120       10283 : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
     121             : {
     122       10283 :     if (!IsHex(hex_tx)) {
     123           3 :         return false;
     124             :     }
     125             : 
     126       10280 :     std::vector<unsigned char> txData(ParseHex(hex_tx));
     127             : 
     128       10280 :     if (try_no_witness) {
     129        2061 :         CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
     130             :         try {
     131        2061 :             ssData >> tx;
     132        2049 :             if (ssData.eof() && (!try_witness || CheckTxScriptsSanity(tx))) {
     133        1991 :                 return true;
     134             :             }
     135          12 :         } catch (const std::exception&) {
     136             :             // Fall through.
     137          12 :         }
     138        2061 :     }
     139             : 
     140        8289 :     if (try_witness) {
     141        8288 :         CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
     142             :         try {
     143        8288 :             ssData >> tx;
     144        8283 :             if (ssData.empty()) {
     145        8282 :                 return true;
     146             :             }
     147           5 :         } catch (const std::exception&) {
     148             :             // Fall through.
     149           5 :         }
     150        8288 :     }
     151             : 
     152           7 :     return false;
     153       10300 : }
     154             : 
     155          13 : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
     156             : {
     157          13 :     if (!IsHex(hex_header)) return false;
     158             : 
     159          12 :     const std::vector<unsigned char> header_data{ParseHex(hex_header)};
     160          12 :     CDataStream ser_header(header_data, SER_NETWORK, PROTOCOL_VERSION);
     161             :     try {
     162          12 :         ser_header >> header;
     163           1 :     } catch (const std::exception&) {
     164             :         return false;
     165           1 :     }
     166          11 :     return true;
     167          14 : }
     168             : 
     169         299 : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
     170             : {
     171         299 :     if (!IsHex(strHexBlk))
     172           0 :         return false;
     173             : 
     174         299 :     std::vector<unsigned char> blockData(ParseHex(strHexBlk));
     175         299 :     CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
     176             :     try {
     177         299 :         ssBlock >> block;
     178           3 :     }
     179             :     catch (const std::exception&) {
     180             :         return false;
     181           3 :     }
     182             : 
     183         296 :     return true;
     184         302 : }
     185             : 
     186          86 : bool ParseHashStr(const std::string& strHex, uint256& result)
     187             : {
     188          86 :     if ((strHex.size() != 64) || !IsHex(strHex))
     189           9 :         return false;
     190             : 
     191          77 :     result.SetHex(strHex);
     192          77 :     return true;
     193          86 : }
     194             : 
     195           3 : std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
     196             : {
     197           3 :     std::string strHex;
     198           3 :     if (v.isStr())
     199           3 :         strHex = v.getValStr();
     200           3 :     if (!IsHex(strHex))
     201           0 :         throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
     202           3 :     return ParseHex(strHex);
     203           3 : }
     204             : 
     205        1861 : int ParseSighashString(const UniValue& sighash)
     206             : {
     207             :     int hash_type = SIGHASH_ALL;
     208        1861 :     if (!sighash.isNull()) {
     209         358 :         static std::map<std::string, int> map_sighash_values = {
     210          15 :             {std::string("ALL"), int(SIGHASH_ALL)},
     211          15 :             {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
     212          15 :             {std::string("NONE"), int(SIGHASH_NONE)},
     213          15 :             {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
     214          15 :             {std::string("SINGLE"), int(SIGHASH_SINGLE)},
     215          15 :             {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
     216             :         };
     217         283 :         std::string strHashType = sighash.get_str();
     218         283 :         const auto& it = map_sighash_values.find(strHashType);
     219         283 :         if (it != map_sighash_values.end()) {
     220         283 :             hash_type = it->second;
     221             :         } else {
     222           0 :             throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
     223             :         }
     224         283 :     }
     225        1861 :     return hash_type;
     226           0 : }

Generated by: LCOV version 1.15