|
35 | 35 |
|
36 | 36 | using namespace std; |
37 | 37 |
|
38 | | -void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex) |
39 | | -{ |
40 | | - txnouttype type; |
41 | | - vector<CTxDestination> addresses; |
42 | | - int nRequired; |
43 | | - |
44 | | - out.push_back(Pair("asm", ScriptToAsmStr(scriptPubKey))); |
45 | | - if (fIncludeHex) |
46 | | - out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); |
47 | | - |
48 | | - if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { |
49 | | - out.push_back(Pair("type", GetTxnOutputType(type))); |
50 | | - return; |
51 | | - } |
52 | | - |
53 | | - out.push_back(Pair("reqSigs", nRequired)); |
54 | | - out.push_back(Pair("type", GetTxnOutputType(type))); |
55 | | - |
56 | | - UniValue a(UniValue::VARR); |
57 | | - BOOST_FOREACH(const CTxDestination& addr, addresses) |
58 | | - a.push_back(CBitcoinAddress(addr).ToString()); |
59 | | - out.push_back(Pair("addresses", a)); |
60 | | -} |
61 | | - |
62 | 38 | void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) |
63 | 39 | { |
64 | | - entry.push_back(Pair("txid", tx.GetHash().GetHex())); |
65 | | - entry.push_back(Pair("hash", tx.GetWitnessHash().GetHex())); |
66 | | - entry.push_back(Pair("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION))); |
67 | | - entry.push_back(Pair("vsize", (int)::GetVirtualTransactionSize(tx))); |
68 | | - entry.push_back(Pair("version", tx.nVersion)); |
69 | | - entry.push_back(Pair("locktime", (int64_t)tx.nLockTime)); |
70 | | - |
71 | | - UniValue vin(UniValue::VARR); |
72 | | - for (unsigned int i = 0; i < tx.vin.size(); i++) { |
73 | | - const CTxIn& txin = tx.vin[i]; |
74 | | - UniValue in(UniValue::VOBJ); |
75 | | - if (tx.IsCoinBase()) |
76 | | - in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); |
77 | | - else { |
78 | | - in.push_back(Pair("txid", txin.prevout.hash.GetHex())); |
79 | | - in.push_back(Pair("vout", (int64_t)txin.prevout.n)); |
80 | | - UniValue o(UniValue::VOBJ); |
81 | | - o.push_back(Pair("asm", ScriptToAsmStr(txin.scriptSig, true))); |
82 | | - o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); |
83 | | - in.push_back(Pair("scriptSig", o)); |
84 | | - } |
85 | | - if (!tx.wit.IsNull()) { |
86 | | - if (!tx.wit.vtxinwit[i].IsNull()) { |
87 | | - UniValue txinwitness(UniValue::VARR); |
88 | | - for (unsigned int j = 0; j < tx.wit.vtxinwit[i].scriptWitness.stack.size(); j++) { |
89 | | - std::vector<unsigned char> item = tx.wit.vtxinwit[i].scriptWitness.stack[j]; |
90 | | - txinwitness.push_back(HexStr(item.begin(), item.end())); |
91 | | - } |
92 | | - in.push_back(Pair("txinwitness", txinwitness)); |
93 | | - } |
94 | | - |
95 | | - } |
96 | | - in.push_back(Pair("sequence", (int64_t)txin.nSequence)); |
97 | | - vin.push_back(in); |
98 | | - } |
99 | | - entry.push_back(Pair("vin", vin)); |
100 | | - UniValue vout(UniValue::VARR); |
101 | | - for (unsigned int i = 0; i < tx.vout.size(); i++) { |
102 | | - const CTxOut& txout = tx.vout[i]; |
103 | | - UniValue out(UniValue::VOBJ); |
104 | | - out.push_back(Pair("value", ValueFromAmount(txout.nValue))); |
105 | | - out.push_back(Pair("n", (int64_t)i)); |
106 | | - UniValue o(UniValue::VOBJ); |
107 | | - ScriptPubKeyToJSON(txout.scriptPubKey, o, true); |
108 | | - out.push_back(Pair("scriptPubKey", o)); |
109 | | - vout.push_back(out); |
110 | | - } |
111 | | - 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); |
112 | 46 |
|
113 | 47 | if (!hashBlock.IsNull()) { |
114 | 48 | entry.push_back(Pair("blockhash", hashBlock.GetHex())); |
@@ -512,7 +446,7 @@ UniValue decoderawtransaction(const UniValue& params, bool fHelp) |
512 | 446 | throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); |
513 | 447 |
|
514 | 448 | UniValue result(UniValue::VOBJ); |
515 | | - TxToJSON(tx, uint256(), result); |
| 449 | + TxToUniv(tx, uint256(), result); |
516 | 450 |
|
517 | 451 | return result; |
518 | 452 | } |
@@ -552,7 +486,7 @@ UniValue decodescript(const UniValue& params, bool fHelp) |
552 | 486 | } else { |
553 | 487 | // Empty scripts are valid |
554 | 488 | } |
555 | | - ScriptPubKeyToJSON(script, r, false); |
| 489 | + ScriptPubKeyToUniv(script, r, false); |
556 | 490 |
|
557 | 491 | r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString())); |
558 | 492 | return r; |
|
0 commit comments