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