Improve empty constructors behavior
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
index c3b2fc5..9983c65 100644 (file)
@@ -12,45 +12,112 @@ namespace Novacoin
                /// <summary>
                /// Hash of parent transaction.
                /// </summary>
-               private Hash256 txID = new Hash256();
+               public Hash256 txID;
 
                /// <summary>
                /// Parent input number.
                /// </summary>
-               private uint nInput = 0;
+        public uint n = 0;
 
                /// <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 = 0xffffffff;
 
-               public CTxIn ()
-               {
-               }
+        /// <summary>
+        /// Initialize new CTxIn instance as copy of another one.
+        /// </summary>
+        /// <param name="i">CTxIn instance.</param>
+        public CTxIn(CTxIn i)
+        {
+            txID = i.txID;
+            n = i.n;
+            scriptSig = i.scriptSig;
+            nSequence = i.nSequence;
+        }
+
+        /// <summary>
+        /// Initialize an empty instance of CTxIn class
+        /// </summary>
+        public CTxIn()
+        {
+            txID = new Hash256();
+            scriptSig = new CScript();
+        }
 
-        public IList<byte> ToBytes()
+        /// <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 WrappedList<byte> wBytes)
         {
-            List<byte> inputBytes = new List<byte>();
+            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();
 
-            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
+                vin[nIndex].txID = new Hash256(wBytes.GetItems(32));
+                vin[nIndex].n = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+                vin[nIndex].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)));
+                vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+            }
 
-            return inputBytes;
+            // Return inputs array
+            return vin;
+        }
+
+        /// <summary>
+        /// Get raw bytes representation of our input.
+        /// </summary>
+        /// <returns>Byte sequence.</returns>
+        public IList<byte> Bytes
+        {
+            get
+            {
+                List<byte> inputBytes = new List<byte>();
+
+                inputBytes.AddRange(txID.hashBytes); // Input transaction id
+                inputBytes.AddRange(BitConverter.GetBytes(n)); // Output number
+
+                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 bool IsCoinBase
+        {
+            get { return txID.IsZero; }
         }
 
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2}", nInput, nInput, scriptSig.ToString());
+
+            if (IsCoinBase)
+            {
+                sb.AppendFormat("CTxIn(txId={0}, coinbase={2}, nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig.Bytes), nSequence);
+            }
+            else
+            {
+                sb.AppendFormat("CTxIn(txId={0}, n={1}, scriptSig={2}, nSequence={3})", txID.ToString(), n, scriptSig.ToString(), nSequence);
+            }
 
                        return sb.ToString ();
                }