public static unsafe Block CreateBlock(int no, byte[] previous, int nonce, byte[] data) { if (previous.Length != 32) { throw new System.ArgumentOutOfRangeException("previous", "Invalid hash key."); } System.TimeSpan t = System.DateTime.UtcNow - new System.DateTime(1970, 1, 1); Block block = new Block() { no = no, nonce = nonce }; fixed(byte *p = previous) { Block.Copy(block.previous, p, 32); } if (data != null) { block.len = data.Length; if (block.len > 940) { throw new System.ArgumentOutOfRangeException("data", "Invalid data size."); } fixed(byte *p = data) { Block.Copy(block.data, p, block.len); } } block.timestamp = (int)t.TotalSeconds; byte[] hash = Block.Sign(CreateBlock(&block, false)); fixed(byte *p = hash) { Block.Copy(block.hash, p, 32); } return(block); }
public static unsafe bool IsValidBlock(Block *src, byte *previous) { if (src == null) { return(false); } byte[] hash = src->GetHash(); if (!Compare(Block.Sign(CreateBlock(src, true)), hash)) { return(false); } if (previous != null) { if (!Compare(src->GetPreviousHash(), previous)) { return(false); } } return(true); }