/tmp/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 | | #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 | | OpCodeParser() |
58 | 12 | { |
59 | 2.24k | for (unsigned int op = 0; op <= MAX_OPCODE; ++op) { |
60 | | // Allow OP_RESERVED to get into mapOpNames |
61 | 2.23k | if (op < OP_NOP && op != OP_RESERVED) { |
62 | 1.15k | continue; |
63 | 1.15k | } |
64 | | |
65 | 1.08k | std::string strName = GetOpName(static_cast<opcodetype>(op)); |
66 | 1.08k | if (strName == "OP_UNKNOWN") { |
67 | 0 | continue; |
68 | 0 | } |
69 | 1.08k | mapOpNames[strName] = static_cast<opcodetype>(op); |
70 | | // Convenience: OP_ADD and just ADD are both recognized: |
71 | 1.08k | if (strName.starts_with("OP_")) { |
72 | 1.08k | mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op); |
73 | 1.08k | } |
74 | 1.08k | } |
75 | 12 | } |
76 | | opcodetype Parse(const std::string& s) const |
77 | 3.93k | { |
78 | 3.93k | auto it = mapOpNames.find(s); |
79 | 3.93k | if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode"); |
80 | 3.92k | return it->second; |
81 | 3.93k | } |
82 | | }; |
83 | | |
84 | | opcodetype ParseOpCode(const std::string& s) |
85 | 3.93k | { |
86 | 3.93k | static const OpCodeParser ocp; |
87 | 3.93k | return ocp.Parse(s); |
88 | 3.93k | } |
89 | | |
90 | | } // namespace |
91 | | |
92 | | CScript ParseScript(const std::string& s) |
93 | 2.77k | { |
94 | 2.77k | CScript result; |
95 | | |
96 | 2.77k | std::vector<std::string> words = SplitString(s, " \t\n"); |
97 | | |
98 | 12.2k | for (const std::string& w : words) { |
99 | 12.2k | if (w.empty()) { |
100 | | // Empty string, ignore. (SplitString doesn't combine multiple separators) |
101 | 12.0k | } else if (std::all_of(w.begin(), w.end(), ::IsDigit) || |
102 | 12.0k | (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit))) |
103 | 4.62k | { |
104 | | // Number |
105 | 4.62k | 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 | 4.62k | if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) { |
110 | 7 | throw std::runtime_error("script parse error: decimal numeric value only allowed in the " |
111 | 7 | "range -0xFFFFFFFF...0xFFFFFFFF"); |
112 | 7 | } |
113 | | |
114 | 4.61k | result << num.value(); |
115 | 7.42k | } 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 | 2.24k | std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end())); |
118 | 2.24k | result.insert(result.end(), raw.begin(), raw.end()); |
119 | 5.17k | } 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 | 1.24k | std::vector<unsigned char> value(w.begin() + 1, w.end() - 1); |
123 | 1.24k | result << value; |
124 | 3.93k | } else { |
125 | | // opcode, e.g. OP_ADD or ADD: |
126 | 3.93k | result << ParseOpCode(w); |
127 | 3.93k | } |
128 | 12.2k | } |
129 | | |
130 | 2.76k | return result; |
131 | 2.77k | } |
132 | | |
133 | | /// Check that all of the input and output scripts of a transaction contain valid opcodes |
134 | | static bool CheckTxScriptsSanity(const CMutableTransaction& tx) |
135 | 30.8k | { |
136 | | // Check input scripts for non-coinbase txs |
137 | 30.8k | if (!CTransaction(tx).IsCoinBase()) { |
138 | 103k | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
139 | 72.4k | if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) { |
140 | 1 | return false; |
141 | 1 | } |
142 | 72.4k | } |
143 | 30.8k | } |
144 | | // Check output scripts |
145 | 128k | for (unsigned int i = 0; i < tx.vout.size(); i++) { |
146 | 99.6k | if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) { |
147 | 1.65k | return false; |
148 | 1.65k | } |
149 | 99.6k | } |
150 | | |
151 | 29.1k | return true; |
152 | 30.8k | } |
153 | | |
154 | | static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness) |
155 | 30.8k | { |
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 | 30.8k | CMutableTransaction tx_extended, tx_legacy; |
170 | 30.8k | 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 | 30.8k | if (try_witness) { |
175 | 30.8k | SpanReader ssData{tx_data}; |
176 | 30.8k | try { |
177 | 30.8k | ssData >> TX_WITH_WITNESS(tx_extended); |
178 | 30.8k | if (ssData.empty()) ok_extended = true; |
179 | 30.8k | } catch (const std::exception&) { |
180 | | // Fall through. |
181 | 200 | } |
182 | 30.8k | } |
183 | | |
184 | | // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity, |
185 | | // don't bother decoding the other way. |
186 | 30.8k | if (ok_extended && CheckTxScriptsSanity(tx_extended)) { |
187 | 28.9k | tx = std::move(tx_extended); |
188 | 28.9k | return true; |
189 | 28.9k | } |
190 | | |
191 | | // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input. |
192 | 1.86k | if (try_no_witness) { |
193 | 223 | SpanReader ssData{tx_data}; |
194 | 223 | try { |
195 | 223 | ssData >> TX_NO_WITNESS(tx_legacy); |
196 | 223 | if (ssData.empty()) ok_legacy = true; |
197 | 223 | } catch (const std::exception&) { |
198 | | // Fall through. |
199 | 2 | } |
200 | 223 | } |
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 | 1.86k | if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) { |
205 | 198 | tx = std::move(tx_legacy); |
206 | 198 | return true; |
207 | 198 | } |
208 | | |
209 | | // If extended decoding succeeded, and neither decoding passes sanity, return the extended one. |
210 | 1.67k | if (ok_extended) { |
211 | 1.65k | tx = std::move(tx_extended); |
212 | 1.65k | return true; |
213 | 1.65k | } |
214 | | |
215 | | // If legacy decoding succeeded and extended didn't, return the legacy one. |
216 | 12 | if (ok_legacy) { |
217 | 1 | tx = std::move(tx_legacy); |
218 | 1 | return true; |
219 | 1 | } |
220 | | |
221 | | // If none succeeded, we failed. |
222 | 11 | return false; |
223 | 12 | } |
224 | | |
225 | | bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness) |
226 | 30.8k | { |
227 | 30.8k | if (!IsHex(hex_tx)) { |
228 | 4 | return false; |
229 | 4 | } |
230 | | |
231 | 30.8k | std::vector<unsigned char> txData(ParseHex(hex_tx)); |
232 | 30.8k | return DecodeTx(tx, txData, try_no_witness, try_witness); |
233 | 30.8k | } |
234 | | |
235 | | bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header) |
236 | 1.84k | { |
237 | 1.84k | if (!IsHex(hex_header)) return false; |
238 | | |
239 | 1.84k | const std::vector<unsigned char> header_data{ParseHex(hex_header)}; |
240 | 1.84k | try { |
241 | 1.84k | SpanReader{header_data} >> header; |
242 | 1.84k | } catch (const std::exception&) { |
243 | 2 | return false; |
244 | 2 | } |
245 | 1.84k | return true; |
246 | 1.84k | } |
247 | | |
248 | | bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk) |
249 | 6.97k | { |
250 | 6.97k | if (!IsHex(strHexBlk)) |
251 | 1 | return false; |
252 | | |
253 | 6.97k | std::vector<unsigned char> blockData(ParseHex(strHexBlk)); |
254 | 6.97k | try { |
255 | 6.97k | SpanReader{blockData} >> TX_WITH_WITNESS(block); |
256 | 6.97k | } |
257 | 6.97k | catch (const std::exception&) { |
258 | 3 | return false; |
259 | 3 | } |
260 | | |
261 | 6.97k | return true; |
262 | 6.97k | } |
263 | | |
264 | | util::Result<int> SighashFromStr(const std::string& sighash) |
265 | 79 | { |
266 | 79 | static const std::map<std::string, int> map_sighash_values = { |
267 | 79 | {std::string("DEFAULT"), int(SIGHASH_DEFAULT)}, |
268 | 79 | {std::string("ALL"), int(SIGHASH_ALL)}, |
269 | 79 | {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)}, |
270 | 79 | {std::string("NONE"), int(SIGHASH_NONE)}, |
271 | 79 | {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)}, |
272 | 79 | {std::string("SINGLE"), int(SIGHASH_SINGLE)}, |
273 | 79 | {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)}, |
274 | 79 | }; |
275 | 79 | const auto& it = map_sighash_values.find(sighash); |
276 | 79 | if (it != map_sighash_values.end()) { |
277 | 75 | return it->second; |
278 | 75 | } else { |
279 | 4 | return util::Error{Untranslated("'" + sighash + "' is not a valid sighash parameter.")}; |
280 | 4 | } |
281 | 79 | } |
282 | | |
283 | | UniValue ValueFromAmount(const CAmount amount) |
284 | 298k | { |
285 | 298k | static_assert(COIN > 1); |
286 | 298k | int64_t quotient = amount / COIN; |
287 | 298k | int64_t remainder = amount % COIN; |
288 | 298k | if (amount < 0) { |
289 | 3.63k | quotient = -quotient; |
290 | 3.63k | remainder = -remainder; |
291 | 3.63k | } |
292 | 298k | return UniValue(UniValue::VNUM, |
293 | 298k | strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder)); |
294 | 298k | } |
295 | | |
296 | | std::string FormatScript(const CScript& script) |
297 | 268 | { |
298 | 268 | std::string ret; |
299 | 268 | CScript::const_iterator it = script.begin(); |
300 | 268 | opcodetype op; |
301 | 867 | while (it != script.end()) { |
302 | 599 | CScript::const_iterator it2 = it; |
303 | 599 | std::vector<unsigned char> vch; |
304 | 599 | if (script.GetOp(it, op, vch)) { |
305 | 599 | if (op == OP_0) { |
306 | 63 | ret += "0 "; |
307 | 63 | continue; |
308 | 536 | } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) { |
309 | 63 | ret += strprintf("%i ", op - OP_1NEGATE - 1); |
310 | 63 | continue; |
311 | 473 | } else if (op >= OP_NOP && op <= OP_NOP10) { |
312 | 178 | std::string str(GetOpName(op)); |
313 | 178 | if (str.substr(0, 3) == std::string("OP_")) { |
314 | 178 | ret += str.substr(3, std::string::npos) + " "; |
315 | 178 | continue; |
316 | 178 | } |
317 | 178 | } |
318 | 295 | if (vch.size() > 0) { |
319 | 295 | ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())), |
320 | 295 | HexStr(std::vector<uint8_t>(it - vch.size(), it))); |
321 | 295 | } else { |
322 | 0 | ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it))); |
323 | 0 | } |
324 | 295 | continue; |
325 | 599 | } |
326 | 0 | ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end()))); |
327 | 0 | break; |
328 | 599 | } |
329 | 268 | return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1); |
330 | 268 | } |
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 | | std::string SighashToStr(unsigned char sighash_type) |
342 | 7 | { |
343 | 7 | const auto& it = mapSigHashTypes.find(sighash_type); |
344 | 7 | if (it == mapSigHashTypes.end()) return ""; |
345 | 7 | return it->second; |
346 | 7 | } |
347 | | |
348 | | /** |
349 | | * Create the assembly string representation of a CScript object. |
350 | | * @param[in] script CScript object to convert into the asm string representation. |
351 | | * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format |
352 | | * of a signature. Only pass true for scripts you believe could contain signatures. For example, |
353 | | * pass false, or omit the this argument (defaults to false), for scriptPubKeys. |
354 | | */ |
355 | | std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode) |
356 | 30.7k | { |
357 | 30.7k | std::string str; |
358 | 30.7k | opcodetype opcode; |
359 | 30.7k | std::vector<unsigned char> vch; |
360 | 30.7k | CScript::const_iterator pc = script.begin(); |
361 | 21.5M | while (pc < script.end()) { |
362 | 21.5M | if (!str.empty()) { |
363 | 21.5M | str += " "; |
364 | 21.5M | } |
365 | 21.5M | if (!script.GetOp(pc, opcode, vch)) { |
366 | 3 | str += "[error]"; |
367 | 3 | return str; |
368 | 3 | } |
369 | 21.5M | if (0 <= opcode && opcode <= OP_PUSHDATA4) { |
370 | 22.2k | if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) { |
371 | 1.72k | str += strprintf("%d", CScriptNum(vch, false).getint()); |
372 | 20.4k | } else { |
373 | | // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature |
374 | 20.4k | if (fAttemptSighashDecode && !script.IsUnspendable()) { |
375 | 2.59k | std::string strSigHashDecode; |
376 | | // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig. |
377 | | // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to |
378 | | // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the |
379 | | // checks in CheckSignatureEncoding. |
380 | 2.59k | if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) { |
381 | 1.09k | const unsigned char chSigHashType = vch.back(); |
382 | 1.09k | const auto it = mapSigHashTypes.find(chSigHashType); |
383 | 1.09k | if (it != mapSigHashTypes.end()) { |
384 | 1.09k | strSigHashDecode = "[" + it->second + "]"; |
385 | 1.09k | vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode. |
386 | 1.09k | } |
387 | 1.09k | } |
388 | 2.59k | str += HexStr(vch) + strSigHashDecode; |
389 | 17.8k | } else { |
390 | 17.8k | str += HexStr(vch); |
391 | 17.8k | } |
392 | 20.4k | } |
393 | 21.4M | } else { |
394 | 21.4M | str += GetOpName(opcode); |
395 | 21.4M | } |
396 | 21.5M | } |
397 | 30.7k | return str; |
398 | 30.7k | } |
399 | | |
400 | | std::string EncodeHexTx(const CTransaction& tx) |
401 | 5.88k | { |
402 | 5.88k | DataStream ssTx; |
403 | 5.88k | ssTx << TX_WITH_WITNESS(tx); |
404 | 5.88k | return HexStr(ssTx); |
405 | 5.88k | } |
406 | | |
407 | | void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider) |
408 | 18.0k | { |
409 | 18.0k | CTxDestination address; |
410 | | |
411 | 18.0k | out.pushKV("asm", ScriptToAsmStr(script)); |
412 | 18.0k | if (include_address) { |
413 | 17.9k | out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString()); |
414 | 17.9k | } |
415 | 18.0k | if (include_hex) { |
416 | 18.0k | out.pushKV("hex", HexStr(script)); |
417 | 18.0k | } |
418 | | |
419 | 18.0k | std::vector<std::vector<unsigned char>> solns; |
420 | 18.0k | const TxoutType type{Solver(script, solns)}; |
421 | | |
422 | 18.0k | if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) { |
423 | 15.7k | out.pushKV("address", EncodeDestination(address)); |
424 | 15.7k | } |
425 | 18.0k | out.pushKV("type", GetTxnOutputType(type)); |
426 | 18.0k | } |
427 | | |
428 | | 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) |
429 | 8.34k | { |
430 | 8.34k | CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS); |
431 | | |
432 | 8.34k | entry.pushKV("txid", tx.GetHash().GetHex()); |
433 | 8.34k | entry.pushKV("hash", tx.GetWitnessHash().GetHex()); |
434 | 8.34k | entry.pushKV("version", tx.version); |
435 | 8.34k | entry.pushKV("size", tx.ComputeTotalSize()); |
436 | 8.34k | entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR); |
437 | 8.34k | entry.pushKV("weight", GetTransactionWeight(tx)); |
438 | 8.34k | entry.pushKV("locktime", tx.nLockTime); |
439 | | |
440 | 8.34k | UniValue vin{UniValue::VARR}; |
441 | 8.34k | vin.reserve(tx.vin.size()); |
442 | | |
443 | | // If available, use Undo data to calculate the fee. Note that txundo == nullptr |
444 | | // for coinbase transactions and for transactions where undo data is unavailable. |
445 | 8.34k | const bool have_undo = txundo != nullptr; |
446 | 8.34k | CAmount amt_total_in = 0; |
447 | 8.34k | CAmount amt_total_out = 0; |
448 | | |
449 | 21.3k | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
450 | 13.0k | const CTxIn& txin = tx.vin[i]; |
451 | 13.0k | UniValue in(UniValue::VOBJ); |
452 | 13.0k | if (tx.IsCoinBase()) { |
453 | 366 | in.pushKV("coinbase", HexStr(txin.scriptSig)); |
454 | 12.6k | } else { |
455 | 12.6k | in.pushKV("txid", txin.prevout.hash.GetHex()); |
456 | 12.6k | in.pushKV("vout", txin.prevout.n); |
457 | 12.6k | UniValue o(UniValue::VOBJ); |
458 | 12.6k | o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true)); |
459 | 12.6k | o.pushKV("hex", HexStr(txin.scriptSig)); |
460 | 12.6k | in.pushKV("scriptSig", std::move(o)); |
461 | 12.6k | } |
462 | 13.0k | if (!tx.vin[i].scriptWitness.IsNull()) { |
463 | 11.6k | UniValue txinwitness(UniValue::VARR); |
464 | 11.6k | txinwitness.reserve(tx.vin[i].scriptWitness.stack.size()); |
465 | 23.0k | for (const auto& item : tx.vin[i].scriptWitness.stack) { |
466 | 23.0k | txinwitness.push_back(HexStr(item)); |
467 | 23.0k | } |
468 | 11.6k | in.pushKV("txinwitness", std::move(txinwitness)); |
469 | 11.6k | } |
470 | 13.0k | if (have_undo) { |
471 | 38 | const Coin& prev_coin = txundo->vprevout[i]; |
472 | 38 | const CTxOut& prev_txout = prev_coin.out; |
473 | | |
474 | 38 | amt_total_in += prev_txout.nValue; |
475 | | |
476 | 38 | if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) { |
477 | 18 | UniValue o_script_pub_key(UniValue::VOBJ); |
478 | 18 | ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true); |
479 | | |
480 | 18 | UniValue p(UniValue::VOBJ); |
481 | 18 | p.pushKV("generated", prev_coin.IsCoinBase()); |
482 | 18 | p.pushKV("height", prev_coin.nHeight); |
483 | 18 | p.pushKV("value", ValueFromAmount(prev_txout.nValue)); |
484 | 18 | p.pushKV("scriptPubKey", std::move(o_script_pub_key)); |
485 | 18 | in.pushKV("prevout", std::move(p)); |
486 | 18 | } |
487 | 38 | } |
488 | 13.0k | in.pushKV("sequence", txin.nSequence); |
489 | 13.0k | vin.push_back(std::move(in)); |
490 | 13.0k | } |
491 | 8.34k | entry.pushKV("vin", std::move(vin)); |
492 | | |
493 | 8.34k | UniValue vout(UniValue::VARR); |
494 | 8.34k | vout.reserve(tx.vout.size()); |
495 | 25.1k | for (unsigned int i = 0; i < tx.vout.size(); i++) { |
496 | 16.8k | const CTxOut& txout = tx.vout[i]; |
497 | | |
498 | 16.8k | UniValue out(UniValue::VOBJ); |
499 | | |
500 | 16.8k | out.pushKV("value", ValueFromAmount(txout.nValue)); |
501 | 16.8k | out.pushKV("n", i); |
502 | | |
503 | 16.8k | UniValue o(UniValue::VOBJ); |
504 | 16.8k | ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true); |
505 | 16.8k | out.pushKV("scriptPubKey", std::move(o)); |
506 | | |
507 | 16.8k | if (is_change_func && is_change_func(txout)) { |
508 | 75 | out.pushKV("ischange", true); |
509 | 75 | } |
510 | | |
511 | 16.8k | vout.push_back(std::move(out)); |
512 | | |
513 | 16.8k | if (have_undo) { |
514 | 40 | amt_total_out += txout.nValue; |
515 | 40 | } |
516 | 16.8k | } |
517 | 8.34k | entry.pushKV("vout", std::move(vout)); |
518 | | |
519 | 8.34k | if (have_undo) { |
520 | 38 | const CAmount fee = amt_total_in - amt_total_out; |
521 | 38 | CHECK_NONFATAL(MoneyRange(fee)); |
522 | 38 | entry.pushKV("fee", ValueFromAmount(fee)); |
523 | 38 | } |
524 | | |
525 | 8.34k | if (!block_hash.IsNull()) { |
526 | 0 | entry.pushKV("blockhash", block_hash.GetHex()); |
527 | 0 | } |
528 | | |
529 | 8.34k | if (include_hex) { |
530 | 3.97k | entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction". |
531 | 3.97k | } |
532 | 8.34k | } |