Use implicit type casting operator for serialization.
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
index bc80f0f..a33606a 100644 (file)
@@ -1,4 +1,22 @@
-\feffusing System;
+\feff/**
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
 using System.Text;
 using System.Collections.Generic;
 
@@ -22,7 +40,7 @@ namespace Novacoin
                /// <summary>
                /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
                /// </summary>
-        public uint nSequence = 0xffffffff;
+        public uint nSequence = uint.MaxValue;
 
         /// <summary>
         /// Initialize new CTxIn instance as copy of another one.
@@ -49,21 +67,19 @@ namespace Novacoin
         /// </summary>
         /// <param name="wBytes">Reference to byte sequence</param>
         /// <returns>Inputs array</returns>
-        public static CTxIn[] ReadTxInList(ref WrappedList<byte> wBytes)
+        public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
         {
-            CTxIn[] vin;
-
             // Get amount
-            int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
-            vin = new CTxIn[nInputs];
+            int nInputs = (int)wBytes.GetVarInt();
+            var vin = new CTxIn[nInputs];
 
             for (int nIndex = 0; nIndex < nInputs; nIndex++)
             {
                 // Fill inputs array
                 vin[nIndex] = new CTxIn();
-                vin[nIndex].prevout = new COutPoint(wBytes.GetItems(36));
-                vin[nIndex].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)));
-                vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+                vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
+                vin[nIndex].scriptSig = new CScript(wBytes.Get((int)wBytes.GetVarInt()));
+                vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
             }
 
             // Return inputs array
@@ -74,24 +90,21 @@ namespace Novacoin
         /// Get raw bytes representation of our input.
         /// </summary>
         /// <returns>Byte sequence.</returns>
-        public IList<byte> Bytes
+        public static implicit operator byte[] (CTxIn input)
         {
-            get
-            {
-                List<byte> inputBytes = new List<byte>();
-
-                inputBytes.AddRange(prevout.Bytes); // prevout
+            var inputBytes = new List<byte>();
 
-                List<byte> s = new List<byte>(scriptSig.Bytes);
+            inputBytes.AddRange((byte[])input.prevout); // prevout
 
-                inputBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptSig length
-                inputBytes.AddRange(s); // scriptSig
-                inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
+            var s = (byte[])input.scriptSig;
+            inputBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptSig length
+            inputBytes.AddRange(s); // scriptSig
+            inputBytes.AddRange(BitConverter.GetBytes(input.nSequence)); // Sequence
 
-                return inputBytes;
-            }
+            return inputBytes.ToArray();
         }
 
+
         public bool IsFinal
         {
             get { return (nSequence == uint.MaxValue); }
@@ -105,7 +118,7 @@ namespace Novacoin
 
             if(prevout.IsNull)
             {
-                sb.AppendFormat(", coinbase={0}", Interop.ToHex(scriptSig.Bytes));
+                sb.AppendFormat(", coinbase={0}", Interop.ToHex((byte[])scriptSig));
             }
             else
             {