arnaucube
arnaucube/miksi-corePublic Self-hosted repository
miksi-core/dist/miksi.js
dist/miksi.jsRaw
 1var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
 2    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
 3    return new (P || (P = Promise))(function (resolve, reject) {
 4        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
 5        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
 6        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
 7        step((generator = generator.apply(thisArg, _arguments || [])).next());
 8    });
 9};
10const fs = require("fs");
11const { groth } = require('snarkjs');
12const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
13const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
14const circomlib = require("circomlib");
15const smt = require("circomlib").smt;
16const Web3 = require("web3");
17const nLevels = 5;
18const coinCode = "0"; // refearing to ETH
19const ethAmount = '1';
20const amount = Web3.utils.toWei(ethAmount, 'ether');
21exports.calcWitness = (wasm, secret, nullifier, commitments) => __awaiter(this, void 0, void 0, function* () {
22    const poseidon = circomlib.poseidon.createHash(6, 8, 57);
23    const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
24    let tree = yield smt.newMemEmptyTrie();
25    yield tree.insert(1, 0);
26    // old root
27    const rootOld = tree.root;
28    const resOld = yield tree.find(commitment);
29    if (resOld.found) {
30        console.error("leaf expect to not exist but exists");
31    }
32    let siblingsOld = resOld.siblings;
33    while (siblingsOld.length < nLevels) {
34        siblingsOld.push("0");
35    }
36    ;
37    yield tree.insert(commitment, 0);
38    // new root
39    const rootNew = tree.root;
40    const resNew = yield tree.find(commitment);
41    if (!resNew.found) {
42        console.error("leaf expect to exist but not exists");
43    }
44    let siblingsNew = resNew.siblings;
45    while (siblingsNew.length < nLevels) {
46        siblingsNew.push("0");
47    }
48    ;
49    // calculate witness
50    const input = unstringifyBigInts({
51        "coinCode": coinCode,
52        "amount": amount,
53        "secret": secret,
54        "nullifier": nullifier,
55        "siblingsOld": siblingsOld,
56        "siblingsNew": siblingsNew,
57        "rootOld": rootOld,
58        "rootNew": rootNew,
59        "commitment": commitment
60    });
61    const options = {};
62    const wc = yield WitnessCalculatorBuilder(wasm, options);
63    const w = yield wc.calculateWitness(input);
64    const witness = unstringifyBigInts(stringifyBigInts(w));
65    return witness;
66});
67//# sourceMappingURL=miksi.js.map