mirror of
https://github.com/arnaucube/miksi-app.git
synced 2026-02-07 11:46:54 +01:00
Contract Withdraw call successfully called with zkproof
This commit is contained in:
@@ -76276,12 +76276,12 @@ function extend() {
|
||||
const fs = require("fs");
|
||||
const bigInt = require("big-integer");
|
||||
const { groth } = require('snarkjs');
|
||||
const { Fr } = require('ffjavascript').bn128;
|
||||
const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
|
||||
const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
|
||||
const circomlib = require("circomlib");
|
||||
const smt = require("circomlib").smt;
|
||||
const Web3 = require("web3");
|
||||
// const buildBn128 = require("wasmsnark").buildBn128;
|
||||
|
||||
|
||||
const nLevels = 5;
|
||||
@@ -76289,18 +76289,26 @@ const coinCode = "0"; // refearing to ETH
|
||||
const ethAmount = '1';
|
||||
const amount = Web3.utils.toWei(ethAmount, 'ether');
|
||||
|
||||
// let bn128;
|
||||
//
|
||||
// exports.init = async () => {
|
||||
// bn128 = await buildBn128();
|
||||
// }
|
||||
exports.randBigInt = () => {
|
||||
return Fr.random();
|
||||
};
|
||||
|
||||
exports.calcWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
exports.calcCommitment = (secret, nullifier) => {
|
||||
const poseidon = circomlib.poseidon.createHash(6, 8, 57);
|
||||
const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
|
||||
return commitment;
|
||||
};
|
||||
|
||||
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
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);
|
||||
for (let i=0; i<commitments.length; i++) {
|
||||
await tree.insert(commitments[i], 0);
|
||||
}
|
||||
|
||||
// old root
|
||||
const rootOld = tree.root;
|
||||
@@ -76367,6 +76375,67 @@ exports.calcWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
};
|
||||
}
|
||||
|
||||
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr) => {
|
||||
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);
|
||||
for (let i=0; i<commitments.length; i++) {
|
||||
await tree.insert(commitments[i], 0);
|
||||
}
|
||||
// await tree.insert(commitment, 0);
|
||||
|
||||
// root
|
||||
const root = tree.root;
|
||||
const res = await tree.find(commitment);
|
||||
if (!res.found) {
|
||||
console.error("leaf expect to exist but not exists");
|
||||
}
|
||||
let siblings = res.siblings;
|
||||
while (siblings.length < nLevels) {
|
||||
siblings.push("0");
|
||||
};
|
||||
|
||||
// calculate witness
|
||||
const input = unstringifyBigInts({
|
||||
"coinCode": coinCode,
|
||||
"amount": amount,
|
||||
"secret": secret,
|
||||
"nullifier": nullifier,
|
||||
"siblings": siblings,
|
||||
"root": root,
|
||||
"address": addr
|
||||
});
|
||||
console.log("input", input);
|
||||
// const options = {};
|
||||
// const wc = await WitnessCalculatorBuilder(wasm, options);
|
||||
|
||||
const wc = await WitnessCalculatorBuilder(wasm);
|
||||
|
||||
const witness = await wc.calculateWitness(input, {sanityCheck: true});
|
||||
|
||||
const wBuff = Buffer.allocUnsafe(witness.length*32);
|
||||
|
||||
for (let i=0; i<witness.length; i++) {
|
||||
for (let j=0; j<8; j++) {
|
||||
const bi = witness[i];
|
||||
const v = bigInt(bi).shiftRight(j*32).and(0xFFFFFFFF).toJSNumber();
|
||||
wBuff.writeUInt32LE(v, i*32 + j*4, 4)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// const witness = unstringifyBigInts(stringifyBigInts(w));
|
||||
return {
|
||||
witness: wBuff,
|
||||
publicInputs: {
|
||||
address:addr,
|
||||
nullifier:nullifier
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}).call(this,require("buffer").Buffer)
|
||||
|
||||
Reference in New Issue
Block a user