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