arnaucube
arnaucube/miksi-corePublic Self-hosted repository
miksi-core/test/circuits/deposit.test.ts
test/circuits/deposit.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("deposit test", function () {
11    this.timeout(200000);
12
13
14    it("Test Deposit", async () => {
15        const circuit = await tester(
16            path.join(__dirname, "main", "deposit.circom"),
17            {reduceConstraints: false}
18        );
19
20        const nLevels = 4;
21        const secret = "1234567890";
22
23        const coinCode = "0";
24        const amount = '1000000000000000000';
25
26        const poseidon = circomlib.poseidon.createHash(6, 8, 57);
27        const nullifier = poseidon([2, secret]);
28        const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
29
30        // add commitment into SMT
31        let tree = await smt.newMemEmptyTrie();
32        await tree.insert(1, 0);
33
34        let rootOld = tree.root;
35
36        let res = await tree.insert(2, commitment);
37        console.log("INSERT", res);
38        let rootNew = tree.root;
39
40        let siblings = res.siblings;
41        while (siblings.length < nLevels) {
42            siblings.push("0");
43        };
44        console.log("siblings", siblings);
45
46        console.log(res);
47        
48        const witness = await circuit.calculateWitness({
49            "coinCode": coinCode,
50            "amount": amount,
51            "secret": secret,
52            "oldKey": res.isOld0 ? 0 : res.oldKey,
53            "oldValue": res.isOld0 ? 0 : res.oldValue,
54            "isOld0": res.isOld0 ? 1 : 0,
55            "siblings": siblings,
56            "rootOld": res.oldRoot,
57            "rootNew": res.newRoot,
58            "commitment": commitment,
59            "key": 2
60        });
61        await circuit.checkConstraints(witness);
62    });
63});