circomlib/src/smt_memdb.js

51 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-12-11 19:25:21 +03:00
const bigInt = require("snarkjs").bigInt;
class SMTMemDb {
constructor() {
this.nodes = {};
this.root = bigInt(0);
}
async getRoot() {
return this.root;
}
2018-12-13 21:53:32 +03:00
_key2str(k) {
// const keyS = bigInt(key).leInt2Buff(32).toString("hex");
const keyS = bigInt(k).toString();
return keyS;
}
_normalize(n) {
for (let i=0; i<n.length; i++) {
n[i] = bigInt(n[i]);
2018-12-11 19:25:21 +03:00
}
}
2018-12-13 21:53:32 +03:00
async get(key) {
const keyS = this._key2str(key);
return this.nodes[keyS];
}
async setRoot(rt) {
this.root = rt;
}
async multiIns(inserts) {
2018-12-11 19:25:21 +03:00
for (let i=0; i<inserts.length; i++) {
2018-12-13 21:53:32 +03:00
const keyS = this._key2str(inserts[i][0]);
this._normalize(inserts[i][1]);
2018-12-11 19:25:21 +03:00
this.nodes[keyS] = inserts[i][1];
}
2018-12-13 21:53:32 +03:00
}
async multiDel(dels) {
for (let i=0; i<dels.length; i++) {
const keyS = this._key2str(dels[i]);
delete this.nodes[keyS];
}
2018-12-11 19:25:21 +03:00
}
}
module.exports = SMTMemDb;