Exemplo n.º 1
0
        public void ValidateTest()
        {
            int issuerId = 1942;

            // step1: create two 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,
                IssuerId = issuerId,
                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,
                IssuerId = issuerId,
                CurrencyCode = "CNY",
                PublicKey = publicKey,
                PrivateKey = privateKey
            };

            var payRequest = new DirectPayRequest
            {
                PaymentTransaction = new PaymentTransaction
                {
                    Source = address,
                    Dest = address2,
                    Amount = 10.00m,
                    CurrencyCode = "USD",
                    MemoData = "surface"
                }
            };
            payRequest.Signature = CryptoHelper.Sign(privateKey, payRequest.ToMessage());

            bool authorized = ValidationHelper.Validate(payRequest, publicKey);
            Assert.IsTrue(authorized);

            payRequest.PaymentTransaction.Dest = "Bad man";
            payRequest.PaymentTransaction.Amount = 10000.00m;
            payRequest.Timestamp = DateTime.Parse("2016-01-01");
            authorized = ValidationHelper.Validate(payRequest, publicKey);
            Assert.IsFalse(authorized);
        }
Exemplo n.º 2
0
 public PaymentAccount AddAccount(PaymentAccount newAccount)
 {
     var result = QueryStoreProcedure("AddAccount", new Dictionary<string, object>
                                                   {
                                                       {"@address", newAccount.Address},
                                                       {"@issuerId", newAccount.IssuerId},
                                                       {"@currencyCode", newAccount.CurrencyCode},
                                                       {"@publicKey", newAccount.PublicKey},
                                                   });
     if (result.Tables[0].Rows.Count > 0)
     {
         var acct = new PaymentAccount().FromRow(result.Tables[0].Rows[0]);
         return acct;
     }
     return null;
 }
Exemplo n.º 3
0
        private string NewAddressForExchange()
        {
            string privateKey;
            string publicKey;
            CryptoHelper.GenerateKeyPair(out privateKey, out publicKey);

            string fingerPrint = CryptoHelper.Hash(publicKey);
            int issuerId = FiatCoinHelper.GetIssuerId(exchangePayFrom.SelectedValue.ToString());
            string currencyCode = exchangeCurrency.SelectedValue.ToString();
            var account = new PaymentAccount
            {
                Address = FiatCoinHelper.ToAddress(issuerId, fingerPrint),
                CurrencyCode = currencyCode,
                Balance = 0.00m,
                PublicKey = publicKey,
                PrivateKey = null
            };
            // register
            string requestUri = string.Format("issuer/api/{0}/accounts/register", issuerId);
            var registerRequest = new RegisterRequest
            {
                PaymentAccount = account.Mask()
            };
            HttpContent content = new StringContent(JsonHelper.Serialize(registerRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();

            account.PrivateKey = privateKey;
            this.m_Wallet.PaymentAccounts.Add(account);
            return account.Address;
        }
Exemplo n.º 4
0
        private void miNewAddress_Click(object sender, RoutedEventArgs e)
        {
            string privateKey;
            string publicKey;
            CryptoHelper.GenerateKeyPair(out privateKey, out publicKey);

            string fingerPrint = CryptoHelper.Hash(publicKey);
            int issuerId = 0;
            try
            {
                issuerId = (int)comboBoxIssuer.SelectedValue;
            }
            catch(Exception)
            {
                MessageBox.Show("请选择开户银行","警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            string currencyCode = (string)comboBoxCurrencyCode.SelectedValue;
            if(currencyCode == null)
            {
                MessageBox.Show("请选择交易货币代码", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            var account = new PaymentAccount
            {
                Address = FiatCoinHelper.ToAddress(issuerId, fingerPrint),
                CurrencyCode = currencyCode,
                Balance = 0.00m,
                PublicKey = publicKey,
                PrivateKey = null
            };

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

            account.PrivateKey = privateKey;
            this.m_Wallet.PaymentAccounts.Add(account);

            //Allocate initial balance
            string baseAccount = "8gMAAA==+u3qZ1H9Ha0dOT6WX3d7Hr9npKQRreoFdGp4VourKtQ=";
            requestUri = string.Format("issuer/api/{0}/accounts/pay", issuerId);
            Random ran = new Random();
            int i_ranAmount = ran.Next(1, 499);
            float f_ranAmount = (float)(i_ranAmount * 0.01);
            var payRequest = new DirectPayRequest
            {
                PaymentTransaction = new PaymentTransaction
                {
                    Source = baseAccount,
                    Dest = Convert.ToBase64String(BitConverter.GetBytes(issuerId)) + fingerPrint,
                    Amount = Convert.ToDecimal(f_ranAmount),
                    CurrencyCode = currencyCode,
                    MemoData = "Initial-balance"
                }
            };
            payRequest.Signature = CryptoHelper.Sign("RUNTMiAAAAA7Fyutk/Pd2VotgUewM7QpS0lTMUwZC0PewDg47HFhIoq0rjlnUTraDpS5gurmvVybU357HBOZkX3aKon4FcSdrLKIvEgjHUbRuUt2bze5HNflkQRitCWbxYc7FVGxlog=", payRequest.ToMessage());
            content = new StringContent(JsonHelper.Serialize(payRequest));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();

            GetAccountBalances();
            this.UpdateAddressDataGrid();
            this.Save();
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 public PaymentAccount GetAccount(int issuerId, string address)
 {
     var result = QueryStoreProcedure("GetAccount", new Dictionary<string, object>
                                                   {
                                                       {"@issuerId", issuerId},
                                                       {"@address", address},
                                                   });
     if (result.Tables[0].Rows.Count > 0)
     {
         var acct = new PaymentAccount().FromRow(result.Tables[0].Rows[0]);
         return acct;
     }
     return null;
 }
Exemplo n.º 7
0
        public void ValidateRequestor(BaseRequest request, PaymentAccount account)
        {
            string publicKey = account.PublicKey;
            string signature = request.Signature;
            request.Signature = null;
            string jsonString = JsonHelper.Serialize(request);
            request.Signature = signature;
            bool authorized = CryptoHelper.Verify(publicKey, jsonString, signature);

            if (!authorized)
            {
                var message = string.Format("User is not authorized to operate on the object.");
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, message));
            }
        }
Exemplo n.º 8
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);
        }