Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2021 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/split.hpp>
# 19 : :
# 20 : : #include <algorithm>
# 21 : : #include <string>
# 22 : :
# 23 : : namespace {
# 24 : : class OpCodeParser
# 25 : : {
# 26 : : private:
# 27 : : std::map<std::string, opcodetype> mapOpNames;
# 28 : :
# 29 : : public:
# 30 : : OpCodeParser()
# 31 : 2 : {
# 32 [ + + ]: 374 : for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
# 33 : : // Allow OP_RESERVED to get into mapOpNames
# 34 [ + + ][ + + ]: 372 : if (op < OP_NOP && op != OP_RESERVED) {
# 35 : 192 : continue;
# 36 : 192 : }
# 37 : :
# 38 : 180 : std::string strName = GetOpName(static_cast<opcodetype>(op));
# 39 [ - + ]: 180 : if (strName == "OP_UNKNOWN") {
# 40 : 0 : continue;
# 41 : 0 : }
# 42 : 180 : mapOpNames[strName] = static_cast<opcodetype>(op);
# 43 : : // Convenience: OP_ADD and just ADD are both recognized:
# 44 [ + - ]: 180 : if (strName.compare(0, 3, "OP_") == 0) { // strName starts with "OP_"
# 45 : 180 : mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
# 46 : 180 : }
# 47 : 180 : }
# 48 : 2 : }
# 49 : : opcodetype Parse(const std::string& s) const
# 50 : 7740 : {
# 51 : 7740 : auto it = mapOpNames.find(s);
# 52 [ + + ]: 7740 : if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
# 53 : 7738 : return it->second;
# 54 : 7740 : }
# 55 : : };
# 56 : :
# 57 : : opcodetype ParseOpCode(const std::string& s)
# 58 : 7740 : {
# 59 : 7740 : static const OpCodeParser ocp;
# 60 : 7740 : return ocp.Parse(s);
# 61 : 7740 : }
# 62 : :
# 63 : : } // namespace
# 64 : :
# 65 : : CScript ParseScript(const std::string& s)
# 66 : 5452 : {
# 67 : 5452 : CScript result;
# 68 : :
# 69 : 5452 : std::vector<std::string> words;
# 70 : 5452 : boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
# 71 : :
# 72 [ + + ]: 24150 : for (const std::string& w : words) {
# 73 [ + + ]: 24150 : if (w.empty()) {
# 74 : : // Empty string, ignore. (boost::split given '' will return one word)
# 75 [ + + ][ + + ]: 23816 : } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
# 76 [ + + ][ + - ]: 23816 : (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
# [ + - ]
# 77 : 9170 : {
# 78 : : // Number
# 79 : 9170 : const auto num{ToIntegral<int64_t>(w)};
# 80 : :
# 81 : : // limit the range of numbers ParseScript accepts in decimal
# 82 : : // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
# 83 [ + + ][ + + ]: 9170 : if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
# [ + + ][ - + ]
# 84 : 4 : throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
# 85 : 4 : "range -0xFFFFFFFF...0xFFFFFFFF");
# 86 : 4 : }
# 87 : :
# 88 : 9166 : result << num.value();
# 89 [ + + ][ + + ]: 14646 : } else if (w.substr(0, 2) == "0x" && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
# [ + - ][ + - ]
# 90 : : // Raw hex data, inserted NOT pushed onto stack:
# 91 : 4414 : std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
# 92 : 4414 : result.insert(result.end(), raw.begin(), raw.end());
# 93 [ + - ][ + + ]: 10232 : } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
# [ + - ]
# 94 : : // Single-quoted string, pushed as data. NOTE: this is poor-man's
# 95 : : // parsing, spaces/tabs/newlines in single-quoted strings won't work.
# 96 : 2492 : std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
# 97 : 2492 : result << value;
# 98 : 7740 : } else {
# 99 : : // opcode, e.g. OP_ADD or ADD:
# 100 : 7740 : result << ParseOpCode(w);
# 101 : 7740 : }
# 102 : 24150 : }
# 103 : :
# 104 : 5448 : return result;
# 105 : 5452 : }
# 106 : :
# 107 : : // Check that all of the input and output scripts of a transaction contains valid opcodes
# 108 : : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
# 109 : 19853 : {
# 110 : : // Check input scripts for non-coinbase txs
# 111 [ + + ]: 19853 : if (!CTransaction(tx).IsCoinBase()) {
# 112 [ + + ]: 84876 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
# 113 [ + + ][ - + ]: 65029 : if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
# 114 : 1 : return false;
# 115 : 1 : }
# 116 : 65029 : }
# 117 : 19848 : }
# 118 : : // Check output scripts
# 119 [ + + ]: 195432 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
# 120 [ + + ][ - + ]: 175615 : if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
# 121 : 35 : return false;
# 122 : 35 : }
# 123 : 175615 : }
# 124 : :
# 125 : 19817 : return true;
# 126 : 19852 : }
# 127 : :
# 128 : : static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
# 129 : 19868 : {
# 130 : : // General strategy:
# 131 : : // - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
# 132 : : // the presence of witnesses) and with legacy serialization (which interprets the tag as a
# 133 : : // 0-input 1-output incomplete transaction).
# 134 : : // - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
# 135 : : // disables extended if false).
# 136 : : // - Ignore serializations that do not fully consume the hex string.
# 137 : : // - If neither succeeds, fail.
# 138 : : // - If only one succeeds, return that one.
# 139 : : // - If both decode attempts succeed:
# 140 : : // - If only one passes the CheckTxScriptsSanity check, return that one.
# 141 : : // - If neither or both pass CheckTxScriptsSanity, return the extended one.
# 142 : :
# 143 : 19868 : CMutableTransaction tx_extended, tx_legacy;
# 144 : 19868 : bool ok_extended = false, ok_legacy = false;
# 145 : :
# 146 : : // Try decoding with extended serialization support, and remember if the result successfully
# 147 : : // consumes the entire input.
# 148 [ + + ]: 19868 : if (try_witness) {
# 149 : 19860 : CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION);
# 150 : 19860 : try {
# 151 : 19860 : ssData >> tx_extended;
# 152 [ + + ]: 19860 : if (ssData.empty()) ok_extended = true;
# 153 : 19860 : } catch (const std::exception&) {
# 154 : : // Fall through.
# 155 : 270 : }
# 156 : 19860 : }
# 157 : :
# 158 : : // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
# 159 : : // don't bother decoding the other way.
# 160 [ + + ][ + + ]: 19868 : if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
# 161 : 19552 : tx = std::move(tx_extended);
# 162 : 19552 : return true;
# 163 : 19552 : }
# 164 : :
# 165 : : // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
# 166 [ + + ]: 316 : if (try_no_witness) {
# 167 : 275 : CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
# 168 : 275 : try {
# 169 : 275 : ssData >> tx_legacy;
# 170 [ + + ]: 275 : if (ssData.empty()) ok_legacy = true;
# 171 : 275 : } catch (const std::exception&) {
# 172 : : // Fall through.
# 173 : 4 : }
# 174 : 275 : }
# 175 : :
# 176 : : // If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
# 177 : : // at this point that extended decoding either failed or doesn't pass the sanity check.
# 178 [ + + ][ + - ]: 316 : if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
# 179 : 265 : tx = std::move(tx_legacy);
# 180 : 265 : return true;
# 181 : 265 : }
# 182 : :
# 183 : : // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
# 184 [ + + ]: 51 : if (ok_extended) {
# 185 : 36 : tx = std::move(tx_extended);
# 186 : 36 : return true;
# 187 : 36 : }
# 188 : :
# 189 : : // If legacy decoding succeeded and extended didn't, return the legacy one.
# 190 [ - + ]: 15 : if (ok_legacy) {
# 191 : 0 : tx = std::move(tx_legacy);
# 192 : 0 : return true;
# 193 : 0 : }
# 194 : :
# 195 : : // If none succeeded, we failed.
# 196 : 15 : return false;
# 197 : 15 : }
# 198 : :
# 199 : : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
# 200 : 19873 : {
# 201 [ + + ]: 19873 : if (!IsHex(hex_tx)) {
# 202 : 5 : return false;
# 203 : 5 : }
# 204 : :
# 205 : 19868 : std::vector<unsigned char> txData(ParseHex(hex_tx));
# 206 : 19868 : return DecodeTx(tx, txData, try_no_witness, try_witness);
# 207 : 19873 : }
# 208 : :
# 209 : : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
# 210 : 15 : {
# 211 [ + + ]: 15 : if (!IsHex(hex_header)) return false;
# 212 : :
# 213 : 14 : const std::vector<unsigned char> header_data{ParseHex(hex_header)};
# 214 : 14 : CDataStream ser_header(header_data, SER_NETWORK, PROTOCOL_VERSION);
# 215 : 14 : try {
# 216 : 14 : ser_header >> header;
# 217 : 14 : } catch (const std::exception&) {
# 218 : 1 : return false;
# 219 : 1 : }
# 220 : 13 : return true;
# 221 : 14 : }
# 222 : :
# 223 : : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
# 224 : 4252 : {
# 225 [ - + ]: 4252 : if (!IsHex(strHexBlk))
# 226 : 0 : return false;
# 227 : :
# 228 : 4252 : std::vector<unsigned char> blockData(ParseHex(strHexBlk));
# 229 : 4252 : CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
# 230 : 4252 : try {
# 231 : 4252 : ssBlock >> block;
# 232 : 4252 : }
# 233 : 4252 : catch (const std::exception&) {
# 234 : 3 : return false;
# 235 : 3 : }
# 236 : :
# 237 : 4249 : return true;
# 238 : 4252 : }
# 239 : :
# 240 : : bool ParseHashStr(const std::string& strHex, uint256& result)
# 241 : 93 : {
# 242 [ + + ][ - + ]: 93 : if ((strHex.size() != 64) || !IsHex(strHex))
# 243 : 6 : return false;
# 244 : :
# 245 : 87 : result.SetHex(strHex);
# 246 : 87 : return true;
# 247 : 93 : }
# 248 : :
# 249 : : std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
# 250 : 0 : {
# 251 : 0 : std::string strHex;
# 252 [ # # ]: 0 : if (v.isStr())
# 253 : 0 : strHex = v.getValStr();
# 254 [ # # ]: 0 : if (!IsHex(strHex))
# 255 : 0 : throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
# 256 : 0 : return ParseHex(strHex);
# 257 : 0 : }
# 258 : :
# 259 : : int ParseSighashString(const UniValue& sighash)
# 260 : 1722 : {
# 261 : 1722 : int hash_type = SIGHASH_DEFAULT;
# 262 [ + + ]: 1722 : if (!sighash.isNull()) {
# 263 : 214 : static std::map<std::string, int> map_sighash_values = {
# 264 : 214 : {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
# 265 : 214 : {std::string("ALL"), int(SIGHASH_ALL)},
# 266 : 214 : {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
# 267 : 214 : {std::string("NONE"), int(SIGHASH_NONE)},
# 268 : 214 : {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
# 269 : 214 : {std::string("SINGLE"), int(SIGHASH_SINGLE)},
# 270 : 214 : {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
# 271 : 214 : };
# 272 : 214 : std::string strHashType = sighash.get_str();
# 273 : 214 : const auto& it = map_sighash_values.find(strHashType);
# 274 [ + - ]: 214 : if (it != map_sighash_values.end()) {
# 275 : 214 : hash_type = it->second;
# 276 : 214 : } else {
# 277 : 0 : throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
# 278 : 0 : }
# 279 : 214 : }
# 280 : 1722 : return hash_type;
# 281 : 1722 : }
|