public static HashEntry ( |
||
entry | EntryData to be hashed | |
Résultat | byte[] |
/// <summary> /// Commits an entry to the Factom blockchain. Must wait 10 seconds if succeeds then call RevealEntry /// </summary> /// <param name="entry">Entry to be committed</param> /// <param name="name">Name of entry credit wallet</param> /// <returns>ChainID of commited Entry</returns> public static byte[] CommitEntry(DataStructs.EntryData entry, string name) { var byteList = new List <byte>(); // 1 byte version byteList.Add(0); // 6 byte milliTimestamp (truncated unix time) byteList.AddRange(Times.MilliTime()); // 32 byte Entry Hash byteList.AddRange(Entries.HashEntry(entry)); // 1 byte number of entry credits to pay var cost = EntryCost(entry); // TODO: check errors byteList.Add(BitConverter.GetBytes(cost)[0]); var com = new WallerCommit(); com.Message = Arrays.ByteArrayToHex(byteList.ToArray()); //Hex encoded string on bytelist var json = JsonConvert.SerializeObject(com); Console.WriteLine("CE Json = " + json); //TODO: Remove var req = new RestRequest("/commit-entry/{name}", Method.POST); req.RequestFormat = DataFormat.Json; req.AddParameter("application/json", json, ParameterType.RequestBody); req.AddUrlSegment("name", name); var resp = StaticValues.clientD.Execute(req); if (resp.StatusCode != HttpStatusCode.OK) { throw new FactomEntryException("Entry Commit Failed. Message: " + resp.ErrorMessage); } //Console.WriteLine("CommitEntry Resp = " + resp.StatusCode + "|" + resp.StatusCode); if (entry.ExtIDs != null) { return(Entries.ChainIdOfFirstEntry(entry)); } return(entry.ChainId); }
/// <summary> /// First method to add a chain to factom. Spends the entry credits, must wait 10seconds and call /// RevealChain() to finalize the commit. /// </summary> /// <param name="c">Chain to be added</param> /// <param name="name">Name of Entry Credit wallet</param> /// <returns>ChainID of chain added, do not lose this!</returns> public static byte[] CommitChain(ChainType c, string name) { var byteList = new List <byte>(); //1 byte version byteList.Add(0); // 6 byte milliTimestamp (truncated unix time) byteList.AddRange(Times.MilliTime()); var entry = c.FirstEntry; // 32 Byte ChainID Hash //byte[] chainIDHash = Encoding.ASCII.GetBytes(c.ChainId); var chainIDHash = c.ChainId; chainIDHash = SHA256.Create().ComputeHash(chainIDHash); chainIDHash = SHA256.Create().ComputeHash(chainIDHash); byteList.AddRange(chainIDHash); // 32 byte Weld; sha256(sha256(EntryHash + ChainID)) var cid = c.ChainId; var s = Entries.HashEntry(c.FirstEntry); var weld = new byte[cid.Length + s.Length]; s.CopyTo(weld, 0); cid.CopyTo(weld, s.Length); weld = SHA256.Create().ComputeHash(weld); weld = SHA256.Create().ComputeHash(weld); byteList.AddRange(weld); // 32 byte Entry Hash of the First Entry byteList.AddRange(Entries.HashEntry(c.FirstEntry)); // 1 byte number of Entry Credits to pay var cost = (sbyte)(Entry.EntryCost(entry) + 10); // TODO: check errors byteList.Add(BitConverter.GetBytes(cost)[0]); var com = new WalletCommit(); com.Message = Arrays.ByteArrayToHex(byteList.ToArray()); var json = JsonConvert.SerializeObject(com); var req = new RestRequest("/commit-chain/" + name, Method.POST); req.RequestFormat = DataFormat.Json; req.AddParameter("application/json", json, ParameterType.RequestBody); req.AddUrlSegment("name", name); var resp = StaticValues.clientD.Execute(req); Console.WriteLine("CommitChain Resp = " + resp.StatusCode); // TODO: Remove Console.WriteLine("Message= " + com.Message); // TODO: Remove if (resp.StatusCode != HttpStatusCode.OK) { throw new FactomChainException("Chain Commit Failed. Message: " + resp.ErrorMessage); } return(Entries.ChainIdOfFirstEntry(c.FirstEntry)); }