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