Coverage Report

Created: 2026-11-13 17:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/rpc/rawtransaction_util.cpp
Line
Count
Source
1
// Copyright (c) 2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <rpc/rawtransaction_util.h>
7
8
#include <coins.h>
9
#include <consensus/amount.h>
10
#include <core_io.h>
11
#include <crypto/hex_base.h>
12
#include <key_io.h>
13
#include <policy/feerate.h>
14
#include <policy/policy.h>
15
#include <primitives/transaction.h>
16
#include <rpc/protocol.h>
17
#include <rpc/request.h>
18
#include <rpc/util.h>
19
#include <script/interpreter.h>
20
#include <script/script.h>
21
#include <script/sign.h>
22
#include <script/signingprovider.h>
23
#include <tinyformat.h>
24
#include <univalue.h>
25
#include <util/check.h>
26
#include <util/rbf.h>
27
#include <util/translation.h>
28
#include <util/vector.h>
29
30
#include <cstddef>
31
#include <set>
32
#include <span>
33
#include <variant>
34
35
/** Normalize univalue-represented inputs and add them to the transaction */
36
static void AddInputs(CMutableTransaction& rawTx, const UniValue& inputs_in, std::optional<bool> rbf)
37
1.21k
{
38
1.21k
    UniValue inputs;
39
1.21k
    if (inputs_in.isNull()) {
40
351
        inputs = UniValue::VARR;
41
863
    } else {
42
863
        inputs = inputs_in.get_array();
43
863
    }
44
45
4.53k
    for (unsigned int idx = 0; idx < inputs.size(); idx++) {
46
3.32k
        const UniValue& input = inputs[idx];
47
3.32k
        const UniValue& o = input.get_obj();
48
49
3.32k
        Txid txid = Txid::FromUint256(ParseHashO(o, "txid"));
50
51
3.32k
        const UniValue& vout_v = o.find_value("vout");
52
3.32k
        if (!vout_v.isNum())
53
2
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
54
3.32k
        int nOutput = vout_v.getInt<int>();
55
3.32k
        if (nOutput < 0)
56
1
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout cannot be negative");
57
58
3.32k
        uint32_t nSequence;
59
60
3.32k
        if (rbf.value_or(true)) {
61
3.31k
            nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
62
3.31k
        } else if (rawTx.nLockTime) {
63
1
            nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; /* CTxIn::SEQUENCE_FINAL - 1 */
64
7
        } else {
65
7
            nSequence = CTxIn::SEQUENCE_FINAL;
66
7
        }
67
68
        // set the sequence number if passed in the parameters object
69
3.32k
        const UniValue& sequenceObj = o.find_value("sequence");
70
3.32k
        if (sequenceObj.isNum()) {
71
56
            int64_t seqNr64 = sequenceObj.getInt<int64_t>();
72
56
            if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
73
2
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
74
54
            } else {
75
54
                nSequence = (uint32_t)seqNr64;
76
54
            }
77
56
        }
78
79
3.32k
        CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
80
81
3.32k
        rawTx.vin.push_back(in);
82
3.32k
    }
