Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

core_write.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "core_io.h"
  5. #include "base58.h"
  6. #include "primitives/transaction.h"
  7. #include "script/script.h"
  8. #include "script/standard.h"
  9. #include "serialize.h"
  10. #include "streams.h"
  11. #include <univalue.h>
  12. #include "util.h"
  13. #include "utilmoneystr.h"
  14. #include "utilstrencodings.h"
  15. #include <boost/assign/list_of.hpp>
  16. #include <boost/foreach.hpp>
  17. std::string FormatScript(const CScript& script)
  18. {
  19. std::string ret;
  20. CScript::const_iterator it = script.begin();
  21. opcodetype op;
  22. while (it != script.end()) {
  23. CScript::const_iterator it2 = it;
  24. std::vector<unsigned char> vch;
  25. if (script.GetOp2(it, op, &vch)) {
  26. if (op == OP_0) {
  27. ret += "0 ";
  28. continue;
  29. } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
  30. ret += strprintf("%i ", op - OP_1NEGATE - 1);
  31. continue;
  32. } else if (op >= OP_NOP && op <= OP_NOP10) {
  33. std::string str(GetOpName(op));
  34. if (str.substr(0, 3) == std::string("OP_")) {
  35. ret += str.substr(3, std::string::npos) + " ";
  36. continue;
  37. }
  38. }
  39. if (vch.size() > 0) {
  40. ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
  41. } else {
  42. ret += strprintf("0x%x ", HexStr(it2, it));
  43. }
  44. continue;
  45. }
  46. ret += strprintf("0x%x ", HexStr(it2, script.end()));
  47. break;
  48. }
  49. return ret.substr(0, ret.size() - 1);
  50. }
  51. const std::map<unsigned char, std::string> mapSigHashTypes =
  52. boost::assign::map_list_of
  53. (static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL"))
  54. (static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY"))
  55. (static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE"))
  56. (static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY"))
  57. (static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE"))
  58. (static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY"))
  59. ;
  60. /**
  61. * Create the assembly string representation of a CScript object.
  62. * @param[in] script CScript object to convert into the asm string representation.
  63. * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format
  64. * of a signature. Only pass true for scripts you believe could contain signatures. For example,
  65. * pass false, or omit the this argument (defaults to false), for scriptPubKeys.
  66. */
  67. std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
  68. {
  69. std::string str;
  70. opcodetype opcode;
  71. std::vector<unsigned char> vch;
  72. CScript::const_iterator pc = script.begin();
  73. while (pc < script.end()) {
  74. if (!str.empty()) {
  75. str += " ";
  76. }
  77. if (!script.GetOp(pc, opcode, vch)) {
  78. str += "[error]";
  79. return str;
  80. }
  81. if (0 <= opcode && opcode <= OP_PUSHDATA4) {
  82. if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
  83. str += strprintf("%d", CScriptNum(vch, false).getint());
  84. } else {
  85. // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
  86. if (fAttemptSighashDecode && !script.IsUnspendable()) {
  87. std::string strSigHashDecode;
  88. // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
  89. // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
  90. // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
  91. // checks in CheckSignatureEncoding.
  92. if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, NULL)) {
  93. const unsigned char chSigHashType = vch.back();
  94. if (mapSigHashTypes.count(chSigHashType)) {
  95. strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]";
  96. vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
  97. }
  98. }
  99. str += HexStr(vch) + strSigHashDecode;
  100. } else {
  101. str += HexStr(vch);
  102. }
  103. }
  104. } else {
  105. str += GetOpName(opcode);
  106. }
  107. }
  108. return str;
  109. }
  110. std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
  111. {
  112. CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags);
  113. ssTx << tx;
  114. return HexStr(ssTx.begin(), ssTx.end());
  115. }
  116. void ScriptPubKeyToUniv(const CScript& scriptPubKey,
  117. UniValue& out, bool fIncludeHex)
  118. {
  119. txnouttype type;
  120. std::vector<CTxDestination> addresses;
  121. int nRequired;
  122. out.pushKV("asm", ScriptToAsmStr(scriptPubKey));
  123. if (fIncludeHex)
  124. out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
  125. if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
  126. out.pushKV("type", GetTxnOutputType(type));
  127. return;
  128. }
  129. out.pushKV("reqSigs", nRequired);
  130. out.pushKV("type", GetTxnOutputType(type));
  131. UniValue a(UniValue::VARR);
  132. BOOST_FOREACH(const CTxDestination& addr, addresses)
  133. a.push_back(CBitcoinAddress(addr).ToString());
  134. out.pushKV("addresses", a);
  135. }
  136. void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
  137. {
  138. entry.pushKV("txid", tx.GetHash().GetHex());
  139. entry.pushKV("hash", tx.GetWitnessHash().GetHex());
  140. entry.pushKV("version", tx.nVersion);
  141. entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
  142. entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
  143. entry.pushKV("locktime", (int64_t)tx.nLockTime);
  144. UniValue vin(UniValue::VARR);
  145. for (unsigned int i = 0; i < tx.vin.size(); i++) {
  146. const CTxIn& txin = tx.vin[i];
  147. UniValue in(UniValue::VOBJ);
  148. if (tx.IsCoinBase())
  149. in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
  150. else {
  151. in.pushKV("txid", txin.prevout.hash.GetHex());
  152. in.pushKV("vout", (int64_t)txin.prevout.n);
  153. UniValue o(UniValue::VOBJ);
  154. o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
  155. o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
  156. in.pushKV("scriptSig", o);
  157. if (!tx.vin[i].scriptWitness.IsNull()) {
  158. UniValue txinwitness(UniValue::VARR);
  159. for (const auto& item : tx.vin[i].scriptWitness.stack) {
  160. txinwitness.push_back(HexStr(item.begin(), item.end()));
  161. }
  162. in.pushKV("txinwitness", txinwitness);
  163. }
  164. }
  165. in.pushKV("sequence", (int64_t)txin.nSequence);
  166. vin.push_back(in);
  167. }
  168. entry.pushKV("vin", vin);
  169. UniValue vout(UniValue::VARR);
  170. for (unsigned int i = 0; i < tx.vout.size(); i++) {
  171. const CTxOut& txout = tx.vout[i];
  172. UniValue out(UniValue::VOBJ);
  173. UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
  174. out.pushKV("value", outValue);
  175. out.pushKV("n", (int64_t)i);
  176. UniValue o(UniValue::VOBJ);
  177. ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
  178. out.pushKV("scriptPubKey", o);
  179. vout.push_back(out);
  180. }
  181. entry.pushKV("vout", vout);
  182. if (!hashBlock.IsNull())
  183. entry.pushKV("blockhash", hashBlock.GetHex());
  184. entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
  185. }