Improve empty constructors behavior
[NovacoinLibrary.git] / Novacoin / CTransaction.cs
index 1fa5bf5..b92fad1 100644 (file)
@@ -39,12 +39,44 @@ namespace Novacoin
         /// </summary>
         public CTransaction()
         {
+            vin = new CTxIn[1];
+            vin[0] = new CTxIn();
+
+            vout = new CTxOut[1];
+            vout[0] = new CTxOut();
+        }
+
+        /// <summary>
+        /// Initialize new instance as a copy of another transaction
+        /// </summary>
+        /// <param name="tx">Transaction to copy from</param>
+        public CTransaction(CTransaction tx)
+        {
+            nVersion = tx.nVersion;
+            nTime = tx.nTime;
+
+            vin = new CTxIn[tx.vin.Length];
+
+            for (int i = 0; i < vin.Length; i++)
+            {
+                vin[i] = new CTxIn(tx.vin[i]);
+            }
+
+            vout = new CTxOut[tx.vout.Length];
+
+            for (int i = 0; i < vout.Length; i++)
+            {
+                vout[i] = new CTxOut(tx.vout[i]);
+            }
+
+            nLockTime = tx.nLockTime;
         }
 
+
         /// <summary>
         /// Parse byte sequence and initialize new instance of CTransaction
         /// </summary>
-        /// <param name="txBytes"></param>
+        /// <param name="txBytes">Byte sequence</param>
                public CTransaction (IList<byte> txBytes)
                {
             WrappedList<byte> wBytes = new WrappedList<byte>(txBytes);
@@ -61,9 +93,11 @@ namespace Novacoin
                 vin[nCurrentInput] = new CTxIn();
 
                 vin[nCurrentInput].txID = new Hash256(wBytes.GetItems(32));
-                vin[nCurrentInput].n = BitConverter.ToUInt32(wBytes.GetItems(4),0);
-                vin[nCurrentInput].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
-                vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4),0);
+                vin[nCurrentInput].n = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+
+                int nScriptSigLen = (int)VarInt.ReadVarInt(ref wBytes);
+                vin[nCurrentInput].scriptSig = new CScript(wBytes.GetItems(nScriptSigLen));
+                vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
             }
 
             int nOutputs = (int)VarInt.ReadVarInt(ref wBytes);
@@ -73,8 +107,10 @@ namespace Novacoin
             {
                 // Fill outputs array
                 vout[nCurrentOutput] = new CTxOut();
-                vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.GetItems(8),0);
-                vout[nCurrentOutput].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
+                vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.GetItems(8), 0);
+
+                int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
+                vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
             }
 
             nLockTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0);