Limit duplicity on stake & Process orphans only if there are some.
[NovacoinLibrary.git] / Novacoin / CTxOut.cs
index eb24845..36f4817 100644 (file)
@@ -39,6 +39,17 @@ namespace Novacoin
         public CScript scriptPubKey;
 
         /// <summary>
+        /// Initialize new outpoint using provided value and script.
+        /// </summary>
+        /// <param name="nValue">Input value</param>
+        /// <param name="scriptPubKey">Spending instructions.</param>
+        public CTxOut(ulong nValue, CScript scriptPubKey)
+        {
+            this.nValue = nValue;
+            this.scriptPubKey = scriptPubKey;
+        }
+
+        /// <summary>
         /// Initialize new CTxOut instance as a copy of another instance.
         /// </summary>
         /// <param name="o">CTxOut instance.</param>
@@ -59,27 +70,69 @@ namespace Novacoin
         /// <summary>
         /// Read vout list from byte sequence.
         /// </summary>
-        /// <param name="wBytes">Reference to byte sequence</param>
+        /// <param name="wBytes">Reference to binary reader</param>
         /// <returns>Outputs array</returns>
-        public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes)
+        internal static CTxOut[] ReadTxOutList(ref BinaryReader reader)
         {
-            int nOutputs = (int)wBytes.GetVarInt();
+            int nOutputs = (int)VarInt.ReadVarInt(ref reader);
             var vout =new CTxOut[nOutputs];
 
             for (int nIndex = 0; nIndex < nOutputs; nIndex++)
             {
                 // Fill outputs array
                 vout[nIndex] = new CTxOut();
-                vout[nIndex].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
+                vout[nIndex].nValue = reader.ReadUInt64();
 
-                int nScriptPKLen = (int)wBytes.GetVarInt();
-                vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+                int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
+                vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
             }
 
             return vout;
         }
 
         /// <summary>
+        /// Deserialize outputs array.
+        /// </summary>
+        /// <param name="outBytes">Byte array</param>
+        /// <returns>Outputs array</returns>
+        public static CTxOut[] DeserializeOutputsArray(byte[] outBytes)
+        {
+            var stream = new MemoryStream(outBytes);
+            var reader = new BinaryReader(stream);
+
+            CTxOut[] result = ReadTxOutList(ref reader);
+
+            reader.Close();
+
+            return result;
+        }
+
+        /// <summary>
+        /// Create serialized representation of outputs array.
+        /// </summary>
+        /// <param name="vout">Outputs array</param>
+        /// <returns>Byte array</returns>
+        public static byte[] SerializeOutputsArray(CTxOut[] vout)
+        {
+            var stream = new MemoryStream();
+            var writer = new BinaryWriter(stream);
+
+            writer.Write(VarInt.EncodeVarInt(vout.Length));
+
+            foreach (var o in vout)
+            {
+                writer.Write(o);
+            }
+
+            var resultBytes = stream.ToArray();
+
+            writer.Close();
+
+            return resultBytes;
+        }
+
+
+        /// <summary>
         /// Get raw bytes representation of our output.
         /// </summary>
         /// <returns>Byte sequence.</returns>
@@ -130,7 +183,7 @@ namespace Novacoin
         /// <summary>
         /// Serialized size
         /// </summary>
-        public int Size
+        public uint Size
         {
             get
             {