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