UNPKG

@iden3/js-merkletree

Version:

javascript sparse merkle tree library

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