arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/src/smt_memdb.js
src/smt_memdb.jsRaw
 1const bigInt = require("snarkjs").bigInt;
 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 setRoot(rt) {
31        this.root = rt;
32    }
33
34    async multiIns(inserts) {
35        for (let i=0; i<inserts.length; i++) {
36            const keyS = this._key2str(inserts[i][0]);
37            this._normalize(inserts[i][1]);
38            this.nodes[keyS] = inserts[i][1];
39        }
40    }
41
42    async multiDel(dels) {
43        for (let i=0; i<dels.length; i++) {
44            const keyS = this._key2str(dels[i]);
45            delete this.nodes[keyS];
46        }
47    }
48}
49
50module.exports = SMTMemDb;