CTransaction constructor. Also, trying to remove some excessive code.
authorCryptoManiac <balthazar@yandex.ru>
Sun, 16 Aug 2015 23:22:02 +0000 (02:22 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Sun, 16 Aug 2015 23:22:02 +0000 (02:22 +0300)
Novacoin/CTransaction.cs
Novacoin/CTxIn.cs
Novacoin/CTxOut.cs

index b58dd7e..77d6a62 100644 (file)
@@ -33,9 +33,40 @@ namespace Novacoin
                /// </summary>
                public uint nLockTime = 0;
 
-               public CTransaction ()
+        /// <summary>
+        /// Parse byte sequence and initialize new instance of CTransaction
+        /// </summary>
+        /// <param name="txBytes"></param>
+               public CTransaction (IList<byte> txBytes)
                {
+            WrappedList<byte> wBytes = new WrappedList<byte>(txBytes);
 
+            nVersion = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+            nTime = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+
+            int nInputs = (int)VarInt.ReadVarInt(wBytes);
+            inputs = new CTxIn[nInputs];
+
+            for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
+            {
+                // Fill inputs array
+                inputs[nCurrentInput].txID = new Hash256(wBytes.GetItems(32));
+                inputs[nCurrentInput].n = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+                inputs[nCurrentInput].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(wBytes));
+                inputs[nCurrentInput].nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+            }
+
+            int nOutputs = (int)VarInt.ReadVarInt(wBytes);
+            outputs = new CTxOut[nOutputs];
+
+            for (int nCurrentOutput = 0; nCurrentOutput < nInputs; nCurrentOutput++)
+            {
+                // Fill outputs array
+                outputs[nCurrentOutput].nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8));
+                outputs[nCurrentOutput].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(wBytes));
+            }
+
+            nLockTime = Interop.LEBytesToUInt32(wBytes.GetItems(4));
                }
 
         IList<byte> ToBytes()
index b87e594..d079d15 100644 (file)
@@ -12,22 +12,22 @@ namespace Novacoin
                /// <summary>
                /// Hash of parent transaction.
                /// </summary>
-               private Hash256 txID = new Hash256();
+               public Hash256 txID = new Hash256();
 
                /// <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 byte[] 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;
 
         /// <summary>
         /// Initialize new CTxIn instance as copy of another one.
@@ -36,27 +36,12 @@ namespace Novacoin
         public CTxIn(CTxIn i)
         {
             txID = i.txID;
-            nInput = i.nInput;
+            n = i.n;
             scriptSig = i.scriptSig;
             nSequence = i.nSequence;
         }
 
         /// <summary>
-        /// Decode byte sequence and initialize new instance of CTxIn class.
-        /// </summary>
-        /// <param name="bytes">Byte sequence</param>
-               public CTxIn (IList<byte> bytes)
-               {
-            WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
-
-            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>
         /// Get raw bytes representation of our input.
         /// </summary>
         /// <returns>Byte sequence.</returns>
@@ -65,9 +50,9 @@ namespace Novacoin
             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(n)); // Output number
+            inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // scriptSig length
+            inputBytes.AddRange(scriptSig); // scriptSig
             inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
 
             return inputBytes;
@@ -76,7 +61,7 @@ namespace Novacoin
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2}", nInput, nInput, scriptSig.ToString());
+                       sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2}", txID.ToString(), n, scriptSig.ToString());
 
                        return sb.ToString ();
                }
index 42d2b8d..aec5a88 100644 (file)
@@ -12,12 +12,12 @@ namespace Novacoin
                /// <summary>
                /// Input value.
                /// </summary>
-               private ulong nValue;
+        public ulong nValue;
 
                /// <summary>
                /// Second half of script which contains spending instructions.
                /// </summary>
-               private byte[] scriptPubKey;
+        public byte[] scriptPubKey;
 
         /// <summary>
         /// Initialize new CTxOut instance as a copy of another instance.
@@ -30,20 +30,6 @@ namespace Novacoin
         }
 
         /// <summary>
-        /// Parse input byte sequence and initialize new CTxOut instance.
-        /// </summary>
-        /// <param name="bytes">Byte sequence.</param>
-        public CTxOut(IList<byte> bytes)
-        {
-            WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
-            
-            nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8));
-            int spkLength = (int)VarInt.ReadVarInt(wBytes);
-
-            scriptPubKey = wBytes.GetItems(spkLength);
-        }
-
-        /// <summary>
         /// Get raw bytes representation of our output.
         /// </summary>
         /// <returns>Byte sequence.</returns>