Example #1
0
        /// <summary>
        ///     Decode - Decodes a raw bitcoin block header
        /// </summary>
        private void Decode()
        {
            // block version number
            BlockVersion = ReadUInt();

            // previous block hash
            PrevBlockHash = ByteHexConverter.ByteArrayToHex(ReadSlice(32).Reverse().ToArray());

            // merkle root hash
            MerkleRootHash = ReadSlice(32);

            // block timestamp (seconds since 1970-01-01T00:00 UTC)
            TimeStamp = ReadUInt();

            // difficulty target in compact format
            DiffTarget = ReadUInt();

            // nonce
            Nonce = ReadUInt();

            // strict validation - we should be at the end of the header
            LengthMatch = Offset == ByteData.Length;

            // don't store the extra data; useful when serializing multiple block headers
            if (!LengthMatch)
            {
                ByteData = ByteData.Take(Offset).ToArray();
            }

            // block hash = sha256(sha256(header_data)) -> reverse byte data -> convert to hex
            SHA256 sha256 = new SHA256Managed();

            BlockHashHex = ByteHexConverter.ByteArrayToHex(sha256.ComputeHash(sha256.ComputeHash(ByteData)).Reverse().ToArray());
        }