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.

transactiondesc.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Copyright (c) 2011-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 "transactiondesc.h"
  5. #include "bitcoinunits.h"
  6. #include "guiutil.h"
  7. #include "paymentserver.h"
  8. #include "transactionrecord.h"
  9. #include "base58.h"
  10. #include "db.h"
  11. #include "main.h"
  12. #include "script/script.h"
  13. #include "timedata.h"
  14. #include "ui_interface.h"
  15. #include "util.h"
  16. #include "wallet.h"
  17. #include <stdint.h>
  18. #include <string>
  19. using namespace std;
  20. QString TransactionDesc::FormatTxStatus(const CWalletTx& wtx)
  21. {
  22. AssertLockHeld(cs_main);
  23. if (!IsFinalTx(wtx, chainActive.Height() + 1))
  24. {
  25. if (wtx.nLockTime < LOCKTIME_THRESHOLD)
  26. return tr("Open for %n more block(s)", "", wtx.nLockTime - chainActive.Height());
  27. else
  28. return tr("Open until %1").arg(GUIUtil::dateTimeStr(wtx.nLockTime));
  29. }
  30. else
  31. {
  32. int nDepth = wtx.GetDepthInMainChain();
  33. if (nDepth < 0)
  34. return tr("conflicted");
  35. else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
  36. return tr("%1/offline").arg(nDepth);
  37. else if (nDepth < 6)
  38. return tr("%1/unconfirmed").arg(nDepth);
  39. else
  40. return tr("%1 confirmations").arg(nDepth);
  41. }
  42. }
  43. QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, TransactionRecord *rec, int unit)
  44. {
  45. QString strHTML;
  46. LOCK2(cs_main, wallet->cs_wallet);
  47. strHTML.reserve(4000);
  48. strHTML += "<html><font face='verdana, arial, helvetica, sans-serif'>";
  49. int64_t nTime = wtx.GetTxTime();
  50. int64_t nCredit = wtx.GetCredit(ISMINE_ALL);
  51. int64_t nDebit = wtx.GetDebit(ISMINE_ALL);
  52. int64_t nNet = nCredit - nDebit;
  53. strHTML += "<b>" + tr("Status") + ":</b> " + FormatTxStatus(wtx);
  54. int nRequests = wtx.GetRequestCount();
  55. if (nRequests != -1)
  56. {
  57. if (nRequests == 0)
  58. strHTML += tr(", has not been successfully broadcast yet");
  59. else if (nRequests > 0)
  60. strHTML += tr(", broadcast through %n node(s)", "", nRequests);
  61. }
  62. strHTML += "<br>";
  63. strHTML += "<b>" + tr("Date") + ":</b> " + (nTime ? GUIUtil::dateTimeStr(nTime) : "") + "<br>";
  64. //
  65. // From
  66. //
  67. if (wtx.IsCoinBase())
  68. {
  69. strHTML += "<b>" + tr("Source") + ":</b> " + tr("Generated") + "<br>";
  70. }
  71. else if (wtx.mapValue.count("from") && !wtx.mapValue["from"].empty())
  72. {
  73. // Online transaction
  74. strHTML += "<b>" + tr("From") + ":</b> " + GUIUtil::HtmlEscape(wtx.mapValue["from"]) + "<br>";
  75. }
  76. else
  77. {
  78. // Offline transaction
  79. if (nNet > 0)
  80. {
  81. // Credit
  82. if (CBitcoinAddress(rec->address).IsValid())
  83. {
  84. CTxDestination address = CBitcoinAddress(rec->address).Get();
  85. if (wallet->mapAddressBook.count(address))
  86. {
  87. strHTML += "<b>" + tr("From") + ":</b> " + tr("unknown") + "<br>";
  88. strHTML += "<b>" + tr("To") + ":</b> ";
  89. strHTML += GUIUtil::HtmlEscape(rec->address);
  90. QString addressOwned = (::IsMine(*wallet, address) == ISMINE_SPENDABLE) ? tr("own address") : tr("watch-only");
  91. if (!wallet->mapAddressBook[address].name.empty())
  92. strHTML += " (" + addressOwned + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + ")";
  93. else
  94. strHTML += " (" + addressOwned + ")";
  95. strHTML += "<br>";
  96. }
  97. }
  98. }
  99. }
  100. //
  101. // To
  102. //
  103. if (wtx.mapValue.count("to") && !wtx.mapValue["to"].empty())
  104. {
  105. // Online transaction
  106. std::string strAddress = wtx.mapValue["to"];
  107. strHTML += "<b>" + tr("To") + ":</b> ";
  108. CTxDestination dest = CBitcoinAddress(strAddress).Get();
  109. if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].name.empty())
  110. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest].name) + " ";
  111. strHTML += GUIUtil::HtmlEscape(strAddress) + "<br>";
  112. }
  113. //
  114. // Amount
  115. //
  116. if (wtx.IsCoinBase() && nCredit == 0)
  117. {
  118. //
  119. // Coinbase
  120. //
  121. int64_t nUnmatured = 0;
  122. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  123. nUnmatured += wallet->GetCredit(txout, ISMINE_ALL);
  124. strHTML += "<b>" + tr("Credit") + ":</b> ";
  125. if (wtx.IsInMainChain())
  126. strHTML += BitcoinUnits::formatHtmlWithUnit(unit, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", wtx.GetBlocksToMaturity()) + ")";
  127. else
  128. strHTML += "(" + tr("not accepted") + ")";
  129. strHTML += "<br>";
  130. }
  131. else if (nNet > 0)
  132. {
  133. //
  134. // Credit
  135. //
  136. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nNet) + "<br>";
  137. }
  138. else
  139. {
  140. isminetype fAllFromMe = ISMINE_SPENDABLE;
  141. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  142. {
  143. isminetype mine = wallet->IsMine(txin);
  144. if(fAllFromMe > mine) fAllFromMe = mine;
  145. }
  146. isminetype fAllToMe = ISMINE_SPENDABLE;
  147. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  148. {
  149. isminetype mine = wallet->IsMine(txout);
  150. if(fAllToMe > mine) fAllToMe = mine;
  151. }
  152. if (fAllFromMe)
  153. {
  154. if(fAllFromMe == ISMINE_WATCH_ONLY)
  155. strHTML += "<b>" + tr("From") + ":</b> " + tr("watch-only") + "<br>";
  156. //
  157. // Debit
  158. //
  159. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  160. {
  161. // Ignore change
  162. isminetype toSelf = wallet->IsMine(txout);
  163. if ((toSelf == ISMINE_SPENDABLE) && (fAllFromMe == ISMINE_SPENDABLE))
  164. continue;
  165. if (!wtx.mapValue.count("to") || wtx.mapValue["to"].empty())
  166. {
  167. // Offline transaction
  168. CTxDestination address;
  169. if (ExtractDestination(txout.scriptPubKey, address))
  170. {
  171. strHTML += "<b>" + tr("To") + ":</b> ";
  172. if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty())
  173. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " ";
  174. strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString());
  175. if(toSelf == ISMINE_SPENDABLE)
  176. strHTML += " (own address)";
  177. else if(toSelf == ISMINE_WATCH_ONLY)
  178. strHTML += " (watch-only)";
  179. strHTML += "<br>";
  180. }
  181. }
  182. strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -txout.nValue) + "<br>";
  183. if(toSelf)
  184. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, txout.nValue) + "<br>";
  185. }
  186. if (fAllToMe)
  187. {
  188. // Payment to self
  189. int64_t nChange = wtx.GetChange();
  190. int64_t nValue = nCredit - nChange;
  191. strHTML += "<b>" + tr("Total debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -nValue) + "<br>";
  192. strHTML += "<b>" + tr("Total credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nValue) + "<br>";
  193. }
  194. int64_t nTxFee = nDebit - wtx.GetValueOut();
  195. if (nTxFee > 0)
  196. strHTML += "<b>" + tr("Transaction fee") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -nTxFee) + "<br>";
  197. }
  198. else
  199. {
  200. //
  201. // Mixed debit transaction
  202. //
  203. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  204. if (wallet->IsMine(txin))
  205. strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -wallet->GetDebit(txin, ISMINE_ALL)) + "<br>";
  206. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  207. if (wallet->IsMine(txout))
  208. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, wallet->GetCredit(txout, ISMINE_ALL)) + "<br>";
  209. }
  210. }
  211. strHTML += "<b>" + tr("Net amount") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nNet, true) + "<br>";
  212. //
  213. // Message
  214. //
  215. if (wtx.mapValue.count("message") && !wtx.mapValue["message"].empty())
  216. strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.mapValue["message"], true) + "<br>";
  217. if (wtx.mapValue.count("comment") && !wtx.mapValue["comment"].empty())
  218. strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.mapValue["comment"], true) + "<br>";
  219. strHTML += "<b>" + tr("Transaction ID") + ":</b> " + TransactionRecord::formatSubTxId(wtx.GetHash(), rec->idx) + "<br>";
  220. // Message from normal bitcoin:URI (bitcoin:123...?message=example)
  221. foreach (const PAIRTYPE(string, string)& r, wtx.vOrderForm)
  222. if (r.first == "Message")
  223. strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(r.second, true) + "<br>";
  224. //
  225. // PaymentRequest info:
  226. //
  227. foreach (const PAIRTYPE(string, string)& r, wtx.vOrderForm)
  228. {
  229. if (r.first == "PaymentRequest")
  230. {
  231. PaymentRequestPlus req;
  232. req.parse(QByteArray::fromRawData(r.second.data(), r.second.size()));
  233. QString merchant;
  234. if (req.getMerchant(PaymentServer::getCertStore(), merchant))
  235. strHTML += "<b>" + tr("Merchant") + ":</b> " + GUIUtil::HtmlEscape(merchant) + "<br>";
  236. }
  237. }
  238. if (wtx.IsCoinBase())
  239. {
  240. quint32 numBlocksToMaturity = COINBASE_MATURITY + 1;
  241. strHTML += "<br>" + tr("Generated coins must mature %1 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \"not accepted\" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.").arg(QString::number(numBlocksToMaturity)) + "<br>";
  242. }
  243. //
  244. // Debug view
  245. //
  246. if (fDebug)
  247. {
  248. strHTML += "<hr><br>" + tr("Debug information") + "<br><br>";
  249. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  250. if(wallet->IsMine(txin))
  251. strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -wallet->GetDebit(txin, ISMINE_ALL)) + "<br>";
  252. BOOST_FOREACH(const CTxOut& txout, wtx.vout)
  253. if(wallet->IsMine(txout))
  254. strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, wallet->GetCredit(txout, ISMINE_ALL)) + "<br>";
  255. strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
  256. strHTML += GUIUtil::HtmlEscape(wtx.ToString(), true);
  257. strHTML += "<br><b>" + tr("Inputs") + ":</b>";
  258. strHTML += "<ul>";
  259. BOOST_FOREACH(const CTxIn& txin, wtx.vin)
  260. {
  261. COutPoint prevout = txin.prevout;
  262. CCoins prev;
  263. if(pcoinsTip->GetCoins(prevout.hash, prev))
  264. {
  265. if (prevout.n < prev.vout.size())
  266. {
  267. strHTML += "<li>";
  268. const CTxOut &vout = prev.vout[prevout.n];
  269. CTxDestination address;
  270. if (ExtractDestination(vout.scriptPubKey, address))
  271. {
  272. if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty())
  273. strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " ";
  274. strHTML += QString::fromStdString(CBitcoinAddress(address).ToString());
  275. }
  276. strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatHtmlWithUnit(unit, vout.nValue);
  277. strHTML = strHTML + " IsMine=" + (wallet->IsMine(vout) & ISMINE_SPENDABLE ? tr("true") : tr("false")) + "</li>";
  278. strHTML = strHTML + " IsWatchOnly=" + (wallet->IsMine(vout) & ISMINE_WATCH_ONLY ? tr("true") : tr("false")) + "</li>";
  279. }
  280. }
  281. }
  282. strHTML += "</ul>";
  283. }
  284. strHTML += "</font></html>";
  285. return strHTML;
  286. }