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 // 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, 0);
35
36 let rootOld = tree.root;
37 let res = await tree.find(2);
38 // console.log(res);
39 assert(!res.found);
40 let siblingsOld = res.siblings;
41 while (siblingsOld.length < nLevels) {
42 siblingsOld.push("0");
43 };
44 console.log("siblingsOld", siblingsOld);
45
46 await tree.insert(2, commitment);
47 let rootNew = tree.root;
48
49 res = await tree.find(2);
50 // console.log(res);
51 assert(res.found);
52 let siblingsNew = res.siblings;
53 while (siblingsNew.length < nLevels) {
54 siblingsNew.push("0");
55 };
56 console.log("siblingsNew", siblingsNew);
57 console.log("rootOld", rootOld);
58 console.log("rootNew", rootNew);
59
60 const witness = await circuit.calculateWitness({
61 "coinCode": coinCode,
62 "amount": amount,
63 "secret": secret,
64 "oldKey": "1",
65 "oldValue": "0",
66 "siblingsOld": siblingsOld,
67 "siblingsNew": siblingsNew,
68 "rootOld": rootOld,
69 "rootNew": rootNew,
70 "commitment": commitment,
71 "key": 2
72 });
73 await circuit.checkConstraints(witness);
74 });
75});