예제 #1
0
        private void SendCoin(string sender, string recipient, double amount, float fee)
        {
            var Txnin = new TxnInput
            {
                SenderAddress = sender,
                TimeStamp     = Utils.GetTime()
            };

            var TxnOut = new TxnOutput
            {
                RecipientAddress = recipient,
                Amount           = amount,
                Fee = fee,
            };

            var TxnHash   = Utils.GetTransactionHash(Txnin, TxnOut);
            var signature = account.CreateSignature(TxnHash);

            Txnin.Signature = signature;
            var sendRequest = new SendRequest
            {
                TxnId     = TxnHash,
                PublicKey = account.GetPubKeyHex(),
                TxnInput  = Txnin,
                TxnOutput = TxnOut
            };

            try
            {
                var responseSend = service.SendCoin(sendRequest);
                if (responseSend.Result.ToLower() == "success")
                {
                    Console.WriteLine("== success == ");
                }
                else
                {
                    Console.WriteLine("Error: {0}", responseSend.Result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
예제 #2
0
        public static string GetTransactionHash(TxnInput input, TxnOutput output)
        {
            var TxnId = GenHash(GenHash(input.TimeStamp + input.SenderAddress + output.Amount + output.Fee + output.RecipientAddress));

            return(TxnId);
        }
예제 #3
0
        private void DoSendCoin()
        {
            Console.Clear();
            Console.WriteLine("\n\n\n\nTransfer Coin");
            Console.WriteLine("Time: {0}", DateTime.Now);
            Console.WriteLine("======================");

            Console.WriteLine("Sender address:");
            string sender = account.GetAddress();

            Console.WriteLine(sender);


            Console.WriteLine("\nPlease enter the recipient address!:");
            string recipient = Console.ReadLine();

            Console.WriteLine("\nPlease enter the amount (number)!:");
            string strAmount = Console.ReadLine();

            Console.WriteLine("\nPlease enter fee (number)!:");
            string strFee = Console.ReadLine();
            double amount;

            if (string.IsNullOrEmpty(sender) ||
                string.IsNullOrEmpty(recipient) ||
                string.IsNullOrEmpty(strAmount) ||
                string.IsNullOrEmpty(strFee))
            {
                Console.WriteLine("\n\nError, Please input all data: sender, recipient, amount and fee!\n");
                return;
            }

            try
            {
                amount = double.Parse(strAmount);
            }
            catch
            {
                Console.WriteLine("\nError! You have inputted the wrong value for  the amount!");
                return;
            }

            float fee;

            try
            {
                fee = float.Parse(strFee);
            }
            catch
            {
                Console.WriteLine("\nError! You have inputted the wrong value for the fee!");
                return;
            }


            var response = service.GetBalance(new CommonRequest
            {
                Address = sender
            });

            var senderBalance = response.Balance;


            if ((amount + fee) > senderBalance)
            {
                Console.WriteLine("\nError! Sender ({0}) don't have enough balance!", sender);
                Console.WriteLine("Sender ({0}) balance is {1}", sender, senderBalance);
                return;
            }

            var Txnin = new TxnInput
            {
                SenderAddress = account.GetAddress(),
                TimeStamp     = Utils.GetTime(),
            };

            var TxnOut = new TxnOutput
            {
                RecipientAddress = recipient,
                Amount           = amount,
                Fee = fee,
            };

            var TxnHash   = Utils.GetTransactionHash(Txnin, TxnOut);
            var signature = account.CreateSignature(TxnHash);


            Txnin.Signature = signature;

            var sendRequest = new SendRequest
            {
                TxnId     = TxnHash,
                PublicKey = account.GetPubKeyHex(),
                TxnInput  = Txnin,
                TxnOutput = TxnOut
            };

            try
            {
                var responseSend = service.SendCoin(sendRequest);

                if (responseSend.Result.ToLower() == "success")
                {
                    DateTime utcDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(Convert.ToDouble(Txnin.TimeStamp));

                    Console.Clear();
                    Console.WriteLine("\n\n\n\nTransaction has send to Blockchain.!.");
                    Console.WriteLine("Timestamp: {0}", utcDate.ToLocalTime());
                    Console.WriteLine("Sender: {0}", Txnin.SenderAddress);
                    Console.WriteLine("Recipient {0}", TxnOut.RecipientAddress);
                    Console.WriteLine("Amount: {0}", TxnOut.Amount);
                    Console.WriteLine("Fee: {0}", TxnOut.Fee);
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Need around 1 minute to be processed!");
                }
                else
                {
                    Console.WriteLine("Error: {0}", responseSend.Result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }