2 lines
1.8 MiB
JavaScript
2 lines
1.8 MiB
JavaScript
|
/*! For license information please see merkleTreeWorker.umd.min.js.LICENSE.txt */
|
||
|
globalThis.process={browser:!0,env:{}},function(e,a){if("object"==typeof exports&&"object"==typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var c=a();for(var f in c)("object"==typeof exports?exports:e)[f]=c[f]}}(self,(()=>(()=>{var __webpack_modules__={7736:(e,a)=>{"use strict";Object.defineProperty(a,"__esModule",{value:!0}),a.BaseTree=void 0,a.BaseTree=class{get capacity(){return 2**this.levels}get layers(){return this._layers.slice()}get zeros(){return this._zeros.slice()}get elements(){return this._layers[0].slice()}get root(){var e;return null!==(e=this._layers[this.levels][0])&&void 0!==e?e:this._zeros[this.levels]}static indexOf(e,a,c,f){return f?e.findIndex((e=>f(a,e))):e.indexOf(a,c)}insert(e){if(this._layers[0].length>=this.capacity)throw new Error("Tree is full");this.update(this._layers[0].length,e)}bulkInsert(e){if(e.length){if(this._layers[0].length+e.length>this.capacity)throw new Error("Tree is full");for(let a=0;a<e.length-1;a++){this._layers[0].push(e[a]);let c=0,f=this._layers[0].length-1;for(;f%2==1;){c++,f>>=1;const e=this._layers[c-1][2*f],a=this._layers[c-1][2*f+1];this._layers[c][f]=this._hashFn(e,a)}}this.insert(e[e.length-1])}}update(e,a){if(isNaN(Number(e))||e<0||e>this._layers[0].length||e>=this.capacity)throw new Error("Insert index out of bounds: "+e);this._layers[0][e]=a,this._processUpdate(e)}path(e){if(isNaN(Number(e))||e<0||e>=this._layers[0].length)throw new Error("Index out of bounds: "+e);let a=+e;const c=[],f=[],d=[];for(let e=0;e<this.levels;e++){f[e]=a%2;const b=1^a;b<this._layers[e].length?(c[e]=this._layers[e][b],d[e]=b):(c[e]=this._zeros[e],d[e]=0),a>>=1}return{pathElements:c,pathIndices:f,pathPositions:d,pathRoot:this.root}}_buildZeros(){this._zeros=[this.zeroElement];for(let e=1;e<=this.levels;e++)this._zeros[e]=this._hashFn(this._zeros[e-1],this._zeros[e-1])}_processNodes(e,a){const c=e.length;let f=Math.ceil(c/2);const d=new Array(f);f--;const b=c-(c%2^1);let t=0;for(let i=b;i>=0&&void 0!==e[i-1];i-=2){const r=e[i-1],n=i===b&&c%2==1?this._zeros[a-1]:e[i];d[f-t]=this._hashFn(r,n),t++}return d}_processUpdate(e){for(let a=1;a<=this.levels;a++){e>>=1;const c=this._layers[a-1][2*e],f=2*e+1<this._layers[a-1].length?this._layers[a-1][2*e+1]:this._zeros[a-1];this._layers[a][e]=this._hashFn(c,f)}}}},9093:function(e,a,c){"use strict";var f=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(a,"__esModule",{value:!0});const d=f(c(5319)),b=c(7736);class t extends b.BaseTree{constructor(e,a=[],{hashFunction:c=d.default,zeroElement:f=0}={}){if(super(),this.levels=e,a.length>this.capacity)throw new Error("Tree is full");this._hashFn=c,this.zeroElement=f,this._layers=[];const b=a.slice();this._layers=[b],this._buildZeros(),this._buildHashes()}_buildHashes(){for(let e=1;e<=this.levels;e++){const a=this._layers[e-1];this._layers[e]=this._processNodes(a,e)}}bulkInsert(e){if(e.length){if(this._layers[0].length+e.length>this.capacity)throw new Error("Tree is full");for(let a=0;a<e.length-1;a++){this._layers[0].push(e[a]);let c=0,f=this._layers[0].length-1;for(;f%2==1;)c++,f>>=1,this._layers[c][f]=this._hashFn(this._layers[c-1][2*f],this._layers[c-1][2*f+1])}this.insert(e[e.length-1])}}indexOf(e,a){return b.BaseTree.indexOf(this._layers[0],e,0,a)}proof(e){const a=this.indexOf(e);return this.path(a)}getTreeEdge(e){const a=this._layers[0][e];if(void 0===a)throw new Error("Element not found");return{edgePath:this.path(e),edgeElement:a,edgeIndex:e,edgeElementsCount:this._layers[0].length}}getTreeSlices(e=4){const a=this._layers[0].length;let c=Math.ceil(a/e);c%2&&c++;const f=[];for(let e=0;e<a;e+=c){const a=e,d=e+c;f.push({edge:this.getTreeEdge(a),elements:this.elements.slice(a,d)})}return f}serialize(){return{levels:this.levels,_zeros:this._zeros,_layers:this._layers}}static deserialize(e,a){const c=Object.assign(Object.create(this.prototype),e);return c._hashFn=a||d.default,c.zeroElement=c._zeros[0],c}toString(){return JSON.stringify(this.serialize())}}a.default=t},1230:function(e,a,c){"use strict";var
|