UNPKG

merkle-btree

Version:
3 lines (2 loc) 34.1 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("crypto")):"function"==typeof define&&define.amd?define(["crypto"],e):t.merkleBtree=e(t.crypto)}(this,function(t){"use strict";function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){return t<e}function n(t,e){return t<=e}function o(t,e){return t>e}function i(t,e){return t>=e}function s(){return!0}function u(t,e){return t.substring(0,e.length)===e}function h(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(){_t=!0;for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",e=0,r=t.length;e<r;++e)bt[e]=t[e],Pt[t.charCodeAt(e)]=e;Pt["-".charCodeAt(0)]=62,Pt["_".charCodeAt(0)]=63}function f(t){_t||a();var e,r,n,o,i,s,u=t.length;if(u%4>0)throw new Error("Invalid string. Length must be a multiple of 4");i="="===t[u-2]?2:"="===t[u-1]?1:0,s=new Rt(3*u/4-i),n=i>0?u-4:u;var h=0;for(e=0,r=0;e<n;e+=4,r+=3)o=Pt[t.charCodeAt(e)]<<18|Pt[t.charCodeAt(e+1)]<<12|Pt[t.charCodeAt(e+2)]<<6|Pt[t.charCodeAt(e+3)],s[h++]=o>>16&255,s[h++]=o>>8&255,s[h++]=255&o;return 2===i?(o=Pt[t.charCodeAt(e)]<<2|Pt[t.charCodeAt(e+1)]>>4,s[h++]=255&o):1===i&&(o=Pt[t.charCodeAt(e)]<<10|Pt[t.charCodeAt(e+1)]<<4|Pt[t.charCodeAt(e+2)]>>2,s[h++]=o>>8&255,s[h++]=255&o),s}function c(t){return bt[t>>18&63]+bt[t>>12&63]+bt[t>>6&63]+bt[63&t]}function l(t,e,r){for(var n,o=[],i=e;i<r;i+=3)n=(t[i]<<16)+(t[i+1]<<8)+t[i+2],o.push(c(n));return o.join("")}function p(t){_t||a();for(var e,r=t.length,n=r%3,o="",i=[],s=0,u=r-n;s<u;s+=16383)i.push(l(t,s,s+16383>u?u:s+16383));return 1===n?(e=t[r-1],o+=bt[e>>2],o+=bt[e<<4&63],o+="=="):2===n&&(e=(t[r-2]<<8)+t[r-1],o+=bt[e>>10],o+=bt[e>>4&63],o+=bt[e<<2&63],o+="="),i.push(o),i.join("")}function g(t,e,r,n,o){var i,s,u=8*o-n-1,h=(1<<u)-1,a=h>>1,f=-7,c=r?o-1:0,l=r?-1:1,p=t[e+c];for(c+=l,i=p&(1<<-f)-1,p>>=-f,f+=u;f>0;i=256*i+t[e+c],c+=l,f-=8);for(s=i&(1<<-f)-1,i>>=-f,f+=n;f>0;s=256*s+t[e+c],c+=l,f-=8);if(0===i)i=1-a;else{if(i===h)return s?NaN:1/0*(p?-1:1);s+=Math.pow(2,n),i-=a}return(p?-1:1)*s*Math.pow(2,i-n)}function y(t,e,r,n,o,i){var s,u,h,a=8*i-o-1,f=(1<<a)-1,c=f>>1,l=23===o?Math.pow(2,-24)-Math.pow(2,-77):0,p=n?0:i-1,g=n?1:-1,y=e<0||0===e&&1/e<0?1:0;for(e=Math.abs(e),isNaN(e)||e===1/0?(u=isNaN(e)?1:0,s=f):(s=Math.floor(Math.log(e)/Math.LN2),e*(h=Math.pow(2,-s))<1&&(s--,h*=2),e+=s+c>=1?l/h:l*Math.pow(2,1-c),e*h>=2&&(s++,h/=2),s+c>=f?(u=0,s=f):s+c>=1?(u=(e*h-1)*Math.pow(2,o),s+=c):(u=e*Math.pow(2,c-1)*Math.pow(2,o),s=0));o>=8;t[r+p]=255&u,p+=g,u/=256,o-=8);for(s=s<<o|u,a+=o;a>0;t[r+p]=255&s,p+=g,s/=256,a-=8);t[r+p-g]|=128*y}function d(){return w.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function v(t,e){if(d()<e)throw new RangeError("Invalid typed array length");return w.TYPED_ARRAY_SUPPORT?(t=new Uint8Array(e),t.__proto__=w.prototype):(null===t&&(t=new w(e)),t.length=e),t}function w(t,e,r){if(!(w.TYPED_ARRAY_SUPPORT||this instanceof w))return new w(t,e,r);if("number"==typeof t){if("string"==typeof e)throw new Error("If encoding is specified then the first argument must be a string");return A(this,t)}return m(this,t,e,r)}function m(t,e,r,n){if("number"==typeof e)throw new TypeError('"value" argument must not be a number');return"undefined"!=typeof ArrayBuffer&&e instanceof ArrayBuffer?R(t,e,r,n):"string"==typeof e?b(t,e,r):_(t,e)}function k(t){if("number"!=typeof t)throw new TypeError('"size" argument must be a number');if(t<0)throw new RangeError('"size" argument must not be negative')}function E(t,e,r,n){return k(e),e<=0?v(t,e):void 0!==r?"string"==typeof n?v(t,e).fill(r,n):v(t,e).fill(r):v(t,e)}function A(t,e){if(k(e),t=v(t,e<0?0:0|T(e)),!w.TYPED_ARRAY_SUPPORT)for(var r=0;r<e;++r)t[r]=0;return t}function b(t,e,r){if("string"==typeof r&&""!==r||(r="utf8"),!w.isEncoding(r))throw new TypeError('"encoding" must be a valid string encoding');var n=0|I(e,r);t=v(t,n);var o=t.write(e,r);return o!==n&&(t=t.slice(0,o)),t}function P(t,e){var r=e.length<0?0:0|T(e.length);t=v(t,r);for(var n=0;n<r;n+=1)t[n]=255&e[n];return t}function R(t,e,r,n){if(e.byteLength,r<0||e.byteLength<r)throw new RangeError("'offset' is out of bounds");if(e.byteLength<r+(n||0))throw new RangeError("'length' is out of bounds");return e=void 0===r&&void 0===n?new Uint8Array(e):void 0===n?new Uint8Array(e,r):new Uint8Array(e,r,n),w.TYPED_ARRAY_SUPPORT?(t=e,t.__proto__=w.prototype):t=P(t,e),t}function _(t,e){if(S(e)){var r=0|T(e.length);return t=v(t,r),0===t.length?t:(e.copy(t,0,0,r),t)}if(e){if("undefined"!=typeof ArrayBuffer&&e.buffer instanceof ArrayBuffer||"length"in e)return"number"!=typeof e.length||ht(e.length)?v(t,0):P(t,e);if("Buffer"===e.type&&St(e.data))return P(t,e.data)}throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.")}function T(t){if(t>=d())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+d().toString(16)+" bytes");return 0|t}function S(t){return!(null==t||!t._isBuffer)}function I(t,e){if(S(t))return t.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(t)||t instanceof ArrayBuffer))return t.byteLength;"string"!=typeof t&&(t=""+t);var r=t.length;if(0===r)return 0;for(var n=!1;;)switch(e){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":case void 0:return nt(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;case"base64":return st(t).length;default:if(n)return nt(t).length;e=(""+e).toLowerCase(),n=!0}}function U(t,e,r){var n=!1;if((void 0===e||e<0)&&(e=0),e>this.length)return"";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return"";if(r>>>=0,e>>>=0,r<=e)return"";for(t||(t="utf8");;)switch(t){case"hex":return J(this,e,r);case"utf8":case"utf-8":return D(this,e,r);case"ascii":return j(this,e,r);case"latin1":case"binary":return q(this,e,r);case"base64":return z(this,e,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return G(this,e,r);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}function B(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}function C(t,e,r,n,o){if(0===t.length)return-1;if("string"==typeof r?(n=r,r=0):r>2147483647?r=2147483647:r<-2147483648&&(r=-2147483648),r=+r,isNaN(r)&&(r=o?0:t.length-1),r<0&&(r=t.length+r),r>=t.length){if(o)return-1;r=t.length-1}else if(r<0){if(!o)return-1;r=0}if("string"==typeof e&&(e=w.from(e,n)),S(e))return 0===e.length?-1:N(t,e,r,n,o);if("number"==typeof e)return e&=255,w.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?o?Uint8Array.prototype.indexOf.call(t,e,r):Uint8Array.prototype.lastIndexOf.call(t,e,r):N(t,[e],r,n,o);throw new TypeError("val must be string, number or Buffer")}function N(t,e,r,n,o){function i(t,e){return 1===s?t[e]:t.readUInt16BE(e*s)}var s=1,u=t.length,h=e.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||e.length<2)return-1;s=2,u/=2,h/=2,r/=2}var a;if(o){var f=-1;for(a=r;a<u;a++)if(i(t,a)===i(e,-1===f?0:a-f)){if(-1===f&&(f=a),a-f+1===h)return f*s}else-1!==f&&(a-=a-f),f=-1}else for(r+h>u&&(r=u-h),a=r;a>=0;a--){for(var c=!0,l=0;l<h;l++)if(i(t,a+l)!==i(e,l)){c=!1;break}if(c)return a}return-1}function x(t,e,r,n){r=Number(r)||0;var o=t.length-r;n?(n=Number(n))>o&&(n=o):n=o;var i=e.length;if(i%2!=0)throw new TypeError("Invalid hex string");n>i/2&&(n=i/2);for(var s=0;s<n;++s){var u=parseInt(e.substr(2*s,2),16);if(isNaN(u))return s;t[r+s]=u}return s}function Y(t,e,r,n){return ut(nt(e,t.length-r),t,r,n)}function H(t,e,r,n){return ut(ot(e),t,r,n)}function L(t,e,r,n){return H(t,e,r,n)}function M(t,e,r,n){return ut(st(e),t,r,n)}function O(t,e,r,n){return ut(it(e,t.length-r),t,r,n)}function z(t,e,r){return p(0===e&&r===t.length?t:t.slice(e,r))}function D(t,e,r){r=Math.min(t.length,r);for(var n=[],o=e;o<r;){var i=t[o],s=null,u=i>239?4:i>223?3:i>191?2:1;if(o+u<=r){var h,a,f,c;switch(u){case 1:i<128&&(s=i);break;case 2:h=t[o+1],128==(192&h)&&(c=(31&i)<<6|63&h)>127&&(s=c);break;case 3:h=t[o+1],a=t[o+2],128==(192&h)&&128==(192&a)&&(c=(15&i)<<12|(63&h)<<6|63&a)>2047&&(c<55296||c>57343)&&(s=c);break;case 4:h=t[o+1],a=t[o+2],f=t[o+3],128==(192&h)&&128==(192&a)&&128==(192&f)&&(c=(15&i)<<18|(63&h)<<12|(63&a)<<6|63&f)>65535&&c<1114112&&(s=c)}}null===s?(s=65533,u=1):s>65535&&(s-=65536,n.push(s>>>10&1023|55296),s=56320|1023&s),n.push(s),o+=u}return F(n)}function F(t){var e=t.length;if(e<=It)return String.fromCharCode.apply(String,t);for(var r="",n=0;n<e;)r+=String.fromCharCode.apply(String,t.slice(n,n+=It));return r}function j(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;o<r;++o)n+=String.fromCharCode(127&t[o]);return n}function q(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;o<r;++o)n+=String.fromCharCode(t[o]);return n}function J(t,e,r){var n=t.length;(!e||e<0)&&(e=0),(!r||r<0||r>n)&&(r=n);for(var o="",i=e;i<r;++i)o+=rt(t[i]);return o}function G(t,e,r){for(var n=t.slice(e,r),o="",i=0;i<n.length;i+=2)o+=String.fromCharCode(n[i]+256*n[i+1]);return o}function K(t,e,r){if(t%1!=0||t<0)throw new RangeError("offset is not uint");if(t+e>r)throw new RangeError("Trying to access beyond buffer length")}function V(t,e,r,n,o,i){if(!S(t))throw new TypeError('"buffer" argument must be a Buffer instance');if(e>o||e<i)throw new RangeError('"value" argument is out of bounds');if(r+n>t.length)throw new RangeError("Index out of range")}function Z(t,e,r,n){e<0&&(e=65535+e+1);for(var o=0,i=Math.min(t.length-r,2);o<i;++o)t[r+o]=(e&255<<8*(n?o:1-o))>>>8*(n?o:1-o)}function W(t,e,r,n){e<0&&(e=4294967295+e+1);for(var o=0,i=Math.min(t.length-r,4);o<i;++o)t[r+o]=e>>>8*(n?o:3-o)&255}function X(t,e,r,n,o,i){if(r+n>t.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function Q(t,e,r,n,o){return o||X(t,e,r,4,3.4028234663852886e38,-3.4028234663852886e38),y(t,e,r,n,23,4),r+4}function $(t,e,r,n,o){return o||X(t,e,r,8,1.7976931348623157e308,-1.7976931348623157e308),y(t,e,r,n,52,8),r+8}function tt(t){if(t=et(t).replace(Ut,""),t.length<2)return"";for(;t.length%4!=0;)t+="=";return t}function et(t){return t.trim?t.trim():t.replace(/^\s+|\s+$/g,"")}function rt(t){return t<16?"0"+t.toString(16):t.toString(16)}function nt(t,e){e=e||1/0;for(var r,n=t.length,o=null,i=[],s=0;s<n;++s){if((r=t.charCodeAt(s))>55295&&r<57344){if(!o){if(r>56319){(e-=3)>-1&&i.push(239,191,189);continue}if(s+1===n){(e-=3)>-1&&i.push(239,191,189);continue}o=r;continue}if(r<56320){(e-=3)>-1&&i.push(239,191,189),o=r;continue}r=65536+(o-55296<<10|r-56320)}else o&&(e-=3)>-1&&i.push(239,191,189);if(o=null,r<128){if((e-=1)<0)break;i.push(r)}else if(r<2048){if((e-=2)<0)break;i.push(r>>6|192,63&r|128)}else if(r<65536){if((e-=3)<0)break;i.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;i.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return i}function ot(t){for(var e=[],r=0;r<t.length;++r)e.push(255&t.charCodeAt(r));return e}function it(t,e){for(var r,n,o,i=[],s=0;s<t.length&&!((e-=2)<0);++s)r=t.charCodeAt(s),n=r>>8,o=r%256,i.push(o),i.push(n);return i}function st(t){return f(tt(t))}function ut(t,e,r,n){for(var o=0;o<n&&!(o+r>=e.length||o>=t.length);++o)e[o+r]=t[o];return o}function ht(t){return t!==t}function at(t){return null!=t&&(!!t._isBuffer||ft(t)||ct(t))}function ft(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}function ct(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&ft(t.slice(0,0))}function lt(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function pt(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function gt(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function yt(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function dt(t){return new Promise(function(e,r){var n=new XMLHttpRequest;n.onreadystatechange=function(){4===n.readyState&&(n.status>=200&&n.status<300?e(n.responseText):r(n.responseText))},n.open("GET",t,!0),n.send(null)})}function vt(t){return new Promise(function(e,r){(t.startsWith("https")?require("https"):require("http")).get(t,function(t){(t.statusCode<200||t.statusCode>299)&&r(new Error("Failed to load page, status code: "+t.statusCode));var n=[];t.on("data",function(t){return n.push(t)}),t.on("end",function(){return e(n.join(""))})}).on("error",function(t){return r(t)})})}function wt(t){return xt&&require.resolve("http")&&require.resolve("https")?vt(t):dt(t)}var mt=function t(r,n,o){e(this,t),this.key=r,this.value=n,this.targetHash=o},kt=function(){function t(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],o=arguments[2];e(this,t),this.hash=o,this.leftChildHash=r,this.keys=n;var i=new mt("",null,r);this.keys.length&&!this.keys[0].key.length||this.keys.unshift(i)}return t.prototype.get=function(e,r){for(var n=this.keys[0],o=this.keys,i=Array.isArray(o),s=0,o=i?o:o[Symbol.iterator]();;){var u;if(i){if(s>=o.length)break;u=o[s++]}else{if(s=o.next(),s.done)break;u=s.value}var h=u;if(e<h.key)break;n=h}return n.targetHash?r.get(n.targetHash).then(function(o){return t.deserialize(o,n.targetHash).get(e,r)}):n.key===e?Promise.resolve(n.value):Promise.resolve(null)},t.prototype.searchText=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:100,r=arguments[2],n=arguments[3],o=arguments[4],i=r||t,s=void 0,u=!r,h=!0;return n&&(i=t,s=r||void 0,u=!0,h=!r),this.searchRange(i,s,t,e,u,h,n,o)},t.prototype.searchRange=function(e,h,a,f){function c(r){if(r<0||r>=v.keys.length)return Promise.resolve(d);if(f&&d.length>=f)return d=d.slice(0,f+1),Promise.resolve(d);var n=g?r-1:r+1,o=v.keys[r];if(n>=0&&n<v.keys.length){if(!g&&e>=v.keys[n].key)return c(n);if(g&&h<v.keys[n].key)return c(n)}if(g&&r+1<v.keys.length&&!w(v.keys[r+1].key,e))return Promise.resolve(d);if(!g&&!m(o.key,h))return Promise.resolve(d);if(o.targetHash)return y.get(o.targetHash).then(function(r){var n=f?f-d.length:void 0;return t.deserialize(r,o.targetHash).searchRange(e,h,a,n,l,p,g,y)}).then(function(t){if(t){if(d.length&&!t.length)return Promise.resolve(d);if(a&&!t.length)return Promise.resolve(d);Array.prototype.push.apply(d,t)}return c(n)});if(o.key.length&&o.value){if(a&&d.length&&!u(o.key,a))return Promise.resolve(d);w(o.key,e)&&m(o.key,h)&&(a&&!u(o.key,a)||d.push({key:o.key,value:o.value}))}return c(n)}var l=!(arguments.length>4&&void 0!==arguments[4])||arguments[4],p=!(arguments.length>5&&void 0!==arguments[5])||arguments[5],g=arguments[6],y=arguments[7],d=[],v=this,w=void 0,m=void 0;return w=e?l?i:o:s,m=h?p?n:r:s,c(g?this.keys.length-1:0)},t.prototype.save=function(t){var e=this;return t.put(this.serialize()).then(function(t){return e.hash=t,e.hash})},t.prototype._getLeafInsertIndex=function(t){var e=this._getNextSmallestIndex(t),r=e.index,n=e.exists,o=r+1;return t>this.keys[this.keys.length-1].key&&(o=this.keys.length),{leafInsertIndex:o,exists:n}},t.prototype._getNextSmallestIndex=function(t){for(var e=0,r=void 0,n=1;n<this.keys.length;n++){if(t===this.keys[n].key){r=!0;break}if(t<this.keys[n].key)break;e=n}return{index:e,exists:r}},t.prototype._splitNode=function(e){var r=Math.floor(this.keys.length/2),n=this.keys[r],o=new t(null,this.keys.slice(0,r)),i=e.put(o.serialize()),s=this.keys.slice(r,this.keys.length);this.keys[0].targetHash&&s.shift();var u=new t(this.keys[r].targetHash,s),h=e.put(u.serialize());return Promise.all([i,h,this._removeIfNotEmpty(e)]).then(function(e){var r=e[0],o=e[1];return new t(r,[new mt(n.key,null,o)])})},t.prototype._removeIfNotEmpty=function(t){return this.hash&&this.keys.length>0?t.remove(this.hash):Promise.resolve()},t.prototype._saveToLeafNode=function(e,r,n,o){var i=this,s=new mt(e,r,null),u=this._getLeafInsertIndex(e),h=u.leafInsertIndex;return u.exists?(this.keys[h]=s,this._removeIfNotEmpty(n).then(function(){return n.put(i.serialize())}).then(function(e){return new t(i.leftChildHash,i.keys,e)})):(this.keys.splice(h,0,s),this.keys.length<o?this._removeIfNotEmpty(n).then(function(){return n.put(i.serialize())}).then(function(e){return new t(i.leftChildHash,i.keys,e)}):this._splitNode(n))},t.prototype._saveToBranchNode=function(e,r,n,o){var i=this,s=this._getNextSmallestIndex(e),u=s.index,h=this.keys[u];return n.get(h.targetHash).then(function(i){return t.deserialize(i,h.targetHash).put(e,r,n,o)}).then(function(e){return e.hash?(i.keys[u]=new mt(i.keys[u].key,null,e.hash),n.remove(i.hash),i._removeIfNotEmpty(n).then(function(){n.put(i.serialize())}).then(function(e){return new t(i.leftChildHash,i.keys,e)})):(i.keys[u]=new mt(h.key,null,e.keys[0].targetHash),i.keys.splice(u+1,0,e.keys[e.keys.length-1]),i.keys.length<o?i._removeIfNotEmpty(n).then(function(){return n.put(i.serialize())}).then(function(e){return new t(i.leftChildHash,i.keys,e)}):i._splitNode(n))})},t.prototype.put=function(t,e,r,n){return this.keys[0].targetHash?this._saveToBranchNode(t,e,r,n):this._saveToLeafNode(t,e,r,n)},t.prototype.delete=function(e,r){var n=this,o=this.keys[0],i=void 0;for(i=0;i<this.keys.length;i++){if(e<this.keys[i].key){i=Math.max(i-1,0);break}o=this.keys[i]}var s=Promise.resolve();return o.targetHash?s=s.then(function(){return r.get(o.targetHash).then(function(n){return t.deserialize(n,o.targetHash).delete(e,r)}).then(function(t){var e=o.targetHash;return o.targetHash=t.hash,r.remove(e)})}):o.key===e&&this.keys.splice(i,1),s.then(function(){return r.put(n.serialize())}).then(function(e){return new t(n.leftChildHash,n.keys,e)})},t.prototype.size=function(e){return this.keys.reduce(function(r,n){return r.then(function(r){return n.targetHash?e.get(n.targetHash).then(function(r){return t.deserialize(r).size(e)}).then(function(t){return t+r}):r})},Promise.resolve(this.keys.length))},t.prototype.smallestKey=function(){return this.keys.length?this.keys[0]:null},t.prototype.smallestNonZeroKey=function(){return this.keys.length>1?this.keys[1]:null},t.prototype.rebalance=function(){},t.prototype.print=function(e){for(var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n="node: "+this.hash+"\n",o="",i=0;i<=r;i++)o+="- ";return this.keys.forEach(function(t){n+=o,n+="key: "+t.key,t.targetHash&&(n+=" link: "+t.targetHash),t.value&&(n+="\n"+o+" value: "+JSON.stringify(t.value)),n+="\n"}),this.keys.forEach(function(i){i.targetHash&&e.get(i.targetHash).then(function(s){var u=t.deserialize(s,i.targetHash);n+="\n",n+=o,n+=u.print(e,r+1)})}),n},t.prototype.serialize=function(){return JSON.stringify({leftChildHash:this.leftChildHash,keys:this.keys})},t.prototype.toSortedList=function(e){function r(i){return i>=o.keys.length?Promise.resolve(n):o.keys[i].targetHash?e.get(o.keys[i].targetHash).then(function(e){return t.deserialize(e,o.keys[i].targetHash).toSortedList()}).then(function(t){return n=n.concat(t),r(i+1)}):(o.keys[i].key.length&&n.push(o.keys[i]),r(i+1))}var n=[],o=this;return r(0)},t.deserialize=function(e,r){return e=JSON.parse(e),new t(e.leftChildHash,e.keys,r)},t.fromSortedList=function(e,r,n){function o(i){if(e.length){var s=e.splice(0,r);return n.put(new t(s[0].targetHash,s).serialize()).then(function(t){return i.push({key:s[1].key,targetHash:t,value:null}),o(i)})}return i.length&&i[0].targetHash&&(i[0].key=""),t.fromSortedList(i,r,n)}if(e.length>r)return o([]);var i=e.length?e[0].targetHash:null,s=new t(i,e);return n.put(s.serialize()).then(function(t){return s.hash=t,s})},t}(),Et=function(){function t(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:new kt;h(this,t),this.rootNode=n,this.storage=e,this.maxChildren=Math.max(r,2)}return t.prototype.get=function(t){return this.rootNode.get(t,this.storage)},t.prototype.searchText=function(t,e,r){var n=arguments.length>3&&void 0!==arguments[3]&&arguments[3];return this.rootNode.searchText(t,e,r,n,this.storage)},t.prototype.searchRange=function(t,e,r){var n=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],o=!(arguments.length>4&&void 0!==arguments[4])||arguments[4],i=arguments.length>5&&void 0!==arguments[5]&&arguments[5];return this.rootNode.searchRange(t,e,!1,r,n,o,i,this.storage)},t.prototype.put=function(t,e){var r=this;return this.rootNode.put(t,e,this.storage,this.maxChildren).then(function(t){return r.rootNode=t,r.rootNode.hash})},t.prototype.delete=function(t){var e=this;return this.rootNode.delete(t,this.storage,this.maxChildren).then(function(t){return e.rootNode=t,e.rootNode.hash})},t.prototype.print=function(){return this.rootNode.print(this.storage)},t.prototype.size=function(){return this.rootNode.size(this.storage)},t.prototype.toSortedList=function(){return this.rootNode.toSortedList(this.storage)},t.prototype.save=function(){return this.rootNode.save(this.storage)},t.getByHash=function(e,r,n){return r.get(e).then(function(e){var o=kt.deserialize(e);return new t(r,n,o)})},t.fromSortedList=function(e,r,n){return kt.fromSortedList(e,r,n).then(function(e){return new t(n,r,e)})},t}(),At="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{},bt=[],Pt=[],Rt="undefined"!=typeof Uint8Array?Uint8Array:Array,_t=!1,Tt={}.toString,St=Array.isArray||function(t){return"[object Array]"==Tt.call(t)};w.TYPED_ARRAY_SUPPORT=void 0===At.TYPED_ARRAY_SUPPORT||At.TYPED_ARRAY_SUPPORT,w.poolSize=8192,w._augment=function(t){return t.__proto__=w.prototype,t},w.from=function(t,e,r){return m(null,t,e,r)},w.TYPED_ARRAY_SUPPORT&&(w.prototype.__proto__=Uint8Array.prototype,w.__proto__=Uint8Array,"undefined"!=typeof Symbol&&Symbol.species&&w[Symbol.species]),w.alloc=function(t,e,r){return E(null,t,e,r)},w.allocUnsafe=function(t){return A(null,t)},w.allocUnsafeSlow=function(t){return A(null,t)},w.isBuffer=at,w.compare=function(t,e){if(!S(t)||!S(e))throw new TypeError("Arguments must be Buffers");if(t===e)return 0;for(var r=t.length,n=e.length,o=0,i=Math.min(r,n);o<i;++o)if(t[o]!==e[o]){r=t[o],n=e[o];break}return r<n?-1:n<r?1:0},w.isEncoding=function(t){switch(String(t).toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"latin1":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return!0;default:return!1}},w.concat=function(t,e){if(!St(t))throw new TypeError('"list" argument must be an Array of Buffers');if(0===t.length)return w.alloc(0);var r;if(void 0===e)for(e=0,r=0;r<t.length;++r)e+=t[r].length;var n=w.allocUnsafe(e),o=0;for(r=0;r<t.length;++r){var i=t[r];if(!S(i))throw new TypeError('"list" argument must be an Array of Buffers');i.copy(n,o),o+=i.length}return n},w.byteLength=I,w.prototype._isBuffer=!0,w.prototype.swap16=function(){var t=this.length;if(t%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(var e=0;e<t;e+=2)B(this,e,e+1);return this},w.prototype.swap32=function(){var t=this.length;if(t%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(var e=0;e<t;e+=4)B(this,e,e+3),B(this,e+1,e+2);return this},w.prototype.swap64=function(){var t=this.length;if(t%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(var e=0;e<t;e+=8)B(this,e,e+7),B(this,e+1,e+6),B(this,e+2,e+5),B(this,e+3,e+4);return this},w.prototype.toString=function(){var t=0|this.length;return 0===t?"":0===arguments.length?D(this,0,t):U.apply(this,arguments)},w.prototype.equals=function(t){if(!S(t))throw new TypeError("Argument must be a Buffer");return this===t||0===w.compare(this,t)},w.prototype.inspect=function(){var t="";return this.length>0&&(t=this.toString("hex",0,50).match(/.{2}/g).join(" "),this.length>50&&(t+=" ... ")),"<Buffer "+t+">"},w.prototype.compare=function(t,e,r,n,o){if(!S(t))throw new TypeError("Argument must be a Buffer");if(void 0===e&&(e=0),void 0===r&&(r=t?t.length:0),void 0===n&&(n=0),void 0===o&&(o=this.length),e<0||r>t.length||n<0||o>this.length)throw new RangeError("out of range index");if(n>=o&&e>=r)return 0;if(n>=o)return-1;if(e>=r)return 1;if(e>>>=0,r>>>=0,n>>>=0,o>>>=0,this===t)return 0;for(var i=o-n,s=r-e,u=Math.min(i,s),h=this.slice(n,o),a=t.slice(e,r),f=0;f<u;++f)if(h[f]!==a[f]){i=h[f],s=a[f];break}return i<s?-1:s<i?1:0},w.prototype.includes=function(t,e,r){return-1!==this.indexOf(t,e,r)},w.prototype.indexOf=function(t,e,r){return C(this,t,e,r,!0)},w.prototype.lastIndexOf=function(t,e,r){return C(this,t,e,r,!1)},w.prototype.write=function(t,e,r,n){if(void 0===e)n="utf8",r=this.length,e=0;else if(void 0===r&&"string"==typeof e)n=e,r=this.length,e=0;else{if(!isFinite(e))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");e|=0,isFinite(r)?(r|=0,void 0===n&&(n="utf8")):(n=r,r=void 0)}var o=this.length-e;if((void 0===r||r>o)&&(r=o),t.length>0&&(r<0||e<0)||e>this.length)throw new RangeError("Attempt to write outside buffer bounds");n||(n="utf8");for(var i=!1;;)switch(n){case"hex":return x(this,t,e,r);case"utf8":case"utf-8":return Y(this,t,e,r);case"ascii":return H(this,t,e,r);case"latin1":case"binary":return L(this,t,e,r);case"base64":return M(this,t,e,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return O(this,t,e,r);default:if(i)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),i=!0}},w.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var It=4096;w.prototype.slice=function(t,e){var r=this.length;t=~~t,e=void 0===e?r:~~e,t<0?(t+=r)<0&&(t=0):t>r&&(t=r),e<0?(e+=r)<0&&(e=0):e>r&&(e=r),e<t&&(e=t);var n;if(w.TYPED_ARRAY_SUPPORT)n=this.subarray(t,e),n.__proto__=w.prototype;else{var o=e-t;n=new w(o,void 0);for(var i=0;i<o;++i)n[i]=this[i+t]}return n},w.prototype.readUIntLE=function(t,e,r){t|=0,e|=0,r||K(t,e,this.length);for(var n=this[t],o=1,i=0;++i<e&&(o*=256);)n+=this[t+i]*o;return n},w.prototype.readUIntBE=function(t,e,r){t|=0,e|=0,r||K(t,e,this.length);for(var n=this[t+--e],o=1;e>0&&(o*=256);)n+=this[t+--e]*o;return n},w.prototype.readUInt8=function(t,e){return e||K(t,1,this.length),this[t]},w.prototype.readUInt16LE=function(t,e){return e||K(t,2,this.length),this[t]|this[t+1]<<8},w.prototype.readUInt16BE=function(t,e){return e||K(t,2,this.length),this[t]<<8|this[t+1]},w.prototype.readUInt32LE=function(t,e){return e||K(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},w.prototype.readUInt32BE=function(t,e){return e||K(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},w.prototype.readIntLE=function(t,e,r){t|=0,e|=0,r||K(t,e,this.length);for(var n=this[t],o=1,i=0;++i<e&&(o*=256);)n+=this[t+i]*o;return o*=128,n>=o&&(n-=Math.pow(2,8*e)),n},w.prototype.readIntBE=function(t,e,r){t|=0,e|=0,r||K(t,e,this.length);for(var n=e,o=1,i=this[t+--n];n>0&&(o*=256);)i+=this[t+--n]*o;return o*=128,i>=o&&(i-=Math.pow(2,8*e)),i},w.prototype.readInt8=function(t,e){return e||K(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},w.prototype.readInt16LE=function(t,e){e||K(t,2,this.length);var r=this[t]|this[t+1]<<8;return 32768&r?4294901760|r:r},w.prototype.readInt16BE=function(t,e){e||K(t,2,this.length);var r=this[t+1]|this[t]<<8;return 32768&r?4294901760|r:r},w.prototype.readInt32LE=function(t,e){return e||K(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},w.prototype.readInt32BE=function(t,e){return e||K(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},w.prototype.readFloatLE=function(t,e){return e||K(t,4,this.length),g(this,t,!0,23,4)},w.prototype.readFloatBE=function(t,e){return e||K(t,4,this.length),g(this,t,!1,23,4)},w.prototype.readDoubleLE=function(t,e){return e||K(t,8,this.length),g(this,t,!0,52,8)},w.prototype.readDoubleBE=function(t,e){return e||K(t,8,this.length),g(this,t,!1,52,8)},w.prototype.writeUIntLE=function(t,e,r,n){if(t=+t,e|=0,r|=0,!n){V(this,t,e,r,Math.pow(2,8*r)-1,0)}var o=1,i=0;for(this[e]=255&t;++i<r&&(o*=256);)this[e+i]=t/o&255;return e+r},w.prototype.writeUIntBE=function(t,e,r,n){if(t=+t,e|=0,r|=0,!n){V(this,t,e,r,Math.pow(2,8*r)-1,0)}var o=r-1,i=1;for(this[e+o]=255&t;--o>=0&&(i*=256);)this[e+o]=t/i&255;return e+r},w.prototype.writeUInt8=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,1,255,0),w.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),this[e]=255&t,e+1},w.prototype.writeUInt16LE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,2,65535,0),w.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):Z(this,t,e,!0),e+2},w.prototype.writeUInt16BE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,2,65535,0),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):Z(this,t,e,!1),e+2},w.prototype.writeUInt32LE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,4,4294967295,0),w.TYPED_ARRAY_SUPPORT?(this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t):W(this,t,e,!0),e+4},w.prototype.writeUInt32BE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,4,4294967295,0),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):W(this,t,e,!1),e+4},w.prototype.writeIntLE=function(t,e,r,n){if(t=+t,e|=0,!n){var o=Math.pow(2,8*r-1);V(this,t,e,r,o-1,-o)}var i=0,s=1,u=0;for(this[e]=255&t;++i<r&&(s*=256);)t<0&&0===u&&0!==this[e+i-1]&&(u=1),this[e+i]=(t/s>>0)-u&255;return e+r},w.prototype.writeIntBE=function(t,e,r,n){if(t=+t,e|=0,!n){var o=Math.pow(2,8*r-1);V(this,t,e,r,o-1,-o)}var i=r-1,s=1,u=0;for(this[e+i]=255&t;--i>=0&&(s*=256);)t<0&&0===u&&0!==this[e+i+1]&&(u=1),this[e+i]=(t/s>>0)-u&255;return e+r},w.prototype.writeInt8=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,1,127,-128),w.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),t<0&&(t=255+t+1),this[e]=255&t,e+1},w.prototype.writeInt16LE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,2,32767,-32768),w.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):Z(this,t,e,!0),e+2},w.prototype.writeInt16BE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,2,32767,-32768),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):Z(this,t,e,!1),e+2},w.prototype.writeInt32LE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,4,2147483647,-2147483648),w.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24):W(this,t,e,!0),e+4},w.prototype.writeInt32BE=function(t,e,r){return t=+t,e|=0,r||V(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):W(this,t,e,!1),e+4},w.prototype.writeFloatLE=function(t,e,r){return Q(this,t,e,!0,r)},w.prototype.writeFloatBE=function(t,e,r){return Q(this,t,e,!1,r)},w.prototype.writeDoubleLE=function(t,e,r){return $(this,t,e,!0,r)},w.prototype.writeDoubleBE=function(t,e,r){return $(this,t,e,!1,r)},w.prototype.copy=function(t,e,r,n){if(r||(r=0),n||0===n||(n=this.length),e>=t.length&&(e=t.length),e||(e=0),n>0&&n<r&&(n=r),n===r)return 0;if(0===t.length||0===this.length)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(r<0||r>=this.length)throw new RangeError("sourceStart out of bounds");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-e<n-r&&(n=t.length-e+r);var o,i=n-r;if(this===t&&r<e&&e<n)for(o=i-1;o>=0;--o)t[o+e]=this[o+r];else if(i<1e3||!w.TYPED_ARRAY_SUPPORT)for(o=0;o<i;++o)t[o+e]=this[o+r];else Uint8Array.prototype.set.call(t,this.subarray(r,r+i),e);return i},w.prototype.fill=function(t,e,r,n){if("string"==typeof t){if("string"==typeof e?(n=e,e=0,r=this.length):"string"==typeof r&&(n=r,r=this.length),1===t.length){var o=t.charCodeAt(0);o<256&&(t=o)}if(void 0!==n&&"string"!=typeof n)throw new TypeError("encoding must be a string");if("string"==typeof n&&!w.isEncoding(n))throw new TypeError("Unknown encoding: "+n)}else"number"==typeof t&&(t&=255);if(e<0||this.length<e||this.length<r)throw new RangeError("Out of range index");if(r<=e)return this;e>>>=0,r=void 0===r?this.length:r>>>0,t||(t=0);var i;if("number"==typeof t)for(i=e;i<r;++i)this[i]=t;else{var s=S(t)?t:nt(new w(t,n).toString()),u=s.length;for(i=0;i<r-e;++i)this[i+e]=s[i%u]}return this};var Ut=/[^+\/0-9A-Za-z-_]/g,Bt=function(){function t(e){lt(this,t),this.ipfs=e}return t.prototype.put=function(t){ return this.ipfs.files.add(new w(t,"utf8")).then(function(t){return t[0].hash})},t.prototype.get=function(t){return this.ipfs.files.cat(t).then(function(t){return t.toString("utf8")})},t.prototype.remove=function(){return Promise.resolve()},t.prototype.clear=function(){return Promise.resolve()},t}(),Ct=function(){function e(){pt(this,e),this.storage={}}return e.prototype.put=function(e){var r=this;return new Promise(function(n){t.randomBytes(32,function(t,o){var i=o.toString("base64");r.storage[i]=e,n(i)})})},e.prototype.get=function(t){var e=this;return new Promise(function(r){r(e.storage[t])})},e.prototype.remove=function(t){var e=this;return new Promise(function(r){delete e.storage[t],r()})},e.prototype.clear=function(){var t=this;return new Promise(function(e){t.storage={},e()})},e}(),Nt=function(){function e(t){gt(this,e),this.gun=t.get("identifi")}return e.prototype.put=function(e,r){var n=this;return new Promise(function(o){t.randomBytes(32,function(t,i){var s=r||i.toString("base64");n.gun.get(s).put(e,function(t){if(console.log("waiting for ack"),t.err)return void console.log("ack error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");console.log("ack success!",s)}),o(s)})})},e.prototype.get=function(t){var e=this;return new Promise(function(r){e.gun.get(t).on(function(t,e,n,o){o.off(),t&&r(t)})})},e.prototype.remove=function(t){var e=this;return new Promise(function(r,n){e.gun.get(t).put(null,function(t){if(t.err)return n(t.err)}),r(t)})},e.prototype.clear=function(){return Promise.resolve()},e}(),xt=!1;try{xt="[object process]"===Object.prototype.toString.call(At.process)}catch(t){}var Yt=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";yt(this,t),this.apiRoot=e}return t.prototype.get=function(t){return t.match(/^\/ip(fs|ns)\//)||(t="/ipfs/"+t),wt(this.apiRoot+t)},t.prototype.remove=function(){return Promise.resolve()},t.prototype.put=function(){return Promise.resolve()},t}();return{MerkleBTree:Et,TreeNode:kt,RAMStorage:Ct,IPFSStorage:Bt,IPFSGatewayStorage:Yt,GUNStorage:Nt}});