Exemplo n.º 1
0
        private static async Task <Coin> TransferCoin(Guid serialNumber, string issuingAuthority, List <string> holdersPublicKey)
        {
            Transfer transfer = new Transfer();

            transfer.SerialNumber     = serialNumber;
            transfer.IssuingAuthority = issuingAuthority;
            transfer.Timestamp        = DateTime.UtcNow;
            transfer.Holders          = new List <Holder>();

            for (int i = 0; i < holdersPublicKey.Count; i++)
            {
                transfer.Holders.Add(new Holder()
                {
                    PublicKey = holdersPublicKey[i]
                });
            }

            transfer.Signature = CoinTools.ECSign(JsonConvert.SerializeObject(transfer), privateECKey);

            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://localhost:44341/api/Transfer"))
            {
                request.Content = new StringContent(JsonConvert.SerializeObject(transfer));
                request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                request.Headers.Add("x-api-key", "86A5DFC6-0EB7-4249-B32A-366F2F8F9A51");

                HttpResponseMessage response = null;

                try
                {
                    using (HttpClient client = new HttpClient())
                        response = await client.SendAsync(request);

                    if (response != null)
                    {
                        return(JsonConvert.DeserializeObject <Coin>(await response.Content.ReadAsStringAsync()));
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
            }
        }
Exemplo n.º 2
0
        private static async Task <List <Coin> > SearchCoin(string publicKey)
        {
            Search search = new Search();

            search.PublicKey = publicKey;
            search.Timestamp = DateTime.UtcNow;
            search.Signature = CoinTools.ECSign(JsonConvert.SerializeObject(search), transferPrivateKey);

            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://localhost:44341/api/Search"))
            {
                request.Content = new StringContent(JsonConvert.SerializeObject(search));
                request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                request.Headers.Add("x-api-key", "86A5DFC6-0EB7-4249-B32A-366F2F8F9A51");

                HttpResponseMessage response = null;

                try
                {
                    using (HttpClient client = new HttpClient())
                        response = await client.SendAsync(request);

                    if (response != null)
                    {
                        return(JsonConvert.DeserializeObject <List <Coin> >(await response.Content.ReadAsStringAsync()));
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
            }
        }