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 : 12 : OpCodeParser()
60 : 12 : {
61 [ + + ]: 2244 : for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
62 : : // Allow OP_RESERVED to get into mapOpNames
63 [ + + ]: 2232 : if (op < OP_NOP && op != OP_RESERVED) {
64 : 1152 : continue;
65 : : }
66 : :
67 [ + - ]: 1080 : std::string strName = GetOpName(static_cast<opcodetype>(op));
68 [ - + ]: 1080 : if (strName == "OP_UNKNOWN") {
69 : 0 : continue;
70 : : }
71 [ + - ]: 1080 : mapOpNames[strName] = static_cast<opcodetype>(op);
72 : : // Convenience: OP_ADD and just ADD are both recognized:
73 [ - + + - ]: 1080 : if (strName.starts_with("OP_")) {
74 [ + - + - ]: 1080 : mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
75 : : }
76 : 1080 : }
77 : 12 : }
78 : 3921 : opcodetype Parse(const std::string& s) const
79 : : {
80 : 3921 : auto it = mapOpNames.find(s);
81 [ + + + - ]: 3921 : if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
82 : 3919 : return it->second;
83 : : }
84 : : };
85 : :
86 : 3921 : opcodetype ParseOpCode(const std::string& s)
87 : : {
88 [ + + + - : 3938 : static const OpCodeParser ocp;
+ - ]
89 : 3921 : return ocp.Parse(s);
90 : : }
91 : :
92 : : } // namespace
93 : :
94 : 2766 : CScript ParseScript(const std::string& s)
95 : : {
96 : 2766 : CScript result;
97 : :
98 [ - + + - ]: 2766 : std::vector<std::string> words = SplitString(s, " \t\n");
99 : :
100 [ + + ]: 14974 : for (const std::string& w : words) {
101 [ + + ]: 12217 : if (w.empty()) {
102 : : // Empty string, ignore. (SplitString doesn't combine multiple separators)
103 [ - + + - : 11993 : } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
+ + ]
104 [ + + + - : 7552 : (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
+ - + - ]
105 : : {
106 : : // Number
107 [ - + ]: 4607 : 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 [ + + + + : 4607 : if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
+ + ]
112 : 7 : throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
113 [ + - ]: 7 : "range -0xFFFFFFFF...0xFFFFFFFF");
114 : : }
115 : :
116 [ + - ]: 12208 : result << num.value();
117 [ - + + + : 14043 : } 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 [ - + + - : 4438 : std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
- + + - ]
120 : 2219 : result.insert(result.end(), raw.begin(), raw.end());
121 [ - + + - : 7386 : } 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 [ + - ]: 1246 : std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
125 [ - + ]: 1246 : result << value;
126 : 1246 : } else {
127 : : // opcode, e.g. OP_ADD or ADD:
128 [ + + + - ]: 3921 : result << ParseOpCode(w);
129 : : }
130 : : }
131 : :
132 : 2757 : return result;
133 : 2766 : }
134 : :
135 : : /// Check that all of the input and output scripts of a transaction contain valid opcodes
136 : 29987 : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
137 : : {
138 : : // Check input scripts for non-coinbase txs
139 [ + + ]: 59974 : if (!CTransaction(tx).IsCoinBase()) {
140 [ - + + + ]: 101407 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
141 [ + + + + : 71424 : 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 [ - + + + ]: 126782 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
148 [ + + + + : 98454 : 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 : 30017 : 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 [ + - ]: 30017 : CMutableTransaction tx_extended, tx_legacy;
172 : 30017 : 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 [ + + ]: 30017 : if (try_witness) {
177 [ - + + - ]: 30010 : DataStream ssData(tx_data);
178 : 30010 : try {
179 [ + + ]: 30010 : ssData >> TX_WITH_WITNESS(tx_extended);
180 [ - + + + ]: 29815 : if (ssData.empty()) ok_extended = true;
181 [ - + ]: 195 : } catch (const std::exception&) {
182 : : // Fall through.
183 : 195 : }
184 : 0 : }
185 : :
186 : : // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
187 : : // don't bother decoding the other way.
188 [ + + + - : 30010 : if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
+ + ]
189 : 28135 : tx = std::move(tx_extended);
190 : 28135 : return true;
191 : : }
192 : :
193 : : // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
194 [ + + ]: 1882 : if (try_no_witness) {
195 [ - + + - ]: 218 : DataStream ssData(tx_data);
196 : 218 : try {
197 [ + + ]: 218 : ssData >> TX_NO_WITNESS(tx_legacy);
198 [ - + + + ]: 216 : if (ssData.empty()) ok_legacy = true;
199 [ - + ]: 2 : } catch (const std::exception&) {
200 : : // Fall through.
201 : 2 : }
202 : 0 : }
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 [ + + + - : 218 : if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
+ + ]
207 : 193 : tx = std::move(tx_legacy);
208 : 193 : return true;
209 : : }
210 : :
211 : : // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
212 [ + + ]: 1689 : if (ok_extended) {
213 : 1658 : tx = std::move(tx_extended);
214 : 1658 : return true;
215 : : }
216 : :
217 : : // If legacy decoding succeeded and extended didn't, return the legacy one.
218 [ + + ]: 31 : if (ok_legacy) {
219 : 1 : tx = std::move(tx_legacy);
220 : 1 : return true;
221 : : }
222 : :
223 : : // If none succeeded, we failed.
224 : : return false;
225 : 60034 : }
226 : :
227 : 30021 : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
228 : : {
229 [ - + + + ]: 30021 : if (!IsHex(hex_tx)) {
230 : : return false;
231 : : }
232 : :
233 [ - + ]: 30017 : std::vector<unsigned char> txData(ParseHex(hex_tx));
234 [ + - ]: 30017 : return DecodeTx(tx, txData, try_no_witness, try_witness);
235 : 30017 : }
236 : :
237 : 1947 : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
238 : : {
239 [ - + + + ]: 1947 : if (!IsHex(hex_header)) return false;
240 : :
241 [ - + ]: 1946 : const std::vector<unsigned char> header_data{ParseHex(hex_header)};
242 [ - + + - ]: 1946 : DataStream ser_header{header_data};
243 : 1946 : try {
244 [ + + ]: 3892 : ser_header >> header;
245 [ - + ]: 2 : } catch (const std::exception&) {
246 : 2 : return false;
247 : 2 : }
248 : : return true;
249 : 1946 : }
250 : :
251 : 27651 : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
252 : : {
253 [ - + + - ]: 27651 : if (!IsHex(strHexBlk))
254 : : return false;
255 : :
256 [ - + ]: 27651 : std::vector<unsigned char> blockData(ParseHex(strHexBlk));
257 [ - + + - ]: 27651 : DataStream ssBlock(blockData);
258 : 27651 : try {
259 [ + + ]: 55302 : ssBlock >> TX_WITH_WITNESS(block);
260 : : }
261 [ - + ]: 3 : catch (const std::exception&) {
262 : 3 : return false;
263 : 3 : }
264 : :
265 : : return true;
266 : 27651 : }
267 : :
268 : 79 : util::Result<int> SighashFromStr(const std::string& sighash)
269 : : {
270 : 79 : static const std::map<std::string, int> map_sighash_values = {
271 [ + - ]: 34 : {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
272 : 34 : {std::string("ALL"), int(SIGHASH_ALL)},
273 : 34 : {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
274 : 34 : {std::string("NONE"), int(SIGHASH_NONE)},
275 : 34 : {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
276 : 34 : {std::string("SINGLE"), int(SIGHASH_SINGLE)},
277 : 34 : {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
278 [ + + + - : 215 : };
+ + - - ]
279 : 79 : const auto& it = map_sighash_values.find(sighash);
280 [ + + ]: 79 : if (it != map_sighash_values.end()) {
281 : 75 : return it->second;
282 : : } else {
283 [ + - + - ]: 16 : return util::Error{Untranslated("'" + sighash + "' is not a valid sighash parameter.")};
284 : : }
285 [ + - + - : 119 : }
+ - + - +
- + - - +
- - ]
286 : :
287 : 297198 : UniValue ValueFromAmount(const CAmount amount)
288 : : {
289 : 297198 : static_assert(COIN > 1);
290 : 297198 : int64_t quotient = amount / COIN;
291 : 297198 : int64_t remainder = amount % COIN;
292 [ + + ]: 297198 : if (amount < 0) {
293 : 2156 : quotient = -quotient;
294 : 2156 : remainder = -remainder;
295 : : }
296 : 2156 : return UniValue(UniValue::VNUM,
297 : 297198 : strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
298 : : }
299 : :
300 : 268 : std::string FormatScript(const CScript& script)
301 : : {
302 [ + + ]: 268 : std::string ret;
303 [ + + ]: 536 : CScript::const_iterator it = script.begin();
304 : : opcodetype op;
305 [ + + ]: 867 : while (it != script.end()) {
306 : 599 : CScript::const_iterator it2 = it;
307 : 599 : std::vector<unsigned char> vch;
308 [ + - + - ]: 599 : if (script.GetOp(it, op, vch)) {
309 [ + + ]: 599 : if (op == OP_0) {
310 [ + - ]: 63 : ret += "0 ";
311 : 63 : continue;
312 [ + + - + ]: 536 : } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
313 [ + - ]: 126 : ret += strprintf("%i ", op - OP_1NEGATE - 1);
314 : 63 : continue;
315 [ + + ]: 473 : } else if (op >= OP_NOP && op <= OP_NOP10) {
316 [ + - ]: 178 : std::string str(GetOpName(op));
317 [ + - + - : 178 : if (str.substr(0, 3) == std::string("OP_")) {
+ - ]
318 [ + - - + ]: 534 : ret += str.substr(3, std::string::npos) + " ";
319 : 178 : continue;
320 : : }
321 : 178 : }
322 [ - + + - ]: 295 : if (vch.size() > 0) {
323 [ + - + - : 885 : ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
+ - ]
324 [ + - + - ]: 1180 : HexStr(std::vector<uint8_t>(it - vch.size(), it)));
325 : : } else {
326 [ # # # # : 0 : ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
# # ]
327 : : }
328 : 295 : continue;
329 : 295 : }
330 [ # # # # : 0 : ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
# # ]
331 : 0 : break;
332 : 599 : }
333 [ + + + - ]: 509 : return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
334 : 268 : }
335 : :
336 : : const std::map<unsigned char, std::string> mapSigHashTypes = {
337 : : {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
338 : : {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
339 : : {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
340 : : {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
341 : : {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
342 : : {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
343 : : };
344 : :
345 : 7 : std::string SighashToStr(unsigned char sighash_type)
346 : : {
347 : 7 : const auto& it = mapSigHashTypes.find(sighash_type);
348 [ - + ]: 7 : if (it == mapSigHashTypes.end()) return "";
349 [ - + ]: 7 : return it->second;
350 : : }
351 : :
352 : : /**
353 : : * Create the assembly string representation of a CScript object.
354 : : * @param[in] script CScript object to convert into the asm string representation.
355 : : * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format
356 : : * of a signature. Only pass true for scripts you believe could contain signatures. For example,
357 : : * pass false, or omit the this argument (defaults to false), for scriptPubKeys.
358 : : */
359 : 32077 : std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
360 : : {
361 [ + + ]: 32077 : std::string str;
362 : 32077 : opcodetype opcode;
363 : 32077 : std::vector<unsigned char> vch;
364 [ + + ]: 64154 : CScript::const_iterator pc = script.begin();
365 [ + + + + ]: 43107250 : while (pc < script.end()) {
366 [ + + ]: 21521551 : if (!str.empty()) {
367 [ + - ]: 21502071 : str += " ";
368 : : }
369 [ + - + + ]: 21521551 : if (!script.GetOp(pc, opcode, vch)) {
370 [ + - ]: 32077 : str += "[error]";
371 : : return str;
372 : : }
373 [ + + ]: 21521548 : if (0 <= opcode && opcode <= OP_PUSHDATA4) {
374 [ - + + + ]: 21966 : if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
375 [ + - + - ]: 5019 : str += strprintf("%d", CScriptNum(vch, false).getint());
376 : : } else {
377 : : // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
378 [ + + + + ]: 20293 : if (fAttemptSighashDecode && !script.IsUnspendable()) {
379 [ + - ]: 2607 : std::string strSigHashDecode;
380 : : // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
381 : : // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
382 : : // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
383 : : // checks in CheckSignatureEncoding.
384 [ + - + + ]: 2607 : if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
385 : 1096 : const unsigned char chSigHashType = vch.back();
386 : 1096 : const auto it = mapSigHashTypes.find(chSigHashType);
387 [ + - ]: 1096 : if (it != mapSigHashTypes.end()) {
388 [ + - ]: 2192 : strSigHashDecode = "[" + it->second + "]";
389 : 1096 : vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
390 : : }
391 : : }
392 [ - + + - : 5214 : str += HexStr(vch) + strSigHashDecode;
+ - ]
393 : 2607 : } else {
394 [ + - ]: 35372 : str += HexStr(vch);
395 : : }
396 : : }
397 : : } else {
398 [ + - ]: 42999164 : str += GetOpName(opcode);
399 : : }
400 : : }
401 : : return str;
402 : 32077 : }
403 : :
404 : 5964 : std::string EncodeHexTx(const CTransaction& tx)
405 : : {
406 : 5964 : DataStream ssTx;
407 [ + - ]: 5964 : ssTx << TX_WITH_WITNESS(tx);
408 [ - + + - ]: 5964 : return HexStr(ssTx);
409 : 5964 : }
410 : :
411 : 17834 : void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
412 : : {
413 : 17834 : CTxDestination address;
414 : :
415 [ + - + - : 35668 : out.pushKV("asm", ScriptToAsmStr(script));
+ - + - ]
416 [ + + ]: 17834 : if (include_address) {
417 [ + + + - : 35520 : out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
+ - + - +
- + - ]
418 : : }
419 [ + + ]: 17834 : if (include_hex) {
420 [ + + + - : 53409 : out.pushKV("hex", HexStr(script));
+ - + - +
- ]
421 : : }
422 : :
423 : 17834 : std::vector<std::vector<unsigned char>> solns;
424 [ + - ]: 17834 : const TxoutType type{Solver(script, solns)};
425 : :
426 [ + + + - : 17834 : if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
+ + + - ]
427 [ + - + - : 31090 : out.pushKV("address", EncodeDestination(address));
+ - + - ]
428 : : }
429 [ + - + - : 35668 : out.pushKV("type", GetTxnOutputType(type));
+ - + - ]
430 : 17834 : }
431 : :
432 : 8501 : 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)
433 : : {
434 : 8501 : CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
435 : :
436 [ + - + - : 17002 : entry.pushKV("txid", tx.GetHash().GetHex());
+ - ]
437 [ + - + - : 17002 : entry.pushKV("hash", tx.GetWitnessHash().GetHex());
+ - ]
438 [ + - + - ]: 17002 : entry.pushKV("version", tx.version);
439 [ + - + - ]: 17002 : entry.pushKV("size", tx.ComputeTotalSize());
440 [ + - + - ]: 17002 : entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
441 [ + - + - ]: 17002 : entry.pushKV("weight", GetTransactionWeight(tx));
442 [ + - + - ]: 17002 : entry.pushKV("locktime", (int64_t)tx.nLockTime);
443 : :
444 : 8501 : UniValue vin{UniValue::VARR};
445 [ - + + - ]: 8501 : vin.reserve(tx.vin.size());
446 : :
447 : : // If available, use Undo data to calculate the fee. Note that txundo == nullptr
448 : : // for coinbase transactions and for transactions where undo data is unavailable.
449 : : const bool have_undo = txundo != nullptr;
450 : : CAmount amt_total_in = 0;
451 : 23076 : CAmount amt_total_out = 0;
452 : :
453 [ - + + + ]: 23076 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
454 : 14575 : const CTxIn& txin = tx.vin[i];
455 : 14575 : UniValue in(UniValue::VOBJ);
456 [ + + ]: 14575 : if (tx.IsCoinBase()) {
457 [ + + + - : 1107 : in.pushKV("coinbase", HexStr(txin.scriptSig));
+ - + - +
- ]
458 : : } else {
459 [ + - + - : 28412 : in.pushKV("txid", txin.prevout.hash.GetHex());
+ - + - ]
460 [ + - + - : 28412 : in.pushKV("vout", (int64_t)txin.prevout.n);
+ - ]
461 : 14206 : UniValue o(UniValue::VOBJ);
462 [ + - + - : 28412 : o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
+ - + - ]
463 [ + + + - : 42618 : o.pushKV("hex", HexStr(txin.scriptSig));
+ - + - +
- ]
464 [ + - + - ]: 28412 : in.pushKV("scriptSig", std::move(o));
465 : 14206 : }
466 [ + + ]: 14575 : if (!tx.vin[i].scriptWitness.IsNull()) {
467 : 12784 : UniValue txinwitness(UniValue::VARR);
468 [ - + + - ]: 12784 : txinwitness.reserve(tx.vin[i].scriptWitness.stack.size());
469 [ + + ]: 38145 : for (const auto& item : tx.vin[i].scriptWitness.stack) {
470 [ - + + - : 25361 : txinwitness.push_back(HexStr(item));
+ - + - ]
471 : : }
472 [ + - + - ]: 25568 : in.pushKV("txinwitness", std::move(txinwitness));
473 : 12784 : }
474 [ + + ]: 14575 : if (have_undo) {
475 [ + + ]: 36 : const Coin& prev_coin = txundo->vprevout[i];
476 : 36 : const CTxOut& prev_txout = prev_coin.out;
477 : :
478 : 36 : amt_total_in += prev_txout.nValue;
479 : :
480 [ + + ]: 36 : if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
481 : 18 : UniValue o_script_pub_key(UniValue::VOBJ);
482 [ + - ]: 18 : ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
483 : :
484 : 18 : UniValue p(UniValue::VOBJ);
485 [ + - + - : 36 : p.pushKV("generated", bool(prev_coin.fCoinBase));
+ - ]
486 [ + - + - : 36 : p.pushKV("height", uint64_t(prev_coin.nHeight));
+ - ]
487 [ + - + - : 36 : p.pushKV("value", ValueFromAmount(prev_txout.nValue));
+ - ]
488 [ + - + - ]: 36 : p.pushKV("scriptPubKey", std::move(o_script_pub_key));
489 [ + - + - ]: 36 : in.pushKV("prevout", std::move(p));
490 : 18 : }
491 : : }
492 [ + - + - : 29150 : in.pushKV("sequence", (int64_t)txin.nSequence);
+ - ]
493 [ + - ]: 14575 : vin.push_back(std::move(in));
494 : 14575 : }
495 [ + - + - ]: 17002 : entry.pushKV("vin", std::move(vin));
496 : :
497 : 8501 : UniValue vout(UniValue::VARR);
498 [ - + + - ]: 8501 : vout.reserve(tx.vout.size());
499 [ - + + + ]: 25793 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
500 : 17292 : const CTxOut& txout = tx.vout[i];
501 : :
502 : 17292 : UniValue out(UniValue::VOBJ);
503 : :
504 [ + - + - : 34584 : out.pushKV("value", ValueFromAmount(txout.nValue));
+ - ]
505 [ + - + - : 34584 : out.pushKV("n", (int64_t)i);
+ - ]
506 : :
507 : 17292 : UniValue o(UniValue::VOBJ);
508 [ + - ]: 17292 : ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
509 [ + - + - ]: 34584 : out.pushKV("scriptPubKey", std::move(o));
510 : :
511 [ + + + - : 17292 : if (is_change_func && is_change_func(txout)) {
+ + ]
512 [ + - + - : 152 : out.pushKV("ischange", true);
+ - ]
513 : : }
514 : :
515 [ + - ]: 17292 : vout.push_back(std::move(out));
516 : :
517 [ + + ]: 17292 : if (have_undo) {
518 : 38 : amt_total_out += txout.nValue;
519 : : }
520 : 17292 : }
521 [ + - + - ]: 17002 : entry.pushKV("vout", std::move(vout));
522 : :
523 [ + + ]: 8501 : if (have_undo) {
524 : 36 : const CAmount fee = amt_total_in - amt_total_out;
525 [ + - ]: 36 : CHECK_NONFATAL(MoneyRange(fee));
526 [ + - + - : 72 : entry.pushKV("fee", ValueFromAmount(fee));
+ - ]
527 : : }
528 : :
529 [ - + ]: 17002 : if (!block_hash.IsNull()) {
530 [ # # # # : 0 : entry.pushKV("blockhash", block_hash.GetHex());
# # # # ]
531 : : }
532 : :
533 [ + + ]: 8501 : if (include_hex) {
534 [ + - + - : 8154 : entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
+ - + - ]
535 : : }
536 : 8501 : }
|