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