Browse Source

Add logs box, fix UX flows & error management

pull/1/head
arnaucube 4 years ago
parent
commit
57791d7458
4 changed files with 92 additions and 25 deletions
  1. +9
    -0
      css/style.css
  2. +8
    -5
      index.html
  3. +74
    -19
      index.js
  4. +1
    -1
      libs/miksi-browser.js

+ 9
- 0
css/style.css

@ -99,3 +99,12 @@ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#cc66ff', end
padding: 20px;
color: #FF00B9;
}
.logs {
background:#000000;
padding:10px;
margin-top:20px;
font-family: "Courier";
max-height: 300px;
overflow-y: scroll;
}

+ 8
- 5
index.html

@ -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 @@
</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="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>
@ -92,6 +92,9 @@
<div id="withdrawRes"></div>
</div>
</div>
<div id="logs" class="logs">
<b>> Logs</b><br>
</div>
<br><br>
<h5 class="card-title">Stats</h5>
<hr>

+ 74
- 19
index.js

@ -5,14 +5,34 @@ const abi = JSON.parse(`[{"inputs":[{"internalType":"address","name":"_depositVe
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 @@ function loadCircuit(circuitname) {
}).then( (b) => {
witnessCalc[circuitname] = b;
console.log("w", b);
console.log("witnessCalc loaded for", circuitname);
println("witnessCalc loaded for", circuitname);
});
}
@ -35,9 +55,12 @@ async function deposit(circuitname) {
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 @@ async function deposit(circuitname) {
// 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
const start = new Date().getTime();
console.log(provingKey[circuitname]);
println("generate zkSNARK Groth16 proof for deposit");
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);
@ -79,6 +108,7 @@ async function deposit(circuitname) {
],
[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 @@ async function deposit(circuitname) {
).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 @@ async function withdraw(circuitname) {
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 @@ async function withdraw(circuitname) {
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 @@ async function withdraw(circuitname) {
],
[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 @@ async function withdraw(circuitname) {
).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 @@ async function connectMetamask() {
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];

+ 1
- 1
libs/miksi-browser.js

@ -76325,7 +76325,7 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments, key) =
// 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";

Loading…
Cancel
Save