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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2009-2014 The Bitcoin developers
  2. // Distributed under the MIT/X11 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 "core.h"
  7. #include "script/script.h"
  8. #include "script/standard.h"
  9. #include "serialize.h"
  10. #include "univalue/univalue.h"
  11. #include "util.h"
  12. #include "utilmoneystr.h"
  13. #include "utilstrencodings.h"
  14. #include <boost/foreach.hpp>
  15. using namespace std;
  16. string FormatScript(const CScript& script)
  17. {
  18. string ret;
  19. CScript::const_iterator it = script.begin();
  20. opcodetype op;
  21. while (it != script.end()) {
  22. CScript::const_iterator it2 = it;
  23. vector<unsigned char> vch;
  24. if (script.GetOp2(it, op, &vch)) {
  25. if (op == OP_0) {
  26. ret += "0 ";
  27. continue;
  28. } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
  29. ret += strprintf("%i ", op - OP_1NEGATE - 1);
  30. continue;
  31. } else if (op >= OP_NOP && op <= OP_CHECKMULTISIGVERIFY) {
  32. string str(GetOpName(op));
  33. if (str.substr(0, 3) == string("OP_")) {
  34. ret += str.substr(3, string::npos) + " ";
  35. continue;
  36. }
  37. }
  38. if (vch.size() > 0) {
  39. ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
  40. } else {
  41. ret += strprintf("0x%x", HexStr(it2, it));
  42. }
  43. continue;
  44. }
  45. ret += strprintf("0x%x ", HexStr(it2, script.end()));
  46. break;
  47. }
  48. return ret.substr(0, ret.size() - 1);
  49. }
  50. string EncodeHexTx(const CTransaction& tx)
  51. {
  52. CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
  53. ssTx << tx;
  54. return HexStr(ssTx.begin(), ssTx.end());
  55. }
  56. void ScriptPubKeyToUniv(const CScript& scriptPubKey,
  57. UniValue& out, bool fIncludeHex)
  58. {
  59. txnouttype type;
  60. vector<CTxDestination> addresses;
  61. int nRequired;
  62. out.pushKV("asm", scriptPubKey.ToString());
  63. if (fIncludeHex)
  64. out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
  65. if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
  66. out.pushKV("type", GetTxnOutputType(type));
  67. return;
  68. }
  69. out.pushKV("reqSigs", nRequired);
  70. out.pushKV("type", GetTxnOutputType(type));
  71. UniValue a(UniValue::VARR);
  72. BOOST_FOREACH(const CTxDestination& addr, addresses)
  73. a.push_back(CBitcoinAddress(addr).ToString());
  74. out.pushKV("addresses", a);
  75. }
  76. void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
  77. {
  78. entry.pushKV("txid", tx.GetHash().GetHex());
  79. entry.pushKV("version", tx.nVersion);
  80. entry.pushKV("locktime", (int64_t)tx.nLockTime);
  81. UniValue vin(UniValue::VARR);
  82. BOOST_FOREACH(const CTxIn& txin, tx.vin) {
  83. UniValue in(UniValue::VOBJ);
  84. if (tx.IsCoinBase())
  85. in.pushKV("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
  86. else {
  87. in.pushKV("txid", txin.prevout.hash.GetHex());
  88. in.pushKV("vout", (int64_t)txin.prevout.n);
  89. UniValue o(UniValue::VOBJ);
  90. o.pushKV("asm", txin.scriptSig.ToString());
  91. o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
  92. in.pushKV("scriptSig", o);
  93. }
  94. in.pushKV("sequence", (int64_t)txin.nSequence);
  95. vin.push_back(in);
  96. }
  97. entry.pushKV("vin", vin);
  98. UniValue vout(UniValue::VARR);
  99. for (unsigned int i = 0; i < tx.vout.size(); i++) {
  100. const CTxOut& txout = tx.vout[i];
  101. UniValue out(UniValue::VOBJ);
  102. UniValue outValue(UniValue::VNUM, FormatMoney(txout.nValue));
  103. out.pushKV("value", outValue);
  104. out.pushKV("n", (int64_t)i);
  105. UniValue o(UniValue::VOBJ);
  106. ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
  107. out.pushKV("scriptPubKey", o);
  108. vout.push_back(out);
  109. }
  110. entry.pushKV("vout", vout);
  111. if (hashBlock != 0)
  112. entry.pushKV("blockhash", hashBlock.GetHex());
  113. }