83
1.21k
}
84
85
UniValue NormalizeOutputs(const UniValue& outputs_in)
86
1.68k
{
87
1.68k
    if (outputs_in.isNull()) {
88
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output argument must be non-null");
89
0
    }
90
91
1.68k
    const bool outputs_is_obj = outputs_in.isObject();
92
1.68k
    UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
93
94
1.68k
    if (!outputs_is_obj) {
95
        // Translate array of key-value pairs into dict
96
993
        UniValue outputs_dict = UniValue(UniValue::VOBJ);
97
8.72k
        for (size_t i = 0; i < outputs.size(); ++i) {
98
7.73k
            const UniValue& output = outputs[i];
99
7.73k
            if (!output.isObject()) {
100
1
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair not an object as expected");
101
1
            }
102
7.73k
            if (output.size() != 1) {
103
1
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair must contain exactly one key");
104
1
            }
105
7.73k
            outputs_dict.pushKVs(output);
106
7.73k
        }
107
991
        outputs = std::move(outputs_dict);
108
991
    }
109
1.68k
    return outputs;
110
1.68k
}
111
112
std::vector<std::pair<CTxDestination, CAmount>> ParseOutputs(const UniValue& outputs)
113
3.02k
{
114
    // Duplicate checking
115
3.02k
    std::set<CTxDestination> destinations;
116
3.02k
    std::vector<std::pair<CTxDestination, CAmount>> parsed_outputs;
117
3.02k
    bool has_data{false};
118
3.02k
    const auto& keys{outputs.getKeys()};
119
3.02k
    const auto& values{outputs.getValues()};
120
18.1k
    for (size_t i{0}; i < keys.size(); ++i) {
121
15.0k
        const auto& name_{keys[i]};
122
15.0k
        const auto& value{values[i]};
123
15.0k
        if (name_ == "data") {
124
27
            if (has_data) {
125
3
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
126
3
            }
127
24
            has_data = true;
128
24
            std::vector<unsigned char> data = ParseHexV(value.getValStr(), "Data");
129
24
            CTxDestination destination{CNoDestination{CScript() << OP_RETURN << data}};
130
24
            CAmount amount{0};
131
24
            parsed_outputs.emplace_back(destination, amount);
132
15.0k
        } else {
133
15.0k
            CTxDestination destination{DecodeDestination(name_)};
134
15.0k
            CAmount amount{AmountFromValue(value)};
135
15.0k
            if (!IsValidDestination(destination)) {
136
1
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + name_);
137
1
            }
138
139
15.0k
            if (!destinations.insert(destination).second) {
140
4
                throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
141
4
            }
142
15.0k
            parsed_outputs.emplace_back(destination, amount);
143
15.0k
        }
144
15.0k
    }
145
3.02k
    return parsed_outputs;
