arnaucube
arnaucube/miksi-corePublic Self-hosted repository
miksi-core/test/circuits/withdraw.test.ts
test/circuits/withdraw.test.tsRaw
 1const path = require("path");
 2const tester = require("circom").tester;
 3const chai = require("chai");
 4const assert = chai.assert;
 5const circomlib = require("circomlib");
 6const smt = require("circomlib").smt;
 7
 8export {};
 9
10describe("withdraw test", function () {
11    this.timeout(200000);
12
13
14    it("Test Withdraw", async () => {
15        const circuit = await tester(
16            path.join(__dirname, "main", "withdraw.circom"),
17            {reduceConstraints: false}
18        );
19
20        const nLevels = 4;
21        const secret = "1234567890";
22
23        const coinCode = "0";
24        const amount = '1000000000000000000';
25        // const nullifier = "567891234";
26
27        const poseidon = circomlib.poseidon.createHash(6, 8, 57);
28        const nullifier = poseidon([2, secret]);
29        const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
30
31        // add commitment into SMT
32        let tree = await smt.newMemEmptyTrie();
33        await tree.insert(1, 0);
34        await tree.insert(2, commitment);
35        await tree.insert(3, 0);
36        console.log("root", tree.root);
37        const res = await tree.find(2);
38        assert(res.found);
39        let siblings = res.siblings;
40        while (siblings.length < nLevels) {
41            siblings.push("0");
42        };
43        console.log("siblings", siblings);
44
45        let root = tree.root;
46        
47        const witness = await circuit.calculateWitness({
48            "coinCode": coinCode,
49            "amount": amount,
50            "secret": secret,
51            "nullifier": nullifier,
52            "siblings": siblings,
53            "root": root,
54            "address": "987654321",
55            "key": 2
56        });
57        await circuit.checkConstraints(witness);
58    });
59});