]>
Commit | Line | Data |
---|---|---|
f914f1a7 | 1 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
78253fcb | 2 | // Distributed under the MIT software license, see the accompanying |
2b600992 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
ae775b5b JG |
4 | |
5 | #include "core_io.h" | |
611116d4 PK |
6 | |
7 | #include "base58.h" | |
d2270111 | 8 | #include "primitives/transaction.h" |
c4408a6c | 9 | #include "script/script.h" |
10 | #include "script/standard.h" | |
ae775b5b | 11 | #include "serialize.h" |
fa736190 | 12 | #include "streams.h" |
611116d4 | 13 | #include "univalue/univalue.h" |
ae775b5b | 14 | #include "util.h" |
ad49c256 | 15 | #include "utilmoneystr.h" |
85c579e3 | 16 | #include "utilstrencodings.h" |
ae775b5b | 17 | |
53efb09e | 18 | #include <boost/foreach.hpp> |
19 | ||
ae775b5b JG |
20 | using namespace std; |
21 | ||
8138cbea PW |
22 | string FormatScript(const CScript& script) |
23 | { | |
24 | string ret; | |
25 | CScript::const_iterator it = script.begin(); | |
26 | opcodetype op; | |
27 | while (it != script.end()) { | |
28 | CScript::const_iterator it2 = it; | |
29 | vector<unsigned char> vch; | |
30 | if (script.GetOp2(it, op, &vch)) { | |
31 | if (op == OP_0) { | |
32 | ret += "0 "; | |
33 | continue; | |
34 | } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) { | |
35 | ret += strprintf("%i ", op - OP_1NEGATE - 1); | |
36 | continue; | |
37 | } else if (op >= OP_NOP && op <= OP_CHECKMULTISIGVERIFY) { | |
38 | string str(GetOpName(op)); | |
39 | if (str.substr(0, 3) == string("OP_")) { | |
40 | ret += str.substr(3, string::npos) + " "; | |
41 | continue; | |
42 | } | |
43 | } | |
44 | if (vch.size() > 0) { | |
45 | ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it)); | |
46 | } else { | |
47 | ret += strprintf("0x%x", HexStr(it2, it)); | |
48 | } | |
49 | continue; | |
50 | } | |
51 | ret += strprintf("0x%x ", HexStr(it2, script.end())); | |
52 | break; | |
53 | } | |
54 | return ret.substr(0, ret.size() - 1); | |
55 | } | |
56 | ||
ae775b5b JG |
57 | string EncodeHexTx(const CTransaction& tx) |
58 | { | |
59 | CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); | |
60 | ssTx << tx; | |
61 | return HexStr(ssTx.begin(), ssTx.end()); | |
62 | } | |
63 | ||
cbe39a38 JG |
64 | void ScriptPubKeyToUniv(const CScript& scriptPubKey, |
65 | UniValue& out, bool fIncludeHex) | |
66 | { | |
67 | txnouttype type; | |
68 | vector<CTxDestination> addresses; | |
69 | int nRequired; | |
70 | ||
71 | out.pushKV("asm", scriptPubKey.ToString()); | |
72 | if (fIncludeHex) | |
73 | out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())); | |
74 | ||
75 | if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { | |
76 | out.pushKV("type", GetTxnOutputType(type)); | |
77 | return; | |
78 | } | |
79 | ||
80 | out.pushKV("reqSigs", nRequired); | |
81 | out.pushKV("type", GetTxnOutputType(type)); | |
82 | ||
83 | UniValue a(UniValue::VARR); | |
84 | BOOST_FOREACH(const CTxDestination& addr, addresses) | |
85 | a.push_back(CBitcoinAddress(addr).ToString()); | |
86 | out.pushKV("addresses", a); | |
87 | } | |
88 | ||
89 | void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry) | |
90 | { | |
805344dc | 91 | entry.pushKV("txid", tx.GetHash().GetHex()); |
cbe39a38 JG |
92 | entry.pushKV("version", tx.nVersion); |
93 | entry.pushKV("locktime", (int64_t)tx.nLockTime); | |
94 | ||
95 | UniValue vin(UniValue::VARR); | |
96 | BOOST_FOREACH(const CTxIn& txin, tx.vin) { | |
97 | UniValue in(UniValue::VOBJ); | |
98 | if (tx.IsCoinBase()) | |
99 | in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); | |
100 | else { | |
101 | in.pushKV("txid", txin.prevout.hash.GetHex()); | |
102 | in.pushKV("vout", (int64_t)txin.prevout.n); | |
103 | UniValue o(UniValue::VOBJ); | |
104 | o.pushKV("asm", txin.scriptSig.ToString()); | |
105 | o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); | |
106 | in.pushKV("scriptSig", o); | |
107 | } | |
108 | in.pushKV("sequence", (int64_t)txin.nSequence); | |
109 | vin.push_back(in); | |
110 | } | |
111 | entry.pushKV("vin", vin); | |
112 | ||
113 | UniValue vout(UniValue::VARR); | |
114 | for (unsigned int i = 0; i < tx.vout.size(); i++) { | |
115 | const CTxOut& txout = tx.vout[i]; | |
116 | ||
117 | UniValue out(UniValue::VOBJ); | |
118 | ||
119 | UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue)); | |
120 | out.pushKV("value", outValue); | |
121 | out.pushKV("n", (int64_t)i); | |
122 | ||
123 | UniValue o(UniValue::VOBJ); | |
124 | ScriptPubKeyToUniv(txout.scriptPubKey, o, true); | |
125 | out.pushKV("scriptPubKey", o); | |
126 | vout.push_back(out); | |
127 | } | |
128 | entry.pushKV("vout", vout); | |
129 | ||
4f152496 | 130 | if (!hashBlock.IsNull()) |
cbe39a38 | 131 | entry.pushKV("blockhash", hashBlock.GetHex()); |
84877904 | 132 | |
133 | entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction". | |
cbe39a38 | 134 | } |