ReadTxInList, ReadTxOutList and ReadTxList are internal now.
[NovacoinLibrary.git] / Novacoin / CTransaction.cs
index 83d2a09..e67ed45 100644 (file)
@@ -19,6 +19,7 @@
 using System;
 using System.Text;
 using System.Collections.Generic;
+using System.IO;
 
 namespace Novacoin
 {
@@ -244,9 +245,9 @@ namespace Novacoin
             }
             if (nBlockTime == 0)
             {
-                nBlockTime = NetInfo.GetAdjustedTime();
+                nBlockTime = NetUtils.GetAdjustedTime();
             }
-            if (nLockTime < (nLockTime < NetInfo.nLockTimeThreshold ? nBlockHeight : nBlockTime))
+            if (nLockTime < (nLockTime < NetUtils.nLockTimeThreshold ? nBlockHeight : nBlockTime))
             {
                 return true;
             }
@@ -268,7 +269,7 @@ namespace Novacoin
         {
             try
             {
-                var wBytes = new ByteQueue(txBytes);
+                var wBytes = new ByteQueue(ref txBytes);
 
                 nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
                 nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
@@ -341,12 +342,12 @@ namespace Novacoin
         /// </summary>
         /// <param name="wTxBytes">Bytes sequence</param>
         /// <returns>Transactions array</returns>
-        public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
+        internal static CTransaction[] ReadTransactionsList(ref BinaryReader reader)
         {
             try
             {
                 // Read amount of transactions
-                int nTransactions = (int)wTxBytes.GetVarInt();
+                int nTransactions = (int)VarInt.ReadVarInt(ref reader);
                 var tx = new CTransaction[nTransactions];
 
                 for (int nTx = 0; nTx < nTransactions; nTx++)
@@ -354,16 +355,16 @@ namespace Novacoin
                     // Fill the transactions array
                     tx[nTx] = new CTransaction();
 
-                    tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
-                    tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+                    tx[nTx].nVersion = reader.ReadUInt32();
+                    tx[nTx].nTime = reader.ReadUInt32();
 
                     // Inputs array
-                    tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
+                    tx[nTx].vin = CTxIn.ReadTxInList(ref reader);
 
                     // outputs array
-                    tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
+                    tx[nTx].vout = CTxOut.ReadTxOutList(ref reader);
 
-                    tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+                    tx[nTx].nLockTime = reader.ReadUInt32();
                 }
 
                 return tx;
@@ -401,29 +402,34 @@ namespace Novacoin
         /// </summary>
         public static implicit operator byte[] (CTransaction tx)
         {
-            var resultBytes = new List<byte>();
+            var stream = new MemoryStream();
+            var writer = new BinaryWriter(stream);
 
-            resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion));
-            resultBytes.AddRange(BitConverter.GetBytes(tx.nTime));
-            resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength));
+            writer.Write(tx.nVersion);
+            writer.Write(tx.nTime);
+            writer.Write(VarInt.EncodeVarInt(tx.vin.LongLength));
 
             foreach (var input in tx.vin)
             {
-                resultBytes.AddRange((byte[])input);
+                writer.Write(input);
             }
 
-            resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength));
+            writer.Write(VarInt.EncodeVarInt(tx.vout.LongLength));
 
             foreach (var output in tx.vout)
             {
-                resultBytes.AddRange((byte[])output);
+                writer.Write(output);
             }
 
-            resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime));
+            writer.Write(tx.nLockTime);
+            var resultBytes = stream.ToArray();
+            writer.Close();
 
-            return resultBytes.ToArray();
+            return resultBytes;
         }
 
+
+
         public override string ToString()
         {
             var sb = new StringBuilder();