Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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));
        }