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