Line data Source code
1 : // Copyright (c) 2011-2019 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 : #ifdef HAVE_CONFIG_H
6 : #include <config/bitcoin-config.h>
7 : #endif
8 :
9 : #include <qt/transactiondesc.h>
10 :
11 : #include <qt/bitcoinunits.h>
12 : #include <qt/guiutil.h>
13 : #include <qt/paymentserver.h>
14 : #include <qt/transactionrecord.h>
15 :
16 : #include <consensus/consensus.h>
17 : #include <interfaces/node.h>
18 : #include <interfaces/wallet.h>
19 : #include <key_io.h>
20 : #include <policy/policy.h>
21 : #include <script/script.h>
22 : #include <util/system.h>
23 : #include <validation.h>
24 : #include <wallet/ismine.h>
25 :
26 : #include <stdint.h>
27 : #include <string>
28 :
29 0 : QString TransactionDesc::FormatTxStatus(const interfaces::WalletTx& wtx, const interfaces::WalletTxStatus& status, bool inMempool, int numBlocks)
30 : {
31 0 : if (!status.is_final)
32 : {
33 0 : if (wtx.tx->nLockTime < LOCKTIME_THRESHOLD)
34 0 : return tr("Open for %n more block(s)", "", wtx.tx->nLockTime - numBlocks);
35 : else
36 0 : return tr("Open until %1").arg(GUIUtil::dateTimeStr(wtx.tx->nLockTime));
37 : }
38 : else
39 : {
40 0 : int nDepth = status.depth_in_main_chain;
41 0 : if (nDepth < 0)
42 0 : return tr("conflicted with a transaction with %1 confirmations").arg(-nDepth);
43 0 : else if (nDepth == 0)
44 0 : return tr("0/unconfirmed, %1").arg((inMempool ? tr("in memory pool") : tr("not in memory pool"))) + (status.is_abandoned ? ", "+tr("abandoned") : "");
45 0 : else if (nDepth < 6)
46 0 : return tr("%1/unconfirmed").arg(nDepth);
47 : else
48 0 : return tr("%1 confirmations").arg(nDepth);
49 0 : }
50 0 : }
51 :
52 : // Takes an encoded PaymentRequest as a string and tries to find the Common Name of the X.509 certificate
53 : // used to sign the PaymentRequest.
54 0 : bool GetPaymentRequestMerchant(const std::string& pr, QString& merchant)
55 : {
56 : // Search for the supported pki type strings
57 0 : if (pr.find(std::string({0x12, 0x0b}) + "x509+sha256") != std::string::npos || pr.find(std::string({0x12, 0x09}) + "x509+sha1") != std::string::npos) {
58 : // We want the common name of the Subject of the cert. This should be the second occurrence
59 : // of the bytes 0x0603550403. The first occurrence of those is the common name of the issuer.
60 : // After those bytes will be either 0x13 or 0x0C, then length, then either the ascii or utf8
61 : // string with the common name which is the merchant name
62 0 : size_t cn_pos = pr.find({0x06, 0x03, 0x55, 0x04, 0x03});
63 0 : if (cn_pos != std::string::npos) {
64 0 : cn_pos = pr.find({0x06, 0x03, 0x55, 0x04, 0x03}, cn_pos + 5);
65 0 : if (cn_pos != std::string::npos) {
66 0 : cn_pos += 5;
67 0 : if (pr[cn_pos] == 0x13 || pr[cn_pos] == 0x0c) {
68 0 : cn_pos++; // Consume the type
69 0 : int str_len = pr[cn_pos];
70 0 : cn_pos++; // Consume the string length
71 0 : merchant = QString::fromUtf8(pr.data() + cn_pos, str_len);
72 : return true;
73 : }
74 : }
75 : }
76 0 : }
77 0 : return false;
78 0 : }
79 :
80 0 : QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wallet, TransactionRecord *rec, int unit)
81 : {
82 0 : int numBlocks;
83 0 : interfaces::WalletTxStatus status;
84 0 : interfaces::WalletOrderForm orderForm;
85 0 : bool inMempool;
86 0 : interfaces::WalletTx wtx = wallet.getWalletTxDetails(rec->hash, status, orderForm, inMempool, numBlocks);
87 :
88 0 : QString strHTML;
89 :
90 0 : strHTML.reserve(4000);
91 0 : strHTML += "<html><font face='verdana, arial, helvetica, sans-serif'>";
92 :
93 0 : int64_t nTime = wtx.time;
94 0 : CAmount nCredit = wtx.credit;
95 0 : CAmount nDebit = wtx.debit;
96 0 : CAmount nNet = nCredit - nDebit;
97 :
98 0 : strHTML += "<b>" + tr("Status") + ":</b> " + FormatTxStatus(wtx, status, inMempool, numBlocks);
99 0 : strHTML += "<br>";
100 :
101 0 : strHTML += "<b>" + tr("Date") + ":</b> " + (nTime ? GUIUtil::dateTimeStr(nTime) : "") + "<br>";
102 :
103 : //
104 : // From
105 : //
106 0 : if (wtx.is_coinbase)
107 : {
108 0 : strHTML += "<b>" + tr("Source") + ":</b> " + tr("Generated") + "<br>";
109 0 : }
110 0 : else if (wtx.value_map.count("from") && !wtx.value_map["from"].empty())
111 : {
112 : // Online transaction
113 0 : strHTML += "<b>" + tr("From") + ":</b> " + GUIUtil::HtmlEscape(wtx.value_map["from"]) + "<br>";
114 0 : }
115 : else
116 : {
117 : // Offline transaction
118 0 : if (nNet > 0)
119 : {
120 : // Credit
121 0 : CTxDestination address = DecodeDestination(rec->address);
122 0 : if (IsValidDestination(address)) {
123 0 : std::string name;
124 0 : isminetype ismine;
125 0 : if (wallet.getAddress(address, &name, &ismine, /* purpose= */ nullptr))
126 : {
127 0 : strHTML += "<b>" + tr("From") + ":</b> " + tr("unknown") + "<br>";
128 0 : strHTML += "<b>" + tr("To") + ":</b> ";
129 0 : strHTML += GUIUtil::HtmlEscape(rec->address);
130 0 : QString addressOwned = ismine == ISMINE_SPENDABLE ? tr("own address") : tr("watch-only");
131 0 : if (!name.empty())
132 0 : strHTML += " (" + addressOwned + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(name) + ")";
133 : else
134 0 : strHTML += " (" + addressOwned + ")";
135 0 : strHTML += "<br>";
136 0 : }
137 0 : }
138 0 : }
139 : }
140 :
141 : //
142 : // To
143 : //
144 0 : if (wtx.value_map.count("to") && !wtx.value_map["to"].empty())
145 : {
146 : // Online transaction
147 0 : std::string strAddress = wtx.value_map["to"];
148 0 : strHTML += "<b>" + tr("To") + ":</b> ";
149 0 : CTxDestination dest = DecodeDestination(strAddress);
150 0 : std::string name;
151 0 : if (wallet.getAddress(
152 0 : dest, &name, /* is_mine= */ nullptr, /* purpose= */ nullptr) && !name.empty())
153 0 : strHTML += GUIUtil::HtmlEscape(name) + " ";
154 0 : strHTML += GUIUtil::HtmlEscape(strAddress) + "<br>";
155 0 : }
156 :
157 : //
158 : // Amount
159 : //
160 0 : if (wtx.is_coinbase && nCredit == 0)
161 : {
162 : //
163 : // Coinbase
164 : //
165 0 : CAmount nUnmatured = 0;
166 0 : for (const CTxOut& txout : wtx.tx->vout)
167 0 : nUnmatured += wallet.getCredit(txout, ISMINE_ALL);
168 0 : strHTML += "<b>" + tr("Credit") + ":</b> ";
169 0 : if (status.is_in_main_chain)
170 0 : strHTML += BitcoinUnits::formatHtmlWithUnit(unit, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", status.blocks_to_maturity) + ")";
171 : else
172 0 : strHTML += "(" + tr("not accepted") + ")";
173 0 : strHTML += "<br>";
174 0 : }
175 0 : else if (nNet > 0)
176 : {
177 : //
178 : // Credit
179 : //
180 0 : strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nNet) + "<br>";
181 0 : }
182 : else
183 : {
184 : isminetype fAllFromMe = ISMINE_SPENDABLE;
185 0 : for (const isminetype mine : wtx.txin_is_mine)
186 : {
187 0 : if(fAllFromMe > mine) fAllFromMe = mine;
188 : }
189 :
190 : isminetype fAllToMe = ISMINE_SPENDABLE;
191 0 : for (const isminetype mine : wtx.txout_is_mine)
192 : {
193 0 : if(fAllToMe > mine) fAllToMe = mine;
194 : }
195 :
196 0 : if (fAllFromMe)
197 : {
198 0 : if(fAllFromMe & ISMINE_WATCH_ONLY)
199 0 : strHTML += "<b>" + tr("From") + ":</b> " + tr("watch-only") + "<br>";
200 :
201 : //
202 : // Debit
203 : //
204 0 : auto mine = wtx.txout_is_mine.begin();
205 0 : for (const CTxOut& txout : wtx.tx->vout)
206 : {
207 : // Ignore change
208 0 : isminetype toSelf = *(mine++);
209 0 : if ((toSelf == ISMINE_SPENDABLE) && (fAllFromMe == ISMINE_SPENDABLE))
210 0 : continue;
211 :
212 0 : if (!wtx.value_map.count("to") || wtx.value_map["to"].empty())
213 : {
214 : // Offline transaction
215 0 : CTxDestination address;
216 0 : if (ExtractDestination(txout.scriptPubKey, address))
217 : {
218 0 : strHTML += "<b>" + tr("To") + ":</b> ";
219 0 : std::string name;
220 0 : if (wallet.getAddress(
221 0 : address, &name, /* is_mine= */ nullptr, /* purpose= */ nullptr) && !name.empty())
222 0 : strHTML += GUIUtil::HtmlEscape(name) + " ";
223 0 : strHTML += GUIUtil::HtmlEscape(EncodeDestination(address));
224 0 : if(toSelf == ISMINE_SPENDABLE)
225 0 : strHTML += " (own address)";
226 0 : else if(toSelf & ISMINE_WATCH_ONLY)
227 0 : strHTML += " (watch-only)";
228 0 : strHTML += "<br>";
229 0 : }
230 0 : }
231 :
232 0 : strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -txout.nValue) + "<br>";
233 0 : if(toSelf)
234 0 : strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, txout.nValue) + "<br>";
235 0 : }
236 :
237 0 : if (fAllToMe)
238 : {
239 : // Payment to self
240 0 : CAmount nChange = wtx.change;
241 0 : CAmount nValue = nCredit - nChange;
242 0 : strHTML += "<b>" + tr("Total debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -nValue) + "<br>";
243 0 : strHTML += "<b>" + tr("Total credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nValue) + "<br>";
244 0 : }
245 :
246 0 : CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
247 0 : if (nTxFee > 0)
248 0 : strHTML += "<b>" + tr("Transaction fee") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -nTxFee) + "<br>";
249 0 : }
250 : else
251 : {
252 : //
253 : // Mixed debit transaction
254 : //
255 0 : auto mine = wtx.txin_is_mine.begin();
256 0 : for (const CTxIn& txin : wtx.tx->vin) {
257 0 : if (*(mine++)) {
258 0 : strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -wallet.getDebit(txin, ISMINE_ALL)) + "<br>";
259 0 : }
260 : }
261 0 : mine = wtx.txout_is_mine.begin();
262 0 : for (const CTxOut& txout : wtx.tx->vout) {
263 0 : if (*(mine++)) {
264 0 : strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, wallet.getCredit(txout, ISMINE_ALL)) + "<br>";
265 0 : }
266 : }
267 0 : }
268 0 : }
269 :
270 0 : strHTML += "<b>" + tr("Net amount") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nNet, true) + "<br>";
271 :
272 : //
273 : // Message
274 : //
275 0 : if (wtx.value_map.count("message") && !wtx.value_map["message"].empty())
276 0 : strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["message"], true) + "<br>";
277 0 : if (wtx.value_map.count("comment") && !wtx.value_map["comment"].empty())
278 0 : strHTML += "<br><b>" + tr("Comment") + ":</b><br>" + GUIUtil::HtmlEscape(wtx.value_map["comment"], true) + "<br>";
279 :
280 0 : strHTML += "<b>" + tr("Transaction ID") + ":</b> " + rec->getTxHash() + "<br>";
281 0 : strHTML += "<b>" + tr("Transaction total size") + ":</b> " + QString::number(wtx.tx->GetTotalSize()) + " bytes<br>";
282 0 : strHTML += "<b>" + tr("Transaction virtual size") + ":</b> " + QString::number(GetVirtualTransactionSize(*wtx.tx)) + " bytes<br>";
283 0 : strHTML += "<b>" + tr("Output index") + ":</b> " + QString::number(rec->getOutputIndex()) + "<br>";
284 :
285 : // Message from normal bitcoin:URI (bitcoin:123...?message=example)
286 0 : for (const std::pair<std::string, std::string>& r : orderForm) {
287 0 : if (r.first == "Message")
288 0 : strHTML += "<br><b>" + tr("Message") + ":</b><br>" + GUIUtil::HtmlEscape(r.second, true) + "<br>";
289 :
290 : //
291 : // PaymentRequest info:
292 : //
293 0 : if (r.first == "PaymentRequest")
294 : {
295 0 : QString merchant;
296 0 : if (!GetPaymentRequestMerchant(r.second, merchant)) {
297 0 : merchant.clear();
298 : } else {
299 0 : merchant += tr(" (Certificate was not verified)");
300 : }
301 0 : if (!merchant.isNull()) {
302 0 : strHTML += "<b>" + tr("Merchant") + ":</b> " + GUIUtil::HtmlEscape(merchant) + "<br>";
303 0 : }
304 0 : }
305 : }
306 :
307 0 : if (wtx.is_coinbase)
308 : {
309 : quint32 numBlocksToMaturity = COINBASE_MATURITY + 1;
310 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>";
311 0 : }
312 :
313 : //
314 : // Debug view
315 : //
316 0 : if (node.getLogCategories() != BCLog::NONE)
317 : {
318 0 : strHTML += "<hr><br>" + tr("Debug information") + "<br><br>";
319 0 : for (const CTxIn& txin : wtx.tx->vin)
320 0 : if(wallet.txinIsMine(txin))
321 0 : strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -wallet.getDebit(txin, ISMINE_ALL)) + "<br>";
322 0 : for (const CTxOut& txout : wtx.tx->vout)
323 0 : if(wallet.txoutIsMine(txout))
324 0 : strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, wallet.getCredit(txout, ISMINE_ALL)) + "<br>";
325 :
326 0 : strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
327 0 : strHTML += GUIUtil::HtmlEscape(wtx.tx->ToString(), true);
328 :
329 0 : strHTML += "<br><b>" + tr("Inputs") + ":</b>";
330 0 : strHTML += "<ul>";
331 :
332 0 : for (const CTxIn& txin : wtx.tx->vin)
333 : {
334 0 : COutPoint prevout = txin.prevout;
335 :
336 0 : Coin prev;
337 0 : if(node.getUnspentOutput(prevout, prev))
338 : {
339 : {
340 0 : strHTML += "<li>";
341 0 : const CTxOut &vout = prev.out;
342 0 : CTxDestination address;
343 0 : if (ExtractDestination(vout.scriptPubKey, address))
344 : {
345 0 : std::string name;
346 0 : if (wallet.getAddress(address, &name, /* is_mine= */ nullptr, /* purpose= */ nullptr) && !name.empty())
347 0 : strHTML += GUIUtil::HtmlEscape(name) + " ";
348 0 : strHTML += QString::fromStdString(EncodeDestination(address));
349 0 : }
350 0 : strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatHtmlWithUnit(unit, vout.nValue);
351 0 : strHTML = strHTML + " IsMine=" + (wallet.txoutIsMine(vout) & ISMINE_SPENDABLE ? tr("true") : tr("false")) + "</li>";
352 0 : strHTML = strHTML + " IsWatchOnly=" + (wallet.txoutIsMine(vout) & ISMINE_WATCH_ONLY ? tr("true") : tr("false")) + "</li>";
353 0 : }
354 0 : }
355 0 : }
356 :
357 0 : strHTML += "</ul>";
358 : }
359 :
360 0 : strHTML += "</font></html>";
361 : return strHTML;
362 0 : }
|