示例#1
0
        static void Main(string[] args)
        {
            logger.Info("prepare issuers");
            HttpResponseMessage response = GetClient().GetAsync("certifier/api/issuers").Result;
            response.EnsureSuccessStatusCode();
            var jsonString = response.Content.ReadAsStringAsync();
            List<Issuer> issuers = JsonConvert.DeserializeObject<List<Issuer>>(jsonString.Result);

            int count = issuers.Count;

            logger.Info("create accounts");
            List<PaymentAccount> accounts = new List<PaymentAccount>();
            for (int i = 1; i <= NumberOfAccounts; i++)
            {
                accounts.Add(new PaymentAccount());
            }

            Parallel.ForEach(accounts, account =>
            {
                int index = (new Random()).Next() % count;
                int issuerId = issuers[index].Id;
                string privateKey, publicKey;
                CryptoHelper.GenerateKeyPair(out privateKey, out publicKey);
                string fingerPrint = CryptoHelper.Hash(publicKey);
                string address = FiatCoinHelper.ToAddress(issuerId, fingerPrint);
                account.Address = address;
                account.CurrencyCode = "USD";
                account.PublicKey = publicKey;
                account.PrivateKey = privateKey;
            });

            logger.Info("register accounts");
            Parallel.ForEach(accounts, account =>
            {
                try
                {
                    int issuerId = FiatCoinHelper.GetIssuerId(account.Address);
                    string requestUri = string.Format("issuer/api/{0}/accounts/register", issuerId);
                    var registerRequest = new RegisterRequest
                    {
                        PaymentAccount = account
                    };
                    HttpContent content = new StringContent(JsonHelper.Serialize(registerRequest));
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    response = GetClient().PostAsync(requestUri, content).Result;
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message, ex);
                }
            });

            logger.Info("fund accounts");
            Parallel.ForEach(accounts, account =>
            {
                try
                {
                    int issuerId = FiatCoinHelper.GetIssuerId(account.Address);
                    string requestUri = string.Format("issuer/api/{0}/accounts/fund", issuerId);
                    var fundRequest = new FundRequest
                    {
                        PaymentTransaction = new PaymentTransaction
                        {
                            Source = FiatCoinHelper.EncodeIssuerId(issuerId),
                            Dest = account.Address,
                            Amount = Convert.ToDecimal((new Random()).NextDouble()) * 100.00m,
                            CurrencyCode = "USD",
                            MemoData = "fund with CC"
                        }
                    };
                    HttpContent content = new StringContent(JsonHelper.Serialize(fundRequest));
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    response = GetClient().PostAsync(requestUri, content).Result;
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message, ex);
                }
            });

            logger.Info("direct pay");
            var result = Stress.DoWork(() =>
            {
                var transaction = new PaymentTransaction
                {
                    Amount = Convert.ToDecimal((new Random()).NextDouble()) * 20.00m,
                    CurrencyCode = "USD",
                    Source = accounts[(new Random()).Next() % NumberOfAccounts].Address,
                    Dest = accounts[(new Random()).Next() % NumberOfAccounts].Address,
                    MemoData = "TODO: RANDOM STRING"
                };

                int issuerId = FiatCoinHelper.GetIssuerId(transaction.Source);
                string requestUri = string.Format("issuer/api/{0}/accounts/pay", issuerId);
                var payRequest = new DirectPayRequest
                {
                    PaymentTransaction = transaction
                };
                var account = accounts.FirstOrDefault<PaymentAccount>(acct => acct.Address == transaction.Source);
                payRequest.Signature = CryptoHelper.Sign(account.PrivateKey, payRequest.ToMessage());
                HttpContent content = new StringContent(JsonHelper.Serialize(payRequest));
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = GetClient().PostAsync(requestUri, content).Result;
            });

            logger.Info(result);
        }
