Exemple #1
0
        private async void btnKMSSetItem_Click(object sender, EventArgs e)
        {
            if (ValidateKMSSet() == false)
            {
                return;
            }

            if (contractHandler == null &&
                !string.IsNullOrEmpty(txtKMSContractAddress.Text.Trim()))
            {
                Web3 web3 = GetWeb3();
                contractHandler = web3.Eth.GetContractHandler(txtKMSContractAddress.Text.Trim());
            }

            if (contractHandler == null)
            {
                MessageBox.Show("Problem in getting an instance of the Contract Handler. " +
                                "Please try specifying a valid Contract Address", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            StartProgressBar();

            btnKMSSetItem.Enabled = false;
            var encryptedText = RSAEncryptionHelper.Encrypt(txtValue.Text.Trim(), filteredCert);
            var account       = new Nethereum.Web3.Accounts.Account(privateKey);

            var setItemRequest = new SetItemFunction
            {
                Key         = txtKeyName.Text.Trim(),
                Value       = encryptedText,
                FromAddress = account.Address
            };

            // Set the Gas value
            var estimate = await contractHandler
                           .EstimateGasAsync(setItemRequest);

            setItemRequest.Gas = estimate.Value;

            var setItemFunctionTxnReceipt = await contractHandler
                                            .SendRequestAndWaitForReceiptAsync(setItemRequest);

            if (setItemFunctionTxnReceipt != null &&
                setItemFunctionTxnReceipt.BlockNumber.Value > 0)
            {
                string[]     keyCollection = new string[3];
                ListViewItem listItem;
                keyCollection[0] = txtKeyName.Text.Trim();
                keyCollection[1] = encryptedText;
                keyCollection[2] = "";
                listItem         = new ListViewItem(keyCollection);
                listView1.Items.Add(listItem);
            }

            btnKMSSetItem.Enabled = true;

            StopProgressBar();
        }