Created
June 2, 2024 19:34
-
-
Save Turupawn/f8fdc9ed8d780abcc2bb6b64cd2dbe6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NETWORK_ID = 534351 | |
const MY_CONTRACT_ADDRESS = "0xdE19032216f861c6011F6898B14fb49Fd26c45F2" | |
const MY_CONTRACT_ABI_PATH = "./json_abi/MyContract.json" | |
var my_contract | |
var accounts | |
var web3 | |
function metamaskReloadCallback() { | |
window.ethereum.on('accountsChanged', (accounts) => { | |
document.getElementById("web3_message").textContent="Se cambió el account, refrescando..."; | |
window.location.reload() | |
}) | |
window.ethereum.on('networkChanged', (accounts) => { | |
document.getElementById("web3_message").textContent="Se el network, refrescando..."; | |
window.location.reload() | |
}) | |
} | |
const getWeb3 = async () => { | |
return new Promise((resolve, reject) => { | |
if(document.readyState=="complete") | |
{ | |
if (window.ethereum) { | |
const web3 = new Web3(window.ethereum) | |
window.location.reload() | |
resolve(web3) | |
} else { | |
reject("must install MetaMask") | |
document.getElementById("web3_message").textContent="Error: Porfavor conéctate a Metamask"; | |
} | |
}else | |
{ | |
window.addEventListener("load", async () => { | |
if (window.ethereum) { | |
const web3 = new Web3(window.ethereum) | |
resolve(web3) | |
} else { | |
reject("must install MetaMask") | |
document.getElementById("web3_message").textContent="Error: Please install Metamask"; | |
} | |
}); | |
} | |
}); | |
}; | |
const getContract = async (web3, address, abi_path) => { | |
const response = await fetch(abi_path); | |
const data = await response.json(); | |
const netId = await web3.eth.net.getId(); | |
contract = new web3.eth.Contract( | |
data, | |
address | |
); | |
return contract | |
} | |
async function loadDapp() { | |
metamaskReloadCallback() | |
document.getElementById("web3_message").textContent="Please connect to Metamask" | |
var awaitWeb3 = async function () { | |
web3 = await getWeb3() | |
web3.eth.net.getId((err, netId) => { | |
if (netId == NETWORK_ID) { | |
var awaitContract = async function () { | |
my_contract = await getContract(web3, MY_CONTRACT_ADDRESS, MY_CONTRACT_ABI_PATH) | |
document.getElementById("web3_message").textContent="You are connected to Metamask" | |
onContractInitCallback() | |
web3.eth.getAccounts(function(err, _accounts){ | |
accounts = _accounts | |
if (err != null) | |
{ | |
console.error("An error occurred: "+err) | |
} else if (accounts.length > 0) | |
{ | |
onWalletConnectedCallback() | |
document.getElementById("account_address").style.display = "block" | |
} else | |
{ | |
document.getElementById("connect_button").style.display = "block" | |
} | |
}); | |
}; | |
awaitContract(); | |
} else { | |
document.getElementById("web3_message").textContent="Please connect to Goerli"; | |
} | |
}); | |
}; | |
awaitWeb3(); | |
} | |
async function connectWallet() { | |
await window.ethereum.request({ method: "eth_requestAccounts" }) | |
accounts = await web3.eth.getAccounts() | |
onWalletConnectedCallback() | |
} | |
loadDapp() | |
const onContractInitCallback = async () => { | |
/* | |
var hello = await my_contract.methods.hello().call() | |
var count = await my_contract.methods.count().call() | |
var last_writer = await my_contract.methods.count().call() | |
var contract_state = "Hello: " + hello | |
+ ", Count: " + count | |
+ ", Last Writer: " + last_writer | |
document.getElementById("contract_state").textContent = contract_state; | |
*/ | |
} | |
const onWalletConnectedCallback = async () => { | |
} | |
//// Functions //// | |
const setHello = async (proof, publicSignals) => { | |
proof.pi_a.pop() | |
proof.pi_b.pop() | |
proof.pi_c.pop() | |
console.log(proof.pi_a) | |
console.log(proof.pi_b) | |
console.log(proof.pi_c) | |
console.log(publicSignals) | |
const result = await my_contract.methods.castVote( | |
proof.pi_a, | |
proof.pi_b, | |
proof.pi_c, | |
publicSignals) | |
.send({ from: accounts[0], gas: 0, value: 0 }) | |
.on('transactionHash', function(hash){ | |
document.getElementById("web3_message").textContent="Executing..."; | |
}) | |
.on('receipt', function(receipt){ | |
document.getElementById("web3_message").textContent="Success."; }) | |
.catch((revertReason) => { | |
console.log("ERROR! Transaction reverted: " + revertReason.receipt.transactionHash) | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json_abi/MyContract.json | |
blockchain_stuff.js | |
circuit_final.zkey | |
circuit.wasm | |
index.html | |
proveVote_0001.zkey | |
snarkjs.min.js | |
verification_key.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Snarkjs client example</title> | |
</head> | |
<body> | |
<h1>Snarkjs client example</h1> | |
<button id="bGenProof"> Create proof </button> | |
<!-- JS-generated output will be added here. --> | |
<pre class="proof"> Proof: <code id="proof"></code></pre> | |
<pre class="proof"> Result: <code id="result"></code></pre> | |
<input id="connect_button" type="button" value="Connect" onclick="connectWallet()" style="display: none"></input> | |
<p id="account_address" style="display: none"></p> | |
<p id="web3_message"></p> | |
<p id="contract_state"></p> | |
<input type="input" value="" id="_hello"></input> | |
<input type="button" value="Set Hello" onclick="_setHello()"></input> | |
<br> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.3.5/web3.min.js"></script> | |
<script type="text/javascript" src="blockchain_stuff.js"></script> | |
<script src="snarkjs.min.js"> </script> | |
<!-- This is the bundle generated by rollup.js --> | |
<script> | |
const proofComponent = document.getElementById('proof'); | |
const resultComponent = document.getElementById('result'); | |
const bGenProof = document.getElementById("bGenProof"); | |
bGenProof.addEventListener("click", calculateProof); | |
async function calculateProof() { | |
const { proof, publicSignals } = | |
await snarkjs.groth16.fullProve( { | |
"privateKey": "111", | |
"root": "172702405816516791996779728912308790882282610188111072512380034048458433129", | |
"proposalId": "0", | |
"vote": "1", | |
"pathElements": ["3370092681714607727019534888747304108045661953819543369463810453568040251648", "11117482755699627218224304590393929490559713427701237904426421590969988571596"], | |
"pathIndices": ["0","0"] | |
}, "circuit.wasm", "circuit_final.zkey"); | |
proofComponent.innerHTML = JSON.stringify(proof, null, 1); | |
const vkey = await fetch("verification_key.json").then( function(res) { | |
return res.json(); | |
}); | |
const res = await snarkjs.groth16.verify(vkey, publicSignals, proof); | |
resultComponent.innerHTML = res; | |
setHello(proof, publicSignals) | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment