Block parser
[NovacoinLibrary.git] / Novacoin / CBlock.cs
index 4eb3a33..136941a 100644 (file)
@@ -1,4 +1,5 @@
 \feffusing System;
+using System.Collections.Generic;
 
 namespace Novacoin
 {
@@ -20,10 +21,31 @@ namespace Novacoin
                /// <summary>
                /// Block header signature.
                /// </summary>
-               public byte[] signature = {};
+               public byte[] signature;
 
-               public CBlock ()
+        /// <summary>
+        /// Parse byte sequence and initialize new block instance
+        /// </summary>
+        /// <param name="blockBytes"></param>
+               public CBlock (List<byte> blockBytes)
                {
+            header = new CBlockHeader();
+
+            WrappedList<byte> wBytes = new WrappedList<byte>(blockBytes);
+
+            // Fill the block header fields
+            header.nVersion = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+            header.prevHash = new Hash256(wBytes.GetItems(32));
+            header.merkleRoot = new Hash256(wBytes.GetItems(32));
+            header.nTime = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+            header.nBits = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+            header.nNonce = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+
+            // Parse transactions list
+            tx = CTransaction.ParseTransactionsList(ref wBytes);
+
+            // Read block signature
+            signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
                }
        }
 }