Browse Source

Update to use key from contract

pull/1/head
arnaucube 3 years ago
parent
commit
c673a1e3e2
8 changed files with 56 additions and 30 deletions
  1. +3
    -0
      README.md
  2. BIN
      circuits-files/deposit-proving_key.bin
  3. BIN
      circuits-files/deposit.wasm
  4. BIN
      circuits-files/withdraw-proving_key.bin
  5. BIN
      circuits-files/withdraw.wasm
  6. +5
    -4
      index.html
  7. +23
    -7
      index.js
  8. +25
    -19
      libs/miksi-browser.js

+ 3
- 0
README.md

@ -0,0 +1,3 @@
# miksi-app
miksi-core can be found at https://github.com/arnaucube/miksi-core

BIN
circuits-files/deposit-proving_key.bin


BIN
circuits-files/deposit.wasm


BIN
circuits-files/withdraw-proving_key.bin


BIN
circuits-files/withdraw.wasm


+ 5
- 4
index.html

@ -27,7 +27,7 @@
<body class="dark-theme">
<br>
<a target="_blank" href="https://github.com/miksi-labs/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-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>
<div id="wallet-maincard" class="card mainBox">
<!-- menu -->
@ -98,7 +98,7 @@
<h5><i class="fa fa-info" aria-hidden="true"></i> Info</h5>
<hr>
Spec & core code: <a href="https://github.com/miksi-labs/miksi-core" target="_blank">https://github.com/miksi-labs/miksi-core</a>
Spec & core code: <a href="https://github.com/arnaucube/miksi-core" target="_blank">https://github.com/arnaucube/miksi-core</a>
<br>
What is a <a href="https://en.wikipedia.org/wiki/Cryptocurrency_tumbler" target="_blank">mixer</a>?
<br><br><br>
@ -121,8 +121,9 @@
<!-- <script src="node_modules/jquery/dist/jquery.min.js"></script> -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<!-- <script src="node_modules/toastr/build/toastr.min.js"></script> -->
<!-- <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> -->
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<!-- [> <script src="node_modules/toastr/build/toastr.min.js"></script> <] -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"

+ 23
- 7
index.js

