Add logs box, fix UX flows & error management
@@ -99,3 +99,12 @@ border-radius: 5px;
padding: 20px; color: #FF00B9; } + +.logs { + background:#000000; + padding:10px; + margin-top:20px; + font-family: "Courier"; + max-height: 300px; + overflow-y: scroll; +}
@@ -4,11 +4,11 @@
<head> <meta charset="utf-8"> <title>miksi-app</title> - <link rel="icon" type="image/png" href="img/web-wallet-icon.png"> + <link rel="icon" type="image/png" href="miksi-logo00-square-pink.png"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, user-scalable=no" /> - <link rel="manifest" href="manifest.json" /> + <!-- <link rel="manifest" href="manifest.json" /> --> <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/utils.css">@@ -26,8 +26,7 @@ <!-- to delete and use another in the future -->
</head> <body class="dark-theme"> - <br> - <a target="_blank" href="https://github.com/arnaucube/miksi-app" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#fff; color:#151513; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style> + <a target="_blank" href="https://github.com/arnaucube/miksi-core" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#fff; color:#151513; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style> <div id="wallet-maincard" class="card mainBox"> <!-- menu -->@@ -51,7 +50,8 @@ <div class="tab-content" id="myTabContent">
<div class="float-right" style="text-align:right;"> <button onclick="connectMetamask()" class="btn color_primary-o">Connect Metamask</button> <br><br> - Current address: <div id="contractAddr"></div> + Current miksi address: <div id="contractAddr"></div> + Version: v0.0.1 </div> <h2><i>miksi</i></h2> <i>From Esperanto, <b>miksi</b> (miks·i): to mingle, to blend, to mix, to shuffle</i>@@ -91,6 +91,9 @@ <button onclick="withdraw('withdraw')" class="btn color_primary float-right">Withdraw 1 ETH</button>
<br> <div id="withdrawRes"></div> </div> + </div> + <div id="logs" class="logs"> + <b>> Logs</b><br> </div> <br><br> <h5 class="card-title">Stats</h5>
@@ -5,14 +5,34 @@ const abi = JSON.parse(`[{"inputs":[{"internalType":"address","name":"_depositVerifierContractAddr","type":"address"},{"internalType":"address","name":"_withdrawVerifierContractAddr","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"_commitment","type":"uint256"},{"internalType":"uint256","name":"_root","type":"uint256"},{"internalType":"uint256[2]","name":"a","type":"uint256[2]"},{"internalType":"uint256[2][2]","name":"b","type":"uint256[2][2]"},{"internalType":"uint256[2]","name":"c","type":"uint256[2]"}],"name":"deposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"getCommitments","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address payable","name":"_address","type":"address"},{"internalType":"uint256","name":"nullifier","type":"uint256"},{"internalType":"uint256[2]","name":"a","type":"uint256[2]"},{"internalType":"uint256[2][2]","name":"b","type":"uint256[2][2]"},{"internalType":"uint256[2]","name":"c","type":"uint256[2]"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]`);
const miksiAddress = "0x4cc45573481A2977fcC0b9DD9f8c710201B5a5cd"; let metamask = false; -document.getElementById("contractAddr").innerHTML=`<a href="https://goerli.etherscan.io/address/`+miksiAddress+`" target="_blank">`+miksiAddress+`</a>`; +document.getElementById("contractAddr").innerHTML=`<a href="https://goerli.etherscan.io/address/`+miksiAddress+`" target="_blank" title="Miksi Smart Contract Address">`+miksiAddress+`</a>`; + +function println(...s) { + let r = ""; + for (let i=0; i<s.length; i++) { + r = r + " " + s[i]; + } + console.log(r); + document.getElementById("logs").innerHTML += r + "<br>"; + document.getElementById("logs").scrollTop = document.getElementById("logs").scrollHeight; +} +function printerr(...s) { + // println(s); + let r = ""; + for (let i=0; i<s.length; i++) { + r = r + " " + s[i]; + } + console.log(r); + document.getElementById("logs").innerHTML += `<span style="color:red;">Error: ` + r + `</span><br>`; + document.getElementById("logs").scrollTop = document.getElementById("logs").scrollHeight; +} function loadCircuit(circuitname) { fetch("circuits-files/"+circuitname+"-proving_key.bin").then( (response) => { return response.arrayBuffer(); }).then( (b) => { provingKey[circuitname] = b; - console.log("proving_key loaded for", circuitname); + println("proving_key loaded for", circuitname); }); fetch("circuits-files/"+circuitname+".wasm").then( (response) => {@@ -20,7 +40,7 @@ return response.arrayBuffer();
}).then( (b) => { witnessCalc[circuitname] = b; console.log("w", b); - console.log("witnessCalc loaded for", circuitname); + println("witnessCalc loaded for", circuitname); }); }@@ -35,9 +55,12 @@ `;
console.log("circuit:", circuitname); // TODO + println("generate random secret & nullifier"); const secret = miksi.randBigInt().toString(); const nullifier = miksi.randBigInt().toString(); + console.log("S N", secret, nullifier); + println("get commitments from the miksi Smart Contract"); let res = await miksiContract.methods.getCommitments().call(); console.log("res", res); const commitments = res[0];@@ -47,20 +70,26 @@ console.log("key", key);
// getCommitments from the tree // calculate witness + println("rebuild the Merkle Tree & calculate witness for deposit"); console.log(witnessCalc[circuitname]); - const cw = await miksi.calcDepositWitness(witnessCalc[circuitname], secret, nullifier, commitments, key); + const cw = await miksi.calcDepositWitness(witnessCalc[circuitname], secret, nullifier, commitments, key).catch((e) => { + toastr.error(e); + printerr(e); + }); + const witness = cw.witness; const publicInputs = cw.publicInputs; console.log("w", witness); console.log("publicInputs", publicInputs); // generate proof + console.log(provingKey[circuitname]); + println("generate zkSNARK Groth16 proof for deposit"); const start = new Date().getTime(); - console.log(provingKey[circuitname]); const proof = await window.groth16GenProof(witness.buffer, provingKey[circuitname]); const end = new Date().getTime(); const time = end - start; - console.log("circuit " + circuitname + " took " + time + "ms to compute"); + println("circuit " + circuitname + " took " + time + "ms to compute"); console.log(proof);@@ -79,6 +108,7 @@ [proof.pi_b[1][1], proof.pi_b[1][0]]
], [proof.pi_c[0], proof.pi_c[1]], ); + println("send publicInputs & zkProof to the miksi Smart Contract for the deposit"); miksiContract.methods.deposit( publicInputs.commitment, publicInputs.root.toString(),@@ -91,8 +121,15 @@ [proof.pi_c[0], proof.pi_c[1]],
).send( {from: sender, value: 1000000000000000000}, function(error, transactionHash){ - console.log("https://goerli.etherscan.io/tx/"+transactionHash); - console.log(error); + if (error!=undefined) { + console.log(error); + toastr.error(error); + printerr(JSON.stringify(error)); + } else { + let link = `<a href="https://goerli.etherscan.io/tx/`+transactionHash+`" target="_blank"> + https://goerli.etherscan.io/tx/`+transactionHash+`</a>`; + println(link); + } }); // print secret & nullifier@@ -118,14 +155,21 @@ document.getElementById("withdrawRes").innerHTML = `
Generating zkProof & making the withdraw `; console.log("circuit:", circuitname); - const jw = JSON.parse(document.getElementById("jsonWithdraw").value); + let jw; + try { + jw = JSON.parse(document.getElementById("jsonWithdraw").value); + } catch(e) { + toastr.error("Error reading secret & nullifier: " + e); + } const secret = jw.secret; const nullifier = jw.nullifier; const key = jw.key; console.log(secret, nullifier); + println("calculate commitment for the secret & nullifier"); const commitment = miksi.calcCommitment(secret, nullifier); // getCommitments from the tree + println("get commitments from the miksi Smart Contract"); let res = await miksiContract.methods.getCommitments().call(); console.log("res", res); const commitments = res[0];@@ -142,19 +186,25 @@ if (!web3.utils.isAddress(addr)) {
toastr.error("Error with withdraw address"); return; } - const cw = await miksi.calcWithdrawWitness(witnessCalc[circuitname], secret, nullifier, commitments, addr, key); + println("rebuild the Merkle Tree & calculate witness for withdraw"); + const cw = await miksi.calcWithdrawWitness(witnessCalc[circuitname], secret, nullifier, commitments, addr, key).catch((e) => { + toastr.error(e); + printerr(e); + }); + const witness = cw.witness; const publicInputs = cw.publicInputs; console.log("w", witness); console.log("publicInputs", publicInputs); // generate proof - const start = new Date().getTime(); console.log(provingKey[circuitname]); + println("generate zkSNARK Groth16 proof for withdraw"); + const start = new Date().getTime(); const proof = await window.groth16GenProof(witness.buffer, provingKey[circuitname]); const end = new Date().getTime(); const time = end - start; - console.log("circuit " + circuitname + " took " + time + "ms to compute"); + println("circuit " + circuitname + " took " + time + "ms to compute"); console.log(proof);@@ -173,6 +223,7 @@ [proof.pi_b[1][1], proof.pi_b[1][0]]
], [proof.pi_c[0], proof.pi_c[1]], ); + println("send publicInputs & zkProof to the miksi Smart Contract for the withdraw"); miksiContract.methods.withdraw( publicInputs.address, publicInputs.nullifier,@@ -185,13 +236,16 @@ [proof.pi_c[0], proof.pi_c[1]],
).send( {from: sender}, function(error, transactionHash){ - console.log("https://goerli.etherscan.io/tx/"+transactionHash); - console.log(error); + if (error!=undefined) { + console.log(error); + toastr.error(error); + printerr(JSON.stringify(error)); + } else { + let link = `<a href="https://goerli.etherscan.io/tx/`+transactionHash+`" target="_blank"> + https://goerli.etherscan.io/tx/`+transactionHash+`</a>`; + println(link); + } }); - - // print secret & nullifier - document.getElementById("depositRes").innerHTML = ` - `; } loadCircuit("deposit");@@ -222,7 +276,8 @@
console.log("abi", abi); miksiContract = new web3.eth.Contract(abi, miksiAddress); console.log("miksiContract", miksiContract); - toastr.info("Metamask connected. Miksi contract: ", miksiAddress); + toastr.info("Metamask connected. Miksi contract: " + miksiAddress); + println("Metamask connected. Miksi contract: ", miksiAddress); const acc = await web3.eth.getAccounts(); const addr = acc[0];
@@ -76325,7 +76325,7 @@ }
// old root const rootOld = tree.root; - const resOld = await tree.find(commitment); + const resOld = await tree.find(key); console.log("FIND old", resOld); let oldKey = "0"; let oldValue = "0";