PIVX Core  5.6.99
P2P Digital Currency
core_write.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin developers
2 // Copyright (c) 2017-2021 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "core_io.h"
7 
8 #include "key_io.h"
10 #include "script/script.h"
11 #include "script/standard.h"
13 #include "serialize.h"
14 #include "streams.h"
15 #include <univalue.h>
16 #include "util/system.h"
17 #include "utilmoneystr.h"
18 #include "utilstrencodings.h"
19 
20 std::string FormatScript(const CScript& script)
21 {
22  std::string ret;
23  CScript::const_iterator it = script.begin();
24  opcodetype op;
25  while (it != script.end()) {
26  CScript::const_iterator it2 = it;
27  std::vector<unsigned char> vch;
28  if (script.GetOp2(it, op, &vch)) {
29  if (op == OP_0) {
30  ret += "0 ";
31  continue;
32  } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
33  ret += strprintf("%i ", op - OP_1NEGATE - 1);
34  continue;
35  } else if (op >= OP_NOP && op <= OP_NOP10) {
36  std::string str(GetOpName(op));
37  if (str.substr(0, 3) == std::string("OP_")) {
38  ret += str.substr(3, std::string::npos) + " ";
39  continue;
40  }
41  }
42  if (vch.size() > 0) {
43  ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
44  HexStr(std::vector<uint8_t>(it - vch.size(), it)));
45  } else {
46  ret += strprintf("0x%x", HexStr(std::vector<uint8_t>(it2, it)));
47  }
48  continue;
49  }
50  ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(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  {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
58  {static_cast<unsigned char>(SIGHASH_ALL | SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
59  {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
60  {static_cast<unsigned char>(SIGHASH_NONE | SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
61  {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
62  {static_cast<unsigned char>(SIGHASH_SINGLE | SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")}
63 };
64 
72 std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
73 {
74  std::string str;
75  opcodetype opcode;
76  std::vector<unsigned char> vch;
77  CScript::const_iterator pc = script.begin();
78  while (pc < script.end()) {
79  if (!str.empty()) {
80  str += " ";
81  }
82  if (!script.GetOp(pc, opcode, vch)) {
83  str += "[error]";
84  return str;
85  }
86  if (0 <= opcode && opcode <= OP_PUSHDATA4) {
87  if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
88  str += strprintf("%d", CScriptNum(vch, false).getint());
89  } else {
90  // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
91  if (fAttemptSighashDecode && !script.IsUnspendable()) {
92  std::string strSigHashDecode;
93  // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
94  // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
95  // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
96  // checks in CheckSignatureEncoding.
98  const unsigned char chSigHashType = vch.back();
99  if (mapSigHashTypes.count(chSigHashType)) {
100  strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
101  vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
102  }
103  }
104  str += HexStr(vch) + strSigHashDecode;
105  } else {
106  str += HexStr(vch);
107  }
108  }
109  } else {
110  str += GetOpName(opcode);
111  }
112  }
113  return str;
114 }
115 
116 std::string EncodeHexTx(const CTransaction& tx)
117 {
118  CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
119  ssTx << tx;
120  return HexStr(ssTx);
121 }
122 
123 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
124  UniValue& out,
125  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));
134 
135  if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
136  if (!scriptPubKey.empty() && scriptPubKey.IsZerocoinMint()) {
137  out.pushKV("type", "zerocoinmint"); // unsupported type.
138  } else {
139  out.pushKV("type", GetTxnOutputType(type));
140  }
141  return;
142  }
143 
144  out.pushKV("reqSigs", nRequired);
145  out.pushKV("type", GetTxnOutputType(type));
146 
148  if (type == TX_COLDSTAKE && addresses.size() == 2) {
151  } else {
152  for (const CTxDestination& addr : addresses)
153  a.push_back(EncodeDestination(addr));
154  }
155  out.pushKV("addresses", a);
156 }
157 
158 static void SpecialTxToJSON(const CTransaction& tx, UniValue& entry)
159 {
160  if (tx.IsSpecialTx()) {
161  entry.pushKV("extraPayloadSize", (int)tx.extraPayload->size());
162  entry.pushKV("extraPayload", HexStr(*(tx.extraPayload)));
163  }
164 }
165 
166 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
167 {
168  entry.pushKV("txid", tx.GetHash().GetHex());
169  entry.pushKV("version", tx.nVersion);
170  entry.pushKV("type", tx.nType);
171  entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION));
172  entry.pushKV("locktime", (int64_t)tx.nLockTime);
173 
175  for (const CTxIn& txin : tx.vin) {
177  if (tx.IsCoinBase())
178  in.pushKV("coinbase", HexStr(txin.scriptSig));
179  else {
180  in.pushKV("txid", txin.prevout.hash.GetHex());
181  in.pushKV("vout", (int64_t)txin.prevout.n);
183  o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
184  o.pushKV("hex", HexStr(txin.scriptSig));
185  in.pushKV("scriptSig", o);
186  }
187  in.pushKV("sequence", (int64_t)txin.nSequence);
188  vin.push_back(in);
189  }
190  entry.pushKV("vin", vin);
191 
192  UniValue vout(UniValue::VARR);
193  for (unsigned int i = 0; i < tx.vout.size(); i++) {
194  const CTxOut& txout = tx.vout[i];
195 
197 
198  UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
199  out.pushKV("value", outValue);
200  out.pushKV("n", (int64_t)i);
201 
203  ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
204  out.pushKV("scriptPubKey", o);
205  vout.push_back(out);
206  }
207  entry.pushKV("vout", vout);
208 
209  // Sapling
210  TxSaplingToJSON(tx, entry);
211 
212  // Special Txes
213  SpecialTxToJSON(tx, entry);
214 
215  if (!hashBlock.IsNull())
216  entry.pushKV("blockhash", hashBlock.GetHex());
217 
218  entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
219 }
uint256 hash
Definition: transaction.h:35
uint32_t n
Definition: transaction.h:36
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:654
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:488
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
Definition: script.h:515
bool IsZerocoinMint() const
Definition: script.cpp:270
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
const int16_t nType
Definition: transaction.h:273
std::vector< CTxIn > vin
Definition: transaction.h:270
const uint32_t nLockTime
Definition: transaction.h:274
const int16_t nVersion
Definition: transaction.h:272
const uint256 & GetHash() const
Definition: transaction.h:301
Optional< std::vector< uint8_t > > extraPayload
Definition: transaction.h:276
bool IsCoinBase() const
Definition: transaction.h:376
bool IsSpecialTx() const
Definition: transaction.h:329
std::vector< CTxOut > vout
Definition: transaction.h:271
An input of a transaction.
Definition: transaction.h:94
uint32_t nSequence
Definition: transaction.h:98
CScript scriptSig
Definition: transaction.h:97
COutPoint prevout
Definition: transaction.h:96
An output of a transaction.
Definition: transaction.h:137
CScript scriptPubKey
Definition: transaction.h:140
CAmount nValue
Definition: transaction.h:139
@ VOBJ
Definition: univalue.h:21
@ VARR
Definition: univalue.h:21
@ VNUM
Definition: univalue.h:21
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool IsNull() const
Definition: uint256.h:36
std::string GetHex() const
Definition: uint256.cpp:21
bool empty() const
Definition: prevector.h:281
iterator begin()
Definition: prevector.h:285
iterator end()
Definition: prevector.h:287
256-bit opaque blob.
Definition: uint256.h:138
std::string EncodeHexTx(const CTransaction &tx)
Definition: core_write.cpp:116
std::string FormatScript(const CScript &script)
Definition: core_write.cpp:20
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:123
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:72
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:166
bool CheckSignatureEncoding(const std::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
std::string EncodeDestination(const CWDestination &address, const CChainParams::Base58Type addrType)
void TxSaplingToJSON(const CTransaction &tx, UniValue &entry)
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:14
opcodetype
Script opcodes.
Definition: script.h:50
@ OP_PUSHDATA4
Definition: script.h:56
@ OP_1NEGATE
Definition: script.h:57
@ OP_16
Definition: script.h:75
@ OP_NOP10
Definition: script.h:182
@ OP_NOP
Definition: script.h:78
@ OP_1
Definition: script.h:59
@ OP_0
Definition: script.h:52
@ SER_NETWORK
Definition: serialize.h:174
unsigned int GetSerializeSize(const std::array< T, N > &item)
array
Definition: serialize.h:847
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:195
const char * GetTxnOutputType(txnouttype t)
Definition: standard.cpp:18
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:72
txnouttype
Definition: standard.h:46
@ TX_COLDSTAKE
Definition: standard.h:54
#define strprintf
Definition: tinyformat.h:1056
std::string FormatMoney(const CAmount &n, bool fPlus)
Money parsing/formatting utilities.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.