146
3.02k
}
147
148
void AddOutputs(CMutableTransaction& rawTx, const UniValue& outputs_in)
149
1.21k
{
150
1.21k
    UniValue outputs(UniValue::VOBJ);
151
1.21k
    outputs = NormalizeOutputs(outputs_in);
152
153
1.21k
    std::vector<std::pair<CTxDestination, CAmount>> parsed_outputs = ParseOutputs(outputs);
154
5.38k
    for (const auto& [destination, nAmount] : parsed_outputs) {
155
5.38k
        CScript scriptPubKey = GetScriptForDestination(destination);
156
157
5.38k
        CTxOut out(nAmount, scriptPubKey);
158
5.38k
        rawTx.vout.push_back(out);
159
5.38k
    }
160
1.21k
}
161
162
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, std::optional<bool> rbf, const uint32_t version)
163
1.21k
{
164
1.21k
    CMutableTransaction rawTx;
165
166
1.21k
    if (!locktime.isNull()) {
167
161
        int64_t nLockTime = locktime.getInt<int64_t>();
168
161
        if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
169
2
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
170
159
        rawTx.nLockTime = nLockTime;
171
159
    }
172
173
1.21k
    if (version < TX_MIN_STANDARD_VERSION || version > TX_MAX_STANDARD_VERSION) {
174
2
        throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid parameter, version out of range(%d~%d)", TX_MIN_STANDARD_VERSION, TX_MAX_STANDARD_VERSION));
175
2
    }
176
1.21k
    rawTx.version = version;
177
178
1.21k
    AddInputs(rawTx, inputs_in, rbf);
179
1.21k
    AddOutputs(rawTx, outputs_in);
180
181
1.21k
    if (rbf.has_value() && rbf.value() && rawTx.vin.size() > 0 && !SignalsOptInRBF(CTransaction(rawTx))) {
182
1
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
183
1
    }
184
185
1.21k
    return rawTx;
186
1.21k
}
187
188
/** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
189
static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::string& strMessage)
190
96
{
191
96
    UniValue entry(UniValue::VOBJ);
192
96
    entry.pushKV("txid", txin.prevout.hash.ToString());
193
96
    entry.pushKV("vout", txin.prevout.n);
194
96
    UniValue witness(UniValue::VARR);
195
598
    for (unsigned int i = 0; i < txin.scriptWitness.stack.size(); i++) {
196
502
        witness.push_back(HexStr(txin.scriptWitness.stack[i]));
197
502
    }
198
96
    entry.pushKV("witness", std::move(witness));
199
96
    entry.pushKV("scriptSig", HexStr(txin.scriptSig));
200
96
    entry.pushKV("sequence", txin.nSequence);
201
96
    entry.pushKV("error", strMessage);
202
96
    vErrorsRet.push_back(std::move(entry));
203
96
}
204
205
void ParsePrevouts(const UniValue& prevTxsUnival, FlatSigningProvider* keystore, std::map<COutPoint, Coin>& coins)
206
519
{
207
519
    if (!prevTxsUnival.isNull()) {
208
210
        const UniValue& prevTxs = prevTxsUnival.get_array();
209
735
        for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
210
609
            const UniValue& p = prevTxs[idx];
211
609
            if (!p.isObject()) {
212
0
                throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
213
0
            }
214
215
609
            const UniValue& prevOut = p.get_obj();
216
217
609
            RPCTypeCheckObj(prevOut,
218
609
                {
219
609
                    {"txid", UniValueType(UniValue::VSTR)},
220
609
                    {"vout", UniValueType(UniValue::VNUM)},
221
609
                    {"scriptPubKey", UniValueType(UniValue::VSTR)},
222
609
                });
223
224
609
            Txid txid = Txid::FromUint256(ParseHashO(prevOut, "txid"));
225
226
609
            int nOut = prevOut.find_value("vout").getInt<int>();
227
609
            if (nOut < 0) {
228
0
                throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout cannot be negative");
229
0
            }
230
231
609
            COutPoint out(txid, nOut);
232
609
            std::vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
233
609
            CScript scriptPubKey(pkData.begin(), pkData.end());
234
235
609
            {
236
609
                auto coin = coins.find(out);
237
609
                if (coin != coins.end() && !coin->second.IsSpent() && coin->second.out.scriptPubKey != scriptPubKey) {
238
0
                    std::string err("Previous output scriptPubKey mismatch:\n");
239
0
                    err = err + ScriptToAsmStr(coin->second.out.scriptPubKey) + "\nvs:\n"+
240
0
                        ScriptToAsmStr(scriptPubKey);
241
0
                    throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
242
0
                }
243
609
                Coin newcoin;
244
609
                newcoin.out.scriptPubKey = scriptPubKey;
245
609
                newcoin.out.nValue = MAX_MONEY;
246
609
                if (prevOut.exists("amount")) {
247
587
                    newcoin.out.nValue = AmountFromValue(prevOut.find_value("amount"));
248
587
                }
249
609
                newcoin.nHeight = 1;
250
609
                coins[out] = std::move(newcoin);
251
609
            }
252
253
            // if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
254
0
            const bool is_p2sh = scriptPubKey.IsPayToScriptHash();
255
609
            const bool is_p2wsh = scriptPubKey.IsPayToWitnessScriptHash();
256
609
            if (keystore && (is_p2sh || is_p2wsh)) {
257
383
                RPCTypeCheckObj(prevOut,
258
383
                    {
259
383
                        {"redeemScript", UniValueType(UniValue::VSTR)},
260
383
                        {"witnessScript", UniValueType(UniValue::VSTR)},
261
383
                    }, true);
262
383
                const UniValue& rs{prevOut.find_value("redeemScript")};
263
383
                const UniValue& ws{prevOut.find_value("witnessScript")};
264
383
                if (rs.isNull() && ws.isNull()) {
265
21
                    throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
266
21
                }
267
268
                // work from witnessScript when possible
269
362
                std::vector<unsigned char> scriptData(!ws.isNull() ? ParseHexV(ws, "witnessScript") : ParseHexV(rs, "redeemScript"));
270
362
                CScript script(scriptData.begin(), scriptData.end());
271
362
                keystore->scripts.emplace(CScriptID(script), script);
272
                // Automatically also add the P2WSH wrapped version of the script (to deal with P2SH-P2WSH).
273
                // This is done for redeemScript only for compatibility, it is encouraged to use the explicit witnessScript field instead.
274
362
                CScript witness_output_script{GetScriptForDestination(WitnessV0ScriptHash(script))};
275
362
                keystore->scripts.emplace(CScriptID(witness_output_script), witness_output_script);
276
277
362
                if (!ws.isNull() && !rs.isNull()) {
278
                    // if both witnessScript and redeemScript are provided,
279
                    // they should either be the same (for backwards compat),
280
                    // or the redeemScript should be the encoded form of
281
                    // the witnessScript (ie, for p2sh-p2wsh)
282
45
                    if (ws.get_str() != rs.get_str()) {
283
24
                        std::vector<unsigned char> redeemScriptData(ParseHexV(rs, "redeemScript"));
284
24
                        CScript redeemScript(redeemScriptData.begin(), redeemScriptData.end());
285
24
                        if (redeemScript != witness_output_script) {
286
21
                            throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript does not correspond to witnessScript");
287
21
                        }
288
24
                    }
289
45
                }
290
291
341
                if (is_p2sh) {
292
293
                    const CTxDestination p2sh{ScriptHash(script)};
293
293
                    const CTxDestination p2sh_p2wsh{ScriptHash(witness_output_script)};
294
293
                    if (scriptPubKey == GetScriptForDestination(p2sh)) {
295
                        // traditional p2sh; arguably an error if
296
                        // we got here with rs.IsNull(), because
297
                        // that means the p2sh script was specified
298
                        // via witnessScript param, but for now
299
                        // we'll just quietly accept it
300
235
                    } else if (scriptPubKey == GetScriptForDestination(p2sh_p2wsh)) {
301
                        // p2wsh encoded as p2sh; ideally the witness
302
                        // script was specified in the witnessScript
303
                        // param, but also support specifying it via
304
                        // redeemScript param for backwards compat
305
                        // (in which case ws.IsNull() == true)
306
32
                    } else {
307
                        // otherwise, can't generate scriptPubKey from
308
                        // either script, so we got unusable parameters
309
26
                        throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
310
26
                    }
311
293
                } else if (is_p2wsh) {
312
                    // plain p2wsh; could throw an error if script
313
                    // was specified by redeemScript rather than
314
                    // witnessScript (ie, ws.IsNull() == true), but
315
                    // accept it for backwards compat
316
48
                    const CTxDestination p2wsh{WitnessV0ScriptHash(script)};
317
48
                    if (scriptPubKey != GetScriptForDestination(p2wsh)) {
318
16
                        throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
319
16
                    }
320
48
                }
321
341
            }
322
609
        }
323
210
    }
324
519
}
325
326
void SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const UniValue& hashType, UniValue& result)
327
113
{
328
113
    std::optional<int> nHashType = ParseSighashString(hashType);
329
113
    if (!nHashType) {
330
112
        nHashType = SIGHASH_DEFAULT;
331
112
    }
332
333
    // Script verification errors
334
113
    std::map<int, bilingual_str> input_errors;
335
336
113
    bool complete = SignTransaction(mtx, keystore, coins, {.sighash_type = *nHashType}, input_errors);
337
113
    SignTransactionResultToJSON(mtx, complete, coins, input_errors, result);
338
113
}
339
340
void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const std::map<COutPoint, Coin>& coins, const std::map<int, bilingual_str>& input_errors, UniValue& result)
341
424
{
342
    // Make errors UniValue
343
424
    UniValue vErrors(UniValue::VARR);
344
424
    for (const auto& err_pair : input_errors) {
345
98
        if (err_pair.second.original == "Missing amount") {
346
            // This particular error needs to be an exception for some reason
347
2
            throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coins.at(mtx.vin.at(err_pair.first).prevout).out.ToString()));
348
2
        }
349
96
        TxInErrorToJSON(mtx.vin.at(err_pair.first), vErrors, err_pair.second.original);
350
96
    }
351
352
422
    result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
353
422
    result.pushKV("complete", complete);
354
422
    if (!vErrors.empty()) {
355
94
        if (result.exists("errors")) {
356
0
            vErrors.push_backV(result["errors"].getValues());
357
0
        }
358
94
        result.pushKV("errors", std::move(vErrors));
359
94
    }
360
422
}
361
362
std::vector<RPCResult> TxDoc(const TxDocOptions& opts)
363
37.2k
{
364
37.2k
    CHECK_NONFATAL(!opts.fee_doc || opts.fee);
365
37.2k
    CHECK_NONFATAL(!opts.prevout_doc || opts.prevout);
366
37.2k
    CHECK_NONFATAL(!opts.vin_item_doc || opts.vin_inner_elision);
367
37.2k
    CHECK_NONFATAL(opts.elision_mode != ElisionMode::WithSummary || opts.elision_summary.has_value());
368
369
37.2k
    const std::string fee_doc{opts.fee_doc.value_or(
370
37.2k
        "transaction fee in " + CURRENCY_UNIT + ", omitted if block undo data is not available")};
371
37.2k
    const std::string prevout_doc{opts.prevout_doc.value_or(
372
37.2k
        "The previous output, omitted if block undo data is not available")};
373
37.2k
    const std::string vin_item_doc{opts.vin_item_doc.value_or("utxo being spent")};
374
375
37.2k
    auto vin_inner = std::vector<RPCResult>{
376
37.2k
        {RPCResult::Type::STR_HEX, "coinbase", /*optional=*/true, "The coinbase value (only if coinbase transaction)"},
