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