From 6d9ab8cf9d261dced15f67110043e71a4d5a56c7 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 17 Aug 2015 14:23:18 +0300 Subject: [PATCH] Add ToBytes() implementation for CBlock and CBlockHeader --- Novacoin/CBlock.cs | 22 ++++++++++++++++++++++ Novacoin/CBlockHeader.cs | 21 +++++++++++++++++++++ Novacoin/CTransaction.cs | 2 +- 3 files changed, 44 insertions(+), 1 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 4d4aa3f..f8b9e7e 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -49,6 +49,28 @@ namespace Novacoin signature = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); } + /// + /// Convert current instance into sequence of bytes + /// + /// Byte sequence + public IList ToBytes() + { + List r = new List(); + + r.AddRange(header.ToBytes()); + r.AddRange(VarInt.EncodeVarInt(tx.LongLength)); // transactions count + + foreach (CTransaction t in tx) + { + r.AddRange(t.ToBytes()); + } + + r.AddRange(VarInt.EncodeVarInt(signature.LongLength)); + r.AddRange(signature); + + return r; + } + public override string ToString() { StringBuilder sb = new StringBuilder(); diff --git a/Novacoin/CBlockHeader.cs b/Novacoin/CBlockHeader.cs index b0759ce..b3b18c3 100644 --- a/Novacoin/CBlockHeader.cs +++ b/Novacoin/CBlockHeader.cs @@ -39,10 +39,31 @@ namespace Novacoin /// public uint nNonce = 0; + /// + /// Initialize an empty instance + /// public CBlockHeader () { } + /// + /// Convert current block header instance into sequence of bytes + /// + /// Byte sequence + public IList ToBytes() + { + List r = new List(); + + r.AddRange(Interop.LEBytes(nVersion)); + r.AddRange(prevHash.hashBytes); + r.AddRange(merkleRoot.hashBytes); + r.AddRange(Interop.LEBytes(nTime)); + r.AddRange(Interop.LEBytes(nBits)); + r.AddRange(Interop.LEBytes(nNonce)); + + return r; + } + public override string ToString() { StringBuilder sb = new StringBuilder(); diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 8146685..6e40a0b 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -113,7 +113,7 @@ namespace Novacoin return tx; } - IList ToBytes() + public IList ToBytes() { List resultBytes = new List(); -- 1.7.1