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