Raven Core  3.0.0
P2P Digital Currency
core_write.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT 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 "base58.h"
9 #include "consensus/consensus.h"
10 #include "consensus/validation.h"
11 #include "script/script.h"
12 #include "script/standard.h"
13 #include "serialize.h"
14 #include "streams.h"
15 #include <univalue.h>
16 #include <iomanip>
17 #include "util.h"
18 #include "utilmoneystr.h"
19 #include "utilstrencodings.h"
20 #include "assets/assets.h"
21 
22 std::string ValueFromAmountString(const CAmount& amount, const int8_t units)
23 {
24  bool sign = amount < 0;
25  int64_t n_abs = (sign ? -amount : amount);
26  int64_t quotient = n_abs / COIN;
27  int64_t remainder = n_abs % COIN;
28  remainder = remainder / pow(10, 8 - units);
29 
30  if (units == 0 && remainder == 0) {
31  return strprintf("%s%d", sign ? "-" : "", quotient);
32  }
33  else {
34  return strprintf("%s%d.%0" + std::to_string(units) + "d", sign ? "-" : "", quotient, remainder);
35  }
36 }
37 
38 UniValue ValueFromAmount(const CAmount& amount, const int8_t units)
39 {
40  return UniValue(UniValue::VNUM, ValueFromAmountString(amount, units));
41 }
42 
44 {
45  return ValueFromAmount(amount, 8);
46 }
47 
48 std::string FormatScript(const CScript& script)
49 {
50  std::string ret;
51  CScript::const_iterator it = script.begin();
52  opcodetype op;
53  while (it != script.end()) {
54  CScript::const_iterator it2 = it;
55  std::vector<unsigned char> vch;
56  if (script.GetOp2(it, op, &vch)) {
57  if (op == OP_0) {
58  ret += "0 ";
59  continue;
60  } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
61  ret += strprintf("%i ", op - OP_1NEGATE - 1);
62  continue;
63  } else if (op >= OP_NOP && op <= OP_NOP10) {
64  std::string str(GetOpName(op));
65  if (str.substr(0, 3) == std::string("OP_")) {
66  ret += str.substr(3, std::string::npos) + " ";
67  continue;
68  }
69  }
70  if (vch.size() > 0) {
71  ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
72  } else {
73  ret += strprintf("0x%x ", HexStr(it2, it));
74  }
75  continue;
76  }
77  ret += strprintf("0x%x ", HexStr(it2, script.end()));
78  break;
79  }
80  return ret.substr(0, ret.size() - 1);
81 }
82 
83 const std::map<unsigned char, std::string> mapSigHashTypes = {
84  {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
85  {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
86  {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
87  {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
88  {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
89  {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
90 };
91 
99 std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
100 {
101  std::string str;
102  opcodetype opcode;
103  std::vector<unsigned char> vch;
104  CScript::const_iterator pc = script.begin();
105  while (pc < script.end()) {
106  if (!str.empty()) {
107  str += " ";
108  }
109  if (!script.GetOp(pc, opcode, vch)) {
110  str += "[error]";
111  return str;
112  }
113  if (0 <= opcode && opcode <= OP_PUSHDATA4) {
114  if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
115  str += strprintf("%d", CScriptNum(vch, false).getint());
116  } else {
117  // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
118  if (fAttemptSighashDecode && !script.IsUnspendable()) {
119  std::string strSigHashDecode;
120  // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
121  // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
122  // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
123  // checks in CheckSignatureEncoding.
124  if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
125  const unsigned char chSigHashType = vch.back();
126  if (mapSigHashTypes.count(chSigHashType)) {
127  strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
128  vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
129  }
130  }
131  str += HexStr(vch) + strSigHashDecode;
132  } else {
133  str += HexStr(vch);
134  }
135  }
136  } else {
137  str += GetOpName(opcode);
138  }
139  }
140  return str;
141 }
142 
143 std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
144 {
145  CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags);
146  ssTx << tx;
147  return HexStr(ssTx.begin(), ssTx.end());
148 }
149 
150 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
151  UniValue& out, bool fIncludeHex)
152 {
154  std::vector<CTxDestination> addresses;
155  int nRequired;
156 
157  out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
158  if (fIncludeHex)
159  out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
160 
161  if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
162  out.pushKV("type", GetTxnOutputType(type));
163  return;
164  }
165 
166  out.pushKV("reqSigs", nRequired);
167  out.pushKV("type", GetTxnOutputType(type));
168 
170  if (type == TX_NEW_ASSET || type == TX_TRANSFER_ASSET || type == TX_REISSUE_ASSET) {
171  UniValue assetInfo(UniValue::VOBJ);
172 
173  std::string name;
174  CAmount amount;
175  std::string _assetAddress;
176 
177  if (GetAssetInfoFromScript(scriptPubKey, name, amount)) {
178  assetInfo.pushKV("name", name);
179  assetInfo.pushKV("amount", ValueFromAmount(amount));
180 
181  switch (type) {
182  case TX_NEW_ASSET:
183  if (IsAssetNameAnOwner(name)) {
184  // pwnd n00b
185  } else {
186  CNewAsset asset;
187  if (AssetFromScript(scriptPubKey, asset, _assetAddress)) {
188  assetInfo.pushKV("units", asset.units);
189  assetInfo.pushKV("reissuable", asset.nReissuable > 0 ? true : false);
190  if (asset.nHasIPFS > 0) {
191  assetInfo.pushKV("ipfs_hash", EncodeAssetData(asset.strIPFSHash));
192  }
193  }
194  }
195  break;
196  case TX_TRANSFER_ASSET:
197  break;
198  case TX_REISSUE_ASSET:
199  CReissueAsset asset;
200  if (ReissueAssetFromScript(scriptPubKey, asset, _assetAddress)) {
201  if (asset.nUnits >= 0) {
202  assetInfo.pushKV("units", asset.nUnits);
203  }
204  assetInfo.pushKV("reissuable", asset.nReissuable > 0 ? true : false);
205  if (!asset.strIPFSHash.empty()) {
206  assetInfo.pushKV("ipfs_hash", EncodeAssetData(asset.strIPFSHash));
207  }
208  }
209  break;
210  }
211  }
212 
213  out.pushKV("asset", assetInfo);
214  }
215 
216  if (type == TX_RESTRICTED_ASSET_DATA) {
217  UniValue assetInfo(UniValue::VOBJ);
218  CNullAssetTxData data;
219  CNullAssetTxVerifierString verifierData;
220  std::string address;
221  if (AssetNullDataFromScript(scriptPubKey, data, address)) {
222  AssetType type;
223  IsAssetNameValid(data.asset_name, type);
224  if (type == AssetType::QUALIFIER || type == AssetType::SUB_QUALIFIER) {
225  assetInfo.pushKV("asset_name", data.asset_name);
226  assetInfo.pushKV("qualifier_type", data.flag ? "adding qualifier" : "removing qualifier");
227  assetInfo.pushKV("address", address);
228  } else if (type == AssetType::RESTRICTED) {
229  assetInfo.pushKV("asset_name", data.asset_name);
230  assetInfo.pushKV("restricted_type", data.flag ? "freezing address" : "unfreezing address");
231  assetInfo.pushKV("address", address);
232  }
233  } else if (GlobalAssetNullDataFromScript(scriptPubKey, data)) {
234  assetInfo.pushKV("restricted_name", data.asset_name);
235  assetInfo.pushKV("restricted_type", data.flag ? "freezing" : "unfreezing");
236  assetInfo.pushKV("address", "all addresses");
237  } else if (AssetNullVerifierDataFromScript(scriptPubKey, verifierData)) {
238  assetInfo.pushKV("verifier_string", verifierData.verifier_string);
239  }
240 
241  out.pushKV("asset_data", assetInfo);
242  }
246  for (const CTxDestination& addr : addresses) {
247  a.push_back(EncodeDestination(addr));
248  }
249  out.pushKV("addresses", a);
250 }
251 
252 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags)
253 {
254  entry.pushKV("txid", tx.GetHash().GetHex());
255  entry.pushKV("hash", tx.GetWitnessHash().GetHex());
256  entry.pushKV("version", tx.nVersion);
257  entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
258  entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
259  entry.pushKV("locktime", (int64_t)tx.nLockTime);
260 
262  for (unsigned int i = 0; i < tx.vin.size(); i++) {
263  const CTxIn& txin = tx.vin[i];
265  if (tx.IsCoinBase())
266  in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
267  else {
268  in.pushKV("txid", txin.prevout.hash.GetHex());
269  in.pushKV("vout", (int64_t)txin.prevout.n);
271  o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
272  o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
273  in.pushKV("scriptSig", o);
274  if (!tx.vin[i].scriptWitness.IsNull()) {
275  UniValue txinwitness(UniValue::VARR);
276  for (const auto& item : tx.vin[i].scriptWitness.stack) {
277  txinwitness.push_back(HexStr(item.begin(), item.end()));
278  }
279  in.pushKV("txinwitness", txinwitness);
280  }
281  }
282  in.pushKV("sequence", (int64_t)txin.nSequence);
283  vin.push_back(in);
284  }
285  entry.pushKV("vin", vin);
286 
287  UniValue vout(UniValue::VARR);
288  for (unsigned int i = 0; i < tx.vout.size(); i++) {
289  const CTxOut& txout = tx.vout[i];
290 
292 
293  out.pushKV("value", ValueFromAmount(txout.nValue));
294  out.pushKV("n", (int64_t)i);
295 
297  ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
298  out.pushKV("scriptPubKey", o);
299  vout.push_back(out);
300  }
301  entry.pushKV("vout", vout);
302 
303  if (!hashBlock.IsNull())
304  entry.pushKV("blockhash", hashBlock.GetHex());
305 
306  if (include_hex) {
307  entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
308  }
309 }
CAmount nValue
Definition: transaction.h:140
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry, bool include_hex, int serialize_flags)
Definition: core_write.cpp:252
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:89
uint256 GetWitnessHash() const
Definition: transaction.cpp:79
int8_t units
Definition: assettypes.h:102
CScript scriptPubKey
Definition: transaction.h:141
int8_t nReissuable
Definition: assettypes.h:242
std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags)
Definition: core_write.cpp:143
UniValue ValueFromAmount(const CAmount &amount, const int8_t units)
Definition: core_write.cpp:38
#define strprintf
Definition: tinyformat.h:1054
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:967
bool AssetFromScript(const CScript &scriptPubKey, CNewAsset &assetNew, std::string &strAddress)
Definition: assets.cpp:664
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
int8_t nUnits
Definition: assettypes.h:241
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
bool AssetNullDataFromScript(const CScript &scriptPubKey, CNullAssetTxData &assetData, std::string &strAddress)
Definition: assets.cpp:815
std::string ValueFromAmountString(const CAmount &amount, const int8_t units)
Definition: core_write.cpp:22
bool ReissueAssetFromScript(const CScript &scriptPubKey, CReissueAsset &reissue, std::string &strAddress)
Definition: assets.cpp:789
std::string asset_name
Definition: assettypes.h:278
std::string strIPFSHash
Definition: assettypes.h:105
bool IsNull() const
Definition: uint256.h:33
bool IsCoinBase() const
Definition: transaction.h:360
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:150
Definition: script.h:77
const std::vector< CTxIn > vin
Definition: transaction.h:287
int8_t nReissuable
Definition: assettypes.h:103
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:99
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.cpp:449
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:251
const std::map< unsigned char, std::string > mapSigHashTypes
Definition: core_write.cpp:83
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
iterator end()
Definition: prevector.h:293
bool GlobalAssetNullDataFromScript(const CScript &scriptPubKey, CNullAssetTxData &assetData)
Definition: assets.cpp:840
bool IsAssetNameValid(const std::string &name, AssetType &assetType, std::string &error)
Definition: assets.cpp:207
int8_t nHasIPFS
Definition: assettypes.h:104
opcodetype
Script opcodes.
Definition: script.h:51
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
An input of a transaction.
Definition: transaction.h:67
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
const uint256 & GetHash() const
Definition: transaction.h:320
UniValue()
Definition: univalue.h:24
Definition: script.h:61
uint32_t n
Definition: transaction.h:26
Definition: script.h:80
const std::vector< CTxOut > vout
Definition: transaction.h:288
const char * GetTxnOutputType(txnouttype t)
Get the name of a txnouttype as a C string, or nullptr if unknown.
Definition: standard.cpp:24
bool GetAssetInfoFromScript(const CScript &scriptPubKey, std::string &strName, CAmount &nAmount)
Definition: assets.cpp:3423
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
An output of a transaction.
Definition: transaction.h:137
std::string FormatScript(const CScript &script)
Definition: core_write.cpp:48
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:15
AssetType
Definition: assettypes.h:21
CScript scriptSig
Definition: transaction.h:71
txnouttype
Definition: standard.h:57
256-bit opaque blob.
Definition: uint256.h:123
const int32_t nVersion
Definition: transaction.h:289
enum VType type() const
Definition: univalue.h:175
const_iterator end() const
Definition: streams.h:236
std::string EncodeDestination(const CTxDestination &dest)
Definition: base58.cpp:326
const_iterator begin() const
Definition: streams.h:234
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
uint32_t nSequence
Definition: transaction.h:72
RVN START.
Definition: standard.h:69
std::string GetHex() const
Definition: uint256.cpp:22
iterator begin()
Definition: prevector.h:291
bool AssetNullVerifierDataFromScript(const CScript &scriptPubKey, CNullAssetTxVerifierString &verifierData)
Definition: assets.cpp:860
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:503
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
COutPoint prevout
Definition: transaction.h:70
std::string strIPFSHash
Definition: assettypes.h:243
Definition: script.h:54
std::string EncodeAssetData(std::string decoded)
Definition: assets.cpp:3716
unspendable OP_RAVEN_ASSET script that carries data
Definition: standard.h:72
const uint32_t nLockTime
Definition: transaction.h:290
bool IsAssetNameAnOwner(const std::string &name)
Check if an asset is an owner.
Definition: assets.cpp:296
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
Definition: script.h:530
uint256 hash
Definition: transaction.h:25