Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

core_write.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2015 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 <spentindex.h>
19 
20 #include <evo/cbtx.h>
21 #include <evo/providertx.h>
22 #include <evo/specialtx.h>
24 
26 {
27  bool sign = amount < 0;
28  int64_t n_abs = (sign ? -amount : amount);
29  int64_t quotient = n_abs / COIN;
30  int64_t remainder = n_abs % COIN;
31  return UniValue(UniValue::VNUM,
32  strprintf("%s%d.%08d", sign ? "-" : "", quotient, remainder));
33 }
34 
35 std::string FormatScript(const CScript& script)
36 {
37  std::string ret;
38  CScript::const_iterator it = script.begin();
39  opcodetype op;
40  while (it != script.end()) {
41  CScript::const_iterator it2 = it;
42  std::vector<unsigned char> vch;
43  if (script.GetOp2(it, op, &vch)) {
44  if (op == OP_0) {
45  ret += "0 ";
46  continue;
47  } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
48  ret += strprintf("%i ", op - OP_1NEGATE - 1);
49  continue;
50  } else if (op >= OP_NOP && op <= OP_NOP10) {
51  std::string str(GetOpName(op));
52  if (str.substr(0, 3) == std::string("OP_")) {
53  ret += str.substr(3, std::string::npos) + " ";
54  continue;
55  }
56  }
57  if (vch.size() > 0) {
58  ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
59  } else {
60  ret += strprintf("0x%x ", HexStr(it2, it));
61  }
62  continue;
63  }
64  ret += strprintf("0x%x ", HexStr(it2, script.end()));
65  break;
66  }
67  return ret.substr(0, ret.size() - 1);
68 }
69 
70 const std::map<unsigned char, std::string> mapSigHashTypes = {
71  {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
72  {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
73  {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
74  {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
75  {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
76  {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
77 };
78 
86 std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
87 {
88  std::string str;
89  opcodetype opcode;
90  std::vector<unsigned char> vch;
91  CScript::const_iterator pc = script.begin();
92  while (pc < script.end()) {
93  if (!str.empty()) {
94  str += " ";
95  }
96  if (!script.GetOp(pc, opcode, vch)) {
97  str += "[error]";
98  return str;
99  }
100  if (0 <= opcode && opcode <= OP_PUSHDATA4) {
101  if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
102  str += strprintf("%d", CScriptNum(vch, false).getint());
103  } else {
104  // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
105  if (fAttemptSighashDecode && !script.IsUnspendable()) {
106  std::string strSigHashDecode;
107  // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
108  // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
109  // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
110  // checks in CheckSignatureEncoding.
111  if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
112  const unsigned char chSigHashType = vch.back();
113  if (mapSigHashTypes.count(chSigHashType)) {
114  strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
115  vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
116  }
117  }
118  str += HexStr(vch) + strSigHashDecode;
119  } else {
120  str += HexStr(vch);
121  }
122  }
123  } else {
124  str += GetOpName(opcode);
125  }
126  }
127  return str;
128 }
129 
130 std::string EncodeHexTx(const CTransaction& tx)
131 {
133  ssTx << tx;
134  return HexStr(ssTx.begin(), ssTx.end());
135 }
136 
137 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
138  UniValue& out, bool fIncludeHex)
139 {
140  txnouttype type;
141  std::vector<CTxDestination> addresses;
142  int nRequired;
143 
144  out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
145  if (fIncludeHex)
146  out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
147 
148  if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
149  out.pushKV("type", GetTxnOutputType(type));
150  return;
151  }
152 
153  out.pushKV("reqSigs", nRequired);
154  out.pushKV("type", GetTxnOutputType(type));
155 
157  for (const CTxDestination& addr : addresses) {
158  a.push_back(EncodeDestination(addr));
159  }
160  out.pushKV("addresses", a);
161 }
162 
163 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, const CSpentIndexTxInfo* ptxSpentInfo)
164 {
165  uint256 txid = tx.GetHash();
166  entry.pushKV("txid", txid.GetHex());
167  entry.pushKV("version", tx.nVersion);
168  entry.pushKV("type", tx.nType);
169  entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
170  entry.pushKV("locktime", (int64_t)tx.nLockTime);
171 
173  for (const CTxIn& txin : tx.vin) {
175  if (tx.IsCoinBase())
176  in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
177  else {
178  in.pushKV("txid", txin.prevout.hash.GetHex());
179  in.pushKV("vout", (int64_t)txin.prevout.n);
181  o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
182  o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
183  in.pushKV("scriptSig", o);
184 
185  // Add address and value info if spentindex enabled
186  if (ptxSpentInfo != nullptr) {
187  CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n);
188  auto it = ptxSpentInfo->mSpentInfo.find(spentKey);
189  if (it != ptxSpentInfo->mSpentInfo.end()) {
190  auto spentInfo = it->second;
191  in.push_back(Pair("value", ValueFromAmount(spentInfo.satoshis)));
192  in.push_back(Pair("valueSat", spentInfo.satoshis));
193  if (spentInfo.addressType == 1) {
194  in.push_back(Pair("address", EncodeDestination(CKeyID(spentInfo.addressHash))));
195  } else if (spentInfo.addressType == 2) {
196  in.push_back(Pair("address", EncodeDestination(CScriptID(spentInfo.addressHash))));
197  }
198  }
199  }
200  }
201  in.pushKV("sequence", (int64_t)txin.nSequence);
202  vin.push_back(in);
203  }
204  entry.pushKV("vin", vin);
205 
206  UniValue vout(UniValue::VARR);
207  for (unsigned int i = 0; i < tx.vout.size(); i++) {
208  const CTxOut& txout = tx.vout[i];
209 
211 
212  out.pushKV("value", ValueFromAmount(txout.nValue));
213  out.pushKV("valueSat", txout.nValue);
214  out.pushKV("n", (int64_t)i);
215 
217  ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
218  out.pushKV("scriptPubKey", o);
219 
220  // Add spent information if spentindex is enabled
221  if (ptxSpentInfo != nullptr) {
222  CSpentIndexKey spentKey(txid, i);
223  auto it = ptxSpentInfo->mSpentInfo.find(spentKey);
224  if (it != ptxSpentInfo->mSpentInfo.end()) {
225  auto spentInfo = it->second;
226  out.push_back(Pair("spentTxId", spentInfo.txid.GetHex()));
227  out.push_back(Pair("spentIndex", (int)spentInfo.inputIndex));
228  out.push_back(Pair("spentHeight", spentInfo.blockHeight));
229  }
230  }
231  vout.push_back(out);
232  }
233  entry.pushKV("vout", vout);
234 
235  if (!tx.vExtraPayload.empty()) {
236  entry.push_back(Pair("extraPayloadSize", (int)tx.vExtraPayload.size()));
237  entry.push_back(Pair("extraPayload", HexStr(tx.vExtraPayload)));
238  }
239 
241  CProRegTx proTx;
242  if (GetTxPayload(tx, proTx)) {
243  UniValue obj;
244  proTx.ToJson(obj);
245  entry.push_back(Pair("proRegTx", obj));
246  }
247  } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
248  CProUpServTx proTx;
249  if (GetTxPayload(tx, proTx)) {
250  UniValue obj;
251  proTx.ToJson(obj);
252  entry.push_back(Pair("proUpServTx", obj));
253  }
254  } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
255  CProUpRegTx proTx;
256  if (GetTxPayload(tx, proTx)) {
257  UniValue obj;
258  proTx.ToJson(obj);
259  entry.push_back(Pair("proUpRegTx", obj));
260  }
261  } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) {
262  CProUpRevTx proTx;
263  if (GetTxPayload(tx, proTx)) {
264  UniValue obj;
265  proTx.ToJson(obj);
266  entry.push_back(Pair("proUpRevTx", obj));
267  }
268  } else if (tx.nType == TRANSACTION_COINBASE) {
269  CCbTx cbTx;
270  if (GetTxPayload(tx, cbTx)) {
271  UniValue obj;
272  cbTx.ToJson(obj);
273  entry.push_back(Pair("cbTx", obj));
274  }
275  } else if (tx.nType == TRANSACTION_QUORUM_COMMITMENT) {
277  if (GetTxPayload(tx, qcTx)) {
278  UniValue obj;
279  qcTx.ToJson(obj);
280  entry.push_back(Pair("qcTx", obj));
281  }
282  }
283 
284  if (!hashBlock.IsNull())
285  entry.pushKV("blockhash", hashBlock.GetHex());
286 
287  if (include_hex) {
288  entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
289  }
290 }
bool GetTxPayload(const std::vector< unsigned char > &payload, T &obj)
Definition: specialtx.h:21
CAmount nValue
Definition: transaction.h:147
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:80
void ToJson(UniValue &obj) const
Definition: cbtx.h:44
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry, bool include_hex, const CSpentIndexTxInfo *ptxSpentInfo)
Definition: core_write.cpp:163
void ToJson(UniValue &obj) const
Definition: providertx.h:171
CScript scriptPubKey
Definition: transaction.h:148
Definition: cbtx.h:16
#define strprintf
Definition: tinyformat.h:1066
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:1295
static const CAmount COIN
Definition: amount.h:14
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
bool IsNull() const
Definition: uint256.h:33
bool IsCoinBase() const
Definition: transaction.h:272
void ToJson(UniValue &obj) const
Definition: providertx.h:226
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:137
Definition: script.h:74
const std::vector< CTxIn > vin
Definition: transaction.h:215
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:86
std::string EncodeHexTx(const CTransaction &tx)
Definition: core_write.cpp:130
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:659
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Parse a standard scriptPubKey with one or more destination addresses.
Definition: standard.cpp:188
const std::map< unsigned char, std::string > mapSigHashTypes
Definition: core_write.cpp:70
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
iterator end()
Definition: prevector.h:320
opcodetype
Script opcodes.
Definition: script.h:48
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
void ToJson(UniValue &obj) const
Definition: providertx.h:66
std::map< CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare > mSpentInfo
Definition: spentindex.h:102
An input of a transaction.
Definition: transaction.h:70
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
const uint256 & GetHash() const
Definition: transaction.h:256
Definition: script.h:58
uint32_t n
Definition: transaction.h:30
Definition: script.h:77
const std::vector< CTxOut > vout
Definition: transaction.h:216
const char * GetTxnOutputType(txnouttype t)
Get the name of a txnouttype as a C string, or nullptr if unknown.
Definition: standard.cpp:21
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
An output of a transaction.
Definition: transaction.h:144
std::string FormatScript(const CScript &script)
Definition: core_write.cpp:35
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
Definition: univalue.h:185
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:10
const int16_t nVersion
Definition: transaction.h:217
CScript scriptSig
Definition: transaction.h:74
txnouttype
Definition: standard.h:56
256-bit opaque blob.
Definition: uint256.h:123
const_iterator end() const
Definition: streams.h:192
std::string EncodeDestination(const CTxDestination &dest)
Definition: base58.cpp:329
const_iterator begin() const
Definition: streams.h:190
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
uint32_t nSequence
Definition: transaction.h:75
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:14
const std::vector< uint8_t > vExtraPayload
Definition: transaction.h:220
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
std::string GetHex() const
Definition: uint256.cpp:21
iterator begin()
Definition: prevector.h:318
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
void ToJson(UniValue &obj) const
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:496
void ToJson(UniValue &obj) const
Definition: providertx.h:120
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
COutPoint prevout
Definition: transaction.h:73
Definition: script.h:51
const int16_t nType
Definition: transaction.h:218
UniValue ValueFromAmount(const CAmount &amount)
Definition: core_write.cpp:25
const uint32_t nLockTime
Definition: transaction.h:219
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
Definition: script.h:523
uint256 hash
Definition: transaction.h:29
Released under the MIT license