Fix transaction type in UI: not all tx'es with "from"/"to" field are necessarily...
[novacoin.git] / src / qt / transactionrecord.cpp
index 34f30d5..52a3080 100644 (file)
@@ -1,5 +1,6 @@
 #include "transactionrecord.h"
 
+#include "headers.h"
 
 /* Return positive answer if transaction should be shown in list.
  */
@@ -29,7 +30,7 @@ bool TransactionRecord::showTransaction(const CWalletTx &wtx)
 /*
  * Decompose CWallet transaction to model transaction records.
  */
-QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx &wtx)
+QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
 {
     QList<TransactionRecord> parts;
     int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
@@ -59,33 +60,34 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
                 {
                     int64 nUnmatured = 0;
                     BOOST_FOREACH(const CTxOut& txout, wtx.vout)
-                        nUnmatured += txout.GetCredit();
+                        nUnmatured += wallet->GetCredit(txout);
                     sub.credit = nUnmatured;
                 }
             }
-            else if (!mapValue["from"].empty() || !mapValue["message"].empty())
-            {
-                // Received by IP connection
-                sub.type = TransactionRecord::RecvFromIP;
-                if (!mapValue["from"].empty())
-                    sub.address = mapValue["from"];
-            }
             else
             {
+                bool foundAddress = false;
                 // Received by Bitcoin Address
-                sub.type = TransactionRecord::RecvWithAddress;
                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
                 {
-                    if (txout.IsMine())
+                    if(wallet->IsMine(txout))
                     {
-                        std::vector<unsigned char> vchPubKey;
-                        if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
+                        CBitcoinAddress address;
+                        if (ExtractAddress(txout.scriptPubKey, wallet, address))
                         {
-                            sub.address = PubKeyToAddress(vchPubKey);
+                            sub.type = TransactionRecord::RecvWithAddress;
+                            sub.address = address.ToString();
+                            foundAddress = true;
+                            break;
                         }
-                        break;
                     }
                 }
+                if(!foundAddress)
+                {
+                    // Received by IP connection, or other non-address transaction like OP_EVAL
+                    sub.type = TransactionRecord::RecvFromOther;
+                    sub.address = mapValue["from"];
+                }
             }
             parts.append(sub);
         }
@@ -93,11 +95,11 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
         {
             bool fAllFromMe = true;
             BOOST_FOREACH(const CTxIn& txin, wtx.vin)
-                fAllFromMe = fAllFromMe && txin.IsMine();
+                fAllFromMe = fAllFromMe && wallet->IsMine(txin);
 
             bool fAllToMe = true;
             BOOST_FOREACH(const CTxOut& txout, wtx.vout)
-                fAllToMe = fAllToMe && txout.IsMine();
+                fAllToMe = fAllToMe && wallet->IsMine(txout);
 
             if (fAllFromMe && fAllToMe)
             {
@@ -120,25 +122,25 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
                     TransactionRecord sub(hash, nTime);
                     sub.idx = parts.size();
 
-                    if (txout.IsMine())
+                    if(wallet->IsMine(txout))
                     {
                         // Ignore parts sent to self, as this is usually the change
                         // from a transaction sent back to our own address.
                         continue;
                     }
-                    else if (!mapValue["to"].empty())
+
+                    CBitcoinAddress address;
+                    if (ExtractAddress(txout.scriptPubKey, 0, address))
                     {
-                        // Sent to IP
-                        sub.type = TransactionRecord::SendToIP;
-                        sub.address = mapValue["to"];
+                        // Sent to Bitcoin Address
+                        sub.type = TransactionRecord::SendToAddress;
+                        sub.address = address.ToString();
                     }
                     else
                     {
-                        // Sent to Bitcoin Address
-                        sub.type = TransactionRecord::SendToAddress;
-                        uint160 hash160;
-                        if (ExtractHash160(txout.scriptPubKey, hash160))
-                            sub.address = Hash160ToAddress(hash160);
+                        // Sent to IP, or other non-address transaction like OP_EVAL
+                        sub.type = TransactionRecord::SendToOther;
+                        sub.address = mapValue["to"];
                     }
 
                     int64 nValue = txout.nValue;
@@ -160,9 +162,9 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
                 //
                 bool fAllMine = true;
                 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
-                    fAllMine = fAllMine && txout.IsMine();
+                    fAllMine = fAllMine && wallet->IsMine(txout);
                 BOOST_FOREACH(const CTxIn& txin, wtx.vin)
-                    fAllMine = fAllMine && txin.IsMine();
+                    fAllMine = fAllMine && wallet->IsMine(txin);
 
                 parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
             }
@@ -194,7 +196,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx)
 
     if (!wtx.IsFinal())
     {
-        if (wtx.nLockTime < 500000000)
+        if (wtx.nLockTime < LOCKTIME_THRESHOLD)
         {
             status.status = TransactionStatus::OpenUntilBlock;
             status.open_for = nBestHeight - wtx.nLockTime;
@@ -211,7 +213,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx)
         {
             status.status = TransactionStatus::Offline;
         }
-        else if (status.depth < 6)
+        else if (status.depth < NumConfirmations)
         {
             status.status = TransactionStatus::Unconfirmed;
         }
@@ -253,3 +255,9 @@ bool TransactionRecord::statusUpdateNeeded()
 {
     return status.cur_num_blocks != nBestHeight;
 }
+
+std::string TransactionRecord::getTxID()
+{
+    return hash.ToString() + strprintf("-%03d", idx);
+}
+