377
37.2k
        {RPCResult::Type::STR_HEX, "txid", /*optional=*/true, "The transaction id (if not coinbase transaction)"},
378
37.2k
        {RPCResult::Type::NUM, "vout", /*optional=*/true, "The output number (if not coinbase transaction)"},
379
37.2k
        {RPCResult::Type::OBJ, "scriptSig", /*optional=*/true, "The script (if not coinbase transaction)",
380
37.2k
        {
381
37.2k
            {RPCResult::Type::STR, "asm", "Disassembly of the signature script"},
382
37.2k
            {RPCResult::Type::STR_HEX, "hex", "The raw signature script bytes, hex-encoded"},
383
37.2k
        }},
384
37.2k
        {RPCResult::Type::ARR, "txinwitness", /*optional=*/true, "",
385
37.2k
        {
386
37.2k
            {RPCResult::Type::STR_HEX, "hex", "hex-encoded witness data (if any)"},
387
37.2k
        }},
388
37.2k
    };
389
37.2k
    if (opts.prevout) {
390
11.8k
        vin_inner.emplace_back(
391
11.8k
            RPCResult::Type::OBJ, "prevout", opts.prevout_optional, prevout_doc,
392
11.8k
            std::vector<RPCResult>{
393
11.8k
                {RPCResult::Type::BOOL, "generated", "Coinbase or not"},
394
11.8k
                {RPCResult::Type::NUM, "height", "The height of the prevout"},
395
11.8k
                {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
396
11.8k
                {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
397
11.8k
            }
398
11.8k
        );
399
11.8k
    }
400
37.2k
    vin_inner.emplace_back(RPCResult::Type::NUM, "sequence", "The script sequence number");
401
402
37.2k
    if (opts.vin_inner_elision) {
403
11.8k
        vin_inner = ElideGroup(std::move(vin_inner), *opts.vin_inner_elision);
404
11.8k
        if (opts.prevout) {
405
            // prevout remains visible even when other fields are elided
406
11.8k
            std::vector<RPCResult> new_vin;
407
11.8k
            new_vin.reserve(vin_inner.size());
408
83.2k
            for (const auto& r : vin_inner) {
409
83.2k
                if (r.m_key_name == "prevout") {
410
11.8k
                    RPCResultOptions unopts = r.m_opts;
411
11.8k
                    unopts.print_elision = HelpElisionNone{};
412
11.8k
                    new_vin.emplace_back(r, std::move(unopts));
413
71.3k
                } else {
414
71.3k
                    new_vin.push_back(r);
415
71.3k
                }
416
83.2k
            }
417
11.8k
            vin_inner = std::move(new_vin);
418
11.8k
        }
419
11.8k
    }
420
421
37.2k
    auto fields = std::vector<RPCResult>{
422
37.2k
        {RPCResult::Type::STR_HEX, "txid", opts.txid_field_doc},
423
37.2k
        {RPCResult::Type::STR_HEX, "hash", "The transaction hash (differs from txid for witness transactions)"},
424
37.2k
        {RPCResult::Type::NUM, "size", "The serialized transaction size"},
425
37.2k
        {RPCResult::Type::NUM, "vsize", "The virtual transaction size (differs from size for witness transactions)"},
426
37.2k
        {RPCResult::Type::NUM, "weight", "The transaction's weight (between vsize*4-3 and vsize*4)"},
427
37.2k
        {RPCResult::Type::NUM, "version", "The version"},
428
37.2k
        {RPCResult::Type::NUM_TIME, "locktime", "The lock time"},
429
37.2k
        {RPCResult::Type::ARR, "vin", "",
430
37.2k
        {
431
37.2k
            {RPCResult::Type::OBJ, "", opts.vin_inner_elision ? vin_item_doc : "", std::move(vin_inner)},
432
37.2k
        }},
433
37.2k
        {RPCResult::Type::ARR, "vout", "",
434
37.2k
        {
435
37.2k
            {RPCResult::Type::OBJ, "", "", Cat(
436
37.2k
                {
437
37.2k
                    {RPCResult::Type::STR_AMOUNT, "value", "The value in " + CURRENCY_UNIT},
438
37.2k
                    {RPCResult::Type::NUM, "n", "index"},
439
37.2k
                    {RPCResult::Type::OBJ, "scriptPubKey", "", ScriptPubKeyDoc()},
440
37.2k
                },
441
37.2k
                opts.wallet ?
442
2.29k
                    std::vector<RPCResult>{{RPCResult::Type::BOOL, "ischange", /*optional=*/true, "Output script is change (only present if true)"}} :
443
37.2k
                    std::vector<RPCResult>{}
444
37.2k
            )},
445
37.2k
        }},
446
37.2k
    };
447
448
37.2k
    if (opts.fee) fields.emplace_back(RPCResult::Type::NUM, "fee", /*optional=*/true, fee_doc);
449
37.2k
    if (opts.hex) fields.emplace_back(RPCResult::Type::STR_HEX, "hex", "The hex-encoded transaction data");
450
451
37.2k
    if (opts.elision_mode != ElisionMode::None) {
452
22.0k
        const bool silent = opts.elision_mode == ElisionMode::Silent;
453
22.0k
        std::vector<RPCResult> new_fields;
454
22.0k
        new_fields.reserve(fields.size());
455
22.0k
        bool first = true;
456
221k
        for (const auto& f : fields) {
457
221k
            if (!silent && f.m_key_name == "fee") {
458
5.71k
                new_fields.push_back(f);
459
5.71k
                continue;
460
5.71k
            }
461
215k
            if (f.m_key_name == "vin" && opts.vin_inner_elision) {
462
11.8k
                new_fields.push_back(f);
463
11.8k
                continue;
464
11.8k
            }
465
203k
            if (!silent && first) {
466
10.1k
                RPCResultOptions eopts = f.m_opts;
467
10.1k
                eopts.print_elision = opts.elision_summary.value_or("");
468
10.1k
                new_fields.emplace_back(f, std::move(eopts));
469
10.1k
                first = false;
470
193k
            } else {
471
193k
                RPCResultOptions eopts = f.m_opts;
472
193k
                eopts.print_elision = HelpElisionSkip{};
473
193k
                new_fields.emplace_back(f, std::move(eopts));
474
193k
            }
475
203k
        }
476
22.0k
        fields = std::move(new_fields);
477
22.0k
    }
478
479
37.2k
    return fields;
480
37.2k
}