public async Task <bool> deployDistrictBallot() { bool success = false; try { MySqlCommand comm; //access ui elements from Task. Source of code: https://stackoverflow.com/questions/11109042/getting-the-text-in-a-richtextbox-thats-on-another-thread var districtID = (string)dt_nullDistrict.Invoke(new Func <string>(() => dt_nullDistrict.Rows[selectedIndex].Cells["districtID"].Value.ToString())); var districtName = (string)dt_nullDistrict.Invoke(new Func <string>(() => dt_nullDistrict.Rows[selectedIndex].Cells["District Name"].Value.ToString())); //blockchain interactions string districtKey = "0x" + txt_districtKey.Text; Account sub_account = new Account(districtKey); string districtKeyString = districtKey.Substring(2, 4) + districtKey.Substring(districtKey.Length - 4, 4); string keyString = privateKey.Substring(2, 4) + privateKey.Substring(privateKey.Length - 4, 4); Bytes32TypeEncoder encoder = new Bytes32TypeEncoder(); byte[] districtIDbyte = encoder.Encode(districtID); byte[] byte_keyString = encoder.Encode(keyString); byte[] byte_districtKeyString = encoder.Encode(districtKeyString); var theContract = web3.Eth.GetContract(getContractABI(), getContractAddress()); var spawnBallotFunction = theContract.GetFunction("spawn_ballot"); var getChildContract = theContract.GetFunction("getDeployedChildContracts"); var spawnBallot = await spawnBallotFunction.SendTransactionAndWaitForReceiptAsync(account.Address, gasLimit, gasPrice, null, null, districtID, byte_districtKeyString, byte_keyString); var ballotAddress = await getChildContract.CallAsync <string>(byte_keyString); MessageBox.Show("Contract for " + districtName + " has been deployed successfully!"); //store into the database //random string for the salt source:https://www.dotnetperls.com/random-string string salt = RandomString.GetRandomString(); string contractAddressEncryption = "CONCAT('" + salt + "', ':', HEX(AES_ENCRYPT('" + ballotAddress + "', CONCAT('XwncLQ=dbd#qLB3p', '" + salt + "'))))"; string contractKeyEncryption = "CONCAT('" + salt + "', ':', HEX(AES_ENCRYPT('" + txt_districtKey.Text + "', CONCAT('XwncLQ=dbd#qLB3p', '" + salt + "'))))"; string listContract = "UPDATE `electiondb`.`district` SET `districtContractAddress` = " + contractAddressEncryption + ", `salt` ='" + salt + "'," + "`deploymentcost` = '" + spawnBallot.GasUsed.ToString() + " wei', `districtAccountKey` = " + contractKeyEncryption + " WHERE (`districtID` = '" + districtID + "');"; conn.Open(); comm = new MySqlCommand(listContract, conn); comm.ExecuteNonQuery(); conn.Close(); success = true; } catch (Exception ex) { MessageBox.Show("The following error has occured: " + ex.Message, "An error has occured...", MessageBoxButtons.OK , MessageBoxIcon.Error); success = false; } return(success); }
public async Task <bool> deployContractTask() { string contractBytecode = txt_contractBytecode.Text; string contractABI = txt_contractABI.Text; string keyString = privateKey.Substring(2, 4) + privateKey.Substring(privateKey.Length - 4, 4); Bytes32TypeEncoder encoder = new Bytes32TypeEncoder(); byte[] byte_key = encoder.Encode(keyString); bool success = false; try { var contractDeployment = new ContractDeployer(txt_contractBytecode.Text) { _key = byte_key }; var deploymentHandler = web3.Eth.GetContractDeploymentHandler <ContractDeployer>(); var deploy = await deploymentHandler.SendRequestAndWaitForReceiptAsync(contractDeployment); contractAddress = deploy.ContractAddress; MessageBox.Show("Contract deployed successfully!", "Contract Deployed", MessageBoxButtons.OK, MessageBoxIcon.Information); //random string for the salt source:https://www.dotnetperls.com/random-string string salt = RandomString.GetRandomString(); string contractAddressEncryption = "CONCAT('" + salt + "', ':', HEX(AES_ENCRYPT('" + contractAddress + "', CONCAT('XwncLQ=dbd#qLB3p', '" + salt + "'))))"; //Store contract address into the database string listContract = "INSERT INTO `electiondb`.`smartcontracts` (`contractAddress`, `contractABI`, `contractName`, `deployedBy`,`deployDate`,`salt`,`deploymentcost`) VALUES" + " (" + contractAddressEncryption + ", '" + contractABI + "' , '" + txt_contractName.Text + "', '" + adminID + "', now(),'" + salt + "','" + deploy.GasUsed.ToString() + " wei');"; conn.Open(); MySqlCommand comm = new MySqlCommand(listContract, conn); comm.ExecuteNonQuery(); conn.Close(); success = true; } catch (Exception ex) { MessageBox.Show("The following error has occured: " + ex.Message, "An error has occured...", MessageBoxButtons.OK , MessageBoxIcon.Error); success = false; } return(success); }
public async Task <bool> blockchain_registerVoter() { bool success = false; MySqlCommand comm; DataTable voterTable = new DataTable(); DataTable key = new DataTable(); //access ui elements from Task. Source of code: https://stackoverflow.com/questions/11109042/getting-the-text-in-a-richtextbox-thats-on-another-thread var firstname = (string)txt_firstname.Invoke(new Func <string>(() => txt_firstname.Text)); var middlename = (string)txt_middlename.Invoke(new Func <string>(() => txt_middlename.Text)); var lastname = (string)txt_lastname.Invoke(new Func <string>(() => txt_lastname.Text)); var district = (int)cmb_districts.Invoke(new Func <int>(() => cmb_districts.SelectedIndex)); var precinct = (int)cmb_districts.Invoke(new Func <int>(() => cmb_precinct.SelectedIndex)); var password = (string)txt_voterPassword.Invoke(new Func <string>(() => txt_voterPassword.Text)); setDistrictAccount(districts.Rows[district][0].ToString()); try { //Add voters to the database string salt = RandomString.GetRandomString(); string passwordVal = "sha2(concat('" + txt_voterPassword.Text + "','" + salt + "'),512)"; string insertVoter = "INSERT INTO `electiondb`.`voter` (`firstname`, `lastname`, `middlename`, `addedBy`, `districtID`, `precinctID`,`voterPassword`,`salt`)" + " VALUES ('" + txt_firstname.Text + "', '" + txt_lastname.Text + "', '" + txt_middlename.Text + "', '" + adminID + "', " + districts.Rows[district][0].ToString() + ", " + precincts.Rows[precinct][0].ToString() + ", " + passwordVal + ", '" + salt + "');"; conn.Open(); comm = new MySqlCommand(insertVoter, conn); comm.ExecuteNonQuery(); conn.Close(); conn.Open(); string readVoter = "select voterID, districtContractAddress, middlename, precinctID from electiondb.voter c join electiondb.district d on c.districtID = d.districtID where c.firstname = '" + firstname + "' and c.lastname = '" + lastname + "' and c.middlename = '" + middlename + "' and c.districtID = " + districts.Rows[district][0].ToString() + ";"; comm = new MySqlCommand(readVoter, conn); MySqlDataAdapter adp = new MySqlDataAdapter(comm); adp.Fill(voterTable); conn.Close(); //add voters to the blockchain var ballotABI = @"[{""inputs"":[{""internalType"":""bytes32"",""name"":""_main_key"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_sub_key"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_district"",""type"":""bytes32""}],""stateMutability"":""nonpayable"",""type"":""constructor""},{""inputs"":[{""internalType"":""bytes32"",""name"":""_voter_id"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_key"",""type"":""bytes32""}],""name"":""Vote_Details"",""outputs"":[{""internalType"":""bytes32"",""name"":"""",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":"""",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":"""",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":"""",""type"":""bytes32""}],""stateMutability"":""view"",""type"":""function""},{""inputs"":[{""internalType"":""bytes32"",""name"":""_voter_id"",""type"":""bytes32""}],""name"":""checkVoterExists"",""outputs"":[{""internalType"":""uint8"",""name"":"""",""type"":""uint8""},{""internalType"":""bytes32"",""name"":"""",""type"":""bytes32""}],""stateMutability"":""view"",""type"":""function""},{""inputs"":[{""internalType"":""bytes32"",""name"":""_voter_id"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_precint_no"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_key"",""type"":""bytes32""}],""name"":""registerVoter"",""outputs"":[],""stateMutability"":""nonpayable"",""type"":""function""},{""inputs"":[{""internalType"":""bytes32"",""name"":""_voteStringA"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_voteStringB"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_voter_id"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_vote_date"",""type"":""bytes32""},{""internalType"":""bytes32"",""name"":""_key"",""type"":""bytes32""}],""name"":""vote"",""outputs"":[],""stateMutability"":""nonpayable"",""type"":""function""}]"; var theContract = web3.Eth.GetContract(ballotABI, getContractAddress(districts.Rows[district][0].ToString())); var addVoterFunction = theContract.GetFunction("registerVoter"); string keyString = privateKey.Substring(2, 4) + privateKey.Substring(privateKey.Length - 4, 4); Bytes32TypeEncoder encoder = new Bytes32TypeEncoder(); byte[] voterID = encoder.Encode(voterTable.Rows[0][0].ToString()); byte[] precinctID = encoder.Encode(voterTable.Rows[0][3].ToString()); byte[] byte_keyString = encoder.Encode(keyString); var addVoter = await addVoterFunction.SendTransactionAndWaitForReceiptAsync(account.Address, gasLimit, gasPrice, null, null, voterID, precinctID, byte_keyString); success = true; MessageBox.Show("Voter successfully registered!"); byte[] ba = Encoding.Default.GetBytes(voterTable.Rows[0][0].ToString() + txt_middlename.Text.Substring(0, 1)); string hexString = BitConverter.ToString(ba); voterCode = hexString.Replace("-", ""); } catch (Exception ex) { MessageBox.Show("The following error has occured: " + ex.Message, "An error has occured...", MessageBoxButtons.OK , MessageBoxIcon.Error); string deleteCandidate = "Delete from `electiondb`.`voter` where voterID = " + voterTable.Rows[0][0].ToString() + ";"; conn.Open(); comm = new MySqlCommand(deleteCandidate, conn); comm.ExecuteNonQuery(); conn.Close(); success = false; } return(success); }