2014-12-17 02:47:57 +01:00
// Copyright (c) 2011-2014 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.
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"
2014-09-05 13:18:35 +02:00
# include "paymentserver.h"
# include "transactionrecord.h"
2013-04-13 07:13:08 +02:00
2012-05-14 23:44:52 +02:00
# include "base58.h"
2015-01-24 15:29:29 +01:00
# include "consensus/consensus.h"
2013-04-13 07:13:08 +02:00
# include "main.h"
2014-08-20 17:37:40 +02:00
# include "script/script.h"
2014-06-19 15:08:37 +02:00
# include "timedata.h"
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
# include "util.h"
2015-01-24 15:29:29 +01:00
# include "wallet/db.h"
2015-02-03 21:09:47 +01:00
# 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 ) ;
2015-05-25 06:48:33 +02:00
if ( ! CheckFinalTx ( wtx ) )
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 ( ) ;
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
2014-06-19 01:42:39 +02:00
if ( CBitcoinAddress ( rec - > address ) . IsValid ( ) )
2011-06-10 15:05:51 +02:00
{
2014-06-19 01:42:39 +02:00
CTxDestination address = CBitcoinAddress ( rec - > address ) . Get ( ) ;
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> " ;
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-23 00:46:19 +02:00
CAmount nUnmatured = 0 ;
2014-04-15 17:38:25 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . 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 ;
2014-04-15 17:38:25 +02:00
BOOST_FOREACH ( const CTxIn & txin , wtx . 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 ;
2014-04-15 17:38:25 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . 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
//
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
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 ) + " " ;
strHTML + = GUIUtil : : HtmlEscape ( CBitcoinAddress ( address ) . ToString ( ) ) ;
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
2014-04-23 00:46:19 +02:00
CAmount nTxFee = nDebit - wtx . 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
//
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
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> " ;
2014-04-15 17:38:25 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
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
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)
2015-07-09 13:23:44 +02:00
Q_FOREACH ( const PAIRTYPE ( 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:
//
2015-07-09 13:23:44 +02:00
Q_FOREACH ( const PAIRTYPE ( 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
//
if ( fDebug )
{
strHTML + = " <hr><br> " + tr ( " Debug information " ) + " <br><br> " ;
BOOST_FOREACH ( const CTxIn & txin , wtx . vin )
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> " ;
2014-04-15 17:38:25 +02:00
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
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> " ;
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-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 ;
}