You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

  1. const bigInt = require("snarkjs").bigInt;
  2. class SMTMemDb {
  3. constructor() {
  4. this.nodes = {};
  5. this.root = bigInt(0);
  6. }
  7. async getRoot() {
  8. return this.root;
  9. }
  10. _key2str(k) {
  11. // const keyS = bigInt(key).leInt2Buff(32).toString("hex");
  12. const keyS = bigInt(k).toString();
  13. return keyS;
  14. }
  15. _normalize(n) {
  16. for (let i=0; i<n.length; i++) {
  17. n[i] = bigInt(n[i]);
  18. }
  19. }
  20. async get(key) {
  21. const keyS = this._key2str(key);
  22. return this.nodes[keyS];
  23. }
  24. async setRoot(rt) {
  25. this.root = rt;
  26. }
  27. async multiIns(inserts) {
  28. for (let i=0; i<inserts.length; i++) {
  29. const keyS = this._key2str(inserts[i][0]);
  30. this._normalize(inserts[i][1]);
  31. this.nodes[keyS] = inserts[i][1];
  32. }
  33. }
  34. async multiDel(dels) {
  35. for (let i=0; i<dels.length; i++) {
  36. const keyS = this._key2str(dels[i]);
  37. delete this.nodes[keyS];
  38. }
  39. }
  40. }
  41. module.exports = SMTMemDb;