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(); }
/// <summary> /// The AES Key/Value with the Private Key for Contract is for demonstration purpose only /// Feel free to use it. /// </summary> /// <param name="args"></param> public static void Main(string[] args) { string key = "aesKey"; string value = "testing"; string rnsResolvedAddress = GetRnsResolvedAddress("ranjancse.rsk", true); string nodeUrl = ConfigurationManager.AppSettings["RskTestnetNodeUrl"]; var privateKey = ConfigurationManager.AppSettings["PrivateKey"]; var fromTransferPrivateKey = ConfigurationManager.AppSettings["FromTransferPrivateKey"]; var account = new Nethereum.Web3.Accounts.Account(privateKey); IGasPriceService gasPriceService = new GasPriceService(nodeUrl); int gasPrice = gasPriceService.GetRskMinGasPrice(); // Load some RBTC LoadSomeRBTC(nodeUrl, fromTransferPrivateKey, account.Address, 0.001m, 0.06m); Stopwatch stopwatch = new Stopwatch(); System.Console.WriteLine("Trying to pull the RSA certificate from the local store using the Thumbprint"); // Get the certificate by Thumbprint string thumbPrint = ConfigurationManager.AppSettings["Thumbprint"].ToUpper(); X509Certificate2 filteredCert = X509CertificateHelper.GetRSKCertificate(thumbPrint, StoreLocation.LocalMachine); if (filteredCert == null) { System.Console.WriteLine($"Unable to find the RSK certificate by Thumbprint: " + $"{thumbPrint}"); System.Console.ReadLine(); return; } // Encrypt Text/Data var encryptedText = RSAEncryptionHelper.Encrypt(value, filteredCert); var url = ConfigurationManager.AppSettings["ContractDeploymentUrl"]; Web3 web3 = new Web3(account, url); // Get the balance stopwatch.Start(); var weiBalance = AccountHelper.GetBalance(web3, account); var etherAmount = Web3.Convert.FromWei(weiBalance.Value); stopwatch.Stop(); System.Console.WriteLine($"Account Balance: {etherAmount}"); System.Console.WriteLine($"Time take to fetch the balance:" + $" {stopwatch.Elapsed.Seconds} seconds"); // Gas estimated, in wei System.Console.WriteLine($"Estimated Gas Price: {gasPrice}"); System.Console.WriteLine("Deploying the Iterable Mapping Library"); stopwatch.Restart(); // Deploy Iterable Mapping Library TransactionReceipt transactionReceiptDeployment; string contractAddress; ContractHandler contractHandler; RSKContractHelper.DeployIterableMappingContract(web3, out transactionReceiptDeployment, out contractAddress, out contractHandler); stopwatch.Stop(); System.Console.WriteLine($"Iterable Mapping Contarct Address: " + $"{contractAddress}"); System.Console.WriteLine($"Time taken to deploy the Iterable mapping:" + $" {stopwatch.Elapsed.Seconds} seconds"); System.Console.WriteLine("Deploying the RSK KMS Contract"); // Deploy the RSK Contract stopwatch.Restart(); contractHandler = RSKContractHelper.DeployRSKKeyManagmentContract(web3, transactionReceiptDeployment, out contractAddress); stopwatch.Stop(); System.Console.WriteLine($"RSK Contract Address {contractAddress}"); System.Console.WriteLine($"Time taken to deploy the RSK Contract: " + $"{stopwatch.Elapsed.Seconds} seconds"); System.Console.WriteLine("Trying to set a value in RSK KMS Contract"); /** Function: setItem**/ var setItemRequest = new SetItemFunction { Key = key, Value = encryptedText, FromAddress = account.Address }; setItemRequest.GasPrice = new BigInteger(gasPrice * 1.1); stopwatch.Restart(); var setItemFunctionTxnReceipt = contractHandler .SendRequestAndWaitForReceiptAsync(setItemRequest) .ConfigureAwait(false) .GetAwaiter() .GetResult(); stopwatch.Stop(); System.Console.WriteLine($"Time taken to set the KMS Key Item: " + $"{stopwatch.Elapsed.Seconds} seconds"); System.Console.WriteLine("Trying to get a value from the RSK KMS Contract"); /** Function: getItem**/ var getItemRequest = new GetItemFunction { Key = key, FromAddress = account.Address }; stopwatch.Restart(); var getItemResponse = contractHandler .QueryAsync <GetItemFunction, string>(getItemRequest) .ConfigureAwait(false) .GetAwaiter() .GetResult(); stopwatch.Stop(); System.Console.WriteLine($"Time taken to get the KMS Key Item: " + $"{stopwatch.Elapsed.Seconds} seconds"); if (!string.IsNullOrEmpty(getItemResponse)) { var decryptedText = RSAEncryptionHelper.Decrypt(getItemResponse, filteredCert); System.Console.WriteLine($"Decrypted Text: {decryptedText}"); } else { System.Console.WriteLine("The KMS Response as empty"); } System.Console.WriteLine("Press any key to exit"); System.Console.ReadLine(); }