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