public byte[] Serialize() { using (var stream = new MemoryStream()) { using (var writer = new BinaryWriter(stream)) { writer.Write(Version); writer.Write(PreviousHash != null ? PreviousHash.ToArray() : new byte[32]); writer.Write(MerkleRoot != null ? MerkleRoot : new byte[32]); writer.Write((uint)Date.ToTimestamp()); writer.Write(Height); writer.Write((UInt64)ConsensusData); writer.Write(Validator != null ? Validator.ToArray(): new byte[20]); writer.Write((byte)1); if (witness != null) { witness.Serialize(writer); } else { writer.WriteVarBytes(null); writer.WriteVarBytes(null); } writer.WriteVarInt(transactions.Length); foreach (var tx in transactions) { var bytes = tx.Serialize(true); writer.Write(bytes); } return(stream.ToArray()); } } }
public void SerializeTest(PushDataOp[] pushOps, byte[] expected) { Witness wit = new Witness() { Items = pushOps }; FastStream stream = new FastStream(expected.Length); // Setting length for small optimization wit.Serialize(stream); Assert.Equal(expected, stream.ToByteArray()); }