Generic class was a bit excessive for our purposes
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
index b87e594..8cff600 100644 (file)
@@ -9,25 +9,20 @@ namespace Novacoin
        /// </summary>
        public class CTxIn
        {
-               /// <summary>
-               /// Hash of parent transaction.
-               /// </summary>
-               private Hash256 txID = new Hash256();
-
-               /// <summary>
-               /// Parent input number.
-               /// </summary>
-               private uint nInput = 0;
+        /// <summary>
+        /// Previous input data
+        /// </summary>
+        public COutPoint prevout;
 
                /// <summary>
                /// First half of script, signatures for the scriptPubKey
                /// </summary>
-               private byte[] scriptSig;
+        public CScript scriptSig;
 
                /// <summary>
                /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
                /// </summary>
-               private uint nSequence = 0xffffffff;
+        public uint nSequence = uint.MaxValue;
 
         /// <summary>
         /// Initialize new CTxIn instance as copy of another one.
@@ -35,50 +30,97 @@ namespace Novacoin
         /// <param name="i">CTxIn instance.</param>
         public CTxIn(CTxIn i)
         {
-            txID = i.txID;
-            nInput = i.nInput;
+            prevout = new COutPoint(i.prevout);
             scriptSig = i.scriptSig;
             nSequence = i.nSequence;
         }
 
         /// <summary>
-        /// Decode byte sequence and initialize new instance of CTxIn class.
+        /// Initialize an empty instance of CTxIn class
         /// </summary>
-        /// <param name="bytes">Byte sequence</param>
-               public CTxIn (IList<byte> bytes)
-               {
-            WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
+        public CTxIn()
+        {
+            prevout = new COutPoint();
+            scriptSig = new CScript();
+        }
 
-            txID = new Hash256(wBytes.GetItems(32));
-            nInput = Interop.LEBytesToUInt32(wBytes.GetItems(4));
-            int ssLength = (int)VarInt.ReadVarInt(wBytes);
-            scriptSig = wBytes.GetItems(ssLength);
-            nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
-               }
+        /// <summary>
+        /// Read vin list from byte sequence.
+        /// </summary>
+        /// <param name="wBytes">Reference to byte sequence</param>
+        /// <returns>Inputs array</returns>
+        public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
+        {
+            CTxIn[] vin;
+
+            // Get amount
+            int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
+            vin = new CTxIn[nInputs];
+
+            for (int nIndex = 0; nIndex < nInputs; nIndex++)
+            {
+                // Fill inputs array
+                vin[nIndex] = new CTxIn();
+                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
+            return vin;
+        }
 
         /// <summary>
         /// 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(txID.hashBytes); // Input transaction id
-            inputBytes.AddRange(Interop.LEBytes(nInput)); // Input number
-            inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // Scriptsig length
-            inputBytes.AddRange(scriptSig); // ScriptSig
-            inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
+                inputBytes.AddRange(prevout.Bytes); // prevout
 
-            return inputBytes;
+                List<byte> s = new List<byte>(scriptSig.Bytes);
+
+                inputBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptSig length
+                inputBytes.AddRange(s); // scriptSig
+                inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
+
+                return inputBytes;
+            }
         }
 
-               public override string ToString ()
+        public bool IsFinal
+        {
+            get { return (nSequence == uint.MaxValue); }
+        }
+        public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2}", nInput, nInput, scriptSig.ToString());
 
-                       return sb.ToString ();
+            sb.AppendFormat("CTxIn(");
+            sb.Append(prevout.ToString());
+
+            if(prevout.IsNull)
+            {
+                sb.AppendFormat(", coinbase={0}", Interop.ToHex(scriptSig.Bytes));
+            }
+            else
+            {
+                sb.AppendFormat(", scriptsig={0}", scriptSig.ToString());
+            }
+
+            if (nSequence != uint.MaxValue)
+            {
+                sb.AppendFormat(", nSequence={0}", nSequence);
+            }
+
+            sb.Append(")");
+
+
+            return sb.ToString ();
                }
 
        }