]> Git Repo - VerusCoin.git/blob - src/core_write.cpp
Merge pull request #5243
[VerusCoin.git] / src / core_write.cpp
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/univalue.h"
14 #include "util.h"
15 #include "utilmoneystr.h"
16 #include "utilstrencodings.h"
17
18 #include <boost/foreach.hpp>
19
20 using namespace std;
21
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
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
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 {
91     entry.pushKV("txid", tx.GetHash().GetHex());
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
130     if (!hashBlock.IsNull())
131         entry.pushKV("blockhash", hashBlock.GetHex());
132
133     entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
134 }
This page took 0.027276 seconds and 4 git commands to generate.