Generic class was a bit excessive for our purposes
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
index bc6850a..8cff600 100644 (file)
@@ -9,25 +9,20 @@ namespace Novacoin
        /// </summary>
        public class CTxIn
        {
-               /// <summary>
-               /// Hash of parent transaction.
-               /// </summary>
-               public Hash256 txID = new Hash256();
-
-               /// <summary>
-               /// Parent input number.
-               /// </summary>
-        public uint n = 0;
+        /// <summary>
+        /// Previous input data
+        /// </summary>
+        public COutPoint prevout;
 
                /// <summary>
                /// First half of script, signatures for the scriptPubKey
                /// </summary>
-        public byte[] scriptSig;
+        public CScript scriptSig;
 
                /// <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.
@@ -35,8 +30,7 @@ namespace Novacoin
         /// <param name="i">CTxIn instance.</param>
         public CTxIn(CTxIn i)
         {
-            txID = i.txID;
-            n = i.n;
+            prevout = new COutPoint(i.prevout);
             scriptSig = i.scriptSig;
             nSequence = i.nSequence;
         }
@@ -46,6 +40,8 @@ namespace Novacoin
         /// </summary>
         public CTxIn()
         {
+            prevout = new COutPoint();
+            scriptSig = new CScript();
         }
 
         /// <summary>
@@ -53,7 +49,7 @@ 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;
 
@@ -65,11 +61,9 @@ namespace Novacoin
             {
                 // Fill inputs array
                 vin[nIndex] = new CTxIn();
-
-                vin[nIndex].txID = new Hash256(wBytes.GetItems(32));
-                vin[nIndex].n = Interop.LEBytesToUInt32(wBytes.GetItems(4));
-                vin[nIndex].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
-                vin[nIndex].nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+                vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
+                vin[nIndex].scriptSig = new CScript(wBytes.Get((int)VarInt.ReadVarInt(ref wBytes)));
+                vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
             }
 
             // Return inputs array
@@ -80,38 +74,53 @@ namespace Novacoin
         /// Get raw bytes representation of our input.
         /// </summary>
         /// <returns>Byte sequence.</returns>
-        public IList<byte> ToBytes()
+        public IList<byte> Bytes
         {
-            List<byte> inputBytes = new List<byte>();
+            get
+            {
+                List<byte> inputBytes = new List<byte>();
+
+                inputBytes.AddRange(prevout.Bytes); // prevout
 
-            inputBytes.AddRange(txID.hashBytes); // Input transaction id
-            inputBytes.AddRange(Interop.LEBytes(n)); // Output number
-            inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // scriptSig length
-            inputBytes.AddRange(scriptSig); // scriptSig
-            inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
+                List<byte> s = new List<byte>(scriptSig.Bytes);
 
-            return inputBytes;
+                inputBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptSig length
+                inputBytes.AddRange(s); // scriptSig
+                inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
+
+                return inputBytes;
+            }
         }
 
-        public bool IsCoinBase()
+        public bool IsFinal
         {
-            return txID.IsZero();
+            get { return (nSequence == uint.MaxValue); }
         }
-
-               public override string ToString ()
+        public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
 
-            if (IsCoinBase())
+            sb.AppendFormat("CTxIn(");
+            sb.Append(prevout.ToString());
+
+            if(prevout.IsNull)
             {
-                sb.AppendFormat("CTxIn(txId={0}, coinbase={2}, nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig), nSequence);
+                sb.AppendFormat(", coinbase={0}", Interop.ToHex(scriptSig.Bytes));
             }
             else
             {
-                sb.AppendFormat("CTxIn(txId={0}, n={1}, scriptSig={2}, nSequence={3})", txID.ToString(), n, (new CScript(scriptSig)).ToString(), nSequence);
+                sb.AppendFormat(", scriptsig={0}", scriptSig.ToString());
             }
 
-                       return sb.ToString ();
+            if (nSequence != uint.MaxValue)
+            {
+                sb.AppendFormat(", nSequence={0}", nSequence);
+            }
+
+            sb.Append(")");
+
+
+            return sb.ToString ();
                }
 
        }