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