We're at little-endian only
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
index a712606..f5fbc77 100644 (file)
@@ -49,6 +49,34 @@ namespace Novacoin
         }
 
         /// <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)
+        {
+            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].txID = new Hash256(wBytes.GetItems(32));
+                vin[nIndex].n = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+                vin[nIndex].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
+                vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+            }
+
+            // Return inputs array
+            return vin;
+        }
+
+        /// <summary>
         /// Get raw bytes representation of our input.
         /// </summary>
         /// <returns>Byte sequence.</returns>
@@ -57,18 +85,31 @@ namespace Novacoin
             List<byte> inputBytes = new List<byte>();
 
             inputBytes.AddRange(txID.hashBytes); // Input transaction id
-            inputBytes.AddRange(Interop.LEBytes(n)); // Output number
+            inputBytes.AddRange(BitConverter.GetBytes(n)); // Output number
             inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // scriptSig length
             inputBytes.AddRange(scriptSig); // scriptSig
-            inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
+            inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
 
             return inputBytes;
         }
 
+        public bool IsCoinBase()
+        {
+            return txID.IsZero();
+        }
+
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2})", txID.ToString(), n, (new CScript(scriptSig)).ToString());
+
+            if (IsCoinBase())
+            {
+                sb.AppendFormat("CTxIn(txId={0}, coinbase={2}, nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig), nSequence);
+            }
+            else
+            {
+                sb.AppendFormat("CTxIn(txId={0}, n={1}, scriptSig={2}, nSequence={3})", txID.ToString(), n, (new CScript(scriptSig)).ToString(), nSequence);
+            }
 
                        return sb.ToString ();
                }