Coverage Report

Created: 2026-09-14 20:36

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