You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core_write.cpp 7.4KB

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