@ -1,8 +1,9 @@
var circuit = {};
var provingKey = {};
var witnessCalc = {};
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"}],"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 = "0x29DB549Ea98d41e0F28A5aA50ED1D37d62BA8C4B";
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 = "0x3a88725bf9ABc85Dca64A4e6bc629D448032FA0F";
let metamask = false;
function loadCircuit(circuitname) {
fetch("circuits-files/"+circuitname+"-proving_key.bin").then( (response) => {
@ -22,6 +23,10 @@ function loadCircuit(circuitname) {
}
async function deposit(circuitname) {
if (!metamask) {
toastr.error("Please install/connect Metamask");
return;
}
document.getElementById("depositRes").innerHTML = `
Generating zkProof & making the deposit
`;
@ -30,17 +35,18 @@ async function deposit(circuitname) {
// TODO
const secret = miksi.randBigInt().toString();
const nullifier = miksi.randBigInt().toString();
// const secret = "1234567890";
// const nullifier = "5678901234";
let res = await miksiContract.methods.getCommitments().call();
console.log("res", res);
const commitments = res[0];
const key = res[2];
console.log("commitments", commitments);
console.log("key", key);
// getCommitments from the tree
// calculate witness
console.log(witnessCalc[circuitname]);
const cw = await miksi.calcDepositWitness(witnessCalc[circuitname], secret, nullifier, commitments);
const cw = await miksi.calcDepositWitness(witnessCalc[circuitname], secret, nullifier, commitments, key);
const witness = cw.witness;
const publicInputs = cw.publicInputs;
console.log("w", witness);
@ -90,7 +96,8 @@ async function deposit(circuitname) {
// print secret & nullifier
let jw = {
secret: secret,
nullifier: nullifier
nullifier: nullifier,
key: key
};
console.log("jw", JSON.stringify(jw));
document.getElementById("depositRes").innerHTML = `
@ -101,6 +108,10 @@ async function deposit(circuitname) {
}
async function withdraw(circuitname) {
if (!metamask) {
toastr.error("Please install/connect Metamask");
return;
}
document.getElementById("withdrawRes").innerHTML = `
Generating zkProof & making the withdraw
`;
@ -108,6 +119,7 @@ async function withdraw(circuitname) {
const jw = JSON.parse(document.getElementById("jsonWithdraw").value);
const secret = jw.secret;
const nullifier = jw.nullifier;
const key = jw.key;
console.log(secret, nullifier);
const commitment = miksi.calcCommitment(secret, nullifier);
@ -121,7 +133,7 @@ async function withdraw(circuitname) {
console.log(witnessCalc[circuitname]);
const proverAccounts = await web3.eth.getAccounts();
const addr = proverAccounts[0];
const cw = await miksi.calcWithdrawWitness(witnessCalc[circuitname], secret, nullifier, commitments, addr);
const cw = await miksi.calcWithdrawWitness(witnessCalc[circuitname], secret, nullifier, commitments, addr, key);
const witness = cw.witness;
const publicInputs = cw.publicInputs;
console.log("w", witness);
@ -192,12 +204,16 @@ async function connectMetamask() {
}
if (!ethEnabled()) {
toastr.warning("Please install Metamask to use miksi");
alert("Please install MetaMask to use miksi");
} else {
metamask = true;
}
console.log("abi", abi);
miksiContract = new web3.eth.Contract(abi, miksiAddress);
console.log("miksiContract", miksiContract);
toastr.info("Metamask connected. Miksi contract: ", miksiAddress);
const acc = await web3.eth.getAccounts();
const addr = acc[0];

+ 25
- 19
libs/miksi-browser.js

@ -61,7 +61,7 @@ module.exports={
"_args": [
[
"@web3-js/websocket@1.0.30",
"/home/user/git/miksi/miksi-core"
"/home/nicebox/miksi/miksi-core"
]
],
"_from": "@web3-js/websocket@1.0.30",
@ -86,7 +86,7 @@ module.exports={
],
"_resolved": "https://registry.npmjs.org/@web3-js/websocket/-/websocket-1.0.30.tgz",
"_spec": "1.0.30",
"_where": "/home/user/git/miksi/miksi-core",
"_where": "/home/nicebox/miksi/miksi-core",
"author": {
"name": "Brian McKelvey",
"email": "theturtle32@gmail.com",
@ -27405,7 +27405,7 @@ module.exports={
"_args": [
[
"elliptic@6.5.2",
"/home/user/git/miksi/miksi-core"
"/home/nicebox/miksi/miksi-core"
]
],
"_from": "elliptic@6.5.2",
@ -27434,7 +27434,7 @@ module.exports={
],
"_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.2.tgz",
"_spec": "6.5.2",
"_where": "/home/user/git/miksi/miksi-core",
"_where": "/home/nicebox/miksi/miksi-core",
"author": {
"name": "Fedor Indutny",
"email": "fedor@indutny.com"
@ -75059,7 +75059,7 @@ module.exports={
"_args": [
[
"web3@1.2.7",
"/home/user/git/miksi/miksi-core"
"/home/nicebox/miksi/miksi-core"
]
],
"_from": "web3@1.2.7",
@ -75084,7 +75084,7 @@ module.exports={
],
"_resolved": "https://registry.npmjs.org/web3/-/web3-1.2.7.tgz",
"_spec": "1.2.7",
"_where": "/home/user/git/miksi/miksi-core",
"_where": "/home/nicebox/miksi/miksi-core",
"author": {
"name": "ethereum.org"
},
@ -76310,25 +76310,28 @@ exports.calcCommitment = (secret, nullifier) => {
return commitment;
};
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments, key) => {
const poseidon = circomlib.poseidon.createHash(6, 8, 57);
const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
// rebuild the tree
let tree = await smt.newMemEmptyTrie();
await tree.insert(1, 0);
await tree.insert(0, 0);
for (let i=0; i<commitments.length; i++) {
await tree.insert(commitments[i], 0);
await tree.insert(i+1, commitments[i]);
}
// old root
const rootOld = tree.root;
const resOld = await tree.find(commitment);
let oldKey = "0";
let oldValue = "0";
if (!resOld.found) {
oldKey = resOld.notFoundKey.toString();
oldValue = resOld.notFoundValue.toString();
}
console.log("oldKey", oldKey);
console.log("oldValue", oldValue);
// if (resOld.found) {
// console.error("leaf expect to not exist but exists");
// }
@ -76337,13 +76340,13 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
siblingsOld.push("0");
};
await tree.insert(commitment, 0);
await tree.insert(key, commitment);
// new root
const rootNew = tree.root;
const resNew = await tree.find(commitment);
const resNew = await tree.find(key);
if (!resNew.found) {
console.error("leaf expect to exist but not exists");
console.error("leaf with the new commitment expect to exist but not exists");
}
let siblingsNew = resNew.siblings;
while (siblingsNew.length < nLevels) {
@ -76357,11 +76360,13 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
"secret": secret,
"nullifier": nullifier,
"oldKey": oldKey,
"oldValue": oldValue,
"siblingsOld": siblingsOld,
"siblingsNew": siblingsNew,
"rootOld": rootOld,
"rootNew": rootNew,
"commitment": commitment
"commitment": commitment,
"key": key
});
console.log("input", input);
// const options = {};
@ -76394,23 +76399,23 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
};
}
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr) => {
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr, key) => {
const poseidon = circomlib.poseidon.createHash(6, 8, 57);
const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
// rebuild the tree
let tree = await smt.newMemEmptyTrie();
await tree.insert(1, 0);
await tree.insert(0, 0);
for (let i=0; i<commitments.length; i++) {
await tree.insert(commitments[i], 0);
await tree.insert(i+1, commitments[i]);
}
// await tree.insert(commitment, 0);
// root
const root = tree.root;
const res = await tree.find(commitment);
const res = await tree.find(key);
if (!res.found) {
console.error("leaf expect to exist but not exists");
console.error("leaf expect to exist but not exists, key:", key);
}
let siblings = res.siblings;
while (siblings.length < nLevels) {
@ -76425,7 +76430,8 @@ exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr)
"nullifier": nullifier,
"siblings": siblings,
"root": root,
"address": addr
"address": addr,
"key": key
});
console.log("input", input);
// const options = {};

Loading…
Cancel
Save