Merge #11667: Add scripts to dumpwallet RPC
[bitcoinplatinum.git] / src / core_write.cpp
blobe97876bef313c27a5073aad43f602f92d61dabed
1 // Copyright (c) 2009-2016 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 <consensus/consensus.h>
9 #include <consensus/validation.h>
10 #include <script/script.h>
11 #include <script/standard.h>
12 #include <serialize.h>
13 #include <streams.h>
14 #include <univalue.h>
15 #include <util.h>
16 #include <utilmoneystr.h>
17 #include <utilstrencodings.h>
19 UniValue ValueFromAmount(const CAmount& amount)
21 bool sign = amount < 0;
22 int64_t n_abs = (sign ? -amount : amount);
23 int64_t quotient = n_abs / COIN;
24 int64_t remainder = n_abs % COIN;
25 return UniValue(UniValue::VNUM,
26 strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
29 std::string FormatScript(const CScript& script)
31 std::string ret;
32 CScript::const_iterator it = script.begin();
33 opcodetype op;
34 while (it != script.end()) {
35 CScript::const_iterator it2 = it;
36 std::vector<unsigned char> vch;
37 if (script.GetOp2(it, op, &vch)) {
38 if (op == OP_0) {
39 ret += "0 ";
40 continue;
41 } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
42 ret += strprintf("%i ", op - OP_1NEGATE - 1);
43 continue;
44 } else if (op >= OP_NOP && op <= OP_NOP10) {
45 std::string str(GetOpName(op));
46 if (str.substr(0, 3) == std::string("OP_")) {
47 ret += str.substr(3, std::string::npos) + " ";
48 continue;
51 if (vch.size() > 0) {
52 ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
53 } else {
54 ret += strprintf("0x%x ", HexStr(it2, it));
56 continue;
58 ret += strprintf("0x%x ", HexStr(it2, script.end()));
59 break;
61 return ret.substr(0, ret.size() - 1);
64 const std::map<unsigned char, std::string> mapSigHashTypes = {
65 {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
66 {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
67 {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
68 {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
69 {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
70 {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
73 /**
74 * Create the assembly string representation of a CScript object.
75 * @param[in] script CScript object to convert into the asm string representation.
76 * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format
77 * of a signature. Only pass true for scripts you believe could contain signatures. For example,
78 * pass false, or omit the this argument (defaults to false), for scriptPubKeys.
80 std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
82 std::string str;
83 opcodetype opcode;
84 std::vector<unsigned char> vch;
85 CScript::const_iterator pc = script.begin();
86 while (pc < script.end()) {
87 if (!str.empty()) {
88 str += " ";
90 if (!script.GetOp(pc, opcode, vch)) {
91 str += "[error]";
92 return str;
94 if (0 <= opcode && opcode <= OP_PUSHDATA4) {
95 if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
96 str += strprintf("%d", CScriptNum(vch, false).getint());
97 } else {
98 // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
99 if (fAttemptSighashDecode && !script.IsUnspendable()) {
100 std::string strSigHashDecode;
101 // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
102 // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
103 // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
104 // checks in CheckSignatureEncoding.
105 if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
106 const unsigned char chSigHashType = vch.back();
107 if (mapSigHashTypes.count(chSigHashType)) {
108 strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
109 vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
112 str += HexStr(vch) + strSigHashDecode;
113 } else {
114 str += HexStr(vch);
117 } else {
118 str += GetOpName(opcode);
121 return str;
124 std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
126 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags);
127 ssTx << tx;
128 return HexStr(ssTx.begin(), ssTx.end());
131 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
132 UniValue& out, bool fIncludeHex)
134 txnouttype type;
135 std::vector<CTxDestination> addresses;
136 int nRequired;
138 out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
139 if (fIncludeHex)
140 out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
142 if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
143 out.pushKV("type", GetTxnOutputType(type));
144 return;
147 out.pushKV("reqSigs", nRequired);
148 out.pushKV("type", GetTxnOutputType(type));
150 UniValue a(UniValue::VARR);
151 for (const CTxDestination& addr : addresses) {
152 a.push_back(EncodeDestination(addr));
154 out.pushKV("addresses", a);
157 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags)
159 entry.pushKV("txid", tx.GetHash().GetHex());
160 entry.pushKV("hash", tx.GetWitnessHash().GetHex());
161 entry.pushKV("version", tx.nVersion);
162 entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
163 entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
164 entry.pushKV("locktime", (int64_t)tx.nLockTime);
166 UniValue vin(UniValue::VARR);
167 for (unsigned int i = 0; i < tx.vin.size(); i++) {
168 const CTxIn& txin = tx.vin[i];
169 UniValue in(UniValue::VOBJ);
170 if (tx.IsCoinBase())
171 in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
172 else {
173 in.pushKV("txid", txin.prevout.hash.GetHex());
174 in.pushKV("vout", (int64_t)txin.prevout.n);
175 UniValue o(UniValue::VOBJ);
176 o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
177 o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
178 in.pushKV("scriptSig", o);
179 if (!tx.vin[i].scriptWitness.IsNull()) {
180 UniValue txinwitness(UniValue::VARR);
181 for (const auto& item : tx.vin[i].scriptWitness.stack) {
182 txinwitness.push_back(HexStr(item.begin(), item.end()));
184 in.pushKV("txinwitness", txinwitness);
187 in.pushKV("sequence", (int64_t)txin.nSequence);
188 vin.push_back(in);
190 entry.pushKV("vin", vin);
192 UniValue vout(UniValue::VARR);
193 for (unsigned int i = 0; i < tx.vout.size(); i++) {
194 const CTxOut& txout = tx.vout[i];
196 UniValue out(UniValue::VOBJ);
198 out.pushKV("value", ValueFromAmount(txout.nValue));
199 out.pushKV("n", (int64_t)i);
201 UniValue o(UniValue::VOBJ);
202 ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
203 out.pushKV("scriptPubKey", o);
204 vout.push_back(out);
206 entry.pushKV("vout", vout);
208 if (!hashBlock.IsNull())
209 entry.pushKV("blockhash", hashBlock.GetHex());
211 if (include_hex) {
212 entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".