Branch data Line data Source code
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 : 1 : OpCodeParser()
60 : 1 : {
61 [ + + ]: 187 : for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
62 : : // Allow OP_RESERVED to get into mapOpNames
63 [ + + ]: 186 : if (op < OP_NOP && op != OP_RESERVED) {
64 : 96 : continue;
65 : : }
66 : :
67 [ + - ]: 90 : std::string strName = GetOpName(static_cast<opcodetype>(op));
68 [ - + ]: 90 : if (strName == "OP_UNKNOWN") {
69 : 0 : continue;
70 : : }
71 [ + - ]: 90 : mapOpNames[strName] = static_cast<opcodetype>(op);
72 : : // Convenience: OP_ADD and just ADD are both recognized:
73 [ - + + - ]: 90 : if (strName.starts_with("OP_")) {
74 [ + - + - ]: 90 : mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
75 : : }
76 : 90 : }
77 : 1 : }
78 : 6239 : opcodetype Parse(const std::string& s) const
79 : : {
80 : 6239 : auto it = mapOpNames.find(s);
81 [ + + + - ]: 6239 : if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
82 : 6035 : return it->second;
83 : : }
84 : : };
85 : :
86 : 6239 : opcodetype ParseOpCode(const std::string& s)
87 : : {
88 [ + + + - : 6241 : static const OpCodeParser ocp;
+ - ]
89 : 6239 : return ocp.Parse(s);
90 : : }
91 : :
92 : : } // namespace
93 : :
94 : 371 : CScript ParseScript(const std::string& s)
95 : : {
96 : 371 : CScript result;
97 : :
98 [ - + + - ]: 371 : std::vector<std::string> words = SplitString(s, " \t\n");
99 : :
100 [ + + ]: 440365 : for (const std::string& w : words) {
101 [ + + ]: 440243 : if (w.empty()) {
102 : : // Empty string, ignore. (SplitString doesn't combine multiple separators)
103 [ - + + - : 381090 : } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
+ + ]
104 [ + + + + : 185666 : (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
+ - + + ]
105 : : {
106 : : // Number
107 [ - + ]: 251591 : 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 [ + + + + : 251591 : if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
+ + ]
112 : 45 : throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
113 [ + - ]: 45 : "range -0xFFFFFFFF...0xFFFFFFFF");
114 : : }
115 : :
116 [ + - ]: 439994 : result << num.value();
117 [ - + + + : 166441 : } 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 [ - + + - : 24560 : std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
- + + - ]
120 : 12280 : result.insert(result.end(), raw.begin(), raw.end());
121 [ - + + + : 129499 : } 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 [ + - ]: 110980 : std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
125 [ - + ]: 110980 : result << value;
126 : 110980 : } else {
127 : : // opcode, e.g. OP_ADD or ADD:
128 [ + + + - ]: 6239 : result << ParseOpCode(w);
129 : : }
130 : : }
131 : :
132 : 122 : return result;
133 : 371 : }
134 : :
135 : : /// Check that all of the input and output scripts of a transaction contain valid opcodes
136 : 8910 : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
137 : : {
138 : : // Check input scripts for non-coinbase txs
139 [ + + ]: 17820 : if (!CTransaction(tx).IsCoinBase()) {
140 [ - + + + ]: 35160 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
141 [ + + + + : 30017 : 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 [ - + + + ]: 262165 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
148 [ + + + + : 257825 : 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 : 9461 : 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 [ + - ]: 9461 : CMutableTransaction tx_extended, tx_legacy;
172 : 9461 : 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 [ + + ]: 9461 : if (try_witness) {
177 [ - + ]: 8157 : SpanReader ssData{tx_data};
178 : 8157 : try {
179 [ + + ]: 8157 : ssData >> TX_WITH_WITNESS(tx_extended);
180 [ + + ]: 7879 : if (ssData.empty()) ok_extended = true;
181 [ - + ]: 278 : } catch (const std::exception&) {
182 : : // Fall through.
183 : 278 : }
184 : : }
185 : :
186 : : // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
187 : : // don't bother decoding the other way.
188 [ + - + + ]: 8131 : if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
189 : 4109 : tx = std::move(tx_extended);
190 : 4109 : return true;
191 : : }
192 : :
193 : : // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
194 [ + + ]: 5352 : if (try_no_witness) {
195 [ - + ]: 1577 : SpanReader ssData{tx_data};
196 : 1577 : try {
197 [ + + ]: 1577 : ssData >> TX_NO_WITNESS(tx_legacy);
198 [ + + ]: 1360 : if (ssData.empty()) ok_legacy = true;
199 [ - + ]: 217 : } catch (const std::exception&) {
200 : : // Fall through.
201 : 217 : }
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 [ + - + + ]: 1274 : if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
207 : 231 : tx = std::move(tx_legacy);
208 : 231 : return true;
209 : : }
210 : :
211 : : // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
212 [ + + ]: 5121 : if (ok_extended) {
213 : 3718 : tx = std::move(tx_extended);
214 : 3718 : return true;
215 : : }
216 : :
217 : : // If legacy decoding succeeded and extended didn't, return the legacy one.
218 [ + + ]: 1403 : if (ok_legacy) {
219 : 251 : tx = std::move(tx_legacy);
220 : 251 : return true;
221 : : }
222 : :
223 : : // If none succeeded, we failed.
224 : : return false;
225 : 18922 : }
226 : :
227 : 9547 : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
228 : : {
229 [ - + + + ]: 9547 : if (!IsHex(hex_tx)) {
230 : : return false;
231 : : }
232 : :
233 [ - + ]: 9461 : std::vector<unsigned char> txData(ParseHex(hex_tx));
234 [ + - ]: 9461 : return DecodeTx(tx, txData, try_no_witness, try_witness);
235 : 9461 : }
236 : :
237 : 602 : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
238 : : {
239 [ - + + + ]: 602 : if (!IsHex(hex_header)) return false;
240 : :
241 [ - + ]: 524 : const std::vector<unsigned char> header_data{ParseHex(hex_header)};
242 : 524 : try {
243 [ - + + + ]: 1048 : SpanReader{header_data} >> header;
244 [ - + ]: 53 : } catch (const std::exception&) {
245 : 53 : return false;
246 : 53 : }
247 : : return true;
248 : 524 : }
249 : :
250 : 562 : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
251 : : {
252 [ - + + + ]: 562 : if (!IsHex(strHexBlk))
253 : : return false;
254 : :
255 [ - + ]: 479 : std::vector<unsigned char> blockData(ParseHex(strHexBlk));
256 : 479 : try {
257 [ - + + + ]: 958 : SpanReader{blockData} >> TX_WITH_WITNESS(block);
258 : : }
259 [ - + ]: 352 : catch (const std::exception&) {
260 : 352 : return false;
261 : 352 : }
262 : :
263 : : return true;
264 : 479 : }
265 : :
266 : 3653 : util::Result<int> SighashFromStr(const std::string& sighash)
267 : : {
268 : 3653 : static const std::map<std::string, int> map_sighash_values = {
269 [ + - ]: 4 : {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
270 : 4 : {std::string("ALL"), int(SIGHASH_ALL)},
271 : 4 : {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
272 : 4 : {std::string("NONE"), int(SIGHASH_NONE)},
273 : 4 : {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
274 : 4 : {std::string("SINGLE"), int(SIGHASH_SINGLE)},
275 : 4 : {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
276 [ + + + - : 3669 : };
+ + - - ]
277 : 3653 : const auto& it = map_sighash_values.find(sighash);
278 [ + + ]: 3653 : if (it != map_sighash_values.end()) {
279 : 6 : return it->second;
280 : : } else {
281 [ + - + - ]: 14588 : return util::Error{Untranslated("'" + sighash + "' is not a valid sighash parameter.")};
282 : : }
283 [ + - + - : 14 : }
+ - + - +
- + - - +
- - ]
284 : :
285 : 1664755 : UniValue ValueFromAmount(const CAmount amount)
286 : : {
287 : 1664755 : static_assert(COIN > 1);
288 : 1664755 : int64_t quotient = amount / COIN;
289 : 1664755 : int64_t remainder = amount % COIN;
290 [ + + ]: 1664755 : if (amount < 0) {
291 : 113221 : quotient = -quotient;
292 : 113221 : remainder = -remainder;
293 : : }
294 : 113221 : return UniValue(UniValue::VNUM,
295 : 1664755 : strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
296 : : }
297 : :
298 : 2630 : std::string FormatScript(const CScript& script)
299 : : {
300 [ + + ]: 2630 : std::string ret;
301 [ + + ]: 5260 : CScript::const_iterator it = script.begin();
302 : : opcodetype op;
303 [ + + ]: 2152094 : while (it != script.end()) {
304 : 2149829 : CScript::const_iterator it2 = it;
305 : 2149829 : std::vector<unsigned char> vch;
306 [ + - + + ]: 2149829 : if (script.GetOp(it, op, vch)) {
307 [ + + ]: 2149464 : if (op == OP_0) {
308 [ + - ]: 209753 : ret += "0 ";
309 : 209753 : continue;
310 [ + + + + ]: 1939711 : } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
311 [ + - ]: 152394 : ret += strprintf("%i ", op - OP_1NEGATE - 1);
312 : 76197 : continue;
313 [ + + ]: 1863514 : } else if (op >= OP_NOP && op <= OP_NOP10) {
314 [ + - ]: 1178919 : std::string str(GetOpName(op));
315 [ + - + - : 1178919 : if (str.substr(0, 3) == std::string("OP_")) {
+ - ]
316 [ + - - + ]: 3536757 : ret += str.substr(3, std::string::npos) + " ";
317 : 1178919 : continue;
318 : : }
319 : 1178919 : }
320 [ - + + + ]: 684595 : if (vch.size() > 0) {
321 [ + - + - : 840867 : ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
+ - ]
322 [ + - + - ]: 1121156 : HexStr(std::vector<uint8_t>(it - vch.size(), it)));
323 : : } else {
324 [ + - + - : 1212918 : ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
+ - ]
325 : : }
326 : 684595 : continue;
327 : 684595 : }
328 [ + - + - : 1095 : ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
+ - ]
329 : 365 : break;
330 : 2149829 : }
331 [ + + + - ]: 5235 : return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
332 : 2630 : }
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 : 476 : std::string SighashToStr(unsigned char sighash_type)
344 : : {
345 : 476 : const auto& it = mapSigHashTypes.find(sighash_type);
346 [ + + ]: 476 : if (it == mapSigHashTypes.end()) return "";
347 [ - + ]: 34 : 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 : 1850692 : std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
358 : : {
359 [ + + ]: 1850692 : std::string str;
360 : 1850692 : opcodetype opcode;
361 : 1850692 : std::vector<unsigned char> vch;
362 [ + + ]: 3701384 : CScript::const_iterator pc = script.begin();
363 [ + + + + ]: 43709106 : while (pc < script.end()) {
364 [ + + ]: 20933214 : if (!str.empty()) {
365 [ + - ]: 19462512 : str += " ";
366 : : }
367 [ + - + + ]: 20933214 : if (!script.GetOp(pc, opcode, vch)) {
368 [ + - ]: 1850692 : str += "[error]";
369 : : return str;
370 : : }
371 [ + + ]: 20003861 : if (0 <= opcode && opcode <= OP_PUSHDATA4) {
372 [ - + + + ]: 6784216 : if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
373 [ + - + - ]: 16330758 : 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 [ + + + + ]: 1340630 : if (fAttemptSighashDecode && !script.IsUnspendable()) {
377 [ + - ]: 128526 : 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 [ + - + + ]: 128526 : if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
383 : 7130 : const unsigned char chSigHashType = vch.back();
384 : 7130 : const auto it = mapSigHashTypes.find(chSigHashType);
385 [ + - ]: 7130 : if (it != mapSigHashTypes.end()) {
386 [ + - ]: 14260 : strSigHashDecode = "[" + it->second + "]";
387 : 7130 : vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
388 : : }
389 : : }
390 [ - + + - : 257052 : str += HexStr(vch) + strSigHashDecode;
+ - ]
391 : 128526 : } else {
392 [ + - ]: 2424208 : str += HexStr(vch);
393 : : }
394 : : }
395 : : } else {
396 [ + - ]: 26439290 : str += GetOpName(opcode);
397 : : }
398 : : }
399 : : return str;
400 : 1850692 : }
401 : :
402 : 4823 : std::string EncodeHexTx(const CTransaction& tx)
403 : : {
404 : 4823 : DataStream ssTx;
405 [ + - ]: 4823 : ssTx << TX_WITH_WITNESS(tx);
406 [ - + + - ]: 4823 : return HexStr(ssTx);
407 : 4823 : }
408 : :
409 : 1668164 : void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
410 : : {
411 : 1668164 : CTxDestination address;
412 : :
413 [ + - + - : 3336328 : out.pushKV("asm", ScriptToAsmStr(script));
+ - + - ]
414 [ + + ]: 1668164 : if (include_address) {
415 [ + + + - : 3334068 : out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
+ - + - +
- + - ]
416 : : }
417 [ + + ]: 1668164 : if (include_hex) {
418 [ + + + - : 4995945 : out.pushKV("hex", HexStr(script));
+ - + - +
- ]
419 : : }
420 : :
421 : 1668164 : std::vector<std::vector<unsigned char>> solns;
422 [ + - ]: 1668164 : const TxoutType type{Solver(script, solns)};
423 : :
424 [ + + + - : 1668164 : if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
+ + + - ]
425 [ + - + - : 747924 : out.pushKV("address", EncodeDestination(address));
+ - + - ]
426 : : }
427 [ + - + - : 3336328 : out.pushKV("type", GetTxnOutputType(type));
+ - + - ]
428 : 1668164 : }
429 : :
430 : 4097 : 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 : 4097 : CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
433 : :
434 [ + - + - : 8194 : entry.pushKV("txid", tx.GetHash().GetHex());
+ - ]
435 [ + - + - : 8194 : entry.pushKV("hash", tx.GetWitnessHash().GetHex());
+ - ]
436 [ + - + - ]: 8194 : entry.pushKV("version", tx.version);
437 [ + - + - ]: 8194 : entry.pushKV("size", tx.ComputeTotalSize());
438 [ + - + - ]: 8194 : entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
439 [ + - + - ]: 8194 : entry.pushKV("weight", GetTransactionWeight(tx));
440 [ + - + - ]: 8194 : entry.pushKV("locktime", (int64_t)tx.nLockTime);
441 : :
442 : 4097 : UniValue vin{UniValue::VARR};
443 [ - + + - ]: 4097 : 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 : 184001 : CAmount amt_total_out = 0;
450 : :
451 [ - + + + ]: 184001 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
452 : 179904 : const CTxIn& txin = tx.vin[i];
453 : 179904 : UniValue in(UniValue::VOBJ);
454 [ + + ]: 179904 : if (tx.IsCoinBase()) {
455 [ + + + - : 90 : in.pushKV("coinbase", HexStr(txin.scriptSig));
+ - + - +
- ]
456 : : } else {
457 [ + - + - : 359748 : in.pushKV("txid", txin.prevout.hash.GetHex());
+ - + - ]
458 [ + - + - : 359748 : in.pushKV("vout", (int64_t)txin.prevout.n);
+ - ]
459 : 179874 : UniValue o(UniValue::VOBJ);
460 [ + - + - : 359748 : o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
+ - + - ]
461 [ + + + - : 539622 : o.pushKV("hex", HexStr(txin.scriptSig));
+ - + - +
- ]
462 [ + - + - ]: 359748 : in.pushKV("scriptSig", std::move(o));
463 : 179874 : }
464 [ + + ]: 179904 : if (!tx.vin[i].scriptWitness.IsNull()) {
465 : 14818 : UniValue txinwitness(UniValue::VARR);
466 [ - + + - ]: 14818 : txinwitness.reserve(tx.vin[i].scriptWitness.stack.size());
467 [ + + ]: 687599 : for (const auto& item : tx.vin[i].scriptWitness.stack) {
468 [ - + + - : 672781 : txinwitness.push_back(HexStr(item));
+ - + - ]
469 : : }
470 [ + - + - ]: 29636 : in.pushKV("txinwitness", std::move(txinwitness));
471 : 14818 : }
472 [ - + ]: 179904 : if (have_undo) {
473 [ # # ]: 0 : const Coin& prev_coin = txundo->vprevout[i];
474 : 0 : const CTxOut& prev_txout = prev_coin.out;
475 : :
476 : 0 : amt_total_in += prev_txout.nValue;
477 : :
478 [ # # ]: 0 : if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
479 : 0 : UniValue o_script_pub_key(UniValue::VOBJ);
480 [ # # ]: 0 : ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
481 : :
482 : 0 : UniValue p(UniValue::VOBJ);
483 [ # # # # : 0 : p.pushKV("generated", bool(prev_coin.fCoinBase));
# # ]
484 [ # # # # : 0 : p.pushKV("height", uint64_t(prev_coin.nHeight));
# # ]
485 [ # # # # : 0 : p.pushKV("value", ValueFromAmount(prev_txout.nValue));
# # ]
486 [ # # # # ]: 0 : p.pushKV("scriptPubKey", std::move(o_script_pub_key));
487 [ # # # # ]: 0 : in.pushKV("prevout", std::move(p));
488 : 0 : }
489 : : }
490 [ + - + - : 359808 : in.pushKV("sequence", (int64_t)txin.nSequence);
+ - ]
491 [ + - ]: 179904 : vin.push_back(std::move(in));
492 : 179904 : }
493 [ + - + - ]: 8194 : entry.pushKV("vin", std::move(vin));
494 : :
495 : 4097 : UniValue vout(UniValue::VARR);
496 [ - + + - ]: 4097 : vout.reserve(tx.vout.size());
497 [ - + + + ]: 1666868 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
498 : 1662771 : const CTxOut& txout = tx.vout[i];
499 : :
500 : 1662771 : UniValue out(UniValue::VOBJ);
501 : :
502 [ + - + - : 3325542 : out.pushKV("value", ValueFromAmount(txout.nValue));
+ - ]
503 [ + - + - : 3325542 : out.pushKV("n", (int64_t)i);
+ - ]
504 : :
505 : 1662771 : UniValue o(UniValue::VOBJ);
506 [ + - ]: 1662771 : ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
507 [ + - + - ]: 3325542 : out.pushKV("scriptPubKey", std::move(o));
508 : :
509 [ - + - - : 1662771 : if (is_change_func && is_change_func(txout)) {
- - ]
510 [ # # # # : 0 : out.pushKV("ischange", true);
# # ]
511 : : }
512 : :
513 [ + - ]: 1662771 : vout.push_back(std::move(out));
514 : :
515 [ - + ]: 1662771 : if (have_undo) {
516 : 0 : amt_total_out += txout.nValue;
517 : : }
518 : 1662771 : }
519 [ + - + - ]: 8194 : entry.pushKV("vout", std::move(vout));
520 : :
521 [ - + ]: 4097 : if (have_undo) {
522 : 0 : const CAmount fee = amt_total_in - amt_total_out;
523 [ # # ]: 0 : CHECK_NONFATAL(MoneyRange(fee));
524 [ # # # # : 0 : entry.pushKV("fee", ValueFromAmount(fee));
# # ]
525 : : }
526 : :
527 [ + + ]: 8194 : if (!block_hash.IsNull()) {
528 [ + - + - : 2956 : entry.pushKV("blockhash", block_hash.GetHex());
+ - + - ]
529 : : }
530 : :
531 [ + + ]: 4097 : if (include_hex) {
532 [ + - + - : 5918 : entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
+ - + - ]
533 : : }
534 : 4097 : }
|