UNPKG

@iden3/js-merkletree

Version:

javascript sparse merkle tree library

86 lines (85 loc) 3.73 kB
"use strict"; 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; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkEntryInField = exports.elemBytesToBigInts = exports.Entry = void 0; const data_1 = require("../entry/data"); const hash_1 = require("../hash/hash"); const utils_1 = require("../utils"); 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_1.Data(), "f"); __classPrivateFieldSet(this, _Entry_hIndex, hash_1.ZERO_HASH, "f"); __classPrivateFieldSet(this, _Entry_hValue, hash_1.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") === hash_1.ZERO_HASH) { return (0, hash_1.hashElems)((0, exports.elemBytesToBigInts)(this.index)); } return __classPrivateFieldGet(this, _Entry_hIndex, "f"); } async hValue() { if (__classPrivateFieldGet(this, _Entry_hValue, "f") === hash_1.ZERO_HASH) { return (0, hash_1.hashElems)((0, exports.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")); } } exports.Entry = Entry; _Entry_data = new WeakMap(), _Entry_hIndex = new WeakMap(), _Entry_hValue = new WeakMap(); const elemBytesToBigInts = (es) => { const bigInts = es.map((e) => { return e.bigInt(); }); return bigInts; }; exports.elemBytesToBigInts = elemBytesToBigInts; const checkEntryInField = (e) => { const bigInts = (0, exports.elemBytesToBigInts)(e.data.value); let flag = true; bigInts.forEach((b) => { if (!(0, utils_1.checkBigIntInField)(b)) { flag = false; } }); return flag; }; exports.checkEntryInField = checkEntryInField;