New constructor for block header class
[NovacoinLibrary.git] / Novacoin / CTxOut.cs
index eac9409..8c8ad4f 100644 (file)
@@ -12,12 +12,12 @@ namespace Novacoin
                /// <summary>
                /// Input value.
                /// </summary>
-        public ulong nValue;
+        public long nValue = -1;
 
                /// <summary>
                /// Second half of script which contains spending instructions.
                /// </summary>
-        public byte[] scriptPubKey;
+        public CScript scriptPubKey;
 
         /// <summary>
         /// Initialize new CTxOut instance as a copy of another instance.
@@ -34,6 +34,7 @@ namespace Novacoin
         /// </summary>
         public CTxOut()
         {
+            SetEmpty();
         }
 
         /// <summary>
@@ -50,8 +51,10 @@ namespace Novacoin
             {
                 // Fill outputs array
                 vout[nIndex] = new CTxOut();
-                vout[nIndex].nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8));
-                vout[nIndex].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
+                vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.GetItems(8), 0);
+
+                int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes);
+                vout[nIndex].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen));
             }
 
             return vout;
@@ -61,21 +64,55 @@ namespace Novacoin
         /// Get raw bytes representation of our output.
         /// </summary>
         /// <returns>Byte sequence.</returns>
-        public IList<byte> ToBytes()
+        public IList<byte> Bytes
         {
-            List<byte> resultBytes = new List<byte>();
+            get
+            {
+                List<byte> resultBytes = new List<byte>();
+
+                resultBytes.AddRange(BitConverter.GetBytes(nValue)); // txout value
 
-            resultBytes.AddRange(Interop.LEBytes(nValue)); // txout value
-            resultBytes.AddRange(VarInt.EncodeVarInt(scriptPubKey.LongLength)); // scriptPubKey length
-            resultBytes.AddRange(scriptPubKey); // scriptPubKey
+                List<byte> s = new List<byte>(scriptPubKey.Bytes);
 
-            return resultBytes;
+                resultBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptPubKey length
+                resultBytes.AddRange(s); // scriptPubKey
+
+                return resultBytes;
+            }
+        }
+
+        /// <summary>
+        /// Null prevouts have -1 value
+        /// </summary>
+        public void SetNull()
+        {
+            nValue = -1;
+            scriptPubKey = new CScript();
+        }
+
+        /// <summary>
+        /// Empty outputs have zero value and empty scriptPubKey
+        /// </summary>
+        public void SetEmpty()
+        {
+            nValue = 0;
+            scriptPubKey = new CScript();
+        }
+
+        public bool IsNull
+        {
+            get { return (nValue == -1); }
+        }
+
+        public bool IsEmpty
+        {
+           get { return nValue == 0 && scriptPubKey.IsNull; }
         }
 
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       sb.AppendFormat ("CTxOut(nValue={0},scriptPubKey={1})", nValue, (new CScript(scriptPubKey)).ToString());
+                       sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString());
 
                        return sb.ToString ();
                }