Coverage Report

Created: 2026-09-14 20:36

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