/// <summary> /// Read from Binary file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public EthereumUser ReadBinaryFile(string fileName) { EthereumUser user = new EthereumUser(); try { fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream); entriesList = new List <string>(); string decryptedValue = ""; while ((entry = streamReader.ReadLine()) != null) { decryptedValue = DecryptString(entry, Password); entriesList.Add(decryptedValue); } user.PublicAddress = entriesList[0]; user.PrivateKey = entriesList[1]; streamReader.Close(); fileStream.Close(); } catch (Exception ex) { } return(user); }
private void LoadAccount() { user = new EthereumUser(); user = fileHelper.ReadBinaryFile(fileHelper.FilePathGenerator(UserFile)); if (user != null) { tbPublicAddress.Text = user.PublicAddress; tbPrivateKey.Text = user.PrivateKey; } }
private void GetRecipientInfo() { fileHelper = new FileOperationsHelper(); EthereumUser userInfo = fileHelper.ReadBinaryFile(fileHelper.FilePathGenerator(Constants.RecipientFile)); if (userInfo != null) { tbAddress.Text = userInfo.PublicAddress; tbPrivateKey.Text = userInfo.PrivateKey; } }
/// <summary> /// Get recipient's saved Ethereum account details (If exists) /// </summary> private void GetRecipientInfo() { // Email recipient emailRecipient = new EthereumUser(); EthereumUser userInfo = fileHelper.ReadBinaryFile(fileHelper.FilePathGenerator(Constants.RecipientFile)); if (userInfo != null) { emailRecipient.PublicAddress = userInfo.PublicAddress; emailRecipient.PrivateKey = userInfo.PrivateKey; } }
private void GetSenderInfo() { // Email sender emailSender = new EthereumUser(); fileHelper = new FileOperationsHelper(); EthereumUser userInfo = fileHelper.ReadBinaryFile(fileHelper.FilePathGenerator(Constants.SenderFile)); if (userInfo != null) { emailSender.PublicAddress = userInfo.PublicAddress; emailSender.PrivateKey = userInfo.PrivateKey; } }
private void SaveAccount() { user = new EthereumUser(); user.PublicAddress = tbPublicAddress.Text.Trim(); user.PrivateKey = tbPrivateKey.Text.Trim(); if (fileHelper.WriteBinaryFile(fileHelper.FilePathGenerator(UserFile), user)) { lblStatus.Text = "Saved Successfully!"; } else { lblStatus.Text = "Unable to save. Please try again."; } }
/// <summary> /// Write into Binary file /// </summary> /// <param name="fileName"></param> /// <param name="publicAddress"></param> /// <param name="privateKey"></param> /// <returns></returns> public bool WriteBinaryFile(string fileName, EthereumUser user) { try { if (File.Exists(fileName)) { File.Delete(fileName); } fileStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite); StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.WriteLine(EncryptString(user.PublicAddress, Password)); streamWriter.WriteLine(EncryptString(user.PrivateKey, Password)); streamWriter.Close(); fileStream.Close(); return(true); } catch (Exception ex) { return(false); } }
/// <summary> /// Used by the sender to get the public encryption key provided by the recipient that will be used to encrypt the shared key. /// </summary> /// <param name="ethContract"></param> /// <param name="sender"></param> /// <param name="emailId"></param> /// <returns></returns> protected internal async Task <string> GetEmailEncryptionPublicKeyWithTransaction(EthereumContract ethContract, EthereumUser sender, string emailId) { var contract = web3.Eth.GetContract(ethContract.ContractABI, ethContract.ContractAddress); var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(sender.PublicAddress).ConfigureAwait(false); var function = contract.GetFunction("getEmailEncryptionPublicKey"); var data = function.GetData(emailId); var encoded = web3.OfflineTransactionSigning.SignTransaction(sender.PrivateKey, ethContract.ContractAddress, 0, txCount.Value + 1, 1000000000000L, 900000, data); return(await web3.Eth.Transactions.SendRawTransaction.SendRequestAsync(encoded).ConfigureAwait(false)); }
/// <summary> /// Used by the recipient to confirm the email receival. The recipient uses this method to create a transaction on Ethereum with the received email hash and the public encryption key of the public/private generated RSA encryption keypair. /// </summary> /// <param name="ethContract"></param> /// <param name="recipient"></param> /// <param name="emailId"></param> /// <param name="emailHash"></param> /// <param name="encryptionPublicKey"></param> /// <returns></returns> protected internal async Task <string> ConfirmReceivalAndUploadEmailHash(EthereumContract ethContract, EthereumUser recipient, string emailId, string emailHash, string encryptionPublicKey) { var contract = web3.Eth.GetContract(ethContract.ContractABI, ethContract.ContractAddress); var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(recipient.PublicAddress).ConfigureAwait(false); var function = contract.GetFunction("confirmReceivalAndUploadEmailHash"); var data = function.GetData(emailId, emailHash, encryptionPublicKey); var encoded = web3.OfflineTransactionSigning.SignTransaction(recipient.PrivateKey, ethContract.ContractAddress, 0, txCount.Value, 1000000000000L, 900000, data); return(await web3.Eth.Transactions.SendRawTransaction.SendRequestAsync(encoded).ConfigureAwait(false)); }