ToInt64 -> ToUInt64
[NovacoinLibrary.git] / Novacoin / CTransaction.cs
index 13ce0c7..343968a 100644 (file)
@@ -30,12 +30,12 @@ namespace Novacoin
         /// <summary>
         /// Version of transaction schema.
         /// </summary>
-        public uint nVersion = 1;
+        public uint nVersion;
 
         /// <summary>
         /// Transaction timestamp.
         /// </summary>
-        public uint nTime = 0;
+        public uint nTime;
 
         /// <summary>
         /// Array of transaction inputs
@@ -50,7 +50,7 @@ namespace Novacoin
         /// <summary>
         /// Block height or timestamp when transaction is final
         /// </summary>
-        public uint nLockTime = 0;
+        public uint nLockTime;
 
         /// <summary>
         /// Initialize an empty instance
@@ -60,8 +60,11 @@ namespace Novacoin
             // Initialize empty input and output arrays. Please note that such 
             // configuration is not valid for real transaction, you have to supply 
             // at least one input and one output.
+            nVersion = 1;
+            nTime = 0;
             vin = new CTxIn[0];
             vout = new CTxOut[0];
+            nLockTime = 0;
         }
 
         /// <summary>
@@ -102,7 +105,7 @@ namespace Novacoin
             nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
             nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
 
-            int nInputs = (int)(int)wBytes.GetVarInt();
+            int nInputs = (int)wBytes.GetVarInt();
             vin = new CTxIn[nInputs];
 
             for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
@@ -125,7 +128,7 @@ namespace Novacoin
             {
                 // Fill outputs array
                 vout[nCurrentOutput] = new CTxOut();
-                vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
+                vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
 
                 int nScriptPKLen = (int)wBytes.GetVarInt();
                 vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
@@ -135,6 +138,32 @@ namespace Novacoin
         }
 
         /// <summary>
+        /// Serialized size
+        /// </summary>
+        public int Size
+        {
+            get
+            {
+                int nSize = 12; // nVersion, nTime, nLockLime
+
+                nSize += VarInt.GetEncodedSize(vin.Length);
+                nSize += VarInt.GetEncodedSize(vout.Length);
+
+                foreach (var input in vin)
+                {
+                    nSize += input.Size;
+                }
+
+                foreach (var output in vout)
+                {
+                    nSize += output.Size;
+                }
+
+                return nSize;
+            }
+        }
+
+        /// <summary>
         /// Read transactions array which is encoded in the block body.
         /// </summary>
         /// <param name="wTxBytes">Bytes sequence</param>
@@ -183,38 +212,35 @@ namespace Novacoin
         /// </summary>
         public Hash256 Hash
         {
-            get { return Hash256.Compute256(Bytes); }
+            get { return Hash256.Compute256(this); }
         }
 
         /// <summary>
         /// A sequence of bytes, which corresponds to the current state of CTransaction.
         /// </summary>
-        public byte[] Bytes
+        public static implicit operator byte[] (CTransaction tx)
         {
-            get
-            {
-                var resultBytes = new List<byte>();
+            var resultBytes = new List<byte>();
 
-                resultBytes.AddRange(BitConverter.GetBytes(nVersion));
-                resultBytes.AddRange(BitConverter.GetBytes(nTime));
-                resultBytes.AddRange(VarInt.EncodeVarInt(vin.LongLength));
+            resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion));
+            resultBytes.AddRange(BitConverter.GetBytes(tx.nTime));
+            resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength));
 
-                foreach (var input in vin)
-                {
-                    resultBytes.AddRange(input.Bytes);
-                }
+            foreach (var input in tx.vin)
+            {
+                resultBytes.AddRange((byte[])input);
+            }
 
-                resultBytes.AddRange(VarInt.EncodeVarInt(vout.LongLength));
+            resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength));
 
-                foreach (var output in vout)
-                {
-                    resultBytes.AddRange(output.Bytes);
-                }
+            foreach (var output in tx.vout)
+            {
+                resultBytes.AddRange((byte[])output);
+            }
 
-                resultBytes.AddRange(BitConverter.GetBytes(nLockTime));
+            resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime));
 
-                return resultBytes.ToArray();
-            }
+            return resultBytes.ToArray();
         }
 
         public override string ToString()