public IEnumerable <Block> Decode(byte[] data) { byte[] encryptData = new byte[256]; byte[] bigEndianData = null; using (MemoryStream rawStream = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(rawStream)) { encryptData = reader.ReadBytes(256); bigEndianData = reader.ReadBytes((int)reader.BaseStream.Length - encryptData.Length); } } var decryptKey = key.RSA.Decrypt(encryptData, false); var decrypt = Aes256.Decode(bigEndianData, Aes256.GetString(decryptKey)); MemoryStream stream = new MemoryStream(decrypt); List <Block> blocks = new List <Block>(); Dictionary <string, object> blockKeyValuePairs = new Dictionary <string, object>(); try { using (BinaryReader reader = new BinaryReader(stream)) { while (reader.BaseStream.Position <= reader.BaseStream.Length) { blockKeyValuePairs["index"] = reader.ReadInt32(); blockKeyValuePairs["hash"] = reader.ReadString(); blockKeyValuePairs["merkleroot"] = reader.ReadString(); blockKeyValuePairs["previousHash"] = reader.ReadString(); blockKeyValuePairs["stamp"] = reader.ReadInt64(); blockKeyValuePairs["count"] = reader.ReadInt32(); List <Transaction> list = new List <Transaction>(); for (int i = 0; i < (int)blockKeyValuePairs["count"]; i++) { blockKeyValuePairs["tx_hash"] = reader.ReadString(); var tx = subscribe.GetTransactionAsync( HexString.Parse((string)blockKeyValuePairs["tx_hash"]) .ToByteArray()).Result; list.Add(tx); } blocks.Add(new Block(HexString.Parse((string)blockKeyValuePairs["hash"]), HexString.Parse((string)blockKeyValuePairs["previousHash"]), DateTime.FromBinary((long)blockKeyValuePairs["stamp"]), (int)blockKeyValuePairs["index"], HexString.Parse((string)blockKeyValuePairs["merkleroot"]), list )); } } } catch (EndOfStreamException) { } return(blocks.AsEnumerable()); }
public static Block CreateGenesis() { List <Transaction> list = new List <Transaction>(); GenesisBlock = new Block(new HexString(HexString.Parse("00").ToByteArray()), new HexString(HexString.Parse("00").ToByteArray()), DateTime.UtcNow, 1, new HexString(HexString.Parse("00").ToByteArray()), list); string jsondata = JsonConvert.SerializeObject(GenesisBlock); string path = "/DataChain/Genesis/"; DirectoryInfo dirInfo = new DirectoryInfo(path); if (!dirInfo.Exists) { dirInfo.Create(); } File.WriteAllText(path + "genesis.json", jsondata); return(GenesisBlock); }