2014-02-08 22:50:24 +01:00
// Copyright (c) 2011-2014 The Bitcoin developers
2013-11-04 16:20:43 +01:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2012-03-18 23:14:03 +01:00
# include "transactiondesc.h"
2011-06-10 15:05:51 +02:00
2011-08-08 17:38:17 +02:00
# include "bitcoinunits.h"
2013-04-13 07:13:08 +02:00
# include "guiutil.h"
2012-05-14 23:44:52 +02:00
# include "base58.h"
2013-04-13 07:13:08 +02:00
# include "db.h"
# include "main.h"
2013-07-22 08:50:39 +02:00
# include "paymentserver.h"
2013-09-05 12:23:08 +02:00
# include "transactionrecord.h"
2013-04-13 07:13:08 +02:00
# include "ui_interface.h"
# include "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 ) ;
2014-01-27 03:50:15 +01:00
if ( ! IsFinalTx ( wtx , chainActive . Height ( ) + 1 ) )
2011-06-10 15:05:51 +02:00
{
2011-08-08 17:38:17 +02:00
if ( wtx . nLockTime < LOCKTIME_THRESHOLD )
2014-01-27 03:50:15 +01:00
return tr ( " Open for %n more block(s) " , " " , wtx.nLockTime - chainActive.Height()) ;
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 ( ) ;
2014-02-12 19:43:07 +01:00
if ( nDepth < 0 )
return tr ( " conflicted " ) ;
else if ( GetAdjustedTime ( ) - wtx . nTimeReceived > 2 * 60 & & wtx . GetRequestCount ( ) = = 0 )
2012-07-03 14:37:51 +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
}
}
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 ( ) ;
int64_t nCredit = wtx . GetCredit ( ) ;
int64_t nDebit = wtx . GetDebit ( ) ;
int64_t nNet = nCredit - nDebit ;
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
2014-05-28 01:38:40 +02:00
if ( CBitcoinAddress ( rec - > address ) . IsValid ( ) )
2011-06-10 15:05:51 +02:00
{
2014-05-28 01:38:40 +02:00
CTxDestination address = CBitcoinAddress ( rec - > address ) . Get ( ) ;
if ( wallet - > mapAddressBook . count ( address ) )
2011-06-10 15:05:51 +02:00
{
2014-05-28 01:38:40 +02:00
strHTML + = " <b> " + tr ( " From " ) + " :</b> " + tr ( " unknown " ) + " <br> " ;
strHTML + = " <b> " + tr ( " To " ) + " :</b> " ;
strHTML + = GUIUtil : : HtmlEscape ( rec - > address ) ;
if ( ! wallet - > mapAddressBook [ address ] . name . empty ( ) )
strHTML + = " ( " + tr ( " own address " ) + " , " + tr ( " label " ) + " : " + GUIUtil : : HtmlEscape ( wallet - > mapAddressBook [ address ] . name ) + " ) " ;
else
strHTML + = " ( " + tr ( " own address " ) + " ) " ;
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> " ;
CTxDestination dest = CBitcoinAddress ( strAddress ) . Get ( ) ;
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-15 17:38:25 +02:00
int64_t nUnmatured = 0 ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
nUnmatured + = wallet - > GetCredit ( txout ) ;
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " ;
if ( wtx . IsInMainChain ( ) )
strHTML + = BitcoinUnits : : formatWithUnit ( unit , nUnmatured ) + " ( " + tr ( " matures in %n more block(s) " , " " , wtx . GetBlocksToMaturity ( ) ) + " ) " ;
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-04-15 17:38:25 +02:00
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , nNet ) + " <br> " ;
}
else
{
bool fAllFromMe = true ;
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
fAllFromMe = fAllFromMe & & wallet - > IsMine ( txin ) ;
bool fAllToMe = true ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
fAllToMe = fAllToMe & & wallet - > IsMine ( txout ) ;
if ( fAllFromMe )
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
//
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
2014-04-15 17:38:25 +02:00
if ( wallet - > IsMine ( txout ) )
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 ) + " " ;
strHTML + = GUIUtil : : HtmlEscape ( CBitcoinAddress ( address ) . ToString ( ) ) ;
strHTML + = " <br> " ;
2011-06-10 15:05:51 +02:00
}
}
2014-04-15 17:38:25 +02:00
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( 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
int64_t nChange = wtx . GetChange ( ) ;
int64_t nValue = nCredit - nChange ;
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - nValue ) + " <br> " ;
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , nValue ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2014-04-15 17:38:25 +02:00
int64_t nTxFee = nDebit - wtx . GetValueOut ( ) ;
if ( nTxFee > 0 )
strHTML + = " <b> " + tr ( " Transaction fee " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - nTxFee ) + " <br> " ;
2011-06-10 15:05:51 +02:00
}
2014-04-15 17:38:25 +02:00
else
{
//
// Mixed debit transaction
//
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
if ( wallet - > IsMine ( txin ) )
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - wallet - > GetDebit ( txin ) ) + " <br> " ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
if ( wallet - > IsMine ( txout ) )
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , wallet - > GetCredit ( txout ) ) + " <br> " ;
}
}
2011-06-10 15:05:51 +02:00
2014-04-15 17:38:25 +02:00
strHTML + = " <b> " + tr ( " Net amount " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( 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
2014-05-28 01:38:40 +02:00
strHTML + = " <b> " + tr ( " Transaction ID " ) + " :</b> " + TransactionRecord : : formatSubTxId ( wtx . GetHash ( ) , rec - > idx ) + " <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)
foreach ( const PAIRTYPE ( string , string ) & r , wtx . vOrderForm )
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:
//
foreach ( const PAIRTYPE ( string , string ) & r , wtx . vOrderForm )
{
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
//
if ( fDebug )
{
strHTML + = " <hr><br> " + tr ( " Debug information " ) + " <br><br> " ;
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
if ( wallet - > IsMine ( txin ) )
strHTML + = " <b> " + tr ( " Debit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , - wallet - > GetDebit ( txin ) ) + " <br> " ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
if ( wallet - > IsMine ( txout ) )
strHTML + = " <b> " + tr ( " Credit " ) + " :</b> " + BitcoinUnits : : formatWithUnit ( unit , wallet - > GetCredit ( txout ) ) + " <br> " ;
strHTML + = " <br><b> " + tr ( " Transaction " ) + " :</b><br> " ;
strHTML + = GUIUtil : : HtmlEscape ( wtx . 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
2014-04-15 17:38:25 +02:00
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
{
COutPoint prevout = txin . prevout ;
2012-04-06 18:39:12 +02:00
2014-04-15 17:38:25 +02:00
CCoins prev ;
if ( pcoinsTip - > GetCoins ( prevout . hash , prev ) )
2011-06-10 15:05:51 +02:00
{
2014-04-15 17:38:25 +02:00
if ( prevout . n < prev . vout . size ( ) )
2011-06-10 15:05:51 +02:00
{
2014-04-15 17:38:25 +02:00
strHTML + = " <li> " ;
const CTxOut & vout = prev . vout [ prevout . n ] ;
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 ) + " " ;
strHTML + = QString : : fromStdString ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2011-06-10 15:05:51 +02:00
}
2014-04-15 17:38:25 +02:00
strHTML = strHTML + " " + tr ( " Amount " ) + " = " + BitcoinUnits : : formatWithUnit ( unit , vout . nValue ) ;
strHTML = strHTML + " IsMine= " + ( wallet - > IsMine ( vout ) ? 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 ;
}