示例#2
0
        public void IssuerControllerTest()
        {
            int issuerId = 1942;
            int sourceIssuerId = 1942;
            int destIssuerId = 1010;

            // step1: create two account
            // first
            string privateKey, publicKey;
            CryptoHelper.GenerateKeyPair(out privateKey, out publicKey);
            string fingerPrint = CryptoHelper.Hash(publicKey);
            string address = FiatCoinHelper.ToAddress(sourceIssuerId, fingerPrint);
            var account = new PaymentAccount
            {
                Address = address,
                IssuerId = sourceIssuerId,
                CurrencyCode = "USD",
                PublicKey = publicKey,
                PrivateKey = privateKey
            };
            // second
            string privateKey2, publicKey2;
            CryptoHelper.GenerateKeyPair(out privateKey2, out publicKey2);
            string fingerPrint2 = CryptoHelper.Hash(publicKey2);
            string address2 = FiatCoinHelper.ToAddress(destIssuerId, fingerPrint2);
            var account2 = new PaymentAccount
            {
                Address = address2,
                IssuerId = destIssuerId,
                CurrencyCode = "USD",
                PublicKey = publicKey,
                PrivateKey = privateKey
            };

            // step2: register
            string requestUri = string.Format("issuer/api/{0}/accounts/register", issuerId);
            var registerRequest = new RegisterRequest
            {
                PaymentAccount = account
            };
            HttpContent content = new StringContent(JsonHelper.Serialize(registerRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();

            // step3: fund
            requestUri = string.Format("issuer/api/{0}/accounts/fund", issuerId);
            var fundRequest = new FundRequest
            {
                PaymentTransaction = new PaymentTransaction
                {
                    IssuerId = issuerId,
                    Source = FiatCoinHelper.EncodeIssuerId(issuerId),
                    Dest = address,
                    Amount = 100.00m,
                    CurrencyCode = "USD",
                    MemoData = "fund with CC"
                }
            };
            content = new StringContent(JsonHelper.Serialize(fundRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();

            // step4: get this account & verify
            requestUri = string.Format("issuer/api/{0}/accounts/get", issuerId);
            var getRequest = new GetAccountRequest
            {
                Address = address
            };
            content = new StringContent(JsonHelper.Serialize(getRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();
            var jsonString = response.Content.ReadAsStringAsync();
            var account3 = JsonConvert.DeserializeObject<PaymentAccount>(jsonString.Result);
            Assert.AreEqual(account.Address, account3.Address);
            Assert.AreEqual(account.PublicKey, account3.PublicKey);
            Assert.AreEqual(100.00m, account3.Balance);

            // step5: direct pay
            requestUri = string.Format("issuer/api/{0}/accounts/pay", issuerId);
            var payRequest = new DirectPayRequest
            {
                PaymentTransaction = new PaymentTransaction
                {
                     IssuerId = issuerId,
                     Source = address,
                     Dest = address2,
                     Amount = 10.00m,
                     CurrencyCode = "USD",
                     MemoData = "surface"
                }
            };
            payRequest.Signature = CryptoHelper.Sign(privateKey, payRequest.ToMessage());
            content = new StringContent(JsonHelper.Serialize(payRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();

            // step6: get & verify
            requestUri = string.Format("issuer/api/{0}/accounts/get", issuerId);
            getRequest = new GetAccountRequest
            {
                Address = address
            };
            content = new StringContent(JsonHelper.Serialize(getRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();
            jsonString = response.Content.ReadAsStringAsync();
            account3 = JsonConvert.DeserializeObject<PaymentAccount>(jsonString.Result);
            Assert.AreEqual(90.00m, account3.Balance);

            // step7: delete this account
            requestUri = string.Format("issuer/api/{0}/accounts/unregister", issuerId);
            var unregisterRequest = new UnregisterRequest
            {
                Address = address
            };
            unregisterRequest.Signature = CryptoHelper.Sign(privateKey, unregisterRequest.ToMessage());
            content = new StringContent(JsonHelper.Serialize(unregisterRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();

            // step8: get & verify
            requestUri = string.Format("issuer/api/{0}/accounts/get", issuerId);
            getRequest = new GetAccountRequest
            {
                Address = address
            };
            content = new StringContent(JsonHelper.Serialize(getRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = HttpClient.PostAsync(requestUri, content).Result;
            Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
        }