示例#1
0
        private static Block CreateGenesisBlock()
        {
            var transactions = new List <Transaction>();

            foreach (var account in Ico.Accounts)
            {
                var transaction = new Transaction("ico", account.Address, account.Balance, 0.0F);

                transaction.Hash = TransactionHelpers.GetTransactionHash(transaction);

                transactions.Add(transaction);
            }

            return(new Block(
                       height: 0,
                       previousHash: string.Empty,
                       transactions: transactions.ToArray(),
                       validator: "Admin"));
        }
        public override Task <TransactionResponse> SendCoin(SendRequest request, ServerCallContext context)
        {
            // Create new transaction
            var newTrx = new Transaction(
                request.TransactionInput.SenderAddress,
                request.TransactionOutput.RecipientAddress,
                request.TransactionOutput.Amount,
                request.TransactionOutput.Fee)
            {
                Hash = request.TransactionId, TimeStamp = request.TransactionInput.TimeStamp
            };

            // verify transaction ID
            var trxHash = TransactionHelpers.GetTransactionHash(newTrx);

            if (!trxHash.Equals(request.TransactionId))
            {
                return(Task.FromResult(new TransactionResponse
                {
                    Result = "Transaction ID not valid",
                }));
            }

            // Verify signature
            var isValid = TransactionHelpers.VerifySignature(request.PublicKey, request.TransactionId, request.TransactionInput.Signature);

            if (!isValid)
            {
                return(Task.FromResult(new TransactionResponse()
                {
                    Result = "Signature not valid",
                }));
            }

            this.blockChain.AddTransaction(newTrx);

            return(Task.FromResult(new TransactionResponse()
            {
                Result = "Success",
            }));
        }
示例#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 = this.account.Address;

            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;
            }

            var success = double.TryParse(strAmount, out amount);

            if (!success)
            {
                Console.WriteLine("\nError! You have inputted the wrong value for  the amount!");
                return;
            }

            success = float.TryParse(strFee, out var fee);

            if (!success)
            {
                Console.WriteLine("\nError! You have inputted the wrong value for the fee!");
                return;
            }

            var response = this.service.GetBalance(new AccountRequest
            {
                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 transactionInput = new TransactionInput()
            {
                SenderAddress = this.account.Address,
                TimeStamp     = DateTime.Now.Ticks,
            };

            var transactionOutput = new TransactionOutput()
            {
                RecipientAddress = recipient,
                Amount           = amount,
                Fee = fee,
            };

            var transactionHash = TransactionHelpers.GetTransactionHash(transactionInput, transactionOutput);
            var signature       = this.account.CreateSignature(transactionHash);

            transactionInput.Signature = signature;

            var sendRequest = new SendRequest
            {
                TransactionId     = transactionHash,
                PublicKey         = this.account.PublicKey,
                TransactionInput  = transactionInput,
                TransactionOutput = transactionOutput,
            };

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

                if (responseSend.Result.ToLower() == "success")
                {
                    Console.Clear();
                    Console.WriteLine("\n\n\n\nTransaction has send to Blockchain.!.");
                    Console.WriteLine("Sender: {0}", sender);
                    Console.WriteLine("Recipient {0}", recipient);
                    Console.WriteLine("Amount: {0}", amount);
                    Console.WriteLine("Fee: {0}", fee);
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Need around 30 second to be processed!");
                }
                else
                {
                    Console.WriteLine("Error: {0}", responseSend.Result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }