public PaymentTransaction AddTransaction(PaymentTransaction newTransaction)
 {
     var result = QueryStoreProcedure("AddTransaction", new Dictionary<string, object>
                                                   {
                                                       {"@issuerId", newTransaction.IssuerId},
                                                       {"@source", newTransaction.Source},
                                                       {"@dest", newTransaction.Dest},
                                                       {"@amount", newTransaction.Amount},
                                                       {"@currencyCode", newTransaction.CurrencyCode},
                                                       {"@memoData", newTransaction.MemoData},
                                                   });
     if (result.Tables[0].Rows.Count > 0)
     {
         var acct = new PaymentTransaction().FromRow(result.Tables[0].Rows[0]);
         return acct;
     }
     return null;
 }
        private List<PaymentTransaction> GetAllTransactions()
        {
            List<PaymentTransaction> result = new List<PaymentTransaction>();
            string requestUri = "certifier/api/blocks";
            HttpResponseMessage response = HttpClient.GetAsync(requestUri).Result;
            if (response.IsSuccessStatusCode)
            {
                List<HigherLevelBlock> transactions = response.Content.ReadAsAsync<List<HigherLevelBlock>>().Result;
                foreach (var block in transactions)
                {
                    PaymentTransaction trans = new PaymentTransaction();
                    trans = block.LowerLevelBlockSet[0].TransactionSet[0];
                    result.Add(trans);
                }
            }

            return result;
        }
        public void CalculateBalanceTest()
        {
            int issuerId = 1942;
            // step1: create three account
            // first
            string privateKey, publicKey;
            CryptoHelper.GenerateKeyPair(out privateKey, out publicKey);
            string fingerPrint = CryptoHelper.Hash(publicKey);
            string address = FiatCoinHelper.ToAddress(issuerId, fingerPrint);
            var account = new PaymentAccount
            {
                Address = address,
                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(issuerId, fingerPrint2);
            var account2 = new PaymentAccount
            {
                Address = address2,
                CurrencyCode = "CNY",
                PublicKey = publicKey,
                PrivateKey = privateKey
            };
            // third
            string privateKey3, publicKey3;
            CryptoHelper.GenerateKeyPair(out privateKey3, out publicKey3);
            string fingerPrint3 = CryptoHelper.Hash(publicKey3);
            string address3 = FiatCoinHelper.ToAddress(issuerId, fingerPrint3);
            var account3 = new PaymentAccount
            {
                Address = address2,
                CurrencyCode = "CNY",
                PublicKey = publicKey,
                PrivateKey = privateKey
            };

            // step2 create two transactions
            var trx1 = new PaymentTransaction
            {
                Source = address,
                Dest = address2,
                Amount = 100.00m,
                CurrencyCode = "USD",
                MemoData = "surface"
            };
            var trx2 = new PaymentTransaction
            {
                Source = address2,
                Dest = address3,
                Amount = 55.00m,
                CurrencyCode = "USD",
                MemoData = "surface"
            };
            var journal = new List<PaymentTransaction>();
            journal.Add(trx1);
            journal.Add(trx2);

            // step3 calculate & verify
            var balance = FiatCoinHelper.CalculateBalance(journal, address2);

            Assert.AreEqual(45m, balance);
        }
Exemple #4
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);
        }
Exemple #5
0
 private bool VerifyTransaction(PaymentTransaction transaction)
 {
     throw new NotImplementedException();
 }