core_io.cpp raw

   1  // Copyright (c) 2009-present 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 <addresstype.h>
   8  #include <coins.h>
   9  #include <consensus/amount.h>
  10  #include <consensus/consensus.h>
  11  #include <consensus/validation.h>
  12  #include <crypto/hex_base.h>
  13  #include <key_io.h>
  14  #include <prevector.h>
  15  #include <primitives/block.h>
  16  #include <primitives/transaction.h>
  17  #include <script/descriptor.h>
  18  #include <script/interpreter.h>
  19  #include <script/script.h>
  20  #include <script/signingprovider.h>
  21  #include <script/solver.h>
  22  #include <serialize.h>
  23  #include <streams.h>
  24  #include <tinyformat.h>
  25  #include <uint256.h>
  26  #include <undo.h>
  27  #include <univalue.h>
  28  #include <util/check.h>
  29  #include <util/result.h>
  30  #include <util/strencodings.h>
  31  #include <util/string.h>
  32  #include <util/translation.h>
  33  
  34  #include <algorithm>
  35  #include <compare>
  36  #include <cstdint>
  37  #include <exception>
  38  #include <functional>
  39  #include <map>
  40  #include <memory>
  41  #include <optional>
  42  #include <span>
  43  #include <stdexcept>
  44  #include <string>
  45  #include <utility>
  46  #include <vector>
  47  
  48  using util::SplitString;
  49  
  50  namespace {
  51  class OpCodeParser
  52  {
  53  private:
  54      std::map<std::string, opcodetype> mapOpNames;
  55  
  56  public:
  57      OpCodeParser()
  58      {
  59          for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
  60              // Allow OP_RESERVED to get into mapOpNames
  61              if (op < OP_NOP && op != OP_RESERVED) {
  62                  continue;
  63              }
  64  
  65              std::string strName = GetOpName(static_cast<opcodetype>(op));
  66              if (strName == "OP_UNKNOWN") {
  67                  continue;
  68              }
  69              mapOpNames[strName] = static_cast<opcodetype>(op);
  70              // Convenience: OP_ADD and just ADD are both recognized:
  71              if (strName.starts_with("OP_")) {
  72                  mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
  73              }
  74          }
  75      }
  76      opcodetype Parse(const std::string& s) const
  77      {
  78          auto it = mapOpNames.find(s);
  79          if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
  80          return it->second;
  81      }
  82  };
  83  
  84  opcodetype ParseOpCode(const std::string& s)
  85  {
  86      static const OpCodeParser ocp;
  87      return ocp.Parse(s);
  88  }
  89  
  90  } // namespace
  91  
  92  CScript ParseScript(const std::string& s)
  93  {
  94      CScript result;
  95  
  96      std::vector<std::string> words = SplitString(s, " \t\n");
  97  
  98      for (const std::string& w : words) {
  99          if (w.empty()) {
 100              // Empty string, ignore. (SplitString doesn't combine multiple separators)
 101          } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
 102                     (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
 103          {
 104              // Number
 105              const auto num{ToIntegral<int64_t>(w)};
 106  
 107              // limit the range of numbers ParseScript accepts in decimal
 108              // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
 109              if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
 110                  throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
 111                                           "range -0xFFFFFFFF...0xFFFFFFFF");
 112              }
 113  
 114              result << num.value();
 115          } else if (w.starts_with("0x") && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
 116              // Raw hex data, inserted NOT pushed onto stack:
 117              std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
 118              result.insert(result.end(), raw.begin(), raw.end());
 119          } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
 120              // Single-quoted string, pushed as data. NOTE: this is poor-man's
 121              // parsing, spaces/tabs/newlines in single-quoted strings won't work.
 122              std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
 123              result << value;
 124          } else {
 125              // opcode, e.g. OP_ADD or ADD:
 126              result << ParseOpCode(w);
 127          }
 128      }
 129  
 130      return result;
 131  }
 132  
 133  /// Check that all of the input and output scripts of a transaction contain valid opcodes
 134  static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
 135  {
 136      // Check input scripts for non-coinbase txs
 137      if (!CTransaction(tx).IsCoinBase()) {
 138          for (unsigned int i = 0; i < tx.vin.size(); i++) {
 139              if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
 140                  return false;
 141              }
 142          }
 143      }
 144      // Check output scripts
 145      for (unsigned int i = 0; i < tx.vout.size(); i++) {
 146          if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
 147              return false;
 148          }
 149      }
 150  
 151      return true;
 152  }
 153  
 154  static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
 155  {
 156      // General strategy:
 157      // - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
 158      //   the presence of witnesses) and with legacy serialization (which interprets the tag as a
 159      //   0-input 1-output incomplete transaction).
 160      //   - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
 161      //     disables extended if false).
 162      //   - Ignore serializations that do not fully consume the hex string.
 163      // - If neither succeeds, fail.
 164      // - If only one succeeds, return that one.
 165      // - If both decode attempts succeed:
 166      //   - If only one passes the CheckTxScriptsSanity check, return that one.
 167      //   - If neither or both pass CheckTxScriptsSanity, return the extended one.
 168  
 169      CMutableTransaction tx_extended, tx_legacy;
 170      bool ok_extended = false, ok_legacy = false;
 171  
 172      // Try decoding with extended serialization support, and remember if the result successfully
 173      // consumes the entire input.
 174      if (try_witness) {
 175          SpanReader ssData{tx_data};
 176          try {
 177              ssData >> TX_WITH_WITNESS(tx_extended);
 178              if (ssData.empty()) ok_extended = true;
 179          } catch (const std::exception&) {
 180              // Fall through.
 181          }
 182      }
 183  
 184      // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
 185      // don't bother decoding the other way.
 186      if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
 187          tx = std::move(tx_extended);
 188          return true;
 189      }
 190  
 191      // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
 192      if (try_no_witness) {
 193          SpanReader ssData{tx_data};
 194          try {
 195              ssData >> TX_NO_WITNESS(tx_legacy);
 196              if (ssData.empty()) ok_legacy = true;
 197          } catch (const std::exception&) {
 198              // Fall through.
 199          }
 200      }
 201  
 202      // If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
 203      // at this point that extended decoding either failed or doesn't pass the sanity check.
 204      if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
 205          tx = std::move(tx_legacy);
 206          return true;
 207      }
 208  
 209      // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
 210      if (ok_extended) {
 211          tx = std::move(tx_extended);
 212          return true;
 213      }
 214  
 215      // If legacy decoding succeeded and extended didn't, return the legacy one.
 216      if (ok_legacy) {
 217          tx = std::move(tx_legacy);
 218          return true;
 219      }
 220  
 221      // If none succeeded, we failed.
 222      return false;
 223  }
 224  
 225  bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
 226  {
 227      if (!IsHex(hex_tx)) {
 228          return false;
 229      }
 230  
 231      std::vector<unsigned char> txData(ParseHex(hex_tx));
 232      return DecodeTx(tx, txData, try_no_witness, try_witness);
 233  }
 234  
 235  bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
 236  {
 237      if (!IsHex(hex_header)) return false;
 238  
 239      const std::vector<unsigned char> header_data{ParseHex(hex_header)};
 240      try {
 241          SpanReader{header_data} >> header;
 242      } catch (const std::exception&) {
 243          return false;
 244      }
 245      return true;
 246  }
 247  
 248  bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
 249  {
 250      if (!IsHex(strHexBlk))
 251          return false;
 252  
 253      std::vector<unsigned char> blockData(ParseHex(strHexBlk));
 254      try {
 255          SpanReader{blockData} >> TX_WITH_WITNESS(block);
 256      }
 257      catch (const std::exception&) {
 258          return false;
 259      }
 260  
 261      return true;
 262  }
 263  
 264  util::Result<int> SighashFromStr(const std::string& sighash)
 265  {
 266      static const std::map<std::string, int> map_sighash_values = {
 267          {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
 268          {std::string("ALL"), int(SIGHASH_ALL)},
 269          {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
 270          {std::string("NONE"), int(SIGHASH_NONE)},
 271          {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
 272          {std::string("SINGLE"), int(SIGHASH_SINGLE)},
 273          {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
 274      };
 275      const auto& it = map_sighash_values.find(sighash);
 276      if (it != map_sighash_values.end()) {
 277          return it->second;
 278      } else {
 279          return util::Error{Untranslated("'" + sighash + "' is not a valid sighash parameter.")};
 280      }
 281  }
 282  
 283  UniValue ValueFromAmount(const CAmount amount)
 284  {
 285      static_assert(COIN > 1);
 286      int64_t quotient = amount / COIN;
 287      int64_t remainder = amount % COIN;
 288      if (amount < 0) {
 289          quotient = -quotient;
 290          remainder = -remainder;
 291      }
 292      return UniValue(UniValue::VNUM,
 293              strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
 294  }
 295  
 296  std::string FormatScript(const CScript& script)
 297  {
 298      std::string ret;
 299      CScript::const_iterator it = script.begin();
 300      opcodetype op;
 301      while (it != script.end()) {
 302          CScript::const_iterator it2 = it;
 303          std::vector<unsigned char> vch;
 304          if (script.GetOp(it, op, vch)) {
 305              if (op == OP_0) {
 306                  ret += "0 ";
 307                  continue;
 308              } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
 309                  ret += strprintf("%i ", op - OP_1NEGATE - 1);
 310                  continue;
 311              } else if (op >= OP_NOP && op <= OP_NOP10) {
 312                  std::string str(GetOpName(op));
 313                  if (str.substr(0, 3) == std::string("OP_")) {
 314                      ret += str.substr(3, std::string::npos) + " ";
 315                      continue;
 316                  }
 317              }
 318              if (vch.size() > 0) {
 319                  ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
 320                                                 HexStr(std::vector<uint8_t>(it - vch.size(), it)));
 321              } else {
 322                  ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
 323              }
 324              continue;
 325          }
 326          ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
 327          break;
 328      }
 329      return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
 330  }
 331  
 332  const std::map<unsigned char, std::string> mapSigHashTypes = {
 333      {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
 334      {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
 335      {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
 336      {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
 337      {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
 338      {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
 339  };
 340  
 341  std::string SighashToStr(unsigned char sighash_type)
 342  {
 343      const auto& it = mapSigHashTypes.find(sighash_type);
 344      if (it == mapSigHashTypes.end()) return "";
 345      return it->second;
 346  }
 347  
 348  /**
 349   * Create the assembly string representation of a CScript object.
 350   * @param[in] script    CScript object to convert into the asm string representation.
 351   * @param[in] fAttemptSighashDecode    Whether to attempt to decode sighash types on data within the script that matches the format
 352   *                                     of a signature. Only pass true for scripts you believe could contain signatures. For example,
 353   *                                     pass false, or omit the this argument (defaults to false), for scriptPubKeys.
 354   */
 355  std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
 356  {
 357      std::string str;
 358      opcodetype opcode;
 359      std::vector<unsigned char> vch;
 360      CScript::const_iterator pc = script.begin();
 361      while (pc < script.end()) {
 362          if (!str.empty()) {
 363              str += " ";
 364          }
 365          if (!script.GetOp(pc, opcode, vch)) {
 366              str += "[error]";
 367              return str;
 368          }
 369          if (0 <= opcode && opcode <= OP_PUSHDATA4) {
 370              if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
 371                  str += strprintf("%d", CScriptNum(vch, false).getint());
 372              } else {
 373                  // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
 374                  if (fAttemptSighashDecode && !script.IsUnspendable()) {
 375                      std::string strSigHashDecode;
 376                      // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
 377                      // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
 378                      // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
 379                      // checks in CheckSignatureEncoding.
 380                      if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
 381                          const unsigned char chSigHashType = vch.back();
 382                          const auto it = mapSigHashTypes.find(chSigHashType);
 383                          if (it != mapSigHashTypes.end()) {
 384                              strSigHashDecode = "[" + it->second + "]";
 385                              vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
 386                          }
 387                      }
 388                      str += HexStr(vch) + strSigHashDecode;
 389                  } else {
 390                      str += HexStr(vch);
 391                  }
 392              }
 393          } else {
 394              str += GetOpName(opcode);
 395          }
 396      }
 397      return str;
 398  }
 399  
 400  std::string EncodeHexTx(const CTransaction& tx)
 401  {
 402      DataStream ssTx;
 403      ssTx << TX_WITH_WITNESS(tx);
 404      return HexStr(ssTx);
 405  }
 406  
 407  void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
 408  {
 409      CTxDestination address;
 410  
 411      out.pushKV("asm", ScriptToAsmStr(script));
 412      if (include_address) {
 413          out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
 414      }
 415      if (include_hex) {
 416          out.pushKV("hex", HexStr(script));
 417      }
 418  
 419      std::vector<std::vector<unsigned char>> solns;
 420      const TxoutType type{Solver(script, solns)};
 421  
 422      if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
 423          out.pushKV("address", EncodeDestination(address));
 424      }
 425      out.pushKV("type", GetTxnOutputType(type));
 426  }
 427  
 428  void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, const CTxUndo* txundo, TxVerbosity verbosity, std::function<bool(const CTxOut&)> is_change_func)
 429  {
 430      CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
 431  
 432      entry.pushKV("txid", tx.GetHash().GetHex());
 433      entry.pushKV("hash", tx.GetWitnessHash().GetHex());
 434      entry.pushKV("version", tx.version);
 435      entry.pushKV("size", tx.ComputeTotalSize());
 436      entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
 437      entry.pushKV("weight", GetTransactionWeight(tx));
 438      entry.pushKV("locktime", tx.nLockTime);
 439  
 440      UniValue vin{UniValue::VARR};
 441      vin.reserve(tx.vin.size());
 442  
 443      // If available, use Undo data to calculate the fee. Note that txundo == nullptr
 444      // for coinbase transactions and for transactions where undo data is unavailable.
 445      const bool have_undo = txundo != nullptr;
 446      CAmount amt_total_in = 0;
 447      CAmount amt_total_out = 0;
 448  
 449      for (unsigned int i = 0; i < tx.vin.size(); i++) {
 450          const CTxIn& txin = tx.vin[i];
 451          UniValue in(UniValue::VOBJ);
 452          if (tx.IsCoinBase()) {
 453              in.pushKV("coinbase", HexStr(txin.scriptSig));
 454          } else {
 455              in.pushKV("txid", txin.prevout.hash.GetHex());
 456              in.pushKV("vout", txin.prevout.n);
 457              UniValue o(UniValue::VOBJ);
 458              o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
 459              o.pushKV("hex", HexStr(txin.scriptSig));
 460              in.pushKV("scriptSig", std::move(o));
 461          }
 462          if (!tx.vin[i].scriptWitness.IsNull()) {
 463              UniValue txinwitness(UniValue::VARR);
 464              txinwitness.reserve(tx.vin[i].scriptWitness.stack.size());
 465              for (const auto& item : tx.vin[i].scriptWitness.stack) {
 466                  txinwitness.push_back(HexStr(item));
 467              }
 468              in.pushKV("txinwitness", std::move(txinwitness));
 469          }
 470          if (have_undo) {
 471              const Coin& prev_coin = txundo->vprevout[i];
 472              const CTxOut& prev_txout = prev_coin.out;
 473  
 474              amt_total_in += prev_txout.nValue;
 475  
 476              if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
 477                  UniValue o_script_pub_key(UniValue::VOBJ);
 478                  ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
 479  
 480                  UniValue p(UniValue::VOBJ);
 481                  p.pushKV("generated", prev_coin.IsCoinBase());
 482                  p.pushKV("height", prev_coin.nHeight);
 483                  p.pushKV("value", ValueFromAmount(prev_txout.nValue));
 484                  p.pushKV("scriptPubKey", std::move(o_script_pub_key));
 485                  in.pushKV("prevout", std::move(p));
 486              }
 487          }
 488          in.pushKV("sequence", txin.nSequence);
 489          vin.push_back(std::move(in));
 490      }
 491      entry.pushKV("vin", std::move(vin));
 492  
 493      UniValue vout(UniValue::VARR);
 494      vout.reserve(tx.vout.size());
 495      for (unsigned int i = 0; i < tx.vout.size(); i++) {
 496          const CTxOut& txout = tx.vout[i];
 497  
 498          UniValue out(UniValue::VOBJ);
 499  
 500          out.pushKV("value", ValueFromAmount(txout.nValue));
 501          out.pushKV("n", i);
 502  
 503          UniValue o(UniValue::VOBJ);
 504          ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
 505          out.pushKV("scriptPubKey", std::move(o));
 506  
 507          if (is_change_func && is_change_func(txout)) {
 508              out.pushKV("ischange", true);
 509          }
 510  
 511          vout.push_back(std::move(out));
 512  
 513          if (have_undo) {
 514              amt_total_out += txout.nValue;
 515          }
 516      }
 517      entry.pushKV("vout", std::move(vout));
 518  
 519      if (have_undo) {
 520          const CAmount fee = amt_total_in - amt_total_out;
 521          CHECK_NONFATAL(MoneyRange(fee));
 522          entry.pushKV("fee", ValueFromAmount(fee));
 523      }
 524  
 525      if (!block_hash.IsNull()) {
 526          entry.pushKV("blockhash", block_hash.GetHex());
 527      }
 528  
 529      if (include_hex) {
 530          entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
 531      }
 532  }
 533