2011-06-10 15:05:51 +02:00
# include <transactiondesc.h>
# include "guiutil.h"
2011-08-08 17:38:17 +02:00
# include "bitcoinunits.h"
2011-07-03 20:53:56 +02:00
# include "headers.h"
2011-06-25 23:14:10 +02:00
# include "qtui.h"
2011-06-10 15:05:51 +02:00
# include <QString>
2011-08-08 17:38:17 +02:00
# include <QTextDocument> // For Qt::escape
2011-06-10 15:05:51 +02:00
using namespace std ;
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : HtmlEscape ( const QString & str , bool fMultiLine )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
QString escaped = Qt : : escape ( str ) ;
if ( fMultiLine )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
escaped = escaped . replace ( " \n " , " <br> \n " ) ;
2011-06-10 15:05:51 +02:00
}
2011-08-08 17:38:17 +02:00
return escaped ;
2011-06-10 15:05:51 +02:00
}
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : HtmlEscape ( const std : : string & str , bool fMultiLine )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
return HtmlEscape ( QString : : fromStdString ( str ) , fMultiLine ) ;
2011-06-10 15:05:51 +02:00
}
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : FormatTxStatus ( const CWalletTx & wtx )
2011-06-10 15:05:51 +02:00
{
if ( ! wtx . IsFinal ( ) )
{
2011-08-08 17:38:17 +02:00
if ( wtx . nLockTime < LOCKTIME_THRESHOLD )
return tr ( " Open for %1 blocks " ) . arg ( nBestHeight - wtx . nLockTime ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
return tr ( " Open until %1 " ) . arg ( GUIUtil : : dateTimeStr ( wtx . nLockTime ) ) ;
2011-06-10 15:05:51 +02:00
}
else
{
int nDepth = wtx . GetDepthInMainChain ( ) ;
if ( GetAdjustedTime ( ) - wtx . nTimeReceived > 2 * 60 & & wtx . GetRequestCount ( ) = = 0 )
2011-08-08 17:38:17 +02:00
return tr ( " %1/offline? " ) . arg ( nDepth ) ;
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
}
}
2011-08-08 17:38:17 +02:00
QString TransactionDesc : : toHTML ( CWallet * wallet , CWalletTx & wtx )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
QString strHTML ;
2011-09-02 18:02:22 +02:00
CRITICAL_BLOCK ( wallet - > cs_wallet )
2011-06-10 15:05:51 +02:00
{
strHTML . reserve ( 4000 ) ;
strHTML + = " <html><font face='verdana, arial, helvetica, sans-serif'> " ;
2011-12-21 22:33:19 +01:00
int64 nTime = wtx . GetTxTime ( ) ;
int64 nCredit = wtx . GetCredit ( ) ;
int64 nDebit = wtx . GetDebit ( ) ;
int64 nNet = nCredit - nDebit ;
2011-06-10 15:05:51 +02:00
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Status:</b> " ) + FormatTxStatus ( wtx ) ;
2011-06-10 15:05:51 +02:00
int nRequests = wtx . GetRequestCount ( ) ;
if ( nRequests ! = - 1 )
{
if ( nRequests = = 0 )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " , has not been successfully broadcast yet " ) ;
2011-06-10 15:05:51 +02:00
else if ( nRequests = = 1 )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " , broadcast through %1 node " ) . arg ( nRequests ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " , broadcast through %1 nodes " ) . arg ( nRequests ) ;
2011-06-10 15:05:51 +02:00
}
strHTML + = " <br> " ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Date:</b> " ) + ( nTime ? GUIUtil : : dateTimeStr ( nTime ) : QString ( " " ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// From
//
if ( wtx . IsCoinBase ( ) )
{
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Source:</b> Generated<br> " ) ;
2011-06-10 15:05:51 +02:00
}
else if ( ! wtx . mapValue [ " from " ] . empty ( ) )
{
// Online transaction
if ( ! wtx . mapValue [ " from " ] . empty ( ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>From:</b> " ) + HtmlEscape ( wtx . mapValue [ " from " ] ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
// Offline transaction
if ( nNet > 0 )
{
// Credit
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-06-10 15:05:51 +02:00
{
2011-07-26 15:38:31 +02:00
CBitcoinAddress address ;
2011-12-22 21:51:44 +01:00
if ( ExtractAddress ( txout . scriptPubKey , address ) & & wallet - > HaveKey ( address ) )
2011-06-10 15:05:51 +02:00
{
2011-07-26 15:38:31 +02:00
if ( wallet - > mapAddressBook . count ( address ) )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>From:</b> " ) + tr ( " unknown " ) + " <br> " ;
strHTML + = tr ( " <b>To:</b> " ) ;
2011-07-26 15:38:31 +02:00
strHTML + = HtmlEscape ( address . ToString ( ) ) ;
if ( ! wallet - > mapAddressBook [ address ] . empty ( ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " (yours, label: " ) + HtmlEscape ( wallet - > mapAddressBook [ address ] ) + " ) " ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " (yours) " ) ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
}
break ;
}
}
}
}
//
// To
//
string strAddress ;
if ( ! wtx . mapValue [ " to " ] . empty ( ) )
{
// Online transaction
strAddress = wtx . mapValue [ " to " ] ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>To:</b> " ) ;
2011-06-26 19:23:24 +02:00
if ( wallet - > mapAddressBook . count ( strAddress ) & & ! wallet - > mapAddressBook [ strAddress ] . empty ( ) )
2011-07-31 12:56:46 +02:00
strHTML + = HtmlEscape ( wallet - > mapAddressBook [ strAddress ] ) + " " ;
2011-06-10 15:05:51 +02:00
strHTML + = HtmlEscape ( strAddress ) + " <br> " ;
}
//
// Amount
//
if ( wtx . IsCoinBase ( ) & & nCredit = = 0 )
{
//
// Coinbase
//
2011-12-21 22:33:19 +01:00
int64 nUnmatured = 0 ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
nUnmatured + = wallet - > GetCredit ( txout ) ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Credit:</b> " ) ;
2011-06-10 15:05:51 +02:00
if ( wtx . IsInMainChain ( ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " (%1 matures in %2 more blocks) " )
. arg ( BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nUnmatured ) )
. arg ( wtx . GetBlocksToMaturity ( ) ) ;
2011-06-10 15:05:51 +02:00
else
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " (not accepted) " ) ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
else if ( nNet > 0 )
{
//
// Credit
//
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Credit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nNet ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
bool fAllFromMe = true ;
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
fAllFromMe = fAllFromMe & & wallet - > IsMine ( txin ) ;
2011-06-10 15:05:51 +02:00
bool fAllToMe = true ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
fAllToMe = fAllToMe & & wallet - > IsMine ( txout ) ;
2011-06-10 15:05:51 +02:00
if ( fAllFromMe )
{
//
// Debit
//
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-06-10 15:05:51 +02:00
continue ;
if ( wtx . mapValue [ " to " ] . empty ( ) )
{
// Offline transaction
2011-07-26 15:38:31 +02:00
CBitcoinAddress address ;
2011-12-22 21:51:44 +01:00
if ( ExtractAddress ( txout . scriptPubKey , address ) )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>To:</b> " ) ;
2011-07-26 15:38:31 +02:00
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . empty ( ) )
2011-07-31 12:56:46 +02:00
strHTML + = HtmlEscape ( wallet - > mapAddressBook [ address ] ) + " " ;
strHTML + = HtmlEscape ( address . ToString ( ) ) ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br> " ;
}
}
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Debit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - txout . nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
if ( fAllToMe )
{
// Payment to self
2011-12-21 22:33:19 +01:00
int64 nChange = wtx . GetChange ( ) ;
int64 nValue = nCredit - nChange ;
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Debit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - nValue ) + " <br> " ;
strHTML + = tr ( " <b>Credit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2011-12-21 22:33:19 +01:00
int64 nTxFee = nDebit - wtx . GetValueOut ( ) ;
2011-06-10 15:05:51 +02:00
if ( nTxFee > 0 )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Transaction fee:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - nTxFee ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
else
{
//
// Mixed debit transaction
//
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txin ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Debit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - wallet - > GetDebit ( txin ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Credit:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
}
2011-08-08 17:38:17 +02:00
strHTML + = tr ( " <b>Net amount:</b> " ) + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , nNet , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// Message
//
if ( ! wtx . mapValue [ " message " ] . empty ( ) )
2011-08-08 17:38:17 +02:00
strHTML + = QString ( " <br><b> " ) + tr ( " Message: " ) + " </b><br> " + HtmlEscape ( wtx . mapValue [ " message " ] , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
if ( ! wtx . mapValue [ " comment " ] . empty ( ) )
2011-08-08 17:38:17 +02:00
strHTML + = QString ( " <br><b> " ) + tr ( " Comment: " ) + " </b><br> " + HtmlEscape ( wtx . mapValue [ " comment " ] , true ) + " <br> " ;
2011-06-10 15:05:51 +02:00
if ( wtx . IsCoinBase ( ) )
2011-08-08 17:38:17 +02:00
strHTML + = QString ( " <br> " ) + tr ( " Generated coins must wait 120 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, it will change to \" not accepted \" and not be spendable. This may occasionally happen if another node generates a block within a few seconds of yours. " ) + " <br> " ;
2011-06-10 15:05:51 +02:00
//
// Debug view
//
if ( fDebug )
{
2011-07-29 23:11:40 +02:00
strHTML + = " <hr><br>Debug information<br><br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txin ) )
2011-08-08 17:38:17 +02:00
strHTML + = " <b>Debit:</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , - wallet - > GetDebit ( txin ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
2011-06-26 19:23:24 +02:00
if ( wallet - > IsMine ( txout ) )
2011-08-08 17:38:17 +02:00
strHTML + = " <b>Credit:</b> " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , wallet - > GetCredit ( txout ) ) + " <br> " ;
2011-06-10 15:05:51 +02:00
strHTML + = " <br><b>Transaction:</b><br> " ;
strHTML + = HtmlEscape ( wtx . ToString ( ) , true ) ;
2011-07-29 23:11:40 +02:00
CTxDB txdb ( " r " ) ; // To fetch source txouts
strHTML + = " <br><b>Inputs:</b> " ;
strHTML + = " <ul> " ;
2011-09-02 18:02:22 +02:00
CRITICAL_BLOCK ( wallet - > cs_wallet )
2011-06-10 15:05:51 +02:00
{
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
{
COutPoint prevout = txin . prevout ;
2011-07-29 23:11:40 +02:00
CTransaction prev ;
if ( txdb . ReadDiskTx ( prevout . hash , prev ) )
2011-06-10 15:05:51 +02:00
{
if ( prevout . n < prev . vout . size ( ) )
{
2011-07-29 23:11:40 +02:00
strHTML + = " <li> " ;
const CTxOut & vout = prev . vout [ prevout . n ] ;
CBitcoinAddress address ;
2011-12-22 21:51:44 +01:00
if ( ExtractAddress ( vout . scriptPubKey , address ) )
2011-07-29 23:11:40 +02:00
{
if ( wallet - > mapAddressBook . count ( address ) & & ! wallet - > mapAddressBook [ address ] . empty ( ) )
2011-07-31 12:56:46 +02:00
strHTML + = HtmlEscape ( wallet - > mapAddressBook [ address ] ) + " " ;
2011-08-08 17:38:17 +02:00
strHTML + = QString : : fromStdString ( address . ToString ( ) ) ;
2011-07-29 23:11:40 +02:00
}
2011-08-08 17:38:17 +02:00
strHTML = strHTML + " Amount= " + BitcoinUnits : : formatWithUnit ( BitcoinUnits : : BTC , vout . nValue ) ;
2011-07-29 23:11:40 +02:00
strHTML = strHTML + " IsMine= " + ( wallet - > IsMine ( vout ) ? " true " : " false " ) + " </li> " ;
2011-06-10 15:05:51 +02:00
}
}
}
}
2011-07-29 23:11:40 +02:00
strHTML + = " </ul> " ;
2011-06-10 15:05:51 +02:00
}
strHTML + = " </font></html> " ;
}
return strHTML ;
}