示例#1
0
        public void TestKey_GoodData_ReturnTrue()
        {
            //Arrange
            //Act
            EccService.GenerateKey(out var privateKey, out var publicKey);

            //Assert
            Assert.True(EccService.TestKey(privateKey, publicKey));
        }
        private void ButtonToPay_Click(object sender, RoutedEventArgs e)
        {
            if (int.Parse(TextBoxAmountAct.Text) > Amount)
            {
                MessageBox.Show("Error!", "You haven't avaliable ACT for makking transaction.", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return;
            }

            var recipientUserHash = HexConvert.ToBytes(TextBoxRecipientId.Text);

            var inEntry = new InEntry()
            {
                PublicKey = _userCoinPocket.KeyPair.PublicKey,
                Amount    = Amount
            };

            var outEntry = new List <OutEntry>(15)
            {
                new OutEntry()
                {
                    RecipientHash = recipientUserHash,
                    Value         = int.Parse(TextBoxAmountAct.Text)
                },
                new OutEntry()
                {
                    RecipientHash = _userCoinPocket.KeyPair.PublicKey,
                    Value         = Amount - int.Parse(TextBoxAmountAct.Text)
                }
            };

            var transaction = new Transaction()
            {
                Id        = DataManager.UploadBlockchainDictionary().Last().Value.Transaction.Id + 1,
                InEntries = new List <InEntry>()
                {
                    inEntry
                },
                OutEntries = outEntry,
                Signature  = EccService.Sign(_userCoinPocket.KeyPair.PublicKey, _userCoinPocket.KeyPair.PrivateKey,
                                             _userCoinPocket.KeyPair.PublicKey),
                Timestamp = DateTime.Now
            };

            var message = BlockchainUtil.SerializeTransaction(transaction);

            var senderUdpClient = new UdpClient(_userCoinPocket.ReceivePort);

            try
            {
                senderUdpClient.Send(
                    message,
                    message.Length,
                    "127.0.0.1",
                    9999);
            }

            catch
            {
                throw new Exception();
            }

            finally
            {
                senderUdpClient.Close();
            }

            var        receiver = new UdpClient(_userCoinPocket.ReceivePort);
            IPEndPoint remoteIp = null;
            var        data     = receiver.Receive(ref remoteIp);
            var        chain    = MessagePackSerializer.Deserialize <KeyValuePair <string, Block> >(data);
            var        json     = JsonConvert.SerializeObject(chain, Formatting.Indented);

            GroupBoxStatus.Header = "You trancsaction has been added";

            var blockchain = DataManager.UploadBlockchainDictionary();

            TextBlockAct.Text = Executor
                                .GetCashRec(blockchain, _userCoinPocket.KeyPair.PublicKey, blockchain.Last().Key).ToString();
            TextBlockStatus.Text = json;
            receiver.Close();
        }