Fabcoin Core  0.16.2
P2P Digital Currency
core_write.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2017 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>
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>
18 
20 {
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));
27 }
28 
29 std::string FormatScript(const CScript& script)
30 {
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;
49  }
50  }
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));
55  }
56  continue;
57  }
58  ret += strprintf("0x%x ", HexStr(it2, script.end()));
59  break;
60  }
61  return ret.substr(0, ret.size() - 1);
62 }
63 
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")},
71 };
72 
80 std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
81 {
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 += " ";
89  }
90  if (!script.GetOp(pc, opcode, vch)) {
91  str += "[error]";
92  return str;
93  }
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.
110  }
111  }
112  str += HexStr(vch) + strSigHashDecode;
113  } else {
114  str += HexStr(vch);
115  }
116  }
117  } else {
118  str += GetOpName(opcode);
119  }
120  }
121  return str;
122 }
123 
124 std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
125 {
126  CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags);
127  ssTx << tx;
128  return HexStr(ssTx.begin(), ssTx.end());
129 }
130 
131 void ScriptPubKeyToUniv(const CScript& scriptPubKey,
132  UniValue& out, bool fIncludeHex)
133 {
135  std::vector<CTxDestination> addresses;
136  int nRequired;
137 
138  out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
139  if (fIncludeHex)
140  out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
141 
142  if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
143  out.pushKV("type", GetTxnOutputType(type));
144  return;
145  }
146 
147  out.pushKV("reqSigs", nRequired);
148  out.pushKV("type", GetTxnOutputType(type));
149 
151  for (const CTxDestination& addr : addresses)
152  a.push_back(CFabcoinAddress(addr).ToString());
153  out.pushKV("addresses", a);
154 }
155 
156 void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry, bool include_hex, int serialize_flags)
157 {
158  entry.pushKV("txid", tx.GetHash().GetHex());
159  entry.pushKV("hash", tx.GetWitnessHash().GetHex());
160  entry.pushKV("version", tx.nVersion);
161  entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
162  entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
163  entry.pushKV("locktime", (int64_t)tx.nLockTime);
164 
166  for (unsigned int i = 0; i < tx.vin.size(); i++) {
167  const CTxIn& txin = tx.vin[i];
169  if (tx.IsCoinBase())
170  in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
171  else {
172  in.pushKV("txid", txin.prevout.hash.GetHex());
173  in.pushKV("vout", (int64_t)txin.prevout.n);
175  o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
176  o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
177  in.pushKV("scriptSig", o);
178  if (!tx.vin[i].scriptWitness.IsNull()) {
179  UniValue txinwitness(UniValue::VARR);
180  for (const auto& item : tx.vin[i].scriptWitness.stack) {
181  txinwitness.push_back(HexStr(item.begin(), item.end()));
182  }
183  in.pushKV("txinwitness", txinwitness);
184  }
185  }
186  in.pushKV("sequence", (int64_t)txin.nSequence);
187  vin.push_back(in);
188  }
189  entry.pushKV("vin", vin);
190 
191  UniValue vout(UniValue::VARR);
192  for (unsigned int i = 0; i < tx.vout.size(); i++) {
193  const CTxOut& txout = tx.vout[i];
194 
196 
197  out.pushKV("value", ValueFromAmount(txout.nValue));
198  out.pushKV("n", (int64_t)i);
199 
201  ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
202  out.pushKV("scriptPubKey", o);
203  vout.push_back(out);
204  }
205  entry.pushKV("vout", vout);
206 
207  if (!hashBlock.IsNull())
208  entry.pushKV("blockhash", hashBlock.GetHex());
209 
210  if (include_hex) {
211  entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
212  }
213 }
CAmount nValue
Definition: transaction.h:134
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry, bool include_hex, int serialize_flags)
Definition: core_write.cpp:156
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:79
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
Definition: script.h:550
int64_t GetTransactionWeight(const CTransaction &tx)
const_iterator begin() const
Definition: streams.h:233
CScript scriptPubKey
Definition: transaction.h:135
std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags)
Definition: core_write.cpp:124
#define strprintf
Definition: tinyformat.h:1054
uint256 GetWitnessHash() const
Definition: transaction.cpp:71
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:989
std::string GetHex() const
Definition: uint256.cpp:21
base58-encoded Fabcoin addresses.
Definition: base58.h:104
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:131
Definition: script.h:74
const std::vector< CTxIn > vin
Definition: transaction.h:292
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:80
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Definition: standard.cpp:302
const std::map< unsigned char, std::string > mapSigHashTypes
Definition: core_write.cpp:64
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
#define a(i)
iterator end()
Definition: prevector.h:292
opcodetype
Script opcodes.
Definition: script.h:48
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
An input of a transaction.
Definition: transaction.h:61
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
bool IsNull() const
Definition: uint256.h:38
UniValue()
Definition: univalue.h:24
Definition: script.h:58
uint32_t n
Definition: transaction.h:22
Definition: script.h:77
const std::vector< CTxOut > vout
Definition: transaction.h:293
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:690
const char * GetTxnOutputType(txnouttype t)
Definition: standard.cpp:25
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
An output of a transaction.
Definition: transaction.h:131
std::string FormatScript(const CScript &script)
Definition: core_write.cpp:29
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:11
CScript scriptSig
Definition: transaction.h:65
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:233
txnouttype
Definition: standard.h:51
256-bit opaque blob.
Definition: uint256.h:132
const int32_t nVersion
Definition: transaction.h:294
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
uint32_t nSequence
Definition: transaction.h:66
iterator begin()
Definition: prevector.h:290
bool IsCoinBase() const
Definition: transaction.h:349
const uint256 & GetHash() const
Definition: transaction.h:325
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:523
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:275
COutPoint prevout
Definition: transaction.h:64
Definition: script.h:51
UniValue ValueFromAmount(const CAmount &amount)
Definition: core_write.cpp:19
const uint32_t nLockTime
Definition: transaction.h:295
const_iterator end() const
Definition: streams.h:235
uint256 hash
Definition: transaction.h:21