|
34 | 34 |
|
35 | 35 | #include <univalue.h> |
36 | 36 |
|
37 | | -void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex) |
38 | | -{ |
39 | | - txnouttype type; |
40 | | - std::vector<CTxDestination> addresses; |
41 | | - int nRequired; |
42 | | - |
43 | | - out.push_back(Pair("asm", ScriptToAsmStr(scriptPubKey))); |
44 | | - if (fIncludeHex) |
45 | | - out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); |
46 | | - |
47 | | - if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { |
48 | | - out.push_back(Pair("type", GetTxnOutputType(type))); |
49 | | - return; |
50 | | - } |
51 | | - |
52 | | - out.push_back(Pair("reqSigs", nRequired)); |
53 | | - out.push_back(Pair("type", GetTxnOutputType(type))); |
54 | | - |
55 | | - UniValue a(UniValue::VARR); |
56 | | - BOOST_FOREACH(const CTxDestination& addr, addresses) |
57 | | - a.push_back(CBitcoinAddress(addr).ToString()); |
58 | | - out.push_back(Pair("addresses", a)); |
59 | | -} |
60 | 37 |
|
61 | 38 | void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) |
62 | 39 | { |
63 | | - entry.push_back(Pair("txid", tx.GetHash().GetHex())); |
64 | | - entry.push_back(Pair("hash", tx.GetWitnessHash().GetHex())); |
65 | | - entry.push_back(Pair("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION))); |
66 | | - entry.push_back(Pair("vsize", (int)::GetVirtualTransactionSize(tx))); |
67 | | - entry.push_back(Pair("version", tx.nVersion)); |
68 | | - entry.push_back(Pair("locktime", (int64_t)tx.nLockTime)); |
69 | | - |
70 | | - UniValue vin(UniValue::VARR); |
71 | | - for (unsigned int i = 0; i < tx.vin.size(); i++) { |
72 | | - const CTxIn& txin = tx.vin[i]; |
73 | | - UniValue in(UniValue::VOBJ); |
74 | | - if (tx.IsCoinBase()) |
75 | | - in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); |
76 | | - else { |
77 | | - in.push_back(Pair("txid", txin.prevout.hash.GetHex())); |
78 | | - in.push_back(Pair("vout", (int64_t)txin.prevout.n)); |
79 | | - UniValue o(UniValue::VOBJ); |
80 | | - o.push_back(Pair("asm", ScriptToAsmStr(txin.scriptSig, true))); |
81 | | - o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); |
82 | | - in.push_back(Pair("scriptSig", o)); |
83 | | - } |
84 | | - if (tx.HasWitness()) { |
85 | | - UniValue txinwitness(UniValue::VARR); |
86 | | - for (unsigned int j = 0; j < tx.vin[i].scriptWitness.stack.size(); j++) { |
87 | | - std::vector<unsigned char> item = tx.vin[i].scriptWitness.stack[j]; |
88 | | - txinwitness.push_back(HexStr(item.begin(), item.end())); |
89 | | - } |
90 | | - in.push_back(Pair("txinwitness", txinwitness)); |
91 | | - } |
92 | | - in.push_back(Pair("sequence", (int64_t)txin.nSequence)); |
93 | | - vin.push_back(in); |
94 | | - } |
95 | | - entry.push_back(Pair("vin", vin)); |
96 | | - UniValue vout(UniValue::VARR); |
97 | | - for (unsigned int i = 0; i < tx.vout.size(); i++) { |
98 | | - const CTxOut& txout = tx.vout[i]; |
99 | | - UniValue out(UniValue::VOBJ); |
100 | | - out.push_back(Pair("value", ValueFromAmount(txout.nValue))); |
101 | | - out.push_back(Pair("n", (int64_t)i)); |
102 | | - UniValue o(UniValue::VOBJ); |
103 | | - ScriptPubKeyToJSON(txout.scriptPubKey, o, true); |
104 | | - out.push_back(Pair("scriptPubKey", o)); |
105 | | - vout.push_back(out); |
106 | | - } |
107 | | - entry.push_back(Pair("vout", vout)); |
| 40 | + // Call into TxToUniv() in bitcoin-common to decode the transaction hex. |
| 41 | + // |
| 42 | + // Blockchain contextual information (confirmations and blocktime) is not |
| 43 | + // available to code in bitcoin-common, so we query them here and push the |
| 44 | + // data into the returned UniValue. |
| 45 | + TxToUniv(tx, uint256(), entry); |
108 | 46 |
|
109 | 47 | if (!hashBlock.IsNull()) { |
110 | 48 | entry.push_back(Pair("blockhash", hashBlock.GetHex())); |
@@ -525,7 +463,7 @@ UniValue decoderawtransaction(const JSONRPCRequest& request) |
525 | 463 | throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); |
526 | 464 |
|
527 | 465 | UniValue result(UniValue::VOBJ); |
528 | | - TxToJSON(CTransaction(std::move(mtx)), uint256(), result); |
| 466 | + TxToUniv(CTransaction(std::move(mtx)), uint256(), result); |
529 | 467 |
|
530 | 468 | return result; |
531 | 469 | } |
@@ -565,7 +503,7 @@ UniValue decodescript(const JSONRPCRequest& request) |
565 | 503 | } else { |
566 | 504 | // Empty scripts are valid |
567 | 505 | } |
568 | | - ScriptPubKeyToJSON(script, r, false); |
| 506 | + ScriptPubKeyToUniv(script, r, false); |
569 | 507 |
|
570 | 508 | UniValue type; |
571 | 509 | type = find_value(r, "type"); |
|
0 commit comments