Dogecoin Core  1.14.2
P2P Digital Currency
core_write.cpp
Go to the documentation of this file.
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.
4 
5 #include "core_io.h"
6 
7 #include "base58.h"
9 #include "script/script.h"
10 #include "script/standard.h"
11 #include "serialize.h"
12 #include "streams.h"
13 #include <univalue.h>
14 #include "util.h"
15 #include "utilmoneystr.h"
16 #include "utilstrencodings.h"
17 
18 #include <boost/assign/list_of.hpp>
19 #include <boost/foreach.hpp>
20 
21 std::string FormatScript(const CScript& script)
22 {
23  std::string ret;
24  CScript::const_iterator it = script.begin();
25  opcodetype op;
26  while (it != script.end()) {
27  CScript::const_iterator it2 = it;
28  std::vector<unsigned char> vch;
29  if (script.GetOp2(it, op, &vch)) {
30  if (op == OP_0) {
31  ret += "0 ";
32  continue;
33  } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
34  ret += strprintf("%i ", op - OP_1NEGATE - 1);
35  continue;
36  } else if (op >= OP_NOP && op <= OP_NOP10) {
37  std::string str(GetOpName(op));
38  if (str.substr(0, 3) == std::string("OP_")) {
39  ret += str.substr(3, std::string::npos) + " ";
40  continue;
41  }
42  }
43  if (vch.size() > 0) {
44  ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
45  } else {
46  ret += strprintf("0x%x ", HexStr(it2, it));
47  }
48  continue;
49  }
50  ret += strprintf("0x%x ", HexStr(it2, script.end()));
51  break;
52  }
53  return ret.substr(0, ret.size() - 1);
54 }
55 
56 const std::map<unsigned char, std::string> mapSigHashTypes =
57  boost::assign::map_list_of
58  (static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL"))
59  (static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY"))
60  (static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE"))
61  (static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY"))
62  (static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE"))
63  (static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY"))
64  ;
65 
73 std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
74 {
75  std::string str;
76  opcodetype opcode;
77  std::vector<unsigned char> vch;
78  CScript::const_iterator pc = script.begin();
79  while (pc < script.end()) {
80  if (!str.empty()) {
81  str += " ";
82  }
83  if (!script.GetOp(pc, opcode, vch)) {
84  str += "[error]";
85  return str;
86  }
87  if (0 <= opcode && opcode <= OP_PUSHDATA4) {
88  if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
89  str += strprintf("%d", CScriptNum(vch, false).getint());
90  } else {
91  // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
92  if (fAttemptSighashDecode && !script.IsUnspendable()) {
93  std::string strSigHashDecode;
94  // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
95  // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
96  // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
97  // checks in CheckSignatureEncoding.
99  const unsigned char chSigHashType = vch.back();
100  if (mapSigHashTypes.count(chSigHashType)) {
101  strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
102  vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
103  }
104  }
105  str += HexStr(vch) + strSigHashDecode;
106  } else {
107  str += HexStr(vch);
108  }
109  }
110  } else {
111  str += GetOpName(opcode);
112  }
113  }
114  return str;
115 }
116 
117 std::string EncodeHexTx(const CTransaction& tx, const int serialFlags)
118 {
119  CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serialFlags);
120  ssTx << tx;
121  return HexStr(ssTx.begin(), ssTx.end());
122 }
123 
124 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
125  UniValue& out, bool fIncludeHex)
126 {
127  txnouttype type;
128  std::vector<CTxDestination> addresses;
129  int nRequired;
130 
131  out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
132  if (fIncludeHex)
133  out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
134 
135  if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
136  out.pushKV("type", GetTxnOutputType(type));
137  return;
138  }
139 
140  out.pushKV("reqSigs", nRequired);
141  out.pushKV("type", GetTxnOutputType(type));
142 
144  BOOST_FOREACH(const CTxDestination& addr, addresses)
145  a.push_back(CBitcoinAddress(addr).ToString());
146  out.pushKV("addresses", a);
147 }
148 
149 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
150 {
151  entry.pushKV("txid", tx.GetHash().GetHex());
152  entry.pushKV("hash", tx.GetWitnessHash().GetHex());
153  entry.pushKV("version", tx.nVersion);
154  entry.pushKV("locktime", (int64_t)tx.nLockTime);
155 
157  for (unsigned int i = 0; i < tx.vin.size(); i++) {
158  const CTxIn& txin = tx.vin[i];
160  if (tx.IsCoinBase())
161  in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
162  else {
163  in.pushKV("txid", txin.prevout.hash.GetHex());
164  in.pushKV("vout", (int64_t)txin.prevout.n);
166  o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
167  o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
168  in.pushKV("scriptSig", o);
169  if (!tx.vin[i].scriptWitness.IsNull()) {
170  UniValue txinwitness(UniValue::VARR);
171  for (const auto& item : tx.vin[i].scriptWitness.stack) {
172  txinwitness.push_back(HexStr(item.begin(), item.end()));
173  }
174  in.pushKV("txinwitness", txinwitness);
175  }
176  }
177  in.pushKV("sequence", (int64_t)txin.nSequence);
178  vin.push_back(in);
179  }
180  entry.pushKV("vin", vin);
181 
182  UniValue vout(UniValue::VARR);
183  for (unsigned int i = 0; i < tx.vout.size(); i++) {
184  const CTxOut& txout = tx.vout[i];
185 
187 
188  UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
189  out.pushKV("value", outValue);
190  out.pushKV("n", (int64_t)i);
191 
193  ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
194  out.pushKV("scriptPubKey", o);
195  vout.push_back(out);
196  }
197  entry.pushKV("vout", vout);
198 
199  if (!hashBlock.IsNull())
200  entry.pushKV("blockhash", hashBlock.GetHex());
201 
202  entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
203 }
base58-encoded Bitcoin addresses.
Definition: base58.h:104
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
const_iterator begin() const
Definition: streams.h:233
const_iterator end() const
Definition: streams.h:235
uint32_t n
Definition: transaction.h:23
uint256 hash
Definition: transaction.h:22
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:377
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:635
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:475
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
Definition: script.h:502
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:308
const uint32_t nLockTime
Definition: transaction.h:328
const std::vector< CTxOut > vout
Definition: transaction.h:327
const uint256 & GetHash() const
Definition: transaction.h:358
uint256 GetWitnessHash() const
Definition: transaction.cpp:70
bool IsCoinBase() const
Definition: transaction.h:383
const int32_t nVersion
Definition: transaction.h:325
const std::vector< CTxIn > vin
Definition: transaction.h:326
An input of a transaction.
Definition: transaction.h:63
uint32_t nSequence
Definition: transaction.h:67
CScript scriptSig
Definition: transaction.h:66
COutPoint prevout
Definition: transaction.h:65
An output of a transaction.
Definition: transaction.h:133
CScript scriptPubKey
Definition: transaction.h:136
CAmount nValue
Definition: transaction.h:135
@ VOBJ
Definition: univalue.h:21
@ VARR
Definition: univalue.h:21
@ VNUM
Definition: univalue.h:21
bool push_back(const UniValue &val)
Definition: univalue.cpp:173
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:192
bool IsNull() const
Definition: uint256.h:32
std::string GetHex() const
Definition: uint256.cpp:21
iterator begin()
Definition: prevector.h:289
iterator end()
Definition: prevector.h:291
256-bit opaque blob.
Definition: uint256.h:123
std::string FormatScript(const CScript &script)
Definition: core_write.cpp:21
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:124
std::string EncodeHexTx(const CTransaction &tx, const int serialFlags)
Definition: core_write.cpp:117
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:73
const std::map< unsigned char, std::string > mapSigHashTypes
Definition: core_write.cpp:56
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry)
Definition: core_write.cpp:149
bool CheckSignatureEncoding(const vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
@ SCRIPT_VERIFY_STRICTENC
Definition: interpreter.h:41
@ SIGHASH_ANYONECANPAY
Definition: interpreter.h:27
@ SIGHASH_ALL
Definition: interpreter.h:24
@ SIGHASH_NONE
Definition: interpreter.h:25
@ SIGHASH_SINGLE
Definition: interpreter.h:26
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:13
opcodetype
Script opcodes.
Definition: script.h:45
@ OP_PUSHDATA4
Definition: script.h:51
@ OP_1NEGATE
Definition: script.h:52
@ OP_16
Definition: script.h:70
@ OP_NOP10
Definition: script.h:178
@ OP_NOP
Definition: script.h:73
@ OP_1
Definition: script.h:54
@ OP_0
Definition: script.h:47
@ SER_NETWORK
Definition: serialize.h:146
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, vector< CTxDestination > &addressRet, int &nRequiredRet)
Definition: standard.cpp:212
const char * GetTxnOutputType(txnouttype t)
Definition: standard.cpp:24
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:71
txnouttype
Definition: standard.h:46
#define strprintf
Definition: tinyformat.h:1047
std::string FormatMoney(const CAmount &n)
Money parsing/formatting utilities.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)