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.
8 #include "primitives/transaction.h"
9 #include "script/script.h"
10 #include "script/standard.h"
11 #include "serialize.h"
15 #include "utilmoneystr.h"
16 #include "utilstrencodings.h"
18 #include <boost/assign/list_of.hpp>
19 #include <boost/foreach.hpp>
23 string FormatScript(const CScript& script)
26 CScript::const_iterator it = script.begin();
28 while (it != script.end()) {
29 CScript::const_iterator it2 = it;
30 vector<unsigned char> vch;
31 if (script.GetOp2(it, op, &vch)) {
35 } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
36 ret += strprintf("%i ", op - OP_1NEGATE - 1);
38 } else if (op >= OP_NOP && op <= OP_CHECKMULTISIGVERIFY) {
39 string str(GetOpName(op));
40 if (str.substr(0, 3) == string("OP_")) {
41 ret += str.substr(3, string::npos) + " ";
46 ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
48 ret += strprintf("0x%x", HexStr(it2, it));
52 ret += strprintf("0x%x ", HexStr(it2, script.end()));
55 return ret.substr(0, ret.size() - 1);
58 const map<unsigned char, string> mapSigHashTypes =
59 boost::assign::map_list_of
60 (static_cast<unsigned char>(SIGHASH_ALL), string("ALL"))
61 (static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), string("ALL|ANYONECANPAY"))
62 (static_cast<unsigned char>(SIGHASH_NONE), string("NONE"))
63 (static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), string("NONE|ANYONECANPAY"))
64 (static_cast<unsigned char>(SIGHASH_SINGLE), string("SINGLE"))
65 (static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), string("SINGLE|ANYONECANPAY"))
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.
75 string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
79 vector<unsigned char> vch;
80 CScript::const_iterator pc = script.begin();
81 while (pc < script.end()) {
85 if (!script.GetOp(pc, opcode, vch)) {
89 if (0 <= opcode && opcode <= OP_PUSHDATA4) {
90 if (vch.size() <= static_cast<vector<unsigned char>::size_type>(4)) {
91 str += strprintf("%d", CScriptNum(vch, false).getint());
93 // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
94 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 if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, NULL)) {
101 const unsigned char chSigHashType = vch.back();
102 if (mapSigHashTypes.count(chSigHashType)) {
103 strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
104 vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
107 str += HexStr(vch) + strSigHashDecode;
113 str += GetOpName(opcode);
119 string EncodeHexTx(const CTransaction& tx)
121 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
123 return HexStr(ssTx.begin(), ssTx.end());
126 string EncodeHexBlk(const CBlock& tx)
128 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
130 return HexStr(ssTx.begin(), ssTx.end());
133 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
134 UniValue& out, bool fIncludeHex)
137 vector<CTxDestination> addresses;
140 out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
142 out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
144 if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
145 out.pushKV("type", GetTxnOutputType(type));
149 out.pushKV("reqSigs", nRequired);
150 out.pushKV("type", GetTxnOutputType(type));
152 UniValue a(UniValue::VARR);
153 for (const CTxDestination& addr : addresses) {
154 a.push_back(EncodeDestination(addr));
156 out.pushKV("addresses", a);
159 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
161 entry.pushKV("txid", tx.GetHash().GetHex());
162 entry.pushKV("version", tx.nVersion);
163 entry.pushKV("locktime", (int64_t)tx.nLockTime);
165 UniValue vin(UniValue::VARR);
166 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
167 UniValue in(UniValue::VOBJ);
169 in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
171 in.pushKV("txid", txin.prevout.hash.GetHex());
172 in.pushKV("vout", (int64_t)txin.prevout.n);
173 UniValue o(UniValue::VOBJ);
174 o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
175 o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
176 in.pushKV("scriptSig", o);
178 in.pushKV("sequence", (int64_t)txin.nSequence);
181 entry.pushKV("vin", vin);
183 UniValue vout(UniValue::VARR);
184 for (unsigned int i = 0; i < tx.vout.size(); i++) {
185 const CTxOut& txout = tx.vout[i];
187 UniValue out(UniValue::VOBJ);
189 UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
190 out.pushKV("value", outValue);
191 out.pushKV("n", (int64_t)i);
193 UniValue o(UniValue::VOBJ);
194 ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
195 out.pushKV("scriptPubKey", o);
198 entry.pushKV("vout", vout);
200 if (!hashBlock.IsNull())
201 entry.pushKV("blockhash", hashBlock.GetHex());
203 entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".