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