Bitcoin ABC  0.24.7
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 <config.h>
8 #include <key_io.h>
10 #include <script/script.h>
11 #include <script/sigencoding.h>
12 #include <script/standard.h>
13 #include <serialize.h>
14 #include <streams.h>
15 #include <util/strencodings.h>
16 #include <util/system.h>
17 
18 #include <univalue.h>
19 
20 std::string FormatScript(const CScript &script) {
21  std::string ret;
22  CScript::const_iterator it = script.begin();
23  opcodetype op;
24  while (it != script.end()) {
25  CScript::const_iterator it2 = it;
26  std::vector<uint8_t> vch;
27  if (script.GetOp(it, op, vch)) {
28  if (op == OP_0) {
29  ret += "0 ";
30  continue;
31  }
32 
33  if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
34  ret += strprintf("%i ", op - OP_1NEGATE - 1);
35  continue;
36  }
37 
38  if (op >= OP_NOP && op < FIRST_UNDEFINED_OP_VALUE) {
39  std::string str(GetOpName(op));
40  if (str.substr(0, 3) == std::string("OP_")) {
41  ret += str.substr(3, std::string::npos) + " ";
42  continue;
43  }
44  }
45 
46  if (vch.size() > 0) {
47  ret += strprintf(
48  "0x%x 0x%x ",
49  HexStr(std::vector<uint8_t>(it2, it - vch.size())),
50  HexStr(std::vector<uint8_t>(it - vch.size(), it)));
51  } else {
52  ret +=
53  strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
54  }
55 
56  continue;
57  }
58 
59  ret +=
60  strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
61  break;
62  }
63 
64  return ret.substr(0, ret.size() - 1);
65 }
66 
67 const std::map<uint8_t, std::string> mapSigHashTypes = {
68  {SIGHASH_ALL, "ALL"},
69  {SIGHASH_ALL | SIGHASH_ANYONECANPAY, "ALL|ANYONECANPAY"},
70  {SIGHASH_ALL | SIGHASH_FORKID, "ALL|FORKID"},
72  "ALL|FORKID|ANYONECANPAY"},
73  {SIGHASH_NONE, "NONE"},
74  {SIGHASH_NONE | SIGHASH_ANYONECANPAY, "NONE|ANYONECANPAY"},
75  {SIGHASH_NONE | SIGHASH_FORKID, "NONE|FORKID"},
77  "NONE|FORKID|ANYONECANPAY"},
78  {SIGHASH_SINGLE, "SINGLE"},
79  {SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, "SINGLE|ANYONECANPAY"},
80  {SIGHASH_SINGLE | SIGHASH_FORKID, "SINGLE|FORKID"},
82  "SINGLE|FORKID|ANYONECANPAY"},
83 };
84 
85 std::string SighashToStr(uint8_t sighash_type) {
86  const auto &it = mapSigHashTypes.find(sighash_type);
87  if (it == mapSigHashTypes.end()) {
88  return "";
89  }
90  return it->second;
91 }
92 
102 std::string ScriptToAsmStr(const CScript &script,
103  const bool fAttemptSighashDecode) {
104  std::string str;
105  opcodetype opcode;
106  std::vector<uint8_t> vch;
107  CScript::const_iterator pc = script.begin();
108  while (pc < script.end()) {
109  if (!str.empty()) {
110  str += " ";
111  }
112 
113  if (!script.GetOp(pc, opcode, vch)) {
114  str += "[error]";
115  return str;
116  }
117 
118  if (0 <= opcode && opcode <= OP_PUSHDATA4) {
119  if (vch.size() <= static_cast<std::vector<uint8_t>::size_type>(4)) {
120  str += strprintf("%d", CScriptNum(vch, false).getint());
121  } else {
122  // the IsUnspendable check makes sure not to try to decode
123  // OP_RETURN data that may match the format of a signature
124  if (fAttemptSighashDecode && !script.IsUnspendable()) {
125  std::string strSigHashDecode;
126  // goal: only attempt to decode a defined sighash type from
127  // data that looks like a signature within a scriptSig. This
128  // won't decode correctly formatted public keys in Pubkey or
129  // Multisig scripts due to the restrictions on the pubkey
130  // formats (see IsCompressedOrUncompressedPubKey) being
131  // incongruous with the checks in
132  // CheckTransactionSignatureEncoding.
133  uint32_t flags = SCRIPT_VERIFY_STRICTENC;
134  if (vch.back() & SIGHASH_FORKID) {
135  // If the transaction is using SIGHASH_FORKID, we need
136  // to set the appropriate flag.
137  // TODO: Remove after the Hard Fork.
139  }
141  nullptr)) {
142  const uint8_t chSigHashType = vch.back();
143  const auto it = mapSigHashTypes.find(chSigHashType);
144  if (it != mapSigHashTypes.end()) {
145  strSigHashDecode = "[" + it->second + "]";
146  // remove the sighash type byte. it will be replaced
147  // by the decode.
148  vch.pop_back();
149  }
150  }
151 
152  str += HexStr(vch) + strSigHashDecode;
153  } else {
154  str += HexStr(vch);
155  }
156  }
157  } else {
158  str += GetOpName(opcode);
159  }
160  }
161 
162  return str;
163 }
164 
165 std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags) {
166  CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags);
167  ssTx << tx;
168  return HexStr(ssTx);
169 }
170 
171 void ScriptToUniv(const CScript &script, UniValue &out, bool include_address) {
172  out.pushKV("asm", ScriptToAsmStr(script));
173  out.pushKV("hex", HexStr(script));
174 
175  std::vector<std::vector<uint8_t>> solns;
176  TxoutType type = Solver(script, solns);
177  out.pushKV("type", GetTxnOutputType(type));
178 
179  CTxDestination address;
180  if (include_address && ExtractDestination(script, address) &&
181  type != TxoutType::PUBKEY) {
182  out.pushKV("address", EncodeDestination(address, GetConfig()));
183  }
184 }
185 
186 void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out,
187  bool fIncludeHex) {
188  TxoutType type;
189  std::vector<CTxDestination> addresses;
190  int nRequired;
191 
192  out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
193  if (fIncludeHex) {
194  out.pushKV("hex", HexStr(scriptPubKey));
195  }
196 
197  if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired) ||
198  type == TxoutType::PUBKEY) {
199  out.pushKV("type", GetTxnOutputType(type));
200  return;
201  }
202 
203  out.pushKV("reqSigs", nRequired);
204  out.pushKV("type", GetTxnOutputType(type));
205 
207  for (const CTxDestination &addr : addresses) {
209  }
210  out.pushKV("addresses", a);
211 }
212 
213 void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry,
214  bool include_hex, int serialize_flags) {
215  entry.pushKV("txid", tx.GetId().GetHex());
216  entry.pushKV("hash", tx.GetHash().GetHex());
217  // Transaction version is actually unsigned in consensus checks, just
218  // signed in memory, so cast to unsigned before giving it to the user.
219  entry.pushKV("version",
220  static_cast<int64_t>(static_cast<uint32_t>(tx.nVersion)));
221  entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION));
222  entry.pushKV("locktime", (int64_t)tx.nLockTime);
223 
225  for (unsigned int i = 0; i < tx.vin.size(); i++) {
226  const CTxIn &txin = tx.vin[i];
228  if (tx.IsCoinBase()) {
229  in.pushKV("coinbase", HexStr(txin.scriptSig));
230  } else {
231  in.pushKV("txid", txin.prevout.GetTxId().GetHex());
232  in.pushKV("vout", int64_t(txin.prevout.GetN()));
234  o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
235  o.pushKV("hex", HexStr(txin.scriptSig));
236  in.pushKV("scriptSig", o);
237  }
238 
239  in.pushKV("sequence", (int64_t)txin.nSequence);
240  vin.push_back(in);
241  }
242 
243  entry.pushKV("vin", vin);
244 
245  UniValue vout(UniValue::VARR);
246  for (unsigned int i = 0; i < tx.vout.size(); i++) {
247  const CTxOut &txout = tx.vout[i];
248 
250 
251  out.pushKV("value", txout.nValue);
252  out.pushKV("n", int64_t(i));
253 
255  ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
256  out.pushKV("scriptPubKey", o);
257  vout.push_back(out);
258  }
259 
260  entry.pushKV("vout", vout);
261 
262  if (!hashBlock.IsNull()) {
263  entry.pushKV("blockhash", hashBlock.GetHex());
264  }
265 
266  if (include_hex) {
267  // The hex-encoded transaction. Used the name "hex" to be consistent
268  // with the verbose output of "getrawtransaction".
269  entry.pushKV("hex", EncodeHexTx(tx, serialize_flags));
270  }
271 }
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1182
CTxIn
An input of a transaction.
Definition: transaction.h:61
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:210
OP_0
@ OP_0
Definition: script.h:48
GetOpName
std::string GetOpName(opcodetype opcode)
Definition: script.cpp:14
CTxOut::nValue
Amount nValue
Definition: transaction.h:132
SIGHASH_SINGLE
@ SIGHASH_SINGLE
Definition: sighashtype.h:17
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:27
FormatScript
std::string FormatScript(const CScript &script)
Definition: core_write.cpp:20
prevector::const_iterator
Definition: prevector.h:139
OP_NOP
@ OP_NOP
Definition: script.h:74
TxoutType
TxoutType
Definition: standard.h:85
ScriptToAsmStr
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:102
flags
int flags
Definition: bitcoin-tx.cpp:532
key_io.h
streams.h
transaction.h
CTransaction::nLockTime
const uint32_t nLockTime
Definition: transaction.h:213
SIGHASH_ALL
@ SIGHASH_ALL
Definition: sighashtype.h:15
TxToUniv
void TxToUniv(const CTransaction &tx, const uint256 &hashBlock, UniValue &entry, bool include_hex, int serialize_flags)
Definition: core_write.cpp:213
SighashToStr
std::string SighashToStr(uint8_t sighash_type)
Definition: core_write.cpp:85
CTransaction::GetHash
const TxHash GetHash() const
Definition: transaction.h:245
core_io.h
ScriptPubKeyToUniv
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:186
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
UniValue
Definition: univalue.h:23
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:165
CTxIn::nSequence
uint32_t nSequence
Definition: transaction.h:65
UniValue::type
enum VType type() const
Definition: univalue.h:135
CScriptNum
Definition: script.h:215
prevector::end
iterator end()
Definition: prevector.h:392
strencodings.h
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:256
COutPoint::GetTxId
const TxId & GetTxId() const
Definition: transaction.h:37
TxoutType::PUBKEY
@ PUBKEY
EncodeHexTx
std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags)
Definition: core_write.cpp:165
CTxOut
An output of a transaction.
Definition: transaction.h:130
CTransaction::GetId
const TxId GetId() const
Definition: transaction.h:244
ScriptToUniv
void ScriptToUniv(const CScript &script, UniValue &out, bool include_address)
Definition: core_write.cpp:171
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:211
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:133
SIGHASH_NONE
@ SIGHASH_NONE
Definition: sighashtype.h:16
Solver
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:111
OP_PUSHDATA4
@ OP_PUSHDATA4
Definition: script.h:52
sigencoding.h
univalue.h
base_blob::GetHex
std::string GetHex() const
Definition: uint256.cpp:16
standard.h
uint256
256-bit opaque blob.
Definition: uint256.h:127
CheckTransactionSignatureEncoding
bool CheckTransactionSignatureEncoding(const valtype &vchSig, uint32_t flags, ScriptError *serror)
Check that the signature provided to authentify a transaction is properly encoded.
Definition: sigencoding.cpp:254
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
ExtractDestination
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:161
OP_16
@ OP_16
Definition: script.h:71
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
GetTxnOutputType
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
Definition: standard.cpp:32
SIGHASH_FORKID
@ SIGHASH_FORKID
Definition: sighashtype.h:18
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:30
CScript::GetOp
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
Definition: script.h:501
EncodeDestination
std::string EncodeDestination(const CTxDestination &dest, const Config &config)
Definition: key_io.cpp:170
CTxIn::prevout
COutPoint prevout
Definition: transaction.h:63
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:64
prevector::begin
iterator begin()
Definition: prevector.h:390
SCRIPT_ENABLE_SIGHASH_FORKID
@ SCRIPT_ENABLE_SIGHASH_FORKID
Definition: script_flags.h:85
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
OP_1NEGATE
@ OP_1NEGATE
Definition: script.h:53
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
config.h
script.h
COutPoint::GetN
uint32_t GetN() const
Definition: transaction.h:38
SCRIPT_VERIFY_STRICTENC
@ SCRIPT_VERIFY_STRICTENC
Definition: script_flags.h:22
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:212
CScript::IsUnspendable
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:548
UniValue::VARR
@ VARR
Definition: univalue.h:27
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:656
ExtractDestinations
bool ExtractDestinations(const CScript &scriptPubKey, TxoutType &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Parse a standard scriptPubKey with one or more destination addresses.
Definition: standard.cpp:187
GetConfig
const Config & GetConfig()
Definition: config.cpp:34
OP_1
@ OP_1
Definition: script.h:55
FIRST_UNDEFINED_OP_VALUE
@ FIRST_UNDEFINED_OP_VALUE
Definition: script.h:189
CTxDestination
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:132
mapSigHashTypes
const std::map< uint8_t, std::string > mapSigHashTypes
Definition: core_write.cpp:67
opcodetype
opcodetype
Script opcodes.
Definition: script.h:46
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
SIGHASH_ANYONECANPAY
@ SIGHASH_ANYONECANPAY
Definition: sighashtype.h:19