2018-01-02 18:12:05 +01:00
// Copyright (c) 2011-2017 The Bitcoin Core developers
2014-12-13 05:09:33 +01:00
// Distributed under the MIT software license, see the accompanying
2013-11-04 16:20:43 +01:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 01:57:53 +01:00
# include <qt/transactiondesc.h>
2011-06-10 15:05:51 +02:00
2017-11-10 01:57:53 +01:00
# include <qt/bitcoinunits.h>
# include <qt/guiutil.h>
# include <qt/paymentserver.h>
# include <qt/transactionrecord.h>
2013-04-13 07:13:08 +02:00
2017-11-10 01:57:53 +01:00
# include <base58.h>
# include <consensus/consensus.h>
# include <validation.h>
# include <script/script.h>
# include <timedata.h>
# include <util.h>
# include <wallet/db.h>
# include <wallet/wallet.h>
2011-06-10 15:05:51 +02:00
2013-04-13 07:13:08 +02:00
# include <stdint.h>
2013-01-23 21:51:02 +01:00
# include <string>
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : FormatTxStatus ( const CWalletTx & wtx )
2011-06-10 15:05:51 +02:00
{
2014-04-15 12:43:17 +02:00
AssertLockHeld ( cs_main ) ;
2017-05-09 08:46:26 +02:00
if ( ! CheckFinalTx ( * wtx . tx ) )
2011-06-10 15:05:51 +02:00
{
2016-11-12 01:54:51 +01:00
if ( wtx . tx - > nLockTime < LOCKTIME_THRESHOLD )
return tr ( " Open for %n more block(s) " , " " , wtx.tx->nLockTime - chainActive.Height()) ;
2011-06-10 15:05:51 +02:00
else
2016-11-12 01:54:51 +01:00
return tr ( " Open until %1 " ) . arg ( GUIUtil : : dateTimeStr ( wtx . tx - > nLockTime ) ) ;
2011-06-10 15:05:51 +02:00
}
else
{
int nDepth = wtx . GetDepthInMainChain ( ) ;
2014-02-12 19:43:07 +01:00
if ( nDepth < 0 )
2015-11-30 16:15:15 +01:00
return tr ( " conflicted with a transaction with %1 confirmations " ) . arg ( - nDepth ) ;
2014-02-12 19:43:07 +01:00
else if ( GetAdjustedTime ( ) - wtx . nTimeReceived > 2 * 60 & & wtx . GetRequestCount ( ) = = 0 )
2012-07-03 14:37:51 +02:00
return tr ( " %1/offline " ) . arg ( nDepth ) ;
2015-11-30 16:15:15 +01:00
else if ( nDepth = = 0 )
2016-03-17 17:54:54 +01:00
return tr ( " 0/unconfirmed, %1 " ) . arg ( ( wtx . InMempool ( ) ? tr ( " in memory pool " ) : tr ( " not in memory pool " ) ) ) + ( wtx . isAbandoned ( ) ? " , " + tr ( " abandoned " ) : " " ) ;
2011-06-10 15:05:51 +02:00
else if ( nDepth < 6 )
2011-08-08 17:38:17 +02:00
return tr ( " %1/unconfirmed " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
return tr ( " %1 confirmations " ) . arg ( nDepth ) ;
2011-06-10 15:05:51 +02:00
}
}
2014-05-28 01:38:40 +02:00
QString TransactionDesc : : toHTML ( CWallet * wallet , CWalletTx & wtx , TransactionRecord * rec , int unit )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
QString strHTML ;
2012-04-06 18:39:12 +02:00
2014-04-15 17:38:25 +02:00
LOCK2 ( cs_main , wallet - > cs_wallet ) ;
strHTML . reserve ( 4000 ) ;
strHTML + = " <html><font face='verdana, arial, helvetica, sans-serif'> " ;
int64_t nTime = wtx . GetTxTime ( ) ;
2014-04-23 00:46:19 +02:00
CAmount nCredit = wtx . GetCredit ( ISMINE_ALL ) ;
CAmount nDebit = wtx . GetDebit ( ISMINE_ALL ) ;
CAmount nNet = nCredit - nDebit ;
2014-04-15 17:38:25 +02:00
strHTML + = " <b> " + tr ( " Status " ) + " :</b> " + FormatTxStatus ( wtx ) ;
int nRequests = wtx . GetRequestCount ( ) ;
if ( nRequests ! = - 1 )
2011-06-10 15:05:51 +02:00
{
2014-04-15 17:38:25 +02:00
if ( nRequests = = 0 )
strHTML + = tr ( " , has not been successfully broadcast yet " ) ;
else if ( nRequests > 0 )
strHTML + = tr ( " , broadcast through %n node(s) " , " " , nRequests ) ;
}
strHTML + = " <br> " ;
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
strHTML + = " <b> " + tr ( " Date " ) + " :</b> " + ( nTime ? GUIUtil : : dateTimeStr ( nTime ) : " " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
//
// From
//
if ( wtx . IsCoinBase ( ) )
{
strHTML + = " <b> " + tr ( " Source " ) + " :</b> " + tr ( " Generated " ) + " <br> " ;
}
else if ( wtx . mapValue . count ( " from " ) & & ! wtx . mapValue [ " from " ] . empty ( ) )
{
// Online transaction
strHTML + = " <b> " + tr ( " From " ) + " :</b> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " from " ] ) + " <br> " ;
}
else
{
// Offline transaction
if ( nNet > 0 )
2011-06-10 15:05:51 +02:00
{
2014-04-15 17:38:25 +02:00
// Credit
2017-09-06 22:44:33 +02:00
CTxDestination address = DecodeDestination ( rec - > address ) ;
if ( IsValidDestination ( address ) ) {
2014-06-19 01:42:39 +02:00
if ( wallet - > mapAddressBook . count ( address ) )
2011-06-10 15:05:51 +02:00
{
2014-06-19 01:42:39 +02:00
strHTML + = " <b> " + tr ( " From " ) + " :</b> " + tr ( " unknown " ) + " <br> " ;
strHTML + = " <b> " + tr ( " To " ) + " :</b> " ;
strHTML + = GUIUtil : : HtmlEscape ( rec - > address ) ;
2014-07-01 11:00:22 +02:00
QString addressOwned = ( : : IsMine ( * wallet , address ) = = ISMINE_SPENDABLE ) ? tr ( " own address " ) : tr ( " watch-only " ) ;
2014-06-19 01:42:39 +02:00
if ( ! wallet - > mapAddressBook [ address ] . name . empty ( ) )
strHTML + = " ( " + addressOwned + " , " + tr ( " label " ) + " : " + GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] . name ) + " ) " ;
else
strHTML + = " ( " + addressOwned + " ) " ;
strHTML + = " <br> " ;
2011-06-10 15:05:51 +02:00
}
}
}
2014-04-15 17:38:25 +02:00
}
//
// To
//
if ( wtx . mapValue . count ( " to " ) & & ! wtx . mapValue [ " to " ] . empty ( ) )
{
// Online transaction
std : : string strAddress = wtx . mapValue [ " to " ] ;
strHTML + = " <b> " + tr ( " To " ) + " :</b> " ;
2017-08-23 03:02:33 +02:00
CTxDestination dest = DecodeDestination ( strAddress ) ;
2014-04-15 17:38:25 +02:00
if ( wallet - > mapAddressBook . count ( dest ) & & ! wallet - > mapAddressBook [ dest ] . name . empty ( ) )
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ dest ] . name ) + " " ;
strHTML + = GUIUtil : : HtmlEscape ( strAddress ) + " <br> " ;
}
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
//
// Amount
//
if ( wtx . IsCoinBase ( ) & & nCredit = = 0 )
{
2011-06-10 15:05:51 +02:00
//
2014-04-15 17:38:25 +02:00
// Coinbase
2011-06-10 15:05:51 +02:00
//
2014-04-23 00:46:19 +02:00
CAmount nUnmatured = 0 ;
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2014-07-01 11:00:22 +02:00
nUnmatured + = wallet - > GetCredit ( txout , ISMINE_ALL ) ;
2014-04-15 17:38:25 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " ;
if ( wtx . IsInMainChain ( ) )
2014-05-10 00:50:09 +02:00
strHTML + = BitcoinUnits : : formatHtmlWithUnit ( unit , nUnmatured ) + " ( " + tr ( " matures in %n more block(s) " , " " , wtx . GetBlocksToMaturity ( ) ) + " ) " ;
2014-04-15 17:38:25 +02:00
else
strHTML + = " ( " + tr ( " not accepted " ) + " ) " ;
strHTML + = " <br> " ;
}
else if ( nNet > 0 )
{
2011-06-10 15:05:51 +02:00
//
2014-04-15 17:38:25 +02:00
// Credit
2011-06-10 15:05:51 +02:00
//
2014-05-10 00:50:09 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , nNet ) + " <br> " ;
2014-04-15 17:38:25 +02:00
}
else
{
2014-07-01 11:00:22 +02:00
isminetype fAllFromMe = ISMINE_SPENDABLE ;
2017-06-02 03:18:57 +02:00
for ( const CTxIn & txin : wtx . tx - > vin )
2014-03-29 05:15:28 +01:00
{
isminetype mine = wallet - > IsMine ( txin ) ;
if ( fAllFromMe > mine ) fAllFromMe = mine ;
}
2014-04-15 17:38:25 +02:00
2014-07-01 11:00:22 +02:00
isminetype fAllToMe = ISMINE_SPENDABLE ;
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2014-03-29 05:15:28 +01:00
{
isminetype mine = wallet - > IsMine ( txout ) ;
if ( fAllToMe > mine ) fAllToMe = mine ;
}
2014-04-15 17:38:25 +02:00
if ( fAllFromMe )
2011-06-10 15:05:51 +02:00
{
2015-06-10 08:36:36 +02:00
if ( fAllFromMe & ISMINE_WATCH_ONLY )
2014-03-29 05:15:28 +01:00
strHTML + = " <b> " + tr ( " From " ) + " :</b> " + tr ( " watch-only " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
2014-04-15 17:38:25 +02:00
// Debit
2011-06-10 15:05:51 +02:00
//
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2011-06-10 15:05:51 +02:00
{
2014-03-29 05:15:28 +01:00
// Ignore change
isminetype toSelf = wallet - > IsMine ( txout ) ;
2014-07-01 11:00:22 +02:00
if ( ( toSelf = = ISMINE_SPENDABLE ) & & ( fAllFromMe = = ISMINE_SPENDABLE ) )
2014-04-15 17:38:25 +02:00
continue ;
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
if ( ! wtx . mapValue . count ( " to " ) | | wtx . mapValue [ " to " ] . empty ( ) )
{
// Offline transaction
CTxDestination address ;
if ( ExtractDestination ( txout . scriptPubKey , address ) )
2011-06-10 15:05:51 +02:00
{
2014-04-15 17:38:25 +02:00
strHTML + = " <b> " + tr ( " To " ) + " :</b> " ;
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . name . empty ( ) )
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] . name ) + " " ;
2017-08-23 03:02:33 +02:00
strHTML + = GUIUtil : : HtmlEscape ( EncodeDestination ( address ) ) ;
2014-07-01 11:00:22 +02:00
if ( toSelf = = ISMINE_SPENDABLE )
2014-03-29 05:15:28 +01:00
strHTML + = " (own address) " ;
2015-06-10 08:36:36 +02:00
else if ( toSelf & ISMINE_WATCH_ONLY )
2014-03-29 05:15:28 +01:00
strHTML + = " (watch-only) " ;
2014-04-15 17:38:25 +02:00
strHTML + = " <br> " ;
2011-06-10 15:05:51 +02:00
}
}
2014-05-10 00:50:09 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , - txout . nValue ) + " <br> " ;
2014-03-29 05:15:28 +01:00
if ( toSelf )
2014-07-07 23:06:21 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , txout . nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2014-04-15 17:38:25 +02:00
if ( fAllToMe )
2011-06-10 15:05:51 +02:00
{
2014-04-15 17:38:25 +02:00
// Payment to self
2014-04-23 00:46:19 +02:00
CAmount nChange = wtx . GetChange ( ) ;
CAmount nValue = nCredit - nChange ;
2014-07-07 23:06:21 +02:00
strHTML + = " <b> " + tr ( " Total debit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , - nValue ) + " <br> " ;
strHTML + = " <b> " + tr ( " Total credit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2014-04-15 17:38:25 +02:00
2016-11-12 01:54:51 +01:00
CAmount nTxFee = nDebit - wtx . tx - > GetValueOut ( ) ;
2014-04-15 17:38:25 +02:00
if ( nTxFee > 0 )
2014-05-10 00:50:09 +02:00
strHTML + = " <b> " + tr ( " Transaction fee " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , - nTxFee ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2014-04-15 17:38:25 +02:00
else
{
//
// Mixed debit transaction
//
2017-06-02 03:18:57 +02:00
for ( const CTxIn & txin : wtx . tx - > vin )
2014-04-15 17:38:25 +02:00
if ( wallet - > IsMine ( txin ) )
2014-07-07 23:06:21 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , - wallet - > GetDebit ( txin , ISMINE_ALL ) ) + " <br> " ;
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2014-04-15 17:38:25 +02:00
if ( wallet - > IsMine ( txout ) )
2014-07-07 23:06:21 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , wallet - > GetCredit ( txout , ISMINE_ALL ) ) + " <br> " ;
2014-04-15 17:38:25 +02:00
}
}
2011-06-10 15:05:51 +02:00
2014-05-10 00:50:09 +02:00
strHTML + = " <b> " + tr ( " Net amount " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , nNet , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
//
// Message
//
if ( wtx . mapValue . count ( " message " ) & & ! wtx . mapValue [ " message " ] . empty ( ) )
strHTML + = " <br><b> " + tr ( " Message " ) + " :</b><br> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " message " ] , true ) + " <br> " ;
if ( wtx . mapValue . count ( " comment " ) & & ! wtx . mapValue [ " comment " ] . empty ( ) )
strHTML + = " <br><b> " + tr ( " Comment " ) + " :</b><br> " + GUIUtil : : HtmlEscape ( wtx . mapValue [ " comment " ] , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
2016-03-29 11:17:47 +02:00
strHTML + = " <b> " + tr ( " Transaction ID " ) + " :</b> " + rec - > getTxID ( ) + " <br> " ;
2016-11-12 01:54:51 +01:00
strHTML + = " <b> " + tr ( " Transaction total size " ) + " :</b> " + QString : : number ( wtx . tx - > GetTotalSize ( ) ) + " bytes<br> " ;
2016-03-29 11:17:47 +02:00
strHTML + = " <b> " + tr ( " Output index " ) + " :</b> " + QString : : number ( rec - > getOutputIndex ( ) ) + " <br> " ;
2012-01-10 19:25:02 +01:00
2014-04-15 17:38:25 +02:00
// Message from normal bitcoin:URI (bitcoin:123...?message=example)
2017-06-02 03:28:42 +02:00
for ( const std : : pair < std : : string , std : : string > & r : wtx . vOrderForm )
2014-04-15 17:38:25 +02:00
if ( r . first = = " Message " )
strHTML + = " <br><b> " + tr ( " Message " ) + " :</b><br> " + GUIUtil : : HtmlEscape ( r . second , true ) + " <br> " ;
2014-01-21 23:39:29 +01:00
2014-04-15 17:38:25 +02:00
//
// PaymentRequest info:
//
2017-06-02 03:28:42 +02:00
for ( const std : : pair < std : : string , std : : string > & r : wtx . vOrderForm )
2014-04-15 17:38:25 +02:00
{
if ( r . first = = " PaymentRequest " )
2013-07-22 08:50:39 +02:00
{
2014-04-15 17:38:25 +02:00
PaymentRequestPlus req ;
req . parse ( QByteArray : : fromRawData ( r . second . data ( ) , r . second . size ( ) ) ) ;
QString merchant ;
if ( req . getMerchant ( PaymentServer : : getCertStore ( ) , merchant ) )
strHTML + = " <b> " + tr ( " Merchant " ) + " :</b> " + GUIUtil : : HtmlEscape ( merchant ) + " <br> " ;
2013-07-22 08:50:39 +02:00
}
2014-04-15 17:38:25 +02:00
}
2013-07-22 08:50:39 +02:00
2014-04-15 17:38:25 +02:00
if ( wtx . IsCoinBase ( ) )
{
quint32 numBlocksToMaturity = COINBASE_MATURITY + 1 ;
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> " ;
}
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
//
// Debug view
//
2016-12-25 21:19:40 +01:00
if ( logCategories ! = BCLog : : NONE )
2014-04-15 17:38:25 +02:00
{
strHTML + = " <hr><br> " + tr ( " Debug information " ) + " <br><br> " ;
2017-06-02 03:18:57 +02:00
for ( const CTxIn & txin : wtx . tx - > vin )
2014-04-15 17:38:25 +02:00
if ( wallet - > IsMine ( txin ) )
2014-07-07 23:06:21 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , - wallet - > GetDebit ( txin , ISMINE_ALL ) ) + " <br> " ;
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2014-04-15 17:38:25 +02:00
if ( wallet - > IsMine ( txout ) )
2014-07-07 23:06:21 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatHtmlWithUnit ( unit , wallet - > GetCredit ( txout , ISMINE_ALL ) ) + " <br> " ;
2014-04-15 17:38:25 +02:00
strHTML + = " <br><b> " + tr ( " Transaction " ) + " :</b><br> " ;
2016-11-12 01:54:51 +01:00
strHTML + = GUIUtil : : HtmlEscape ( wtx . tx - > ToString ( ) , true ) ;
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
strHTML + = " <br><b> " + tr ( " Inputs " ) + " :</b> " ;
strHTML + = " <ul> " ;
2011-06-10 15:05:51 +02:00
2017-06-02 03:18:57 +02:00
for ( const CTxIn & txin : wtx . tx - > vin )
2014-04-15 17:38:25 +02:00
{
COutPoint prevout = txin . prevout ;
2012-04-06 18:39:12 +02:00
2017-04-25 20:29:39 +02:00
Coin prev ;
2017-05-31 02:58:54 +02:00
if ( pcoinsTip - > GetCoin ( prevout , prev ) )
2011-06-10 15:05:51 +02:00
{
{
2014-04-15 17:38:25 +02:00
strHTML + = " <li> " ;
2017-04-25 20:29:39 +02:00
const CTxOut & vout = prev . out ;
2014-04-15 17:38:25 +02:00
CTxDestination address ;
if ( ExtractDestination ( vout . scriptPubKey , address ) )
2011-06-10 15:05:51 +02:00
{
2014-04-15 17:38:25 +02:00
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . name . empty ( ) )
strHTML + = GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] . name ) + " " ;
2017-08-23 03:02:33 +02:00
strHTML + = QString : : fromStdString ( EncodeDestination ( address ) ) ;
2011-06-10 15:05:51 +02:00
}
2014-05-10 00:50:09 +02:00
strHTML = strHTML + " " + tr ( " Amount " ) + " = " + BitcoinUnits : : formatHtmlWithUnit ( unit , vout . nValue ) ;
2014-07-01 11:00:22 +02:00
strHTML = strHTML + " IsMine= " + ( wallet - > IsMine ( vout ) & ISMINE_SPENDABLE ? tr ( " true " ) : tr ( " false " ) ) + " </li> " ;
strHTML = strHTML + " IsWatchOnly= " + ( wallet - > IsMine ( vout ) & ISMINE_WATCH_ONLY ? tr ( " true " ) : tr ( " false " ) ) + " </li> " ;
2011-06-10 15:05:51 +02:00
}
}
}
2014-04-15 17:38:25 +02:00
strHTML + = " </ul> " ;
2011-06-10 15:05:51 +02:00
}
2014-04-15 17:38:25 +02:00
strHTML + = " </font></html> " ;
2011-06-10 15:05:51 +02:00
return strHTML ;
}