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"
13 #include "univalue/univalue.h"
15 #include "utilmoneystr.h"
16 #include "utilstrencodings.h"
18 #include <boost/foreach.hpp>
22 string
FormatScript(const CScript
& script
)
25 CScript::const_iterator it
= script
.begin();
27 while (it
!= script
.end()) {
28 CScript::const_iterator it2
= it
;
29 vector
<unsigned char> vch
;
30 if (script
.GetOp2(it
, op
, &vch
)) {
34 } else if ((op
>= OP_1
&& op
<= OP_16
) || op
== OP_1NEGATE
) {
35 ret
+= strprintf("%i ", op
- OP_1NEGATE
- 1);
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
) + " ";
45 ret
+= strprintf("0x%x 0x%x ", HexStr(it2
, it
- vch
.size()), HexStr(it
- vch
.size(), it
));
47 ret
+= strprintf("0x%x", HexStr(it2
, it
));
51 ret
+= strprintf("0x%x ", HexStr(it2
, script
.end()));
54 return ret
.substr(0, ret
.size() - 1);
57 string
EncodeHexTx(const CTransaction
& tx
)
59 CDataStream
ssTx(SER_NETWORK
, PROTOCOL_VERSION
);
61 return HexStr(ssTx
.begin(), ssTx
.end());
64 void ScriptPubKeyToUniv(const CScript
& scriptPubKey
,
65 UniValue
& out
, bool fIncludeHex
)
68 vector
<CTxDestination
> addresses
;
71 out
.pushKV("asm", scriptPubKey
.ToString());
73 out
.pushKV("hex", HexStr(scriptPubKey
.begin(), scriptPubKey
.end()));
75 if (!ExtractDestinations(scriptPubKey
, type
, addresses
, nRequired
)) {
76 out
.pushKV("type", GetTxnOutputType(type
));
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
);
99 in
.pushKV("coinbase", HexStr(txin
.scriptSig
.begin(), txin
.scriptSig
.end()));
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
);
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
);
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".