ByteArrayToHex() public static méthode

Converts byte[] to hex string
public static ByteArrayToHex ( byte ba ) : string
ba byte
Résultat string
Exemple #1
0
        /// <summary>
        /// Returns the data of an entry.
        /// </summary>
        /// <param name="entryHash">Entryhash of entry</param>
        /// <returns>The entry data</returns>
        public static DataStructs.EntryData GetEntryData(byte[] entryHash)
        {
            var req = new RestRequest("/entry-by-hash/{hash}", Method.GET);

            req.AddUrlSegment("hash", Arrays.ByteArrayToHex(entryHash));

            var resp      = StaticValues.clientWallet.Execute(req);
            var entryType = JsonConvert.DeserializeObject <DataStructs.EntryDataStringFormat>(resp.Content);

            return(DataStructs.ConvertStringFormatToByteFormat(entryType));
        }
Exemple #2
0
        /// <summary>
        /// Returns an EntryBlock
        /// </summary>
        /// <param name="hash">String of KeyMr</param>
        /// <returns>EntryBlockData</returns>
        public static DataStructs.EntryBlockData GetEntryBlockByKeyMR(byte[] keyMR)
        {
            var req         = new RestRequest("/entry-block-by-keymr/{hash}", Method.GET);
            var keyMRString = Arrays.ByteArrayToHex(keyMR);

            req.AddUrlSegment("hash", keyMRString);

            var resp = StaticValues.clientWallet.Execute(req);

            if (resp.Content == "EBlock not found")
            {
                throw new FactomEntryException("EBlock not Found, Zerohash looked up");
            }
            var entryBlock = JsonConvert.DeserializeObject <DataStructs.EntryBlockDataStringFormat>(resp.Content);

            return(DataStructs.ConvertStringFormatToByteFormat(entryBlock));
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
        /// <summary>
        /// Takes in an entry chain hash and returns Key MR of the first entry. Can be used to
        /// get all the entries
        /// </summary>
        /// <param name="hash">ChainID of chain</param>
        /// <returns>KeyMR of first entry (last in list)</returns>
        public static DataStructs.ChainHeadData GetChainHead(byte[] hash)
        {
            var hashString = Arrays.ByteArrayToHex(hash);
            var req        = new RestRequest("/chain-head/{hash}", Method.GET);

            // var x = Arrays.ByteArrayToHex(hash);
            req.AddUrlSegment("hash", hashString);
            var resp = StaticValues.clientWallet.Execute(req);

            if (resp.Content.Contains("Chain not found"))
            {
                throw new FactomChainException("Chain not found");
            }
            try {
                var chainHead = JsonConvert.DeserializeObject <DataStructs.ChainHeadDataStringFormat>(resp.Content);
                return(DataStructs.ConvertStringFormatToByteFormat(chainHead));
            } catch (Exception) {
                throw new FactomEntryException("Error when serializing the chainhead. In GetChainHead: " + resp.Content);
            }
        }
Exemple #5
0
        /// <summary>
        /// Second and final step in adding an entry to a chain on the factom blockchain
        /// </summary>
        /// <param name="entry">Entry to be added</param>
        /// <returns>Boolean true/false for success/failure</returns>
        public static bool RevealEntry(DataStructs.EntryData entry)
        {
            var rev            = new Reveal();
            var marshaledEntry = Entries.MarshalBinary(entry);

            rev.Entry = Arrays.ByteArrayToHex(marshaledEntry);
            var req  = new RestRequest("/reveal-entry/", Method.POST);
            var json = JsonConvert.SerializeObject(rev);

            Console.WriteLine("RE Json = " + json);

            req.RequestFormat = DataFormat.Json;
            req.AddParameter("application/json", json, ParameterType.RequestBody);
            IRestResponse resp = StaticValues.clientWallet.Execute <RestRequest>(req);

            Console.WriteLine("RevealEntry Resp = " + resp.StatusCode + "|" + resp.StatusCode);

            if (resp.StatusCode != HttpStatusCode.OK)
            {
                throw new FactomEntryException("Entry Reveal Failed. Message: " + resp.ErrorMessage);
            }
            return(true);
        }
Exemple #6
0
        /// <summary>
        /// Second step in committing a new chain. Only run this if CommitChain was successful.
        /// </summary>
        /// <param name="c">Chain to be added</param>
        /// <returns>Boolean true/false for success/failure</returns>
        public static bool RevealChain(ChainType c)
        {
            var r = new Reveal();
            var b = Entries.MarshalBinary(c.FirstEntry);

            r.Entry = Arrays.ByteArrayToHex(b);

            var json     = JsonConvert.SerializeObject(r);
            var byteJson = Encoding.ASCII.GetBytes(json);

            var req = new RestRequest("/reveal-chain/", Method.POST);

            req.RequestFormat = DataFormat.Json;
            req.AddParameter("application/json", json, ParameterType.RequestBody);
            var resp = StaticValues.clientWallet.Execute(req);

            Console.WriteLine("RevealChain Resp = " + resp.StatusCode); //TODO: Remove

            if (resp.StatusCode != HttpStatusCode.OK)
            {
                throw new FactomChainException("Chain Reveal Failed. Message: " + resp.ErrorMessage);
            }
            return(true);
        }
Exemple #7
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));
        }