Line data Source code
1 : // Copyright (c) 2009-2014 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 "base58.h"
8 : #include "primitives/transaction.h"
9 : #include "script/script.h"
10 : #include "script/standard.h"
11 : #include "serialize.h"
12 : #include "streams.h"
13 : #include <univalue.h>
14 : #include "util.h"
15 : #include "utilmoneystr.h"
16 : #include "utilstrencodings.h"
17 :
18 : #include <boost/assign/list_of.hpp>
19 : #include <boost/foreach.hpp>
20 :
21 : using namespace std;
22 :
23 158 : string FormatScript(const CScript& script)
24 : {
25 : string ret;
26 316 : CScript::const_iterator it = script.begin();
27 : opcodetype op;
28 1752 : while (it != script.end()) {
29 426 : CScript::const_iterator it2 = it;
30 : vector<unsigned char> vch;
31 426 : if (script.GetOp2(it, op, &vch)) {
32 426 : if (op == OP_0) {
33 : ret += "0 ";
34 : continue;
35 390 : } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
36 120 : ret += strprintf("%i ", op - OP_1NEGATE - 1);
37 60 : continue;
38 330 : } else if (op >= OP_NOP && op <= OP_CHECKMULTISIGVERIFY) {
39 240 : string str(GetOpName(op));
40 480 : if (str.substr(0, 3) == string("OP_")) {
41 360 : ret += str.substr(3, string::npos) + " ";
42 : continue;
43 : }
44 : }
45 420 : if (vch.size() > 0) {
46 1680 : ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
47 : } else {
48 0 : ret += strprintf("0x%x", HexStr(it2, it));
49 : }
50 : continue;
51 : }
52 0 : ret += strprintf("0x%x ", HexStr(it2, script.end()));
53 : break;
54 : }
55 316 : return ret.substr(0, ret.size() - 1);
56 : }
57 :
58 110 : const map<unsigned char, string> mapSigHashTypes =
59 : boost::assign::map_list_of
60 : (static_cast<unsigned char>(SIGHASH_ALL), string("ALL"))
61 660 : (static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), string("ALL|ANYONECANPAY"))
62 550 : (static_cast<unsigned char>(SIGHASH_NONE), string("NONE"))
63 550 : (static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), string("NONE|ANYONECANPAY"))
64 550 : (static_cast<unsigned char>(SIGHASH_SINGLE), string("SINGLE"))
65 550 : (static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), string("SINGLE|ANYONECANPAY"))
66 : ;
67 :
68 : /**
69 : * Create the assembly string representation of a CScript object.
70 : * @param[in] script CScript object to convert into the asm string representation.
71 : * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format
72 : * of a signature. Only pass true for scripts you believe could contain signatures. For example,
73 : * pass false, or omit the this argument (defaults to false), for scriptPubKeys.
74 : */
75 205 : string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
76 : {
77 : string str;
78 : opcodetype opcode;
79 : vector<unsigned char> vch;
80 410 : CScript::const_iterator pc = script.begin();
81 2472 : while (pc < script.end()) {
82 619 : if (!str.empty()) {
83 : str += " ";
84 : }
85 619 : if (!script.GetOp(pc, opcode, vch)) {
86 : str += "[error]";
87 : return str;
88 : }
89 619 : if (0 <= opcode && opcode <= OP_PUSHDATA4) {
90 442 : if (vch.size() <= static_cast<vector<unsigned char>::size_type>(4)) {
91 33 : str += strprintf("%d", CScriptNum(vch, false).getint());
92 : } else {
93 : // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
94 290 : if (fAttemptSighashDecode && !script.IsUnspendable()) {
95 : string strSigHashDecode;
96 : // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
97 : // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
98 : // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
99 : // checks in CheckSignatureEncoding.
100 79 : if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, NULL)) {
101 55 : const unsigned char chSigHashType = vch.back();
102 55 : if (mapSigHashTypes.count(chSigHashType)) {
103 220 : strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
104 55 : vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
105 : }
106 : }
107 237 : str += HexStr(vch) + strSigHashDecode;
108 : } else {
109 131 : str += HexStr(vch);
110 : }
111 : }
112 : } else {
113 398 : str += GetOpName(opcode);
114 : }
115 : }
116 : return str;
117 : }
118 :
119 163 : string EncodeHexTx(const CTransaction& tx)
120 : {
121 : CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
122 : ssTx << tx;
123 326 : return HexStr(ssTx.begin(), ssTx.end());
124 : }
125 :
126 0 : void ScriptPubKeyToUniv(const CScript& scriptPubKey,
127 : UniValue& out, bool fIncludeHex)
128 : {
129 : txnouttype type;
130 : vector<CTxDestination> addresses;
131 : int nRequired;
132 :
133 0 : out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
134 0 : if (fIncludeHex)
135 0 : out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
136 :
137 0 : if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
138 0 : out.pushKV("type", GetTxnOutputType(type));
139 0 : return;
140 : }
141 :
142 0 : out.pushKV("reqSigs", nRequired);
143 0 : out.pushKV("type", GetTxnOutputType(type));
144 :
145 0 : UniValue a(UniValue::VARR);
146 0 : BOOST_FOREACH(const CTxDestination& addr, addresses)
147 0 : a.push_back(CBitcoinAddress(addr).ToString());
148 0 : out.pushKV("addresses", a);
149 : }
150 :
151 0 : void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
152 : {
153 0 : entry.pushKV("txid", tx.GetHash().GetHex());
154 0 : entry.pushKV("version", tx.nVersion);
155 0 : entry.pushKV("locktime", (int64_t)tx.nLockTime);
156 :
157 0 : UniValue vin(UniValue::VARR);
158 0 : BOOST_FOREACH(const CTxIn& txin, tx.vin) {
159 0 : UniValue in(UniValue::VOBJ);
160 0 : if (tx.IsCoinBase())
161 0 : in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
162 : else {
163 0 : in.pushKV("txid", txin.prevout.hash.GetHex());
164 0 : in.pushKV("vout", (int64_t)txin.prevout.n);
165 0 : UniValue o(UniValue::VOBJ);
166 0 : o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
167 0 : o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
168 0 : in.pushKV("scriptSig", o);
169 : }
170 0 : in.pushKV("sequence", (int64_t)txin.nSequence);
171 0 : vin.push_back(in);
172 0 : }
173 0 : entry.pushKV("vin", vin);
174 :
175 0 : UniValue vout(UniValue::VARR);
176 0 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
177 0 : const CTxOut& txout = tx.vout[i];
178 :
179 0 : UniValue out(UniValue::VOBJ);
180 :
181 0 : UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
182 0 : out.pushKV("value", outValue);
183 0 : out.pushKV("n", (int64_t)i);
184 :
185 0 : UniValue o(UniValue::VOBJ);
186 0 : ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
187 0 : out.pushKV("scriptPubKey", o);
188 0 : vout.push_back(out);
189 0 : }
190 0 : entry.pushKV("vout", vout);
191 :
192 0 : if (!hashBlock.IsNull())
193 0 : entry.pushKV("blockhash", hashBlock.GetHex());
194 :
195 0 : entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
196 330 : }
|