UNPKG

@iden3/js-merkletree

Version:

javascript sparse merkle tree library

66 lines (65 loc) 3.35 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 _Data_value; Object.defineProperty(exports, "__esModule", { value: true }); exports.newDataFromBytes = exports.Data = void 0; const elemBytes_1 = require("./elemBytes"); const constants_1 = require("../../constants"); const utils_1 = require("../utils"); class Data { constructor() { _Data_value.set(this, void 0); __classPrivateFieldSet(this, _Data_value, new Array(constants_1.DATA_LEN), "f"); } get value() { return __classPrivateFieldGet(this, _Data_value, "f"); } set value(_v) { if (_v.length !== constants_1.DATA_LEN) { throw `expected bytes length to be ${constants_1.DATA_LEN}, got ${_v.length}`; } __classPrivateFieldSet(this, _Data_value, _v, "f"); } bytes() { const b = new Uint8Array(constants_1.DATA_LEN * constants_1.ELEM_BYTES_LEN); for (let idx = 0; idx < constants_1.DATA_LEN; idx += 1) { __classPrivateFieldGet(this, _Data_value, "f")[idx].value.forEach((v, _idx) => { b[idx * constants_1.ELEM_BYTES_LEN + _idx] = v; }); } return b; } equal(d2) { return ((0, utils_1.bytesEqual)(__classPrivateFieldGet(this, _Data_value, "f")[0].value, d2.value[0].value) && (0, utils_1.bytesEqual)(__classPrivateFieldGet(this, _Data_value, "f")[1].value, d2.value[1].value) && (0, utils_1.bytesEqual)(__classPrivateFieldGet(this, _Data_value, "f")[2].value, d2.value[2].value) && (0, utils_1.bytesEqual)(__classPrivateFieldGet(this, _Data_value, "f")[3].value, d2.value[3].value)); } } exports.Data = Data; _Data_value = new WeakMap(); const newDataFromBytes = (bytes) => { if (bytes.length !== constants_1.DATA_LEN_BYTES) { throw `expected bytes length to be ${constants_1.DATA_LEN_BYTES}, got ${bytes.length}`; } const d = new Data(); const arr = new Array(constants_1.DATA_LEN_BYTES); for (let i = 0; i < constants_1.DATA_LEN; i += 1) { const tmp = new elemBytes_1.ElemBytes(); tmp.value = bytes.slice(i * constants_1.ELEM_BYTES_LEN, (i + 1) * constants_1.DATA_LEN_BYTES); arr[i] = tmp; } d.value = arr; return d; }; exports.newDataFromBytes = newDataFromBytes;