Merge pull request #6599
[bitcoinplatinum.git] / src / core_write.cpp
blobc3babec2fc0a1ce56492fd6fd15ceef696af0cac
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.
5 #include "core_io.h"
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/univalue.h"
14 #include "util.h"
15 #include "utilmoneystr.h"
16 #include "utilstrencodings.h"
18 #include <boost/foreach.hpp>
20 using namespace std;
22 string FormatScript(const CScript& script)
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;
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));
49 continue;
51 ret += strprintf("0x%x ", HexStr(it2, script.end()));
52 break;
54 return ret.substr(0, ret.size() - 1);
57 string EncodeHexTx(const CTransaction& tx)
59 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
60 ssTx << tx;
61 return HexStr(ssTx.begin(), ssTx.end());
64 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
65 UniValue& out, bool fIncludeHex)
67 txnouttype type;
68 vector<CTxDestination> addresses;
69 int nRequired;
71 out.pushKV("asm", scriptPubKey.ToString());
72 if (fIncludeHex)
73 out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
75 if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
76 out.pushKV("type", GetTxnOutputType(type));
77 return;
80 out.pushKV("reqSigs", nRequired);
81 out.pushKV("type", GetTxnOutputType(type));
83 UniValue a(UniValue::VARR);
84 BOOST_FOREACH(const CTxDestination& addr, addresses)
85 a.push_back(CBitcoinAddress(addr).ToString());
86 out.pushKV("addresses", a);
89 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
91 entry.pushKV("txid", tx.GetHash().GetHex());
92 entry.pushKV("version", tx.nVersion);
93 entry.pushKV("locktime", (int64_t)tx.nLockTime);
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);
108 in.pushKV("sequence", (int64_t)txin.nSequence);
109 vin.push_back(in);
111 entry.pushKV("vin", vin);
113 UniValue vout(UniValue::VARR);
114 for (unsigned int i = 0; i < tx.vout.size(); i++) {
115 const CTxOut& txout = tx.vout[i];
117 UniValue out(UniValue::VOBJ);
119 UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
120 out.pushKV("value", outValue);
121 out.pushKV("n", (int64_t)i);
123 UniValue o(UniValue::VOBJ);
124 ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
125 out.pushKV("scriptPubKey", o);
126 vout.push_back(out);
128 entry.pushKV("vout", vout);
130 if (!hashBlock.IsNull())
131 entry.pushKV("blockhash", hashBlock.GetHex());
133 entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".