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 8.4KB

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