UNPKG

@zk-kit/merkle-tree

Version:
10 lines (9 loc) 3.2 kB
/** * @module @zk-kit/merkle-tree * @version 0.1.0 * @file Merkle tree implementation in TypeScript. * @copyright Omar Desogus 2022 * @license MIT * @see [Github]{@link https://github.com/appliedzkp/zk-kit/tree/main/packages/merkle-tree} */ var zkKitMerkleTree=function(e){"use strict";function t(e,t){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];if(void 0===e)throw new TypeError("Parameter '".concat(t,"' is not defined"));if(!r.includes(typeof e))throw new TypeError("Parameter '".concat(t,"' is none of these types: ").concat(r.join(", ")))}var r=function(){function e(r,o,n){if(t(r,"hash","function"),t(o,"depth","number"),t(n,"zeroValue","number","string","bigint"),o<1||o>e.maxDepth)throw new Error("The tree depth must be between 1 and 32");this._hash=r,this._depth=o,this._zeroes=[],this._nodes=[];for(var i=0;i<o;i+=1)this._zeroes.push(n),this._nodes[i]=[],n=r([n,n]);this._root=n,Object.freeze(this._zeroes),Object.freeze(this._nodes)}return Object.defineProperty(e.prototype,"root",{get:function(){return this._root},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"depth",{get:function(){return this._depth},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"leaves",{get:function(){return this._nodes[0].slice()},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"zeroes",{get:function(){return this._zeroes},enumerable:!1,configurable:!0}),e.prototype.insert=function(e){var r=this;if(t(e,"leaf","number","string","bigint"),e===this._zeroes[0])throw new Error("The leaf cannot be a zero value");if(this.leaves.length>=Math.pow(2,this._depth))throw new Error("The tree is full");var o=e;this.forEachLevel(this.leaves.length,(function(e,t,n){r._nodes[e][t]=o,o=n?r._hash([o,r._zeroes[e]]):r._hash([r._nodes[e][t-1],o])})),this._root=o},e.prototype.delete=function(e){var r=this;if(t(e,"index","number"),e<0||e>=this.leaves.length)throw new Error("The leaf does not exist in this tree");var o=this._zeroes[0];this.forEachLevel(e,(function(e,t,n){r._nodes[e][t]=o,o=n?r._hash([o,r._nodes[e][t+1]||r._zeroes[e]]):r._hash([r._nodes[e][t-1],o])})),this._root=o},e.prototype.createProof=function(e){var r=this;if(t(e,"index","number"),e<0||e>=this.leaves.length)throw new Error("The leaf does not exist in this tree");var o=[],n=[];return this.forEachLevel(e,(function(e,t,i){i?(n.push(0),o.push(r._nodes[e][t+1]||r._zeroes[e])):(n.push(1),o.push(r._nodes[e][t-1]))})),{root:this._root,leaf:this.leaves[e],siblingNodes:o,path:n}},e.prototype.verifyProof=function(e){t(e,"proof","object"),t(e.root,"proof.root","number","string","bigint"),t(e.leaf,"proof.leaf","number","string","bigint"),t(e.siblingNodes,"proof.siblingNodes","object"),t(e.path,"proof.path","object");for(var r=e.leaf,o=0;o<e.siblingNodes.length;o+=1)r=e.path[o]?this._hash([e.siblingNodes[o],r]):this._hash([r,e.siblingNodes[o]]);return e.root===r},e.prototype.indexOf=function(e){return t(e,"leaf","number","string","bigint"),this.leaves.indexOf(e)},e.prototype.forEachLevel=function(e,t){for(var r=0;r<this._depth;r+=1)t(r,e,e%2==0),e=Math.floor(e/2)},e.maxDepth=32,e}();return e.MerkleTree=r,Object.defineProperty(e,"__esModule",{value:!0}),e}({});