circomlib/src/smt_memdb.js
src/smt_memdb.jsRaw
1const bigInt = require("big-integer");
2
3class SMTMemDb {
4 constructor() {
5 this.nodes = {};
6 this.root = bigInt(0);
7 }
8
9 async getRoot() {
10 return this.root;
11 }
12
13 _key2str(k) {
14 // const keyS = bigInt(key).leInt2Buff(32).toString("hex");
15 const keyS = bigInt(k).toString();
16 return keyS;
17 }
18
19 _normalize(n) {
20 for (let i=0; i<n.length; i++) {
21 n[i] = bigInt(n[i]);
22 }
23 }
24
25 async get(key) {
26 const keyS = this._key2str(key);
27 return this.nodes[keyS];
28 }
29
30 async multiGet(keys) {
31 const promises = [];
32 for (let i=0; i<keys.length; i++) {
33 promises.push(this.get(keys[i]));
34 }
35 return await Promise.all(promises);
36 }
37
38 async setRoot(rt) {
39 this.root = rt;
40 }
41
42 async multiIns(inserts) {
43 for (let i=0; i<inserts.length; i++) {
44 const keyS = this._key2str(inserts[i][0]);
45 this._normalize(inserts[i][1]);
46 this.nodes[keyS] = inserts[i][1];
47 }
48 }
49
50 async multiDel(dels) {
51 for (let i=0; i<dels.length; i++) {
52 const keyS = this._key2str(dels[i]);
53 delete this.nodes[keyS];
54 }
55 }
56}
57
58module.exports = SMTMemDb;