Transaction script verification, unserealize exceptions
[NovacoinLibrary.git] / Novacoin / CBlockHeader.cs
index 129be06..b9c9a4c 100644 (file)
@@ -19,6 +19,7 @@
 using System;
 using System.Text;
 using System.Collections.Generic;
+using System.Diagnostics.Contracts;
 
 namespace Novacoin
 {
@@ -76,6 +77,8 @@ namespace Novacoin
 
         public CBlockHeader(byte[] bytes)
         {
+            Contract.Requires<ArgumentException>(bytes.Length == 80, "Any valid block header is exactly 80 bytes long.");
+
             nVersion = BitConverter.ToUInt32(bytes, 0);
             prevHash = new Hash256(bytes, 4);
             merkleRoot = new Hash256(bytes, 36);
@@ -88,27 +91,24 @@ namespace Novacoin
         /// Convert current block header instance into sequence of bytes
         /// </summary>
         /// <returns>Byte sequence</returns>
-        public byte[] Bytes
+        public static implicit operator byte[] (CBlockHeader h)
         {
-            get
-            {
-                var r = new List<byte>();
-
-                r.AddRange(BitConverter.GetBytes(nVersion));
-                r.AddRange(prevHash.hashBytes);
-                r.AddRange(merkleRoot.hashBytes);
-                r.AddRange(BitConverter.GetBytes(nTime));
-                r.AddRange(BitConverter.GetBytes(nBits));
-                r.AddRange(BitConverter.GetBytes(nNonce));
-
-                return r.ToArray();
-            }
+            var r = new List<byte>();
+
+            r.AddRange(BitConverter.GetBytes(h.nVersion));
+            r.AddRange((byte[])h.prevHash);
+            r.AddRange((byte[])h.merkleRoot);
+            r.AddRange(BitConverter.GetBytes(h.nTime));
+            r.AddRange(BitConverter.GetBytes(h.nBits));
+            r.AddRange(BitConverter.GetBytes(h.nNonce));
+
+            return r.ToArray();
         }
 
         public ScryptHash256 Hash
         {
             get {
-                return ScryptHash256.Compute256(Bytes);
+                return ScryptHash256.Compute256(this);
             }
         }