@iden3/js-merkletree
Version:
javascript sparse merkle tree library
78 lines (77 loc) • 3.76 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;
import { Hash, ZERO_HASH } from '../hash/hash';
import { NODE_TYPE_EMPTY, NODE_TYPE_LEAF, NODE_TYPE_MIDDLE } from '../../constants';
import { NodeEmpty, NodeLeaf, NodeMiddle } from '../node/node';
import { bytes2Hex } from '../utils';
export class LocalStorageDB {
constructor(_prefix) {
this._prefix = _prefix;
_LocalStorageDB_currentRoot.set(this, void 0);
const rootStr = localStorage.getItem(bytes2Hex(_prefix));
if (rootStr) {
const bytes = JSON.parse(rootStr);
__classPrivateFieldSet(this, _LocalStorageDB_currentRoot, new Hash(Uint8Array.from(bytes)), "f");
}
else {
__classPrivateFieldSet(this, _LocalStorageDB_currentRoot, ZERO_HASH, "f");
}
}
async get(k) {
const kBytes = new Uint8Array([...this._prefix, ...k]);
const key = bytes2Hex(kBytes);
const val = localStorage.getItem(key);
if (val === null) {
return undefined;
}
const obj = JSON.parse(val);
switch (obj.type) {
case NODE_TYPE_EMPTY:
return new NodeEmpty();
case NODE_TYPE_MIDDLE:
const cL = new Hash(Uint8Array.from(obj.childL));
const cR = new Hash(Uint8Array.from(obj.childR));
return new NodeMiddle(cL, cR);
case NODE_TYPE_LEAF:
const k = new Hash(Uint8Array.from(obj.entry[0]));
const v = new Hash(Uint8Array.from(obj.entry[1]));
return new NodeLeaf(k, v);
}
throw `error: value found for key ${bytes2Hex(kBytes)} is not of type Node`;
}
async put(k, n) {
const kBytes = new Uint8Array([...this._prefix, ...k]);
const key = bytes2Hex(kBytes);
const toSerialize = {
type: n.type
};
if (n instanceof NodeMiddle) {
toSerialize.childL = Array.from(n.childL.bytes);
toSerialize.childR = Array.from(n.childR.bytes);
}
else if (n instanceof 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(bytes2Hex(this._prefix), JSON.stringify(Array.from(r.bytes)));
}
}
_LocalStorageDB_currentRoot = new WeakMap();