@iden3/js-merkletree
Version:
javascript sparse merkle tree library
82 lines (81 loc) • 4.01 kB
JavaScript
;
/* eslint-disable no-case-declarations */
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _LocalStorageDB_currentRoot;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalStorageDB = void 0;
const hash_1 = require("../hash/hash");
const constants_1 = require("../../constants");
const node_1 = require("../node/node");
const utils_1 = require("../utils");
class LocalStorageDB {
constructor(_prefix) {
this._prefix = _prefix;
_LocalStorageDB_currentRoot.set(this, void 0);
const rootStr = localStorage.getItem((0, utils_1.bytes2Hex)(_prefix));
if (rootStr) {
const bytes = JSON.parse(rootStr);
__classPrivateFieldSet(this, _LocalStorageDB_currentRoot, new hash_1.Hash(Uint8Array.from(bytes)), "f");
}
else {
__classPrivateFieldSet(this, _LocalStorageDB_currentRoot, hash_1.ZERO_HASH, "f");
}
}
async get(k) {
const kBytes = new Uint8Array([...this._prefix, ...k]);
const key = (0, utils_1.bytes2Hex)(kBytes);
const val = localStorage.getItem(key);
if (val === null) {
return undefined;
}
const obj = JSON.parse(val);
switch (obj.type) {
case constants_1.NODE_TYPE_EMPTY:
return new node_1.NodeEmpty();
case constants_1.NODE_TYPE_MIDDLE:
const cL = new hash_1.Hash(Uint8Array.from(obj.childL));
const cR = new hash_1.Hash(Uint8Array.from(obj.childR));
return new node_1.NodeMiddle(cL, cR);
case constants_1.NODE_TYPE_LEAF:
const k = new hash_1.Hash(Uint8Array.from(obj.entry[0]));
const v = new hash_1.Hash(Uint8Array.from(obj.entry[1]));
return new node_1.NodeLeaf(k, v);
}
throw `error: value found for key ${(0, utils_1.bytes2Hex)(kBytes)} is not of type Node`;
}
async put(k, n) {
const kBytes = new Uint8Array([...this._prefix, ...k]);
const key = (0, utils_1.bytes2Hex)(kBytes);
const toSerialize = {
type: n.type
};
if (n instanceof node_1.NodeMiddle) {
toSerialize.childL = Array.from(n.childL.bytes);
toSerialize.childR = Array.from(n.childR.bytes);
}
else if (n instanceof node_1.NodeLeaf) {
toSerialize.entry = [Array.from(n.entry[0].bytes), Array.from(n.entry[1].bytes)];
}
const val = JSON.stringify(toSerialize);
localStorage.setItem(key, val);
}
async getRoot() {
return __classPrivateFieldGet(this, _LocalStorageDB_currentRoot, "f");
}
async setRoot(r) {
__classPrivateFieldSet(this, _LocalStorageDB_currentRoot, r, "f");
localStorage.setItem((0, utils_1.bytes2Hex)(this._prefix), JSON.stringify(Array.from(r.bytes)));
}
}
exports.LocalStorageDB = LocalStorageDB;
_LocalStorageDB_currentRoot = new WeakMap();