UNPKG

@iden3/js-merkletree

Version:

javascript sparse merkle tree library

80 lines (79 loc) 3.38 kB
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 _Entry_data, _Entry_hIndex, _Entry_hValue; import { Data } from '../entry/data'; import { ZERO_HASH, hashElems } from '../hash/hash'; import { checkBigIntInField } from '../utils'; export class Entry { constructor(_data) { _Entry_data.set(this, void 0); _Entry_hIndex.set(this, void 0); _Entry_hValue.set(this, void 0); __classPrivateFieldSet(this, _Entry_data, _data ? _data : new Data(), "f"); __classPrivateFieldSet(this, _Entry_hIndex, ZERO_HASH, "f"); __classPrivateFieldSet(this, _Entry_hValue, ZERO_HASH, "f"); } get data() { return __classPrivateFieldGet(this, _Entry_data, "f"); } get index() { return __classPrivateFieldGet(this, _Entry_data, "f").value.slice(0, 4); } get value() { return __classPrivateFieldGet(this, _Entry_data, "f").value.slice(4, 8); } async hIndex() { if (__classPrivateFieldGet(this, _Entry_hIndex, "f") === ZERO_HASH) { return hashElems(elemBytesToBigInts(this.index)); } return __classPrivateFieldGet(this, _Entry_hIndex, "f"); } async hValue() { if (__classPrivateFieldGet(this, _Entry_hValue, "f") === ZERO_HASH) { return hashElems(elemBytesToBigInts(this.value)); } return __classPrivateFieldGet(this, _Entry_hValue, "f"); } hiHv() { return (async () => { const hi = await this.hIndex(); const hv = await this.hValue(); return { hi, hv }; })(); } bytes() { return __classPrivateFieldGet(this, _Entry_data, "f").value; } equal(e2) { return __classPrivateFieldGet(this, _Entry_data, "f").equal(e2.data); } clone() { return new Entry(__classPrivateFieldGet(this, _Entry_data, "f")); } } _Entry_data = new WeakMap(), _Entry_hIndex = new WeakMap(), _Entry_hValue = new WeakMap(); export const elemBytesToBigInts = (es) => { const bigInts = es.map((e) => { return e.bigInt(); }); return bigInts; }; export const checkEntryInField = (e) => { const bigInts = elemBytesToBigInts(e.data.value); let flag = true; bigInts.forEach((b) => { if (!checkBigIntInField(b)) { flag = false; } }); return flag; };