@oceanprotocol/lib 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- import{v4 as t}from"uuid";import e from"decimal.js";import i from"web3";import r from"fs";import n from"save-file";import s from"node-abort-controller";import o from"buffer";import a from"crypto";import{fetch as h}from"cross-fetch";import u from"@oceanprotocol/contracts/artifacts/Metadata.json";import{LZMA as l}from"lzma/src/lzma-c";import c from"@oceanprotocol/contracts/artifacts/DTFactory.json";import d from"@oceanprotocol/contracts/artifacts/DataTokenTemplate.json";import{lookup as f}from"@ethereum-navigator/navigator";import{SHA256 as m}from"crypto-js";import p from"@oceanprotocol/contracts/artifacts/BPool.json";import g from"@oceanprotocol/contracts/artifacts/BFactory.json";import w from"@oceanprotocol/contracts/artifacts/FixedRateExchange.json";import v from"@oceanprotocol/contracts/artifacts/Dispenser.json";import{homedir as y}from"os";import M from"@oceanprotocol/contracts/artifacts/address.json";class b{constructor(){this.nodeUri=void 0,this.providerAddress=void 0,this.metadataCacheUri=void 0,this.providerUri=void 0,this.web3Provider=void 0,this.oceanTokenAddress=void 0,this.factoryAddress=void 0,this.factoryABI=void 0,this.datatokensABI=void 0,this.poolFactoryAddress=void 0,this.poolFactoryABI=void 0,this.poolABI=void 0,this.fixedRateExchangeAddress=void 0,this.fixedRateExchangeAddressABI=void 0,this.dispenserAddress=void 0,this.dispenserABI=void 0,this.metadataContractAddress=void 0,this.metadataContractABI=void 0,this.startBlock=void 0,this.verbose=void 0,this.authMessage=void 0,this.authTokenExpiration=void 0,this.parityUri=void 0,this.threshold=void 0}}function A(){return(A=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var r in i)Object.prototype.hasOwnProperty.call(i,r)&&(t[r]=i[r])}return t}).apply(this,arguments)}var x;!function(t){t[t.None=-1]="None",t[t.Error=0]="Error",t[t.Warn=1]="Warn",t[t.Log=2]="Log",t[t.Verbose=3]="Verbose"}(x||(x={}));class E{constructor(t=x.Error){this.logLevel=void 0,this.logLevel=t}setLevel(t){this.logLevel=t}bypass(...t){this.dispatch("log",-Infinity,...t)}debug(...t){this.dispatch("debug",x.Verbose,...t)}log(...t){this.dispatch("log",x.Log,...t)}warn(...t){this.dispatch("warn",x.Warn,...t)}error(...t){this.dispatch("error",x.Error,...t)}dispatch(t,e,...i){this.logLevel>=e&&console[t](...i)}}const R=new E,k=t=>I(t,!0),S=t=>I(t,!1);function I(t="",e){const{valid:i,output:r}=_(t,/^(?:0x)*([a-f0-9]+)$/i,"zeroXTransformer");return(e&&i?"0x":"")+r}const T=t=>O(t,!0);function O(t="",e){const{valid:i,output:r}=_(t,/^(?:0x|did:op:)*([a-f0-9]{40})$/i,"didTransformer");return(e&&i?"did:op:":"")+r}const B=t=>k(O(t,!1)),C=t=>S(O(t,!1));function _(t,e,i){if("string"!=typeof t)throw R.debug("Not input string:"),R.debug(t),new Error(`[${i}] Expected string, input type: ${typeof t}`);const r=t.match(e);return r?{valid:!0,output:r[1]}:(R.warn(`[${i}] Input transformation failed.`),{valid:!1,output:t})}class D{constructor(){this.completed=!1,this.subscriptions=new Set}subscribe(t,e,i){if(this.completed)throw new Error("Observer completed.");const r={onNext:t,onComplete:e,onError:i};return this.subscriptions.add(r),{unsubscribe:()=>this.subscriptions.delete(r)}}next(t){this.emit("onNext",t)}complete(t){this.emit("onComplete",t),this.unsubscribe()}error(t){this.emit("onError",t),this.unsubscribe()}emit(t,e){Array.from(this.subscriptions).map(e=>e[t]).filter(t=>t&&"function"==typeof t).forEach(t=>t(e))}unsubscribe(){this.completed=!0,this.subscriptions.clear()}}class P{constructor(t){this.observer=new D,this.promise=Object.assign(new Promise((t,e)=>{setTimeout(()=>{this.observer.subscribe(void 0,t,e)},0)}),this),setTimeout(()=>this.init(t),1)}subscribe(t){return this.observer.subscribe(t)}next(t){return this.observer.subscribe(t),this}then(t,e){return Object.assign(this.promise.then(t,e),this)}catch(t){return Object.assign(this.promise.catch(t),this)}finally(t){return Object.assign(this.promise.finally(t),this)}init(t){const e=t(this.observer);Promise.resolve(e).then(t=>{"function"==typeof e.then&&this.observer.complete(t)}).catch(t=>{"function"==typeof e.then&&this.observer.error(t)})}}var N=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,W=Math.ceil,L=Math.floor,F="[BigNumber Error] ",U=F+"Number primitive has more than 15 significant digits: ",$=1e14,j=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],q=1e9;function G(t){var e=0|t;return t>0||t===e?e:e-1}function z(t){for(var e,i,r=1,n=t.length,s=t[0]+"";r<n;){for(i=14-(e=t[r++]+"").length;i--;e="0"+e);s+=e}for(n=s.length;48===s.charCodeAt(--n););return s.slice(0,n+1||1)}function H(t,e){var i,r,n=t.c,s=e.c,o=t.s,a=e.s,h=t.e,u=e.e;if(!o||!a)return null;if(r=s&&!s[0],(i=n&&!n[0])||r)return i?r?0:-a:o;if(o!=a)return o;if(i=o<0,r=h==u,!n||!s)return r?0:!n^i?1:-1;if(!r)return h>u^i?1:-1;for(a=(h=n.length)<(u=s.length)?h:u,o=0;o<a;o++)if(n[o]!=s[o])return n[o]>s[o]^i?1:-1;return h==u?0:h>u^i?1:-1}function Z(t,e,i,r){if(t<e||t>i||t!==L(t))throw Error(F+(r||"Argument")+("number"==typeof t?t<e||t>i?" out of range: ":" not an integer: ":" not a primitive number: ")+String(t))}function V(t){var e=t.c.length-1;return G(t.e/14)==e&&t.c[e]%2!=0}function J(t,e){return(t.length>1?t.charAt(0)+"."+t.slice(1):t)+(e<0?"e":"e+")+e}function K(t,e,i){var r,n;if(e<0){for(n=i+".";++e;n+=i);t=n+t}else if(++e>(r=t.length)){for(n=i,e-=r;--e;n+=i);t+=n}else e<r&&(t=t.slice(0,e)+"."+t.slice(e));return t}var X=function t(e){var i,r,n,s,o,a,h,u,l,c,d=R.prototype={constructor:R,toString:null,valueOf:null},f=new R(1),m=20,p=4,g=-7,w=21,v=-1e7,y=1e7,M=!1,b=1,A=0,x={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:" ",suffix:""},E="0123456789abcdefghijklmnopqrstuvwxyz";function R(t,e){var i,s,o,a,h,u,l,c,d=this;if(!(d instanceof R))return new R(t,e);if(null==e){if(t&&!0===t._isBigNumber)return d.s=t.s,void(!t.c||t.e>y?d.c=d.e=null:t.e<v?d.c=[d.e=0]:(d.e=t.e,d.c=t.c.slice()));if((u="number"==typeof t)&&0*t==0){if(d.s=1/t<0?(t=-t,-1):1,t===~~t){for(a=0,h=t;h>=10;h/=10,a++);return void(a>y?d.c=d.e=null:(d.e=a,d.c=[t]))}c=String(t)}else{if(!N.test(c=String(t)))return n(d,c,u);d.s=45==c.charCodeAt(0)?(c=c.slice(1),-1):1}(a=c.indexOf("."))>-1&&(c=c.replace(".","")),(h=c.search(/e/i))>0?(a<0&&(a=h),a+=+c.slice(h+1),c=c.substring(0,h)):a<0&&(a=c.length)}else{if(Z(e,2,E.length,"Base"),10==e)return T(d=new R(t),m+d.e+1,p);if(c=String(t),u="number"==typeof t){if(0*t!=0)return n(d,c,u,e);if(d.s=1/t<0?(c=c.slice(1),-1):1,R.DEBUG&&c.replace(/^0\.0*|\./,"").length>15)throw Error(U+t)}else d.s=45===c.charCodeAt(0)?(c=c.slice(1),-1):1;for(i=E.slice(0,e),a=h=0,l=c.length;h<l;h++)if(i.indexOf(s=c.charAt(h))<0){if("."==s){if(h>a){a=l;continue}}else if(!o&&(c==c.toUpperCase()&&(c=c.toLowerCase())||c==c.toLowerCase()&&(c=c.toUpperCase()))){o=!0,h=-1,a=0;continue}return n(d,String(t),u,e)}u=!1,(a=(c=r(c,e,10,d.s)).indexOf("."))>-1?c=c.replace(".",""):a=c.length}for(h=0;48===c.charCodeAt(h);h++);for(l=c.length;48===c.charCodeAt(--l););if(c=c.slice(h,++l)){if(l-=h,u&&R.DEBUG&&l>15&&(t>9007199254740991||t!==L(t)))throw Error(U+d.s*t);if((a=a-h-1)>y)d.c=d.e=null;else if(a<v)d.c=[d.e=0];else{if(d.e=a,d.c=[],h=(a+1)%14,a<0&&(h+=14),h<l){for(h&&d.c.push(+c.slice(0,h)),l-=14;h<l;)d.c.push(+c.slice(h,h+=14));h=14-(c=c.slice(h)).length}else h-=l;for(;h--;c+="0");d.c.push(+c)}}else d.c=[d.e=0]}function k(t,e,i,r){var n,s,o,a,h;if(null==i?i=p:Z(i,0,8),!t.c)return t.toString();if(n=t.c[0],o=t.e,null==e)h=z(t.c),h=1==r||2==r&&(o<=g||o>=w)?J(h,o):K(h,o,"0");else if(s=(t=T(new R(t),e,i)).e,a=(h=z(t.c)).length,1==r||2==r&&(e<=s||s<=g)){for(;a<e;h+="0",a++);h=J(h,s)}else if(e-=o,h=K(h,s,"0"),s+1>a){if(--e>0)for(h+=".";e--;h+="0");}else if((e+=s-a)>0)for(s+1==a&&(h+=".");e--;h+="0");return t.s<0&&n?"-"+h:h}function S(t,e){for(var i,r=1,n=new R(t[0]);r<t.length;r++){if(!(i=new R(t[r])).s){n=i;break}e.call(n,i)&&(n=i)}return n}function I(t,e,i){for(var r=1,n=e.length;!e[--n];e.pop());for(n=e[0];n>=10;n/=10,r++);return(i=r+14*i-1)>y?t.c=t.e=null:i<v?t.c=[t.e=0]:(t.e=i,t.c=e),t}function T(t,e,i,r){var n,s,o,a,h,u,l,c=t.c,d=j;if(c){t:{for(n=1,a=c[0];a>=10;a/=10,n++);if((s=e-n)<0)s+=14,l=(h=c[u=0])/d[n-(o=e)-1]%10|0;else if((u=W((s+1)/14))>=c.length){if(!r)break t;for(;c.length<=u;c.push(0));h=l=0,n=1,o=(s%=14)-14+1}else{for(h=a=c[u],n=1;a>=10;a/=10,n++);l=(o=(s%=14)-14+n)<0?0:h/d[n-o-1]%10|0}if(r=r||e<0||null!=c[u+1]||(o<0?h:h%d[n-o-1]),r=i<4?(l||r)&&(0==i||i==(t.s<0?3:2)):l>5||5==l&&(4==i||r||6==i&&(s>0?o>0?h/d[n-o]:0:c[u-1])%10&1||i==(t.s<0?8:7)),e<1||!c[0])return c.length=0,r?(c[0]=d[(14-(e-=t.e+1)%14)%14],t.e=-e||0):c[0]=t.e=0,t;if(0==s?(c.length=u,a=1,u--):(c.length=u+1,a=d[14-s],c[u]=o>0?L(h/d[n-o]%d[o])*a:0),r)for(;;){if(0==u){for(s=1,o=c[0];o>=10;o/=10,s++);for(o=c[0]+=a,a=1;o>=10;o/=10,a++);s!=a&&(t.e++,c[0]==$&&(c[0]=1));break}if(c[u]+=a,c[u]!=$)break;c[u--]=0,a=1}for(s=c.length;0===c[--s];c.pop());}t.e>y?t.c=t.e=null:t.e<v&&(t.c=[t.e=0])}return t}function O(t){var e,i=t.e;return null===i?t.toString():(e=z(t.c),e=i<=g||i>=w?J(e,i):K(e,i,"0"),t.s<0?"-"+e:e)}return R.clone=t,R.ROUND_UP=0,R.ROUND_DOWN=1,R.ROUND_CEIL=2,R.ROUND_FLOOR=3,R.ROUND_HALF_UP=4,R.ROUND_HALF_DOWN=5,R.ROUND_HALF_EVEN=6,R.ROUND_HALF_CEIL=7,R.ROUND_HALF_FLOOR=8,R.EUCLID=9,R.config=R.set=function(t){var e,i;if(null!=t){if("object"!=typeof t)throw Error(F+"Object expected: "+t);if(t.hasOwnProperty(e="DECIMAL_PLACES")&&(Z(i=t[e],0,q,e),m=i),t.hasOwnProperty(e="ROUNDING_MODE")&&(Z(i=t[e],0,8,e),p=i),t.hasOwnProperty(e="EXPONENTIAL_AT")&&((i=t[e])&&i.pop?(Z(i[0],-q,0,e),Z(i[1],0,q,e),g=i[0],w=i[1]):(Z(i,-q,q,e),g=-(w=i<0?-i:i))),t.hasOwnProperty(e="RANGE"))if((i=t[e])&&i.pop)Z(i[0],-q,-1,e),Z(i[1],1,q,e),v=i[0],y=i[1];else{if(Z(i,-q,q,e),!i)throw Error(F+e+" cannot be zero: "+i);v=-(y=i<0?-i:i)}if(t.hasOwnProperty(e="CRYPTO")){if((i=t[e])!==!!i)throw Error(F+e+" not true or false: "+i);if(i){if("undefined"==typeof crypto||!crypto||!crypto.getRandomValues&&!crypto.randomBytes)throw M=!i,Error(F+"crypto unavailable");M=i}else M=i}if(t.hasOwnProperty(e="MODULO_MODE")&&(Z(i=t[e],0,9,e),b=i),t.hasOwnProperty(e="POW_PRECISION")&&(Z(i=t[e],0,q,e),A=i),t.hasOwnProperty(e="FORMAT")){if("object"!=typeof(i=t[e]))throw Error(F+e+" not an object: "+i);x=i}if(t.hasOwnProperty(e="ALPHABET")){if("string"!=typeof(i=t[e])||/^.$|[+-.\s]|(.).*\1/.test(i))throw Error(F+e+" invalid: "+i);E=i}}return{DECIMAL_PLACES:m,ROUNDING_MODE:p,EXPONENTIAL_AT:[g,w],RANGE:[v,y],CRYPTO:M,MODULO_MODE:b,POW_PRECISION:A,FORMAT:x,ALPHABET:E}},R.isBigNumber=function(t){if(!t||!0!==t._isBigNumber)return!1;if(!R.DEBUG)return!0;var e,i,r=t.c,n=t.e,s=t.s;t:if("[object Array]"=={}.toString.call(r)){if((1===s||-1===s)&&n>=-q&&n<=q&&n===L(n)){if(0===r[0]){if(0===n&&1===r.length)return!0;break t}if((e=(n+1)%14)<1&&(e+=14),String(r[0]).length==e){for(e=0;e<r.length;e++)if((i=r[e])<0||i>=$||i!==L(i))break t;if(0!==i)return!0}}}else if(null===r&&null===n&&(null===s||1===s||-1===s))return!0;throw Error(F+"Invalid BigNumber: "+t)},R.maximum=R.max=function(){return S(arguments,d.lt)},R.minimum=R.min=function(){return S(arguments,d.gt)},R.random=(s=9007199254740992,o=Math.random()*s&2097151?function(){return L(Math.random()*s)}:function(){return 8388608*(1073741824*Math.random()|0)+(8388608*Math.random()|0)},function(t){var e,i,r,n,s,a=0,h=[],u=new R(f);if(null==t?t=m:Z(t,0,q),n=W(t/14),M)if(crypto.getRandomValues){for(e=crypto.getRandomValues(new Uint32Array(n*=2));a<n;)(s=131072*e[a]+(e[a+1]>>>11))>=9e15?(i=crypto.getRandomValues(new Uint32Array(2)),e[a]=i[0],e[a+1]=i[1]):(h.push(s%1e14),a+=2);a=n/2}else{if(!crypto.randomBytes)throw M=!1,Error(F+"crypto unavailable");for(e=crypto.randomBytes(n*=7);a<n;)(s=281474976710656*(31&e[a])+1099511627776*e[a+1]+4294967296*e[a+2]+16777216*e[a+3]+(e[a+4]<<16)+(e[a+5]<<8)+e[a+6])>=9e15?crypto.randomBytes(7).copy(e,a):(h.push(s%1e14),a+=7);a=n/7}if(!M)for(;a<n;)(s=o())<9e15&&(h[a++]=s%1e14);for(t%=14,(n=h[--a])&&t&&(h[a]=L(n/(s=j[14-t]))*s);0===h[a];h.pop(),a--);if(a<0)h=[r=0];else{for(r=-1;0===h[0];h.splice(0,1),r-=14);for(a=1,s=h[0];s>=10;s/=10,a++);a<14&&(r-=14-a)}return u.e=r,u.c=h,u}),R.sum=function(){for(var t=1,e=arguments,i=new R(e[0]);t<e.length;)i=i.plus(e[t++]);return i},r=function(){var t="0123456789";function e(t,e,i,r){for(var n,s,o=[0],a=0,h=t.length;a<h;){for(s=o.length;s--;o[s]*=e);for(o[0]+=r.indexOf(t.charAt(a++)),n=0;n<o.length;n++)o[n]>i-1&&(null==o[n+1]&&(o[n+1]=0),o[n+1]+=o[n]/i|0,o[n]%=i)}return o.reverse()}return function(r,n,s,o,a){var h,u,l,c,d,f,g,w,v=r.indexOf("."),y=m,M=p;for(v>=0&&(c=A,A=0,r=r.replace(".",""),f=(w=new R(n)).pow(r.length-v),A=c,w.c=e(K(z(f.c),f.e,"0"),10,s,t),w.e=w.c.length),l=c=(g=e(r,n,s,a?(h=E,t):(h=t,E))).length;0==g[--c];g.pop());if(!g[0])return h.charAt(0);if(v<0?--l:(f.c=g,f.e=l,f.s=o,g=(f=i(f,w,y,M,s)).c,d=f.r,l=f.e),v=g[u=l+y+1],c=s/2,d=d||u<0||null!=g[u+1],d=M<4?(null!=v||d)&&(0==M||M==(f.s<0?3:2)):v>c||v==c&&(4==M||d||6==M&&1&g[u-1]||M==(f.s<0?8:7)),u<1||!g[0])r=d?K(h.charAt(1),-y,h.charAt(0)):h.charAt(0);else{if(g.length=u,d)for(--s;++g[--u]>s;)g[u]=0,u||(++l,g=[1].concat(g));for(c=g.length;!g[--c];);for(v=0,r="";v<=c;r+=h.charAt(g[v++]));r=K(r,l,h.charAt(0))}return r}}(),i=function(){function t(t,e,i){var r,n,s,o,a=0,h=t.length,u=e%1e7,l=e/1e7|0;for(t=t.slice();h--;)a=((n=u*(s=t[h]%1e7)+(r=l*s+(o=t[h]/1e7|0)*u)%1e7*1e7+a)/i|0)+(r/1e7|0)+l*o,t[h]=n%i;return a&&(t=[a].concat(t)),t}function e(t,e,i,r){var n,s;if(i!=r)s=i>r?1:-1;else for(n=s=0;n<i;n++)if(t[n]!=e[n]){s=t[n]>e[n]?1:-1;break}return s}function i(t,e,i,r){for(var n=0;i--;)t[i]-=n,t[i]=(n=t[i]<e[i]?1:0)*r+t[i]-e[i];for(;!t[0]&&t.length>1;t.splice(0,1));}return function(r,n,s,o,a){var h,u,l,c,d,f,m,p,g,w,v,y,M,b,A,x,E,k=r.s==n.s?1:-1,S=r.c,I=n.c;if(!(S&&S[0]&&I&&I[0]))return new R(r.s&&n.s&&(S?!I||S[0]!=I[0]:I)?S&&0==S[0]||!I?0*k:k/0:NaN);for(g=(p=new R(k)).c=[],k=s+(u=r.e-n.e)+1,a||(a=$,u=G(r.e/14)-G(n.e/14),k=k/14|0),l=0;I[l]==(S[l]||0);l++);if(I[l]>(S[l]||0)&&u--,k<0)g.push(1),c=!0;else{for(b=S.length,x=I.length,l=0,k+=2,(d=L(a/(I[0]+1)))>1&&(I=t(I,d,a),S=t(S,d,a),x=I.length,b=S.length),M=x,v=(w=S.slice(0,x)).length;v<x;w[v++]=0);E=I.slice(),E=[0].concat(E),A=I[0],I[1]>=a/2&&A++;do{if(d=0,(h=e(I,w,x,v))<0){if(y=w[0],x!=v&&(y=y*a+(w[1]||0)),(d=L(y/A))>1)for(d>=a&&(d=a-1),m=(f=t(I,d,a)).length,v=w.length;1==e(f,w,m,v);)d--,i(f,x<m?E:I,m,a),m=f.length,h=1;else 0==d&&(h=d=1),m=(f=I.slice()).length;if(m<v&&(f=[0].concat(f)),i(w,f,v,a),v=w.length,-1==h)for(;e(I,w,x,v)<1;)d++,i(w,x<v?E:I,v,a),v=w.length}else 0===h&&(d++,w=[0]);g[l++]=d,w[0]?w[v++]=S[M]||0:(w=[S[M]],v=1)}while((M++<b||null!=w[0])&&k--);c=null!=w[0],g[0]||g.splice(0,1)}if(a==$){for(l=1,k=g[0];k>=10;k/=10,l++);T(p,s+(p.e=l+14*u-1)+1,o,c)}else p.e=u,p.r=+c;return p}}(),a=/^(-?)0([xbo])(?=\w[\w.]*$)/i,h=/^([^.]+)\.$/,u=/^\.([^.]+)$/,l=/^-?(Infinity|NaN)$/,c=/^\s*\+(?=[\w.])|^\s+|\s+$/g,n=function(t,e,i,r){var n,s=i?e:e.replace(c,"");if(l.test(s))t.s=isNaN(s)?null:s<0?-1:1;else{if(!i&&(s=s.replace(a,function(t,e,i){return n="x"==(i=i.toLowerCase())?16:"b"==i?2:8,r&&r!=n?t:e}),r&&(n=r,s=s.replace(h,"$1").replace(u,"0.$1")),e!=s))return new R(s,n);if(R.DEBUG)throw Error(F+"Not a"+(r?" base "+r:"")+" number: "+e);t.s=null}t.c=t.e=null},d.absoluteValue=d.abs=function(){var t=new R(this);return t.s<0&&(t.s=1),t},d.comparedTo=function(t,e){return H(this,new R(t,e))},d.decimalPlaces=d.dp=function(t,e){var i,r,n,s=this;if(null!=t)return Z(t,0,q),null==e?e=p:Z(e,0,8),T(new R(s),t+s.e+1,e);if(!(i=s.c))return null;if(r=14*((n=i.length-1)-G(this.e/14)),n=i[n])for(;n%10==0;n/=10,r--);return r<0&&(r=0),r},d.dividedBy=d.div=function(t,e){return i(this,new R(t,e),m,p)},d.dividedToIntegerBy=d.idiv=function(t,e){return i(this,new R(t,e),0,1)},d.exponentiatedBy=d.pow=function(t,e){var i,r,n,s,o,a,h,u,l=this;if((t=new R(t)).c&&!t.isInteger())throw Error(F+"Exponent not an integer: "+O(t));if(null!=e&&(e=new R(e)),o=t.e>14,!l.c||!l.c[0]||1==l.c[0]&&!l.e&&1==l.c.length||!t.c||!t.c[0])return u=new R(Math.pow(+O(l),o?2-V(t):+O(t))),e?u.mod(e):u;if(a=t.s<0,e){if(e.c?!e.c[0]:!e.s)return new R(NaN);(r=!a&&l.isInteger()&&e.isInteger())&&(l=l.mod(e))}else{if(t.e>9&&(l.e>0||l.e<-1||(0==l.e?l.c[0]>1||o&&l.c[1]>=24e7:l.c[0]<8e13||o&&l.c[0]<=9999975e7)))return s=l.s<0&&V(t)?-0:0,l.e>-1&&(s=1/s),new R(a?1/s:s);A&&(s=W(A/14+2))}for(o?(i=new R(.5),a&&(t.s=1),h=V(t)):h=(n=Math.abs(+O(t)))%2,u=new R(f);;){if(h){if(!(u=u.times(l)).c)break;s?u.c.length>s&&(u.c.length=s):r&&(u=u.mod(e))}if(n){if(0===(n=L(n/2)))break;h=n%2}else if(T(t=t.times(i),t.e+1,1),t.e>14)h=V(t);else{if(0==(n=+O(t)))break;h=n%2}l=l.times(l),s?l.c&&l.c.length>s&&(l.c.length=s):r&&(l=l.mod(e))}return r?u:(a&&(u=f.div(u)),e?u.mod(e):s?T(u,A,p,void 0):u)},d.integerValue=function(t){var e=new R(this);return null==t?t=p:Z(t,0,8),T(e,e.e+1,t)},d.isEqualTo=d.eq=function(t,e){return 0===H(this,new R(t,e))},d.isFinite=function(){return!!this.c},d.isGreaterThan=d.gt=function(t,e){return H(this,new R(t,e))>0},d.isGreaterThanOrEqualTo=d.gte=function(t,e){return 1===(e=H(this,new R(t,e)))||0===e},d.isInteger=function(){return!!this.c&&G(this.e/14)>this.c.length-2},d.isLessThan=d.lt=function(t,e){return H(this,new R(t,e))<0},d.isLessThanOrEqualTo=d.lte=function(t,e){return-1===(e=H(this,new R(t,e)))||0===e},d.isNaN=function(){return!this.s},d.isNegative=function(){return this.s<0},d.isPositive=function(){return this.s>0},d.isZero=function(){return!!this.c&&0==this.c[0]},d.minus=function(t,e){var i,r,n,s,o=this,a=o.s;if(e=(t=new R(t,e)).s,!a||!e)return new R(NaN);if(a!=e)return t.s=-e,o.plus(t);var h=o.e/14,u=t.e/14,l=o.c,c=t.c;if(!h||!u){if(!l||!c)return l?(t.s=-e,t):new R(c?o:NaN);if(!l[0]||!c[0])return c[0]?(t.s=-e,t):new R(l[0]?o:3==p?-0:0)}if(h=G(h),u=G(u),l=l.slice(),a=h-u){for((s=a<0)?(a=-a,n=l):(u=h,n=c),n.reverse(),e=a;e--;n.push(0));n.reverse()}else for(r=(s=(a=l.length)<(e=c.length))?a:e,a=e=0;e<r;e++)if(l[e]!=c[e]){s=l[e]<c[e];break}if(s&&(n=l,l=c,c=n,t.s=-t.s),(e=(r=c.length)-(i=l.length))>0)for(;e--;l[i++]=0);for(e=$-1;r>a;){if(l[--r]<c[r]){for(i=r;i&&!l[--i];l[i]=e);--l[i],l[r]+=$}l[r]-=c[r]}for(;0==l[0];l.splice(0,1),--u);return l[0]?I(t,l,u):(t.s=3==p?-1:1,t.c=[t.e=0],t)},d.modulo=d.mod=function(t,e){var r,n,s=this;return t=new R(t,e),!s.c||!t.s||t.c&&!t.c[0]?new R(NaN):!t.c||s.c&&!s.c[0]?new R(s):(9==b?(n=t.s,t.s=1,r=i(s,t,0,3),t.s=n,r.s*=n):r=i(s,t,0,b),(t=s.minus(r.times(t))).c[0]||1!=b||(t.s=s.s),t)},d.multipliedBy=d.times=function(t,e){var i,r,n,s,o,a,h,u,l,c,d,f,m,p,g,w=this,v=w.c,y=(t=new R(t,e)).c;if(!(v&&y&&v[0]&&y[0]))return!w.s||!t.s||v&&!v[0]&&!y||y&&!y[0]&&!v?t.c=t.e=t.s=null:(t.s*=w.s,v&&y?(t.c=[0],t.e=0):t.c=t.e=null),t;for(r=G(w.e/14)+G(t.e/14),t.s*=w.s,(h=v.length)<(c=y.length)&&(m=v,v=y,y=m,n=h,h=c,c=n),n=h+c,m=[];n--;m.push(0));for(p=$,g=1e7,n=c;--n>=0;){for(i=0,d=y[n]%g,f=y[n]/g|0,s=n+(o=h);s>n;)i=((u=d*(u=v[--o]%g)+(a=f*u+(l=v[o]/g|0)*d)%g*g+m[s]+i)/p|0)+(a/g|0)+f*l,m[s--]=u%p;m[s]=i}return i?++r:m.splice(0,1),I(t,m,r)},d.negated=function(){var t=new R(this);return t.s=-t.s||null,t},d.plus=function(t,e){var i,r=this,n=r.s;if(e=(t=new R(t,e)).s,!n||!e)return new R(NaN);if(n!=e)return t.s=-e,r.minus(t);var s=r.e/14,o=t.e/14,a=r.c,h=t.c;if(!s||!o){if(!a||!h)return new R(n/0);if(!a[0]||!h[0])return h[0]?t:new R(a[0]?r:0*n)}if(s=G(s),o=G(o),a=a.slice(),n=s-o){for(n>0?(o=s,i=h):(n=-n,i=a),i.reverse();n--;i.push(0));i.reverse()}for((n=a.length)-(e=h.length)<0&&(i=h,h=a,a=i,e=n),n=0;e;)n=(a[--e]=a[e]+h[e]+n)/$|0,a[e]=$===a[e]?0:a[e]%$;return n&&(a=[n].concat(a),++o),I(t,a,o)},d.precision=d.sd=function(t,e){var i,r,n,s=this;if(null!=t&&t!==!!t)return Z(t,1,q),null==e?e=p:Z(e,0,8),T(new R(s),t,e);if(!(i=s.c))return null;if(r=14*(n=i.length-1)+1,n=i[n]){for(;n%10==0;n/=10,r--);for(n=i[0];n>=10;n/=10,r++);}return t&&s.e+1>r&&(r=s.e+1),r},d.shiftedBy=function(t){return Z(t,-9007199254740991,9007199254740991),this.times("1e"+t)},d.squareRoot=d.sqrt=function(){var t,e,r,n,s,o=this,a=o.c,h=o.s,u=o.e,l=m+4,c=new R("0.5");if(1!==h||!a||!a[0])return new R(!h||h<0&&(!a||a[0])?NaN:a?o:1/0);if(0==(h=Math.sqrt(+O(o)))||h==1/0?(((e=z(a)).length+u)%2==0&&(e+="0"),h=Math.sqrt(+e),u=G((u+1)/2)-(u<0||u%2),r=new R(e=h==1/0?"5e"+u:(e=h.toExponential()).slice(0,e.indexOf("e")+1)+u)):r=new R(h+""),r.c[0])for((h=(u=r.e)+l)<3&&(h=0);;)if(r=c.times((s=r).plus(i(o,s,l,1))),z(s.c).slice(0,h)===(e=z(r.c)).slice(0,h)){if(r.e<u&&--h,"9999"!=(e=e.slice(h-3,h+1))&&(n||"4999"!=e)){+e&&(+e.slice(1)||"5"!=e.charAt(0))||(T(r,r.e+m+2,1),t=!r.times(r).eq(o));break}if(!n&&(T(s,s.e+m+2,0),s.times(s).eq(o))){r=s;break}l+=4,h+=4,n=1}return T(r,r.e+m+1,p,t)},d.toExponential=function(t,e){return null!=t&&(Z(t,0,q),t++),k(this,t,e,1)},d.toFixed=function(t,e){return null!=t&&(Z(t,0,q),t=t+this.e+1),k(this,t,e)},d.toFormat=function(t,e,i){var r,n=this;if(null==i)null!=t&&e&&"object"==typeof e?(i=e,e=null):t&&"object"==typeof t?(i=t,t=e=null):i=x;else if("object"!=typeof i)throw Error(F+"Argument not an object: "+i);if(r=n.toFixed(t,e),n.c){var s,o=r.split("."),a=+i.groupSize,h=+i.secondaryGroupSize,u=i.groupSeparator||"",l=o[0],c=o[1],d=n.s<0,f=d?l.slice(1):l,m=f.length;if(h&&(s=a,a=h,h=s,m-=s),a>0&&m>0){for(l=f.substr(0,s=m%a||a);s<m;s+=a)l+=u+f.substr(s,a);h>0&&(l+=u+f.slice(s)),d&&(l="-"+l)}r=c?l+(i.decimalSeparator||"")+((h=+i.fractionGroupSize)?c.replace(new RegExp("\\d{"+h+"}\\B","g"),"$&"+(i.fractionGroupSeparator||"")):c):l}return(i.prefix||"")+r+(i.suffix||"")},d.toFraction=function(t){var e,r,n,s,o,a,h,u,l,c,d,m,g=this,w=g.c;if(null!=t&&(!(h=new R(t)).isInteger()&&(h.c||1!==h.s)||h.lt(f)))throw Error(F+"Argument "+(h.isInteger()?"out of range: ":"not an integer: ")+O(h));if(!w)return new R(g);for(e=new R(f),l=r=new R(f),n=u=new R(f),m=z(w),o=e.e=m.length-g.e-1,e.c[0]=j[(a=o%14)<0?14+a:a],t=!t||h.comparedTo(e)>0?o>0?e:l:h,a=y,y=1/0,h=new R(m),u.c[0]=0;c=i(h,e,0,1),1!=(s=r.plus(c.times(n))).comparedTo(t);)r=n,n=s,l=u.plus(c.times(s=l)),u=s,e=h.minus(c.times(s=e)),h=s;return s=i(t.minus(r),n,0,1),u=u.plus(s.times(l)),r=r.plus(s.times(n)),u.s=l.s=g.s,d=i(l,n,o*=2,p).minus(g).abs().comparedTo(i(u,r,o,p).minus(g).abs())<1?[l,n]:[u,r],y=a,d},d.toNumber=function(){return+O(this)},d.toPrecision=function(t,e){return null!=t&&Z(t,1,q),k(this,t,e,2)},d.toString=function(t){var e,i=this,n=i.s,s=i.e;return null===s?n?(e="Infinity",n<0&&(e="-"+e)):e="NaN":(null==t?e=s<=g||s>=w?J(z(i.c),s):K(z(i.c),s,"0"):10===t?e=K(z((i=T(new R(i),m+s+1,p)).c),i.e,"0"):(Z(t,2,E.length,"Base"),e=r(K(z(i.c),s,"0"),10,t,n,!0)),n<0&&i.c[0]&&(e="-"+e)),e},d.valueOf=d.toJSON=function(){return O(this)},d._isBigNumber=!0,d[Symbol.toStringTag]="BigNumber",d[Symbol.for("nodejs.util.inspect.custom")]=d.valueOf,null!=e&&R.set(e),R}();async function Q(t){return new X(await t.eth.getGasPrice()).multipliedBy(1.05).integerValue(X.ROUND_DOWN).toString(10)}function Y(t){return void 0!==t.id}async function tt(t,e){if(Y(t))return{did:t.id,ddo:t};{const i=await e.assets.resolve(t);return{did:i.id,ddo:i}}}var et={nouns:["Crab","Fish","Seal","Octopus","Shark","Seahorse","Walrus","Starfish","Whale","Orca","Penguin","Jellyfish","Squid","Lobster","Pelican","Shrimp","Oyster","Clam","Seagull","Dolphin","Shell","Cormorant","Otter","Anemone","Turtle","Coral","Ray","Barracuda","Krill","Anchovy","Angelfish","Barnacle","Clownfish","Cod","Cuttlefish","Eel","Fugu","Herring","Haddock","Ling","Mackerel","Manatee","Narwhal","Nautilus","Plankton","Porpoise","Prawn","Pufferfish","Swordfish","Tuna"],adjectives:["adamant","adroit","amatory","ambitious","amused","animistic","antic","arcadian","artistic","astonishing","astounding","baleful","bellicose","bilious","blissful","boorish","brave","breathtaking","brilliant","calamitous","caustic","cerulean","clever","charming","comely","competent","concomitant","confident","contumacious","corpulent","crapulous","creative","dazzling","dedicated","defamatory","delighted","delightful","determined","didactic","dilatory","dowdy","efficacious","effulgent","egregious","empowered","endemic","enthusiastic","equanimous","exceptional","execrable","fabulous","fantastic","fastidious","feckless","fecund","friable","fulsome","garrulous","generous","gentle","guileless","gustatory","heuristic","histrionic","hubristic","incendiary","incredible","insidious","insolent","inspired","intransigent","inveterate","invidious","invigorated","irksome","jejune","juicy","jocular","joyful","judicious","kind","lachrymose","limpid","loquacious","lovely","luminous","mannered","marvelous","mendacious","meretricious","minatory","mordant","motivated","munificent","nefarious","noxious","obtuse","optimistic","parsimonious","pendulous","pernicious","pervasive","petulant","passionate","phenomenal","platitudinous","pleasant","powerful","precipitate","propitious","puckish","querulous","quiescent","rebarbative","recalcitant","redolent","rhadamanthine","risible","ruminative","sagacious","salubrious","sartorial","sclerotic","serpentine","smart","spasmodic","strident","stunning","stupendous","taciturn","tactful","tasty","tenacious","tremendous","tremulous","trenchant","turbulent","turgid","ubiquitous","uxorious","verdant","vibrant","voluble","voracious","wheedling","withering","wonderful","zealous"]};function it(t){const e=t||et,i=Math.floor(Math.random()*e.adjectives.length),r=Math.floor(Math.random()*e.nouns.length),n=Math.floor(100*Math.random()),s=e.adjectives[i].replace(/^\w/,t=>t.toUpperCase()),o=e.nouns[r].replace(/^\w/,t=>t.toUpperCase());return{name:`${s} ${o} Token`,symbol:`${(s.substring(0,3)+o.substring(0,3)).toUpperCase()}-${n}`}}var rt={__proto__:null,objectPromiseAll:async t=>((t=[],e=[])=>t.reduce((t,i,r)=>A({},t,{[i]:e[r]}),{}))(Object.keys(t),await Promise.all(Object.values(t))),get LogLevel(){return x},Logger:E,LoggerInstance:R,zeroX:k,noZeroX:S,zeroXTransformer:I,didPrefixed:T,noDidPrefixed:t=>O(t,!1),didTransformer:O,didZeroX:B,didNoZeroX:C,generateId:function(e=64){let i="";for(;i.length<e;)i+=t().replace(/-/g,"");return i.substr(0,e)},SubscribablePromise:P,SubscribableObserver:D,getFairGasPrice:Q,isDdo:Y,assetResolve:tt,generateDatatokenName:it};class nt{constructor(){this._ocean=void 0,this._web3=void 0,this._config=void 0,this._logger=void 0}get ocean(){return this._ocean||R.error("Ocean instance is not defined."),this._ocean}get web3(){return this._web3||R.error("Web3 instance is not defined."),this._web3}get config(){return this._config||R.error("Config instance is not defined."),this._config}get logger(){return R}get instanceConfig(){const{ocean:t,web3:e,config:i,logger:r}=this;return{ocean:t,web3:e,config:i,logger:r}}static async getInstance(t){R.warn("getInstance() methods has needs to be added to child class.")}static setInstanceConfig(t,{ocean:e,config:i,web3:r,logger:n}){t._ocean=e,t._config=i,t._web3=r,t._logger=n}setInstanceConfig(t){nt.setInstanceConfig(this,t)}}class st extends nt{constructor(t="0x0",e){super(),this.id=void 0,this.password=void 0,this.token=void 0,this.id=t,e&&this.setInstanceConfig(e)}getId(){return this.id}setId(t){this.id=t}setPassword(t){this.password=t}getPassword(){return this.password}async getTokenBalance(t){if(null===t)return null;const i=[{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"}];let r=null;const n=await this.getTokenDecimals(t);try{const s=new this.web3.eth.Contract(i,t,{from:this.id}),o=await s.methods.balanceOf(this.id).call();r=new e(o).div(10**n).toString()}catch(t){this.logger.error(`ERROR: Failed to get the balance: ${t.message}`)}return r}async getTokenDecimals(t){let e=18;if(null===t)return e;const i=[{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],type:"function"}];try{const r=new this.web3.eth.Contract(i,t,{from:this.id});e=await r.methods.decimals().call()}catch(t){this.logger.error(`ERROR: Failed to get decimals : ${t.message}`)}return e}async getOceanBalance(){return this.getTokenBalance(this.config.oceanTokenAddress)}async getTokenSymbol(t){return""}async getEtherBalance(){const t=await this.web3.eth.getBalance(this.id,"latest");return this.web3.utils.fromWei(t)}}class ot{static parse(t){let e;t instanceof ot&&(t=t.getDid());const i=t.match(/^did:op:([a-f0-9]{40})$/i);if(i&&(e=new ot(i[1])),!e)throw new Error(`Parsing DID failed, ${t}`);return e}static generate(t){return new ot(S(t))}constructor(t){this.id=void 0,this.id=t}getDid(){return`did:op:${this.id}`}getId(){return this.id}}class at extends nt{static async getInstance(t){const e=new at;return e.setInstanceConfig(t),e}async list(){const t=(await this.web3.eth.getAccounts()).map(t=>new st(t,this.instanceConfig));return Promise.all(t)}getTokenBalance(t,e){return e.getTokenBalance(t)}getOceanBalance(t){return t.getOceanBalance()}getEtherBalance(t){return t.getEtherBalance()}}class ht{static getWeb3(t={}){return new i(t.web3Provider||i.givenProvider||new i.providers.HttpProvider(t.nodeUri))}}class ut{static serialize(t){return JSON.stringify(t,null,2)}static deserialize(t){const e=JSON.parse(t);return new ut(e)}constructor(t={}){this["@context"]="https://w3id.org/did/v1",this.id=null,this.created=void 0,this.updated=void 0,this.dataToken=void 0,this.publicKey=[],this.authentication=[],this.service=[],this.proof=void 0,this.price=void 0,this.isInPurgatory=void 0,this.purgatoryData=void 0,this.dataTokenInfo=void 0,this.credentials=void 0,this.chainId=void 0,this.event=void 0,Object.assign(this,t,{created:t&&t.created||(new Date).toISOString().replace(/\.[0-9]{3}/,"")})}shortId(){return this.id.replace("did:op:","")}findServiceById(t){if(isNaN(t))throw new Error("index is not set");return this.service.find(e=>e.index===t)}findServiceByType(t){if(!t)throw new Error("serviceType not set");return this.service.find(e=>e.type===t)}getChecksum(){const{attributes:t}=this.findServiceByType("metadata"),{files:e,name:i,author:r,license:n}=t.main,s=[...(e||[]).map(({checksum:t})=>t).filter(t=>!!t),i,r,n,this.id];return ht.getWeb3().utils.sha3(s.join("")).replace(/^0x([a-f0-9]{64})(:!.+)?$/i,"0x$1")}async addProof(t,e,i){if(this.proof)throw new Error("Proof already exists");this.proof={created:(new Date).toISOString().replace(/\.[0-9]{3}/,""),creator:e,type:"AddressHash",signatureValue:ht.getWeb3().utils.sha3(e).replace(/^0x([a-f0-9]{64})(:!.+)?$/i,"0x$1")}}}function lt(t){if(!Number.isInteger(t))throw new TypeError("Expected an integer, got "+typeof t);const e=new WeakMap,i=new s,r=setTimeout(()=>{i.abort()},t);return e.set(i.signal,r),i.signal}const ct=require("cross-fetch");class dt{constructor(t){this.logger=void 0,this.logger=t}post(t,e){return this.postWithHeaders(t,e,{"Content-type":"application/json"})}postWithOctet(t,e){return this.postWithHeaders(t,e,{"Content-type":"application/octet-stream"})}postWithHeaders(t,e,i){return this.fetch(t,null!=e?{method:"POST",body:e,headers:i,signal:lt(5e3)}:{method:"POST",signal:lt(5e3)})}get(t){return this.fetch(t,{method:"GET",headers:{"Content-type":"application/json"},signal:lt(5e3)})}put(t,e){return this.fetch(t,null!=e?{method:"PUT",body:e,headers:{"Content-type":"application/json"},signal:lt(5e3)}:{method:"PUT",headers:{"Content-type":"application/json"},signal:lt(5e3)})}delete(t,e){return this.fetch(t,null!=e?{method:"DELETE",body:e,headers:{"Content-type":"application/json"},signal:lt(5e3)}:{method:"DELETE",headers:{"Content-type":"application/json"},signal:lt(5e3)})}async downloadFile(t,e,i){const s=await this.get(t);if(!s.ok)throw new Error("Response error.");let o;try{o=s.headers.get("content-disposition").match(/attachment;filename=(.+)/)[1]}catch(e){try{o=t.split("/").pop()}catch(t){o=`file${i}`}}if(e)return await new Promise(async(t,i)=>{r.mkdirSync(e,{recursive:!0});const n=r.createWriteStream(`${e}${o}`);s.body.pipe(n),s.body.on("error",i),n.on("finish",t)}),e;n(await s.arrayBuffer(),o)}async downloadFileBrowser(t){const e=document.createElement("a");e.download="",e.href=t,e.click()}async fetch(t,e){const i=await ct(t,e);if(!i.ok)throw this.logger.error(`Error requesting [${e.method}] ${t}`),this.logger.error(`Response message: \n${await i.text()}`),i;return i}}class ft extends nt{constructor(...t){super(...t),this.nonce=void 0,this.baseUrl=void 0,this.servicesEndpoints=void 0,this.computeAddress=void 0,this.providerAddress=void 0,this.providerVersion=void 0}static async getInstance(t){const e=new ft;return e.setInstanceConfig(t),e.nonce="0",await e.setBaseUrl(t.config.providerUri),e}async setBaseUrl(t){return this.baseUrl=t,this.servicesEndpoints=await this.getServiceEndpoints(),!0}get url(){return this.baseUrl}async getServiceEndpoints(){const t=[];try{const e=await(await this.ocean.utils.fetch.get(this.url)).json();this.providerAddress=e.providerAddress,"computeAddress"in e&&(this.computeAddress=e.computeAddress),"version"in e&&(this.providerVersion=e.version);for(const i in e.serviceEndpoints)t.push({serviceName:i,method:e.serviceEndpoints[i][0],urlPath:this.url+e.serviceEndpoints[i][1]});return t}catch(t){return this.logger.error("Finding the service endpoints failed:",t),null}}getEndpointURL(t){return this.servicesEndpoints?this.servicesEndpoints.find(e=>e.serviceName===t):null}async createSignature(t,e){return await this.ocean.utils.signature.signText(S(e),t.getId())}async createHashSignature(t,e){return await this.ocean.utils.signature.signWithHash(e,t.getId())}async encrypt(t,e,i){await this.getNonce(i.getId());const r={documentId:t,document:JSON.stringify(e),publisherAddress:i.getId()},n=this.getEncryptEndpoint()?this.getEncryptEndpoint().urlPath:null;if(!n)return null;try{const t=await this.ocean.utils.fetch.post(n,decodeURI(JSON.stringify(r)));return(await t.json()).encryptedDocument}catch(t){throw this.logger.error(t),new Error("HTTP request failed")}}async fileinfo(t){const e={url:t},i=[],r=this.getFileinfoEndpoint()?this.getFileinfoEndpoint().urlPath:null;if(!r)return null;try{const t=await this.ocean.utils.fetch.post(r,JSON.stringify(e)),n=await t.json();for(const t of n)i.push(t);return i}catch(t){return null}}async isFileConsumable(t,e){const i={did:t.getDid()},r=await this.ocean.metadataCache.retrieveDDO(t);if(!r)return!1;const n=r.findServiceById(e);if(!n)return!1;const s=n.serviceEndpoint+"/api/v1/services/fileinfo";try{const t=await this.ocean.utils.fetch.post(s,JSON.stringify(i));return(await t.json())[0].valid}catch(t){return!1}}async getNonce(t){const e=this.getNonceEndpoint()?this.getNonceEndpoint().urlPath:null;if(!e)return null;try{const i=await this.ocean.utils.fetch.get(e+`?userAddress=${t}`);return this.nonce=String((await i.json()).nonce),this.nonce}catch(t){throw this.logger.error(t),new Error("HTTP request failed")}}async initialize(t,e,i,r,n){const{did:s,ddo:o}=await tt(t,this.ocean);let a=this.getInitializeEndpoint()?this.getInitializeEndpoint().urlPath:null;if(!a)return null;a+=`?documentId=${s}`,a+=`&serviceId=${e}`,a+=`&serviceType=${i}`,a+=`&dataToken=${o.dataToken}`,a+=`&consumerAddress=${r}`,n&&(a+="&userdata="+encodeURI(JSON.stringify(n)));try{const t=await this.ocean.utils.fetch.get(a);return await t.text()}catch(t){throw this.logger.error(t),new Error("Asset URL not found or not available.")}}async download(t,e,i,r,n,s,o,a,h=-1,u){await this.getNonce(o.getId());const l=await this.createSignature(o,t+this.nonce),c=this.getDownloadEndpoint()?this.getDownloadEndpoint().urlPath:null;if(!c)return null;const d=a.filter((t,e)=>-1===h||e===h).map(async({index:a})=>{let h=c;h+=`?fileIndex=${a}`,h+=`&documentId=${t}`,h+=`&serviceId=${n}`,h+=`&serviceType=${r}`,h+=`&dataToken=${i}`,h+=`&transferTxId=${e}`,h+=`&consumerAddress=${o.getId()}`,h+=`&signature=${l}`,u&&(h+="&userdata="+encodeURI(JSON.stringify(u)));try{s?await this.ocean.utils.fetch.downloadFile(h,s,a):await this.ocean.utils.fetch.downloadFileBrowser(h)}catch(t){throw this.logger.error("Error consuming assets"),this.logger.error(t),t}});return await Promise.all(d),s}async computeStart(t,e,i,r,n,s,o,a,h,u){const l=e.getId();await this.getNonce(e.getId());const c=Object();c.documentId=S(t);let d=l;d+=t&&`${S(t)}`||"",d+=this.nonce;const f=await this.createHashSignature(e,d);c.signature=f,r&&(c.output=r),i.did&&(c.algorithmDid=i.did),i.meta&&(c.algorithmMeta=i.meta),c.consumerAddress=l,n&&(c.transferTxId=n),i.transferTxId&&(c.algorithmTransferTxId=i.transferTxId),i.dataToken&&(c.algorithmDataToken=i.dataToken),s&&(c.serviceId=s),o&&(c.serviceType=o),a&&(c.dataToken=a),h&&(c.additionalInputs=h),u&&(c.userData=u),i.algoCustomParameters&&(c.algouserdata=i.algoCustomParameters);const m=this.getComputeStartEndpoint()?this.getComputeStartEndpoint().urlPath:null;if(!m)return null;try{const t=await this.ocean.utils.fetch.post(m,JSON.stringify(c));return null!=t&&t.ok?await t.json():(console.error("Compute start failed:",t.status,t.statusText),this.logger.error("Payload was:",c),null)}catch(t){return this.logger.error("Compute start failed:"),this.logger.error(t),this.logger.error("Payload was:",c),null}}async computeStop(t,e,i){const r=e.getId();await this.getNonce(e.getId());const n=Object();n.documentId=S(t);let s=r;s+=i||"",s+=t&&`${S(t)}`||"",s+=this.nonce;const o=await this.createHashSignature(e,s);n.signature=o,n.jobId=i,n.consumerAddress=r;const a=this.getComputeStopEndpoint()?this.getComputeStopEndpoint().urlPath:null;if(!a)return null;try{const t=await this.ocean.utils.fetch.put(a,JSON.stringify(n));return null!=t&&t.ok?await t.json():(this.logger.error("Compute stop failed:",t.status,t.statusText),this.logger.error("Payload was:",n),null)}catch(t){return this.logger.error("Compute stop failed:"),this.logger.error(t),this.logger.error("Payload was:",n),null}}async computeDelete(t,e,i){const r=e.getId();await this.getNonce(e.getId());const n=Object();n.documentId=S(t);let s=r;s+=i||"",s+=t&&`${S(t)}`||"",s+=this.nonce;const o=await this.createHashSignature(e,s);n.signature=o,n.jobId=i,n.consumerAddress=r;const a=this.getComputeDeleteEndpoint()?this.getComputeDeleteEndpoint().urlPath:null;if(!a)return null;try{const t=await this.ocean.utils.fetch.delete(a,JSON.stringify(n));return null!=t&&t.ok?await t.json():(this.logger.error("Delete compute job failed:",t.status,t.statusText),this.logger.error("Payload was:",n),null)}catch(t){return this.logger.error("Delete compute job failed:"),this.logger.error(t),this.logger.error("Payload was:",n),null}}async computeStatus(t,e,i,r,n=!0){const s=e.getId();await this.getNonce(e.getId());let o="?documentId="+S(t);if(n){let r=s;r+=i||"",r+=t&&`${S(t)}`||"",r+=this.nonce,o+=`&signature=${await this.createHashSignature(e,r)}`}o+=i&&`&jobId=${i}`||"",o+=`&consumerAddress=${s}`,o+=r&&`&transferTxId=${r}`||"";const a=this.getComputeStatusEndpoint()?this.getComputeStatusEndpoint().urlPath:null;if(!a)return null;try{const t=await this.ocean.utils.fetch.get(a+o);return null!=t&&t.ok?await t.json():(this.logger.error("Get compute status failed:",t.status,t.statusText),null)}catch(t){return this.logger.error("Get compute status failed"),this.logger.error(t),null}}getInitializeEndpoint(){return this.getEndpointURL("initialize")}getNonceEndpoint(){return this.getEndpointURL("nonce")}getEncryptEndpoint(){return this.getEndpointURL("encrypt")}getFileinfoEndpoint(){return this.getEndpointURL("fileinfo")}getComputeStartEndpoint(){return this.getEndpointURL("computeStart")}getComputeStopEndpoint(){return this.getEndpointURL("computeStop")}getComputeStatusEndpoint(){return this.getEndpointURL("computeStatus")}getComputeDeleteEndpoint(){return this.getEndpointURL("computeDelete")}getDownloadEndpoint(){return this.getEndpointURL("download")}async isValidProvider(t){try{const e=await this.ocean.utils.fetch.get(t);if(null!=e&&e.ok){const t=await e.json();if(t&&t.providerAddress)return!0}return!1}catch(t){return this.logger.error(`Error validating provider: ${t.message}`),!1}}}var mt="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function pt(t){var e={exports:{}};return t(e,e.exports),e.exports}function gt(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.')}var wt=pt(function(t){!function(t,e){function i(t,e){if(!t)throw new Error(e||"Assertion failed")}function r(t,e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}function n(t,e,i){if(n.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(i=e,e=10),this._init(t||0,e||10,i||"be"))}var s;"object"==typeof t?t.exports=n:e.BN=n,n.BN=n,n.wordSize=26;try{s=gt("buffer").Buffer}catch(t){}function o(t,e,i){for(var r=0,n=Math.min(t.length,i),s=e;s<n;s++){var o=t.charCodeAt(s)-48;r<<=4,r|=o>=49&&o<=54?o-49+10:o>=17&&o<=22?o-17+10:15&o}return r}function a(t,e,i,r){for(var n=0,s=Math.min(t.length,i),o=e;o<s;o++){var a=t.charCodeAt(o)-48;n*=r,n+=a>=49?a-49+10:a>=17?a-17+10:a}return n}n.isBN=function(t){return t instanceof n||null!==t&&"object"==typeof t&&t.constructor.wordSize===n.wordSize&&Array.isArray(t.words)},n.max=function(t,e){return t.cmp(e)>0?t:e},n.min=function(t,e){return t.cmp(e)<0?t:e},n.prototype._init=function(t,e,r){if("number"==typeof t)return this._initNumber(t,e,r);if("object"==typeof t)return this._initArray(t,e,r);"hex"===e&&(e=16),i(e===(0|e)&&e>=2&&e<=36);var n=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&n++,16===e?this._parseHex(t,n):this._parseBase(t,e,n),"-"===t[0]&&(this.negative=1),this.strip(),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(i(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initArray=function(t,e,r){if(i("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,o,a=0;if("be"===r)for(n=t.length-1,s=0;n>=0;n-=3)this.words[s]|=(o=t[n]|t[n-1]<<8|t[n-2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);else if("le"===r)for(n=0,s=0;n<t.length;n+=3)this.words[s]|=(o=t[n]|t[n+1]<<8|t[n+2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);return this.strip()},n.prototype._parseHex=function(t,e){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var r,n,s=0;for(i=t.length-6,r=0;i>=e;i-=6)n=o(t,i,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303,(s+=24)>=26&&(s-=26,r++);i+6!==e&&(n=o(t,e,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303),this.strip()},n.prototype._parseBase=function(t,e,i){this.words=[0],this.length=1;for(var r=0,n=1;n<=67108863;n*=e)r++;r--,n=n/e|0;for(var s=t.length-i,o=s%r,h=Math.min(s,s-o)+i,u=0,l=i;l<h;l+=r)u=a(t,l,l+r,e),this.imuln(n),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(0!==o){var c=1;for(u=a(t,l,t.length,e),l=0;l<o;l++)c*=e;this.imuln(c),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}},n.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},n.prototype.clone=function(){var t=new n(null);return this.copy(t),t},n.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},n.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},n.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},n.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var h=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],u=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],l=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function c(t,e,i){i.negative=e.negative^t.negative;var r=t.length+e.length|0;i.length=r,r=r-1|0;var n=0|t.words[0],s=0|e.words[0],o=n*s,a=o/67108864|0;i.words[0]=67108863&o;for(var h=1;h<r;h++){for(var u=a>>>26,l=67108863&a,c=Math.min(h,e.length-1),d=Math.max(0,h-t.length+1);d<=c;d++)u+=(o=(n=0|t.words[h-d|0])*(s=0|e.words[d])+l)/67108864|0,l=67108863&o;i.words[h]=0|l,a=0|u}return 0!==a?i.words[h]=0|a:i.length--,i.strip()}n.prototype.toString=function(t,e){var r;if(e=0|e||1,16===(t=t||10)||"hex"===t){r="";for(var n=0,s=0,o=0;o<this.length;o++){var a=this.words[o],c=(16777215&(a<<n|s)).toString(16);r=0!=(s=a>>>24-n&16777215)||o!==this.length-1?h[6-c.length]+c+r:c+r,(n+=2)>=26&&(n-=26,o--)}for(0!==s&&(r=s.toString(16)+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}if(t===(0|t)&&t>=2&&t<=36){var d=u[t],f=l[t];r="";var m=this.clone();for(m.negative=0;!m.isZero();){var p=m.modn(f).toString(t);r=(m=m.idivn(f)).isZero()?p+r:h[d-p.length]+p+r}for(this.isZero()&&(r="0"+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}i(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&i(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},n.prototype.toJSON=function(){return this.toString(16)},n.prototype.toBuffer=function(t,e){return i(void 0!==s),this.toArrayLike(s,t,e)},n.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},n.prototype.toArrayLike=function(t,e,r){var n=this.byteLength(),s=r||Math.max(1,n);i(n<=s,"byte array longer than desired length"),i(s>0,"Requested array length <= 0"),this.strip();var o,a,h="le"===e,u=new t(s),l=this.clone();if(h){for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[a]=o;for(;a<s;a++)u[a]=0}else{for(a=0;a<s-n;a++)u[a]=0;for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[s-a-1]=o}return u},n.prototype._countBits=Math.clz32?function(t){return 32-Math.clz32(t)}:function(t){var e=t,i=0;return e>=4096&&(i+=13,e>>>=13),e>=64&&(i+=7,e>>>=7),e>=8&&(i+=4,e>>>=4),e>=2&&(i+=2,e>>>=2),i+e},n.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,i=0;return 0==(8191&e)&&(i+=13,e>>>=13),0==(127&e)&&(i+=7,e>>>=7),0==(15&e)&&(i+=4,e>>>=4),0==(3&e)&&(i+=2,e>>>=2),0==(1&e)&&i++,i},n.prototype.bitLength=function(){var t=this._countBits(this.words[this.length-1]);return 26*(this.length-1)+t},n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var i=this._zeroBits(this.words[e]);if(t+=i,26!==i)break}return t},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},n.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return 0!==this.negative},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this.strip()},n.prototype.ior=function(t){return i(0==(this.negative|t.negative)),this.iuor(t)},n.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},n.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},n.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var i=0;i<e.length;i++)this.words[i]=this.words[i]&t.words[i];return this.length=e.length,this.strip()},n.prototype.iand=function(t){return i(0==(this.negative|t.negative)),this.iuand(t)},n.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},n.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},n.prototype.iuxor=function(t){var e,i;this.length>t.length?(e=this,i=t):(e=t,i=this);for(var r=0;r<i.length;r++)this.words[r]=e.words[r]^i.words[r];if(this!==e)for(;r<e.length;r++)this.words[r]=e.words[r];return this.length=e.length,this.strip()},n.prototype.ixor=function(t){return i(0==(this.negative|t.negative)),this.iuxor(t)},n.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},n.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},n.prototype.inotn=function(t){i("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=67108863&~this.words[n];return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this.strip()},n.prototype.notn=function(t){return this.clone().inotn(t)},n.prototype.setn=function(t,e){i("number"==typeof t&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),this.words[r]=e?this.words[r]|1<<n:this.words[r]&~(1<<n),this.strip()},n.prototype.iadd=function(t){var e,i,r;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(i=this,r=t):(i=t,r=this);for(var n=0,s=0;s<r.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+(0|r.words[s])+n),n=e>>>26;for(;0!==n&&s<i.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+n),n=e>>>26;if(this.length=i.length,0!==n)this.words[this.length]=n,this.length++;else if(i!==this)for(;s<i.length;s++)this.words[s]=i.words[s];return this},n.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},n.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var i,r,n=this.cmp(t);if(0===n)return this.negative=0,this.length=1,this.words[0]=0,this;n>0?(i=this,r=t):(i=t,r=this);for(var s=0,o=0;o<r.length;o++)s=(e=(0|i.words[o])-(0|r.words[o])+s)>>26,this.words[o]=67108863&e;for(;0!==s&&o<i.length;o++)s=(e=(0|i.words[o])+s)>>26,this.words[o]=67108863&e;if(0===s&&o<i.length&&i!==this)for(;o<i.length;o++)this.words[o]=i.words[o];return this.length=Math.max(this.length,o),i!==this&&(this.negative=1),this.strip()},n.prototype.sub=function(t){return this.clone().isub(t)};var d=function(t,e,i){var r,n,s,o=t.words,a=e.words,h=i.words,u=0,l=0|o[0],c=8191&l,d=l>>>13,f=0|o[1],m=8191&f,p=f>>>13,g=0|o[2],w=8191&g,v=g>>>13,y=0|o[3],M=8191&y,b=y>>>13,A=0|o[4],x=8191&A,E=A>>>13,R=0|o[5],k=8191&R,S=R>>>13,I=0|o[6],T=8191&I,O=I>>>13,B=0|o[7],C=8191&B,_=B>>>13,D=0|o[8],P=8191&D,N=D>>>13,W=0|o[9],L=8191&W,F=W>>>13,U=0|a[0],$=8191&U,j=U>>>13,q=0|a[1],G=8191&q,z=q>>>13,H=0|a[2],Z=8191&H,V=H>>>13,J=0|a[3],K=8191&J,X=J>>>13,Q=0|a[4],Y=8191&Q,tt=Q>>>13,et=0|a[5],it=8191&et,rt=et>>>13,nt=0|a[6],st=8191&nt,ot=nt>>>13,at=0|a[7],ht=8191&at,ut=at>>>13,lt=0|a[8],ct=8191&lt,dt=lt>>>13,ft=0|a[9],mt=8191&ft,pt=ft>>>13;i.negative=t.negative^e.negative,i.length=19;var gt=(u+(r=Math.imul(c,$))|0)+((8191&(n=(n=Math.imul(c,j))+Math.imul(d,$)|0))<<13)|0;u=((s=Math.imul(d,j))+(n>>>13)|0)+(gt>>>26)|0,gt&=67108863,r=Math.imul(m,$),n=(n=Math.imul(m,j))+Math.imul(p,$)|0,s=Math.imul(p,j);var wt=(u+(r=r+Math.imul(c,G)|0)|0)+((8191&(n=(n=n+Math.imul(c,z)|0)+Math.imul(d,G)|0))<<13)|0;u=((s=s+Math.imul(d,z)|0)+(n>>>13)|0)+(wt>>>26)|0,wt&=67108863,r=Math.imul(w,$),n=(n=Math.imul(w,j))+Math.imul(v,$)|0,s=Math.imul(v,j),r=r+Math.imul(m,G)|0,n=(n=n+Math.imul(m,z)|0)+Math.imul(p,G)|0,s=s+Math.imul(p,z)|0;var vt=(u+(r=r+Math.imul(c,Z)|0)|0)+((8191&(n=(n=n+Math.imul(c,V)|0)+Math.imul(d,Z)|0))<<13)|0;u=((s=s+Math.imul(d,V)|0)+(n>>>13)|0)+(vt>>>26)|0,vt&=67108863,r=Math.imul(M,$),n=(n=Math.imul(M,j))+Math.imul(b,$)|0,s=Math.imul(b,j),r=r+Math.imul(w,G)|0,n=(n=n+Math.imul(w,z)|0)+Math.imul(v,G)|0,s=s+Math.imul(v,z)|0,r=r+Math.imul(m,Z)|0,n=(n=n+Math.imul(m,V)|0)+Math.imul(p,Z)|0,s=s+Math.imul(p,V)|0;var yt=(u+(r=r+Math.imul(c,K)|0)|0)+((8191&(n=(n=n+Math.imul(c,X)|0)+Math.imul(d,K)|0))<<13)|0;u=((s=s+Math.imul(d,X)|0)+(n>>>13)|0)+(yt>>>26)|0,yt&=67108863,r=Math.imul(x,$),n=(n=Math.imul(x,j))+Math.imul(E,$)|0,s=Math.imul(E,j),r=r+Math.imul(M,G)|0,n=(n=n+Math.imul(M,z)|0)+Math.imul(b,G)|0,s=s+Math.imul(b,z)|0,r=r+Math.imul(w,Z)|0,n=(n=n+Math.imul(w,V)|0)+Math.imul(v,Z)|0,s=s+Math.imul(v,V)|0,r=r+Math.imul(m,K)|0,n=(n=n+Math.imul(m,X)|0)+Math.imul(p,K)|0,s=s+Math.imul(p,X)|0;var Mt=(u+(r=r+Math.imul(c,Y)|0)|0)+((8191&(n=(n=n+Math.imul(c,tt)|0)+Math.imul(d,Y)|0))<<13)|0;u=((s=s+Math.imul(d,tt)|0)+(n>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,r=Math.imul(k,$),n=(n=Math.imul(k,j))+Math.imul(S,$)|0,s=Math.imul(S,j),r=r+Math.imul(x,G)|0,n=(n=n+Math.imul(x,z)|0)+Math.imul(E,G)|0,s=s+Math.imul(E,z)|0,r=r+Math.imul(M,Z)|0,n=(n=n+Math.imul(M,V)|0)+Math.imul(b,Z)|0,s=s+Math.imul(b,V)|0,r=r+Math.imul(w,K)|0,n=(n=n+Math.imul(w,X)|0)+Math.imul(v,K)|0,s=s+Math.imul(v,X)|0,r=r+Math.imul(m,Y)|0,n=(n=n+Math.imul(m,tt)|0)+Math.imul(p,Y)|0,s=s+Math.imul(p,tt)|0;var bt=(u+(r=r+Math.imul(c,it)|0)|0)+((8191&(n=(n=n+Math.imul(c,rt)|0)+Math.imul(d,it)|0))<<13)|0;u=((s=s+Math.imul(d,rt)|0)+(n>>>13)|0)+(bt>>>26)|0,bt&=67108863,r=Math.imul(T,$),n=(n=Math.imul(T,j))+Math.imul(O,$)|0,s=Math.imul(O,j),r=r+Math.imul(k,G)|0,n=(n=n+Math.imul(k,z)|0)+Math.imul(S,G)|0,s=s+Math.imul(S,z)|0,r=r+Math.imul(x,Z)|0,n=(n=n+Math.imul(x,V)|0)+Math.imul(E,Z)|0,s=s+Math.imul(E,V)|0,r=r+Math.imul(M,K)|0,n=(n=n+Math.imul(M,X)|0)+Math.imul(b,K)|0,s=s+Math.imul(b,X)|0,r=r+Math.imul(w,Y)|0,n=(n=n+Math.imul(w,tt)|0)+Math.imul(v,Y)|0,s=s+Math.imul(v,tt)|0,r=r+Math.imul(m,it)|0,n=(n=n+Math.imul(m,rt)|0)+Math.imul(p,it)|0,s=s+Math.imul(p,rt)|0;var At=(u+(r=r+Math.imul(c,st)|0)|0)+((8191&(n=(n=n+Math.imul(c,ot)|0)+Math.imul(d,st)|0))<<13)|0;u=((s=s+Math.imul(d,ot)|0)+(n>>>13)|0)+(At>>>26)|0,At&=67108863,r=Math.imul(C,$),n=(n=Math.imul(C,j))+Math.imul(_,$)|0,s=Math.imul(_,j),r=r+Math.imul(T,G)|0,n=(n=n+Math.imul(T,z)|0)+Math.imul(O,G)|0,s=s+Math.imul(O,z)|0,r=r+Math.imul(k,Z)|0,n=(n=n+Math.imul(k,V)|0)+Math.imul(S,Z)|0,s=s+Math.imul(S,V)|0,r=r+Math.imul(x,K)|0,n=(n=n+Math.imul(x,X)|0)+Math.imul(E,K)|0,s=s+Math.imul(E,X)|0,r=r+Math.imul(M,Y)|0,n=(n=n+Math.imul(M,tt)|0)+Math.imul(b,Y)|0,s=s+Math.imul(b,tt)|0,r=r+Math.imul(w,it)|0,n=(n=n+Math.imul(w,rt)|0)+Math.imul(v,it)|0,s=s+Math.imul(v,rt)|0,r=r+Math.imul(m,st)|0,n=(n=n+Math.imul(m,ot)|0)+Math.imul(p,st)|0,s=s+Math.imul(p,ot)|0;var xt=(u+(r=r+Math.imul(c,ht)|0)|0)+((8191&(n=(n=n+Math.imul(c,ut)|0)+Math.imul(d,ht)|0))<<13)|0;u=((s=s+Math.imul(d,ut)|0)+(n>>>13)|0)+(xt>>>26)|0,xt&=67108863,r=Math.imul(P,$),n=(n=Math.imul(P,j))+Math.imul(N,$)|0,s=Math.imul(N,j),r=r+Math.imul(C,G)|0,n=(n=n+Math.imul(C,z)|0)+Math.imul(_,G)|0,s=s+Math.imul(_,z)|0,r=r+Math.imul(T,Z)|0,n=(n=n+Math.imul(T,V)|0)+Math.imul(O,Z)|0,s=s+Math.imul(O,V)|0,r=r+Math.imul(k,K)|0,n=(n=n+Math.imul(k,X)|0)+Math.imul(S,K)|0,s=s+Math.imul(S,X)|0,r=r+Math.imul(x,Y)|0,n=(n=n+Math.imul(x,tt)|0)+Math.imul(E,Y)|0,s=s+Math.imul(E,tt)|0,r=r+Math.imul(M,it)|0,n=(n=n+Math.imul(M,rt)|0)+Math.imul(b,it)|0,s=s+Math.imul(b,rt)|0,r=r+Math.imul(w,st)|0,n=(n=n+Math.imul(w,ot)|0)+Math.imul(v,st)|0,s=s+Math.imul(v,ot)|0,r=r+Math.imul(m,ht)|0,n=(n=n+Math.imul(m,ut)|0)+Math.imul(p,ht)|0,s=s+Math.imul(p,ut)|0;var Et=(u+(r=r+Math.imul(c,ct)|0)|0)+((8191&(n=(n=n+Math.imul(c,dt)|0)+Math.imul(d,ct)|0))<<13)|0;u=((s=s+Math.imul(d,dt)|0)+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,r=Math.imul(L,$),n=(n=Math.imul(L,j))+Math.imul(F,$)|0,s=Math.imul(F,j),r=r+Math.imul(P,G)|0,n=(n=n+Math.imul(P,z)|0)+Math.imul(N,G)|0,s=s+Math.imul(N,z)|0,r=r+Math.imul(C,Z)|0,n=(n=n+Math.imul(C,V)|0)+Math.imul(_,Z)|0,s=s+Math.imul(_,V)|0,r=r+Math.imul(T,K)|0,n=(n=n+Math.imul(T,X)|0)+Math.imul(O,K)|0,s=s+Math.imul(O,X)|0,r=r+Math.imul(k,Y)|0,n=(n=n+Math.imul(k,tt)|0)+Math.imul(S,Y)|0,s=s+Math.imul(S,tt)|0,r=r+Math.imul(x,it)|0,n=(n=n+Math.imul(x,rt)|0)+Math.imul(E,it)|0,s=s+Math.imul(E,rt)|0,r=r+Math.imul(M,st)|0,n=(n=n+Math.imul(M,ot)|0)+Math.imul(b,st)|0,s=s+Math.imul(b,ot)|0,r=r+Math.imul(w,ht)|0,n=(n=n+Math.imul(w,ut)|0)+Math.imul(v,ht)|0,s=s+Math.imul(v,ut)|0,r=r+Math.imul(m,ct)|0,n=(n=n+Math.imul(m,dt)|0)+Math.imul(p,ct)|0,s=s+Math.imul(p,dt)|0;var Rt=(u+(r=r+Math.imul(c,mt)|0)|0)+((8191&(n=(n=n+Math.imul(c,pt)|0)+Math.imul(d,mt)|0))<<13)|0;u=((s=s+Math.imul(d,pt)|0)+(n>>>13)|0)+(Rt>>>26)|0,Rt&=67108863,r=Math.imul(L,G),n=(n=Math.imul(L,z))+Math.imul(F,G)|0,s=Math.imul(F,z),r=r+Math.imul(P,Z)|0,n=(n=n+Math.imul(P,V)|0)+Math.imul(N,Z)|0,s=s+Math.imul(N,V)|0,r=r+Math.imul(C,K)|0,n=(n=n+Math.imul(C,X)|0)+Math.imul(_,K)|0,s=s+Math.imul(_,X)|0,r=r+Math.imul(T,Y)|0,n=(n=n+Math.imul(T,tt)|0)+Math.imul(O,Y)|0,s=s+Math.imul(O,tt)|0,r=r+Math.imul(k,it)|0,n=(n=n+Math.imul(k,rt)|0)+Math.imul(S,it)|0,s=s+Math.imul(S,rt)|0,r=r+Math.imul(x,st)|0,n=(n=n+Math.imul(x,ot)|0)+Math.imul(E,st)|0,s=s+Math.imul(E,ot)|0,r=r+Math.imul(M,ht)|0,n=(n=n+Math.imul(M,ut)|0)+Math.imul(b,ht)|0,s=s+Math.imul(b,ut)|0,r=r+Math.imul(w,ct)|0,n=(n=n+Math.imul(w,dt)|0)+Math.imul(v,ct)|0,s=s+Math.imul(v,dt)|0;var kt=(u+(r=r+Math.imul(m,mt)|0)|0)+((8191&(n=(n=n+Math.imul(m,pt)|0)+Math.imul(p,mt)|0))<<13)|0;u=((s=s+Math.imul(p,pt)|0)+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,r=Math.imul(L,Z),n=(n=Math.imul(L,V))+Math.imul(F,Z)|0,s=Math.imul(F,V),r=r+Math.imul(P,K)|0,n=(n=n+Math.imul(P,X)|0)+Math.imul(N,K)|0,s=s+Math.imul(N,X)|0,r=r+Math.imul(C,Y)|0,n=(n=n+Math.imul(C,tt)|0)+Math.imul(_,Y)|0,s=s+Math.imul(_,tt)|0,r=r+Math.imul(T,it)|0,n=(n=n+Math.imul(T,rt)|0)+Math.imul(O,it)|0,s=s+Math.imul(O,rt)|0,r=r+Math.imul(k,st)|0,n=(n=n+Math.imul(k,ot)|0)+Math.imul(S,st)|0,s=s+Math.imul(S,ot)|0,r=r+Math.imul(x,ht)|0,n=(n=n+Math.imul(x,ut)|0)+Math.imul(E,ht)|0,s=s+Math.imul(E,ut)|0,r=r+Math.imul(M,ct)|0,n=(n=n+Math.imul(M,dt)|0)+Math.imul(b,ct)|0,s=s+Math.imul(b,dt)|0;var St=(u+(r=r+Math.imul(w,mt)|0)|0)+((8191&(n=(n=n+Math.imul(w,pt)|0)+Math.imul(v,mt)|0))<<13)|0;u=((s=s+Math.imul(v,pt)|0)+(n>>>13)|0)+(St>>>26)|0,St&=67108863,r=Math.imul(L,K),n=(n=Math.imul(L,X))+Math.imul(F,K)|0,s=Math.imul(F,X),r=r+Math.imul(P,Y)|0,n=(n=n+Math.imul(P,tt)|0)+Math.imul(N,Y)|0,s=s+Math.imul(N,tt)|0,r=r+Math.imul(C,it)|0,n=(n=n+Math.imul(C,rt)|0)+Math.imul(_,it)|0,s=s+Math.imul(_,rt)|0,r=r+Math.imul(T,st)|0,n=(n=n+Math.imul(T,ot)|0)+Math.imul(O,st)|0,s=s+Math.imul(O,ot)|0,r=r+Math.imul(k,ht)|0,n=(n=n+Math.imul(k,ut)|0)+Math.imul(S,ht)|0,s=s+Math.imul(S,ut)|0,r=r+Math.imul(x,ct)|0,n=(n=n+Math.imul(x,dt)|0)+Math.imul(E,ct)|0,s=s+Math.imul(E,dt)|0;var It=(u+(r=r+Math.imul(M,mt)|0)|0)+((8191&(n=(n=n+Math.imul(M,pt)|0)+Math.imul(b,mt)|0))<<13)|0;u=((s=s+Math.imul(b,pt)|0)+(n>>>13)|0)+(It>>>26)|0,It&=67108863,r=Math.imul(L,Y),n=(n=Math.imul(L,tt))+Math.imul(F,Y)|0,s=Math.imul(F,tt),r=r+Math.imul(P,it)|0,n=(n=n+Math.imul(P,rt)|0)+Math.imul(N,it)|0,s=s+Math.imul(N,rt)|0,r=r+Math.imul(C,st)|0,n=(n=n+Math.imul(C,ot)|0)+Math.imul(_,st)|0,s=s+Math.imul(_,ot)|0,r=r+Math.imul(T,ht)|0,n=(n=n+Math.imul(T,ut)|0)+Math.imul(O,ht)|0,s=s+Math.imul(O,ut)|0,r=r+Math.imul(k,ct)|0,n=(n=n+Math.imul(k,dt)|0)+Math.imul(S,ct)|0,s=s+Math.imul(S,dt)|0;var Tt=(u+(r=r+Math.imul(x,mt)|0)|0)+((8191&(n=(n=n+Math.imul(x,pt)|0)+Math.imul(E,mt)|0))<<13)|0;u=((s=s+Math.imul(E,pt)|0)+(n>>>13)|0)+(Tt>>>26)|0,Tt&=67108863,r=Math.imul(L,it),n=(n=Math.imul(L,rt))+Math.imul(F,it)|0,s=Math.imul(F,rt),r=r+Math.imul(P,st)|0,n=(n=n+Math.imul(P,ot)|0)+Math.imul(N,st)|0,s=s+Math.imul(N,ot)|0,r=r+Math.imul(C,ht)|0,n=(n=n+Math.imul(C,ut)|0)+Math.imul(_,ht)|0,s=s+Math.imul(_,ut)|0,r=r+Math.imul(T,ct)|0,n=(n=n+Math.imul(T,dt)|0)+Math.imul(O,ct)|0,s=s+Math.imul(O,dt)|0;var Ot=(u+(r=r+Math.imul(k,mt)|0)|0)+((8191&(n=(n=n+Math.imul(k,pt)|0)+Math.imul(S,mt)|0))<<13)|0;u=((s=s+Math.imul(S,pt)|0)+(n>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,r=Math.imul(L,st),n=(n=Math.imul(L,ot))+Math.imul(F,st)|0,s=Math.imul(F,ot),r=r+Math.imul(P,ht)|0,n=(n=n+Math.imul(P,ut)|0)+Math.imul(N,ht)|0,s=s+Math.imul(N,ut)|0,r=r+Math.imul(C,ct)|0,n=(n=n+Math.imul(C,dt)|0)+Math.imul(_,ct)|0,s=s+Math.imul(_,dt)|0;var Bt=(u+(r=r+Math.imul(T,mt)|0)|0)+((8191&(n=(n=n+Math.imul(T,pt)|0)+Math.imul(O,mt)|0))<<13)|0;u=((s=s+Math.imul(O,pt)|0)+(n>>>13)|0)+(Bt>>>26)|0,Bt&=67108863,r=Math.imul(L,ht),n=(n=Math.imul(L,ut))+Math.imul(F,ht)|0,s=Math.imul(F,ut),r=r+Math.imul(P,ct)|0,n=(n=n+Math.imul(P,dt)|0)+Math.imul(N,ct)|0,s=s+Math.imul(N,dt)|0;var Ct=(u+(r=r+Math.imul(C,mt)|0)|0)+((8191&(n=(n=n+Math.imul(C,pt)|0)+Math.imul(_,mt)|0))<<13)|0;u=((s=s+Math.imul(_,pt)|0)+(n>>>13)|0)+(Ct>>>26)|0,Ct&=67108863,r=Math.imul(L,ct),n=(n=Math.imul(L,dt))+Math.imul(F,ct)|0,s=Math.imul(F,dt);var _t=(u+(r=r+Math.imul(P,mt)|0)|0)+((8191&(n=(n=n+Math.imul(P,pt)|0)+Math.imul(N,mt)|0))<<13)|0;u=((s=s+Math.imul(N,pt)|0)+(n>>>13)|0)+(_t>>>26)|0,_t&=67108863;var Dt=(u+(r=Math.imul(L,mt))|0)+((8191&(n=(n=Math.imul(L,pt))+Math.imul(F,mt)|0))<<13)|0;return u=((s=Math.imul(F,pt))+(n>>>13)|0)+(Dt>>>26)|0,Dt&=67108863,h[0]=gt,h[1]=wt,h[2]=vt,h[3]=yt,h[4]=Mt,h[5]=bt,h[6]=At,h[7]=xt,h[8]=Et,h[9]=Rt,h[10]=kt,h[11]=St,h[12]=It,h[13]=Tt,h[14]=Ot,h[15]=Bt,h[16]=Ct,h[17]=_t,h[18]=Dt,0!==u&&(h[19]=u,i.length++),i};function f(t,e,i){return(new m).mulp(t,e,i)}function m(t,e){this.x=t,this.y=e}Math.imul||(d=c),n.prototype.mulTo=function(t,e){var i=this.length+t.length;return 10===this.length&&10===t.length?d(this,t,e):i<63?c(this,t,e):i<1024?function(t,e,i){i.negative=e.negative^t.negative,i.length=t.length+e.length;for(var r=0,n=0,s=0;s<i.length-1;s++){var o=n;n=0;for(var a=67108863&r,h=Math.min(s,e.length-1),u=Math.max(0,s-t.length+1);u<=h;u++){var l=(0|t.words[s-u])*(0|e.words[u]),c=67108863&l;a=67108863&(c=c+a|0),n+=(o=(o=o+(l/67108864|0)|0)+(c>>>26)|0)>>>26,o&=67108863}i.words[s]=a,r=o,o=n}return 0!==r?i.words[s]=r:i.length--,i.strip()}(this,t,e):f(this,t,e)},m.prototype.makeRBT=function(t){for(var e=new Array(t),i=n.prototype._countBits(t)-1,r=0;r<t;r++)e[r]=this.revBin(r,i,t);return e},m.prototype.revBin=function(t,e,i){if(0===t||t===i-1)return t;for(var r=0,n=0;n<e;n++)r|=(1&t)<<e-n-1,t>>=1;return r},m.prototype.permute=function(t,e,i,r,n,s){for(var o=0;o<s;o++)r[o]=e[t[o]],n[o]=i[t[o]]},m.prototype.transform=function(t,e,i,r,n,s){this.permute(s,t,e,i,r,n);for(var o=1;o<n;o<<=1)for(var a=o<<1,h=Math.cos(2*Math.PI/a),u=Math.sin(2*Math.PI/a),l=0;l<n;l+=a)for(var c=h,d=u,f=0;f<o;f++){var m=i[l+f],p=r[l+f],g=i[l+f+o],w=r[l+f+o],v=c*g-d*w;w=c*w+d*g,i[l+f]=m+(g=v),r[l+f]=p+w,i[l+f+o]=m-g,r[l+f+o]=p-w,f!==a&&(v=h*c-u*d,d=h*d+u*c,c=v)}},m.prototype.guessLen13b=function(t,e){var i=1|Math.max(e,t),r=1&i,n=0;for(i=i/2|0;i;i>>>=1)n++;return 1<<n+1+r},m.prototype.conjugate=function(t,e,i){if(!(i<=1))for(var r=0;r<i/2;r++){var n=t[r];t[r]=t[i-r-1],t[i-r-1]=n,n=e[r],e[r]=-e[i-r-1],e[i-r-1]=-n}},m.prototype.normalize13b=function(t,e){for(var i=0,r=0;r<e/2;r++){var n=8192*Math.round(t[2*r+1]/e)+Math.round(t[2*r]/e)+i;t[r]=67108863&n,i=n<67108864?0:n/67108864|0}return t},m.prototype.convert13b=function(t,e,r,n){for(var s=0,o=0;o<e;o++)r[2*o]=8191&(s+=0|t[o]),r[2*o+1]=8191&(s>>>=13),s>>>=13;for(o=2*e;o<n;++o)r[o]=0;i(0===s),i(0==(-8192&s))},m.prototype.stub=function(t){for(var e=new Array(t),i=0;i<t;i++)e[i]=0;return e},m.prototype.mulp=function(t,e,i){var r=2*this.guessLen13b(t.length,e.length),n=this.makeRBT(r),s=this.stub(r),o=new Array(r),a=new Array(r),h=new Array(r),u=new Array(r),l=new Array(r),c=new Array(r),d=i.words;d.length=r,this.convert13b(t.words,t.length,o,r),this.convert13b(e.words,e.length,u,r),this.transform(o,s,a,h,r,n),this.transform(u,s,l,c,r,n);for(var f=0;f<r;f++){var m=a[f]*l[f]-h[f]*c[f];h[f]=a[f]*c[f]+h[f]*l[f],a[f]=m}return this.conjugate(a,h,r),this.transform(a,h,d,s,r,n),this.conjugate(d,s,r),this.normalize13b(d,r),i.negative=t.negative^e.negative,i.length=t.length+e.length,i.strip()},n.prototype.mul=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},n.prototype.mulf=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),f(this,t,e)},n.prototype.imul=function(t){return this.clone().mulTo(t,this)},n.prototype.imuln=function(t){i("number"==typeof t),i(t<67108864);for(var e=0,r=0;r<this.length;r++){var n=(0|this.words[r])*t,s=(67108863&n)+(67108863&e);e>>=26,e+=n/67108864|0,e+=s>>>26,this.words[r]=67108863&s}return 0!==e&&(this.words[r]=e,this.length++),this},n.prototype.muln=function(t){return this.clone().imuln(t)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),i=0;i<e.length;i++){var r=i%26;e[i]=(t.words[i/26|0]&1<<r)>>>r}return e}(t);if(0===e.length)return new n(1);for(var i=this,r=0;r<e.length&&0===e[r];r++,i=i.sqr());if(++r<e.length)for(var s=i.sqr();r<e.length;r++,s=s.sqr())0!==e[r]&&(i=i.mul(s));return i},n.prototype.iushln=function(t){i("number"==typeof t&&t>=0);var e,r=t%26,n=(t-r)/26,s=67108863>>>26-r<<26-r;if(0!==r){var o=0;for(e=0;e<this.length;e++){var a=this.words[e]&s;this.words[e]=(0|this.words[e])-a<<r|o,o=a>>>26-r}o&&(this.words[e]=o,this.length++)}if(0!==n){for(e=this.length-1;e>=0;e--)this.words[e+n]=this.words[e];for(e=0;e<n;e++)this.words[e]=0;this.length+=n}return this.strip()},n.prototype.ishln=function(t){return i(0===this.negative),this.iushln(t)},n.prototype.iushrn=function(t,e,r){var n;i("number"==typeof t&&t>=0),n=e?(e-e%26)/26:0;var s=t%26,o=Math.min((t-s)/26,this.length),a=67108863^67108863>>>s<<s,h=r;if(n-=o,n=Math.max(0,n),h){for(var u=0;u<o;u++)h.words[u]=this.words[u];h.length=o}if(0===o);else if(this.length>o)for(this.length-=o,u=0;u<this.length;u++)this.words[u]=this.words[u+o];else this.words[0]=0,this.length=1;var l=0;for(u=this.length-1;u>=0&&(0!==l||u>=n);u--){var c=0|this.words[u];this.words[u]=l<<26-s|c>>>s,l=c&a}return h&&0!==l&&(h.words[h.length++]=l),0===this.length&&(this.words[0]=0,this.length=1),this.strip()},n.prototype.ishrn=function(t,e,r){return i(0===this.negative),this.iushrn(t,e,r)},n.prototype.shln=function(t){return this.clone().ishln(t)},n.prototype.ushln=function(t){return this.clone().iushln(t)},n.prototype.shrn=function(t){return this.clone().ishrn(t)},n.prototype.ushrn=function(t){return this.clone().iushrn(t)},n.prototype.testn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return!(this.length<=r||!(this.words[r]&1<<e))},n.prototype.imaskn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return i(0===this.negative,"imaskn works only with positive numbers"),this.length<=r?this:(0!==e&&r++,this.length=Math.min(r,this.length),0!==e&&(this.words[this.length-1]&=67108863^67108863>>>e<<e),this.strip())},n.prototype.maskn=function(t){return this.clone().imaskn(t)},n.prototype.iaddn=function(t){return i("number"==typeof t),i(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},n.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},n.prototype.isubn=function(t){if(i("number"==typeof t),i(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this.strip()},n.prototype.addn=function(t){return this.clone().iaddn(t)},n.prototype.subn=function(t){return this.clone().isubn(t)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(t,e,r){var n,s;this._expand(t.length+r);var o=0;for(n=0;n<t.length;n++){s=(0|this.words[n+r])+o;var a=(0|t.words[n])*e;o=((s-=67108863&a)>>26)-(a/67108864|0),this.words[n+r]=67108863&s}for(;n<this.length-r;n++)o=(s=(0|this.words[n+r])+o)>>26,this.words[n+r]=67108863&s;if(0===o)return this.strip();for(i(-1===o),o=0,n=0;n<this.length;n++)o=(s=-(0|this.words[n])+o)>>26,this.words[n]=67108863&s;return this.negative=1,this.strip()},n.prototype._wordDiv=function(t,e){var i,r=this.clone(),s=t,o=0|s.words[s.length-1];0!=(i=26-this._countBits(o))&&(s=s.ushln(i),r.iushln(i),o=0|s.words[s.length-1]);var a,h=r.length-s.length;if("mod"!==e){(a=new n(null)).length=h+1,a.words=new Array(a.length);for(var u=0;u<a.length;u++)a.words[u]=0}var l=r.clone()._ishlnsubmul(s,1,h);0===l.negative&&(r=l,a&&(a.words[h]=1));for(var c=h-1;c>=0;c--){var d=67108864*(0|r.words[s.length+c])+(0|r.words[s.length+c-1]);for(d=Math.min(d/o|0,67108863),r._ishlnsubmul(s,d,c);0!==r.negative;)d--,r.negative=0,r._ishlnsubmul(s,1,c),r.isZero()||(r.negative^=1);a&&(a.words[c]=d)}return a&&a.strip(),r.strip(),"div"!==e&&0!==i&&r.iushrn(i),{div:a||null,mod:r}},n.prototype.divmod=function(t,e,r){return i(!t.isZero()),this.isZero()?{div:new n(0),mod:new n(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(s=a.div.neg()),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.iadd(t)),{div:s,mod:o}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(s=a.div.neg()),{div:s,mod:a.mod}):0!=(this.negative&t.negative)?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.isub(t)),{div:a.div,mod:o}):t.length>this.length||this.cmp(t)<0?{div:new n(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new n(this.modn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new n(this.modn(t.words[0]))}:this._wordDiv(t,e);var s,o,a},n.prototype.div=function(t){return this.divmod(t,"div",!1).div},n.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},n.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},n.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var i=0!==e.div.negative?e.mod.isub(t):e.mod,r=t.ushrn(1),n=t.andln(1),s=i.cmp(r);return s<0||1===n&&0===s?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},n.prototype.modn=function(t){i(t<=67108863);for(var e=(1<<26)%t,r=0,n=this.length-1;n>=0;n--)r=(e*r+(0|this.words[n]))%t;return r},n.prototype.idivn=function(t){i(t<=67108863);for(var e=0,r=this.length-1;r>=0;r--){var n=(0|this.words[r])+67108864*e;this.words[r]=n/t|0,e=n%t}return this.strip()},n.prototype.divn=function(t){return this.clone().idivn(t)},n.prototype.egcd=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s=new n(1),o=new n(0),a=new n(0),h=new n(1),u=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++u;for(var l=r.clone(),c=e.clone();!e.isZero();){for(var d=0,f=1;0==(e.words[0]&f)&&d<26;++d,f<<=1);if(d>0)for(e.iushrn(d);d-- >0;)(s.isOdd()||o.isOdd())&&(s.iadd(l),o.isub(c)),s.iushrn(1),o.iushrn(1);for(var m=0,p=1;0==(r.words[0]&p)&&m<26;++m,p<<=1);if(m>0)for(r.iushrn(m);m-- >0;)(a.isOdd()||h.isOdd())&&(a.iadd(l),h.isub(c)),a.iushrn(1),h.iushrn(1);e.cmp(r)>=0?(e.isub(r),s.isub(a),o.isub(h)):(r.isub(e),a.isub(s),h.isub(o))}return{a:a,b:h,gcd:r.iushln(u)}},n.prototype._invmp=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s,o=new n(1),a=new n(0),h=r.clone();e.cmpn(1)>0&&r.cmpn(1)>0;){for(var u=0,l=1;0==(e.words[0]&l)&&u<26;++u,l<<=1);if(u>0)for(e.iushrn(u);u-- >0;)o.isOdd()&&o.iadd(h),o.iushrn(1);for(var c=0,d=1;0==(r.words[0]&d)&&c<26;++c,d<<=1);if(c>0)for(r.iushrn(c);c-- >0;)a.isOdd()&&a.iadd(h),a.iushrn(1);e.cmp(r)>=0?(e.isub(r),o.isub(a)):(r.isub(e),a.isub(o))}return(s=0===e.cmpn(1)?o:a).cmpn(0)<0&&s.iadd(t),s},n.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),i=t.clone();e.negative=0,i.negative=0;for(var r=0;e.isEven()&&i.isEven();r++)e.iushrn(1),i.iushrn(1);for(;;){for(;e.isEven();)e.iushrn(1);for(;i.isEven();)i.iushrn(1);var n=e.cmp(i);if(n<0){var s=e;e=i,i=s}else if(0===n||0===i.cmpn(1))break;e.isub(i)}return i.iushln(r)},n.prototype.invm=function(t){return this.egcd(t).a.umod(t)},n.prototype.isEven=function(){return 0==(1&this.words[0])},n.prototype.isOdd=function(){return 1==(1&this.words[0])},n.prototype.andln=function(t){return this.words[0]&t},n.prototype.bincn=function(t){i("number"==typeof t);var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return this._expand(r+1),this.words[r]|=n,this;for(var s=n,o=r;0!==s&&o<this.length;o++){var a=0|this.words[o];s=(a+=s)>>>26,this.words[o]=a&=67108863}return 0!==s&&(this.words[o]=s,this.length++),this},n.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},n.prototype.cmpn=function(t){var e,r=t<0;if(0!==this.negative&&!r)return-1;if(0===this.negative&&r)return 1;if(this.strip(),this.length>1)e=1;else{r&&(t=-t),i(t<=67108863,"Number is too big");var n=0|this.words[0];e=n===t?0:n<t?-1:1}return 0!==this.negative?0|-e:e},n.prototype.cmp=function(t){if(0!==this.negative&&0===t.negative)return-1;if(0===this.negative&&0!==t.negative)return 1;var e=this.ucmp(t);return 0!==this.negative?0|-e:e},n.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,i=this.length-1;i>=0;i--){var r=0|this.words[i],n=0|t.words[i];if(r!==n){r<n?e=-1:r>n&&(e=1);break}}return e},n.prototype.gtn=function(t){return 1===this.cmpn(t)},n.prototype.gt=function(t){return 1===this.cmp(t)},n.prototype.gten=function(t){return this.cmpn(t)>=0},n.prototype.gte=function(t){return this.cmp(t)>=0},n.prototype.ltn=function(t){return-1===this.cmpn(t)},n.prototype.lt=function(t){return-1===this.cmp(t)},n.prototype.lten=function(t){return this.cmpn(t)<=0},n.prototype.lte=function(t){return this.cmp(t)<=0},n.prototype.eqn=function(t){return 0===this.cmpn(t)},n.prototype.eq=function(t){return 0===this.cmp(t)},n.red=function(t){return new b(t)},n.prototype.toRed=function(t){return i(!this.red,"Already a number in reduction context"),i(0===this.negative,"red works only with positives"),t.convertTo(this)._forceRed(t)},n.prototype.fromRed=function(){return i(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(t){return this.red=t,this},n.prototype.forceRed=function(t){return i(!this.red,"Already a number in reduction context"),this._forceRed(t)},n.prototype.redAdd=function(t){return i(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},n.prototype.redIAdd=function(t){return i(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},n.prototype.redSub=function(t){return i(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},n.prototype.redISub=function(t){return i(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},n.prototype.redShl=function(t){return i(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},n.prototype.redMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},n.prototype.redIMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},n.prototype.redSqr=function(){return i(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return i(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return i(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return i(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return i(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(t){return i(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var p={k256:null,p224:null,p192:null,p25519:null};function g(t,e){this.name=t,this.p=new n(e,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function w(){g.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function v(){g.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function y(){g.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function M(){g.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function b(t){if("string"==typeof t){var e=n._prime(t);this.m=e.p,this.prime=e}else i(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}function A(t){b.call(this,t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}g.prototype._tmp=function(){var t=new n(null);return t.words=new Array(Math.ceil(this.n/13)),t},g.prototype.ireduce=function(t){var e,i=t;do{this.split(i,this.tmp),e=(i=(i=this.imulK(i)).iadd(this.tmp)).bitLength()}while(e>this.n);var r=e<this.n?-1:i.ucmp(this.p);return 0===r?(i.words[0]=0,i.length=1):r>0?i.isub(this.p):i.strip(),i},g.prototype.split=function(t,e){t.iushrn(this.n,0,e)},g.prototype.imulK=function(t){return t.imul(this.k)},r(w,g),w.prototype.split=function(t,e){for(var i=4194303,r=Math.min(t.length,9),n=0;n<r;n++)e.words[n]=t.words[n];if(e.length=r,t.length<=9)return t.words[0]=0,void(t.length=1);var s=t.words[9];for(e.words[e.length++]=s&i,n=10;n<t.length;n++){var o=0|t.words[n];t.words[n-10]=(o&i)<<4|s>>>22,s=o}t.words[n-10]=s>>>=22,t.length-=0===s&&t.length>10?10:9},w.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,i=0;i<t.length;i++){var r=0|t.words[i];t.words[i]=67108863&(e+=977*r),e=64*r+(e/67108864|0)}return 0===t.words[t.length-1]&&(t.length--,0===t.words[t.length-1]&&t.length--),t},r(v,g),r(y,g),r(M,g),M.prototype.imulK=function(t){for(var e=0,i=0;i<t.length;i++){var r=19*(0|t.words[i])+e,n=67108863&r;r>>>=26,t.words[i]=n,e=r}return 0!==e&&(t.words[t.length++]=e),t},n._prime=function(t){if(p[t])return p[t];var e;if("k256"===t)e=new w;else if("p224"===t)e=new v;else if("p192"===t)e=new y;else{if("p25519"!==t)throw new Error("Unknown prime "+t);e=new M}return p[t]=e,e},b.prototype._verify1=function(t){i(0===t.negative,"red works only with positives"),i(t.red,"red works only with red numbers")},b.prototype._verify2=function(t,e){i(0==(t.negative|e.negative),"red works only with positives"),i(t.red&&t.red===e.red,"red works only with red numbers")},b.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):t.umod(this.m)._forceRed(this)},b.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},b.prototype.add=function(t,e){this._verify2(t,e);var i=t.add(e);return i.cmp(this.m)>=0&&i.isub(this.m),i._forceRed(this)},b.prototype.iadd=function(t,e){this._verify2(t,e);var i=t.iadd(e);return i.cmp(this.m)>=0&&i.isub(this.m),i},b.prototype.sub=function(t,e){this._verify2(t,e);var i=t.sub(e);return i.cmpn(0)<0&&i.iadd(this.m),i._forceRed(this)},b.prototype.isub=function(t,e){this._verify2(t,e);var i=t.isub(e);return i.cmpn(0)<0&&i.iadd(this.m),i},b.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},b.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},b.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},b.prototype.isqr=function(t){return this.imul(t,t.clone())},b.prototype.sqr=function(t){return this.mul(t,t)},b.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(i(e%2==1),3===e){var r=this.m.add(new n(1)).iushrn(2);return this.pow(t,r)}for(var s=this.m.subn(1),o=0;!s.isZero()&&0===s.andln(1);)o++,s.iushrn(1);i(!s.isZero());var a=new n(1).toRed(this),h=a.redNeg(),u=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new n(2*l*l).toRed(this);0!==this.pow(l,u).cmp(h);)l.redIAdd(h);for(var c=this.pow(l,s),d=this.pow(t,s.addn(1).iushrn(1)),f=this.pow(t,s),m=o;0!==f.cmp(a);){for(var p=f,g=0;0!==p.cmp(a);g++)p=p.redSqr();i(g<m);var w=this.pow(c,new n(1).iushln(m-g-1));d=d.redMul(w),c=w.redSqr(),f=f.redMul(c),m=g}return d},b.prototype.invm=function(t){var e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},b.prototype.pow=function(t,e){if(e.isZero())return new n(1);if(0===e.cmpn(1))return t.clone();var i=new Array(16);i[0]=new n(1).toRed(this),i[1]=t;for(var r=2;r<i.length;r++)i[r]=this.mul(i[r-1],t);var s=i[0],o=0,a=0,h=e.bitLength()%26;for(0===h&&(h=26),r=e.length-1;r>=0;r--){for(var u=e.words[r],l=h-1;l>=0;l--){var c=u>>l&1;s!==i[0]&&(s=this.sqr(s)),0!==c||0!==o?(o<<=1,o|=c,(4==++a||0===r&&0===l)&&(s=this.mul(s,i[o]),a=0,o=0)):a=0}h=26}return s},b.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},b.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},n.mont=function(t){return new A(t)},r(A,b),A.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},A.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},A.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var i=t.imul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),n=i.isub(r).iushrn(this.shift),s=n;return n.cmp(this.m)>=0?s=n.isub(this.m):n.cmpn(0)<0&&(s=n.iadd(this.m)),s._forceRed(this)},A.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new n(0)._forceRed(this);var i=t.mul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(r).iushrn(this.shift),o=s;return s.cmp(this.m)>=0?o=s.isub(this.m):s.cmpn(0)<0&&(o=s.iadd(this.m)),o._forceRed(this)},A.prototype.invm=function(t){return this.imod(t._invmp(this.m).mul(this.r2))._forceRed(this)}}(t,mt)}),vt=pt(function(t){!function(t,e){function i(t,e){if(!t)throw new Error(e||"Assertion failed")}function r(t,e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}function n(t,e,i){if(n.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(i=e,e=10),this._init(t||0,e||10,i||"be"))}var s;"object"==typeof t?t.exports=n:e.BN=n,n.BN=n,n.wordSize=26;try{s=gt("buffer").Buffer}catch(t){}function o(t,e,i){for(var r=0,n=Math.min(t.length,i),s=e;s<n;s++){var o=t.charCodeAt(s)-48;r<<=4,r|=o>=49&&o<=54?o-49+10:o>=17&&o<=22?o-17+10:15&o}return r}function a(t,e,i,r){for(var n=0,s=Math.min(t.length,i),o=e;o<s;o++){var a=t.charCodeAt(o)-48;n*=r,n+=a>=49?a-49+10:a>=17?a-17+10:a}return n}n.isBN=function(t){return t instanceof n||null!==t&&"object"==typeof t&&t.constructor.wordSize===n.wordSize&&Array.isArray(t.words)},n.max=function(t,e){return t.cmp(e)>0?t:e},n.min=function(t,e){return t.cmp(e)<0?t:e},n.prototype._init=function(t,e,r){if("number"==typeof t)return this._initNumber(t,e,r);if("object"==typeof t)return this._initArray(t,e,r);"hex"===e&&(e=16),i(e===(0|e)&&e>=2&&e<=36);var n=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&n++,16===e?this._parseHex(t,n):this._parseBase(t,e,n),"-"===t[0]&&(this.negative=1),this.strip(),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(i(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initArray=function(t,e,r){if(i("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,o,a=0;if("be"===r)for(n=t.length-1,s=0;n>=0;n-=3)this.words[s]|=(o=t[n]|t[n-1]<<8|t[n-2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);else if("le"===r)for(n=0,s=0;n<t.length;n+=3)this.words[s]|=(o=t[n]|t[n+1]<<8|t[n+2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);return this.strip()},n.prototype._parseHex=function(t,e){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var r,n,s=0;for(i=t.length-6,r=0;i>=e;i-=6)n=o(t,i,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303,(s+=24)>=26&&(s-=26,r++);i+6!==e&&(n=o(t,e,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303),this.strip()},n.prototype._parseBase=function(t,e,i){this.words=[0],this.length=1;for(var r=0,n=1;n<=67108863;n*=e)r++;r--,n=n/e|0;for(var s=t.length-i,o=s%r,h=Math.min(s,s-o)+i,u=0,l=i;l<h;l+=r)u=a(t,l,l+r,e),this.imuln(n),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(0!==o){var c=1;for(u=a(t,l,t.length,e),l=0;l<o;l++)c*=e;this.imuln(c),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}},n.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},n.prototype.clone=function(){var t=new n(null);return this.copy(t),t},n.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},n.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},n.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},n.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var h=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],u=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],l=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function c(t,e,i){i.negative=e.negative^t.negative;var r=t.length+e.length|0;i.length=r,r=r-1|0;var n=0|t.words[0],s=0|e.words[0],o=n*s,a=o/67108864|0;i.words[0]=67108863&o;for(var h=1;h<r;h++){for(var u=a>>>26,l=67108863&a,c=Math.min(h,e.length-1),d=Math.max(0,h-t.length+1);d<=c;d++)u+=(o=(n=0|t.words[h-d|0])*(s=0|e.words[d])+l)/67108864|0,l=67108863&o;i.words[h]=0|l,a=0|u}return 0!==a?i.words[h]=0|a:i.length--,i.strip()}n.prototype.toString=function(t,e){var r;if(e=0|e||1,16===(t=t||10)||"hex"===t){r="";for(var n=0,s=0,o=0;o<this.length;o++){var a=this.words[o],c=(16777215&(a<<n|s)).toString(16);r=0!=(s=a>>>24-n&16777215)||o!==this.length-1?h[6-c.length]+c+r:c+r,(n+=2)>=26&&(n-=26,o--)}for(0!==s&&(r=s.toString(16)+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}if(t===(0|t)&&t>=2&&t<=36){var d=u[t],f=l[t];r="";var m=this.clone();for(m.negative=0;!m.isZero();){var p=m.modn(f).toString(t);r=(m=m.idivn(f)).isZero()?p+r:h[d-p.length]+p+r}for(this.isZero()&&(r="0"+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}i(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&i(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},n.prototype.toJSON=function(){return this.toString(16)},n.prototype.toBuffer=function(t,e){return i(void 0!==s),this.toArrayLike(s,t,e)},n.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},n.prototype.toArrayLike=function(t,e,r){var n=this.byteLength(),s=r||Math.max(1,n);i(n<=s,"byte array longer than desired length"),i(s>0,"Requested array length <= 0"),this.strip();var o,a,h="le"===e,u=new t(s),l=this.clone();if(h){for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[a]=o;for(;a<s;a++)u[a]=0}else{for(a=0;a<s-n;a++)u[a]=0;for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[s-a-1]=o}return u},n.prototype._countBits=Math.clz32?function(t){return 32-Math.clz32(t)}:function(t){var e=t,i=0;return e>=4096&&(i+=13,e>>>=13),e>=64&&(i+=7,e>>>=7),e>=8&&(i+=4,e>>>=4),e>=2&&(i+=2,e>>>=2),i+e},n.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,i=0;return 0==(8191&e)&&(i+=13,e>>>=13),0==(127&e)&&(i+=7,e>>>=7),0==(15&e)&&(i+=4,e>>>=4),0==(3&e)&&(i+=2,e>>>=2),0==(1&e)&&i++,i},n.prototype.bitLength=function(){var t=this._countBits(this.words[this.length-1]);return 26*(this.length-1)+t},n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var i=this._zeroBits(this.words[e]);if(t+=i,26!==i)break}return t},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},n.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return 0!==this.negative},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this.strip()},n.prototype.ior=function(t){return i(0==(this.negative|t.negative)),this.iuor(t)},n.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},n.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},n.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var i=0;i<e.length;i++)this.words[i]=this.words[i]&t.words[i];return this.length=e.length,this.strip()},n.prototype.iand=function(t){return i(0==(this.negative|t.negative)),this.iuand(t)},n.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},n.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},n.prototype.iuxor=function(t){var e,i;this.length>t.length?(e=this,i=t):(e=t,i=this);for(var r=0;r<i.length;r++)this.words[r]=e.words[r]^i.words[r];if(this!==e)for(;r<e.length;r++)this.words[r]=e.words[r];return this.length=e.length,this.strip()},n.prototype.ixor=function(t){return i(0==(this.negative|t.negative)),this.iuxor(t)},n.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},n.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},n.prototype.inotn=function(t){i("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=67108863&~this.words[n];return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this.strip()},n.prototype.notn=function(t){return this.clone().inotn(t)},n.prototype.setn=function(t,e){i("number"==typeof t&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),this.words[r]=e?this.words[r]|1<<n:this.words[r]&~(1<<n),this.strip()},n.prototype.iadd=function(t){var e,i,r;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(i=this,r=t):(i=t,r=this);for(var n=0,s=0;s<r.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+(0|r.words[s])+n),n=e>>>26;for(;0!==n&&s<i.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+n),n=e>>>26;if(this.length=i.length,0!==n)this.words[this.length]=n,this.length++;else if(i!==this)for(;s<i.length;s++)this.words[s]=i.words[s];return this},n.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},n.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var i,r,n=this.cmp(t);if(0===n)return this.negative=0,this.length=1,this.words[0]=0,this;n>0?(i=this,r=t):(i=t,r=this);for(var s=0,o=0;o<r.length;o++)s=(e=(0|i.words[o])-(0|r.words[o])+s)>>26,this.words[o]=67108863&e;for(;0!==s&&o<i.length;o++)s=(e=(0|i.words[o])+s)>>26,this.words[o]=67108863&e;if(0===s&&o<i.length&&i!==this)for(;o<i.length;o++)this.words[o]=i.words[o];return this.length=Math.max(this.length,o),i!==this&&(this.negative=1),this.strip()},n.prototype.sub=function(t){return this.clone().isub(t)};var d=function(t,e,i){var r,n,s,o=t.words,a=e.words,h=i.words,u=0,l=0|o[0],c=8191&l,d=l>>>13,f=0|o[1],m=8191&f,p=f>>>13,g=0|o[2],w=8191&g,v=g>>>13,y=0|o[3],M=8191&y,b=y>>>13,A=0|o[4],x=8191&A,E=A>>>13,R=0|o[5],k=8191&R,S=R>>>13,I=0|o[6],T=8191&I,O=I>>>13,B=0|o[7],C=8191&B,_=B>>>13,D=0|o[8],P=8191&D,N=D>>>13,W=0|o[9],L=8191&W,F=W>>>13,U=0|a[0],$=8191&U,j=U>>>13,q=0|a[1],G=8191&q,z=q>>>13,H=0|a[2],Z=8191&H,V=H>>>13,J=0|a[3],K=8191&J,X=J>>>13,Q=0|a[4],Y=8191&Q,tt=Q>>>13,et=0|a[5],it=8191&et,rt=et>>>13,nt=0|a[6],st=8191&nt,ot=nt>>>13,at=0|a[7],ht=8191&at,ut=at>>>13,lt=0|a[8],ct=8191&lt,dt=lt>>>13,ft=0|a[9],mt=8191&ft,pt=ft>>>13;i.negative=t.negative^e.negative,i.length=19;var gt=(u+(r=Math.imul(c,$))|0)+((8191&(n=(n=Math.imul(c,j))+Math.imul(d,$)|0))<<13)|0;u=((s=Math.imul(d,j))+(n>>>13)|0)+(gt>>>26)|0,gt&=67108863,r=Math.imul(m,$),n=(n=Math.imul(m,j))+Math.imul(p,$)|0,s=Math.imul(p,j);var wt=(u+(r=r+Math.imul(c,G)|0)|0)+((8191&(n=(n=n+Math.imul(c,z)|0)+Math.imul(d,G)|0))<<13)|0;u=((s=s+Math.imul(d,z)|0)+(n>>>13)|0)+(wt>>>26)|0,wt&=67108863,r=Math.imul(w,$),n=(n=Math.imul(w,j))+Math.imul(v,$)|0,s=Math.imul(v,j),r=r+Math.imul(m,G)|0,n=(n=n+Math.imul(m,z)|0)+Math.imul(p,G)|0,s=s+Math.imul(p,z)|0;var vt=(u+(r=r+Math.imul(c,Z)|0)|0)+((8191&(n=(n=n+Math.imul(c,V)|0)+Math.imul(d,Z)|0))<<13)|0;u=((s=s+Math.imul(d,V)|0)+(n>>>13)|0)+(vt>>>26)|0,vt&=67108863,r=Math.imul(M,$),n=(n=Math.imul(M,j))+Math.imul(b,$)|0,s=Math.imul(b,j),r=r+Math.imul(w,G)|0,n=(n=n+Math.imul(w,z)|0)+Math.imul(v,G)|0,s=s+Math.imul(v,z)|0,r=r+Math.imul(m,Z)|0,n=(n=n+Math.imul(m,V)|0)+Math.imul(p,Z)|0,s=s+Math.imul(p,V)|0;var yt=(u+(r=r+Math.imul(c,K)|0)|0)+((8191&(n=(n=n+Math.imul(c,X)|0)+Math.imul(d,K)|0))<<13)|0;u=((s=s+Math.imul(d,X)|0)+(n>>>13)|0)+(yt>>>26)|0,yt&=67108863,r=Math.imul(x,$),n=(n=Math.imul(x,j))+Math.imul(E,$)|0,s=Math.imul(E,j),r=r+Math.imul(M,G)|0,n=(n=n+Math.imul(M,z)|0)+Math.imul(b,G)|0,s=s+Math.imul(b,z)|0,r=r+Math.imul(w,Z)|0,n=(n=n+Math.imul(w,V)|0)+Math.imul(v,Z)|0,s=s+Math.imul(v,V)|0,r=r+Math.imul(m,K)|0,n=(n=n+Math.imul(m,X)|0)+Math.imul(p,K)|0,s=s+Math.imul(p,X)|0;var Mt=(u+(r=r+Math.imul(c,Y)|0)|0)+((8191&(n=(n=n+Math.imul(c,tt)|0)+Math.imul(d,Y)|0))<<13)|0;u=((s=s+Math.imul(d,tt)|0)+(n>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,r=Math.imul(k,$),n=(n=Math.imul(k,j))+Math.imul(S,$)|0,s=Math.imul(S,j),r=r+Math.imul(x,G)|0,n=(n=n+Math.imul(x,z)|0)+Math.imul(E,G)|0,s=s+Math.imul(E,z)|0,r=r+Math.imul(M,Z)|0,n=(n=n+Math.imul(M,V)|0)+Math.imul(b,Z)|0,s=s+Math.imul(b,V)|0,r=r+Math.imul(w,K)|0,n=(n=n+Math.imul(w,X)|0)+Math.imul(v,K)|0,s=s+Math.imul(v,X)|0,r=r+Math.imul(m,Y)|0,n=(n=n+Math.imul(m,tt)|0)+Math.imul(p,Y)|0,s=s+Math.imul(p,tt)|0;var bt=(u+(r=r+Math.imul(c,it)|0)|0)+((8191&(n=(n=n+Math.imul(c,rt)|0)+Math.imul(d,it)|0))<<13)|0;u=((s=s+Math.imul(d,rt)|0)+(n>>>13)|0)+(bt>>>26)|0,bt&=67108863,r=Math.imul(T,$),n=(n=Math.imul(T,j))+Math.imul(O,$)|0,s=Math.imul(O,j),r=r+Math.imul(k,G)|0,n=(n=n+Math.imul(k,z)|0)+Math.imul(S,G)|0,s=s+Math.imul(S,z)|0,r=r+Math.imul(x,Z)|0,n=(n=n+Math.imul(x,V)|0)+Math.imul(E,Z)|0,s=s+Math.imul(E,V)|0,r=r+Math.imul(M,K)|0,n=(n=n+Math.imul(M,X)|0)+Math.imul(b,K)|0,s=s+Math.imul(b,X)|0,r=r+Math.imul(w,Y)|0,n=(n=n+Math.imul(w,tt)|0)+Math.imul(v,Y)|0,s=s+Math.imul(v,tt)|0,r=r+Math.imul(m,it)|0,n=(n=n+Math.imul(m,rt)|0)+Math.imul(p,it)|0,s=s+Math.imul(p,rt)|0;var At=(u+(r=r+Math.imul(c,st)|0)|0)+((8191&(n=(n=n+Math.imul(c,ot)|0)+Math.imul(d,st)|0))<<13)|0;u=((s=s+Math.imul(d,ot)|0)+(n>>>13)|0)+(At>>>26)|0,At&=67108863,r=Math.imul(C,$),n=(n=Math.imul(C,j))+Math.imul(_,$)|0,s=Math.imul(_,j),r=r+Math.imul(T,G)|0,n=(n=n+Math.imul(T,z)|0)+Math.imul(O,G)|0,s=s+Math.imul(O,z)|0,r=r+Math.imul(k,Z)|0,n=(n=n+Math.imul(k,V)|0)+Math.imul(S,Z)|0,s=s+Math.imul(S,V)|0,r=r+Math.imul(x,K)|0,n=(n=n+Math.imul(x,X)|0)+Math.imul(E,K)|0,s=s+Math.imul(E,X)|0,r=r+Math.imul(M,Y)|0,n=(n=n+Math.imul(M,tt)|0)+Math.imul(b,Y)|0,s=s+Math.imul(b,tt)|0,r=r+Math.imul(w,it)|0,n=(n=n+Math.imul(w,rt)|0)+Math.imul(v,it)|0,s=s+Math.imul(v,rt)|0,r=r+Math.imul(m,st)|0,n=(n=n+Math.imul(m,ot)|0)+Math.imul(p,st)|0,s=s+Math.imul(p,ot)|0;var xt=(u+(r=r+Math.imul(c,ht)|0)|0)+((8191&(n=(n=n+Math.imul(c,ut)|0)+Math.imul(d,ht)|0))<<13)|0;u=((s=s+Math.imul(d,ut)|0)+(n>>>13)|0)+(xt>>>26)|0,xt&=67108863,r=Math.imul(P,$),n=(n=Math.imul(P,j))+Math.imul(N,$)|0,s=Math.imul(N,j),r=r+Math.imul(C,G)|0,n=(n=n+Math.imul(C,z)|0)+Math.imul(_,G)|0,s=s+Math.imul(_,z)|0,r=r+Math.imul(T,Z)|0,n=(n=n+Math.imul(T,V)|0)+Math.imul(O,Z)|0,s=s+Math.imul(O,V)|0,r=r+Math.imul(k,K)|0,n=(n=n+Math.imul(k,X)|0)+Math.imul(S,K)|0,s=s+Math.imul(S,X)|0,r=r+Math.imul(x,Y)|0,n=(n=n+Math.imul(x,tt)|0)+Math.imul(E,Y)|0,s=s+Math.imul(E,tt)|0,r=r+Math.imul(M,it)|0,n=(n=n+Math.imul(M,rt)|0)+Math.imul(b,it)|0,s=s+Math.imul(b,rt)|0,r=r+Math.imul(w,st)|0,n=(n=n+Math.imul(w,ot)|0)+Math.imul(v,st)|0,s=s+Math.imul(v,ot)|0,r=r+Math.imul(m,ht)|0,n=(n=n+Math.imul(m,ut)|0)+Math.imul(p,ht)|0,s=s+Math.imul(p,ut)|0;var Et=(u+(r=r+Math.imul(c,ct)|0)|0)+((8191&(n=(n=n+Math.imul(c,dt)|0)+Math.imul(d,ct)|0))<<13)|0;u=((s=s+Math.imul(d,dt)|0)+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,r=Math.imul(L,$),n=(n=Math.imul(L,j))+Math.imul(F,$)|0,s=Math.imul(F,j),r=r+Math.imul(P,G)|0,n=(n=n+Math.imul(P,z)|0)+Math.imul(N,G)|0,s=s+Math.imul(N,z)|0,r=r+Math.imul(C,Z)|0,n=(n=n+Math.imul(C,V)|0)+Math.imul(_,Z)|0,s=s+Math.imul(_,V)|0,r=r+Math.imul(T,K)|0,n=(n=n+Math.imul(T,X)|0)+Math.imul(O,K)|0,s=s+Math.imul(O,X)|0,r=r+Math.imul(k,Y)|0,n=(n=n+Math.imul(k,tt)|0)+Math.imul(S,Y)|0,s=s+Math.imul(S,tt)|0,r=r+Math.imul(x,it)|0,n=(n=n+Math.imul(x,rt)|0)+Math.imul(E,it)|0,s=s+Math.imul(E,rt)|0,r=r+Math.imul(M,st)|0,n=(n=n+Math.imul(M,ot)|0)+Math.imul(b,st)|0,s=s+Math.imul(b,ot)|0,r=r+Math.imul(w,ht)|0,n=(n=n+Math.imul(w,ut)|0)+Math.imul(v,ht)|0,s=s+Math.imul(v,ut)|0,r=r+Math.imul(m,ct)|0,n=(n=n+Math.imul(m,dt)|0)+Math.imul(p,ct)|0,s=s+Math.imul(p,dt)|0;var Rt=(u+(r=r+Math.imul(c,mt)|0)|0)+((8191&(n=(n=n+Math.imul(c,pt)|0)+Math.imul(d,mt)|0))<<13)|0;u=((s=s+Math.imul(d,pt)|0)+(n>>>13)|0)+(Rt>>>26)|0,Rt&=67108863,r=Math.imul(L,G),n=(n=Math.imul(L,z))+Math.imul(F,G)|0,s=Math.imul(F,z),r=r+Math.imul(P,Z)|0,n=(n=n+Math.imul(P,V)|0)+Math.imul(N,Z)|0,s=s+Math.imul(N,V)|0,r=r+Math.imul(C,K)|0,n=(n=n+Math.imul(C,X)|0)+Math.imul(_,K)|0,s=s+Math.imul(_,X)|0,r=r+Math.imul(T,Y)|0,n=(n=n+Math.imul(T,tt)|0)+Math.imul(O,Y)|0,s=s+Math.imul(O,tt)|0,r=r+Math.imul(k,it)|0,n=(n=n+Math.imul(k,rt)|0)+Math.imul(S,it)|0,s=s+Math.imul(S,rt)|0,r=r+Math.imul(x,st)|0,n=(n=n+Math.imul(x,ot)|0)+Math.imul(E,st)|0,s=s+Math.imul(E,ot)|0,r=r+Math.imul(M,ht)|0,n=(n=n+Math.imul(M,ut)|0)+Math.imul(b,ht)|0,s=s+Math.imul(b,ut)|0,r=r+Math.imul(w,ct)|0,n=(n=n+Math.imul(w,dt)|0)+Math.imul(v,ct)|0,s=s+Math.imul(v,dt)|0;var kt=(u+(r=r+Math.imul(m,mt)|0)|0)+((8191&(n=(n=n+Math.imul(m,pt)|0)+Math.imul(p,mt)|0))<<13)|0;u=((s=s+Math.imul(p,pt)|0)+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,r=Math.imul(L,Z),n=(n=Math.imul(L,V))+Math.imul(F,Z)|0,s=Math.imul(F,V),r=r+Math.imul(P,K)|0,n=(n=n+Math.imul(P,X)|0)+Math.imul(N,K)|0,s=s+Math.imul(N,X)|0,r=r+Math.imul(C,Y)|0,n=(n=n+Math.imul(C,tt)|0)+Math.imul(_,Y)|0,s=s+Math.imul(_,tt)|0,r=r+Math.imul(T,it)|0,n=(n=n+Math.imul(T,rt)|0)+Math.imul(O,it)|0,s=s+Math.imul(O,rt)|0,r=r+Math.imul(k,st)|0,n=(n=n+Math.imul(k,ot)|0)+Math.imul(S,st)|0,s=s+Math.imul(S,ot)|0,r=r+Math.imul(x,ht)|0,n=(n=n+Math.imul(x,ut)|0)+Math.imul(E,ht)|0,s=s+Math.imul(E,ut)|0,r=r+Math.imul(M,ct)|0,n=(n=n+Math.imul(M,dt)|0)+Math.imul(b,ct)|0,s=s+Math.imul(b,dt)|0;var St=(u+(r=r+Math.imul(w,mt)|0)|0)+((8191&(n=(n=n+Math.imul(w,pt)|0)+Math.imul(v,mt)|0))<<13)|0;u=((s=s+Math.imul(v,pt)|0)+(n>>>13)|0)+(St>>>26)|0,St&=67108863,r=Math.imul(L,K),n=(n=Math.imul(L,X))+Math.imul(F,K)|0,s=Math.imul(F,X),r=r+Math.imul(P,Y)|0,n=(n=n+Math.imul(P,tt)|0)+Math.imul(N,Y)|0,s=s+Math.imul(N,tt)|0,r=r+Math.imul(C,it)|0,n=(n=n+Math.imul(C,rt)|0)+Math.imul(_,it)|0,s=s+Math.imul(_,rt)|0,r=r+Math.imul(T,st)|0,n=(n=n+Math.imul(T,ot)|0)+Math.imul(O,st)|0,s=s+Math.imul(O,ot)|0,r=r+Math.imul(k,ht)|0,n=(n=n+Math.imul(k,ut)|0)+Math.imul(S,ht)|0,s=s+Math.imul(S,ut)|0,r=r+Math.imul(x,ct)|0,n=(n=n+Math.imul(x,dt)|0)+Math.imul(E,ct)|0,s=s+Math.imul(E,dt)|0;var It=(u+(r=r+Math.imul(M,mt)|0)|0)+((8191&(n=(n=n+Math.imul(M,pt)|0)+Math.imul(b,mt)|0))<<13)|0;u=((s=s+Math.imul(b,pt)|0)+(n>>>13)|0)+(It>>>26)|0,It&=67108863,r=Math.imul(L,Y),n=(n=Math.imul(L,tt))+Math.imul(F,Y)|0,s=Math.imul(F,tt),r=r+Math.imul(P,it)|0,n=(n=n+Math.imul(P,rt)|0)+Math.imul(N,it)|0,s=s+Math.imul(N,rt)|0,r=r+Math.imul(C,st)|0,n=(n=n+Math.imul(C,ot)|0)+Math.imul(_,st)|0,s=s+Math.imul(_,ot)|0,r=r+Math.imul(T,ht)|0,n=(n=n+Math.imul(T,ut)|0)+Math.imul(O,ht)|0,s=s+Math.imul(O,ut)|0,r=r+Math.imul(k,ct)|0,n=(n=n+Math.imul(k,dt)|0)+Math.imul(S,ct)|0,s=s+Math.imul(S,dt)|0;var Tt=(u+(r=r+Math.imul(x,mt)|0)|0)+((8191&(n=(n=n+Math.imul(x,pt)|0)+Math.imul(E,mt)|0))<<13)|0;u=((s=s+Math.imul(E,pt)|0)+(n>>>13)|0)+(Tt>>>26)|0,Tt&=67108863,r=Math.imul(L,it),n=(n=Math.imul(L,rt))+Math.imul(F,it)|0,s=Math.imul(F,rt),r=r+Math.imul(P,st)|0,n=(n=n+Math.imul(P,ot)|0)+Math.imul(N,st)|0,s=s+Math.imul(N,ot)|0,r=r+Math.imul(C,ht)|0,n=(n=n+Math.imul(C,ut)|0)+Math.imul(_,ht)|0,s=s+Math.imul(_,ut)|0,r=r+Math.imul(T,ct)|0,n=(n=n+Math.imul(T,dt)|0)+Math.imul(O,ct)|0,s=s+Math.imul(O,dt)|0;var Ot=(u+(r=r+Math.imul(k,mt)|0)|0)+((8191&(n=(n=n+Math.imul(k,pt)|0)+Math.imul(S,mt)|0))<<13)|0;u=((s=s+Math.imul(S,pt)|0)+(n>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,r=Math.imul(L,st),n=(n=Math.imul(L,ot))+Math.imul(F,st)|0,s=Math.imul(F,ot),r=r+Math.imul(P,ht)|0,n=(n=n+Math.imul(P,ut)|0)+Math.imul(N,ht)|0,s=s+Math.imul(N,ut)|0,r=r+Math.imul(C,ct)|0,n=(n=n+Math.imul(C,dt)|0)+Math.imul(_,ct)|0,s=s+Math.imul(_,dt)|0;var Bt=(u+(r=r+Math.imul(T,mt)|0)|0)+((8191&(n=(n=n+Math.imul(T,pt)|0)+Math.imul(O,mt)|0))<<13)|0;u=((s=s+Math.imul(O,pt)|0)+(n>>>13)|0)+(Bt>>>26)|0,Bt&=67108863,r=Math.imul(L,ht),n=(n=Math.imul(L,ut))+Math.imul(F,ht)|0,s=Math.imul(F,ut),r=r+Math.imul(P,ct)|0,n=(n=n+Math.imul(P,dt)|0)+Math.imul(N,ct)|0,s=s+Math.imul(N,dt)|0;var Ct=(u+(r=r+Math.imul(C,mt)|0)|0)+((8191&(n=(n=n+Math.imul(C,pt)|0)+Math.imul(_,mt)|0))<<13)|0;u=((s=s+Math.imul(_,pt)|0)+(n>>>13)|0)+(Ct>>>26)|0,Ct&=67108863,r=Math.imul(L,ct),n=(n=Math.imul(L,dt))+Math.imul(F,ct)|0,s=Math.imul(F,dt);var _t=(u+(r=r+Math.imul(P,mt)|0)|0)+((8191&(n=(n=n+Math.imul(P,pt)|0)+Math.imul(N,mt)|0))<<13)|0;u=((s=s+Math.imul(N,pt)|0)+(n>>>13)|0)+(_t>>>26)|0,_t&=67108863;var Dt=(u+(r=Math.imul(L,mt))|0)+((8191&(n=(n=Math.imul(L,pt))+Math.imul(F,mt)|0))<<13)|0;return u=((s=Math.imul(F,pt))+(n>>>13)|0)+(Dt>>>26)|0,Dt&=67108863,h[0]=gt,h[1]=wt,h[2]=vt,h[3]=yt,h[4]=Mt,h[5]=bt,h[6]=At,h[7]=xt,h[8]=Et,h[9]=Rt,h[10]=kt,h[11]=St,h[12]=It,h[13]=Tt,h[14]=Ot,h[15]=Bt,h[16]=Ct,h[17]=_t,h[18]=Dt,0!==u&&(h[19]=u,i.length++),i};function f(t,e,i){return(new m).mulp(t,e,i)}function m(t,e){this.x=t,this.y=e}Math.imul||(d=c),n.prototype.mulTo=function(t,e){var i=this.length+t.length;return 10===this.length&&10===t.length?d(this,t,e):i<63?c(this,t,e):i<1024?function(t,e,i){i.negative=e.negative^t.negative,i.length=t.length+e.length;for(var r=0,n=0,s=0;s<i.length-1;s++){var o=n;n=0;for(var a=67108863&r,h=Math.min(s,e.length-1),u=Math.max(0,s-t.length+1);u<=h;u++){var l=(0|t.words[s-u])*(0|e.words[u]),c=67108863&l;a=67108863&(c=c+a|0),n+=(o=(o=o+(l/67108864|0)|0)+(c>>>26)|0)>>>26,o&=67108863}i.words[s]=a,r=o,o=n}return 0!==r?i.words[s]=r:i.length--,i.strip()}(this,t,e):f(this,t,e)},m.prototype.makeRBT=function(t){for(var e=new Array(t),i=n.prototype._countBits(t)-1,r=0;r<t;r++)e[r]=this.revBin(r,i,t);return e},m.prototype.revBin=function(t,e,i){if(0===t||t===i-1)return t;for(var r=0,n=0;n<e;n++)r|=(1&t)<<e-n-1,t>>=1;return r},m.prototype.permute=function(t,e,i,r,n,s){for(var o=0;o<s;o++)r[o]=e[t[o]],n[o]=i[t[o]]},m.prototype.transform=function(t,e,i,r,n,s){this.permute(s,t,e,i,r,n);for(var o=1;o<n;o<<=1)for(var a=o<<1,h=Math.cos(2*Math.PI/a),u=Math.sin(2*Math.PI/a),l=0;l<n;l+=a)for(var c=h,d=u,f=0;f<o;f++){var m=i[l+f],p=r[l+f],g=i[l+f+o],w=r[l+f+o],v=c*g-d*w;w=c*w+d*g,i[l+f]=m+(g=v),r[l+f]=p+w,i[l+f+o]=m-g,r[l+f+o]=p-w,f!==a&&(v=h*c-u*d,d=h*d+u*c,c=v)}},m.prototype.guessLen13b=function(t,e){var i=1|Math.max(e,t),r=1&i,n=0;for(i=i/2|0;i;i>>>=1)n++;return 1<<n+1+r},m.prototype.conjugate=function(t,e,i){if(!(i<=1))for(var r=0;r<i/2;r++){var n=t[r];t[r]=t[i-r-1],t[i-r-1]=n,n=e[r],e[r]=-e[i-r-1],e[i-r-1]=-n}},m.prototype.normalize13b=function(t,e){for(var i=0,r=0;r<e/2;r++){var n=8192*Math.round(t[2*r+1]/e)+Math.round(t[2*r]/e)+i;t[r]=67108863&n,i=n<67108864?0:n/67108864|0}return t},m.prototype.convert13b=function(t,e,r,n){for(var s=0,o=0;o<e;o++)r[2*o]=8191&(s+=0|t[o]),r[2*o+1]=8191&(s>>>=13),s>>>=13;for(o=2*e;o<n;++o)r[o]=0;i(0===s),i(0==(-8192&s))},m.prototype.stub=function(t){for(var e=new Array(t),i=0;i<t;i++)e[i]=0;return e},m.prototype.mulp=function(t,e,i){var r=2*this.guessLen13b(t.length,e.length),n=this.makeRBT(r),s=this.stub(r),o=new Array(r),a=new Array(r),h=new Array(r),u=new Array(r),l=new Array(r),c=new Array(r),d=i.words;d.length=r,this.convert13b(t.words,t.length,o,r),this.convert13b(e.words,e.length,u,r),this.transform(o,s,a,h,r,n),this.transform(u,s,l,c,r,n);for(var f=0;f<r;f++){var m=a[f]*l[f]-h[f]*c[f];h[f]=a[f]*c[f]+h[f]*l[f],a[f]=m}return this.conjugate(a,h,r),this.transform(a,h,d,s,r,n),this.conjugate(d,s,r),this.normalize13b(d,r),i.negative=t.negative^e.negative,i.length=t.length+e.length,i.strip()},n.prototype.mul=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},n.prototype.mulf=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),f(this,t,e)},n.prototype.imul=function(t){return this.clone().mulTo(t,this)},n.prototype.imuln=function(t){i("number"==typeof t),i(t<67108864);for(var e=0,r=0;r<this.length;r++){var n=(0|this.words[r])*t,s=(67108863&n)+(67108863&e);e>>=26,e+=n/67108864|0,e+=s>>>26,this.words[r]=67108863&s}return 0!==e&&(this.words[r]=e,this.length++),this},n.prototype.muln=function(t){return this.clone().imuln(t)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),i=0;i<e.length;i++){var r=i%26;e[i]=(t.words[i/26|0]&1<<r)>>>r}return e}(t);if(0===e.length)return new n(1);for(var i=this,r=0;r<e.length&&0===e[r];r++,i=i.sqr());if(++r<e.length)for(var s=i.sqr();r<e.length;r++,s=s.sqr())0!==e[r]&&(i=i.mul(s));return i},n.prototype.iushln=function(t){i("number"==typeof t&&t>=0);var e,r=t%26,n=(t-r)/26,s=67108863>>>26-r<<26-r;if(0!==r){var o=0;for(e=0;e<this.length;e++){var a=this.words[e]&s;this.words[e]=(0|this.words[e])-a<<r|o,o=a>>>26-r}o&&(this.words[e]=o,this.length++)}if(0!==n){for(e=this.length-1;e>=0;e--)this.words[e+n]=this.words[e];for(e=0;e<n;e++)this.words[e]=0;this.length+=n}return this.strip()},n.prototype.ishln=function(t){return i(0===this.negative),this.iushln(t)},n.prototype.iushrn=function(t,e,r){var n;i("number"==typeof t&&t>=0),n=e?(e-e%26)/26:0;var s=t%26,o=Math.min((t-s)/26,this.length),a=67108863^67108863>>>s<<s,h=r;if(n-=o,n=Math.max(0,n),h){for(var u=0;u<o;u++)h.words[u]=this.words[u];h.length=o}if(0===o);else if(this.length>o)for(this.length-=o,u=0;u<this.length;u++)this.words[u]=this.words[u+o];else this.words[0]=0,this.length=1;var l=0;for(u=this.length-1;u>=0&&(0!==l||u>=n);u--){var c=0|this.words[u];this.words[u]=l<<26-s|c>>>s,l=c&a}return h&&0!==l&&(h.words[h.length++]=l),0===this.length&&(this.words[0]=0,this.length=1),this.strip()},n.prototype.ishrn=function(t,e,r){return i(0===this.negative),this.iushrn(t,e,r)},n.prototype.shln=function(t){return this.clone().ishln(t)},n.prototype.ushln=function(t){return this.clone().iushln(t)},n.prototype.shrn=function(t){return this.clone().ishrn(t)},n.prototype.ushrn=function(t){return this.clone().iushrn(t)},n.prototype.testn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return!(this.length<=r||!(this.words[r]&1<<e))},n.prototype.imaskn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return i(0===this.negative,"imaskn works only with positive numbers"),this.length<=r?this:(0!==e&&r++,this.length=Math.min(r,this.length),0!==e&&(this.words[this.length-1]&=67108863^67108863>>>e<<e),this.strip())},n.prototype.maskn=function(t){return this.clone().imaskn(t)},n.prototype.iaddn=function(t){return i("number"==typeof t),i(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},n.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},n.prototype.isubn=function(t){if(i("number"==typeof t),i(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this.strip()},n.prototype.addn=function(t){return this.clone().iaddn(t)},n.prototype.subn=function(t){return this.clone().isubn(t)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(t,e,r){var n,s;this._expand(t.length+r);var o=0;for(n=0;n<t.length;n++){s=(0|this.words[n+r])+o;var a=(0|t.words[n])*e;o=((s-=67108863&a)>>26)-(a/67108864|0),this.words[n+r]=67108863&s}for(;n<this.length-r;n++)o=(s=(0|this.words[n+r])+o)>>26,this.words[n+r]=67108863&s;if(0===o)return this.strip();for(i(-1===o),o=0,n=0;n<this.length;n++)o=(s=-(0|this.words[n])+o)>>26,this.words[n]=67108863&s;return this.negative=1,this.strip()},n.prototype._wordDiv=function(t,e){var i,r=this.clone(),s=t,o=0|s.words[s.length-1];0!=(i=26-this._countBits(o))&&(s=s.ushln(i),r.iushln(i),o=0|s.words[s.length-1]);var a,h=r.length-s.length;if("mod"!==e){(a=new n(null)).length=h+1,a.words=new Array(a.length);for(var u=0;u<a.length;u++)a.words[u]=0}var l=r.clone()._ishlnsubmul(s,1,h);0===l.negative&&(r=l,a&&(a.words[h]=1));for(var c=h-1;c>=0;c--){var d=67108864*(0|r.words[s.length+c])+(0|r.words[s.length+c-1]);for(d=Math.min(d/o|0,67108863),r._ishlnsubmul(s,d,c);0!==r.negative;)d--,r.negative=0,r._ishlnsubmul(s,1,c),r.isZero()||(r.negative^=1);a&&(a.words[c]=d)}return a&&a.strip(),r.strip(),"div"!==e&&0!==i&&r.iushrn(i),{div:a||null,mod:r}},n.prototype.divmod=function(t,e,r){return i(!t.isZero()),this.isZero()?{div:new n(0),mod:new n(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(s=a.div.neg()),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.iadd(t)),{div:s,mod:o}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(s=a.div.neg()),{div:s,mod:a.mod}):0!=(this.negative&t.negative)?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.isub(t)),{div:a.div,mod:o}):t.length>this.length||this.cmp(t)<0?{div:new n(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new n(this.modn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new n(this.modn(t.words[0]))}:this._wordDiv(t,e);var s,o,a},n.prototype.div=function(t){return this.divmod(t,"div",!1).div},n.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},n.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},n.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var i=0!==e.div.negative?e.mod.isub(t):e.mod,r=t.ushrn(1),n=t.andln(1),s=i.cmp(r);return s<0||1===n&&0===s?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},n.prototype.modn=function(t){i(t<=67108863);for(var e=(1<<26)%t,r=0,n=this.length-1;n>=0;n--)r=(e*r+(0|this.words[n]))%t;return r},n.prototype.idivn=function(t){i(t<=67108863);for(var e=0,r=this.length-1;r>=0;r--){var n=(0|this.words[r])+67108864*e;this.words[r]=n/t|0,e=n%t}return this.strip()},n.prototype.divn=function(t){return this.clone().idivn(t)},n.prototype.egcd=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s=new n(1),o=new n(0),a=new n(0),h=new n(1),u=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++u;for(var l=r.clone(),c=e.clone();!e.isZero();){for(var d=0,f=1;0==(e.words[0]&f)&&d<26;++d,f<<=1);if(d>0)for(e.iushrn(d);d-- >0;)(s.isOdd()||o.isOdd())&&(s.iadd(l),o.isub(c)),s.iushrn(1),o.iushrn(1);for(var m=0,p=1;0==(r.words[0]&p)&&m<26;++m,p<<=1);if(m>0)for(r.iushrn(m);m-- >0;)(a.isOdd()||h.isOdd())&&(a.iadd(l),h.isub(c)),a.iushrn(1),h.iushrn(1);e.cmp(r)>=0?(e.isub(r),s.isub(a),o.isub(h)):(r.isub(e),a.isub(s),h.isub(o))}return{a:a,b:h,gcd:r.iushln(u)}},n.prototype._invmp=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s,o=new n(1),a=new n(0),h=r.clone();e.cmpn(1)>0&&r.cmpn(1)>0;){for(var u=0,l=1;0==(e.words[0]&l)&&u<26;++u,l<<=1);if(u>0)for(e.iushrn(u);u-- >0;)o.isOdd()&&o.iadd(h),o.iushrn(1);for(var c=0,d=1;0==(r.words[0]&d)&&c<26;++c,d<<=1);if(c>0)for(r.iushrn(c);c-- >0;)a.isOdd()&&a.iadd(h),a.iushrn(1);e.cmp(r)>=0?(e.isub(r),o.isub(a)):(r.isub(e),a.isub(o))}return(s=0===e.cmpn(1)?o:a).cmpn(0)<0&&s.iadd(t),s},n.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),i=t.clone();e.negative=0,i.negative=0;for(var r=0;e.isEven()&&i.isEven();r++)e.iushrn(1),i.iushrn(1);for(;;){for(;e.isEven();)e.iushrn(1);for(;i.isEven();)i.iushrn(1);var n=e.cmp(i);if(n<0){var s=e;e=i,i=s}else if(0===n||0===i.cmpn(1))break;e.isub(i)}return i.iushln(r)},n.prototype.invm=function(t){return this.egcd(t).a.umod(t)},n.prototype.isEven=function(){return 0==(1&this.words[0])},n.prototype.isOdd=function(){return 1==(1&this.words[0])},n.prototype.andln=function(t){return this.words[0]&t},n.prototype.bincn=function(t){i("number"==typeof t);var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return this._expand(r+1),this.words[r]|=n,this;for(var s=n,o=r;0!==s&&o<this.length;o++){var a=0|this.words[o];s=(a+=s)>>>26,this.words[o]=a&=67108863}return 0!==s&&(this.words[o]=s,this.length++),this},n.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},n.prototype.cmpn=function(t){var e,r=t<0;if(0!==this.negative&&!r)return-1;if(0===this.negative&&r)return 1;if(this.strip(),this.length>1)e=1;else{r&&(t=-t),i(t<=67108863,"Number is too big");var n=0|this.words[0];e=n===t?0:n<t?-1:1}return 0!==this.negative?0|-e:e},n.prototype.cmp=function(t){if(0!==this.negative&&0===t.negative)return-1;if(0===this.negative&&0!==t.negative)return 1;var e=this.ucmp(t);return 0!==this.negative?0|-e:e},n.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,i=this.length-1;i>=0;i--){var r=0|this.words[i],n=0|t.words[i];if(r!==n){r<n?e=-1:r>n&&(e=1);break}}return e},n.prototype.gtn=function(t){return 1===this.cmpn(t)},n.prototype.gt=function(t){return 1===this.cmp(t)},n.prototype.gten=function(t){return this.cmpn(t)>=0},n.prototype.gte=function(t){return this.cmp(t)>=0},n.prototype.ltn=function(t){return-1===this.cmpn(t)},n.prototype.lt=function(t){return-1===this.cmp(t)},n.prototype.lten=function(t){return this.cmpn(t)<=0},n.prototype.lte=function(t){return this.cmp(t)<=0},n.prototype.eqn=function(t){return 0===this.cmpn(t)},n.prototype.eq=function(t){return 0===this.cmp(t)},n.red=function(t){return new b(t)},n.prototype.toRed=function(t){return i(!this.red,"Already a number in reduction context"),i(0===this.negative,"red works only with positives"),t.convertTo(this)._forceRed(t)},n.prototype.fromRed=function(){return i(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(t){return this.red=t,this},n.prototype.forceRed=function(t){return i(!this.red,"Already a number in reduction context"),this._forceRed(t)},n.prototype.redAdd=function(t){return i(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},n.prototype.redIAdd=function(t){return i(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},n.prototype.redSub=function(t){return i(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},n.prototype.redISub=function(t){return i(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},n.prototype.redShl=function(t){return i(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},n.prototype.redMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},n.prototype.redIMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},n.prototype.redSqr=function(){return i(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return i(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return i(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return i(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return i(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(t){return i(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var p={k256:null,p224:null,p192:null,p25519:null};function g(t,e){this.name=t,this.p=new n(e,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function w(){g.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function v(){g.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function y(){g.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function M(){g.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function b(t){if("string"==typeof t){var e=n._prime(t);this.m=e.p,this.prime=e}else i(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}function A(t){b.call(this,t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}g.prototype._tmp=function(){var t=new n(null);return t.words=new Array(Math.ceil(this.n/13)),t},g.prototype.ireduce=function(t){var e,i=t;do{this.split(i,this.tmp),e=(i=(i=this.imulK(i)).iadd(this.tmp)).bitLength()}while(e>this.n);var r=e<this.n?-1:i.ucmp(this.p);return 0===r?(i.words[0]=0,i.length=1):r>0?i.isub(this.p):i.strip(),i},g.prototype.split=function(t,e){t.iushrn(this.n,0,e)},g.prototype.imulK=function(t){return t.imul(this.k)},r(w,g),w.prototype.split=function(t,e){for(var i=4194303,r=Math.min(t.length,9),n=0;n<r;n++)e.words[n]=t.words[n];if(e.length=r,t.length<=9)return t.words[0]=0,void(t.length=1);var s=t.words[9];for(e.words[e.length++]=s&i,n=10;n<t.length;n++){var o=0|t.words[n];t.words[n-10]=(o&i)<<4|s>>>22,s=o}t.words[n-10]=s>>>=22,t.length-=0===s&&t.length>10?10:9},w.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,i=0;i<t.length;i++){var r=0|t.words[i];t.words[i]=67108863&(e+=977*r),e=64*r+(e/67108864|0)}return 0===t.words[t.length-1]&&(t.length--,0===t.words[t.length-1]&&t.length--),t},r(v,g),r(y,g),r(M,g),M.prototype.imulK=function(t){for(var e=0,i=0;i<t.length;i++){var r=19*(0|t.words[i])+e,n=67108863&r;r>>>=26,t.words[i]=n,e=r}return 0!==e&&(t.words[t.length++]=e),t},n._prime=function(t){if(p[t])return p[t];var e;if("k256"===t)e=new w;else if("p224"===t)e=new v;else if("p192"===t)e=new y;else{if("p25519"!==t)throw new Error("Unknown prime "+t);e=new M}return p[t]=e,e},b.prototype._verify1=function(t){i(0===t.negative,"red works only with positives"),i(t.red,"red works only with red numbers")},b.prototype._verify2=function(t,e){i(0==(t.negative|e.negative),"red works only with positives"),i(t.red&&t.red===e.red,"red works only with red numbers")},b.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):t.umod(this.m)._forceRed(this)},b.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},b.prototype.add=function(t,e){this._verify2(t,e);var i=t.add(e);return i.cmp(this.m)>=0&&i.isub(this.m),i._forceRed(this)},b.prototype.iadd=function(t,e){this._verify2(t,e);var i=t.iadd(e);return i.cmp(this.m)>=0&&i.isub(this.m),i},b.prototype.sub=function(t,e){this._verify2(t,e);var i=t.sub(e);return i.cmpn(0)<0&&i.iadd(this.m),i._forceRed(this)},b.prototype.isub=function(t,e){this._verify2(t,e);var i=t.isub(e);return i.cmpn(0)<0&&i.iadd(this.m),i},b.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},b.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},b.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},b.prototype.isqr=function(t){return this.imul(t,t.clone())},b.prototype.sqr=function(t){return this.mul(t,t)},b.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(i(e%2==1),3===e){var r=this.m.add(new n(1)).iushrn(2);return this.pow(t,r)}for(var s=this.m.subn(1),o=0;!s.isZero()&&0===s.andln(1);)o++,s.iushrn(1);i(!s.isZero());var a=new n(1).toRed(this),h=a.redNeg(),u=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new n(2*l*l).toRed(this);0!==this.pow(l,u).cmp(h);)l.redIAdd(h);for(var c=this.pow(l,s),d=this.pow(t,s.addn(1).iushrn(1)),f=this.pow(t,s),m=o;0!==f.cmp(a);){for(var p=f,g=0;0!==p.cmp(a);g++)p=p.redSqr();i(g<m);var w=this.pow(c,new n(1).iushln(m-g-1));d=d.redMul(w),c=w.redSqr(),f=f.redMul(c),m=g}return d},b.prototype.invm=function(t){var e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},b.prototype.pow=function(t,e){if(e.isZero())return new n(1);if(0===e.cmpn(1))return t.clone();var i=new Array(16);i[0]=new n(1).toRed(this),i[1]=t;for(var r=2;r<i.length;r++)i[r]=this.mul(i[r-1],t);var s=i[0],o=0,a=0,h=e.bitLength()%26;for(0===h&&(h=26),r=e.length-1;r>=0;r--){for(var u=e.words[r],l=h-1;l>=0;l--){var c=u>>l&1;s!==i[0]&&(s=this.sqr(s)),0!==c||0!==o?(o<<=1,o|=c,(4==++a||0===r&&0===l)&&(s=this.mul(s,i[o]),a=0,o=0)):a=0}h=26}return s},b.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},b.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},n.mont=function(t){return new A(t)},r(A,b),A.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},A.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},A.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var i=t.imul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),n=i.isub(r).iushrn(this.shift),s=n;return n.cmp(this.m)>=0?s=n.isub(this.m):n.cmpn(0)<0&&(s=n.iadd(this.m)),s._forceRed(this)},A.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new n(0)._forceRed(this);var i=t.mul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(r).iushrn(this.shift),o=s;return s.cmp(this.m)>=0?o=s.isub(this.m):s.cmpn(0)<0&&(o=s.iadd(this.m)),o._forceRed(this)},A.prototype.invm=function(t){return this.imod(t._invmp(this.m).mul(this.r2))._forceRed(this)}}(t,mt)}),yt=function(t){return"string"!=typeof t?t:function(t){if("string"!=typeof t)throw new Error("[is-hex-prefixed] value must be type 'string', is currently type "+typeof t+", while checking isHexPrefixed.");return"0x"===t.slice(0,2)}(t)?t.slice(2):t},Mt=function(t){if("string"==typeof t||"number"==typeof t){var e=new vt(1),i=String(t).toLowerCase().trim(),r="0x"===i.substr(0,2)||"-0x"===i.substr(0,3),n=yt(i);if("-"===n.substr(0,1)&&(n=yt(n.slice(1)),e=new vt(-1,10)),!(n=""===n?"0":n).match(/^-?[0-9]+$/)&&n.match(/^[0-9A-Fa-f]+$/)||n.match(/^[a-fA-F]+$/)||!0===r&&n.match(/^[0-9A-Fa-f]+$/))return new vt(n,16).mul(e);if((n.match(/^-?[0-9]+$/)||""===n)&&!1===r)return new vt(n,10).mul(e)}else if("object"==typeof t&&t.toString&&!t.pop&&!t.push&&t.toString(10).match(/^-?[0-9]+$/)&&(t.mul||t.dividedToIntegerBy))return new vt(t.toString(10),10);throw new Error("[number-to-bn] while converting number "+JSON.stringify(t)+" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.")},bt=new wt(0),At=new wt(-1),xt={noether:"0",wei:"1",kwei:"1000",Kwei:"1000",babbage:"1000",femtoether:"1000",mwei:"1000000",Mwei:"1000000",lovelace:"1000000",picoether:"1000000",gwei:"1000000000",Gwei:"1000000000",shannon:"1000000000",nanoether:"1000000000",nano:"1000000000",szabo:"1000000000000",microether:"1000000000000",micro:"1000000000000",finney:"1000000000000000",milliether:"1000000000000000",milli:"1000000000000000",ether:"1000000000000000000",kether:"1000000000000000000000",grand:"1000000000000000000000",mether:"1000000000000000000000000",gether:"1000000000000000000000000000",tether:"1000000000000000000000000000000"};function Et(t){var e=t?t.toLowerCase():"ether",i=xt[e];if("string"!=typeof i)throw new Error("[ethjs-unit] the unit provided "+t+" doesn't exists, please use the one of the following units "+JSON.stringify(xt,null,2));return new wt(i,10)}function Rt(t){if("string"==typeof t){if(!t.match(/^-?[0-9.]+$/))throw new Error("while converting number to string, invalid number value '"+t+"', should be a number matching (^-?[0-9.]+).");return t}if("number"==typeof t)return String(t);if("object"==typeof t&&t.toString&&(t.toTwos||t.dividedToIntegerBy))return t.toPrecision?String(t.toPrecision()):t.toString(10);throw new Error("while converting number to string, invalid number value '"+t+"' type "+typeof t+".")}var kt={unitMap:xt,numberToString:Rt,getValueOfUnit:Et,fromWei:function(t,e,i){var r=Mt(t),n=r.lt(bt),s=Et(e),o=xt[e].length-1||1,a=i||{};n&&(r=r.mul(At));for(var h=r.mod(s).toString(10);h.length<o;)h="0"+h;a.pad||(h=h.match(/^([0-9]*[1-9]|0)(0*)/)[1]);var u=r.div(s).toString(10);a.commify&&(u=u.replace(/\B(?=(\d{3})+(?!\d))/g,","));var l=u+("0"==h?"":"."+h);return n&&(l="-"+l),l},toWei:function(t,e){var i=Rt(t),r=Et(e),n=xt[e].length-1||1,s="-"===i.substring(0,1);if(s&&(i=i.substring(1)),"."===i)throw new Error("[ethjs-unit] while converting number "+t+" to wei, invalid value");var o=i.split(".");if(o.length>2)throw new Error("[ethjs-unit] while converting number "+t+" to wei, too many decimal points");var a=o[0],h=o[1];if(a||(a="0"),h||(h="0"),h.length>n)throw new Error("[ethjs-unit] while converting number "+t+" to wei, too many decimal places");for(;h.length<n;)h+="0";a=new wt(a),h=new wt(h);var u=a.mul(r).add(h);return s&&(u=u.mul(At)),new wt(u.toString(10),10)}},St=pt(function(t){!function(t,e){function i(t,e){if(!t)throw new Error(e||"Assertion failed")}function r(t,e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}function n(t,e,i){if(n.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(i=e,e=10),this._init(t||0,e||10,i||"be"))}var s;"object"==typeof t?t.exports=n:e.BN=n,n.BN=n,n.wordSize=26;try{s=o.Buffer}catch(t){}function a(t,e,i){for(var r=0,n=Math.min(t.length,i),s=e;s<n;s++){var o=t.charCodeAt(s)-48;r<<=4,r|=o>=49&&o<=54?o-49+10:o>=17&&o<=22?o-17+10:15&o}return r}function h(t,e,i,r){for(var n=0,s=Math.min(t.length,i),o=e;o<s;o++){var a=t.charCodeAt(o)-48;n*=r,n+=a>=49?a-49+10:a>=17?a-17+10:a}return n}n.isBN=function(t){return t instanceof n||null!==t&&"object"==typeof t&&t.constructor.wordSize===n.wordSize&&Array.isArray(t.words)},n.max=function(t,e){return t.cmp(e)>0?t:e},n.min=function(t,e){return t.cmp(e)<0?t:e},n.prototype._init=function(t,e,r){if("number"==typeof t)return this._initNumber(t,e,r);if("object"==typeof t)return this._initArray(t,e,r);"hex"===e&&(e=16),i(e===(0|e)&&e>=2&&e<=36);var n=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&n++,16===e?this._parseHex(t,n):this._parseBase(t,e,n),"-"===t[0]&&(this.negative=1),this.strip(),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(i(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initArray=function(t,e,r){if(i("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,o,a=0;if("be"===r)for(n=t.length-1,s=0;n>=0;n-=3)this.words[s]|=(o=t[n]|t[n-1]<<8|t[n-2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);else if("le"===r)for(n=0,s=0;n<t.length;n+=3)this.words[s]|=(o=t[n]|t[n+1]<<8|t[n+2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);return this.strip()},n.prototype._parseHex=function(t,e){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var r,n,s=0;for(i=t.length-6,r=0;i>=e;i-=6)n=a(t,i,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303,(s+=24)>=26&&(s-=26,r++);i+6!==e&&(n=a(t,e,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303),this.strip()},n.prototype._parseBase=function(t,e,i){this.words=[0],this.length=1;for(var r=0,n=1;n<=67108863;n*=e)r++;r--,n=n/e|0;for(var s=t.length-i,o=s%r,a=Math.min(s,s-o)+i,u=0,l=i;l<a;l+=r)u=h(t,l,l+r,e),this.imuln(n),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(0!==o){var c=1;for(u=h(t,l,t.length,e),l=0;l<o;l++)c*=e;this.imuln(c),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}},n.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},n.prototype.clone=function(){var t=new n(null);return this.copy(t),t},n.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},n.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},n.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},n.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var u=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],l=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],c=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function d(t,e,i){i.negative=e.negative^t.negative;var r=t.length+e.length|0;i.length=r,r=r-1|0;var n=0|t.words[0],s=0|e.words[0],o=n*s,a=o/67108864|0;i.words[0]=67108863&o;for(var h=1;h<r;h++){for(var u=a>>>26,l=67108863&a,c=Math.min(h,e.length-1),d=Math.max(0,h-t.length+1);d<=c;d++)u+=(o=(n=0|t.words[h-d|0])*(s=0|e.words[d])+l)/67108864|0,l=67108863&o;i.words[h]=0|l,a=0|u}return 0!==a?i.words[h]=0|a:i.length--,i.strip()}n.prototype.toString=function(t,e){var r;if(e=0|e||1,16===(t=t||10)||"hex"===t){r="";for(var n=0,s=0,o=0;o<this.length;o++){var a=this.words[o],h=(16777215&(a<<n|s)).toString(16);r=0!=(s=a>>>24-n&16777215)||o!==this.length-1?u[6-h.length]+h+r:h+r,(n+=2)>=26&&(n-=26,o--)}for(0!==s&&(r=s.toString(16)+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}if(t===(0|t)&&t>=2&&t<=36){var d=l[t],f=c[t];r="";var m=this.clone();for(m.negative=0;!m.isZero();){var p=m.modn(f).toString(t);r=(m=m.idivn(f)).isZero()?p+r:u[d-p.length]+p+r}for(this.isZero()&&(r="0"+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}i(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&i(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},n.prototype.toJSON=function(){return this.toString(16)},n.prototype.toBuffer=function(t,e){return i(void 0!==s),this.toArrayLike(s,t,e)},n.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},n.prototype.toArrayLike=function(t,e,r){var n=this.byteLength(),s=r||Math.max(1,n);i(n<=s,"byte array longer than desired length"),i(s>0,"Requested array length <= 0"),this.strip();var o,a,h="le"===e,u=new t(s),l=this.clone();if(h){for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[a]=o;for(;a<s;a++)u[a]=0}else{for(a=0;a<s-n;a++)u[a]=0;for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[s-a-1]=o}return u},n.prototype._countBits=Math.clz32?function(t){return 32-Math.clz32(t)}:function(t){var e=t,i=0;return e>=4096&&(i+=13,e>>>=13),e>=64&&(i+=7,e>>>=7),e>=8&&(i+=4,e>>>=4),e>=2&&(i+=2,e>>>=2),i+e},n.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,i=0;return 0==(8191&e)&&(i+=13,e>>>=13),0==(127&e)&&(i+=7,e>>>=7),0==(15&e)&&(i+=4,e>>>=4),0==(3&e)&&(i+=2,e>>>=2),0==(1&e)&&i++,i},n.prototype.bitLength=function(){var t=this._countBits(this.words[this.length-1]);return 26*(this.length-1)+t},n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var i=this._zeroBits(this.words[e]);if(t+=i,26!==i)break}return t},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},n.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return 0!==this.negative},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this.strip()},n.prototype.ior=function(t){return i(0==(this.negative|t.negative)),this.iuor(t)},n.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},n.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},n.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var i=0;i<e.length;i++)this.words[i]=this.words[i]&t.words[i];return this.length=e.length,this.strip()},n.prototype.iand=function(t){return i(0==(this.negative|t.negative)),this.iuand(t)},n.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},n.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},n.prototype.iuxor=function(t){var e,i;this.length>t.length?(e=this,i=t):(e=t,i=this);for(var r=0;r<i.length;r++)this.words[r]=e.words[r]^i.words[r];if(this!==e)for(;r<e.length;r++)this.words[r]=e.words[r];return this.length=e.length,this.strip()},n.prototype.ixor=function(t){return i(0==(this.negative|t.negative)),this.iuxor(t)},n.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},n.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},n.prototype.inotn=function(t){i("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=67108863&~this.words[n];return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this.strip()},n.prototype.notn=function(t){return this.clone().inotn(t)},n.prototype.setn=function(t,e){i("number"==typeof t&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),this.words[r]=e?this.words[r]|1<<n:this.words[r]&~(1<<n),this.strip()},n.prototype.iadd=function(t){var e,i,r;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(i=this,r=t):(i=t,r=this);for(var n=0,s=0;s<r.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+(0|r.words[s])+n),n=e>>>26;for(;0!==n&&s<i.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+n),n=e>>>26;if(this.length=i.length,0!==n)this.words[this.length]=n,this.length++;else if(i!==this)for(;s<i.length;s++)this.words[s]=i.words[s];return this},n.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},n.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var i,r,n=this.cmp(t);if(0===n)return this.negative=0,this.length=1,this.words[0]=0,this;n>0?(i=this,r=t):(i=t,r=this);for(var s=0,o=0;o<r.length;o++)s=(e=(0|i.words[o])-(0|r.words[o])+s)>>26,this.words[o]=67108863&e;for(;0!==s&&o<i.length;o++)s=(e=(0|i.words[o])+s)>>26,this.words[o]=67108863&e;if(0===s&&o<i.length&&i!==this)for(;o<i.length;o++)this.words[o]=i.words[o];return this.length=Math.max(this.length,o),i!==this&&(this.negative=1),this.strip()},n.prototype.sub=function(t){return this.clone().isub(t)};var f=function(t,e,i){var r,n,s,o=t.words,a=e.words,h=i.words,u=0,l=0|o[0],c=8191&l,d=l>>>13,f=0|o[1],m=8191&f,p=f>>>13,g=0|o[2],w=8191&g,v=g>>>13,y=0|o[3],M=8191&y,b=y>>>13,A=0|o[4],x=8191&A,E=A>>>13,R=0|o[5],k=8191&R,S=R>>>13,I=0|o[6],T=8191&I,O=I>>>13,B=0|o[7],C=8191&B,_=B>>>13,D=0|o[8],P=8191&D,N=D>>>13,W=0|o[9],L=8191&W,F=W>>>13,U=0|a[0],$=8191&U,j=U>>>13,q=0|a[1],G=8191&q,z=q>>>13,H=0|a[2],Z=8191&H,V=H>>>13,J=0|a[3],K=8191&J,X=J>>>13,Q=0|a[4],Y=8191&Q,tt=Q>>>13,et=0|a[5],it=8191&et,rt=et>>>13,nt=0|a[6],st=8191&nt,ot=nt>>>13,at=0|a[7],ht=8191&at,ut=at>>>13,lt=0|a[8],ct=8191&lt,dt=lt>>>13,ft=0|a[9],mt=8191&ft,pt=ft>>>13;i.negative=t.negative^e.negative,i.length=19;var gt=(u+(r=Math.imul(c,$))|0)+((8191&(n=(n=Math.imul(c,j))+Math.imul(d,$)|0))<<13)|0;u=((s=Math.imul(d,j))+(n>>>13)|0)+(gt>>>26)|0,gt&=67108863,r=Math.imul(m,$),n=(n=Math.imul(m,j))+Math.imul(p,$)|0,s=Math.imul(p,j);var wt=(u+(r=r+Math.imul(c,G)|0)|0)+((8191&(n=(n=n+Math.imul(c,z)|0)+Math.imul(d,G)|0))<<13)|0;u=((s=s+Math.imul(d,z)|0)+(n>>>13)|0)+(wt>>>26)|0,wt&=67108863,r=Math.imul(w,$),n=(n=Math.imul(w,j))+Math.imul(v,$)|0,s=Math.imul(v,j),r=r+Math.imul(m,G)|0,n=(n=n+Math.imul(m,z)|0)+Math.imul(p,G)|0,s=s+Math.imul(p,z)|0;var vt=(u+(r=r+Math.imul(c,Z)|0)|0)+((8191&(n=(n=n+Math.imul(c,V)|0)+Math.imul(d,Z)|0))<<13)|0;u=((s=s+Math.imul(d,V)|0)+(n>>>13)|0)+(vt>>>26)|0,vt&=67108863,r=Math.imul(M,$),n=(n=Math.imul(M,j))+Math.imul(b,$)|0,s=Math.imul(b,j),r=r+Math.imul(w,G)|0,n=(n=n+Math.imul(w,z)|0)+Math.imul(v,G)|0,s=s+Math.imul(v,z)|0,r=r+Math.imul(m,Z)|0,n=(n=n+Math.imul(m,V)|0)+Math.imul(p,Z)|0,s=s+Math.imul(p,V)|0;var yt=(u+(r=r+Math.imul(c,K)|0)|0)+((8191&(n=(n=n+Math.imul(c,X)|0)+Math.imul(d,K)|0))<<13)|0;u=((s=s+Math.imul(d,X)|0)+(n>>>13)|0)+(yt>>>26)|0,yt&=67108863,r=Math.imul(x,$),n=(n=Math.imul(x,j))+Math.imul(E,$)|0,s=Math.imul(E,j),r=r+Math.imul(M,G)|0,n=(n=n+Math.imul(M,z)|0)+Math.imul(b,G)|0,s=s+Math.imul(b,z)|0,r=r+Math.imul(w,Z)|0,n=(n=n+Math.imul(w,V)|0)+Math.imul(v,Z)|0,s=s+Math.imul(v,V)|0,r=r+Math.imul(m,K)|0,n=(n=n+Math.imul(m,X)|0)+Math.imul(p,K)|0,s=s+Math.imul(p,X)|0;var Mt=(u+(r=r+Math.imul(c,Y)|0)|0)+((8191&(n=(n=n+Math.imul(c,tt)|0)+Math.imul(d,Y)|0))<<13)|0;u=((s=s+Math.imul(d,tt)|0)+(n>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,r=Math.imul(k,$),n=(n=Math.imul(k,j))+Math.imul(S,$)|0,s=Math.imul(S,j),r=r+Math.imul(x,G)|0,n=(n=n+Math.imul(x,z)|0)+Math.imul(E,G)|0,s=s+Math.imul(E,z)|0,r=r+Math.imul(M,Z)|0,n=(n=n+Math.imul(M,V)|0)+Math.imul(b,Z)|0,s=s+Math.imul(b,V)|0,r=r+Math.imul(w,K)|0,n=(n=n+Math.imul(w,X)|0)+Math.imul(v,K)|0,s=s+Math.imul(v,X)|0,r=r+Math.imul(m,Y)|0,n=(n=n+Math.imul(m,tt)|0)+Math.imul(p,Y)|0,s=s+Math.imul(p,tt)|0;var bt=(u+(r=r+Math.imul(c,it)|0)|0)+((8191&(n=(n=n+Math.imul(c,rt)|0)+Math.imul(d,it)|0))<<13)|0;u=((s=s+Math.imul(d,rt)|0)+(n>>>13)|0)+(bt>>>26)|0,bt&=67108863,r=Math.imul(T,$),n=(n=Math.imul(T,j))+Math.imul(O,$)|0,s=Math.imul(O,j),r=r+Math.imul(k,G)|0,n=(n=n+Math.imul(k,z)|0)+Math.imul(S,G)|0,s=s+Math.imul(S,z)|0,r=r+Math.imul(x,Z)|0,n=(n=n+Math.imul(x,V)|0)+Math.imul(E,Z)|0,s=s+Math.imul(E,V)|0,r=r+Math.imul(M,K)|0,n=(n=n+Math.imul(M,X)|0)+Math.imul(b,K)|0,s=s+Math.imul(b,X)|0,r=r+Math.imul(w,Y)|0,n=(n=n+Math.imul(w,tt)|0)+Math.imul(v,Y)|0,s=s+Math.imul(v,tt)|0,r=r+Math.imul(m,it)|0,n=(n=n+Math.imul(m,rt)|0)+Math.imul(p,it)|0,s=s+Math.imul(p,rt)|0;var At=(u+(r=r+Math.imul(c,st)|0)|0)+((8191&(n=(n=n+Math.imul(c,ot)|0)+Math.imul(d,st)|0))<<13)|0;u=((s=s+Math.imul(d,ot)|0)+(n>>>13)|0)+(At>>>26)|0,At&=67108863,r=Math.imul(C,$),n=(n=Math.imul(C,j))+Math.imul(_,$)|0,s=Math.imul(_,j),r=r+Math.imul(T,G)|0,n=(n=n+Math.imul(T,z)|0)+Math.imul(O,G)|0,s=s+Math.imul(O,z)|0,r=r+Math.imul(k,Z)|0,n=(n=n+Math.imul(k,V)|0)+Math.imul(S,Z)|0,s=s+Math.imul(S,V)|0,r=r+Math.imul(x,K)|0,n=(n=n+Math.imul(x,X)|0)+Math.imul(E,K)|0,s=s+Math.imul(E,X)|0,r=r+Math.imul(M,Y)|0,n=(n=n+Math.imul(M,tt)|0)+Math.imul(b,Y)|0,s=s+Math.imul(b,tt)|0,r=r+Math.imul(w,it)|0,n=(n=n+Math.imul(w,rt)|0)+Math.imul(v,it)|0,s=s+Math.imul(v,rt)|0,r=r+Math.imul(m,st)|0,n=(n=n+Math.imul(m,ot)|0)+Math.imul(p,st)|0,s=s+Math.imul(p,ot)|0;var xt=(u+(r=r+Math.imul(c,ht)|0)|0)+((8191&(n=(n=n+Math.imul(c,ut)|0)+Math.imul(d,ht)|0))<<13)|0;u=((s=s+Math.imul(d,ut)|0)+(n>>>13)|0)+(xt>>>26)|0,xt&=67108863,r=Math.imul(P,$),n=(n=Math.imul(P,j))+Math.imul(N,$)|0,s=Math.imul(N,j),r=r+Math.imul(C,G)|0,n=(n=n+Math.imul(C,z)|0)+Math.imul(_,G)|0,s=s+Math.imul(_,z)|0,r=r+Math.imul(T,Z)|0,n=(n=n+Math.imul(T,V)|0)+Math.imul(O,Z)|0,s=s+Math.imul(O,V)|0,r=r+Math.imul(k,K)|0,n=(n=n+Math.imul(k,X)|0)+Math.imul(S,K)|0,s=s+Math.imul(S,X)|0,r=r+Math.imul(x,Y)|0,n=(n=n+Math.imul(x,tt)|0)+Math.imul(E,Y)|0,s=s+Math.imul(E,tt)|0,r=r+Math.imul(M,it)|0,n=(n=n+Math.imul(M,rt)|0)+Math.imul(b,it)|0,s=s+Math.imul(b,rt)|0,r=r+Math.imul(w,st)|0,n=(n=n+Math.imul(w,ot)|0)+Math.imul(v,st)|0,s=s+Math.imul(v,ot)|0,r=r+Math.imul(m,ht)|0,n=(n=n+Math.imul(m,ut)|0)+Math.imul(p,ht)|0,s=s+Math.imul(p,ut)|0;var Et=(u+(r=r+Math.imul(c,ct)|0)|0)+((8191&(n=(n=n+Math.imul(c,dt)|0)+Math.imul(d,ct)|0))<<13)|0;u=((s=s+Math.imul(d,dt)|0)+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,r=Math.imul(L,$),n=(n=Math.imul(L,j))+Math.imul(F,$)|0,s=Math.imul(F,j),r=r+Math.imul(P,G)|0,n=(n=n+Math.imul(P,z)|0)+Math.imul(N,G)|0,s=s+Math.imul(N,z)|0,r=r+Math.imul(C,Z)|0,n=(n=n+Math.imul(C,V)|0)+Math.imul(_,Z)|0,s=s+Math.imul(_,V)|0,r=r+Math.imul(T,K)|0,n=(n=n+Math.imul(T,X)|0)+Math.imul(O,K)|0,s=s+Math.imul(O,X)|0,r=r+Math.imul(k,Y)|0,n=(n=n+Math.imul(k,tt)|0)+Math.imul(S,Y)|0,s=s+Math.imul(S,tt)|0,r=r+Math.imul(x,it)|0,n=(n=n+Math.imul(x,rt)|0)+Math.imul(E,it)|0,s=s+Math.imul(E,rt)|0,r=r+Math.imul(M,st)|0,n=(n=n+Math.imul(M,ot)|0)+Math.imul(b,st)|0,s=s+Math.imul(b,ot)|0,r=r+Math.imul(w,ht)|0,n=(n=n+Math.imul(w,ut)|0)+Math.imul(v,ht)|0,s=s+Math.imul(v,ut)|0,r=r+Math.imul(m,ct)|0,n=(n=n+Math.imul(m,dt)|0)+Math.imul(p,ct)|0,s=s+Math.imul(p,dt)|0;var Rt=(u+(r=r+Math.imul(c,mt)|0)|0)+((8191&(n=(n=n+Math.imul(c,pt)|0)+Math.imul(d,mt)|0))<<13)|0;u=((s=s+Math.imul(d,pt)|0)+(n>>>13)|0)+(Rt>>>26)|0,Rt&=67108863,r=Math.imul(L,G),n=(n=Math.imul(L,z))+Math.imul(F,G)|0,s=Math.imul(F,z),r=r+Math.imul(P,Z)|0,n=(n=n+Math.imul(P,V)|0)+Math.imul(N,Z)|0,s=s+Math.imul(N,V)|0,r=r+Math.imul(C,K)|0,n=(n=n+Math.imul(C,X)|0)+Math.imul(_,K)|0,s=s+Math.imul(_,X)|0,r=r+Math.imul(T,Y)|0,n=(n=n+Math.imul(T,tt)|0)+Math.imul(O,Y)|0,s=s+Math.imul(O,tt)|0,r=r+Math.imul(k,it)|0,n=(n=n+Math.imul(k,rt)|0)+Math.imul(S,it)|0,s=s+Math.imul(S,rt)|0,r=r+Math.imul(x,st)|0,n=(n=n+Math.imul(x,ot)|0)+Math.imul(E,st)|0,s=s+Math.imul(E,ot)|0,r=r+Math.imul(M,ht)|0,n=(n=n+Math.imul(M,ut)|0)+Math.imul(b,ht)|0,s=s+Math.imul(b,ut)|0,r=r+Math.imul(w,ct)|0,n=(n=n+Math.imul(w,dt)|0)+Math.imul(v,ct)|0,s=s+Math.imul(v,dt)|0;var kt=(u+(r=r+Math.imul(m,mt)|0)|0)+((8191&(n=(n=n+Math.imul(m,pt)|0)+Math.imul(p,mt)|0))<<13)|0;u=((s=s+Math.imul(p,pt)|0)+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,r=Math.imul(L,Z),n=(n=Math.imul(L,V))+Math.imul(F,Z)|0,s=Math.imul(F,V),r=r+Math.imul(P,K)|0,n=(n=n+Math.imul(P,X)|0)+Math.imul(N,K)|0,s=s+Math.imul(N,X)|0,r=r+Math.imul(C,Y)|0,n=(n=n+Math.imul(C,tt)|0)+Math.imul(_,Y)|0,s=s+Math.imul(_,tt)|0,r=r+Math.imul(T,it)|0,n=(n=n+Math.imul(T,rt)|0)+Math.imul(O,it)|0,s=s+Math.imul(O,rt)|0,r=r+Math.imul(k,st)|0,n=(n=n+Math.imul(k,ot)|0)+Math.imul(S,st)|0,s=s+Math.imul(S,ot)|0,r=r+Math.imul(x,ht)|0,n=(n=n+Math.imul(x,ut)|0)+Math.imul(E,ht)|0,s=s+Math.imul(E,ut)|0,r=r+Math.imul(M,ct)|0,n=(n=n+Math.imul(M,dt)|0)+Math.imul(b,ct)|0,s=s+Math.imul(b,dt)|0;var St=(u+(r=r+Math.imul(w,mt)|0)|0)+((8191&(n=(n=n+Math.imul(w,pt)|0)+Math.imul(v,mt)|0))<<13)|0;u=((s=s+Math.imul(v,pt)|0)+(n>>>13)|0)+(St>>>26)|0,St&=67108863,r=Math.imul(L,K),n=(n=Math.imul(L,X))+Math.imul(F,K)|0,s=Math.imul(F,X),r=r+Math.imul(P,Y)|0,n=(n=n+Math.imul(P,tt)|0)+Math.imul(N,Y)|0,s=s+Math.imul(N,tt)|0,r=r+Math.imul(C,it)|0,n=(n=n+Math.imul(C,rt)|0)+Math.imul(_,it)|0,s=s+Math.imul(_,rt)|0,r=r+Math.imul(T,st)|0,n=(n=n+Math.imul(T,ot)|0)+Math.imul(O,st)|0,s=s+Math.imul(O,ot)|0,r=r+Math.imul(k,ht)|0,n=(n=n+Math.imul(k,ut)|0)+Math.imul(S,ht)|0,s=s+Math.imul(S,ut)|0,r=r+Math.imul(x,ct)|0,n=(n=n+Math.imul(x,dt)|0)+Math.imul(E,ct)|0,s=s+Math.imul(E,dt)|0;var It=(u+(r=r+Math.imul(M,mt)|0)|0)+((8191&(n=(n=n+Math.imul(M,pt)|0)+Math.imul(b,mt)|0))<<13)|0;u=((s=s+Math.imul(b,pt)|0)+(n>>>13)|0)+(It>>>26)|0,It&=67108863,r=Math.imul(L,Y),n=(n=Math.imul(L,tt))+Math.imul(F,Y)|0,s=Math.imul(F,tt),r=r+Math.imul(P,it)|0,n=(n=n+Math.imul(P,rt)|0)+Math.imul(N,it)|0,s=s+Math.imul(N,rt)|0,r=r+Math.imul(C,st)|0,n=(n=n+Math.imul(C,ot)|0)+Math.imul(_,st)|0,s=s+Math.imul(_,ot)|0,r=r+Math.imul(T,ht)|0,n=(n=n+Math.imul(T,ut)|0)+Math.imul(O,ht)|0,s=s+Math.imul(O,ut)|0,r=r+Math.imul(k,ct)|0,n=(n=n+Math.imul(k,dt)|0)+Math.imul(S,ct)|0,s=s+Math.imul(S,dt)|0;var Tt=(u+(r=r+Math.imul(x,mt)|0)|0)+((8191&(n=(n=n+Math.imul(x,pt)|0)+Math.imul(E,mt)|0))<<13)|0;u=((s=s+Math.imul(E,pt)|0)+(n>>>13)|0)+(Tt>>>26)|0,Tt&=67108863,r=Math.imul(L,it),n=(n=Math.imul(L,rt))+Math.imul(F,it)|0,s=Math.imul(F,rt),r=r+Math.imul(P,st)|0,n=(n=n+Math.imul(P,ot)|0)+Math.imul(N,st)|0,s=s+Math.imul(N,ot)|0,r=r+Math.imul(C,ht)|0,n=(n=n+Math.imul(C,ut)|0)+Math.imul(_,ht)|0,s=s+Math.imul(_,ut)|0,r=r+Math.imul(T,ct)|0,n=(n=n+Math.imul(T,dt)|0)+Math.imul(O,ct)|0,s=s+Math.imul(O,dt)|0;var Ot=(u+(r=r+Math.imul(k,mt)|0)|0)+((8191&(n=(n=n+Math.imul(k,pt)|0)+Math.imul(S,mt)|0))<<13)|0;u=((s=s+Math.imul(S,pt)|0)+(n>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,r=Math.imul(L,st),n=(n=Math.imul(L,ot))+Math.imul(F,st)|0,s=Math.imul(F,ot),r=r+Math.imul(P,ht)|0,n=(n=n+Math.imul(P,ut)|0)+Math.imul(N,ht)|0,s=s+Math.imul(N,ut)|0,r=r+Math.imul(C,ct)|0,n=(n=n+Math.imul(C,dt)|0)+Math.imul(_,ct)|0,s=s+Math.imul(_,dt)|0;var Bt=(u+(r=r+Math.imul(T,mt)|0)|0)+((8191&(n=(n=n+Math.imul(T,pt)|0)+Math.imul(O,mt)|0))<<13)|0;u=((s=s+Math.imul(O,pt)|0)+(n>>>13)|0)+(Bt>>>26)|0,Bt&=67108863,r=Math.imul(L,ht),n=(n=Math.imul(L,ut))+Math.imul(F,ht)|0,s=Math.imul(F,ut),r=r+Math.imul(P,ct)|0,n=(n=n+Math.imul(P,dt)|0)+Math.imul(N,ct)|0,s=s+Math.imul(N,dt)|0;var Ct=(u+(r=r+Math.imul(C,mt)|0)|0)+((8191&(n=(n=n+Math.imul(C,pt)|0)+Math.imul(_,mt)|0))<<13)|0;u=((s=s+Math.imul(_,pt)|0)+(n>>>13)|0)+(Ct>>>26)|0,Ct&=67108863,r=Math.imul(L,ct),n=(n=Math.imul(L,dt))+Math.imul(F,ct)|0,s=Math.imul(F,dt);var _t=(u+(r=r+Math.imul(P,mt)|0)|0)+((8191&(n=(n=n+Math.imul(P,pt)|0)+Math.imul(N,mt)|0))<<13)|0;u=((s=s+Math.imul(N,pt)|0)+(n>>>13)|0)+(_t>>>26)|0,_t&=67108863;var Dt=(u+(r=Math.imul(L,mt))|0)+((8191&(n=(n=Math.imul(L,pt))+Math.imul(F,mt)|0))<<13)|0;return u=((s=Math.imul(F,pt))+(n>>>13)|0)+(Dt>>>26)|0,Dt&=67108863,h[0]=gt,h[1]=wt,h[2]=vt,h[3]=yt,h[4]=Mt,h[5]=bt,h[6]=At,h[7]=xt,h[8]=Et,h[9]=Rt,h[10]=kt,h[11]=St,h[12]=It,h[13]=Tt,h[14]=Ot,h[15]=Bt,h[16]=Ct,h[17]=_t,h[18]=Dt,0!==u&&(h[19]=u,i.length++),i};function m(t,e,i){return(new p).mulp(t,e,i)}function p(t,e){this.x=t,this.y=e}Math.imul||(f=d),n.prototype.mulTo=function(t,e){var i=this.length+t.length;return 10===this.length&&10===t.length?f(this,t,e):i<63?d(this,t,e):i<1024?function(t,e,i){i.negative=e.negative^t.negative,i.length=t.length+e.length;for(var r=0,n=0,s=0;s<i.length-1;s++){var o=n;n=0;for(var a=67108863&r,h=Math.min(s,e.length-1),u=Math.max(0,s-t.length+1);u<=h;u++){var l=(0|t.words[s-u])*(0|e.words[u]),c=67108863&l;a=67108863&(c=c+a|0),n+=(o=(o=o+(l/67108864|0)|0)+(c>>>26)|0)>>>26,o&=67108863}i.words[s]=a,r=o,o=n}return 0!==r?i.words[s]=r:i.length--,i.strip()}(this,t,e):m(this,t,e)},p.prototype.makeRBT=function(t){for(var e=new Array(t),i=n.prototype._countBits(t)-1,r=0;r<t;r++)e[r]=this.revBin(r,i,t);return e},p.prototype.revBin=function(t,e,i){if(0===t||t===i-1)return t;for(var r=0,n=0;n<e;n++)r|=(1&t)<<e-n-1,t>>=1;return r},p.prototype.permute=function(t,e,i,r,n,s){for(var o=0;o<s;o++)r[o]=e[t[o]],n[o]=i[t[o]]},p.prototype.transform=function(t,e,i,r,n,s){this.permute(s,t,e,i,r,n);for(var o=1;o<n;o<<=1)for(var a=o<<1,h=Math.cos(2*Math.PI/a),u=Math.sin(2*Math.PI/a),l=0;l<n;l+=a)for(var c=h,d=u,f=0;f<o;f++){var m=i[l+f],p=r[l+f],g=i[l+f+o],w=r[l+f+o],v=c*g-d*w;w=c*w+d*g,i[l+f]=m+(g=v),r[l+f]=p+w,i[l+f+o]=m-g,r[l+f+o]=p-w,f!==a&&(v=h*c-u*d,d=h*d+u*c,c=v)}},p.prototype.guessLen13b=function(t,e){var i=1|Math.max(e,t),r=1&i,n=0;for(i=i/2|0;i;i>>>=1)n++;return 1<<n+1+r},p.prototype.conjugate=function(t,e,i){if(!(i<=1))for(var r=0;r<i/2;r++){var n=t[r];t[r]=t[i-r-1],t[i-r-1]=n,n=e[r],e[r]=-e[i-r-1],e[i-r-1]=-n}},p.prototype.normalize13b=function(t,e){for(var i=0,r=0;r<e/2;r++){var n=8192*Math.round(t[2*r+1]/e)+Math.round(t[2*r]/e)+i;t[r]=67108863&n,i=n<67108864?0:n/67108864|0}return t},p.prototype.convert13b=function(t,e,r,n){for(var s=0,o=0;o<e;o++)r[2*o]=8191&(s+=0|t[o]),r[2*o+1]=8191&(s>>>=13),s>>>=13;for(o=2*e;o<n;++o)r[o]=0;i(0===s),i(0==(-8192&s))},p.prototype.stub=function(t){for(var e=new Array(t),i=0;i<t;i++)e[i]=0;return e},p.prototype.mulp=function(t,e,i){var r=2*this.guessLen13b(t.length,e.length),n=this.makeRBT(r),s=this.stub(r),o=new Array(r),a=new Array(r),h=new Array(r),u=new Array(r),l=new Array(r),c=new Array(r),d=i.words;d.length=r,this.convert13b(t.words,t.length,o,r),this.convert13b(e.words,e.length,u,r),this.transform(o,s,a,h,r,n),this.transform(u,s,l,c,r,n);for(var f=0;f<r;f++){var m=a[f]*l[f]-h[f]*c[f];h[f]=a[f]*c[f]+h[f]*l[f],a[f]=m}return this.conjugate(a,h,r),this.transform(a,h,d,s,r,n),this.conjugate(d,s,r),this.normalize13b(d,r),i.negative=t.negative^e.negative,i.length=t.length+e.length,i.strip()},n.prototype.mul=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},n.prototype.mulf=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),m(this,t,e)},n.prototype.imul=function(t){return this.clone().mulTo(t,this)},n.prototype.imuln=function(t){i("number"==typeof t),i(t<67108864);for(var e=0,r=0;r<this.length;r++){var n=(0|this.words[r])*t,s=(67108863&n)+(67108863&e);e>>=26,e+=n/67108864|0,e+=s>>>26,this.words[r]=67108863&s}return 0!==e&&(this.words[r]=e,this.length++),this},n.prototype.muln=function(t){return this.clone().imuln(t)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),i=0;i<e.length;i++){var r=i%26;e[i]=(t.words[i/26|0]&1<<r)>>>r}return e}(t);if(0===e.length)return new n(1);for(var i=this,r=0;r<e.length&&0===e[r];r++,i=i.sqr());if(++r<e.length)for(var s=i.sqr();r<e.length;r++,s=s.sqr())0!==e[r]&&(i=i.mul(s));return i},n.prototype.iushln=function(t){i("number"==typeof t&&t>=0);var e,r=t%26,n=(t-r)/26,s=67108863>>>26-r<<26-r;if(0!==r){var o=0;for(e=0;e<this.length;e++){var a=this.words[e]&s;this.words[e]=(0|this.words[e])-a<<r|o,o=a>>>26-r}o&&(this.words[e]=o,this.length++)}if(0!==n){for(e=this.length-1;e>=0;e--)this.words[e+n]=this.words[e];for(e=0;e<n;e++)this.words[e]=0;this.length+=n}return this.strip()},n.prototype.ishln=function(t){return i(0===this.negative),this.iushln(t)},n.prototype.iushrn=function(t,e,r){var n;i("number"==typeof t&&t>=0),n=e?(e-e%26)/26:0;var s=t%26,o=Math.min((t-s)/26,this.length),a=67108863^67108863>>>s<<s,h=r;if(n-=o,n=Math.max(0,n),h){for(var u=0;u<o;u++)h.words[u]=this.words[u];h.length=o}if(0===o);else if(this.length>o)for(this.length-=o,u=0;u<this.length;u++)this.words[u]=this.words[u+o];else this.words[0]=0,this.length=1;var l=0;for(u=this.length-1;u>=0&&(0!==l||u>=n);u--){var c=0|this.words[u];this.words[u]=l<<26-s|c>>>s,l=c&a}return h&&0!==l&&(h.words[h.length++]=l),0===this.length&&(this.words[0]=0,this.length=1),this.strip()},n.prototype.ishrn=function(t,e,r){return i(0===this.negative),this.iushrn(t,e,r)},n.prototype.shln=function(t){return this.clone().ishln(t)},n.prototype.ushln=function(t){return this.clone().iushln(t)},n.prototype.shrn=function(t){return this.clone().ishrn(t)},n.prototype.ushrn=function(t){return this.clone().iushrn(t)},n.prototype.testn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return!(this.length<=r||!(this.words[r]&1<<e))},n.prototype.imaskn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return i(0===this.negative,"imaskn works only with positive numbers"),this.length<=r?this:(0!==e&&r++,this.length=Math.min(r,this.length),0!==e&&(this.words[this.length-1]&=67108863^67108863>>>e<<e),this.strip())},n.prototype.maskn=function(t){return this.clone().imaskn(t)},n.prototype.iaddn=function(t){return i("number"==typeof t),i(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},n.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},n.prototype.isubn=function(t){if(i("number"==typeof t),i(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this.strip()},n.prototype.addn=function(t){return this.clone().iaddn(t)},n.prototype.subn=function(t){return this.clone().isubn(t)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(t,e,r){var n,s;this._expand(t.length+r);var o=0;for(n=0;n<t.length;n++){s=(0|this.words[n+r])+o;var a=(0|t.words[n])*e;o=((s-=67108863&a)>>26)-(a/67108864|0),this.words[n+r]=67108863&s}for(;n<this.length-r;n++)o=(s=(0|this.words[n+r])+o)>>26,this.words[n+r]=67108863&s;if(0===o)return this.strip();for(i(-1===o),o=0,n=0;n<this.length;n++)o=(s=-(0|this.words[n])+o)>>26,this.words[n]=67108863&s;return this.negative=1,this.strip()},n.prototype._wordDiv=function(t,e){var i,r=this.clone(),s=t,o=0|s.words[s.length-1];0!=(i=26-this._countBits(o))&&(s=s.ushln(i),r.iushln(i),o=0|s.words[s.length-1]);var a,h=r.length-s.length;if("mod"!==e){(a=new n(null)).length=h+1,a.words=new Array(a.length);for(var u=0;u<a.length;u++)a.words[u]=0}var l=r.clone()._ishlnsubmul(s,1,h);0===l.negative&&(r=l,a&&(a.words[h]=1));for(var c=h-1;c>=0;c--){var d=67108864*(0|r.words[s.length+c])+(0|r.words[s.length+c-1]);for(d=Math.min(d/o|0,67108863),r._ishlnsubmul(s,d,c);0!==r.negative;)d--,r.negative=0,r._ishlnsubmul(s,1,c),r.isZero()||(r.negative^=1);a&&(a.words[c]=d)}return a&&a.strip(),r.strip(),"div"!==e&&0!==i&&r.iushrn(i),{div:a||null,mod:r}},n.prototype.divmod=function(t,e,r){return i(!t.isZero()),this.isZero()?{div:new n(0),mod:new n(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(s=a.div.neg()),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.iadd(t)),{div:s,mod:o}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(s=a.div.neg()),{div:s,mod:a.mod}):0!=(this.negative&t.negative)?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.isub(t)),{div:a.div,mod:o}):t.length>this.length||this.cmp(t)<0?{div:new n(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new n(this.modn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new n(this.modn(t.words[0]))}:this._wordDiv(t,e);var s,o,a},n.prototype.div=function(t){return this.divmod(t,"div",!1).div},n.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},n.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},n.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var i=0!==e.div.negative?e.mod.isub(t):e.mod,r=t.ushrn(1),n=t.andln(1),s=i.cmp(r);return s<0||1===n&&0===s?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},n.prototype.modn=function(t){i(t<=67108863);for(var e=(1<<26)%t,r=0,n=this.length-1;n>=0;n--)r=(e*r+(0|this.words[n]))%t;return r},n.prototype.idivn=function(t){i(t<=67108863);for(var e=0,r=this.length-1;r>=0;r--){var n=(0|this.words[r])+67108864*e;this.words[r]=n/t|0,e=n%t}return this.strip()},n.prototype.divn=function(t){return this.clone().idivn(t)},n.prototype.egcd=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s=new n(1),o=new n(0),a=new n(0),h=new n(1),u=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++u;for(var l=r.clone(),c=e.clone();!e.isZero();){for(var d=0,f=1;0==(e.words[0]&f)&&d<26;++d,f<<=1);if(d>0)for(e.iushrn(d);d-- >0;)(s.isOdd()||o.isOdd())&&(s.iadd(l),o.isub(c)),s.iushrn(1),o.iushrn(1);for(var m=0,p=1;0==(r.words[0]&p)&&m<26;++m,p<<=1);if(m>0)for(r.iushrn(m);m-- >0;)(a.isOdd()||h.isOdd())&&(a.iadd(l),h.isub(c)),a.iushrn(1),h.iushrn(1);e.cmp(r)>=0?(e.isub(r),s.isub(a),o.isub(h)):(r.isub(e),a.isub(s),h.isub(o))}return{a:a,b:h,gcd:r.iushln(u)}},n.prototype._invmp=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s,o=new n(1),a=new n(0),h=r.clone();e.cmpn(1)>0&&r.cmpn(1)>0;){for(var u=0,l=1;0==(e.words[0]&l)&&u<26;++u,l<<=1);if(u>0)for(e.iushrn(u);u-- >0;)o.isOdd()&&o.iadd(h),o.iushrn(1);for(var c=0,d=1;0==(r.words[0]&d)&&c<26;++c,d<<=1);if(c>0)for(r.iushrn(c);c-- >0;)a.isOdd()&&a.iadd(h),a.iushrn(1);e.cmp(r)>=0?(e.isub(r),o.isub(a)):(r.isub(e),a.isub(o))}return(s=0===e.cmpn(1)?o:a).cmpn(0)<0&&s.iadd(t),s},n.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),i=t.clone();e.negative=0,i.negative=0;for(var r=0;e.isEven()&&i.isEven();r++)e.iushrn(1),i.iushrn(1);for(;;){for(;e.isEven();)e.iushrn(1);for(;i.isEven();)i.iushrn(1);var n=e.cmp(i);if(n<0){var s=e;e=i,i=s}else if(0===n||0===i.cmpn(1))break;e.isub(i)}return i.iushln(r)},n.prototype.invm=function(t){return this.egcd(t).a.umod(t)},n.prototype.isEven=function(){return 0==(1&this.words[0])},n.prototype.isOdd=function(){return 1==(1&this.words[0])},n.prototype.andln=function(t){return this.words[0]&t},n.prototype.bincn=function(t){i("number"==typeof t);var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return this._expand(r+1),this.words[r]|=n,this;for(var s=n,o=r;0!==s&&o<this.length;o++){var a=0|this.words[o];s=(a+=s)>>>26,this.words[o]=a&=67108863}return 0!==s&&(this.words[o]=s,this.length++),this},n.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},n.prototype.cmpn=function(t){var e,r=t<0;if(0!==this.negative&&!r)return-1;if(0===this.negative&&r)return 1;if(this.strip(),this.length>1)e=1;else{r&&(t=-t),i(t<=67108863,"Number is too big");var n=0|this.words[0];e=n===t?0:n<t?-1:1}return 0!==this.negative?0|-e:e},n.prototype.cmp=function(t){if(0!==this.negative&&0===t.negative)return-1;if(0===this.negative&&0!==t.negative)return 1;var e=this.ucmp(t);return 0!==this.negative?0|-e:e},n.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,i=this.length-1;i>=0;i--){var r=0|this.words[i],n=0|t.words[i];if(r!==n){r<n?e=-1:r>n&&(e=1);break}}return e},n.prototype.gtn=function(t){return 1===this.cmpn(t)},n.prototype.gt=function(t){return 1===this.cmp(t)},n.prototype.gten=function(t){return this.cmpn(t)>=0},n.prototype.gte=function(t){return this.cmp(t)>=0},n.prototype.ltn=function(t){return-1===this.cmpn(t)},n.prototype.lt=function(t){return-1===this.cmp(t)},n.prototype.lten=function(t){return this.cmpn(t)<=0},n.prototype.lte=function(t){return this.cmp(t)<=0},n.prototype.eqn=function(t){return 0===this.cmpn(t)},n.prototype.eq=function(t){return 0===this.cmp(t)},n.red=function(t){return new A(t)},n.prototype.toRed=function(t){return i(!this.red,"Already a number in reduction context"),i(0===this.negative,"red works only with positives"),t.convertTo(this)._forceRed(t)},n.prototype.fromRed=function(){return i(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(t){return this.red=t,this},n.prototype.forceRed=function(t){return i(!this.red,"Already a number in reduction context"),this._forceRed(t)},n.prototype.redAdd=function(t){return i(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},n.prototype.redIAdd=function(t){return i(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},n.prototype.redSub=function(t){return i(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},n.prototype.redISub=function(t){return i(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},n.prototype.redShl=function(t){return i(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},n.prototype.redMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},n.prototype.redIMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},n.prototype.redSqr=function(){return i(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return i(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return i(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return i(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return i(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(t){return i(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var g={k256:null,p224:null,p192:null,p25519:null};function w(t,e){this.name=t,this.p=new n(e,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function v(){w.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function y(){w.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function M(){w.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function b(){w.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function A(t){if("string"==typeof t){var e=n._prime(t);this.m=e.p,this.prime=e}else i(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}function x(t){A.call(this,t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}w.prototype._tmp=function(){var t=new n(null);return t.words=new Array(Math.ceil(this.n/13)),t},w.prototype.ireduce=function(t){var e,i=t;do{this.split(i,this.tmp),e=(i=(i=this.imulK(i)).iadd(this.tmp)).bitLength()}while(e>this.n);var r=e<this.n?-1:i.ucmp(this.p);return 0===r?(i.words[0]=0,i.length=1):r>0?i.isub(this.p):void 0!==i.strip?i.strip():i._strip(),i},w.prototype.split=function(t,e){t.iushrn(this.n,0,e)},w.prototype.imulK=function(t){return t.imul(this.k)},r(v,w),v.prototype.split=function(t,e){for(var i=4194303,r=Math.min(t.length,9),n=0;n<r;n++)e.words[n]=t.words[n];if(e.length=r,t.length<=9)return t.words[0]=0,void(t.length=1);var s=t.words[9];for(e.words[e.length++]=s&i,n=10;n<t.length;n++){var o=0|t.words[n];t.words[n-10]=(o&i)<<4|s>>>22,s=o}t.words[n-10]=s>>>=22,t.length-=0===s&&t.length>10?10:9},v.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,i=0;i<t.length;i++){var r=0|t.words[i];t.words[i]=67108863&(e+=977*r),e=64*r+(e/67108864|0)}return 0===t.words[t.length-1]&&(t.length--,0===t.words[t.length-1]&&t.length--),t},r(y,w),r(M,w),r(b,w),b.prototype.imulK=function(t){for(var e=0,i=0;i<t.length;i++){var r=19*(0|t.words[i])+e,n=67108863&r;r>>>=26,t.words[i]=n,e=r}return 0!==e&&(t.words[t.length++]=e),t},n._prime=function(t){if(g[t])return g[t];var e;if("k256"===t)e=new v;else if("p224"===t)e=new y;else if("p192"===t)e=new M;else{if("p25519"!==t)throw new Error("Unknown prime "+t);e=new b}return g[t]=e,e},A.prototype._verify1=function(t){i(0===t.negative,"red works only with positives"),i(t.red,"red works only with red numbers")},A.prototype._verify2=function(t,e){i(0==(t.negative|e.negative),"red works only with positives"),i(t.red&&t.red===e.red,"red works only with red numbers")},A.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):t.umod(this.m)._forceRed(this)},A.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},A.prototype.add=function(t,e){this._verify2(t,e);var i=t.add(e);return i.cmp(this.m)>=0&&i.isub(this.m),i._forceRed(this)},A.prototype.iadd=function(t,e){this._verify2(t,e);var i=t.iadd(e);return i.cmp(this.m)>=0&&i.isub(this.m),i},A.prototype.sub=function(t,e){this._verify2(t,e);var i=t.sub(e);return i.cmpn(0)<0&&i.iadd(this.m),i._forceRed(this)},A.prototype.isub=function(t,e){this._verify2(t,e);var i=t.isub(e);return i.cmpn(0)<0&&i.iadd(this.m),i},A.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},A.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},A.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},A.prototype.isqr=function(t){return this.imul(t,t.clone())},A.prototype.sqr=function(t){return this.mul(t,t)},A.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(i(e%2==1),3===e){var r=this.m.add(new n(1)).iushrn(2);return this.pow(t,r)}for(var s=this.m.subn(1),o=0;!s.isZero()&&0===s.andln(1);)o++,s.iushrn(1);i(!s.isZero());var a=new n(1).toRed(this),h=a.redNeg(),u=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new n(2*l*l).toRed(this);0!==this.pow(l,u).cmp(h);)l.redIAdd(h);for(var c=this.pow(l,s),d=this.pow(t,s.addn(1).iushrn(1)),f=this.pow(t,s),m=o;0!==f.cmp(a);){for(var p=f,g=0;0!==p.cmp(a);g++)p=p.redSqr();i(g<m);var w=this.pow(c,new n(1).iushln(m-g-1));d=d.redMul(w),c=w.redSqr(),f=f.redMul(c),m=g}return d},A.prototype.invm=function(t){var e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},A.prototype.pow=function(t,e){if(e.isZero())return new n(1).toRed(this);if(0===e.cmpn(1))return t.clone();var i=new Array(16);i[0]=new n(1).toRed(this),i[1]=t;for(var r=2;r<i.length;r++)i[r]=this.mul(i[r-1],t);var s=i[0],o=0,a=0,h=e.bitLength()%26;for(0===h&&(h=26),r=e.length-1;r>=0;r--){for(var u=e.words[r],l=h-1;l>=0;l--){var c=u>>l&1;s!==i[0]&&(s=this.sqr(s)),0!==c||0!==o?(o<<=1,o|=c,(4==++a||0===r&&0===l)&&(s=this.mul(s,i[o]),a=0,o=0)):a=0}h=26}return s},A.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},A.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},n.mont=function(t){return new x(t)},r(x,A),x.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},x.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},x.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var i=t.imul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),n=i.isub(r).iushrn(this.shift),s=n;return n.cmp(this.m)>=0?s=n.isub(this.m):n.cmpn(0)<0&&(s=n.iadd(this.m)),s._forceRed(this)},x.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new n(0)._forceRed(this);var i=t.mul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(r).iushrn(this.shift),o=s;return s.cmp(this.m)>=0?o=s.isub(this.m):s.cmpn(0)<0&&(o=s.iadd(this.m)),o._forceRed(this)},x.prototype.invm=function(t){return this.imod(t._invmp(this.m).mul(this.r2))._forceRed(this)}}(t,mt)}),It=pt(function(t,e){!function(t){var e,i,r,n=String.fromCharCode;function s(t){for(var e,i,r=[],n=0,s=t.length;n<s;)(e=t.charCodeAt(n++))>=55296&&e<=56319&&n<s?56320==(64512&(i=t.charCodeAt(n++)))?r.push(((1023&e)<<10)+(1023&i)+65536):(r.push(e),n--):r.push(e);return r}function o(t){if(t>=55296&&t<=57343)throw Error("Lone surrogate U+"+t.toString(16).toUpperCase()+" is not a scalar value")}function a(t,e){return n(t>>e&63|128)}function h(t){if(0==(4294967168&t))return n(t);var e="";return 0==(4294965248&t)?e=n(t>>6&31|192):0==(4294901760&t)?(o(t),e=n(t>>12&15|224),e+=a(t,6)):0==(4292870144&t)&&(e=n(t>>18&7|240),e+=a(t,12),e+=a(t,6)),e+n(63&t|128)}function u(){if(r>=i)throw Error("Invalid byte index");var t=255&e[r];if(r++,128==(192&t))return 63&t;throw Error("Invalid continuation byte")}function l(){var t,n;if(r>i)throw Error("Invalid byte index");if(r==i)return!1;if(t=255&e[r],r++,0==(128&t))return t;if(192==(224&t)){if((n=(31&t)<<6|u())>=128)return n;throw Error("Invalid continuation byte")}if(224==(240&t)){if((n=(15&t)<<12|u()<<6|u())>=2048)return o(n),n;throw Error("Invalid continuation byte")}if(240==(248&t)&&(n=(7&t)<<18|u()<<12|u()<<6|u())>=65536&&n<=1114111)return n;throw Error("Invalid UTF-8 detected")}t.version="3.0.0",t.encode=function(t){for(var e=s(t),i=e.length,r=-1,n="";++r<i;)n+=h(e[r]);return n},t.decode=function(t){e=s(t),i=e.length,r=0;for(var o,a=[];!1!==(o=l());)a.push(o);return function(t){for(var e,i=t.length,r=-1,s="";++r<i;)(e=t[r])>65535&&(s+=n((e-=65536)>>>10&1023|55296),e=56320|1023&e),s+=n(e);return s}(a)}}(e)});const Tt="0123456789abcdef".split(""),Ot=[1,256,65536,16777216],Bt=[0,8,16,24],Ct=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],_t=t=>{var e,i,r,n,s,o,a,h,u,l,c,d,f,m,p,g,w,v,y,M,b,A,x,E,R,k,S,I,T,O,B,C,_,D,P,N,W,L,F,U,$,j,q,G,z,H,Z,V,J,K,X,Q,Y,tt,et,it,rt,nt,st,ot,at,ht,ut;for(r=0;r<48;r+=2)n=t[0]^t[10]^t[20]^t[30]^t[40],s=t[1]^t[11]^t[21]^t[31]^t[41],h=t[4]^t[14]^t[24]^t[34]^t[44],u=t[5]^t[15]^t[25]^t[35]^t[45],l=t[6]^t[16]^t[26]^t[36]^t[46],c=t[7]^t[17]^t[27]^t[37]^t[47],i=(f=t[9]^t[19]^t[29]^t[39]^t[49])^((a=t[3]^t[13]^t[23]^t[33]^t[43])<<1|(o=t[2]^t[12]^t[22]^t[32]^t[42])>>>31),t[0]^=e=(d=t[8]^t[18]^t[28]^t[38]^t[48])^(o<<1|a>>>31),t[1]^=i,t[10]^=e,t[11]^=i,t[20]^=e,t[21]^=i,t[30]^=e,t[31]^=i,t[40]^=e,t[41]^=i,i=s^(u<<1|h>>>31),t[2]^=e=n^(h<<1|u>>>31),t[3]^=i,t[12]^=e,t[13]^=i,t[22]^=e,t[23]^=i,t[32]^=e,t[33]^=i,t[42]^=e,t[43]^=i,i=a^(c<<1|l>>>31),t[4]^=e=o^(l<<1|c>>>31),t[5]^=i,t[14]^=e,t[15]^=i,t[24]^=e,t[25]^=i,t[34]^=e,t[35]^=i,t[44]^=e,t[45]^=i,i=u^(f<<1|d>>>31),t[6]^=e=h^(d<<1|f>>>31),t[7]^=i,t[16]^=e,t[17]^=i,t[26]^=e,t[27]^=i,t[36]^=e,t[37]^=i,t[46]^=e,t[47]^=i,i=c^(s<<1|n>>>31),t[8]^=e=l^(n<<1|s>>>31),t[9]^=i,t[18]^=e,t[19]^=i,t[28]^=e,t[29]^=i,t[38]^=e,t[39]^=i,t[48]^=e,t[49]^=i,p=t[1],H=t[11]<<4|t[10]>>>28,Z=t[10]<<4|t[11]>>>28,I=t[20]<<3|t[21]>>>29,T=t[21]<<3|t[20]>>>29,ot=t[31]<<9|t[30]>>>23,at=t[30]<<9|t[31]>>>23,j=t[40]<<18|t[41]>>>14,q=t[41]<<18|t[40]>>>14,D=t[2]<<1|t[3]>>>31,P=t[3]<<1|t[2]>>>31,w=t[12]<<12|t[13]>>>20,V=t[22]<<10|t[23]>>>22,J=t[23]<<10|t[22]>>>22,O=t[33]<<13|t[32]>>>19,B=t[32]<<13|t[33]>>>19,ht=t[42]<<2|t[43]>>>30,ut=t[43]<<2|t[42]>>>30,tt=t[5]<<30|t[4]>>>2,et=t[4]<<30|t[5]>>>2,N=t[14]<<6|t[15]>>>26,W=t[15]<<6|t[14]>>>26,y=t[24]<<11|t[25]>>>21,K=t[34]<<15|t[35]>>>17,X=t[35]<<15|t[34]>>>17,C=t[45]<<29|t[44]>>>3,_=t[44]<<29|t[45]>>>3,E=t[6]<<28|t[7]>>>4,R=t[7]<<28|t[6]>>>4,it=t[17]<<23|t[16]>>>9,rt=t[16]<<23|t[17]>>>9,L=t[26]<<25|t[27]>>>7,F=t[27]<<25|t[26]>>>7,M=t[36]<<21|t[37]>>>11,b=t[37]<<21|t[36]>>>11,Q=t[47]<<24|t[46]>>>8,Y=t[46]<<24|t[47]>>>8,G=t[8]<<27|t[9]>>>5,z=t[9]<<27|t[8]>>>5,k=t[18]<<20|t[19]>>>12,S=t[19]<<20|t[18]>>>12,nt=t[29]<<7|t[28]>>>25,st=t[28]<<7|t[29]>>>25,U=t[38]<<8|t[39]>>>24,$=t[39]<<8|t[38]>>>24,A=t[48]<<14|t[49]>>>18,x=t[49]<<14|t[48]>>>18,t[0]=(m=t[0])^~(g=t[13]<<12|t[12]>>>20)&(v=t[25]<<11|t[24]>>>21),t[1]=p^~w&y,t[10]=E^~k&I,t[11]=R^~S&T,t[20]=D^~N&L,t[21]=P^~W&F,t[30]=G^~H&V,t[31]=z^~Z&J,t[40]=tt^~it&nt,t[41]=et^~rt&st,t[2]=g^~v&M,t[3]=w^~y&b,t[12]=k^~I&O,t[13]=S^~T&B,t[22]=N^~L&U,t[23]=W^~F&$,t[32]=H^~V&K,t[33]=Z^~J&X,t[42]=it^~nt&ot,t[43]=rt^~st&at,t[4]=v^~M&A,t[5]=y^~b&x,t[14]=I^~O&C,t[15]=T^~B&_,t[24]=L^~U&j,t[25]=F^~$&q,t[34]=V^~K&Q,t[35]=J^~X&Y,t[44]=nt^~ot&ht,t[45]=st^~at&ut,t[6]=M^~A&m,t[7]=b^~x&p,t[16]=O^~C&E,t[17]=B^~_&R,t[26]=U^~j&D,t[27]=$^~q&P,t[36]=K^~Q&G,t[37]=X^~Y&z,t[46]=ot^~ht&tt,t[47]=at^~ut&et,t[8]=A^~m&g,t[9]=x^~p&w,t[18]=C^~E&k,t[19]=_^~R&S,t[28]=j^~D&N,t[29]=q^~P&W,t[38]=Q^~G&H,t[39]=Y^~z&Z,t[48]=ht^~tt&it,t[49]=ut^~et&rt,t[0]^=Ct[r],t[1]^=Ct[r+1]},Dt=t=>e=>{var i;if("0x"===e.slice(0,2)){i=[];for(var r=2,n=e.length;r<n;r+=2)i.push(parseInt(e.slice(r,r+2),16))}else i=e;return((t,e)=>{for(var i,r=e.length,n=t.blocks,s=t.blockCount<<2,o=t.blockCount,a=t.outputBlocks,h=t.s,u=0;u<r;){if(t.reset)for(t.reset=!1,n[0]=t.block,d=1;d<o+1;++d)n[d]=0;if("string"!=typeof e)for(d=t.start;u<r&&d<s;++u)n[d>>2]|=e[u]<<Bt[3&d++];else for(d=t.start;u<r&&d<s;++u)(i=e.charCodeAt(u))<128?n[d>>2]|=i<<Bt[3&d++]:i<2048?(n[d>>2]|=(192|i>>6)<<Bt[3&d++],n[d>>2]|=(128|63&i)<<Bt[3&d++]):i<55296||i>=57344?(n[d>>2]|=(224|i>>12)<<Bt[3&d++],n[d>>2]|=(128|i>>6&63)<<Bt[3&d++],n[d>>2]|=(128|63&i)<<Bt[3&d++]):(i=65536+((1023&i)<<10|1023&e.charCodeAt(++u)),n[d>>2]|=(240|i>>18)<<Bt[3&d++],n[d>>2]|=(128|i>>12&63)<<Bt[3&d++],n[d>>2]|=(128|i>>6&63)<<Bt[3&d++],n[d>>2]|=(128|63&i)<<Bt[3&d++]);if(t.lastByteIndex=d,d>=s){for(t.start=d-s,t.block=n[o],d=0;d<o;++d)h[d]^=n[d];_t(h),t.reset=!0}else t.start=d}if(n[(d=t.lastByteIndex)>>2]|=Ot[3&d],t.lastByteIndex===s)for(n[0]=n[o],d=1;d<o+1;++d)n[d]=0;for(n[o-1]|=2147483648,d=0;d<o;++d)h[d]^=n[d];_t(h);for(var l,c="",d=0,f=0;f<a;){for(d=0;d<o&&f<a;++d,++f)c+=Tt[(l=h[d])>>4&15]+Tt[15&l]+Tt[l>>12&15]+Tt[l>>8&15]+Tt[l>>20&15]+Tt[l>>16&15]+Tt[l>>28&15]+Tt[l>>24&15];f%o==0&&(_t(h),d=0)}return"0x"+c})((t=>{return{blocks:[],reset:!0,block:0,start:0,blockCount:1600-(t<<1)>>5,outputBlocks:t>>5,s:(e=[0,0,0,0,0,0,0,0,0,0],[].concat(e,e,e,e,e))};var e})(t),i)};var Pt={keccak256:Dt(256),keccak512:Dt(512),keccak256s:Dt(256),keccak512s:Dt(512)},Nt=pt(function(t){!function(){var e="input is invalid type",i="object"==typeof window,r=i?window:{};r.JS_SHA3_NO_WINDOW&&(i=!1);var n=!i&&"object"==typeof self;!r.JS_SHA3_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node?r=mt:n&&(r=self);var s=!r.JS_SHA3_NO_COMMON_JS&&t.exports,o=!r.JS_SHA3_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,a="0123456789abcdef".split(""),h=[4,1024,262144,67108864],u=[0,8,16,24],l=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],c=[224,256,384,512],d=[128,256],f=["hex","buffer","arrayBuffer","array","digest"],m={128:168,256:136};!r.JS_SHA3_NO_NODE_JS&&Array.isArray||(Array.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)}),!o||!r.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW&&ArrayBuffer.isView||(ArrayBuffer.isView=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});for(var p=function(t,e,i){return function(r){return new O(t,e,t).update(r)[i]()}},g=function(t,e,i){return function(r,n){return new O(t,e,n).update(r)[i]()}},w=function(t,e,i){return function(e,r,n,s){return A["cshake"+t].update(e,r,n,s)[i]()}},v=function(t,e,i){return function(e,r,n,s){return A["kmac"+t].update(e,r,n,s)[i]()}},y=function(t,e,i,r){for(var n=0;n<f.length;++n){var s=f[n];t[s]=e(i,r,s)}return t},M=function(t,e){var i=p(t,e,"hex");return i.create=function(){return new O(t,e,t)},i.update=function(t){return i.create().update(t)},y(i,p,t,e)},b=[{name:"keccak",padding:[1,256,65536,16777216],bits:c,createMethod:M},{name:"sha3",padding:[6,1536,393216,100663296],bits:c,createMethod:M},{name:"shake",padding:[31,7936,2031616,520093696],bits:d,createMethod:function(t,e){var i=g(t,e,"hex");return i.create=function(i){return new O(t,e,i)},i.update=function(t,e){return i.create(e).update(t)},y(i,g,t,e)}},{name:"cshake",padding:h,bits:d,createMethod:function(t,e){var i=m[t],r=w(t,0,"hex");return r.create=function(r,n,s){return n||s?new O(t,e,r).bytepad([n,s],i):A["shake"+t].create(r)},r.update=function(t,e,i,n){return r.create(e,i,n).update(t)},y(r,w,t,e)}},{name:"kmac",padding:h,bits:d,createMethod:function(t,e){var i=m[t],r=v(t,0,"hex");return r.create=function(r,n,s){return new B(t,e,n).bytepad(["KMAC",s],i).bytepad([r],i)},r.update=function(t,e,i,n){return r.create(t,i,n).update(e)},y(r,v,t,e)}}],A={},x=[],E=0;E<b.length;++E)for(var R=b[E],k=R.bits,S=0;S<k.length;++S){var I=R.name+"_"+k[S];if(x.push(I),A[I]=R.createMethod(k[S],R.padding),"sha3"!==R.name){var T=R.name+k[S];x.push(T),A[T]=A[I]}}function O(t,e,i){this.blocks=[],this.s=[],this.padding=e,this.outputBits=i,this.reset=!0,this.finalized=!1,this.block=0,this.start=0,this.blockCount=1600-(t<<1)>>5,this.byteCount=this.blockCount<<2,this.outputBlocks=i>>5,this.extraBytes=(31&i)>>3;for(var r=0;r<50;++r)this.s[r]=0}function B(t,e,i){O.call(this,t,e,i)}O.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i,r=typeof t;if("string"!==r){if("object"!==r)throw new Error(e);if(null===t)throw new Error(e);if(o&&t.constructor===ArrayBuffer)t=new Uint8Array(t);else if(!(Array.isArray(t)||o&&ArrayBuffer.isView(t)))throw new Error(e);i=!0}for(var n,s,a=this.blocks,h=this.byteCount,l=t.length,c=this.blockCount,d=0,f=this.s;d<l;){if(this.reset)for(this.reset=!1,a[0]=this.block,n=1;n<c+1;++n)a[n]=0;if(i)for(n=this.start;d<l&&n<h;++d)a[n>>2]|=t[d]<<u[3&n++];else for(n=this.start;d<l&&n<h;++d)(s=t.charCodeAt(d))<128?a[n>>2]|=s<<u[3&n++]:s<2048?(a[n>>2]|=(192|s>>6)<<u[3&n++],a[n>>2]|=(128|63&s)<<u[3&n++]):s<55296||s>=57344?(a[n>>2]|=(224|s>>12)<<u[3&n++],a[n>>2]|=(128|s>>6&63)<<u[3&n++],a[n>>2]|=(128|63&s)<<u[3&n++]):(s=65536+((1023&s)<<10|1023&t.charCodeAt(++d)),a[n>>2]|=(240|s>>18)<<u[3&n++],a[n>>2]|=(128|s>>12&63)<<u[3&n++],a[n>>2]|=(128|s>>6&63)<<u[3&n++],a[n>>2]|=(128|63&s)<<u[3&n++]);if(this.lastByteIndex=n,n>=h){for(this.start=n-h,this.block=a[c],n=0;n<c;++n)f[n]^=a[n];C(f),this.reset=!0}else this.start=n}return this},O.prototype.encode=function(t,e){var i=255&t,r=1,n=[i];for(i=255&(t>>=8);i>0;)n.unshift(i),i=255&(t>>=8),++r;return e?n.push(r):n.unshift(r),this.update(n),n.length},O.prototype.encodeString=function(t){var i,r=typeof t;if("string"!==r){if("object"!==r)throw new Error(e);if(null===t)throw new Error(e);if(o&&t.constructor===ArrayBuffer)t=new Uint8Array(t);else if(!(Array.isArray(t)||o&&ArrayBuffer.isView(t)))throw new Error(e);i=!0}var n=0;if(i)n=t.length;else for(var s=0;s<t.length;++s){var a=t.charCodeAt(s);a<128?n+=1:a<2048?n+=2:a<55296||a>=57344?n+=3:(a=65536+((1023&a)<<10|1023&t.charCodeAt(++s)),n+=4)}return n+=this.encode(8*n),this.update(t),n},O.prototype.bytepad=function(t,e){for(var i=this.encode(e),r=0;r<t.length;++r)i+=this.encodeString(t[r]);var n=[];return n.length=e-i%e,this.update(n),this},O.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,e=this.lastByteIndex,i=this.blockCount,r=this.s;if(t[e>>2]|=this.padding[3&e],this.lastByteIndex===this.byteCount)for(t[0]=t[i],e=1;e<i+1;++e)t[e]=0;for(t[i-1]|=2147483648,e=0;e<i;++e)r[e]^=t[e];C(r)}},O.prototype.toString=O.prototype.hex=function(){this.finalize();for(var t,e=this.blockCount,i=this.s,r=this.outputBlocks,n=this.extraBytes,s=0,o=0,h="";o<r;){for(s=0;s<e&&o<r;++s,++o)h+=a[(t=i[s])>>4&15]+a[15&t]+a[t>>12&15]+a[t>>8&15]+a[t>>20&15]+a[t>>16&15]+a[t>>28&15]+a[t>>24&15];o%e==0&&(C(i),s=0)}return n&&(h+=a[(t=i[s])>>4&15]+a[15&t],n>1&&(h+=a[t>>12&15]+a[t>>8&15]),n>2&&(h+=a[t>>20&15]+a[t>>16&15])),h},O.prototype.buffer=O.prototype.arrayBuffer=function(){this.finalize();var t,e=this.blockCount,i=this.s,r=this.outputBlocks,n=this.extraBytes,s=0,o=0,a=this.outputBits>>3;t=n?new ArrayBuffer(r+1<<2):new ArrayBuffer(a);for(var h=new Uint32Array(t);o<r;){for(s=0;s<e&&o<r;++s,++o)h[o]=i[s];o%e==0&&C(i)}return n&&(h[s]=i[s],t=t.slice(0,a)),t},O.prototype.digest=O.prototype.array=function(){this.finalize();for(var t,e,i=this.blockCount,r=this.s,n=this.outputBlocks,s=this.extraBytes,o=0,a=0,h=[];a<n;){for(o=0;o<i&&a<n;++o,++a)h[t=a<<2]=255&(e=r[o]),h[t+1]=e>>8&255,h[t+2]=e>>16&255,h[t+3]=e>>24&255;a%i==0&&C(r)}return s&&(h[t=a<<2]=255&(e=r[o]),s>1&&(h[t+1]=e>>8&255),s>2&&(h[t+2]=e>>16&255)),h},(B.prototype=new O).finalize=function(){return this.encode(this.outputBits,!0),O.prototype.finalize.call(this)};var C=function(t){var e,i,r,n,s,o,a,h,u,c,d,f,m,p,g,w,v,y,M,b,A,x,E,R,k,S,I,T,O,B,C,_,D,P,N,W,L,F,U,$,j,q,G,z,H,Z,V,J,K,X,Q,Y,tt,et,it,rt,nt,st,ot,at,ht,ut,lt;for(r=0;r<48;r+=2)n=t[0]^t[10]^t[20]^t[30]^t[40],s=t[1]^t[11]^t[21]^t[31]^t[41],h=t[4]^t[14]^t[24]^t[34]^t[44],u=t[5]^t[15]^t[25]^t[35]^t[45],c=t[6]^t[16]^t[26]^t[36]^t[46],d=t[7]^t[17]^t[27]^t[37]^t[47],i=(m=t[9]^t[19]^t[29]^t[39]^t[49])^((a=t[3]^t[13]^t[23]^t[33]^t[43])<<1|(o=t[2]^t[12]^t[22]^t[32]^t[42])>>>31),t[0]^=e=(f=t[8]^t[18]^t[28]^t[38]^t[48])^(o<<1|a>>>31),t[1]^=i,t[10]^=e,t[11]^=i,t[20]^=e,t[21]^=i,t[30]^=e,t[31]^=i,t[40]^=e,t[41]^=i,i=s^(u<<1|h>>>31),t[2]^=e=n^(h<<1|u>>>31),t[3]^=i,t[12]^=e,t[13]^=i,t[22]^=e,t[23]^=i,t[32]^=e,t[33]^=i,t[42]^=e,t[43]^=i,i=a^(d<<1|c>>>31),t[4]^=e=o^(c<<1|d>>>31),t[5]^=i,t[14]^=e,t[15]^=i,t[24]^=e,t[25]^=i,t[34]^=e,t[35]^=i,t[44]^=e,t[45]^=i,i=u^(m<<1|f>>>31),t[6]^=e=h^(f<<1|m>>>31),t[7]^=i,t[16]^=e,t[17]^=i,t[26]^=e,t[27]^=i,t[36]^=e,t[37]^=i,t[46]^=e,t[47]^=i,i=d^(s<<1|n>>>31),t[8]^=e=c^(n<<1|s>>>31),t[9]^=i,t[18]^=e,t[19]^=i,t[28]^=e,t[29]^=i,t[38]^=e,t[39]^=i,t[48]^=e,t[49]^=i,g=t[1],Z=t[11]<<4|t[10]>>>28,V=t[10]<<4|t[11]>>>28,T=t[20]<<3|t[21]>>>29,O=t[21]<<3|t[20]>>>29,at=t[31]<<9|t[30]>>>23,ht=t[30]<<9|t[31]>>>23,q=t[40]<<18|t[41]>>>14,G=t[41]<<18|t[40]>>>14,P=t[2]<<1|t[3]>>>31,N=t[3]<<1|t[2]>>>31,v=t[12]<<12|t[13]>>>20,J=t[22]<<10|t[23]>>>22,K=t[23]<<10|t[22]>>>22,B=t[33]<<13|t[32]>>>19,C=t[32]<<13|t[33]>>>19,ut=t[42]<<2|t[43]>>>30,lt=t[43]<<2|t[42]>>>30,et=t[5]<<30|t[4]>>>2,it=t[4]<<30|t[5]>>>2,W=t[14]<<6|t[15]>>>26,L=t[15]<<6|t[14]>>>26,M=t[24]<<11|t[25]>>>21,X=t[34]<<15|t[35]>>>17,Q=t[35]<<15|t[34]>>>17,_=t[45]<<29|t[44]>>>3,D=t[44]<<29|t[45]>>>3,R=t[6]<<28|t[7]>>>4,k=t[7]<<28|t[6]>>>4,rt=t[17]<<23|t[16]>>>9,nt=t[16]<<23|t[17]>>>9,F=t[26]<<25|t[27]>>>7,U=t[27]<<25|t[26]>>>7,b=t[36]<<21|t[37]>>>11,A=t[37]<<21|t[36]>>>11,Y=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,z=t[8]<<27|t[9]>>>5,H=t[9]<<27|t[8]>>>5,S=t[18]<<20|t[19]>>>12,I=t[19]<<20|t[18]>>>12,st=t[29]<<7|t[28]>>>25,ot=t[28]<<7|t[29]>>>25,$=t[38]<<8|t[39]>>>24,j=t[39]<<8|t[38]>>>24,x=t[48]<<14|t[49]>>>18,E=t[49]<<14|t[48]>>>18,t[0]=(p=t[0])^~(w=t[13]<<12|t[12]>>>20)&(y=t[25]<<11|t[24]>>>21),t[1]=g^~v&M,t[10]=R^~S&T,t[11]=k^~I&O,t[20]=P^~W&F,t[21]=N^~L&U,t[30]=z^~Z&J,t[31]=H^~V&K,t[40]=et^~rt&st,t[41]=it^~nt&ot,t[2]=w^~y&b,t[3]=v^~M&A,t[12]=S^~T&B,t[13]=I^~O&C,t[22]=W^~F&$,t[23]=L^~U&j,t[32]=Z^~J&X,t[33]=V^~K&Q,t[42]=rt^~st&at,t[43]=nt^~ot&ht,t[4]=y^~b&x,t[5]=M^~A&E,t[14]=T^~B&_,t[15]=O^~C&D,t[24]=F^~$&q,t[25]=U^~j&G,t[34]=J^~X&Y,t[35]=K^~Q&tt,t[44]=st^~at&ut,t[45]=ot^~ht&lt,t[6]=b^~x&p,t[7]=A^~E&g,t[16]=B^~_&R,t[17]=C^~D&k,t[26]=$^~q&P,t[27]=j^~G&N,t[36]=X^~Y&z,t[37]=Q^~tt&H,t[46]=at^~ut&et,t[47]=ht^~lt&it,t[8]=x^~p&w,t[9]=E^~g&v,t[18]=_^~R&S,t[19]=D^~k&I,t[28]=q^~P&W,t[29]=G^~N&L,t[38]=Y^~z&Z,t[39]=tt^~H&V,t[48]=ut^~et&rt,t[49]=lt^~it&nt,t[0]^=l[r],t[1]^=l[r+1]};if(s)t.exports=A;else for(E=0;E<x.length;++E)r[x[E]]=A[x[E]]}()});function Wt(t){if(null==t)throw new Error("cannot convert null value to array");if("string"==typeof t){const e=t.match(/^(0x)?[0-9a-fA-F]*$/);if(!e)throw new Error("invalid hexidecimal string");if("0x"!==e[1])throw new Error("hex string must have 0x prefix");(t=t.substring(2)).length%2&&(t="0"+t);const i=[];for(let e=0;e<t.length;e+=2)i.push(parseInt(t.substr(e,2),16));return Lt(new Uint8Array(i))}if(function(t){if(!t||parseInt(String(t.length))!=t.length||"string"==typeof t)return!1;for(let e=0;e<t.length;e++){const i=t[e];if(i<0||i>=256||parseInt(String(i))!=i)return!1}return!0}(t))return Lt(new Uint8Array(t));throw new Error("invalid arrayify value")}function Lt(t){return void 0!==t.slice||(t.slice=()=>{const e=Array.prototype.slice.call(arguments);return Lt(new Uint8Array(Array.prototype.slice.apply(t,e)))}),t}var Ft=Object.defineProperty({keccak256:function(t){return"0x"+Nt.keccak_256(Wt(t))},padLeft:(t,e)=>{const i=/^0x/i.test(t)||"number"==typeof t;return t=t.toString().replace(/^0x/i,""),(i?"0x":"")+new Array(e-t.length+1>=0?e-t.length+1:0).join("0")+t},bytesToHex:function(t){const e=[];for(let i=0;i<t.length;i++)e.push((t[i]>>>4).toString(16)),e.push((15&t[i]).toString(16));return`0x${e.join("").replace(/^0+/,"")}`},toByteArray:Wt},"__esModule",{value:!0});function Ut(t){return!("string"!=typeof t||!/^(0x)?[0-9a-f]{512}$/i.test(t)||!/^(0x)?[0-9a-f]{512}$/.test(t)&&!/^(0x)?[0-9A-F]{512}$/.test(t))}function $t(t,e){"object"==typeof e&&e.constructor===Uint8Array&&(e=Ft.bytesToHex(e));const i=Ft.keccak256(e).replace("0x","");for(let e=0;e<12;e+=4){const r=(parseInt(i.substr(e,2),16)<<8)+parseInt(i.substr(e+2,2),16)&2047,n=1<<r%4;if((jt(t.charCodeAt(t.length-1-Math.floor(r/4)))&n)!==n)return!1}return!0}function jt(t){if(t>=48&&t<=57)return t-48;if(t>=65&&t<=70)return t-55;if(t>=97&&t<=102)return t-87;throw new Error("invalid bloom")}function qt(t){return!("string"!=typeof t||!/^(0x)?[0-9a-f]{64}$/i.test(t)||!/^(0x)?[0-9a-f]{64}$/.test(t)&&!/^(0x)?[0-9A-F]{64}$/.test(t))}function Gt(t){return!("string"!=typeof t||!t.match(/^(0x)?[0-9a-fA-F]{40}$/)&&!t.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/))}var zt=Object.defineProperty({isBloom:Ut,isInBloom:$t,isUserEthereumAddressInBloom:function(t,e){if(!Ut(t))throw new Error("Invalid bloom given");if(!Gt(e))throw new Error(`Invalid ethereum address given: "${e}"`);return $t(t,Ft.padLeft(e,64))},isContractAddressInBloom:function(t,e){if(!Ut(t))throw new Error("Invalid bloom given");if(!Gt(e))throw new Error(`Invalid contract address given: "${e}"`);return $t(t,e)},isTopicInBloom:function(t,e){if(!Ut(t))throw new Error("Invalid bloom given");if(!qt(e))throw new Error("Invalid topic");return $t(t,e)},isTopic:qt,isAddress:Gt},"__esModule",{value:!0}),Ht=function(t){return St.isBN(t)},Zt=function(t){return t&&t.constructor&&"BigNumber"===t.constructor.name},Vt=function(t){try{return Mt.apply(null,arguments)}catch(e){throw new Error(e+' Given value: "'+t+'"')}},Jt=function(t){return!!/^(0x)?[0-9a-f]{40}$/i.test(t)&&(!(!/^(0x|0X)?[0-9a-f]{40}$/.test(t)&&!/^(0x|0X)?[0-9A-F]{40}$/.test(t))||Kt(t))},Kt=function(t){t=t.replace(/^0x/i,"");for(var e=se(t.toLowerCase()).replace(/^0x/i,""),i=0;i<40;i++)if(parseInt(e[i],16)>7&&t[i].toUpperCase()!==t[i]||parseInt(e[i],16)<=7&&t[i].toLowerCase()!==t[i])return!1;return!0},Xt=function(t){var e="";t=(t=(t=(t=(t=It.encode(t)).replace(/^(?:\u0000)*/,"")).split("").reverse().join("")).replace(/^(?:\u0000)*/,"")).split("").reverse().join("");for(var i=0;i<t.length;i++){var r=t.charCodeAt(i).toString(16);e+=r.length<2?"0"+r:r}return"0x"+e},Qt=function(t){if(!t)return t;if("string"==typeof t&&!ie(t))throw new Error('Given value "'+t+'" is not a valid hex string.');return Vt(t).toNumber()},Yt=function(t){if(null==t)return t;if(!isFinite(t)&&!ie(t))throw new Error('Given input "'+t+'" is not a number.');var e=Vt(t),i=e.toString(16);return e.lt(new St(0))?"-0x"+i.substr(1):"0x"+i},te=function(t){if(t=t.toString(16),!ie(t))throw new Error('Given value "'+t+'" is not a valid hex string.');t=t.replace(/^0x/i,"");for(var e=[],i=0;i<t.length;i+=2)e.push(parseInt(t.substr(i,2),16));return e},ee=function(t,e){if(Jt(t))return e?"address":"0x"+t.toLowerCase().replace(/^0x/i,"");if("boolean"==typeof t)return e?"bool":t?"0x01":"0x00";if(Buffer.isBuffer(t))return"0x"+t.toString("hex");if("object"==typeof t&&t&&!Zt(t)&&!Ht(t))return e?"string":Xt(JSON.stringify(t));if("string"==typeof t){if(0===t.indexOf("-0x")||0===t.indexOf("-0X"))return e?"int256":Yt(t);if(0===t.indexOf("0x")||0===t.indexOf("0X"))return e?"bytes":t;if(!isFinite(t))return e?"string":Xt(t)}return e?t<0?"int256":"uint256":Yt(t)},ie=function(t){return("string"==typeof t||"number"==typeof t)&&/^(-)?0x[0-9a-f]*$/i.test(t)},re=function(t){return("string"==typeof t||"number"==typeof t)&&/^(-0x|0x)?[0-9a-f]*$/i.test(t)},ne="0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",se=function(t){Ht(t)&&(t=t.toString()),ie(t)&&/^0x/i.test(t.toString())&&(t=te(t));var e=Pt.keccak256(t);return e===ne?null:e};se._Hash=Pt;var oe,ae,he,ue,le={BN:St,isBN:Ht,isBigNumber:Zt,toBN:Vt,isAddress:Jt,isBloom:function(t){return zt.isBloom(t)},isUserEthereumAddressInBloom:function(t,e){return zt.isUserEthereumAddressInBloom(t,e)},isContractAddressInBloom:function(t,e){return zt.isContractAddressInBloom(t,e)},isTopic:function(t){return zt.isTopic(t)},isTopicInBloom:function(t,e){return zt.isTopicInBloom(t,e)},isInBloom:function(t,e){return zt.isInBloom(t,e)},checkAddressChecksum:Kt,utf8ToHex:Xt,hexToUtf8:function(t){if(!ie(t))throw new Error('The parameter "'+t+'" must be a valid HEX string.');for(var e="",i=0,r=(t=(t=(t=(t=(t=t.replace(/^0x/i,"")).replace(/^(?:00)*/,"")).split("").reverse().join("")).replace(/^(?:00)*/,"")).split("").reverse().join("")).length,n=0;n<r;n+=2)i=parseInt(t.substr(n,2),16),e+=String.fromCharCode(i);return It.decode(e)},hexToNumber:Qt,hexToNumberString:function(t){if(!t)return t;if("string"==typeof t&&!ie(t))throw new Error('Given value "'+t+'" is not a valid hex string.');return Vt(t).toString(10)},numberToHex:Yt,toHex:ee,hexToBytes:te,bytesToHex:function(t){for(var e=[],i=0;i<t.length;i++)e.push((t[i]>>>4).toString(16)),e.push((15&t[i]).toString(16));return"0x"+e.join("")},isHex:re,isHexStrict:ie,stripHexPrefix:function(t){return 0!==t&&re(t)?t.replace(/^(-)?0x/i,"$1"):t},leftPad:function(t,e,i){var r=/^0x/i.test(t)||"number"==typeof t;return t=t.toString(16).replace(/^0x/i,""),(r?"0x":"")+new Array(e-t.length+1>=0?e-t.length+1:0).join(i||"0")+t},rightPad:function(t,e,i){return(/^0x/i.test(t)||"number"==typeof t?"0x":"")+(t=t.toString(16).replace(/^0x/i,""))+new Array(e-t.length+1>=0?e-t.length+1:0).join(i||"0")},toTwosComplement:function(t){return"0x"+Vt(t).toTwos(256).toString(16,64)},sha3:se,sha3Raw:function(t){return null===(t=se(t))?ne:t},toNumber:function(t){return"number"==typeof t?t:Qt(ee(t))}},ce=function(t){var e=typeof t;if("string"===e)return le.isHexStrict(t)?new St(t.replace(/0x/i,""),16):new St(t,10);if("number"===e)return new St(t);if(le.isBigNumber(t))return new St(t.toString(10));if(le.isBN(t))return t;throw new Error(t+" is not a number")},de=function(t,e,i){var r,n,s;if("bytes"===(t=(s=t).startsWith("int[")?"int256"+s.slice(3):"int"===s?"int256":s.startsWith("uint[")?"uint256"+s.slice(4):"uint"===s?"uint256":s.startsWith("fixed[")?"fixed128x128"+s.slice(5):"fixed"===s?"fixed128x128":s.startsWith("ufixed[")?"ufixed128x128"+s.slice(6):"ufixed"===s?"ufixed128x128":s)){if(e.replace(/^0x/i,"").length%2!=0)throw new Error("Invalid bytes characters "+e.length);return e}if("string"===t)return le.utf8ToHex(e);if("bool"===t)return e?"01":"00";if(t.startsWith("address")){if(r=i?64:40,!le.isAddress(e))throw new Error(e+" is not a valid address, or the checksum is invalid.");return le.leftPad(e.toLowerCase(),r)}if(r=function(t){var e=/^\D+(\d+).*$/.exec(t);return e?parseInt(e[1],10):null}(t),t.startsWith("bytes")){if(!r)throw new Error("bytes[] not yet supported in solidity");if(i&&(r=32),r<1||r>32||r<e.replace(/^0x/i,"").length/2)throw new Error("Invalid bytes"+r+" for "+e);return le.rightPad(e,2*r)}if(t.startsWith("uint")){if(r%8||r<8||r>256)throw new Error("Invalid uint"+r+" size");if((n=ce(e)).bitLength()>r)throw new Error("Supplied uint exceeds width: "+r+" vs "+n.bitLength());if(n.lt(new St(0)))throw new Error("Supplied uint "+n.toString()+" is negative");return r?le.leftPad(n.toString("hex"),r/8*2):n}if(t.startsWith("int")){if(r%8||r<8||r>256)throw new Error("Invalid int"+r+" size");if((n=ce(e)).bitLength()>r)throw new Error("Supplied int exceeds width: "+r+" vs "+n.bitLength());return n.lt(new St(0))?n.toTwos(r).toString("hex"):r?le.leftPad(n.toString("hex"),r/8*2):n}throw new Error("Unsupported or invalid type: "+t)},fe=function(t){if(Array.isArray(t))throw new Error("Autodetection of array types is not supported.");var e,i,r="";if(t&&"object"==typeof t&&(t.hasOwnProperty("v")||t.hasOwnProperty("t")||t.hasOwnProperty("value")||t.hasOwnProperty("type"))?(e=t.hasOwnProperty("t")?t.t:t.type,r=t.hasOwnProperty("v")?t.v:t.value):(e=le.toHex(t,!0),r=le.toHex(t),e.startsWith("int")||e.startsWith("uint")||(e="bytes")),!e.startsWith("int")&&!e.startsWith("uint")||"string"!=typeof r||/^(-)?0x/i.test(r)||(r=new St(r)),Array.isArray(r)){if((i=function(t){var e=/^\D+\d*\[(\d+)\]$/.exec(t);return e?parseInt(e[1],10):null}(e))&&r.length!==i)throw new Error(e+" is not matching the given array "+JSON.stringify(r));i=r.length}return Array.isArray(r)?r.map(function(t){return de(e,t,i).toString("hex").replace("0x","")}).join(""):de(e,r,i).toString("hex").replace("0x","")},me={soliditySha3:function(){var t=Array.prototype.slice.call(arguments),e=t.map(fe);return le.sha3("0x"+e.join(""))},soliditySha3Raw:function(){return le.sha3Raw("0x"+Array.prototype.slice.call(arguments).map(fe).join(""))},encodePacked:function(){var t=Array.prototype.slice.call(arguments),e=t.map(fe);return"0x"+e.join("").toLowerCase()}},pe=a.randomBytes,ge=function(t,e){var i=[];return e.forEach(function(e){if("object"==typeof e.components){if("tuple"!==e.type.substring(0,5))throw new Error("components found but type is not tuple; report on GitHub");var r="",n=e.type.indexOf("[");n>=0&&(r=e.type.substring(n));var s=ge(t,e.components);Array.isArray(s)&&t?i.push("tuple("+s.join(",")+")"+r):i.push(t?"("+s+")":"("+s.join(",")+")"+r)}else i.push(e.type)}),i},we=function(t){if(!le.isHexStrict(t))throw new Error("The parameter must be a valid HEX string.");var e="",i=0,r=t.length;for("0x"===t.substring(0,2)&&(i=2);i<r;i+=2){var n=parseInt(t.substr(i,2),16);e+=String.fromCharCode(n)}return e},ve=function(t){if(!t)return"0x00";for(var e="",i=0;i<t.length;i++){var r=t.charCodeAt(i).toString(16);e+=r.length<2?"0"+r:r}return"0x"+e},ye=function(t){if(t=t?t.toLowerCase():"ether",!kt.unitMap[t])throw new Error('This unit "'+t+"\" doesn't exist, please use the one of the following units"+JSON.stringify(kt.unitMap,null,2));return t},Me={_fireError:function(t,e,i,r,n){return!t||"object"!=typeof t||t instanceof Error||!t.data||((t.data&&"object"==typeof t.data||Array.isArray(t.data))&&(t.data=JSON.stringify(t.data,null,2)),t=t.message+"\n"+t.data),"string"==typeof t&&(t=new Error(t)),"function"==typeof r&&r(t,n),"function"==typeof i&&((e&&"function"==typeof e.listeners&&e.listeners("error").length||"function"==typeof r)&&e.catch(function(){}),setTimeout(function(){i(t)},1)),e&&"function"==typeof e.emit&&setTimeout(function(){e.emit("error",t,n),e.removeAllListeners()},1),e},_jsonInterfaceMethodToString:function(t){return t&&"object"==typeof t&&t.name&&-1!==t.name.indexOf("(")?t.name:t.name+"("+ge(!1,t.inputs).join(",")+")"},_flattenTypes:ge,randomHex:function(t){return"0x"+pe(t).toString("hex")},BN:le.BN,isBN:le.isBN,isBigNumber:le.isBigNumber,isHex:le.isHex,isHexStrict:le.isHexStrict,sha3:le.sha3,sha3Raw:le.sha3Raw,keccak256:le.sha3,soliditySha3:me.soliditySha3,soliditySha3Raw:me.soliditySha3Raw,encodePacked:me.encodePacked,isAddress:le.isAddress,checkAddressChecksum:le.checkAddressChecksum,toChecksumAddress:function(t){if(void 0===t)return"";if(!/^(0x)?[0-9a-f]{40}$/i.test(t))throw new Error('Given address "'+t+'" is not a valid Ethereum address.');t=t.toLowerCase().replace(/^0x/i,"");for(var e=le.sha3(t).replace(/^0x/i,""),i="0x",r=0;r<t.length;r++)parseInt(e[r],16)>7?i+=t[r].toUpperCase():i+=t[r];return i},toHex:le.toHex,toBN:le.toBN,bytesToHex:le.bytesToHex,hexToBytes:le.hexToBytes,hexToNumberString:le.hexToNumberString,hexToNumber:le.hexToNumber,toDecimal:le.hexToNumber,numberToHex:le.numberToHex,fromDecimal:le.numberToHex,hexToUtf8:le.hexToUtf8,hexToString:le.hexToUtf8,toUtf8:le.hexToUtf8,stripHexPrefix:le.stripHexPrefix,utf8ToHex:le.utf8ToHex,stringToHex:le.utf8ToHex,fromUtf8:le.utf8ToHex,hexToAscii:we,toAscii:we,asciiToHex:ve,fromAscii:ve,unitMap:kt.unitMap,toWei:function(t,e){if(e=ye(e),!le.isBN(t)&&"string"!=typeof t)throw new Error("Please pass numbers as strings or BN objects to avoid precision errors.");return le.isBN(t)?kt.toWei(t,e):kt.toWei(t,e).toString(10)},fromWei:function(t,e){if(e=ye(e),!le.isBN(t)&&"string"!=typeof t)throw new Error("Please pass numbers as strings or BN objects to avoid precision errors.");return le.isBN(t)?kt.fromWei(t,e):kt.fromWei(t,e).toString(10)},padLeft:le.leftPad,leftPad:le.leftPad,padRight:le.rightPad,rightPad:le.rightPad,toTwosComplement:le.toTwosComplement,isBloom:le.isBloom,isUserEthereumAddressInBloom:le.isUserEthereumAddressInBloom,isContractAddressInBloom:le.isContractAddressInBloom,isTopic:le.isTopic,isTopicInBloom:le.isTopicInBloom,isInBloom:le.isInBloom,compareBlockNumbers:function(t,e){if(t==e)return 0;if("genesis"!=t&&"earliest"!=t&&0!=t||"genesis"!=e&&"earliest"!=e&&0!=e){if("genesis"==t||"earliest"==t)return-1;if("genesis"==e||"earliest"==e)return 1;if("latest"==t)return"pending"==e?-1:1;if("latest"===e)return"pending"==t?1:-1;if("pending"==t)return 1;if("pending"==e)return-1;{let i=new St(t),r=new St(e);return i.lt(r)?-1:i.eq(r)?0:1}}return 0},toNumber:le.toNumber};function be(t,e,i){let r=!1;if("allow"===i){if(t&&t.allow){const i=t.allow.find(t=>t.type===e);r=i&&i.values.length>0}return r}if(t&&t.deny){const i=t.deny.find(t=>t.type===e);r=i&&i.values.length>0}return r}function Ae(t,e,i){const r=be(t.credentials,e,i);return"allow"===i?(r&&(t.credentials.allow=t.credentials.allow.filter(t=>t.type!==e)),t.credentials&&!t.credentials.allow&&(t.credentials={deny:t.credentials&&t.credentials.deny})):(r&&(t.credentials.deny=t.credentials.deny.filter(t=>t.type!==e)),t.credentials&&!t.credentials.deny&&(t.credentials={allow:t.credentials&&t.credentials.allow})),t}function xe(t,e,i,r){const n=be(t.credentials,e,r);return"allow"===r?n?t.credentials.allow.find(t=>{t.type===e&&(t.values=i)}):t=Ee(t,e,i,r):n?t.credentials.deny.find(t=>{t.type===e&&(t.values=i)}):t=Ee(t,e,i,r),t}function Ee(t,e,i,r){const n={type:e,values:i};return"allow"===r?t.credentials&&t.credentials.allow?t.credentials.allow.push(n):t.credentials={allow:[n],deny:t.credentials&&t.credentials.deny}:t.credentials&&t.credentials.deny?t.credentials.deny.push(n):t.credentials={allow:t.credentials&&t.credentials.allow,deny:[n]},t}!function(t){t.address="address",t.credential3Box="credential3Box"}(oe||(oe={})),function(t){t[t.CreatingDataToken=0]="CreatingDataToken",t[t.DataTokenCreated=1]="DataTokenCreated",t[t.EncryptingFiles=2]="EncryptingFiles",t[t.FilesEncrypted=3]="FilesEncrypted",t[t.StoringDdo=4]="StoringDdo",t[t.DdoStored=5]="DdoStored"}(ae||(ae={})),function(t){t[t.TransferDataToken=0]="TransferDataToken"}(he||(he={}));class Re extends nt{static async getInstance(t){const e=new Re;return e.setInstanceConfig(t),e}create(t,e,i=[],r,n,s,o,a){return r&&!Me.isAddress(r)?(this.logger.error(`Passed Data Token address ${r} is not valid. Aborting publishing.`),null):(this.logger.log("Creating asset"),new P(async h=>{0===i.length&&this.logger.log("You have no services. Are you sure about this?");const{datatokens:u}=this.ocean;if(!r){if(this.logger.log("Creating datatoken"),h.next(ae.CreatingDataToken),r=await u.create("",e.getId(),n,s,o),!Me.isAddress(r))return this.logger.error(`Created Data Token address ${r} is not valid. Aborting publishing.`),null;this.logger.log(`DataToken ${r} created`),h.next(ae.DataTokenCreated)}const l=ot.generate(r);let c;this.logger.log("Encrypting files"),h.next(ae.EncryptingFiles),a?(c=await ft.getInstance(this.instanceConfig),await c.setBaseUrl(a)):c=this.ocean.provider;const d=await c.encrypt(l.getDid(),t.main.files,e);this.logger.log("Files encrypted"),h.next(ae.FilesEncrypted);let f=0;const m=new ut({id:l.getDid(),dataToken:r,authentication:[{type:"RsaSignatureAuthentication2018",publicKey:l.getDid()}],publicKey:[{id:l.getDid(),type:"EthereumECDSAKey",owner:e.getId()}],service:[{type:"metadata",attributes:A({status:{isListed:!0,isRetired:!1,isOrderDisabled:!1}},t,{encryptedFiles:d,main:A({},t.main,{files:t.main.files.map((t,e)=>A({},t,{index:e,url:void 0}))})})},...i].reverse().filter(({type:t},e,i)=>i.findIndex(({type:e})=>e===t)===e).reverse().map(t=>A({},t,{index:f++}))});return await m.addProof(this.ocean,e.getId()),m.dataTokenInfo={name:await u.getName(r),symbol:await u.getSymbol(r),address:r,cap:parseFloat(await u.getCap(r))},m}))}async resolve(t){return this.ocean.metadataCache.retrieveDDO(t)}async editMetadata(t,e){if(!t)return null;for(let r=0;r<t.service.length;r++){var i;"metadata"===t.service[r].type&&(e.title&&(t.service[r].attributes.main.name=e.title),e.author&&(t.service[r].attributes.main.author=e.author),t.service[r].attributes.additionalInformation||(t.service[r].attributes.additionalInformation=Object()),e.description&&(t.service[r].attributes.additionalInformation.description=e.description),t.service[r].attributes.additionalInformation.links=e.links?e.links:[],void 0!==(null==(i=e.status)?void 0:i.isOrderDisabled)&&(t.service[r].attributes.status?t.service[r].attributes.status.isOrderDisabled=e.status.isOrderDisabled:t.service[r].attributes.status={isOrderDisabled:e.status.isOrderDisabled}))}return t}async updateCredentials(t,e,i,r){let n;return n=i&&i.length>0?xe(t,e,i,"allow"):Ae(t,e,"allow"),n=r&&r.length>0?xe(t,e,r,"deny"):Ae(t,e,"deny"),n}checkCredential(t,e,i){let r=0,n="All good",s=!0;if(t.credentials){if(t.credentials.allow&&t.credentials.allow.length>0){const o=t.credentials.allow.find(t=>t.type===e);o&&!o.values.includes(i)&&(r=2,n="Access is denied, your wallet address is not found on allow list",s=!1)}if(t.credentials.deny&&t.credentials.deny.length>0){const o=t.credentials.deny.find(t=>t.type===e);o&&o.values.includes(i)&&(r=3,n="Access is denied, your wallet address is found on deny list",s=!1)}}return{status:r,message:n,result:s}}async publishDdo(t,e,i=!1){return await this.ocean.onChainMetadata.publish(t.id,t,e,i)}async updateMetadata(t,e){return await this.ocean.onChainMetadata.update(t.id,t,e)}async editServiceTimeout(t,e,i){return t?void 0===t.service[e]||i<0?null:(t.service[e].attributes.main.timeout=parseInt(i.toFixed()),t):null}async creator(t){const{did:e,ddo:i}=await tt(t,this.ocean),r=i.getChecksum(),{creator:n,signatureValue:s}=i.proof,o=await this.ocean.utils.signature.verifyText(r,s);return o.toLowerCase()!==n.toLowerCase()&&this.logger.warn(`Owner of ${e} doesn't match. Expected ${n} instead of ${o}.`),n}async getServiceByType(t,e){const{ddo:i}=await tt(t,this.ocean);let r;return i.service.forEach(t=>{t.type.toString()===e&&(r=t)}),r}async getServiceByIndex(t,e){const{ddo:i}=await tt(t,this.ocean);let r;return i.service.forEach(t=>{t.index===e&&(r=t)}),r}async query(t){return this.ocean.metadataCache.queryMetadata(t)}async createAccessServiceAttributes(t,e,i,r=0,n,s){const o={type:"access",index:2,serviceEndpoint:n||this.ocean.provider.url,attributes:{main:{creator:t.getId(),datePublished:i,cost:e,timeout:r,name:"dataAssetAccess"}}};return null!=s&&s.userCustomParameters&&(o.attributes.userCustomParameters=s.userCustomParameters),null!=s&&s.algoCustomParameters&&(o.attributes.algoCustomParameters=s.algoCustomParameters),o}async initialize(t,e,i,r=-1,n,s){const o=await ft.getInstance(this.instanceConfig);await o.setBaseUrl(n);const a=await o.initialize(t,r,e,i,s);return null===a?null:JSON.parse(a)}async order(t,e,i,r=-1,n,s,o,a=!0){let h;const{ddo:u}=await tt(t,this.ocean),l=await this.isConsumable(u,s);if(!l.result)throw new Error("Order asset failed, "+l.message);if(s||(s=i),-1===r?(h=await this.getServiceByType(u,e),r=h.index):(h=await this.getServiceByIndex(u,r),e=h.type),!await this.isUserCustomParametersValid(h.attributes.userCustomParameters,o))throw new Error("Order asset failed, Missing required fiels in userCustomParameters");try{const t=await this.initialize(u,e,i,r,h.serviceEndpoint,o);if(!t)throw new Error("Order asset failed, Failed to initialize service to compute totalCost for ordering");if(a){const e=await this.ocean.datatokens.getPreviousValidOrders(t.dataToken,t.numTokens,r,h.attributes.main.timeout,s);if(e)return e}const l=new X(await this.ocean.datatokens.balance(t.dataToken,i)),c=new X(String(t.numTokens));if(l.isLessThan(c))throw this.logger.error("ERROR: Not enough funds Needed "+c.toString()+" but balance is "+l.toString()),new Error("ERROR: Not enough funds Needed "+c.toString()+" but balance is "+l.toString());const d=await this.ocean.datatokens.startOrder(t.dataToken,s,String(t.numTokens),r,n,i);if(d)return d.transactionHash}catch(t){throw this.logger.error(`ERROR: Failed to order a service : ${t.message}`),new Error(`${t.message}`)}}async download(t,e,i,r,n){const{did:s,ddo:o}=await tt(t,this.ocean),{attributes:a}=o.findServiceByType("metadata"),h=o.findServiceByType("access"),{files:u}=a.main,{serviceEndpoint:l}=h;if(!l)throw new Error("Consume asset failed, service definition is missing the `serviceEndpoint`.");this.logger.log("Consuming files"),n=n?`${n}/datafile.${o.shortId()}.${h.index}/`:void 0;const c=await ft.getInstance(this.instanceConfig);return await c.setBaseUrl(l),await c.download(s,e,i,h.type,h.index.toString(),n,r,u),!0}async simpleDownload(t,e,i,r){let n=e;n+=`?consumerAddress=${r}`,n+=`&tokenAddress=${t}`,n+=`&transferTxId=${i}`;const s=new dt(this.logger);try{await s.downloadFile(n)}catch(t){throw this.logger.error("Error consuming assets"),this.logger.error(t),t}return e}async getOrderHistory(t,e,i){const r=[],n=t.getId().toLowerCase(),{datatokens:s}=this.ocean,o="0x000000000000000000000000"+n.substring(2),a=s.getStartOrderEventSignature(),h=await this.web3.eth.getPastLogs({topics:[a,null,o],fromBlock:i||0,toBlock:"latest"});for(let t=0;t<h.length;t++){const i={dtAddress:h[t].address,timestamp:0,transactionHash:h[t].transactionHash,amount:null,consumer:"0x"+h[t].topics[1].substring(h[t].topics[1].length-40),payer:"0x"+h[t].topics[2].substring(h[t].topics[2].length-40)};try{const n=this.web3.eth.abi.decodeParameters(["uint256","uint256","uint256","uint256"],h[t].data);i.serviceId=parseInt(n[1]),i.timestamp=parseInt(n[2]),i.amount=this.web3.utils.fromWei(n[0]),i.did=T(C(i.dtAddress));const s=await this.getServiceByIndex(i.did,i.serviceId);i.serviceType=s.type,(!e||e&&e===s.type)&&r.push(i)}catch(t){}}return r}async isConsumable(t,e){var i;let r=0,n="All good",s=!0;return t?null!=(i=t.findServiceByType("metadata").attributes.status)&&i.isOrderDisabled?{status:1,message:"Ordering this asset has been temporarily disabled by the publisher.",result:!1}:(e&&({status:r,message:n,result:s}=this.checkCredential(t,oe.address,e)),{status:r,message:n,result:s}):{status:r,message:n,result:s}}async isUserCustomParametersValid(t,e){if(t)for(const i of t){const t=i.name;if(!e||!e[t])return this.logger.error("Missing key: "+t+" from customData"),!1}return!0}}!function(t){t.Loading="Loading",t.Unknown="Unknown",t.Stopped="Stopped",t.Working="Working"}(ue||(ue={}));class ke extends nt{static async getInstance(t){const e=new ke;return e.setInstanceConfig(t),e}async get(){const t={};t.lib={name:"Lib",version:"0.19.0",commit:"9c94a8789c8dc5ca88b798196cfa5fb807811651",status:ue.Working};try{const{software:e,version:i}=await this.ocean.metadataCache.getVersionInfo();t.metadataCache={name:e,status:ue.Working,version:i}}catch(e){t.metadataCache={name:"MetadataCache",status:ue.Stopped}}const e=Object.values(t);return t.status={ok:!e.find(({status:t})=>t!==ue.Working)},t}}class Se{constructor(t,e){this.web3=void 0,this.logger=void 0,this.web3=t,this.logger=e}async signText(t,e,i){const r=this.web3&&this.web3.currentProvider&&this.web3.currentProvider.isMetaMask;try{return await this.web3.eth.personal.sign(t,e,i)}catch(i){if(r)throw i;this.logger.warn("Error on personal sign."),this.logger.warn(i);try{return await this.web3.eth.sign(t,e)}catch(t){throw this.logger.error("Error on sign."),this.logger.error(t),new Error("Error executing personal sign")}}}async signWithHash(t,e,i){const r=this.web3.utils.utf8ToHex(t),n=this.web3&&this.web3.currentProvider&&this.web3.currentProvider.isMetaMask;try{return await this.web3.eth.personal.sign(r,e,i)}catch(t){if(n)throw t;this.logger.warn("Error on personal sign."),this.logger.warn(t);try{return await this.web3.eth.sign(r,e)}catch(t){throw this.logger.error("Error on sign."),this.logger.error(t),new Error("Error executing personal sign")}}}async verifyText(t,e){return this.web3.eth.personal.ecRecover(t,e)}async getHash(t){let e="";for(let i=0;i<t.length;i++)e+=""+t.charCodeAt(i).toString(16);return"0x"+e}async signForAquarius(t,e){const i=await this.getHash(t),r=this.web3&&this.web3.currentProvider&&this.web3.currentProvider.isMetaMask;try{return this.web3.eth.personal.sign(i,e.getId(),e.getPassword())}catch(t){if(r)throw t;return this.logger.warn("Error on personal sign."),this.logger.warn(t),null}}}class Ie extends nt{constructor(...t){super(...t),this.signature=void 0,this.fetch=void 0}static async getInstance(t){const e=new Ie;return e.setInstanceConfig(t),e.signature=new Se(t.web3,t.logger),e.fetch=new dt(t.logger),e}}const Te="/api/v1/aquarius/assets/ddo";class Oe{get url(){return this.metadataCacheUri}constructor(t,e){this.fetch=void 0,this.logger=void 0,this.metadataCacheUri=void 0,this.fetch=new dt(e),this.logger=e,this.metadataCacheUri=t}async getVersionInfo(){return(await this.fetch.get(this.url)).json()}async getAccessUrl(t,e){return await this.fetch.post(`${t.service_endpoint}/${t.resource_id}`,e).then(t=>t.ok?t.text():(this.logger.error("Failed: ",t.status,t.statusText),null)).then(t=>(this.logger.error("Success accessing consume endpoint: ",t),t)).catch(t=>(this.logger.error("Error fetching the data asset consumption url: ",t),null))}async queryMetadata(t){return await this.fetch.post(`${this.url}/api/v1/aquarius/assets/query`,JSON.stringify(t)).then(t=>t.ok?t.json():(this.logger.error("queryMetadata failed:",t.status,t.statusText),null)).then(t=>t).catch(t=>(this.logger.error("Error fetching querying metadata: ",t),null))}async encryptDDO(t){const e=`${this.url}/api/v1/aquarius/assets/ddo/encryptashex `;return await this.fetch.postWithOctet(e,t).then(e=>e.ok?e.text():(this.logger.error("encryptDDO failed:",e.status,e.statusText,t),null)).catch(t=>(this.logger.error("Error encryptDDO: ",t),null))}async validateMetadata(t){const e={valid:!1},i=Y(t)?"/validate-remote":"/validate";try{const r=await this.fetch.post(`${this.url}${Te}${i}`,JSON.stringify(t));if(r.ok){const t=await r.json();!0===t?e.valid=!0:e.errors=t}else this.logger.error("validate Metadata failed:",r.status,r.statusText)}catch(t){this.logger.error("Error validating metadata: ",t)}return e}async retrieveDDO(t,e){t=t&&ot.parse(t);const i=e||`${this.url}${Te}/${t.getDid()}`;return await this.fetch.get(i).then(e=>e.ok?e.json():(this.logger.log("retrieveDDO failed:",e.status,e.statusText,t),null)).then(t=>new ut(t)).catch(t=>(this.logger.error("Error fetching querying metadata: ",t),null))}async retrieveDDOByUrl(t){return this.retrieveDDO(void 0,t)}getServiceEndpoint(t){return`${this.url}/api/v1/aquarius/assets/ddo/did:op:${t.getId()}`}getURI(){return`${this.url}`}sleep(t){return new Promise(e=>{setTimeout(e,t)})}async waitForAqua(t,e){let i=0;do{try{const i=await h(this.getURI()+"/api/v1/aquarius/assets/ddo/"+t);if(i.ok){if(!e)break;{const t=await i.json();if(t.event&&t.event.txid===e)break}}}catch(t){}await this.sleep(1500),i++}while(i<100)}}class Be{constructor(t,e,i=null,r=null,n){this.GASLIMIT_DEFAULT=1e6,this.DDOContractAddress=void 0,this.DDOContractABI=void 0,this.web3=void 0,this.DDOContract=null,this.logger=void 0,this.metadataCache=void 0,this.web3=t,this.DDOContractAddress=i,this.DDOContractABI=r||u.abi,t&&(this.DDOContract=new this.web3.eth.Contract(this.DDOContractABI,this.DDOContractAddress)),this.logger=e,this.metadataCache=n}async compressDDO(t){return l.disableEndMark=!0,l.compress(t,9)}async publish(t,e,i,r=!1,n=!0){if(n&&!(await this.metadataCache.validateMetadata(e)).valid)throw new Error("DDO has failed validation");const s=await this.prepareRawData(e,r);if(s)return this.publishRaw(B(t),s.flags,s.data,i);throw new Error("Could not prepare raw data for publish")}async update(t,e,i,r=!1,n=!0){if(n&&!(await this.metadataCache.validateMetadata(e)).valid)throw new Error("DDO has failed validation");const s=await this.prepareRawData(e,r);if(s)return this.updateRaw(B(t),s.flags,s.data,i);throw new Error("Could not prepare raw data for udate")}async prepareRawData(t,e=!1){let i=0,r=ut.serialize(t);if(!1===e)r=await this.compressDDO(r),i|=1,r=this.getHex(r);else{if(r=await this.metadataCache.encryptDDO(r),!r)return null;i|=2}return{flags:i,data:r}}async publishRaw(t,e,i,r){if(!this.DDOContract)return this.logger.error("ERROR: Missing DDOContract"),null;const n=this.GASLIMIT_DEFAULT;let s;try{s=await this.DDOContract.methods.create(B(t),e,i).estimateGas({from:r},(t,e)=>t?n:e)}catch(t){s=n}try{return await this.DDOContract.methods.create(B(t),e,i).send({from:r,gas:s+1,gasPrice:await Q(this.web3)})}catch(t){return this.logger.error(`ERROR: Failed to publish raw DDO : ${t.message}`),null}}async updateRaw(t,e,i,r){if(!this.DDOContract)return this.logger.error("ERROR: Missing DDOContract"),null;const n=this.GASLIMIT_DEFAULT;let s;try{s=await this.DDOContract.methods.update(B(t),e,i).estimateGas({from:r},(t,e)=>t?n:e)}catch(t){s=n}try{return await this.DDOContract.methods.update(B(t),e,i).send({from:r,gas:s+1,gasPrice:await Q(this.web3)})}catch(t){return this.logger.error(`ERROR: Failed to update raw DDO : ${t.message}`),null}}async transferOwnership(t,e,i){if(!this.DDOContract)return null;try{return await this.DDOContract.methods.transferOwnership(B(t),e).send({from:i})}catch(t){return this.logger.error(`ERROR: Failed to transfer DDO ownership : ${t.message}`),null}}getHex(t){const e=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];let i="";try{for(let r=0;r<t.length;r++)i+=""+e[t[r]>>4&15]+e[15&t[r]]}catch(t){this.logger.error(`ERROR: Failed to get hex message value : ${t.message}`)}return"0x"+i}}class Ce{constructor(t,e,i,r,n,s){this.GASLIMIT_DEFAULT=1e6,this.factoryAddress=void 0,this.factoryABI=void 0,this.datatokensABI=void 0,this.web3=void 0,this.logger=void 0,this.startBlock=void 0,this.factoryAddress=t,this.factoryABI=e||c.abi,this.datatokensABI=i||d.abi,this.web3=r,this.logger=n,this.startBlock=s||0}generateDtName(t){const{name:e,symbol:i}=it(t);return{name:e,symbol:i}}async create(t,e,i,r,n){i||(i="1000"),r&&n||({name:r,symbol:n}=this.generateDtName());const s=new this.web3.eth.Contract(this.factoryABI,this.factoryAddress,{from:e}),o=this.GASLIMIT_DEFAULT;let a;try{a=await s.methods.createToken(t,r,n,this.web3.utils.toWei(i)).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){a=o}const h=await s.methods.createToken(t,r,n,this.web3.utils.toWei(i)).send({from:e,gas:a+1,gasPrice:await Q(this.web3)});let u=null;try{u=h.events.TokenCreated.returnValues[0]}catch(t){this.logger.error(`ERROR: Failed to create datatoken : ${t.message}`)}return u}async approve(t,e,i,r){const n=new this.web3.eth.Contract(this.datatokensABI,t,{from:r}),s=this.GASLIMIT_DEFAULT;let o;try{o=await n.methods.approve(e,this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){o=s}return await n.methods.approve(e,this.web3.utils.toWei(i)).send({from:r,gas:o+1,gasPrice:await Q(this.web3)})}async mint(t,i,r,n){const s=new this.web3.eth.Contract(this.datatokensABI,t,{from:i}),o=await this.getCap(t);if(new e(o).gte(r)){const t=this.GASLIMIT_DEFAULT;let e;try{e=await s.methods.mint(n||i,this.web3.utils.toWei(r)).estimateGas({from:i},(e,i)=>e?t:i)}catch(i){e=t}return await s.methods.mint(n||i,this.web3.utils.toWei(r)).send({from:i,gas:e+1,gasPrice:await Q(this.web3)})}throw new Error("Mint amount exceeds cap available")}async transfer(t,e,i,r){return this.transferToken(t,e,i,r)}async transferToken(t,e,i,r){const n=this.web3.utils.toWei(i);return this.transferWei(t,e,n,r)}async transferWei(t,e,i,r){const n=new this.web3.eth.Contract(this.datatokensABI,t,{from:r}),s=this.GASLIMIT_DEFAULT;let o;try{o=await n.methods.transfer(e,i).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){o=s}return await n.methods.transfer(e,i).send({from:r,gas:o+1,gasPrice:await Q(this.web3)})}async transferFrom(t,e,i,r){const n=new this.web3.eth.Contract(this.datatokensABI,t,{from:r}),s=this.GASLIMIT_DEFAULT;let o;try{o=await n.methods.transferFrom(e,r,this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){o=s}return await n.methods.transferFrom(e,r,this.web3.utils.toWei(i)).send({from:r,gas:o+1,gasPrice:await Q(this.web3)})}async balance(t,e){const i=new this.web3.eth.Contract(this.datatokensABI,t,{from:e}),r=await i.methods.balanceOf(e).call();return this.web3.utils.fromWei(r)}async allowance(t,e,i){const r=new this.web3.eth.Contract(this.datatokensABI,t,{from:i}),n=await r.methods.allowance(e,i).call();return this.web3.utils.fromWei(n)}async getBlob(t){const e=new this.web3.eth.Contract(this.datatokensABI,t);return await e.methods.blob().call()}async getName(t){const e=new this.web3.eth.Contract(this.datatokensABI,t);return await e.methods.name().call()}async getSymbol(t){const e=new this.web3.eth.Contract(this.datatokensABI,t);return await e.methods.symbol().call()}async getCap(t){const e=new this.web3.eth.Contract(this.datatokensABI,t),i=await e.methods.cap().call();return this.web3.utils.fromWei(i)}toWei(t){return this.web3.utils.toWei(t)}fromWei(t){return this.web3.utils.fromWei(t)}async startOrder(t,e,i,r,n,s){const o=new this.web3.eth.Contract(this.datatokensABI,t,{from:s});n||(n="0x0000000000000000000000000000000000000000");try{const t=this.GASLIMIT_DEFAULT;let a;try{a=await o.methods.startOrder(e,this.web3.utils.toWei(i),String(r),n).estimateGas({from:s},(e,i)=>e?t:i)}catch(e){a=t}return await o.methods.startOrder(e,this.web3.utils.toWei(i),String(r),n).send({from:s,gas:a+1,gasPrice:await Q(this.web3)})}catch(t){throw this.logger.error(`ERROR: Failed to start order : ${t.message}`),new Error(`Failed to start order: ${t.message}`)}}async getPreviousValidOrders(t,e,i,r,n){const s=new this.web3.eth.Contract(this.datatokensABI,t,{from:n});let o;r>0?(o=await this.web3.eth.getBlockNumber()-r,o<this.startBlock&&(o=this.startBlock)):o=this.startBlock;const a=await s.getPastEvents("OrderStarted",{filter:{consumer:n},fromBlock:o,toBlock:"latest"});for(let t=0;t<a.length;t++)if(String(a[t].returnValues.amount)===this.web3.utils.toWei(String(e))&&String(a[t].returnValues.serviceId)===String(i)&&a[t].returnValues.consumer.toLowerCase()===n.toLowerCase()){if(0===r)return a[t].transactionHash;const e=await this.web3.eth.getBlock(a[t].blockHash),i=new X(e.timestamp).plus(r);if(new X(Math.floor(Date.now()/1e3)).isLessThan(i))return a[t].transactionHash}return null}getStartOrderEventSignature(){const t=this.datatokensABI.find(function(t){if("OrderStarted"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}async proposeMinter(t,e,i){const r=new this.web3.eth.Contract(this.datatokensABI,t,{from:i}),n=this.GASLIMIT_DEFAULT;let s;try{s=await r.methods.proposeMinter(e).estimateGas({from:i},(t,e)=>t?n:e)}catch(t){s=n}try{return await r.methods.proposeMinter(e).send({from:i,gas:s+1,gasPrice:await Q(this.web3)})}catch(t){return this.logger.error("ERROR: Propose minter failed"),null}}async approveMinter(t,e){const i=new this.web3.eth.Contract(this.datatokensABI,t,{from:e}),r=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.approveMinter().estimateGas({from:e},(t,e)=>t?r:e)}catch(t){n=r}try{return await i.methods.approveMinter().send({from:e,gas:n+1,gasPrice:await Q(this.web3)})}catch(t){return null}}async isMinter(t,e){const i=new this.web3.eth.Contract(this.datatokensABI,t);return await i.methods.isMinter(e).call()}}class _e extends nt{getNetworkId(){return this.web3.eth.net.getId()}async getNetworkName(){return this.web3.eth.net.getId().then(t=>{const e=f(t);return e&&e.name?e.name:"Development"})}}var De;!function(t){t[t.TransferDataToken=0]="TransferDataToken"}(De||(De={})),Object.freeze({WarmingUp:1,Started:10,ConfiguringVolumes:20,ProvisioningSuccess:30,DataProvisioningFailed:31,AlgorithmProvisioningFailed:32,RunningAlgorithm:40,FilteringResults:50,PublishingResult:60,Completed:70,Stopped:80,Deleted:90});class Pe extends nt{static async getInstance(t){const e=new Pe;return e.setInstanceConfig(t),e}async getComputeAddress(t,e=-1){let i,r="compute";-1===e?(i=await this.ocean.assets.getServiceByType(t,r),e=i.index):(i=await this.ocean.assets.getServiceByIndex(t,e),r=i.type);const{serviceEndpoint:n}=i,s=await ft.getInstance(this.instanceConfig);return await s.setBaseUrl(n),s.computeAddress}async start(t,e,i,r,n,s,o,a,h){s=this.checkOutput(r,s);const{did:u,ddo:l}=await tt(t,this.ocean),c=l.findServiceByType("compute"),{serviceEndpoint:d}=c;if(n.serviceIndex){const{ddo:t}=await tt(n.did,this.ocean),e=t.findServiceById(n.serviceIndex);if(!await this.ocean.assets.isUserCustomParametersValid(e.attributes.algoCustomParameters,n.algoCustomParameters))return null}if(u&&e){const t=await ft.getInstance(this.instanceConfig);await t.setBaseUrl(d);const l=await t.computeStart(u,r,n,s,e,o,a,i,h);return l?l[0]:null}return null}async stop(t,e,i){const{did:r,ddo:n}=await tt(e,this.ocean),s=n.findServiceByType("compute"),{serviceEndpoint:o}=s,a=await ft.getInstance(this.instanceConfig);await a.setBaseUrl(o);const h=await a.computeStop(r,t,i);return h?h[0]:null}async delete(t,e,i){const{did:r,ddo:n}=await tt(e,this.ocean),s=n.findServiceByType("compute"),{serviceEndpoint:o}=s,a=await ft.getInstance(this.instanceConfig);await a.setBaseUrl(o);const h=await a.computeDelete(r,t,i);return h?h[0]:null}async status(t,e,i,r,n,s,o=!0){let a;if(e||r||i){if(!r){if(!i&&!(i=await this.ocean.assets.resolve(e)))throw new Error(`Couldn't resolve the did ${e}`);if(!(r=i.findServiceByType("compute")))throw new Error(`Couldn't find a compute service on the asset with did ${e}`)}const{serviceEndpoint:t}=r;a=await ft.getInstance(this.instanceConfig),await a.setBaseUrl(t)}else a=this.ocean.provider;return await a.computeStatus(e,t,n,s,o)}async result(t,e,i){const{did:r,ddo:n}=await tt(e,this.ocean),s=n.findServiceByType("compute"),{serviceEndpoint:o}=s,a=await ft.getInstance(this.instanceConfig);return await a.setBaseUrl(o),(await a.computeStatus(r,t,i,void 0,!0))[0]}createServerAttributes(t,e,i,r,n,s,o,a){return{serverId:t,serverType:e,cost:i,cpu:r,gpu:n,memory:s,disk:o,maxExecutionTime:a}}createContainerAttributes(t,e,i){return{image:t,tag:e,checksum:i}}createClusterAttributes(t,e){return{type:t,url:e}}createProviderAttributes(t,e,i,r,n){return{type:t,description:e,environment:{cluster:i,supportedServers:n,supportedContainers:r}}}createComputeService(t,e,i,r,n,s,o,a){s||(s=3600);const h={type:"compute",index:3,serviceEndpoint:o||this.ocean.provider.url,attributes:{main:{name:"dataAssetComputingService",creator:t.getId(),datePublished:i,cost:e,timeout:s,provider:r,privacy:{}}}};return n&&(h.attributes.main.privacy=n),null!=a&&a.userCustomParameters&&(h.attributes.userCustomParameters=a.userCustomParameters),null!=a&&a.algoCustomParameters&&(h.attributes.algoCustomParameters=a.algoCustomParameters),h}checkOutput(t,e){return e&&(e.publishAlgorithmLog||e.publishOutput)?{publishAlgorithmLog:e.publishAlgorithmLog,publishOutput:e.publishOutput,providerAddress:e.providerAddress||this.config.providerAddress,providerUri:e.providerUri||this.config.providerUri,metadataUri:e.metadataUri||this.config.metadataCacheUri,nodeUri:e.nodeUri||this.config.nodeUri,owner:e.owner||t.getId()}:{publishAlgorithmLog:!1,publishOutput:!1}}async isOrderable(t,e,i,r){const n=await tt(t,this.ocean),s=n.ddo.findServiceById(e);if(!s)return!1;if("compute"===s.type){if(i.meta)return!(!s.attributes.main.privacy||!s.attributes.main.privacy.allowRawAlgorithm)||(this.logger.error("ERROR: This service does not allow raw algorithm"),!1);if(i.did){if(i.serviceIndex){if(!r&&!(r=await this.ocean.assets.resolve(i.did)))throw new Error(`Couldn't resolve the did ${i.did}`);const t=r.findServiceById(i.serviceIndex);if(t&&"compute"===t.type){const e=await ft.getInstance(this.instanceConfig);await e.setBaseUrl(t.serviceEndpoint);const i=await ft.getInstance(this.instanceConfig);if(await i.setBaseUrl(s.serviceEndpoint),e.providerAddress!==i.providerAddress)return this.logger.error("ERROR: Both assets with compute service are not served by the same provider"),!1}}if(s.attributes.main.privacy){if(s.attributes.main.privacy.allowAllPublishedAlgorithms)return!0;if(!s.attributes.main.privacy.publisherTrustedAlgorithms)return!1;let t;for(t of s.attributes.main.privacy.publisherTrustedAlgorithms)if(t.did===i.did){const e=await this.createPublisherTrustedAlgorithmfromDID(i.did);return t.containerSectionChecksum&&t.containerSectionChecksum!==e.containerSectionChecksum?(this.logger.error("ERROR: Algorithm container section was altered since it was added as trusted by "+n.did),!1):!t.filesChecksum||t.filesChecksum===e.filesChecksum||(this.logger.error("ERROR: Algorithm files section was altered since it was added as trusted by "+n.ddo),!1)}return this.logger.error("ERROR: Algorithm "+i.did+" is not allowed by "+n.did),!1}console.error("Algo Index:"+i.serviceIndex)}}return!0}orderAsset(t,e,i,r,n,s,o,a=!0){return new P(async h=>{const{ddo:u}=await tt(e,this.ocean);if(!await this.isOrderable(u,i,r))throw new Error("Dataset order failed, dataset is not orderable with the specified algorithm");const l=u.findServiceById(i);if(!l)throw new Error("Dataset order failed, Could not find service for the DDO");try{return await this.ocean.assets.order(u,l.type,t,-1,n,s,o,a)}catch(t){throw this.logger.error(`ERROR: Failed to order: ${t.message}`),new Error(`Failed to order dataset: ${t.message}`)}})}async orderAlgorithm(t,e,i,r=-1,n,s,o,a=!0){try{return await this.ocean.assets.order(t,e,i,r,n,s,o,a)}catch(t){throw this.logger.error(`ERROR: Failed to orderAlgorithm: ${t.message}`),new Error(`Failed to order algorithm: ${t.message}`)}}async editComputePrivacy(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return null;e=i.index}return void 0===t.service[e]||"compute"!==t.service[e].type?null:(t.service[e].attributes.main.privacy.allowRawAlgorithm=i.allowRawAlgorithm,t.service[e].attributes.main.privacy.allowAllPublishedAlgorithms=i.allowAllPublishedAlgorithms,t.service[e].attributes.main.privacy.allowNetworkAccess=i.allowNetworkAccess,t.service[e].attributes.main.privacy.publisherTrustedAlgorithms=i.publisherTrustedAlgorithms,t)}async toggleAllowAllPublishedAlgorithms(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return null;e=i.index}return void 0===t.service[e]||"compute"!==t.service[e].type?null:(t.service[e].attributes.main.privacy.allowAllPublishedAlgorithms=i,t)}async createPublisherTrustedAlgorithmfromDID(t,e){if(!e&&!(e=await this.ocean.assets.resolve(t)))return null;const i=e.findServiceByType("metadata");return i&&i.attributes.main.algorithm&&i.attributes.encryptedFiles&&i.attributes.main.files?{did:t,containerSectionChecksum:m(JSON.stringify(i.attributes.main.algorithm.container)).toString(),filesChecksum:m(i.attributes.encryptedFiles+JSON.stringify(i.attributes.main.files)).toString()}:null}async addTrustedAlgorithmtoAsset(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return null;e=i.index}if(void 0===t.service[e])return null;if("compute"!==t.service[e].type)return null;t.service[e].attributes.main.privacy.publisherTrustedAlgorithms||(t.service[e].attributes.main.privacy.publisherTrustedAlgorithms=[]);const r=await this.createPublisherTrustedAlgorithmfromDID(i);return r&&t.service[e].attributes.main.privacy.publisherTrustedAlgorithms.push(r),t}async isAlgorithmTrusted(t,e,i){if(!t)return!1;if(-1===e){const i=t.findServiceByType("compute");if(!i)return!1;e=i.index}if(void 0===t.service[e])return!1;if("compute"!==t.service[e].type)return!1;if(t.service[e].attributes.main.privacy.allowAllPublishedAlgorithms)return!0;let r;for(r of t.service[e].attributes.main.privacy.publisherTrustedAlgorithms)if(r.did===i)return!0;return!1}async removeTrustedAlgorithmFromAsset(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return t;e=i.index}return void 0===t.service[e]||"compute"!==t.service[e].type?t:t.service[e].attributes.main.privacy.publisherTrustedAlgorithms?(t.service[e].attributes.main.privacy.publisherTrustedAlgorithms=t.service[e].attributes.main.privacy.publisherTrustedAlgorithms.filter(function(t){return t.did!==i}),t):t}}const Ne="115792089237316195423570985008687907853269984665640564039457584007913129639934";var We,Le,Fe,Ue;!function(t){t[t.CreatingPool=0]="CreatingPool",t[t.ApprovingDatatoken=1]="ApprovingDatatoken",t[t.ApprovingOcean=2]="ApprovingOcean",t[t.SetupPool=3]="SetupPool"}(We||(We={}));class $e extends class extends class{constructor(t,e,i=null,r=null){this.GASLIMIT_DEFAULT=1e6,this.web3=null,this.factoryABI=void 0,this.factoryAddress=void 0,this.logger=void 0,this.web3=t,this.factoryABI=i||g.abi,r&&(this.factoryAddress=r),this.logger=e}async createPool(t){if(null===this.web3)return this.logger.error("ERROR: Web3 object is null"),null;if(null===this.factoryAddress)return this.logger.error("ERROR: bfactoryAddress is null"),null;const e=new this.web3.eth.Contract(this.factoryABI,this.factoryAddress,{from:t});let i=null;const r=this.GASLIMIT_DEFAULT;let n;try{n=await e.methods.newBPool().estimateGas({from:t},(t,e)=>t?r:e)}catch(t){this.logger.log("Error estimate gas newBPool"),this.logger.log(t),n=r}try{i=await e.methods.newBPool().send({from:t,gas:n+1})}catch(t){this.logger.error(`ERROR: Failed to create new pool: ${t.message}`)}return i}}{constructor(t,e,i=null,r=null,n=null){super(t,e,i,n),this.poolABI=void 0,this.poolABI=r||p.abi}async createPool(t){return await super.createPool(t)}async setup(t,e,i,r,n,s,o,a,h){const u=new this.web3.eth.Contract(this.poolABI,e,{from:t});let l=null;const c=this.GASLIMIT_DEFAULT;let d;try{d=await u.methods.setup(i,r,n,s,o,a,h).estimateGas({from:t},(t,e)=>t?c:e)}catch(t){d=c}try{l=await u.methods.setup(i,r,n,s,o,a,h).send({from:t,gas:d,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to setup a pool: ${t.message}`)}return l}async allowance(t,e,i){const r=new this.web3.eth.Contract(d.abi,t,{from:i}),n=await r.methods.allowance(e,i).call();return this.web3.utils.fromWei(n)}async approve(t,i,r,n,s=!1){const o=new this.web3.eth.Contract([{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"}],i,{from:t});if(!s){const s=await this.allowance(i,t,r);if(new e(this.web3.utils.toWei(s)).greaterThanOrEqualTo(n))return s}let a=null;const h=this.GASLIMIT_DEFAULT;let u;try{u=await o.methods.approve(r,n).estimateGas({from:t},(t,e)=>t?h:e)}catch(t){u=h}try{a=await o.methods.approve(r,n).send({from:t,gas:u+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERRPR: Failed to approve spender to spend tokens : ${t.message}`)}return a}async sharesBalance(t,e){let i=null;try{const r=new this.web3.eth.Contract(this.poolABI,e),n=await r.methods.balanceOf(t).call();i=this.web3.utils.fromWei(n)}catch(t){this.logger.error(`ERROR: Failed to get shares of pool : ${t.message}`)}return i}async addToPool(t,e,i){const r=new this.web3.eth.Contract(this.poolABI,e,{from:t});let n;for(n of i)try{await this.approve(t,n.address,e,this.web3.utils.toWei(`${n.amount}`)),await r.methods.bind(n.address,this.web3.utils.toWei(n.amount),this.web3.utils.toWei(n.weight)).send({from:t,gas:this.GASLIMIT_DEFAULT,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to add tokens to pool: ${t.message}`)}}async setSwapFee(t,e,i){const r=new this.web3.eth.Contract(this.poolABI,e,{from:t});let n=null;try{n=await r.methods.setSwapFee(this.web3.utils.toWei(i)).send({from:t,gas:this.GASLIMIT_DEFAULT,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to set pool swap fee: ${t.message}`)}return n}async finalize(t,e){const i=new this.web3.eth.Contract(this.poolABI,e,{from:t});let r=null;try{r=await i.methods.finalize().send({from:t,gas:this.GASLIMIT_DEFAULT,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to finalize pool: ${t.message}`)}return r}async getNumTokens(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{i=await e.methods.getNumTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get number of tokens: ${t.message}`)}return i}async getPoolSharesTotalSupply(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{const t=await e.methods.totalSupply().call();i=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get total supply of pool shares: ${t.message}`)}return i}async getCurrentTokens(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{i=await e.methods.getCurrentTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get tokens composing this pool: ${t.message}`)}return i}async getFinalTokens(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{i=await e.methods.getFinalTokens().call()}catch(t){this.logger.error("ERROR: Failed to get the final tokens composing this pool")}return i}async getController(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{i=await e.methods.getController().call()}catch(t){this.logger.error(`ERROR: Failed to get pool controller address: ${t.message}`)}return i}async setController(t,e,i){const r=new this.web3.eth.Contract(this.poolABI,e,{from:t});let n=null;try{n=await r.methods.setController(i).send({from:t,gas:this.GASLIMIT_DEFAULT})}catch(t){this.logger.error(`ERROR: Failed to set pool controller: ${t.message}`)}return n}async isBound(t,e){const i=new this.web3.eth.Contract(this.poolABI,t);let r=null;try{r=await i.methods.isBound(e).call()}catch(t){this.logger.error(`ERROR: Failed to check whether a token bounded to a pool. ${t.message}`)}return r}async getReserve(t,e){let i=null;try{const r=new this.web3.eth.Contract(this.poolABI,t),n=await r.methods.getBalance(e).call();i=this.web3.utils.fromWei(n)}catch(t){this.logger.error(`ERROR: Failed to get how many tokens are in the pool: ${t.message}`)}return i}async isFinalized(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{i=await e.methods.isFinalized().call()}catch(t){this.logger.error(`ERROR: Failed to check whether pool is finalized: ${t.message}`)}return i}async getSwapFee(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{const t=await e.methods.getSwapFee().call();i=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get pool fee: ${t.message}`)}return i}async getNormalizedWeight(t,e){const i=new this.web3.eth.Contract(this.poolABI,t);let r=null;try{const t=await i.methods.getNormalizedWeight(e).call();r=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get normalized weight of a token: ${t.message}`)}return r}async getDenormalizedWeight(t,e){const i=new this.web3.eth.Contract(this.poolABI,t);let r=null;try{const t=await i.methods.getDenormalizedWeight(e).call();r=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get denormalized weight of a token in pool")}return r}async getTotalDenormalizedWeight(t){const e=new this.web3.eth.Contract(this.poolABI,t);let i=null;try{const t=await e.methods.getTotalDenormalizedWeight().call();i=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get total denormalized weight in pool")}return i}async swapExactAmountIn(t,e,i,r,n,s,o){const a=new this.web3.eth.Contract(this.poolABI,e,{from:t});let h=null;const u=this.GASLIMIT_DEFAULT;let l;try{l=await a.methods.swapExactAmountIn(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Ne).estimateGas({from:t},(t,e)=>t?u:e)}catch(t){this.logger.log("Error estimate gas swapExactAmountIn"),this.logger.log(t),l=u}try{h=await a.methods.swapExactAmountIn(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Ne).send({from:t,gas:l+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return h}async swapExactAmountOut(t,e,i,r,n,s,o){const a=new this.web3.eth.Contract(this.poolABI,e,{from:t});let h=null;const u=this.GASLIMIT_DEFAULT;let l;try{l=await a.methods.swapExactAmountOut(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Ne).estimateGas({from:t},(t,e)=>t?u:e)}catch(t){l=u,this.logger.log("Error estimate gas swapExactAmountIn"),this.logger.log(t)}try{h=await a.methods.swapExactAmountOut(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Ne).send({from:t,gas:l+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount out: ${t.message}`)}return h}async joinPool(t,e,i,r){const n=new this.web3.eth.Contract(this.poolABI,e,{from:t}),s=[];let o;for(o of r)s.push(this.web3.utils.toWei(o));let a=null;const h=this.GASLIMIT_DEFAULT;let u;try{u=await n.methods.joinPool(this.web3.utils.toWei(i),s).estimateGas({from:t},(t,e)=>t?h:e)}catch(t){u=h}try{a=await n.methods.joinPool(this.web3.utils.toWei(i),s).send({from:t,gas:u+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to join pool: ${t.message}`)}return a}async exitPool(t,e,i,r){const n=new this.web3.eth.Contract(this.poolABI,e,{from:t}),s=[];let o;for(o of r)s.push(this.web3.utils.toWei(o));let a=null;const h=this.GASLIMIT_DEFAULT;let u;try{u=await n.methods.exitPool(this.web3.utils.toWei(i),s).estimateGas({from:t},(t,e)=>t?h:e)}catch(t){u=h}try{a=await n.methods.exitPool(this.web3.utils.toWei(i),s).send({from:t,gas:u,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to exit pool: ${t.message}`)}return a}async joinswapExternAmountIn(t,e,i,r,n){const s=new this.web3.eth.Contract(this.poolABI,e,{from:t});let o=null;const a=this.GASLIMIT_DEFAULT;let h;try{h=await s.methods.joinswapExternAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){h=a}try{o=await s.methods.joinswapExternAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:h+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to pay tokens in order to join the pool: ${t.message}`)}return o}async joinswapPoolAmountOut(t,e,i,r,n){const s=new this.web3.eth.Contract(this.poolABI,e,{from:t});let o=null;const a=this.GASLIMIT_DEFAULT;let h;try{h=await s.methods.joinswapPoolAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){h=a}try{o=await s.methods.joinswapPoolAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:h+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error("ERROR: Failed to join swap pool amount out")}return o}async exitswapPoolAmountIn(t,e,i,r,n){const s=new this.web3.eth.Contract(this.poolABI,e,{from:t});let o=null;const a=this.GASLIMIT_DEFAULT;let h;try{h=await s.methods.exitswapPoolAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){h=a}try{o=await s.methods.exitswapPoolAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:h+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to pay pool shares into the pool: ${t.message}`)}return o}async exitswapExternAmountOut(t,e,i,r,n){const s=this.GASLIMIT_DEFAULT,o=new this.web3.eth.Contract(this.poolABI,e,{from:t});let a,h=null;try{a=await o.methods.exitswapExternAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?s:e)}catch(t){a=s}try{h=await o.methods.exitswapExternAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:a+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error("ERROR: Failed to exitswapExternAmountOut")}return h}async getSpotPrice(t,e,i){const r=new this.web3.eth.Contract(this.poolABI,t);let n=null;try{const t=await r.methods.getSpotPrice(e,i).call();n=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get spot price of swapping tokenIn to tokenOut")}return n}async getSpotPriceSansFee(t,e,i){const r=new this.web3.eth.Contract(this.poolABI,t);let n=null;try{const t=await r.methods.getSpotPriceSansFee(e,i).call();n=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to getSpotPriceSansFee")}return n}async calcSpotPrice(t,e,i,r,n,s){const o=new this.web3.eth.Contract(this.poolABI,t);let a="0";try{const t=await o.methods.calcSpotPrice(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s)).call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to call calcSpotPrice")}return a}async calcInGivenOut(t,i,r,n,s,o,a){const h=new this.web3.eth.Contract(this.poolABI,t);let u=null;if(new e(o).gte(n))return null;try{const t=await h.methods.calcInGivenOut(this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o),this.web3.utils.toWei(a)).call();u=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to calcInGivenOut")}return u}async calcOutGivenIn(t,e,i,r,n,s,o){const a=new this.web3.eth.Contract(this.poolABI,t);let h=null;try{const t=await a.methods.calcOutGivenIn(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to calcOutGivenIn")}return h}async calcPoolOutGivenSingleIn(t,e,i,r,n,s,o){const a=new this.web3.eth.Contract(this.poolABI,t);let h=null;try{const t=await a.methods.calcPoolOutGivenSingleIn(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${t.message}`)}return h}async calcSingleInGivenPoolOut(t,e,i,r,n,s,o){const a=new this.web3.eth.Contract(this.poolABI,t);let h=null;try{const t=await a.methods.calcSingleInGivenPoolOut(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${t.message}`)}return h}async calcSingleOutGivenPoolIn(t,e,i,r,n,s,o){const a=new this.web3.eth.Contract(this.poolABI,t);let h=null;try{const t=await a.methods.calcSingleOutGivenPoolIn(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${t.message}`)}return h}async calcPoolInGivenSingleOut(t,e,i,r,n,s,o){const a=new this.web3.eth.Contract(this.poolABI,t);let h=null;try{const t=await a.methods.calcPoolInGivenSingleOut(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${t.message}`)}return h}getSwapEventSignature(){const t=this.poolABI.find(function(t){if("LOG_SWAP"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getJoinEventSignature(){const t=this.poolABI.find(function(t){if("LOG_JOIN"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getExitEventSignature(){const t=this.poolABI.find(function(t){if("LOG_EXIT"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}}{constructor(t,e,i=null,r=null,n=null,s=null,o){super(t,e,i,r,n),this.oceanAddress=null,this.dtAddress=null,this.startBlock=void 0,s&&(this.oceanAddress=s),this.startBlock=o||0}create(t,e,i,r,n,s){if(null==this.oceanAddress)throw this.logger.error("ERROR: oceanAddress is not defined"),new Error("ERROR: oceanAddress is not defined");if(parseFloat(s)>.1)throw this.logger.error("ERROR: Swap fee too high. The maximum allowed swapFee is 10%"),new Error("ERROR: Swap fee too high. The maximum allowed swapFee is 10%");if(parseFloat(i)<2)throw this.logger.error("ERROR: Amount of DT is too low"),new Error("ERROR: Amount of DT is too low");if(parseFloat(r)>9||parseFloat(r)<1)throw this.logger.error("ERROR: Weight out of bounds (min 1, max9)"),new Error("ERROR: Weight out of bounds (min 1, max9)");return new P(async o=>{o.next(We.CreatingPool);const a=await super.createPool(t);if(!a)throw this.logger.error("ERROR: Failed to call create pool"),new Error("ERROR: Failed to call create pool");const h=a.events.BPoolRegistered.returnValues[0],u=10-parseFloat(r);let l;if(this.dtAddress=e,o.next(We.ApprovingDatatoken),l=await this.approve(t,e,h,this.web3.utils.toWei(String(i))),!l)throw this.logger.error("ERROR: Failed to call approve DT token"),new Error("ERROR: Failed to call approve DT token");if(o.next(We.ApprovingOcean),l=await this.approve(t,this.oceanAddress,h,this.web3.utils.toWei(String(n))),!l)throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");if(o.next(We.SetupPool),l=await super.setup(t,h,e,this.web3.utils.toWei(String(i)),this.web3.utils.toWei(String(r)),this.oceanAddress,this.web3.utils.toWei(String(n)),this.web3.utils.toWei(String(u)),this.web3.utils.toWei(s)),!l)throw this.logger.error("ERROR: Failed to create a new pool"),new Error("ERROR: Failed to create a new pool");return a})}async getDTAddress(t){this.dtAddress=null;const e=await this.getCurrentTokens(t);let i;if(null!=e)for(i of e)i.toLowerCase()!==this.oceanAddress.toLowerCase()&&(this.dtAddress=i);return this.dtAddress}async getOceanReserve(t){return null==this.oceanAddress?(this.logger.error("ERROR: oceanAddress is not defined"),null):super.getReserve(t,this.oceanAddress)}async getDTReserve(t){const e=await this.getDTAddress(t);return super.getReserve(t,e)}async getMaxBuyQuantity(t,i){const r=await super.getReserve(t,i);return new e(r).div(3).toString()}async getOceanMaxBuyQuantity(t){return this.getMaxBuyQuantity(t,this.oceanAddress)}async getDTMaxBuyQuantity(t){return this.getMaxBuyQuantity(t,await this.getDTAddress(t))}async calcInGivenOut(t,e,i,r){return await super.calcInGivenOut(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getReserve(t,i),await super.getDenormalizedWeight(t,i),r,await this.getSwapFee(t))}async calcOutGivenIn(t,e,i,r){return await super.calcOutGivenIn(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getReserve(t,i),await super.getDenormalizedWeight(t,i),r,await super.getSwapFee(t))}async calcPoolOutGivenSingleIn(t,e,i){return super.calcPoolOutGivenSingleIn(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async calcSingleInGivenPoolOut(t,e,i){return super.calcSingleInGivenPoolOut(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async calcSingleOutGivenPoolIn(t,e,i){return super.calcSingleOutGivenPoolIn(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async calcPoolInGivenSingleOut(t,e,i){return super.calcPoolInGivenSingleOut(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async getPoolSharesRequiredToRemoveDT(t,e){const i=await this.getDTAddress(t);return this.calcPoolInGivenSingleOut(t,i,e)}async getDTRemovedforPoolShares(t,e){const i=await this.getDTAddress(t);return this.calcSingleOutGivenPoolIn(t,i,e)}async getPoolSharesRequiredToRemoveOcean(t,e){return this.calcPoolInGivenSingleOut(t,this.oceanAddress,e)}async getOceanRemovedforPoolShares(t,e){return this.calcSingleOutGivenPoolIn(t,this.oceanAddress,e)}async getTokensRemovedforPoolShares(t,i){try{const r=await this.getPoolSharesTotalSupply(t),n=await this.getDTReserve(t),s=await this.getOceanReserve(t);return{dtAmount:new e(i).div(r).mul(n).toString(),oceanAmount:new e(i).div(r).mul(s).toString()}}catch(t){this.logger.error(`ERROR: Unable to get token info. ${t.message}`)}}async getDTMaxAddLiquidity(t){const e=await this.getDTAddress(t);return this.getMaxAddLiquidity(t,e)}async getOceanMaxAddLiquidity(t){return this.getMaxAddLiquidity(t,this.oceanAddress)}async getMaxAddLiquidity(t,i){const r=await super.getReserve(t,i);return parseFloat(r)>0?new e(r).mul(.25).toString():"0"}async getMaxRemoveLiquidity(t,i){const r=await super.getReserve(t,i);return parseFloat(r)>0?new e(r).mul(.25).toString():"0"}async getDTMaxRemoveLiquidity(t){const e=await this.getDTAddress(t);return this.getMaxRemoveLiquidity(t,e)}async getOceanMaxRemoveLiquidity(t){return this.getMaxRemoveLiquidity(t,this.oceanAddress)}async buyDT(t,i,r,n,s){if(null==this.oceanAddress)return this.logger.error("ERROR: undefined ocean token contract address"),null;const o=await this.getDTAddress(i);if(new e(r).greaterThan(await this.getDTMaxBuyQuantity(i)))return this.logger.error("ERROR: Buy quantity exceeds quantity allowed"),null;const a=await this.getOceanNeeded(i,r);if(new e(a).greaterThan(n))return this.logger.error("ERROR: Not enough Ocean Tokens"),null;if(!await super.approve(t,this.oceanAddress,i,this.web3.utils.toWei(n)))throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");return await super.swapExactAmountOut(t,i,this.oceanAddress,n,o,r,s)}async buyDTWithExactOcean(t,i,r,n,s){if(null==this.oceanAddress)return this.logger.error("ERROR: undefined ocean token contract address"),null;const o=await this.getDTAddress(i);if(new e(r).greaterThan(await this.getDTMaxBuyQuantity(i)))return this.logger.error("ERROR: Buy quantity exceeds quantity allowed"),null;const a=await this.getOceanNeeded(i,r);if(new e(a).greaterThan(n))return this.logger.error("ERROR: Not enough Ocean Tokens"),null;if(!await super.approve(t,this.oceanAddress,i,this.web3.utils.toWei(n)))throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");return await super.swapExactAmountIn(t,i,this.oceanAddress,n,o,r,s)}async sellDT(t,i,r,n,s){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const o=await this.getDTAddress(i);if(new e(n).greaterThan(await this.getOceanMaxBuyQuantity(i)))return this.logger.error("ERROR: Buy quantity exceeds quantity allowed"),null;const a=await this.getOceanReceived(i,r);if(new e(a).lessThan(n))return this.logger.error("ERROR: Not enough datatokens"),null;if(!await super.approve(t,o,i,this.web3.utils.toWei(r)))throw this.logger.error("ERROR: Failed to call approve DT token"),new Error("ERROR: Failed to call approve DT token");return await super.swapExactAmountIn(t,i,o,r,this.oceanAddress,n,s)}async addDTLiquidity(t,i,r){const n=await this.getDTAddress(i),s=await this.getMaxAddLiquidity(i,n);if(new e(r).greaterThan(s))return this.logger.error("ERROR: Too much reserve to add"),null;if(!await super.approve(t,n,i,this.web3.utils.toWei(r)))throw this.logger.error("ERROR: Failed to call approve DT token"),new Error("ERROR: Failed to call approve DT token");return await super.joinswapExternAmountIn(t,i,n,r,"0")}async removeDTLiquidity(t,i,r,n){const s=await this.getDTAddress(i),o=await this.getDTMaxRemoveLiquidity(i);if(new e(r).greaterThan(o))return this.logger.error("ERROR: Too much reserve to remove"),null;const a=await this.sharesBalance(t,i);if(new e(a).lessThan(n))return this.logger.error("ERROR: Not enough poolShares"),null;const h=await this.getPoolSharesRequiredToRemoveDT(i,r);return new e(n).lessThan(h)?(this.logger.error("ERROR: Not enough poolShares"),null):(new e(n).lessThan(h)&&(n=new e(n).mul(.9999).toString()),this.exitswapExternAmountOut(t,i,s,r,n))}async addOceanLiquidity(t,i,r){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const n=await this.getOceanMaxAddLiquidity(i);if(new e(r).greaterThan(n))return this.logger.error("ERROR: Too much reserve to add"),null;if(!await super.approve(t,this.oceanAddress,i,this.web3.utils.toWei(r)))throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");return await super.joinswapExternAmountIn(t,i,this.oceanAddress,r,"0")}async removeOceanLiquidityWithMinimum(t,i,r,n){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const s=await this.sharesBalance(t,i);return new e(s).lessThan(r)?(this.logger.error("ERROR: Not enough poolShares"),null):super.exitswapPoolAmountIn(t,i,this.oceanAddress,r,n)}async removeOceanLiquidity(t,i,r,n){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const s=await this.getOceanMaxRemoveLiquidity(i);if(new e(r).greaterThan(s))return this.logger.error("ERROR: Too much reserve to remove"),null;const o=await this.sharesBalance(t,i);if(new e(o).lessThan(n))return this.logger.error("ERROR: Not enough poolShares"),null;const a=await this.getPoolSharesRequiredToRemoveOcean(i,r);return new e(n).lessThan(a)?(this.logger.error("ERROR: Not enough poolShares"),null):(new e(n).lessThan(a)&&(n=new e(n).mul(.9999).toString()),super.exitswapExternAmountOut(t,i,this.oceanAddress,r,n))}async removePoolLiquidity(t,i,r,n="0",s="0"){const o=await this.sharesBalance(t,i);return new e(o).lessThan(r)?(this.logger.error("ERROR: Not enough poolShares"),null):(new e(o).equals(r)&&(r=new e(r).mul(.9999).toString()),this.exitPool(t,i,r,[n,s]))}async getDTPrice(t){return null==this.oceanAddress?(this.logger.error("ERROR: oceanAddress is not defined"),"0"):this.getOceanNeeded(t,"1")}async searchPoolforDT(t){const e=[],i=new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),r=await i.getPastEvents("BPoolRegistered",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});r.sort((t,e)=>t.blockNumber>e.blockNumber?1:-1);for(let i=0;i<r.length;i++)(await super.getCurrentTokens(r[i].returnValues[0])).includes(t)&&e.push(r[i].returnValues[0]);return e}async getOceanNeeded(t,i){const r=await this.getDTAddress(t);return new e(i).greaterThan(await this.getDTMaxBuyQuantity(t))?"0":this.calcInGivenOut(t,this.oceanAddress,r,i)}async getOceanReceived(t,e){const i=await this.getDTAddress(t);return this.calcOutGivenIn(t,i,this.oceanAddress,e)}async getDTReceived(t,e){const i=await this.getDTAddress(t);return this.calcOutGivenIn(t,this.oceanAddress,i,e)}async getDTNeeded(t,i){const r=await this.getDTAddress(t);return new e(i).greaterThan(await this.getOceanMaxBuyQuantity(t))?"0":this.calcInGivenOut(t,r,this.oceanAddress,i)}async getPoolsbyCreator(t){const e=[],i=new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),r=await i.getPastEvents("BPoolRegistered",{filter:t?{registeredBy:t}:{},fromBlock:this.startBlock,toBlock:"latest"});for(let i=0;i<r.length;i++)t&&r[i].returnValues[1].toLowerCase()!==t.toLowerCase()||e.push(await this.getPoolDetails(r[i].returnValues[0]));return e}async getResult(t,e){const i=await super.sharesBalance(t,e.returnValues[0]);if(parseFloat(i)>0){const t=await this.getDTAddress(e.returnValues[0]);if(t)return{shares:i,poolAddress:e.returnValues[0],did:T(C(t))}}}async getPoolSharesByAddress(t){const e=[],i=new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),r=await i.getPastEvents("BPoolRegistered",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});let n=[];for(let i=0;i<r.length;i++)if(n.push(this.getResult(t,r[i])),n.length>10){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}if(n.length>0){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}return e.filter(t=>void 0!==t)}async getPoolDetails(t){return{poolAddress:t,tokens:await super.getFinalTokens(t)}}async getPoolLogs(t,e=0,i){const r=[],n=await this.getDTAddress(t);0===e&&(e=this.startBlock);const s=super.getSwapEventSignature(),o=super.getJoinEventSignature(),a=super.getExitEventSignature();let h;h=i?"0x000000000000000000000000"+i.substring(2).toLowerCase():null;const u=await this.web3.eth.getPastLogs({address:t,topics:[[s,o,a],h],fromBlock:e,toBlock:"latest"});let l=[];for(let e=0;e<u.length;e++)if(l.push(this.getEventData(t,n,u[e])),l.length>10){const t=await Promise.all(l);for(let e=0;e<t.length;e++)r.push(t[e]);l=[]}if(l.length>0){const t=await Promise.all(l);for(let e=0;e<t.length;e++)r.push(t[e]);l=[]}return r.filter(t=>void 0!==t)}async getAllPoolLogs(t){const e=[],i=new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),r=await i.getPastEvents("BPoolRegistered",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});let n=[];for(let i=0;i<r.length;i++)if(n.push(this.getPoolLogs(r[i].returnValues[0],r[i].blockNumber,t)),n.length>10){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}if(n.length>0){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}return e.reduce((t,e)=>t.concat(e))}async getEventData(t,e,i){const r=await this.web3.eth.getBlock(i.blockNumber),n=super.getSwapEventSignature(),s=super.getJoinEventSignature(),o=super.getExitEventSignature();let a;switch(i.topics[0]){case n:a="swap";break;case s:a="join";break;case o:a="exit"}let h,u={poolAddress:t,dtAddress:e,caller:i.topics[1],transactionHash:i.transactionHash,blockNumber:i.blockNumber,timestamp:parseInt(String(r.timestamp)),type:a};switch(a){case"swap":h=this.web3.eth.abi.decodeParameters(["uint256","uint256"],i.data),u=A({},u,{tokenIn:"0x"+i.topics[2].substring(i.topics[2].length-40),tokenOut:"0x"+i.topics[3].substring(i.topics[3].length-40),tokenAmountIn:this.web3.utils.fromWei(h[0]),tokenAmountOut:this.web3.utils.fromWei(h[1])});break;case"join":h=this.web3.eth.abi.decodeParameters(["uint256"],i.data),u=A({},u,{tokenIn:"0x"+i.topics[2].substring(i.topics[2].length-40),tokenAmountIn:this.web3.utils.fromWei(h[0])});break;case"exit":h=this.web3.eth.abi.decodeParameters(["uint256"],i.data),u=A({},u,{tokenOut:"0x"+i.topics[2].substring(i.topics[2].length-40),tokenAmountOut:this.web3.utils.fromWei(h[0])})}return u}async computeSlippage(t,i,r,n,s,o,a,h){const u=await super.calcSpotPrice(t,i,r,n,s,h),l=await super.calcSpotPrice(t,o,r,a,s,h);return new e(l).mul(100).div(u).minus(100).toString()}async computeBuySlippage(t,e){const i=await this.getDTAddress(t),r=await super.getDenormalizedWeight(t,i),n=await super.getDenormalizedWeight(t,this.oceanAddress),s=await super.getReserve(t,i),o=await super.getReserve(t,i),a=await super.getSwapFee(t),h=await super.calcOutGivenIn(t,o,n,s,r,e,a),u=new X(this.web3.utils.toWei(s)).minus(this.web3.utils.toWei(h)),l=new X(this.web3.utils.toWei(o)).plus(this.web3.utils.toWei(e));return await this.computeSlippage(t,o,n,s,r,this.web3.utils.fromWei(l.toString()),this.web3.utils.fromWei(u.toString()),a)}async computeSellSlippage(t,e){const i=await this.getDTAddress(t),r=await super.getDenormalizedWeight(t,i),n=await super.getDenormalizedWeight(t,this.oceanAddress),s=await super.getReserve(t,i),o=await super.getReserve(t,i),a=await super.getSwapFee(t),h=await super.calcOutGivenIn(t,s,r,o,n,e,a),u=new X(this.web3.utils.toWei(s)).plus(this.web3.utils.toWei(e)),l=new X(this.web3.utils.toWei(o)).minus(this.web3.utils.toWei(h));return await this.computeSlippage(t,s,r,o,n,this.web3.utils.fromWei(u.toString()),this.web3.utils.fromWei(l.toString()),a)}}!function(t){t[t.CreatingExchange=0]="CreatingExchange",t[t.ApprovingDatatoken=1]="ApprovingDatatoken"}(Le||(Le={}));class je{constructor(t,e,i=null,r=null,n=null,s,o){this.GASLIMIT_DEFAULT=1e6,this.oceanAddress=null,this.fixedRateExchangeAddress=void 0,this.fixedRateExchangeABI=void 0,this.web3=void 0,this.contract=null,this.logger=void 0,this.datatokens=void 0,this.startBlock=void 0,this.web3=t,this.fixedRateExchangeAddress=i,this.startBlock=o||0,this.fixedRateExchangeABI=r||w.abi,this.oceanAddress=n,this.datatokens=s,t&&(this.contract=new this.web3.eth.Contract(this.fixedRateExchangeABI,this.fixedRateExchangeAddress)),this.logger=e}create(t,e,i,r){return this.createExchange(this.oceanAddress,t,e,i,r)}createExchange(t,e,i,r,n){return new P(async s=>{let o;s.next(Le.CreatingExchange);const a=this.GASLIMIT_DEFAULT;try{o=await this.contract.methods.create(t,e,this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?a:e)}catch(t){o=a}let h=null,u=null;try{u=await this.contract.methods.create(t,e,this.web3.utils.toWei(i)).send({from:r,gas:o+1,gasPrice:await Q(this.web3)}),h=u.events.ExchangeCreated.returnValues[0]}catch(t){this.logger.error(`ERROR: Failed to create new exchange: ${t.message}`)}return n&&h&&(s.next(Le.ApprovingDatatoken),this.datatokens.approve(e,this.fixedRateExchangeAddress,n,r)),u})}async generateExchangeId(t,e){return await this.contract.methods.generateExchangeId(this.oceanAddress,t,e).call()}async buyDT(t,e,i){const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.swap(t,this.web3.utils.toWei(String(e))).estimateGas({from:i},(t,e)=>t?r:e)}catch(t){n=r}try{return await this.contract.methods.swap(t,this.web3.utils.toWei(String(e))).send({from:i,gas:n+1,gasPrice:await Q(this.web3)})}catch(t){return this.logger.error(`ERROR: Failed to buy datatokens: ${t.message}`),null}}async getNumberOfExchanges(){return await this.contract.methods.getNumberOfExchanges().call()}async setRate(t,e,i){const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.setRate(t,this.web3.utils.toWei(String(e))).estimateGas({from:i},(t,e)=>t?r:e)}catch(t){n=r}return await this.contract.methods.setRate(t,this.web3.utils.toWei(String(e))).send({from:i,gas:n+1,gasPrice:await Q(this.web3)})}async activate(t,e){const i=await this.getExchange(t);if(!i)return null;if(!0===i.active)return null;const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.toggleExchangeState(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){n=r}return await this.contract.methods.toggleExchangeState(t).send({from:e,gas:n+1,gasPrice:await Q(this.web3)})}async deactivate(t,e){const i=await this.getExchange(t);if(!i)return null;if(!1===i.active)return null;const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.toggleExchangeState(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){n=r}return await this.contract.methods.toggleExchangeState(t).send({from:e,gas:n+1,gasPrice:await Q(this.web3)})}async getRate(t){const e=await this.contract.methods.getRate(t).call();return this.web3.utils.fromWei(e)}async getSupply(t){const e=await this.contract.methods.getSupply(t).call();return this.web3.utils.fromWei(e)}async getOceanNeeded(t,e){const i=await this.contract.methods.CalcInGivenOut(t,this.web3.utils.toWei(e)).call();return this.web3.utils.fromWei(i)}async getExchange(t){const e=await this.contract.methods.getExchange(t).call();return e.fixedRate=this.web3.utils.fromWei(e.fixedRate),e.supply=this.web3.utils.fromWei(e.supply),e.exchangeID=t,e}async getExchanges(){return await this.contract.methods.getExchanges().call()}async isActive(t){return await this.contract.methods.isActive(t).call()}async CalcInGivenOut(t,e){const i=await this.contract.methods.CalcInGivenOut(t,this.web3.utils.toWei(e)).call();return this.web3.utils.fromWei(i)}async searchforDT(t,e){const i=[],r=await this.contract.getPastEvents("ExchangeCreated",{filter:{datatoken:t.toLowerCase()},fromBlock:this.startBlock,toBlock:"latest"});let n=[];for(let s=0;s<r.length;s++)if(n.push(this.getExchange(r[s].returnValues[0])),n.length>10||s===r.length-1){const r=await Promise.all(n);for(let n=0;n<r.length;n++){const s=r[n];if(!0===s.active&&s.dataToken.toLowerCase()===t.toLowerCase()){const t=new X(s.supply),r=new X(e);t.gte(r)&&i.push(s)}}n=[]}return i}async getExchangesbyCreator(t){const e=[],i=await this.contract.getPastEvents("ExchangeCreated",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});for(let r=0;r<i.length;r++)t&&i[r].returnValues[3].toLowerCase()!==t.toLowerCase()||e.push(await this.getExchange(i[r].returnValues[0]));return e}async getExchangeSwaps(t,e){const i=[],r=await this.contract.getPastEvents("Swapped",{filter:{exchangeId:t},fromBlock:this.startBlock,toBlock:"latest"});for(let t=0;t<r.length;t++)e&&r[t].returnValues[1].toLowerCase()!==e.toLowerCase()||i.push(this.getEventData(r[t]));return i}async getAllExchangesSwaps(t){const e=[],i=await this.contract.getPastEvents("ExchangeCreated",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});for(let r=0;r<i.length;r++)(await this.getExchangeSwaps(i[r].returnValues[0],t)).forEach(t=>{e.push(t)});return e}getEventData(t){return{exchangeID:t.returnValues[0],caller:t.returnValues[1],baseTokenAmount:this.web3.utils.fromWei(t.returnValues[2]),dataTokenAmount:this.web3.utils.fromWei(t.returnValues[3])}}}!function(t){t[t.MakeDispenserMinter=0]="MakeDispenserMinter",t[t.AcceptingNewMinter=1]="AcceptingNewMinter"}(Fe||(Fe={})),function(t){t[t.MakeOwnerMinter=0]="MakeOwnerMinter",t[t.AcceptingNewMinter=1]="AcceptingNewMinter"}(Ue||(Ue={}));class qe{constructor(t,e,i=null,r=null,n,s){this.GASLIMIT_DEFAULT=1e6,this.dispenserAddress=void 0,this.dispenserABI=void 0,this.web3=void 0,this.contract=null,this.logger=void 0,this.datatokens=void 0,this.startBlock=void 0,this.web3=t,this.dispenserAddress=i,this.startBlock=s||0,this.dispenserABI=r||v.abi,this.datatokens=n,t&&(this.contract=new this.web3.eth.Contract(this.dispenserABI,this.dispenserAddress)),this.logger=e}async status(t){try{const e=await this.contract.methods.status(t).call();return e.maxTokens=this.web3.utils.fromWei(e.maxTokens),e.maxBalance=this.web3.utils.fromWei(e.maxBalance),e.balance=this.web3.utils.fromWei(e.balance),e}catch(e){this.logger.warn(`No dispenser available for data token: ${t}`)}return null}async activate(t,e,i,r){let n;const s=this.GASLIMIT_DEFAULT;try{n=await this.contract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){n=s}let o=null;try{o=await this.contract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(i)).send({from:r,gas:n+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return o}async deactivate(t,e){let i;const r=this.GASLIMIT_DEFAULT;try{i=await this.contract.methods.deactivate(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){i=r}let n=null;try{n=await this.contract.methods.deactivate(t).send({from:e,gas:i+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to deactivate dispenser: ${t.message}`)}return n}makeMinter(t,e){return new P(async i=>{let r;i.next(Fe.MakeDispenserMinter);const n=this.GASLIMIT_DEFAULT;if(!await this.datatokens.proposeMinter(t,this.dispenserAddress,e))return null;i.next(Fe.AcceptingNewMinter);try{r=await this.contract.methods.acceptMinter(t).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}let s=null;try{s=await this.contract.methods.acceptMinter(t).send({from:e,gas:r+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to accept minter role: ${t.message}`)}return s})}cancelMinter(t,e){return new P(async i=>{let r;i.next(Ue.MakeOwnerMinter);const n=this.GASLIMIT_DEFAULT;try{r=await this.contract.methods.removeMinter(t).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}let s=null;try{s=await this.contract.methods.removeMinter(t).send({from:e,gas:r+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to remove minter role: ${t.message}`)}return s?(i.next(Ue.AcceptingNewMinter),await this.datatokens.approveMinter(t,e)):null})}async dispense(t,e,i="1"){let r;const n=this.GASLIMIT_DEFAULT;try{r=await this.contract.methods.dispense(t,this.web3.utils.toWei(i)).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}let s=null;try{s=await this.contract.methods.dispense(t,this.web3.utils.toWei(i)).send({from:e,gas:r+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to dispense tokens: ${t.message}`)}return s}async ownerWithdraw(t,e){let i;const r=this.GASLIMIT_DEFAULT;try{i=await this.contract.methods.ownerWithdraw(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){i=r}let n=null;try{n=await this.contract.methods.ownerWithdraw(t).send({from:e,gas:i+1,gasPrice:await Q(this.web3)})}catch(t){this.logger.error(`ERROR: Failed to withdraw tokens: ${t.message}`)}return n}async isDispensable(t,i,r="1"){const n=await this.status(t);return!(!n||!1===n.active||new e(await this.datatokens.balance(t,i)).greaterThanOrEqualTo(n.maxBalance)||new e(String(r)).greaterThan(n.maxTokens)||!new e(n.balance).greaterThanOrEqualTo(r)&&!0!==n.isTrueMinter)}}class Ge extends nt{constructor(...t){super(...t),this.network=void 0,this.provider=void 0,this.web3Provider=void 0,this.metadataCache=void 0,this.onChainMetadata=void 0,this.accounts=void 0,this.assets=void 0,this.compute=void 0,this.datatokens=void 0,this.pool=void 0,this.fixedRateExchange=void 0,this.OceanDispenser=void 0,this.versions=void 0,this.utils=void 0}static async getInstance(t){const e=new Ge,i=A({},function(t){return{config:t,web3:t.web3Provider,logger:R}}(t),{ocean:e});return e.setInstanceConfig(i),e.utils=await Ie.getInstance(i),e.provider=await ft.getInstance(i),e.metadataCache=new Oe(i.config.metadataCacheUri,i.logger),e.accounts=await at.getInstance(i),e.assets=await Re.getInstance(i),e.compute=await Pe.getInstance(i),e.datatokens=new Ce(i.config.factoryAddress,i.config.factoryABI,i.config.datatokensABI,i.config.web3Provider,i.logger,i.config.startBlock),e.pool=new $e(i.config.web3Provider,i.logger,i.config.poolFactoryABI,i.config.poolABI,i.config.poolFactoryAddress,i.config.oceanTokenAddress,i.config.startBlock),e.fixedRateExchange=new je(i.config.web3Provider,i.logger,i.config.fixedRateExchangeAddress,i.config.fixedRateExchangeAddressABI,i.config.oceanTokenAddress,e.datatokens,i.config.startBlock),e.OceanDispenser=new qe(i.config.web3Provider,i.logger,i.config.dispenserAddress,i.config.dispenserABI,e.datatokens,i.config.startBlock),e.onChainMetadata=new Be(i.config.web3Provider,i.logger,i.config.metadataContractAddress,i.config.metadataContractABI,e.metadataCache),e.versions=await ke.getInstance(i),e.network=new _e,e}}const ze={networkId:null,network:"unknown",metadataCacheUri:"https://aquarius.oceanprotocol.com",nodeUri:"http://localhost:8545",providerUri:"http://127.0.0.1:8030",subgraphUri:null,explorerUri:null,oceanTokenAddress:null,oceanTokenSymbol:"OCEAN",factoryAddress:"0x1234",poolFactoryAddress:null,fixedRateExchangeAddress:null,dispenserAddress:null,metadataContractAddress:null,startBlock:0},He=[A({},ze),A({},ze,{networkId:8996,network:"development",metadataCacheUri:"http://127.0.0.1:5000"}),A({},ze,{networkId:3,network:"ropsten",nodeUri:"https://ropsten.infura.io/v3",providerUri:"https://provider.ropsten.oceanprotocol.com",subgraphUri:"https://subgraph.ropsten.oceanprotocol.com",explorerUri:"https://ropsten.etherscan.io",startBlock:9227563}),A({},ze,{networkId:4,network:"rinkeby",nodeUri:"https://rinkeby.infura.io/v3",providerUri:"https://provider.rinkeby.oceanprotocol.com",subgraphUri:"https://subgraph.rinkeby.oceanprotocol.com",explorerUri:"https://rinkeby.etherscan.io",startBlock:7294090}),A({},ze,{networkId:1,network:"mainnet",nodeUri:"https://mainnet.infura.io/v3",providerUri:"https://provider.mainnet.oceanprotocol.com",subgraphUri:"https://subgraph.mainnet.oceanprotocol.com",explorerUri:"https://etherscan.io",startBlock:11105459}),A({},ze,{networkId:137,network:"polygon",nodeUri:"https://polygon-mainnet.infura.io/v3",providerUri:"https://provider.polygon.oceanprotocol.com",subgraphUri:"https://subgraph.polygon.oceanprotocol.com",explorerUri:"https://polygonscan.com",oceanTokenSymbol:"mOCEAN",startBlock:11005222}),A({},ze,{networkId:1287,network:"moonbeamalpha",nodeUri:"https://rpc.testnet.moonbeam.network",providerUri:"https://provider.moonbeamalpha.oceanprotocol.com",subgraphUri:"https://subgraph.moonbeamalpha.oceanprotocol.com",explorerUri:"https://moonbase-blockscout.testnet.moonbeam.network/",startBlock:90707}),A({},ze,{networkId:2021e3,network:"gaiaxtestnet",nodeUri:"https://rpc.gaiaxtestnet.oceanprotocol.com",providerUri:"https://provider.gaiaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.gaiaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.gaiaxtestnet.oceanprotocol.com"}),A({},ze,{networkId:2021001,network:"catenaxtestnet",nodeUri:"https://rpc.catenaxtestnet.oceanprotocol.com",providerUri:"https://provider.catenaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.catenaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.catenaxtestnet.oceanprotocol.com",metadataCacheUri:"https://aquarius.catenaxtestnet.oceanprotocol.com"}),A({},ze,{networkId:80001,network:"mumbai",nodeUri:"https://polygon-mumbai.infura.io/v3",providerUri:"https://provider.mumbai.oceanprotocol.com",subgraphUri:"https://subgraph.mumbai.oceanprotocol.com",explorerUri:"https://mumbai.polygonscan.com"}),A({},ze,{networkId:56,network:"bsc",nodeUri:"https://bsc-dataseed.binance.org",providerUri:"https://provider.bsc.oceanprotocol.com",subgraphUri:"https://subgraph.bsc.oceanprotocol.com",explorerUri:"https://bscscan.com/"})];class Ze{getAddressesFromEnv(t){let e;if(M[t]){const{DTFactory:i,BFactory:r,FixedRateExchange:n,Dispenser:s,Metadata:o,Ocean:a,chainId:h,startBlock:u}=M[t];e=A({factoryAddress:i,poolFactoryAddress:r,fixedRateExchangeAddress:n,dispenserAddress:s,metadataContractAddress:o,oceanTokenAddress:a,networkId:h,startBlock:u},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}if(r&&process.env.ADDRESS_FILE)try{const i=JSON.parse(r.readFileSync(process.env.ADDRESS_FILE||`${y}/.ocean/ocean-contracts/artifacts/address.json`,"utf8")),{DTFactory:n,BFactory:s,FixedRateExchange:o,Dispenser:a,Metadata:h,Ocean:u,chainId:l,startBlock:c}=i[t];e=A({factoryAddress:n,poolFactoryAddress:s,fixedRateExchangeAddress:o,dispenserAddress:a,metadataContractAddress:h,oceanTokenAddress:u,networkId:l,startBlock:c},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}catch(t){}return e}getConfig(t,e){const i="string"==typeof t?"network":"networkId";let r=He.find(e=>e[i]===t);return r?(r=A({},r,this.getAddressesFromEnv(r.network)),A({},r,{nodeUri:e?`${r.nodeUri}/${e}`:r.nodeUri})):(R.error(`No config found for given network '${t}'`),null)}}export{st as Account,b as Config,Ze as ConfigHelper,ae as CreateProgressStep,oe as CredentialType,ut as DDO,ot as DID,Ce as DataTokens,x as LogLevel,R as Logger,Oe as MetadataCache,Ge as Ocean,ue as OceanPlatformTechStatus,he as OrderProgressStep,ft as Provider,rt as utils};
1
+ import{v4 as t}from"uuid";import e from"decimal.js";import i from"web3";import r from"fs";import n from"save-file";import s from"node-abort-controller";import o from"buffer";import a from"crypto";import{fetch as h}from"cross-fetch";import u from"@oceanprotocol/contracts/artifacts/Metadata.json";import{LZMA as l}from"lzma/src/lzma-c";import c from"@oceanprotocol/contracts/artifacts/DTFactory.json";import d from"@oceanprotocol/contracts/artifacts/DataTokenTemplate.json";import{lookup as f}from"@ethereum-navigator/navigator";import{SHA256 as m}from"crypto-js";import g from"@oceanprotocol/contracts/artifacts/BPool.json";import p from"@oceanprotocol/contracts/artifacts/BFactory.json";import w from"@oceanprotocol/contracts/artifacts/FixedRateExchange.json";import v from"@oceanprotocol/contracts/artifacts/Dispenser.json";import{homedir as y}from"os";import M from"@oceanprotocol/contracts/artifacts/address.json";class b{constructor(){this.nodeUri=void 0,this.providerAddress=void 0,this.metadataCacheUri=void 0,this.providerUri=void 0,this.rbacUri=void 0,this.web3Provider=void 0,this.oceanTokenAddress=void 0,this.factoryAddress=void 0,this.factoryABI=void 0,this.datatokensABI=void 0,this.poolFactoryAddress=void 0,this.poolFactoryABI=void 0,this.poolABI=void 0,this.fixedRateExchangeAddress=void 0,this.fixedRateExchangeAddressABI=void 0,this.dispenserAddress=void 0,this.dispenserABI=void 0,this.metadataContractAddress=void 0,this.metadataContractABI=void 0,this.startBlock=void 0,this.verbose=void 0,this.authMessage=void 0,this.authTokenExpiration=void 0,this.parityUri=void 0,this.threshold=void 0}}function A(){return(A=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var r in i)Object.prototype.hasOwnProperty.call(i,r)&&(t[r]=i[r])}return t}).apply(this,arguments)}var x;!function(t){t[t.None=-1]="None",t[t.Error=0]="Error",t[t.Warn=1]="Warn",t[t.Log=2]="Log",t[t.Verbose=3]="Verbose"}(x||(x={}));class E{constructor(t=x.Error){this.logLevel=void 0,this.logLevel=t}setLevel(t){this.logLevel=t}bypass(...t){this.dispatch("log",-Infinity,...t)}debug(...t){this.dispatch("debug",x.Verbose,...t)}log(...t){this.dispatch("log",x.Log,...t)}warn(...t){this.dispatch("warn",x.Warn,...t)}error(...t){this.dispatch("error",x.Error,...t)}dispatch(t,e,...i){this.logLevel>=e&&console[t](...i)}}const R=new E,k=t=>I(t,!0),S=t=>I(t,!1);function I(t="",e){const{valid:i,output:r}=_(t,/^(?:0x)*([a-f0-9]+)$/i,"zeroXTransformer");return(e&&i?"0x":"")+r}const T=t=>O(t,!0);function O(t="",e){const{valid:i,output:r}=_(t,/^(?:0x|did:op:)*([a-f0-9]{40})$/i,"didTransformer");return(e&&i?"did:op:":"")+r}const B=t=>k(O(t,!1)),C=t=>S(O(t,!1));function _(t,e,i){if("string"!=typeof t)throw R.debug("Not input string:"),R.debug(t),new Error(`[${i}] Expected string, input type: ${typeof t}`);const r=t.match(e);return r?{valid:!0,output:r[1]}:(R.warn(`[${i}] Input transformation failed.`),{valid:!1,output:t})}class D{constructor(){this.completed=!1,this.subscriptions=new Set}subscribe(t,e,i){if(this.completed)throw new Error("Observer completed.");const r={onNext:t,onComplete:e,onError:i};return this.subscriptions.add(r),{unsubscribe:()=>this.subscriptions.delete(r)}}next(t){this.emit("onNext",t)}complete(t){this.emit("onComplete",t),this.unsubscribe()}error(t){this.emit("onError",t),this.unsubscribe()}emit(t,e){Array.from(this.subscriptions).map(e=>e[t]).filter(t=>t&&"function"==typeof t).forEach(t=>t(e))}unsubscribe(){this.completed=!0,this.subscriptions.clear()}}class P{constructor(t){this.observer=new D,this.promise=Object.assign(new Promise((t,e)=>{setTimeout(()=>{this.observer.subscribe(void 0,t,e)},0)}),this),setTimeout(()=>this.init(t),1)}subscribe(t){return this.observer.subscribe(t)}next(t){return this.observer.subscribe(t),this}then(t,e){return Object.assign(this.promise.then(t,e),this)}catch(t){return Object.assign(this.promise.catch(t),this)}finally(t){return Object.assign(this.promise.finally(t),this)}init(t){const e=t(this.observer);Promise.resolve(e).then(t=>{"function"==typeof e.then&&this.observer.complete(t)}).catch(t=>{"function"==typeof e.then&&this.observer.error(t)})}}var N=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,L=Math.ceil,W=Math.floor,F="[BigNumber Error] ",U=F+"Number primitive has more than 15 significant digits: ",$=1e14,j=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],q=1e9;function G(t){var e=0|t;return t>0||t===e?e:e-1}function z(t){for(var e,i,r=1,n=t.length,s=t[0]+"";r<n;){for(i=14-(e=t[r++]+"").length;i--;e="0"+e);s+=e}for(n=s.length;48===s.charCodeAt(--n););return s.slice(0,n+1||1)}function H(t,e){var i,r,n=t.c,s=e.c,o=t.s,a=e.s,h=t.e,u=e.e;if(!o||!a)return null;if(r=s&&!s[0],(i=n&&!n[0])||r)return i?r?0:-a:o;if(o!=a)return o;if(i=o<0,r=h==u,!n||!s)return r?0:!n^i?1:-1;if(!r)return h>u^i?1:-1;for(a=(h=n.length)<(u=s.length)?h:u,o=0;o<a;o++)if(n[o]!=s[o])return n[o]>s[o]^i?1:-1;return h==u?0:h>u^i?1:-1}function Z(t,e,i,r){if(t<e||t>i||t!==W(t))throw Error(F+(r||"Argument")+("number"==typeof t?t<e||t>i?" out of range: ":" not an integer: ":" not a primitive number: ")+String(t))}function V(t){var e=t.c.length-1;return G(t.e/14)==e&&t.c[e]%2!=0}function J(t,e){return(t.length>1?t.charAt(0)+"."+t.slice(1):t)+(e<0?"e":"e+")+e}function K(t,e,i){var r,n;if(e<0){for(n=i+".";++e;n+=i);t=n+t}else if(++e>(r=t.length)){for(n=i,e-=r;--e;n+=i);t+=n}else e<r&&(t=t.slice(0,e)+"."+t.slice(e));return t}var X=function t(e){var i,r,n,s,o,a,h,u,l,c,d=R.prototype={constructor:R,toString:null,valueOf:null},f=new R(1),m=20,g=4,p=-7,w=21,v=-1e7,y=1e7,M=!1,b=1,A=0,x={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:" ",suffix:""},E="0123456789abcdefghijklmnopqrstuvwxyz";function R(t,e){var i,s,o,a,h,u,l,c,d=this;if(!(d instanceof R))return new R(t,e);if(null==e){if(t&&!0===t._isBigNumber)return d.s=t.s,void(!t.c||t.e>y?d.c=d.e=null:t.e<v?d.c=[d.e=0]:(d.e=t.e,d.c=t.c.slice()));if((u="number"==typeof t)&&0*t==0){if(d.s=1/t<0?(t=-t,-1):1,t===~~t){for(a=0,h=t;h>=10;h/=10,a++);return void(a>y?d.c=d.e=null:(d.e=a,d.c=[t]))}c=String(t)}else{if(!N.test(c=String(t)))return n(d,c,u);d.s=45==c.charCodeAt(0)?(c=c.slice(1),-1):1}(a=c.indexOf("."))>-1&&(c=c.replace(".","")),(h=c.search(/e/i))>0?(a<0&&(a=h),a+=+c.slice(h+1),c=c.substring(0,h)):a<0&&(a=c.length)}else{if(Z(e,2,E.length,"Base"),10==e)return T(d=new R(t),m+d.e+1,g);if(c=String(t),u="number"==typeof t){if(0*t!=0)return n(d,c,u,e);if(d.s=1/t<0?(c=c.slice(1),-1):1,R.DEBUG&&c.replace(/^0\.0*|\./,"").length>15)throw Error(U+t)}else d.s=45===c.charCodeAt(0)?(c=c.slice(1),-1):1;for(i=E.slice(0,e),a=h=0,l=c.length;h<l;h++)if(i.indexOf(s=c.charAt(h))<0){if("."==s){if(h>a){a=l;continue}}else if(!o&&(c==c.toUpperCase()&&(c=c.toLowerCase())||c==c.toLowerCase()&&(c=c.toUpperCase()))){o=!0,h=-1,a=0;continue}return n(d,String(t),u,e)}u=!1,(a=(c=r(c,e,10,d.s)).indexOf("."))>-1?c=c.replace(".",""):a=c.length}for(h=0;48===c.charCodeAt(h);h++);for(l=c.length;48===c.charCodeAt(--l););if(c=c.slice(h,++l)){if(l-=h,u&&R.DEBUG&&l>15&&(t>9007199254740991||t!==W(t)))throw Error(U+d.s*t);if((a=a-h-1)>y)d.c=d.e=null;else if(a<v)d.c=[d.e=0];else{if(d.e=a,d.c=[],h=(a+1)%14,a<0&&(h+=14),h<l){for(h&&d.c.push(+c.slice(0,h)),l-=14;h<l;)d.c.push(+c.slice(h,h+=14));h=14-(c=c.slice(h)).length}else h-=l;for(;h--;c+="0");d.c.push(+c)}}else d.c=[d.e=0]}function k(t,e,i,r){var n,s,o,a,h;if(null==i?i=g:Z(i,0,8),!t.c)return t.toString();if(n=t.c[0],o=t.e,null==e)h=z(t.c),h=1==r||2==r&&(o<=p||o>=w)?J(h,o):K(h,o,"0");else if(s=(t=T(new R(t),e,i)).e,a=(h=z(t.c)).length,1==r||2==r&&(e<=s||s<=p)){for(;a<e;h+="0",a++);h=J(h,s)}else if(e-=o,h=K(h,s,"0"),s+1>a){if(--e>0)for(h+=".";e--;h+="0");}else if((e+=s-a)>0)for(s+1==a&&(h+=".");e--;h+="0");return t.s<0&&n?"-"+h:h}function S(t,e){for(var i,r=1,n=new R(t[0]);r<t.length;r++){if(!(i=new R(t[r])).s){n=i;break}e.call(n,i)&&(n=i)}return n}function I(t,e,i){for(var r=1,n=e.length;!e[--n];e.pop());for(n=e[0];n>=10;n/=10,r++);return(i=r+14*i-1)>y?t.c=t.e=null:i<v?t.c=[t.e=0]:(t.e=i,t.c=e),t}function T(t,e,i,r){var n,s,o,a,h,u,l,c=t.c,d=j;if(c){t:{for(n=1,a=c[0];a>=10;a/=10,n++);if((s=e-n)<0)s+=14,l=(h=c[u=0])/d[n-(o=e)-1]%10|0;else if((u=L((s+1)/14))>=c.length){if(!r)break t;for(;c.length<=u;c.push(0));h=l=0,n=1,o=(s%=14)-14+1}else{for(h=a=c[u],n=1;a>=10;a/=10,n++);l=(o=(s%=14)-14+n)<0?0:h/d[n-o-1]%10|0}if(r=r||e<0||null!=c[u+1]||(o<0?h:h%d[n-o-1]),r=i<4?(l||r)&&(0==i||i==(t.s<0?3:2)):l>5||5==l&&(4==i||r||6==i&&(s>0?o>0?h/d[n-o]:0:c[u-1])%10&1||i==(t.s<0?8:7)),e<1||!c[0])return c.length=0,r?(c[0]=d[(14-(e-=t.e+1)%14)%14],t.e=-e||0):c[0]=t.e=0,t;if(0==s?(c.length=u,a=1,u--):(c.length=u+1,a=d[14-s],c[u]=o>0?W(h/d[n-o]%d[o])*a:0),r)for(;;){if(0==u){for(s=1,o=c[0];o>=10;o/=10,s++);for(o=c[0]+=a,a=1;o>=10;o/=10,a++);s!=a&&(t.e++,c[0]==$&&(c[0]=1));break}if(c[u]+=a,c[u]!=$)break;c[u--]=0,a=1}for(s=c.length;0===c[--s];c.pop());}t.e>y?t.c=t.e=null:t.e<v&&(t.c=[t.e=0])}return t}function O(t){var e,i=t.e;return null===i?t.toString():(e=z(t.c),e=i<=p||i>=w?J(e,i):K(e,i,"0"),t.s<0?"-"+e:e)}return R.clone=t,R.ROUND_UP=0,R.ROUND_DOWN=1,R.ROUND_CEIL=2,R.ROUND_FLOOR=3,R.ROUND_HALF_UP=4,R.ROUND_HALF_DOWN=5,R.ROUND_HALF_EVEN=6,R.ROUND_HALF_CEIL=7,R.ROUND_HALF_FLOOR=8,R.EUCLID=9,R.config=R.set=function(t){var e,i;if(null!=t){if("object"!=typeof t)throw Error(F+"Object expected: "+t);if(t.hasOwnProperty(e="DECIMAL_PLACES")&&(Z(i=t[e],0,q,e),m=i),t.hasOwnProperty(e="ROUNDING_MODE")&&(Z(i=t[e],0,8,e),g=i),t.hasOwnProperty(e="EXPONENTIAL_AT")&&((i=t[e])&&i.pop?(Z(i[0],-q,0,e),Z(i[1],0,q,e),p=i[0],w=i[1]):(Z(i,-q,q,e),p=-(w=i<0?-i:i))),t.hasOwnProperty(e="RANGE"))if((i=t[e])&&i.pop)Z(i[0],-q,-1,e),Z(i[1],1,q,e),v=i[0],y=i[1];else{if(Z(i,-q,q,e),!i)throw Error(F+e+" cannot be zero: "+i);v=-(y=i<0?-i:i)}if(t.hasOwnProperty(e="CRYPTO")){if((i=t[e])!==!!i)throw Error(F+e+" not true or false: "+i);if(i){if("undefined"==typeof crypto||!crypto||!crypto.getRandomValues&&!crypto.randomBytes)throw M=!i,Error(F+"crypto unavailable");M=i}else M=i}if(t.hasOwnProperty(e="MODULO_MODE")&&(Z(i=t[e],0,9,e),b=i),t.hasOwnProperty(e="POW_PRECISION")&&(Z(i=t[e],0,q,e),A=i),t.hasOwnProperty(e="FORMAT")){if("object"!=typeof(i=t[e]))throw Error(F+e+" not an object: "+i);x=i}if(t.hasOwnProperty(e="ALPHABET")){if("string"!=typeof(i=t[e])||/^.$|[+-.\s]|(.).*\1/.test(i))throw Error(F+e+" invalid: "+i);E=i}}return{DECIMAL_PLACES:m,ROUNDING_MODE:g,EXPONENTIAL_AT:[p,w],RANGE:[v,y],CRYPTO:M,MODULO_MODE:b,POW_PRECISION:A,FORMAT:x,ALPHABET:E}},R.isBigNumber=function(t){if(!t||!0!==t._isBigNumber)return!1;if(!R.DEBUG)return!0;var e,i,r=t.c,n=t.e,s=t.s;t:if("[object Array]"=={}.toString.call(r)){if((1===s||-1===s)&&n>=-q&&n<=q&&n===W(n)){if(0===r[0]){if(0===n&&1===r.length)return!0;break t}if((e=(n+1)%14)<1&&(e+=14),String(r[0]).length==e){for(e=0;e<r.length;e++)if((i=r[e])<0||i>=$||i!==W(i))break t;if(0!==i)return!0}}}else if(null===r&&null===n&&(null===s||1===s||-1===s))return!0;throw Error(F+"Invalid BigNumber: "+t)},R.maximum=R.max=function(){return S(arguments,d.lt)},R.minimum=R.min=function(){return S(arguments,d.gt)},R.random=(s=9007199254740992,o=Math.random()*s&2097151?function(){return W(Math.random()*s)}:function(){return 8388608*(1073741824*Math.random()|0)+(8388608*Math.random()|0)},function(t){var e,i,r,n,s,a=0,h=[],u=new R(f);if(null==t?t=m:Z(t,0,q),n=L(t/14),M)if(crypto.getRandomValues){for(e=crypto.getRandomValues(new Uint32Array(n*=2));a<n;)(s=131072*e[a]+(e[a+1]>>>11))>=9e15?(i=crypto.getRandomValues(new Uint32Array(2)),e[a]=i[0],e[a+1]=i[1]):(h.push(s%1e14),a+=2);a=n/2}else{if(!crypto.randomBytes)throw M=!1,Error(F+"crypto unavailable");for(e=crypto.randomBytes(n*=7);a<n;)(s=281474976710656*(31&e[a])+1099511627776*e[a+1]+4294967296*e[a+2]+16777216*e[a+3]+(e[a+4]<<16)+(e[a+5]<<8)+e[a+6])>=9e15?crypto.randomBytes(7).copy(e,a):(h.push(s%1e14),a+=7);a=n/7}if(!M)for(;a<n;)(s=o())<9e15&&(h[a++]=s%1e14);for(t%=14,(n=h[--a])&&t&&(h[a]=W(n/(s=j[14-t]))*s);0===h[a];h.pop(),a--);if(a<0)h=[r=0];else{for(r=-1;0===h[0];h.splice(0,1),r-=14);for(a=1,s=h[0];s>=10;s/=10,a++);a<14&&(r-=14-a)}return u.e=r,u.c=h,u}),R.sum=function(){for(var t=1,e=arguments,i=new R(e[0]);t<e.length;)i=i.plus(e[t++]);return i},r=function(){var t="0123456789";function e(t,e,i,r){for(var n,s,o=[0],a=0,h=t.length;a<h;){for(s=o.length;s--;o[s]*=e);for(o[0]+=r.indexOf(t.charAt(a++)),n=0;n<o.length;n++)o[n]>i-1&&(null==o[n+1]&&(o[n+1]=0),o[n+1]+=o[n]/i|0,o[n]%=i)}return o.reverse()}return function(r,n,s,o,a){var h,u,l,c,d,f,p,w,v=r.indexOf("."),y=m,M=g;for(v>=0&&(c=A,A=0,r=r.replace(".",""),f=(w=new R(n)).pow(r.length-v),A=c,w.c=e(K(z(f.c),f.e,"0"),10,s,t),w.e=w.c.length),l=c=(p=e(r,n,s,a?(h=E,t):(h=t,E))).length;0==p[--c];p.pop());if(!p[0])return h.charAt(0);if(v<0?--l:(f.c=p,f.e=l,f.s=o,p=(f=i(f,w,y,M,s)).c,d=f.r,l=f.e),v=p[u=l+y+1],c=s/2,d=d||u<0||null!=p[u+1],d=M<4?(null!=v||d)&&(0==M||M==(f.s<0?3:2)):v>c||v==c&&(4==M||d||6==M&&1&p[u-1]||M==(f.s<0?8:7)),u<1||!p[0])r=d?K(h.charAt(1),-y,h.charAt(0)):h.charAt(0);else{if(p.length=u,d)for(--s;++p[--u]>s;)p[u]=0,u||(++l,p=[1].concat(p));for(c=p.length;!p[--c];);for(v=0,r="";v<=c;r+=h.charAt(p[v++]));r=K(r,l,h.charAt(0))}return r}}(),i=function(){function t(t,e,i){var r,n,s,o,a=0,h=t.length,u=e%1e7,l=e/1e7|0;for(t=t.slice();h--;)a=((n=u*(s=t[h]%1e7)+(r=l*s+(o=t[h]/1e7|0)*u)%1e7*1e7+a)/i|0)+(r/1e7|0)+l*o,t[h]=n%i;return a&&(t=[a].concat(t)),t}function e(t,e,i,r){var n,s;if(i!=r)s=i>r?1:-1;else for(n=s=0;n<i;n++)if(t[n]!=e[n]){s=t[n]>e[n]?1:-1;break}return s}function i(t,e,i,r){for(var n=0;i--;)t[i]-=n,t[i]=(n=t[i]<e[i]?1:0)*r+t[i]-e[i];for(;!t[0]&&t.length>1;t.splice(0,1));}return function(r,n,s,o,a){var h,u,l,c,d,f,m,g,p,w,v,y,M,b,A,x,E,k=r.s==n.s?1:-1,S=r.c,I=n.c;if(!(S&&S[0]&&I&&I[0]))return new R(r.s&&n.s&&(S?!I||S[0]!=I[0]:I)?S&&0==S[0]||!I?0*k:k/0:NaN);for(p=(g=new R(k)).c=[],k=s+(u=r.e-n.e)+1,a||(a=$,u=G(r.e/14)-G(n.e/14),k=k/14|0),l=0;I[l]==(S[l]||0);l++);if(I[l]>(S[l]||0)&&u--,k<0)p.push(1),c=!0;else{for(b=S.length,x=I.length,l=0,k+=2,(d=W(a/(I[0]+1)))>1&&(I=t(I,d,a),S=t(S,d,a),x=I.length,b=S.length),M=x,v=(w=S.slice(0,x)).length;v<x;w[v++]=0);E=I.slice(),E=[0].concat(E),A=I[0],I[1]>=a/2&&A++;do{if(d=0,(h=e(I,w,x,v))<0){if(y=w[0],x!=v&&(y=y*a+(w[1]||0)),(d=W(y/A))>1)for(d>=a&&(d=a-1),m=(f=t(I,d,a)).length,v=w.length;1==e(f,w,m,v);)d--,i(f,x<m?E:I,m,a),m=f.length,h=1;else 0==d&&(h=d=1),m=(f=I.slice()).length;if(m<v&&(f=[0].concat(f)),i(w,f,v,a),v=w.length,-1==h)for(;e(I,w,x,v)<1;)d++,i(w,x<v?E:I,v,a),v=w.length}else 0===h&&(d++,w=[0]);p[l++]=d,w[0]?w[v++]=S[M]||0:(w=[S[M]],v=1)}while((M++<b||null!=w[0])&&k--);c=null!=w[0],p[0]||p.splice(0,1)}if(a==$){for(l=1,k=p[0];k>=10;k/=10,l++);T(g,s+(g.e=l+14*u-1)+1,o,c)}else g.e=u,g.r=+c;return g}}(),a=/^(-?)0([xbo])(?=\w[\w.]*$)/i,h=/^([^.]+)\.$/,u=/^\.([^.]+)$/,l=/^-?(Infinity|NaN)$/,c=/^\s*\+(?=[\w.])|^\s+|\s+$/g,n=function(t,e,i,r){var n,s=i?e:e.replace(c,"");if(l.test(s))t.s=isNaN(s)?null:s<0?-1:1;else{if(!i&&(s=s.replace(a,function(t,e,i){return n="x"==(i=i.toLowerCase())?16:"b"==i?2:8,r&&r!=n?t:e}),r&&(n=r,s=s.replace(h,"$1").replace(u,"0.$1")),e!=s))return new R(s,n);if(R.DEBUG)throw Error(F+"Not a"+(r?" base "+r:"")+" number: "+e);t.s=null}t.c=t.e=null},d.absoluteValue=d.abs=function(){var t=new R(this);return t.s<0&&(t.s=1),t},d.comparedTo=function(t,e){return H(this,new R(t,e))},d.decimalPlaces=d.dp=function(t,e){var i,r,n,s=this;if(null!=t)return Z(t,0,q),null==e?e=g:Z(e,0,8),T(new R(s),t+s.e+1,e);if(!(i=s.c))return null;if(r=14*((n=i.length-1)-G(this.e/14)),n=i[n])for(;n%10==0;n/=10,r--);return r<0&&(r=0),r},d.dividedBy=d.div=function(t,e){return i(this,new R(t,e),m,g)},d.dividedToIntegerBy=d.idiv=function(t,e){return i(this,new R(t,e),0,1)},d.exponentiatedBy=d.pow=function(t,e){var i,r,n,s,o,a,h,u,l=this;if((t=new R(t)).c&&!t.isInteger())throw Error(F+"Exponent not an integer: "+O(t));if(null!=e&&(e=new R(e)),o=t.e>14,!l.c||!l.c[0]||1==l.c[0]&&!l.e&&1==l.c.length||!t.c||!t.c[0])return u=new R(Math.pow(+O(l),o?2-V(t):+O(t))),e?u.mod(e):u;if(a=t.s<0,e){if(e.c?!e.c[0]:!e.s)return new R(NaN);(r=!a&&l.isInteger()&&e.isInteger())&&(l=l.mod(e))}else{if(t.e>9&&(l.e>0||l.e<-1||(0==l.e?l.c[0]>1||o&&l.c[1]>=24e7:l.c[0]<8e13||o&&l.c[0]<=9999975e7)))return s=l.s<0&&V(t)?-0:0,l.e>-1&&(s=1/s),new R(a?1/s:s);A&&(s=L(A/14+2))}for(o?(i=new R(.5),a&&(t.s=1),h=V(t)):h=(n=Math.abs(+O(t)))%2,u=new R(f);;){if(h){if(!(u=u.times(l)).c)break;s?u.c.length>s&&(u.c.length=s):r&&(u=u.mod(e))}if(n){if(0===(n=W(n/2)))break;h=n%2}else if(T(t=t.times(i),t.e+1,1),t.e>14)h=V(t);else{if(0==(n=+O(t)))break;h=n%2}l=l.times(l),s?l.c&&l.c.length>s&&(l.c.length=s):r&&(l=l.mod(e))}return r?u:(a&&(u=f.div(u)),e?u.mod(e):s?T(u,A,g,void 0):u)},d.integerValue=function(t){var e=new R(this);return null==t?t=g:Z(t,0,8),T(e,e.e+1,t)},d.isEqualTo=d.eq=function(t,e){return 0===H(this,new R(t,e))},d.isFinite=function(){return!!this.c},d.isGreaterThan=d.gt=function(t,e){return H(this,new R(t,e))>0},d.isGreaterThanOrEqualTo=d.gte=function(t,e){return 1===(e=H(this,new R(t,e)))||0===e},d.isInteger=function(){return!!this.c&&G(this.e/14)>this.c.length-2},d.isLessThan=d.lt=function(t,e){return H(this,new R(t,e))<0},d.isLessThanOrEqualTo=d.lte=function(t,e){return-1===(e=H(this,new R(t,e)))||0===e},d.isNaN=function(){return!this.s},d.isNegative=function(){return this.s<0},d.isPositive=function(){return this.s>0},d.isZero=function(){return!!this.c&&0==this.c[0]},d.minus=function(t,e){var i,r,n,s,o=this,a=o.s;if(e=(t=new R(t,e)).s,!a||!e)return new R(NaN);if(a!=e)return t.s=-e,o.plus(t);var h=o.e/14,u=t.e/14,l=o.c,c=t.c;if(!h||!u){if(!l||!c)return l?(t.s=-e,t):new R(c?o:NaN);if(!l[0]||!c[0])return c[0]?(t.s=-e,t):new R(l[0]?o:3==g?-0:0)}if(h=G(h),u=G(u),l=l.slice(),a=h-u){for((s=a<0)?(a=-a,n=l):(u=h,n=c),n.reverse(),e=a;e--;n.push(0));n.reverse()}else for(r=(s=(a=l.length)<(e=c.length))?a:e,a=e=0;e<r;e++)if(l[e]!=c[e]){s=l[e]<c[e];break}if(s&&(n=l,l=c,c=n,t.s=-t.s),(e=(r=c.length)-(i=l.length))>0)for(;e--;l[i++]=0);for(e=$-1;r>a;){if(l[--r]<c[r]){for(i=r;i&&!l[--i];l[i]=e);--l[i],l[r]+=$}l[r]-=c[r]}for(;0==l[0];l.splice(0,1),--u);return l[0]?I(t,l,u):(t.s=3==g?-1:1,t.c=[t.e=0],t)},d.modulo=d.mod=function(t,e){var r,n,s=this;return t=new R(t,e),!s.c||!t.s||t.c&&!t.c[0]?new R(NaN):!t.c||s.c&&!s.c[0]?new R(s):(9==b?(n=t.s,t.s=1,r=i(s,t,0,3),t.s=n,r.s*=n):r=i(s,t,0,b),(t=s.minus(r.times(t))).c[0]||1!=b||(t.s=s.s),t)},d.multipliedBy=d.times=function(t,e){var i,r,n,s,o,a,h,u,l,c,d,f,m,g,p,w=this,v=w.c,y=(t=new R(t,e)).c;if(!(v&&y&&v[0]&&y[0]))return!w.s||!t.s||v&&!v[0]&&!y||y&&!y[0]&&!v?t.c=t.e=t.s=null:(t.s*=w.s,v&&y?(t.c=[0],t.e=0):t.c=t.e=null),t;for(r=G(w.e/14)+G(t.e/14),t.s*=w.s,(h=v.length)<(c=y.length)&&(m=v,v=y,y=m,n=h,h=c,c=n),n=h+c,m=[];n--;m.push(0));for(g=$,p=1e7,n=c;--n>=0;){for(i=0,d=y[n]%p,f=y[n]/p|0,s=n+(o=h);s>n;)i=((u=d*(u=v[--o]%p)+(a=f*u+(l=v[o]/p|0)*d)%p*p+m[s]+i)/g|0)+(a/p|0)+f*l,m[s--]=u%g;m[s]=i}return i?++r:m.splice(0,1),I(t,m,r)},d.negated=function(){var t=new R(this);return t.s=-t.s||null,t},d.plus=function(t,e){var i,r=this,n=r.s;if(e=(t=new R(t,e)).s,!n||!e)return new R(NaN);if(n!=e)return t.s=-e,r.minus(t);var s=r.e/14,o=t.e/14,a=r.c,h=t.c;if(!s||!o){if(!a||!h)return new R(n/0);if(!a[0]||!h[0])return h[0]?t:new R(a[0]?r:0*n)}if(s=G(s),o=G(o),a=a.slice(),n=s-o){for(n>0?(o=s,i=h):(n=-n,i=a),i.reverse();n--;i.push(0));i.reverse()}for((n=a.length)-(e=h.length)<0&&(i=h,h=a,a=i,e=n),n=0;e;)n=(a[--e]=a[e]+h[e]+n)/$|0,a[e]=$===a[e]?0:a[e]%$;return n&&(a=[n].concat(a),++o),I(t,a,o)},d.precision=d.sd=function(t,e){var i,r,n,s=this;if(null!=t&&t!==!!t)return Z(t,1,q),null==e?e=g:Z(e,0,8),T(new R(s),t,e);if(!(i=s.c))return null;if(r=14*(n=i.length-1)+1,n=i[n]){for(;n%10==0;n/=10,r--);for(n=i[0];n>=10;n/=10,r++);}return t&&s.e+1>r&&(r=s.e+1),r},d.shiftedBy=function(t){return Z(t,-9007199254740991,9007199254740991),this.times("1e"+t)},d.squareRoot=d.sqrt=function(){var t,e,r,n,s,o=this,a=o.c,h=o.s,u=o.e,l=m+4,c=new R("0.5");if(1!==h||!a||!a[0])return new R(!h||h<0&&(!a||a[0])?NaN:a?o:1/0);if(0==(h=Math.sqrt(+O(o)))||h==1/0?(((e=z(a)).length+u)%2==0&&(e+="0"),h=Math.sqrt(+e),u=G((u+1)/2)-(u<0||u%2),r=new R(e=h==1/0?"5e"+u:(e=h.toExponential()).slice(0,e.indexOf("e")+1)+u)):r=new R(h+""),r.c[0])for((h=(u=r.e)+l)<3&&(h=0);;)if(r=c.times((s=r).plus(i(o,s,l,1))),z(s.c).slice(0,h)===(e=z(r.c)).slice(0,h)){if(r.e<u&&--h,"9999"!=(e=e.slice(h-3,h+1))&&(n||"4999"!=e)){+e&&(+e.slice(1)||"5"!=e.charAt(0))||(T(r,r.e+m+2,1),t=!r.times(r).eq(o));break}if(!n&&(T(s,s.e+m+2,0),s.times(s).eq(o))){r=s;break}l+=4,h+=4,n=1}return T(r,r.e+m+1,g,t)},d.toExponential=function(t,e){return null!=t&&(Z(t,0,q),t++),k(this,t,e,1)},d.toFixed=function(t,e){return null!=t&&(Z(t,0,q),t=t+this.e+1),k(this,t,e)},d.toFormat=function(t,e,i){var r,n=this;if(null==i)null!=t&&e&&"object"==typeof e?(i=e,e=null):t&&"object"==typeof t?(i=t,t=e=null):i=x;else if("object"!=typeof i)throw Error(F+"Argument not an object: "+i);if(r=n.toFixed(t,e),n.c){var s,o=r.split("."),a=+i.groupSize,h=+i.secondaryGroupSize,u=i.groupSeparator||"",l=o[0],c=o[1],d=n.s<0,f=d?l.slice(1):l,m=f.length;if(h&&(s=a,a=h,h=s,m-=s),a>0&&m>0){for(l=f.substr(0,s=m%a||a);s<m;s+=a)l+=u+f.substr(s,a);h>0&&(l+=u+f.slice(s)),d&&(l="-"+l)}r=c?l+(i.decimalSeparator||"")+((h=+i.fractionGroupSize)?c.replace(new RegExp("\\d{"+h+"}\\B","g"),"$&"+(i.fractionGroupSeparator||"")):c):l}return(i.prefix||"")+r+(i.suffix||"")},d.toFraction=function(t){var e,r,n,s,o,a,h,u,l,c,d,m,p=this,w=p.c;if(null!=t&&(!(h=new R(t)).isInteger()&&(h.c||1!==h.s)||h.lt(f)))throw Error(F+"Argument "+(h.isInteger()?"out of range: ":"not an integer: ")+O(h));if(!w)return new R(p);for(e=new R(f),l=r=new R(f),n=u=new R(f),m=z(w),o=e.e=m.length-p.e-1,e.c[0]=j[(a=o%14)<0?14+a:a],t=!t||h.comparedTo(e)>0?o>0?e:l:h,a=y,y=1/0,h=new R(m),u.c[0]=0;c=i(h,e,0,1),1!=(s=r.plus(c.times(n))).comparedTo(t);)r=n,n=s,l=u.plus(c.times(s=l)),u=s,e=h.minus(c.times(s=e)),h=s;return s=i(t.minus(r),n,0,1),u=u.plus(s.times(l)),r=r.plus(s.times(n)),u.s=l.s=p.s,d=i(l,n,o*=2,g).minus(p).abs().comparedTo(i(u,r,o,g).minus(p).abs())<1?[l,n]:[u,r],y=a,d},d.toNumber=function(){return+O(this)},d.toPrecision=function(t,e){return null!=t&&Z(t,1,q),k(this,t,e,2)},d.toString=function(t){var e,i=this,n=i.s,s=i.e;return null===s?n?(e="Infinity",n<0&&(e="-"+e)):e="NaN":(null==t?e=s<=p||s>=w?J(z(i.c),s):K(z(i.c),s,"0"):10===t?e=K(z((i=T(new R(i),m+s+1,g)).c),i.e,"0"):(Z(t,2,E.length,"Base"),e=r(K(z(i.c),s,"0"),10,t,n,!0)),n<0&&i.c[0]&&(e="-"+e)),e},d.valueOf=d.toJSON=function(){return O(this)},d._isBigNumber=!0,d[Symbol.toStringTag]="BigNumber",d[Symbol.for("nodejs.util.inspect.custom")]=d.valueOf,null!=e&&R.set(e),R}();async function Q(t,e){const i=new X(await t.eth.getGasPrice());return e&&e.gasFeeMultiplier?i.multipliedBy(e.gasFeeMultiplier).integerValue(X.ROUND_DOWN).toString(10):i.toString(10)}function Y(t,e){return e&&(t.transactionBlockTimeout=e.transactionBlockTimeout,t.transactionConfirmationBlocks=e.transactionConfirmationBlocks,t.transactionPollingTimeout=e.transactionPollingTimeout),t}function tt(t){return void 0!==t.id}async function et(t,e){if(tt(t))return{did:t.id,ddo:t};{const i=await e.assets.resolve(t);return{did:i.id,ddo:i}}}var it={nouns:["Crab","Fish","Seal","Octopus","Shark","Seahorse","Walrus","Starfish","Whale","Orca","Penguin","Jellyfish","Squid","Lobster","Pelican","Shrimp","Oyster","Clam","Seagull","Dolphin","Shell","Cormorant","Otter","Anemone","Turtle","Coral","Ray","Barracuda","Krill","Anchovy","Angelfish","Barnacle","Clownfish","Cod","Cuttlefish","Eel","Fugu","Herring","Haddock","Ling","Mackerel","Manatee","Narwhal","Nautilus","Plankton","Porpoise","Prawn","Pufferfish","Swordfish","Tuna"],adjectives:["adamant","adroit","amatory","ambitious","amused","animistic","antic","arcadian","artistic","astonishing","astounding","baleful","bellicose","bilious","blissful","boorish","brave","breathtaking","brilliant","calamitous","caustic","cerulean","clever","charming","comely","competent","concomitant","confident","contumacious","corpulent","crapulous","creative","dazzling","dedicated","defamatory","delighted","delightful","determined","didactic","dilatory","dowdy","efficacious","effulgent","egregious","empowered","endemic","enthusiastic","equanimous","exceptional","execrable","fabulous","fantastic","fastidious","feckless","fecund","friable","fulsome","garrulous","generous","gentle","guileless","gustatory","heuristic","histrionic","hubristic","incendiary","incredible","insidious","insolent","inspired","intransigent","inveterate","invidious","invigorated","irksome","jejune","juicy","jocular","joyful","judicious","kind","lachrymose","limpid","loquacious","lovely","luminous","mannered","marvelous","mendacious","meretricious","minatory","mordant","motivated","munificent","nefarious","noxious","obtuse","optimistic","parsimonious","pendulous","pernicious","pervasive","petulant","passionate","phenomenal","platitudinous","pleasant","powerful","precipitate","propitious","puckish","querulous","quiescent","rebarbative","recalcitant","redolent","rhadamanthine","risible","ruminative","sagacious","salubrious","sartorial","sclerotic","serpentine","smart","spasmodic","strident","stunning","stupendous","taciturn","tactful","tasty","tenacious","tremendous","tremulous","trenchant","turbulent","turgid","ubiquitous","uxorious","verdant","vibrant","voluble","voracious","wheedling","withering","wonderful","zealous"]};function rt(t){const e=t||it,i=Math.floor(Math.random()*e.adjectives.length),r=Math.floor(Math.random()*e.nouns.length),n=Math.floor(100*Math.random()),s=e.adjectives[i].replace(/^\w/,t=>t.toUpperCase()),o=e.nouns[r].replace(/^\w/,t=>t.toUpperCase());return{name:`${s} ${o} Token`,symbol:`${(s.substring(0,3)+o.substring(0,3)).toUpperCase()}-${n}`}}var nt={__proto__:null,objectPromiseAll:async t=>((t=[],e=[])=>t.reduce((t,i,r)=>A({},t,{[i]:e[r]}),{}))(Object.keys(t),await Promise.all(Object.values(t))),get LogLevel(){return x},Logger:E,LoggerInstance:R,zeroX:k,noZeroX:S,zeroXTransformer:I,didPrefixed:T,noDidPrefixed:t=>O(t,!1),didTransformer:O,didZeroX:B,didNoZeroX:C,generateId:function(e=64){let i="";for(;i.length<e;)i+=t().replace(/-/g,"");return i.substr(0,e)},SubscribablePromise:P,SubscribableObserver:D,getFairGasPrice:Q,setContractDefaults:Y,isDdo:tt,assetResolve:et,generateDatatokenName:rt};class st{constructor(){this._ocean=void 0,this._web3=void 0,this._config=void 0,this._logger=void 0}get ocean(){return this._ocean||R.error("Ocean instance is not defined."),this._ocean}get web3(){return this._web3||R.error("Web3 instance is not defined."),this._web3}get config(){return this._config||R.error("Config instance is not defined."),this._config}get logger(){return R}get instanceConfig(){const{ocean:t,web3:e,config:i,logger:r}=this;return{ocean:t,web3:e,config:i,logger:r}}static async getInstance(t){R.warn("getInstance() methods has needs to be added to child class.")}static setInstanceConfig(t,{ocean:e,config:i,web3:r,logger:n}){t._ocean=e,t._config=i,t._web3=r,t._logger=n}setInstanceConfig(t){st.setInstanceConfig(this,t)}}class ot extends st{constructor(t="0x0",e){super(),this.id=void 0,this.password=void 0,this.token=void 0,this.id=t,e&&this.setInstanceConfig(e)}getId(){return this.id}setId(t){this.id=t}setPassword(t){this.password=t}getPassword(){return this.password}async getTokenBalance(t){if(null===t)return null;const i=[{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"}];let r=null;const n=await this.getTokenDecimals(t);try{const s=new this.web3.eth.Contract(i,t,{from:this.id}),o=await s.methods.balanceOf(this.id).call();r=new e(o).div(10**n).toString()}catch(t){this.logger.error(`ERROR: Failed to get the balance: ${t.message}`)}return r}async getTokenDecimals(t){let e=18;if(null===t)return e;const i=[{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],type:"function"}];try{const r=new this.web3.eth.Contract(i,t,{from:this.id});e=await r.methods.decimals().call()}catch(t){this.logger.error(`ERROR: Failed to get decimals : ${t.message}`)}return e}async getOceanBalance(){return this.getTokenBalance(this.config.oceanTokenAddress)}async getTokenSymbol(t){return""}async getEtherBalance(){const t=await this.web3.eth.getBalance(this.id,"latest");return this.web3.utils.fromWei(t)}}class at{static parse(t){let e;t instanceof at&&(t=t.getDid());const i=t.match(/^did:op:([a-f0-9]{40})$/i);if(i&&(e=new at(i[1])),!e)throw new Error(`Parsing DID failed, ${t}`);return e}static generate(t){return new at(S(t))}constructor(t){this.id=void 0,this.id=t}getDid(){return`did:op:${this.id}`}getId(){return this.id}}class ht extends st{static async getInstance(t){const e=new ht;return e.setInstanceConfig(t),e}async list(){const t=(await this.web3.eth.getAccounts()).map(t=>new ot(t,this.instanceConfig));return Promise.all(t)}getTokenBalance(t,e){return e.getTokenBalance(t)}getOceanBalance(t){return t.getOceanBalance()}getEtherBalance(t){return t.getEtherBalance()}}class ut{static getWeb3(t={}){return new i(t.web3Provider||i.givenProvider||new i.providers.HttpProvider(t.nodeUri))}}class lt{static serialize(t){return JSON.stringify(t,null,2)}static deserialize(t){const e=JSON.parse(t);return new lt(e)}constructor(t={}){this["@context"]="https://w3id.org/did/v1",this.id=null,this.created=void 0,this.updated=void 0,this.dataToken=void 0,this.publicKey=[],this.authentication=[],this.service=[],this.proof=void 0,this.price=void 0,this.isInPurgatory=void 0,this.purgatoryData=void 0,this.dataTokenInfo=void 0,this.credentials=void 0,this.chainId=void 0,this.event=void 0,Object.assign(this,t,{created:t&&t.created||(new Date).toISOString().replace(/\.[0-9]{3}/,"")})}shortId(){return this.id.replace("did:op:","")}findServiceById(t){if(isNaN(t))throw new Error("index is not set");return this.service.find(e=>e.index===t)}findServiceByType(t){if(!t)throw new Error("serviceType not set");return this.service.find(e=>e.type===t)}getChecksum(){const{attributes:t}=this.findServiceByType("metadata"),{files:e,name:i,author:r,license:n}=t.main,s=[...(e||[]).map(({checksum:t})=>t).filter(t=>!!t),i,r,n,this.id];return ut.getWeb3().utils.sha3(s.join("")).replace(/^0x([a-f0-9]{64})(:!.+)?$/i,"0x$1")}async addProof(t,e,i){if(this.proof)throw new Error("Proof already exists");this.proof={created:(new Date).toISOString().replace(/\.[0-9]{3}/,""),creator:e,type:"AddressHash",signatureValue:ut.getWeb3().utils.sha3(e).replace(/^0x([a-f0-9]{64})(:!.+)?$/i,"0x$1")}}}function ct(t){if(!Number.isInteger(t))throw new TypeError("Expected an integer, got "+typeof t);const e=new WeakMap,i=new s,r=setTimeout(()=>{i.abort()},t);return e.set(i.signal,r),i.signal}const dt=require("cross-fetch");class ft{constructor(t){this.logger=void 0,this.logger=t}post(t,e){return this.postWithHeaders(t,e,{"Content-type":"application/json"})}postWithOctet(t,e){return this.postWithHeaders(t,e,{"Content-type":"application/octet-stream"})}postWithHeaders(t,e,i){return this.fetch(t,null!=e?{method:"POST",body:e,headers:i,signal:ct(5e3)}:{method:"POST",signal:ct(5e3)})}get(t){return this.fetch(t,{method:"GET",headers:{"Content-type":"application/json"},signal:ct(5e3)})}put(t,e){return this.fetch(t,null!=e?{method:"PUT",body:e,headers:{"Content-type":"application/json"},signal:ct(5e3)}:{method:"PUT",headers:{"Content-type":"application/json"},signal:ct(5e3)})}delete(t,e){return this.fetch(t,null!=e?{method:"DELETE",body:e,headers:{"Content-type":"application/json"},signal:ct(5e3)}:{method:"DELETE",headers:{"Content-type":"application/json"},signal:ct(5e3)})}async downloadFile(t,e,i){const s=await this.get(t);if(!s.ok)throw new Error("Response error.");let o;try{o=s.headers.get("content-disposition").match(/attachment;filename=(.+)/)[1]}catch(e){try{o=t.split("/").pop()}catch(t){o=`file${i}`}}if(e)return await new Promise(async(t,i)=>{r.mkdirSync(e,{recursive:!0});const n=r.createWriteStream(`${e}${o}`);s.body.pipe(n),s.body.on("error",i),n.on("finish",t)}),e;n(await s.arrayBuffer(),o)}async downloadFileBrowser(t){const e=document.createElement("a");e.download="",e.href=t,e.click()}async fetch(t,e){const i=await dt(t,e);if(!i.ok)throw this.logger.error(`Error requesting [${e.method}] ${t}`),this.logger.error(`Response message: \n${await i.text()}`),i;return i}}class mt extends st{constructor(...t){super(...t),this.nonce=void 0,this.baseUrl=void 0,this.servicesEndpoints=void 0,this.computeAddress=void 0,this.providerAddress=void 0,this.providerVersion=void 0,this.computeLimits=void 0}static async getInstance(t){const e=new mt;return e.setInstanceConfig(t),e.nonce="0",await e.setBaseUrl(t.config.providerUri),e}async setBaseUrl(t){return this.baseUrl=t,this.servicesEndpoints=await this.getServiceEndpoints(),!0}get url(){return this.baseUrl}async getServiceEndpoints(){const t=[];try{const e=await(await this.ocean.utils.fetch.get(this.url)).json();this.providerAddress=e.providerAddress,"computeAddress"in e&&(this.computeAddress=e.computeAddress),"version"in e&&(this.providerVersion=e.version),"computeLimits"in e&&(this.computeLimits=e.computeLimits);for(const i in e.serviceEndpoints)t.push({serviceName:i,method:e.serviceEndpoints[i][0],urlPath:this.url+e.serviceEndpoints[i][1]});return t}catch(t){return this.logger.error("Finding the service endpoints failed:",t),null}}getEndpointURL(t){return this.servicesEndpoints?this.servicesEndpoints.find(e=>e.serviceName===t):null}async createSignature(t,e){return await this.ocean.utils.signature.signText(S(e),t.getId())}async createHashSignature(t,e){return await this.ocean.utils.signature.signWithHash(e,t.getId())}async encrypt(t,e,i){await this.getNonce(i.getId());const r={documentId:t,document:JSON.stringify(e),publisherAddress:i.getId()},n=this.getEncryptEndpoint()?this.getEncryptEndpoint().urlPath:null;if(!n)return null;try{const t=await this.ocean.utils.fetch.post(n,decodeURI(JSON.stringify(r)));return(await t.json()).encryptedDocument}catch(t){throw this.logger.error(t),new Error("HTTP request failed")}}async fileinfo(t){const e={url:t},i=[],r=this.getFileinfoEndpoint()?this.getFileinfoEndpoint().urlPath:null;if(!r)return null;try{const t=await this.ocean.utils.fetch.post(r,JSON.stringify(e)),n=await t.json();for(const t of n)i.push(t);return i}catch(t){return null}}async isFileConsumable(t,e){const i={did:t.getDid()},r=await this.ocean.metadataCache.retrieveDDO(t);if(!r)return!1;const n=r.findServiceById(e);if(!n)return!1;const s=n.serviceEndpoint+"/api/v1/services/fileinfo";try{const t=await this.ocean.utils.fetch.post(s,JSON.stringify(i));return(await t.json())[0].valid}catch(t){return!1}}async getNonce(t){const e=this.getNonceEndpoint()?this.getNonceEndpoint().urlPath:null;if(!e)return null;try{const i=await this.ocean.utils.fetch.get(e+`?userAddress=${t}`);return this.nonce=String((await i.json()).nonce),this.nonce}catch(t){throw this.logger.error(t),new Error("HTTP request failed")}}async initialize(t,e,i,r,n){const{did:s,ddo:o}=await et(t,this.ocean);let a=this.getInitializeEndpoint()?this.getInitializeEndpoint().urlPath:null;if(!a)return null;a+=`?documentId=${s}`,a+=`&serviceId=${e}`,a+=`&serviceType=${i}`,a+=`&dataToken=${o.dataToken}`,a+=`&consumerAddress=${r}`,n&&(a+="&userdata="+encodeURI(JSON.stringify(n)));try{const t=await this.ocean.utils.fetch.get(a);return await t.text()}catch(t){throw this.logger.error(t),new Error("Asset URL not found or not available.")}}async download(t,e,i,r,n,s,o,a,h=-1,u){await this.getNonce(o.getId());const l=await this.createSignature(o,t+this.nonce),c=this.getDownloadEndpoint()?this.getDownloadEndpoint().urlPath:null;if(!c)return null;const d=a.filter((t,e)=>-1===h||e===h).map(async({index:a})=>{let h=c;h+=`?fileIndex=${a}`,h+=`&documentId=${t}`,h+=`&serviceId=${n}`,h+=`&serviceType=${r}`,h+=`&dataToken=${i}`,h+=`&transferTxId=${e}`,h+=`&consumerAddress=${o.getId()}`,h+=`&signature=${l}`,u&&(h+="&userdata="+encodeURI(JSON.stringify(u)));try{s?await this.ocean.utils.fetch.downloadFile(h,s,a):await this.ocean.utils.fetch.downloadFileBrowser(h)}catch(t){throw this.logger.error("Error consuming assets"),this.logger.error(t),t}});return await Promise.all(d),s}async computeStart(t,e,i,r,n,s,o,a,h,u){const l=e.getId();await this.getNonce(e.getId());const c=Object();c.documentId=S(t);let d=l;d+=t&&`${S(t)}`||"",d+=this.nonce;const f=await this.createHashSignature(e,d);c.signature=f,r&&(c.output=r),i.did&&(c.algorithmDid=i.did),i.meta&&(c.algorithmMeta=i.meta),c.consumerAddress=l,n&&(c.transferTxId=n),i.transferTxId&&(c.algorithmTransferTxId=i.transferTxId),i.dataToken&&(c.algorithmDataToken=i.dataToken),s&&(c.serviceId=s),o&&(c.serviceType=o),a&&(c.dataToken=a),h&&(c.additionalInputs=h),u&&(c.userData=u),i.algoCustomParameters&&(c.algouserdata=i.algoCustomParameters);const m=this.getComputeStartEndpoint()?this.getComputeStartEndpoint().urlPath:null;if(!m)return null;try{const t=await this.ocean.utils.fetch.post(m,JSON.stringify(c));return null!=t&&t.ok?await t.json():(console.error("Compute start failed:",t.status,t.statusText),this.logger.error("Payload was:",c),null)}catch(t){return this.logger.error("Compute start failed:"),this.logger.error(t),this.logger.error("Payload was:",c),null}}async computeStop(t,e,i){const r=e.getId();await this.getNonce(e.getId());const n=Object();n.documentId=S(t);let s=r;s+=i||"",s+=t&&`${S(t)}`||"",s+=this.nonce;const o=await this.createHashSignature(e,s);n.signature=o,n.jobId=i,n.consumerAddress=r;const a=this.getComputeStopEndpoint()?this.getComputeStopEndpoint().urlPath:null;if(!a)return null;try{const t=await this.ocean.utils.fetch.put(a,JSON.stringify(n));return null!=t&&t.ok?await t.json():(this.logger.error("Compute stop failed:",t.status,t.statusText),this.logger.error("Payload was:",n),null)}catch(t){return this.logger.error("Compute stop failed:"),this.logger.error(t),this.logger.error("Payload was:",n),null}}async computeDelete(t,e,i){const r=e.getId();await this.getNonce(e.getId());const n=Object();n.documentId=S(t);let s=r;s+=i||"",s+=t&&`${S(t)}`||"",s+=this.nonce;const o=await this.createHashSignature(e,s);n.signature=o,n.jobId=i,n.consumerAddress=r;const a=this.getComputeDeleteEndpoint()?this.getComputeDeleteEndpoint().urlPath:null;if(!a)return null;try{const t=await this.ocean.utils.fetch.delete(a,JSON.stringify(n));return null!=t&&t.ok?await t.json():(this.logger.error("Delete compute job failed:",t.status,t.statusText),this.logger.error("Payload was:",n),null)}catch(t){return this.logger.error("Delete compute job failed:"),this.logger.error(t),this.logger.error("Payload was:",n),null}}async computeStatus(t,e,i,r){const n=e.getId();await this.getNonce(e.getId());let s="?documentId="+S(t);s+=i&&`&jobId=${i}`||"",s+=`&consumerAddress=${n}`,s+=r&&`&transferTxId=${r}`||"";const o=this.getComputeStatusEndpoint()?this.getComputeStatusEndpoint().urlPath:null;if(!o)return null;try{const t=await this.ocean.utils.fetch.get(o+s);return null!=t&&t.ok?await t.json():(this.logger.error("Get compute status failed:",t.status,t.statusText),null)}catch(t){return this.logger.error("Get compute status failed"),this.logger.error(t),null}}async computeResult(t,e,i,r){await this.getNonce(r.getId());let n=r.getId();n+=t,n+=String(e),n+=this.nonce;const s=await this.createHashSignature(r,n),o=this.getComputeResultEndpoint()?this.getComputeResultEndpoint().urlPath:null;if(!o)return null;let a=o;a+=`?jobId=${t}`,a+=`&index=${String(e)}`,a+=`&signature=${s}`,a+=`&consumerAddress=${r.getId()}`;try{i?await this.ocean.utils.fetch.downloadFile(a,i,e):await this.ocean.utils.fetch.downloadFileBrowser(a)}catch(t){throw this.logger.error("Error getting job result"),this.logger.error(t),t}return i}getInitializeEndpoint(){return this.getEndpointURL("initialize")}getNonceEndpoint(){return this.getEndpointURL("nonce")}getEncryptEndpoint(){return this.getEndpointURL("encrypt")}getFileinfoEndpoint(){return this.getEndpointURL("fileinfo")}getComputeStartEndpoint(){return this.getEndpointURL("computeStart")}getComputeStopEndpoint(){return this.getEndpointURL("computeStop")}getComputeStatusEndpoint(){return this.getEndpointURL("computeStatus")}getComputeDeleteEndpoint(){return this.getEndpointURL("computeDelete")}getComputeResultEndpoint(){return this.getEndpointURL("computeResult")}getDownloadEndpoint(){return this.getEndpointURL("download")}async isValidProvider(t){try{const e=await this.ocean.utils.fetch.get(t);if(null!=e&&e.ok){const t=await e.json();if(t&&t.providerAddress)return!0}return!1}catch(t){return this.logger.error(`Error validating provider: ${t.message}`),!1}}}var gt="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function pt(t){var e={exports:{}};return t(e,e.exports),e.exports}function wt(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.')}var vt=pt(function(t){!function(t,e){function i(t,e){if(!t)throw new Error(e||"Assertion failed")}function r(t,e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}function n(t,e,i){if(n.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(i=e,e=10),this._init(t||0,e||10,i||"be"))}var s;"object"==typeof t?t.exports=n:e.BN=n,n.BN=n,n.wordSize=26;try{s=wt("buffer").Buffer}catch(t){}function o(t,e,i){for(var r=0,n=Math.min(t.length,i),s=e;s<n;s++){var o=t.charCodeAt(s)-48;r<<=4,r|=o>=49&&o<=54?o-49+10:o>=17&&o<=22?o-17+10:15&o}return r}function a(t,e,i,r){for(var n=0,s=Math.min(t.length,i),o=e;o<s;o++){var a=t.charCodeAt(o)-48;n*=r,n+=a>=49?a-49+10:a>=17?a-17+10:a}return n}n.isBN=function(t){return t instanceof n||null!==t&&"object"==typeof t&&t.constructor.wordSize===n.wordSize&&Array.isArray(t.words)},n.max=function(t,e){return t.cmp(e)>0?t:e},n.min=function(t,e){return t.cmp(e)<0?t:e},n.prototype._init=function(t,e,r){if("number"==typeof t)return this._initNumber(t,e,r);if("object"==typeof t)return this._initArray(t,e,r);"hex"===e&&(e=16),i(e===(0|e)&&e>=2&&e<=36);var n=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&n++,16===e?this._parseHex(t,n):this._parseBase(t,e,n),"-"===t[0]&&(this.negative=1),this.strip(),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(i(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initArray=function(t,e,r){if(i("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,o,a=0;if("be"===r)for(n=t.length-1,s=0;n>=0;n-=3)this.words[s]|=(o=t[n]|t[n-1]<<8|t[n-2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);else if("le"===r)for(n=0,s=0;n<t.length;n+=3)this.words[s]|=(o=t[n]|t[n+1]<<8|t[n+2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);return this.strip()},n.prototype._parseHex=function(t,e){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var r,n,s=0;for(i=t.length-6,r=0;i>=e;i-=6)n=o(t,i,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303,(s+=24)>=26&&(s-=26,r++);i+6!==e&&(n=o(t,e,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303),this.strip()},n.prototype._parseBase=function(t,e,i){this.words=[0],this.length=1;for(var r=0,n=1;n<=67108863;n*=e)r++;r--,n=n/e|0;for(var s=t.length-i,o=s%r,h=Math.min(s,s-o)+i,u=0,l=i;l<h;l+=r)u=a(t,l,l+r,e),this.imuln(n),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(0!==o){var c=1;for(u=a(t,l,t.length,e),l=0;l<o;l++)c*=e;this.imuln(c),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}},n.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},n.prototype.clone=function(){var t=new n(null);return this.copy(t),t},n.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},n.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},n.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},n.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var h=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],u=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],l=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function c(t,e,i){i.negative=e.negative^t.negative;var r=t.length+e.length|0;i.length=r,r=r-1|0;var n=0|t.words[0],s=0|e.words[0],o=n*s,a=o/67108864|0;i.words[0]=67108863&o;for(var h=1;h<r;h++){for(var u=a>>>26,l=67108863&a,c=Math.min(h,e.length-1),d=Math.max(0,h-t.length+1);d<=c;d++)u+=(o=(n=0|t.words[h-d|0])*(s=0|e.words[d])+l)/67108864|0,l=67108863&o;i.words[h]=0|l,a=0|u}return 0!==a?i.words[h]=0|a:i.length--,i.strip()}n.prototype.toString=function(t,e){var r;if(e=0|e||1,16===(t=t||10)||"hex"===t){r="";for(var n=0,s=0,o=0;o<this.length;o++){var a=this.words[o],c=(16777215&(a<<n|s)).toString(16);r=0!=(s=a>>>24-n&16777215)||o!==this.length-1?h[6-c.length]+c+r:c+r,(n+=2)>=26&&(n-=26,o--)}for(0!==s&&(r=s.toString(16)+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}if(t===(0|t)&&t>=2&&t<=36){var d=u[t],f=l[t];r="";var m=this.clone();for(m.negative=0;!m.isZero();){var g=m.modn(f).toString(t);r=(m=m.idivn(f)).isZero()?g+r:h[d-g.length]+g+r}for(this.isZero()&&(r="0"+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}i(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&i(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},n.prototype.toJSON=function(){return this.toString(16)},n.prototype.toBuffer=function(t,e){return i(void 0!==s),this.toArrayLike(s,t,e)},n.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},n.prototype.toArrayLike=function(t,e,r){var n=this.byteLength(),s=r||Math.max(1,n);i(n<=s,"byte array longer than desired length"),i(s>0,"Requested array length <= 0"),this.strip();var o,a,h="le"===e,u=new t(s),l=this.clone();if(h){for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[a]=o;for(;a<s;a++)u[a]=0}else{for(a=0;a<s-n;a++)u[a]=0;for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[s-a-1]=o}return u},n.prototype._countBits=Math.clz32?function(t){return 32-Math.clz32(t)}:function(t){var e=t,i=0;return e>=4096&&(i+=13,e>>>=13),e>=64&&(i+=7,e>>>=7),e>=8&&(i+=4,e>>>=4),e>=2&&(i+=2,e>>>=2),i+e},n.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,i=0;return 0==(8191&e)&&(i+=13,e>>>=13),0==(127&e)&&(i+=7,e>>>=7),0==(15&e)&&(i+=4,e>>>=4),0==(3&e)&&(i+=2,e>>>=2),0==(1&e)&&i++,i},n.prototype.bitLength=function(){var t=this._countBits(this.words[this.length-1]);return 26*(this.length-1)+t},n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var i=this._zeroBits(this.words[e]);if(t+=i,26!==i)break}return t},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},n.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return 0!==this.negative},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this.strip()},n.prototype.ior=function(t){return i(0==(this.negative|t.negative)),this.iuor(t)},n.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},n.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},n.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var i=0;i<e.length;i++)this.words[i]=this.words[i]&t.words[i];return this.length=e.length,this.strip()},n.prototype.iand=function(t){return i(0==(this.negative|t.negative)),this.iuand(t)},n.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},n.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},n.prototype.iuxor=function(t){var e,i;this.length>t.length?(e=this,i=t):(e=t,i=this);for(var r=0;r<i.length;r++)this.words[r]=e.words[r]^i.words[r];if(this!==e)for(;r<e.length;r++)this.words[r]=e.words[r];return this.length=e.length,this.strip()},n.prototype.ixor=function(t){return i(0==(this.negative|t.negative)),this.iuxor(t)},n.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},n.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},n.prototype.inotn=function(t){i("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=67108863&~this.words[n];return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this.strip()},n.prototype.notn=function(t){return this.clone().inotn(t)},n.prototype.setn=function(t,e){i("number"==typeof t&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),this.words[r]=e?this.words[r]|1<<n:this.words[r]&~(1<<n),this.strip()},n.prototype.iadd=function(t){var e,i,r;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(i=this,r=t):(i=t,r=this);for(var n=0,s=0;s<r.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+(0|r.words[s])+n),n=e>>>26;for(;0!==n&&s<i.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+n),n=e>>>26;if(this.length=i.length,0!==n)this.words[this.length]=n,this.length++;else if(i!==this)for(;s<i.length;s++)this.words[s]=i.words[s];return this},n.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},n.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var i,r,n=this.cmp(t);if(0===n)return this.negative=0,this.length=1,this.words[0]=0,this;n>0?(i=this,r=t):(i=t,r=this);for(var s=0,o=0;o<r.length;o++)s=(e=(0|i.words[o])-(0|r.words[o])+s)>>26,this.words[o]=67108863&e;for(;0!==s&&o<i.length;o++)s=(e=(0|i.words[o])+s)>>26,this.words[o]=67108863&e;if(0===s&&o<i.length&&i!==this)for(;o<i.length;o++)this.words[o]=i.words[o];return this.length=Math.max(this.length,o),i!==this&&(this.negative=1),this.strip()},n.prototype.sub=function(t){return this.clone().isub(t)};var d=function(t,e,i){var r,n,s,o=t.words,a=e.words,h=i.words,u=0,l=0|o[0],c=8191&l,d=l>>>13,f=0|o[1],m=8191&f,g=f>>>13,p=0|o[2],w=8191&p,v=p>>>13,y=0|o[3],M=8191&y,b=y>>>13,A=0|o[4],x=8191&A,E=A>>>13,R=0|o[5],k=8191&R,S=R>>>13,I=0|o[6],T=8191&I,O=I>>>13,B=0|o[7],C=8191&B,_=B>>>13,D=0|o[8],P=8191&D,N=D>>>13,L=0|o[9],W=8191&L,F=L>>>13,U=0|a[0],$=8191&U,j=U>>>13,q=0|a[1],G=8191&q,z=q>>>13,H=0|a[2],Z=8191&H,V=H>>>13,J=0|a[3],K=8191&J,X=J>>>13,Q=0|a[4],Y=8191&Q,tt=Q>>>13,et=0|a[5],it=8191&et,rt=et>>>13,nt=0|a[6],st=8191&nt,ot=nt>>>13,at=0|a[7],ht=8191&at,ut=at>>>13,lt=0|a[8],ct=8191&lt,dt=lt>>>13,ft=0|a[9],mt=8191&ft,gt=ft>>>13;i.negative=t.negative^e.negative,i.length=19;var pt=(u+(r=Math.imul(c,$))|0)+((8191&(n=(n=Math.imul(c,j))+Math.imul(d,$)|0))<<13)|0;u=((s=Math.imul(d,j))+(n>>>13)|0)+(pt>>>26)|0,pt&=67108863,r=Math.imul(m,$),n=(n=Math.imul(m,j))+Math.imul(g,$)|0,s=Math.imul(g,j);var wt=(u+(r=r+Math.imul(c,G)|0)|0)+((8191&(n=(n=n+Math.imul(c,z)|0)+Math.imul(d,G)|0))<<13)|0;u=((s=s+Math.imul(d,z)|0)+(n>>>13)|0)+(wt>>>26)|0,wt&=67108863,r=Math.imul(w,$),n=(n=Math.imul(w,j))+Math.imul(v,$)|0,s=Math.imul(v,j),r=r+Math.imul(m,G)|0,n=(n=n+Math.imul(m,z)|0)+Math.imul(g,G)|0,s=s+Math.imul(g,z)|0;var vt=(u+(r=r+Math.imul(c,Z)|0)|0)+((8191&(n=(n=n+Math.imul(c,V)|0)+Math.imul(d,Z)|0))<<13)|0;u=((s=s+Math.imul(d,V)|0)+(n>>>13)|0)+(vt>>>26)|0,vt&=67108863,r=Math.imul(M,$),n=(n=Math.imul(M,j))+Math.imul(b,$)|0,s=Math.imul(b,j),r=r+Math.imul(w,G)|0,n=(n=n+Math.imul(w,z)|0)+Math.imul(v,G)|0,s=s+Math.imul(v,z)|0,r=r+Math.imul(m,Z)|0,n=(n=n+Math.imul(m,V)|0)+Math.imul(g,Z)|0,s=s+Math.imul(g,V)|0;var yt=(u+(r=r+Math.imul(c,K)|0)|0)+((8191&(n=(n=n+Math.imul(c,X)|0)+Math.imul(d,K)|0))<<13)|0;u=((s=s+Math.imul(d,X)|0)+(n>>>13)|0)+(yt>>>26)|0,yt&=67108863,r=Math.imul(x,$),n=(n=Math.imul(x,j))+Math.imul(E,$)|0,s=Math.imul(E,j),r=r+Math.imul(M,G)|0,n=(n=n+Math.imul(M,z)|0)+Math.imul(b,G)|0,s=s+Math.imul(b,z)|0,r=r+Math.imul(w,Z)|0,n=(n=n+Math.imul(w,V)|0)+Math.imul(v,Z)|0,s=s+Math.imul(v,V)|0,r=r+Math.imul(m,K)|0,n=(n=n+Math.imul(m,X)|0)+Math.imul(g,K)|0,s=s+Math.imul(g,X)|0;var Mt=(u+(r=r+Math.imul(c,Y)|0)|0)+((8191&(n=(n=n+Math.imul(c,tt)|0)+Math.imul(d,Y)|0))<<13)|0;u=((s=s+Math.imul(d,tt)|0)+(n>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,r=Math.imul(k,$),n=(n=Math.imul(k,j))+Math.imul(S,$)|0,s=Math.imul(S,j),r=r+Math.imul(x,G)|0,n=(n=n+Math.imul(x,z)|0)+Math.imul(E,G)|0,s=s+Math.imul(E,z)|0,r=r+Math.imul(M,Z)|0,n=(n=n+Math.imul(M,V)|0)+Math.imul(b,Z)|0,s=s+Math.imul(b,V)|0,r=r+Math.imul(w,K)|0,n=(n=n+Math.imul(w,X)|0)+Math.imul(v,K)|0,s=s+Math.imul(v,X)|0,r=r+Math.imul(m,Y)|0,n=(n=n+Math.imul(m,tt)|0)+Math.imul(g,Y)|0,s=s+Math.imul(g,tt)|0;var bt=(u+(r=r+Math.imul(c,it)|0)|0)+((8191&(n=(n=n+Math.imul(c,rt)|0)+Math.imul(d,it)|0))<<13)|0;u=((s=s+Math.imul(d,rt)|0)+(n>>>13)|0)+(bt>>>26)|0,bt&=67108863,r=Math.imul(T,$),n=(n=Math.imul(T,j))+Math.imul(O,$)|0,s=Math.imul(O,j),r=r+Math.imul(k,G)|0,n=(n=n+Math.imul(k,z)|0)+Math.imul(S,G)|0,s=s+Math.imul(S,z)|0,r=r+Math.imul(x,Z)|0,n=(n=n+Math.imul(x,V)|0)+Math.imul(E,Z)|0,s=s+Math.imul(E,V)|0,r=r+Math.imul(M,K)|0,n=(n=n+Math.imul(M,X)|0)+Math.imul(b,K)|0,s=s+Math.imul(b,X)|0,r=r+Math.imul(w,Y)|0,n=(n=n+Math.imul(w,tt)|0)+Math.imul(v,Y)|0,s=s+Math.imul(v,tt)|0,r=r+Math.imul(m,it)|0,n=(n=n+Math.imul(m,rt)|0)+Math.imul(g,it)|0,s=s+Math.imul(g,rt)|0;var At=(u+(r=r+Math.imul(c,st)|0)|0)+((8191&(n=(n=n+Math.imul(c,ot)|0)+Math.imul(d,st)|0))<<13)|0;u=((s=s+Math.imul(d,ot)|0)+(n>>>13)|0)+(At>>>26)|0,At&=67108863,r=Math.imul(C,$),n=(n=Math.imul(C,j))+Math.imul(_,$)|0,s=Math.imul(_,j),r=r+Math.imul(T,G)|0,n=(n=n+Math.imul(T,z)|0)+Math.imul(O,G)|0,s=s+Math.imul(O,z)|0,r=r+Math.imul(k,Z)|0,n=(n=n+Math.imul(k,V)|0)+Math.imul(S,Z)|0,s=s+Math.imul(S,V)|0,r=r+Math.imul(x,K)|0,n=(n=n+Math.imul(x,X)|0)+Math.imul(E,K)|0,s=s+Math.imul(E,X)|0,r=r+Math.imul(M,Y)|0,n=(n=n+Math.imul(M,tt)|0)+Math.imul(b,Y)|0,s=s+Math.imul(b,tt)|0,r=r+Math.imul(w,it)|0,n=(n=n+Math.imul(w,rt)|0)+Math.imul(v,it)|0,s=s+Math.imul(v,rt)|0,r=r+Math.imul(m,st)|0,n=(n=n+Math.imul(m,ot)|0)+Math.imul(g,st)|0,s=s+Math.imul(g,ot)|0;var xt=(u+(r=r+Math.imul(c,ht)|0)|0)+((8191&(n=(n=n+Math.imul(c,ut)|0)+Math.imul(d,ht)|0))<<13)|0;u=((s=s+Math.imul(d,ut)|0)+(n>>>13)|0)+(xt>>>26)|0,xt&=67108863,r=Math.imul(P,$),n=(n=Math.imul(P,j))+Math.imul(N,$)|0,s=Math.imul(N,j),r=r+Math.imul(C,G)|0,n=(n=n+Math.imul(C,z)|0)+Math.imul(_,G)|0,s=s+Math.imul(_,z)|0,r=r+Math.imul(T,Z)|0,n=(n=n+Math.imul(T,V)|0)+Math.imul(O,Z)|0,s=s+Math.imul(O,V)|0,r=r+Math.imul(k,K)|0,n=(n=n+Math.imul(k,X)|0)+Math.imul(S,K)|0,s=s+Math.imul(S,X)|0,r=r+Math.imul(x,Y)|0,n=(n=n+Math.imul(x,tt)|0)+Math.imul(E,Y)|0,s=s+Math.imul(E,tt)|0,r=r+Math.imul(M,it)|0,n=(n=n+Math.imul(M,rt)|0)+Math.imul(b,it)|0,s=s+Math.imul(b,rt)|0,r=r+Math.imul(w,st)|0,n=(n=n+Math.imul(w,ot)|0)+Math.imul(v,st)|0,s=s+Math.imul(v,ot)|0,r=r+Math.imul(m,ht)|0,n=(n=n+Math.imul(m,ut)|0)+Math.imul(g,ht)|0,s=s+Math.imul(g,ut)|0;var Et=(u+(r=r+Math.imul(c,ct)|0)|0)+((8191&(n=(n=n+Math.imul(c,dt)|0)+Math.imul(d,ct)|0))<<13)|0;u=((s=s+Math.imul(d,dt)|0)+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,r=Math.imul(W,$),n=(n=Math.imul(W,j))+Math.imul(F,$)|0,s=Math.imul(F,j),r=r+Math.imul(P,G)|0,n=(n=n+Math.imul(P,z)|0)+Math.imul(N,G)|0,s=s+Math.imul(N,z)|0,r=r+Math.imul(C,Z)|0,n=(n=n+Math.imul(C,V)|0)+Math.imul(_,Z)|0,s=s+Math.imul(_,V)|0,r=r+Math.imul(T,K)|0,n=(n=n+Math.imul(T,X)|0)+Math.imul(O,K)|0,s=s+Math.imul(O,X)|0,r=r+Math.imul(k,Y)|0,n=(n=n+Math.imul(k,tt)|0)+Math.imul(S,Y)|0,s=s+Math.imul(S,tt)|0,r=r+Math.imul(x,it)|0,n=(n=n+Math.imul(x,rt)|0)+Math.imul(E,it)|0,s=s+Math.imul(E,rt)|0,r=r+Math.imul(M,st)|0,n=(n=n+Math.imul(M,ot)|0)+Math.imul(b,st)|0,s=s+Math.imul(b,ot)|0,r=r+Math.imul(w,ht)|0,n=(n=n+Math.imul(w,ut)|0)+Math.imul(v,ht)|0,s=s+Math.imul(v,ut)|0,r=r+Math.imul(m,ct)|0,n=(n=n+Math.imul(m,dt)|0)+Math.imul(g,ct)|0,s=s+Math.imul(g,dt)|0;var Rt=(u+(r=r+Math.imul(c,mt)|0)|0)+((8191&(n=(n=n+Math.imul(c,gt)|0)+Math.imul(d,mt)|0))<<13)|0;u=((s=s+Math.imul(d,gt)|0)+(n>>>13)|0)+(Rt>>>26)|0,Rt&=67108863,r=Math.imul(W,G),n=(n=Math.imul(W,z))+Math.imul(F,G)|0,s=Math.imul(F,z),r=r+Math.imul(P,Z)|0,n=(n=n+Math.imul(P,V)|0)+Math.imul(N,Z)|0,s=s+Math.imul(N,V)|0,r=r+Math.imul(C,K)|0,n=(n=n+Math.imul(C,X)|0)+Math.imul(_,K)|0,s=s+Math.imul(_,X)|0,r=r+Math.imul(T,Y)|0,n=(n=n+Math.imul(T,tt)|0)+Math.imul(O,Y)|0,s=s+Math.imul(O,tt)|0,r=r+Math.imul(k,it)|0,n=(n=n+Math.imul(k,rt)|0)+Math.imul(S,it)|0,s=s+Math.imul(S,rt)|0,r=r+Math.imul(x,st)|0,n=(n=n+Math.imul(x,ot)|0)+Math.imul(E,st)|0,s=s+Math.imul(E,ot)|0,r=r+Math.imul(M,ht)|0,n=(n=n+Math.imul(M,ut)|0)+Math.imul(b,ht)|0,s=s+Math.imul(b,ut)|0,r=r+Math.imul(w,ct)|0,n=(n=n+Math.imul(w,dt)|0)+Math.imul(v,ct)|0,s=s+Math.imul(v,dt)|0;var kt=(u+(r=r+Math.imul(m,mt)|0)|0)+((8191&(n=(n=n+Math.imul(m,gt)|0)+Math.imul(g,mt)|0))<<13)|0;u=((s=s+Math.imul(g,gt)|0)+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,r=Math.imul(W,Z),n=(n=Math.imul(W,V))+Math.imul(F,Z)|0,s=Math.imul(F,V),r=r+Math.imul(P,K)|0,n=(n=n+Math.imul(P,X)|0)+Math.imul(N,K)|0,s=s+Math.imul(N,X)|0,r=r+Math.imul(C,Y)|0,n=(n=n+Math.imul(C,tt)|0)+Math.imul(_,Y)|0,s=s+Math.imul(_,tt)|0,r=r+Math.imul(T,it)|0,n=(n=n+Math.imul(T,rt)|0)+Math.imul(O,it)|0,s=s+Math.imul(O,rt)|0,r=r+Math.imul(k,st)|0,n=(n=n+Math.imul(k,ot)|0)+Math.imul(S,st)|0,s=s+Math.imul(S,ot)|0,r=r+Math.imul(x,ht)|0,n=(n=n+Math.imul(x,ut)|0)+Math.imul(E,ht)|0,s=s+Math.imul(E,ut)|0,r=r+Math.imul(M,ct)|0,n=(n=n+Math.imul(M,dt)|0)+Math.imul(b,ct)|0,s=s+Math.imul(b,dt)|0;var St=(u+(r=r+Math.imul(w,mt)|0)|0)+((8191&(n=(n=n+Math.imul(w,gt)|0)+Math.imul(v,mt)|0))<<13)|0;u=((s=s+Math.imul(v,gt)|0)+(n>>>13)|0)+(St>>>26)|0,St&=67108863,r=Math.imul(W,K),n=(n=Math.imul(W,X))+Math.imul(F,K)|0,s=Math.imul(F,X),r=r+Math.imul(P,Y)|0,n=(n=n+Math.imul(P,tt)|0)+Math.imul(N,Y)|0,s=s+Math.imul(N,tt)|0,r=r+Math.imul(C,it)|0,n=(n=n+Math.imul(C,rt)|0)+Math.imul(_,it)|0,s=s+Math.imul(_,rt)|0,r=r+Math.imul(T,st)|0,n=(n=n+Math.imul(T,ot)|0)+Math.imul(O,st)|0,s=s+Math.imul(O,ot)|0,r=r+Math.imul(k,ht)|0,n=(n=n+Math.imul(k,ut)|0)+Math.imul(S,ht)|0,s=s+Math.imul(S,ut)|0,r=r+Math.imul(x,ct)|0,n=(n=n+Math.imul(x,dt)|0)+Math.imul(E,ct)|0,s=s+Math.imul(E,dt)|0;var It=(u+(r=r+Math.imul(M,mt)|0)|0)+((8191&(n=(n=n+Math.imul(M,gt)|0)+Math.imul(b,mt)|0))<<13)|0;u=((s=s+Math.imul(b,gt)|0)+(n>>>13)|0)+(It>>>26)|0,It&=67108863,r=Math.imul(W,Y),n=(n=Math.imul(W,tt))+Math.imul(F,Y)|0,s=Math.imul(F,tt),r=r+Math.imul(P,it)|0,n=(n=n+Math.imul(P,rt)|0)+Math.imul(N,it)|0,s=s+Math.imul(N,rt)|0,r=r+Math.imul(C,st)|0,n=(n=n+Math.imul(C,ot)|0)+Math.imul(_,st)|0,s=s+Math.imul(_,ot)|0,r=r+Math.imul(T,ht)|0,n=(n=n+Math.imul(T,ut)|0)+Math.imul(O,ht)|0,s=s+Math.imul(O,ut)|0,r=r+Math.imul(k,ct)|0,n=(n=n+Math.imul(k,dt)|0)+Math.imul(S,ct)|0,s=s+Math.imul(S,dt)|0;var Tt=(u+(r=r+Math.imul(x,mt)|0)|0)+((8191&(n=(n=n+Math.imul(x,gt)|0)+Math.imul(E,mt)|0))<<13)|0;u=((s=s+Math.imul(E,gt)|0)+(n>>>13)|0)+(Tt>>>26)|0,Tt&=67108863,r=Math.imul(W,it),n=(n=Math.imul(W,rt))+Math.imul(F,it)|0,s=Math.imul(F,rt),r=r+Math.imul(P,st)|0,n=(n=n+Math.imul(P,ot)|0)+Math.imul(N,st)|0,s=s+Math.imul(N,ot)|0,r=r+Math.imul(C,ht)|0,n=(n=n+Math.imul(C,ut)|0)+Math.imul(_,ht)|0,s=s+Math.imul(_,ut)|0,r=r+Math.imul(T,ct)|0,n=(n=n+Math.imul(T,dt)|0)+Math.imul(O,ct)|0,s=s+Math.imul(O,dt)|0;var Ot=(u+(r=r+Math.imul(k,mt)|0)|0)+((8191&(n=(n=n+Math.imul(k,gt)|0)+Math.imul(S,mt)|0))<<13)|0;u=((s=s+Math.imul(S,gt)|0)+(n>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,r=Math.imul(W,st),n=(n=Math.imul(W,ot))+Math.imul(F,st)|0,s=Math.imul(F,ot),r=r+Math.imul(P,ht)|0,n=(n=n+Math.imul(P,ut)|0)+Math.imul(N,ht)|0,s=s+Math.imul(N,ut)|0,r=r+Math.imul(C,ct)|0,n=(n=n+Math.imul(C,dt)|0)+Math.imul(_,ct)|0,s=s+Math.imul(_,dt)|0;var Bt=(u+(r=r+Math.imul(T,mt)|0)|0)+((8191&(n=(n=n+Math.imul(T,gt)|0)+Math.imul(O,mt)|0))<<13)|0;u=((s=s+Math.imul(O,gt)|0)+(n>>>13)|0)+(Bt>>>26)|0,Bt&=67108863,r=Math.imul(W,ht),n=(n=Math.imul(W,ut))+Math.imul(F,ht)|0,s=Math.imul(F,ut),r=r+Math.imul(P,ct)|0,n=(n=n+Math.imul(P,dt)|0)+Math.imul(N,ct)|0,s=s+Math.imul(N,dt)|0;var Ct=(u+(r=r+Math.imul(C,mt)|0)|0)+((8191&(n=(n=n+Math.imul(C,gt)|0)+Math.imul(_,mt)|0))<<13)|0;u=((s=s+Math.imul(_,gt)|0)+(n>>>13)|0)+(Ct>>>26)|0,Ct&=67108863,r=Math.imul(W,ct),n=(n=Math.imul(W,dt))+Math.imul(F,ct)|0,s=Math.imul(F,dt);var _t=(u+(r=r+Math.imul(P,mt)|0)|0)+((8191&(n=(n=n+Math.imul(P,gt)|0)+Math.imul(N,mt)|0))<<13)|0;u=((s=s+Math.imul(N,gt)|0)+(n>>>13)|0)+(_t>>>26)|0,_t&=67108863;var Dt=(u+(r=Math.imul(W,mt))|0)+((8191&(n=(n=Math.imul(W,gt))+Math.imul(F,mt)|0))<<13)|0;return u=((s=Math.imul(F,gt))+(n>>>13)|0)+(Dt>>>26)|0,Dt&=67108863,h[0]=pt,h[1]=wt,h[2]=vt,h[3]=yt,h[4]=Mt,h[5]=bt,h[6]=At,h[7]=xt,h[8]=Et,h[9]=Rt,h[10]=kt,h[11]=St,h[12]=It,h[13]=Tt,h[14]=Ot,h[15]=Bt,h[16]=Ct,h[17]=_t,h[18]=Dt,0!==u&&(h[19]=u,i.length++),i};function f(t,e,i){return(new m).mulp(t,e,i)}function m(t,e){this.x=t,this.y=e}Math.imul||(d=c),n.prototype.mulTo=function(t,e){var i=this.length+t.length;return 10===this.length&&10===t.length?d(this,t,e):i<63?c(this,t,e):i<1024?function(t,e,i){i.negative=e.negative^t.negative,i.length=t.length+e.length;for(var r=0,n=0,s=0;s<i.length-1;s++){var o=n;n=0;for(var a=67108863&r,h=Math.min(s,e.length-1),u=Math.max(0,s-t.length+1);u<=h;u++){var l=(0|t.words[s-u])*(0|e.words[u]),c=67108863&l;a=67108863&(c=c+a|0),n+=(o=(o=o+(l/67108864|0)|0)+(c>>>26)|0)>>>26,o&=67108863}i.words[s]=a,r=o,o=n}return 0!==r?i.words[s]=r:i.length--,i.strip()}(this,t,e):f(this,t,e)},m.prototype.makeRBT=function(t){for(var e=new Array(t),i=n.prototype._countBits(t)-1,r=0;r<t;r++)e[r]=this.revBin(r,i,t);return e},m.prototype.revBin=function(t,e,i){if(0===t||t===i-1)return t;for(var r=0,n=0;n<e;n++)r|=(1&t)<<e-n-1,t>>=1;return r},m.prototype.permute=function(t,e,i,r,n,s){for(var o=0;o<s;o++)r[o]=e[t[o]],n[o]=i[t[o]]},m.prototype.transform=function(t,e,i,r,n,s){this.permute(s,t,e,i,r,n);for(var o=1;o<n;o<<=1)for(var a=o<<1,h=Math.cos(2*Math.PI/a),u=Math.sin(2*Math.PI/a),l=0;l<n;l+=a)for(var c=h,d=u,f=0;f<o;f++){var m=i[l+f],g=r[l+f],p=i[l+f+o],w=r[l+f+o],v=c*p-d*w;w=c*w+d*p,i[l+f]=m+(p=v),r[l+f]=g+w,i[l+f+o]=m-p,r[l+f+o]=g-w,f!==a&&(v=h*c-u*d,d=h*d+u*c,c=v)}},m.prototype.guessLen13b=function(t,e){var i=1|Math.max(e,t),r=1&i,n=0;for(i=i/2|0;i;i>>>=1)n++;return 1<<n+1+r},m.prototype.conjugate=function(t,e,i){if(!(i<=1))for(var r=0;r<i/2;r++){var n=t[r];t[r]=t[i-r-1],t[i-r-1]=n,n=e[r],e[r]=-e[i-r-1],e[i-r-1]=-n}},m.prototype.normalize13b=function(t,e){for(var i=0,r=0;r<e/2;r++){var n=8192*Math.round(t[2*r+1]/e)+Math.round(t[2*r]/e)+i;t[r]=67108863&n,i=n<67108864?0:n/67108864|0}return t},m.prototype.convert13b=function(t,e,r,n){for(var s=0,o=0;o<e;o++)r[2*o]=8191&(s+=0|t[o]),r[2*o+1]=8191&(s>>>=13),s>>>=13;for(o=2*e;o<n;++o)r[o]=0;i(0===s),i(0==(-8192&s))},m.prototype.stub=function(t){for(var e=new Array(t),i=0;i<t;i++)e[i]=0;return e},m.prototype.mulp=function(t,e,i){var r=2*this.guessLen13b(t.length,e.length),n=this.makeRBT(r),s=this.stub(r),o=new Array(r),a=new Array(r),h=new Array(r),u=new Array(r),l=new Array(r),c=new Array(r),d=i.words;d.length=r,this.convert13b(t.words,t.length,o,r),this.convert13b(e.words,e.length,u,r),this.transform(o,s,a,h,r,n),this.transform(u,s,l,c,r,n);for(var f=0;f<r;f++){var m=a[f]*l[f]-h[f]*c[f];h[f]=a[f]*c[f]+h[f]*l[f],a[f]=m}return this.conjugate(a,h,r),this.transform(a,h,d,s,r,n),this.conjugate(d,s,r),this.normalize13b(d,r),i.negative=t.negative^e.negative,i.length=t.length+e.length,i.strip()},n.prototype.mul=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},n.prototype.mulf=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),f(this,t,e)},n.prototype.imul=function(t){return this.clone().mulTo(t,this)},n.prototype.imuln=function(t){i("number"==typeof t),i(t<67108864);for(var e=0,r=0;r<this.length;r++){var n=(0|this.words[r])*t,s=(67108863&n)+(67108863&e);e>>=26,e+=n/67108864|0,e+=s>>>26,this.words[r]=67108863&s}return 0!==e&&(this.words[r]=e,this.length++),this},n.prototype.muln=function(t){return this.clone().imuln(t)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),i=0;i<e.length;i++){var r=i%26;e[i]=(t.words[i/26|0]&1<<r)>>>r}return e}(t);if(0===e.length)return new n(1);for(var i=this,r=0;r<e.length&&0===e[r];r++,i=i.sqr());if(++r<e.length)for(var s=i.sqr();r<e.length;r++,s=s.sqr())0!==e[r]&&(i=i.mul(s));return i},n.prototype.iushln=function(t){i("number"==typeof t&&t>=0);var e,r=t%26,n=(t-r)/26,s=67108863>>>26-r<<26-r;if(0!==r){var o=0;for(e=0;e<this.length;e++){var a=this.words[e]&s;this.words[e]=(0|this.words[e])-a<<r|o,o=a>>>26-r}o&&(this.words[e]=o,this.length++)}if(0!==n){for(e=this.length-1;e>=0;e--)this.words[e+n]=this.words[e];for(e=0;e<n;e++)this.words[e]=0;this.length+=n}return this.strip()},n.prototype.ishln=function(t){return i(0===this.negative),this.iushln(t)},n.prototype.iushrn=function(t,e,r){var n;i("number"==typeof t&&t>=0),n=e?(e-e%26)/26:0;var s=t%26,o=Math.min((t-s)/26,this.length),a=67108863^67108863>>>s<<s,h=r;if(n-=o,n=Math.max(0,n),h){for(var u=0;u<o;u++)h.words[u]=this.words[u];h.length=o}if(0===o);else if(this.length>o)for(this.length-=o,u=0;u<this.length;u++)this.words[u]=this.words[u+o];else this.words[0]=0,this.length=1;var l=0;for(u=this.length-1;u>=0&&(0!==l||u>=n);u--){var c=0|this.words[u];this.words[u]=l<<26-s|c>>>s,l=c&a}return h&&0!==l&&(h.words[h.length++]=l),0===this.length&&(this.words[0]=0,this.length=1),this.strip()},n.prototype.ishrn=function(t,e,r){return i(0===this.negative),this.iushrn(t,e,r)},n.prototype.shln=function(t){return this.clone().ishln(t)},n.prototype.ushln=function(t){return this.clone().iushln(t)},n.prototype.shrn=function(t){return this.clone().ishrn(t)},n.prototype.ushrn=function(t){return this.clone().iushrn(t)},n.prototype.testn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return!(this.length<=r||!(this.words[r]&1<<e))},n.prototype.imaskn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return i(0===this.negative,"imaskn works only with positive numbers"),this.length<=r?this:(0!==e&&r++,this.length=Math.min(r,this.length),0!==e&&(this.words[this.length-1]&=67108863^67108863>>>e<<e),this.strip())},n.prototype.maskn=function(t){return this.clone().imaskn(t)},n.prototype.iaddn=function(t){return i("number"==typeof t),i(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},n.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},n.prototype.isubn=function(t){if(i("number"==typeof t),i(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this.strip()},n.prototype.addn=function(t){return this.clone().iaddn(t)},n.prototype.subn=function(t){return this.clone().isubn(t)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(t,e,r){var n,s;this._expand(t.length+r);var o=0;for(n=0;n<t.length;n++){s=(0|this.words[n+r])+o;var a=(0|t.words[n])*e;o=((s-=67108863&a)>>26)-(a/67108864|0),this.words[n+r]=67108863&s}for(;n<this.length-r;n++)o=(s=(0|this.words[n+r])+o)>>26,this.words[n+r]=67108863&s;if(0===o)return this.strip();for(i(-1===o),o=0,n=0;n<this.length;n++)o=(s=-(0|this.words[n])+o)>>26,this.words[n]=67108863&s;return this.negative=1,this.strip()},n.prototype._wordDiv=function(t,e){var i,r=this.clone(),s=t,o=0|s.words[s.length-1];0!=(i=26-this._countBits(o))&&(s=s.ushln(i),r.iushln(i),o=0|s.words[s.length-1]);var a,h=r.length-s.length;if("mod"!==e){(a=new n(null)).length=h+1,a.words=new Array(a.length);for(var u=0;u<a.length;u++)a.words[u]=0}var l=r.clone()._ishlnsubmul(s,1,h);0===l.negative&&(r=l,a&&(a.words[h]=1));for(var c=h-1;c>=0;c--){var d=67108864*(0|r.words[s.length+c])+(0|r.words[s.length+c-1]);for(d=Math.min(d/o|0,67108863),r._ishlnsubmul(s,d,c);0!==r.negative;)d--,r.negative=0,r._ishlnsubmul(s,1,c),r.isZero()||(r.negative^=1);a&&(a.words[c]=d)}return a&&a.strip(),r.strip(),"div"!==e&&0!==i&&r.iushrn(i),{div:a||null,mod:r}},n.prototype.divmod=function(t,e,r){return i(!t.isZero()),this.isZero()?{div:new n(0),mod:new n(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(s=a.div.neg()),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.iadd(t)),{div:s,mod:o}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(s=a.div.neg()),{div:s,mod:a.mod}):0!=(this.negative&t.negative)?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.isub(t)),{div:a.div,mod:o}):t.length>this.length||this.cmp(t)<0?{div:new n(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new n(this.modn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new n(this.modn(t.words[0]))}:this._wordDiv(t,e);var s,o,a},n.prototype.div=function(t){return this.divmod(t,"div",!1).div},n.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},n.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},n.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var i=0!==e.div.negative?e.mod.isub(t):e.mod,r=t.ushrn(1),n=t.andln(1),s=i.cmp(r);return s<0||1===n&&0===s?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},n.prototype.modn=function(t){i(t<=67108863);for(var e=(1<<26)%t,r=0,n=this.length-1;n>=0;n--)r=(e*r+(0|this.words[n]))%t;return r},n.prototype.idivn=function(t){i(t<=67108863);for(var e=0,r=this.length-1;r>=0;r--){var n=(0|this.words[r])+67108864*e;this.words[r]=n/t|0,e=n%t}return this.strip()},n.prototype.divn=function(t){return this.clone().idivn(t)},n.prototype.egcd=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s=new n(1),o=new n(0),a=new n(0),h=new n(1),u=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++u;for(var l=r.clone(),c=e.clone();!e.isZero();){for(var d=0,f=1;0==(e.words[0]&f)&&d<26;++d,f<<=1);if(d>0)for(e.iushrn(d);d-- >0;)(s.isOdd()||o.isOdd())&&(s.iadd(l),o.isub(c)),s.iushrn(1),o.iushrn(1);for(var m=0,g=1;0==(r.words[0]&g)&&m<26;++m,g<<=1);if(m>0)for(r.iushrn(m);m-- >0;)(a.isOdd()||h.isOdd())&&(a.iadd(l),h.isub(c)),a.iushrn(1),h.iushrn(1);e.cmp(r)>=0?(e.isub(r),s.isub(a),o.isub(h)):(r.isub(e),a.isub(s),h.isub(o))}return{a:a,b:h,gcd:r.iushln(u)}},n.prototype._invmp=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s,o=new n(1),a=new n(0),h=r.clone();e.cmpn(1)>0&&r.cmpn(1)>0;){for(var u=0,l=1;0==(e.words[0]&l)&&u<26;++u,l<<=1);if(u>0)for(e.iushrn(u);u-- >0;)o.isOdd()&&o.iadd(h),o.iushrn(1);for(var c=0,d=1;0==(r.words[0]&d)&&c<26;++c,d<<=1);if(c>0)for(r.iushrn(c);c-- >0;)a.isOdd()&&a.iadd(h),a.iushrn(1);e.cmp(r)>=0?(e.isub(r),o.isub(a)):(r.isub(e),a.isub(o))}return(s=0===e.cmpn(1)?o:a).cmpn(0)<0&&s.iadd(t),s},n.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),i=t.clone();e.negative=0,i.negative=0;for(var r=0;e.isEven()&&i.isEven();r++)e.iushrn(1),i.iushrn(1);for(;;){for(;e.isEven();)e.iushrn(1);for(;i.isEven();)i.iushrn(1);var n=e.cmp(i);if(n<0){var s=e;e=i,i=s}else if(0===n||0===i.cmpn(1))break;e.isub(i)}return i.iushln(r)},n.prototype.invm=function(t){return this.egcd(t).a.umod(t)},n.prototype.isEven=function(){return 0==(1&this.words[0])},n.prototype.isOdd=function(){return 1==(1&this.words[0])},n.prototype.andln=function(t){return this.words[0]&t},n.prototype.bincn=function(t){i("number"==typeof t);var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return this._expand(r+1),this.words[r]|=n,this;for(var s=n,o=r;0!==s&&o<this.length;o++){var a=0|this.words[o];s=(a+=s)>>>26,this.words[o]=a&=67108863}return 0!==s&&(this.words[o]=s,this.length++),this},n.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},n.prototype.cmpn=function(t){var e,r=t<0;if(0!==this.negative&&!r)return-1;if(0===this.negative&&r)return 1;if(this.strip(),this.length>1)e=1;else{r&&(t=-t),i(t<=67108863,"Number is too big");var n=0|this.words[0];e=n===t?0:n<t?-1:1}return 0!==this.negative?0|-e:e},n.prototype.cmp=function(t){if(0!==this.negative&&0===t.negative)return-1;if(0===this.negative&&0!==t.negative)return 1;var e=this.ucmp(t);return 0!==this.negative?0|-e:e},n.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,i=this.length-1;i>=0;i--){var r=0|this.words[i],n=0|t.words[i];if(r!==n){r<n?e=-1:r>n&&(e=1);break}}return e},n.prototype.gtn=function(t){return 1===this.cmpn(t)},n.prototype.gt=function(t){return 1===this.cmp(t)},n.prototype.gten=function(t){return this.cmpn(t)>=0},n.prototype.gte=function(t){return this.cmp(t)>=0},n.prototype.ltn=function(t){return-1===this.cmpn(t)},n.prototype.lt=function(t){return-1===this.cmp(t)},n.prototype.lten=function(t){return this.cmpn(t)<=0},n.prototype.lte=function(t){return this.cmp(t)<=0},n.prototype.eqn=function(t){return 0===this.cmpn(t)},n.prototype.eq=function(t){return 0===this.cmp(t)},n.red=function(t){return new b(t)},n.prototype.toRed=function(t){return i(!this.red,"Already a number in reduction context"),i(0===this.negative,"red works only with positives"),t.convertTo(this)._forceRed(t)},n.prototype.fromRed=function(){return i(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(t){return this.red=t,this},n.prototype.forceRed=function(t){return i(!this.red,"Already a number in reduction context"),this._forceRed(t)},n.prototype.redAdd=function(t){return i(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},n.prototype.redIAdd=function(t){return i(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},n.prototype.redSub=function(t){return i(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},n.prototype.redISub=function(t){return i(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},n.prototype.redShl=function(t){return i(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},n.prototype.redMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},n.prototype.redIMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},n.prototype.redSqr=function(){return i(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return i(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return i(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return i(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return i(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(t){return i(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var g={k256:null,p224:null,p192:null,p25519:null};function p(t,e){this.name=t,this.p=new n(e,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function w(){p.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function v(){p.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function y(){p.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function M(){p.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function b(t){if("string"==typeof t){var e=n._prime(t);this.m=e.p,this.prime=e}else i(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}function A(t){b.call(this,t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}p.prototype._tmp=function(){var t=new n(null);return t.words=new Array(Math.ceil(this.n/13)),t},p.prototype.ireduce=function(t){var e,i=t;do{this.split(i,this.tmp),e=(i=(i=this.imulK(i)).iadd(this.tmp)).bitLength()}while(e>this.n);var r=e<this.n?-1:i.ucmp(this.p);return 0===r?(i.words[0]=0,i.length=1):r>0?i.isub(this.p):i.strip(),i},p.prototype.split=function(t,e){t.iushrn(this.n,0,e)},p.prototype.imulK=function(t){return t.imul(this.k)},r(w,p),w.prototype.split=function(t,e){for(var i=4194303,r=Math.min(t.length,9),n=0;n<r;n++)e.words[n]=t.words[n];if(e.length=r,t.length<=9)return t.words[0]=0,void(t.length=1);var s=t.words[9];for(e.words[e.length++]=s&i,n=10;n<t.length;n++){var o=0|t.words[n];t.words[n-10]=(o&i)<<4|s>>>22,s=o}t.words[n-10]=s>>>=22,t.length-=0===s&&t.length>10?10:9},w.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,i=0;i<t.length;i++){var r=0|t.words[i];t.words[i]=67108863&(e+=977*r),e=64*r+(e/67108864|0)}return 0===t.words[t.length-1]&&(t.length--,0===t.words[t.length-1]&&t.length--),t},r(v,p),r(y,p),r(M,p),M.prototype.imulK=function(t){for(var e=0,i=0;i<t.length;i++){var r=19*(0|t.words[i])+e,n=67108863&r;r>>>=26,t.words[i]=n,e=r}return 0!==e&&(t.words[t.length++]=e),t},n._prime=function(t){if(g[t])return g[t];var e;if("k256"===t)e=new w;else if("p224"===t)e=new v;else if("p192"===t)e=new y;else{if("p25519"!==t)throw new Error("Unknown prime "+t);e=new M}return g[t]=e,e},b.prototype._verify1=function(t){i(0===t.negative,"red works only with positives"),i(t.red,"red works only with red numbers")},b.prototype._verify2=function(t,e){i(0==(t.negative|e.negative),"red works only with positives"),i(t.red&&t.red===e.red,"red works only with red numbers")},b.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):t.umod(this.m)._forceRed(this)},b.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},b.prototype.add=function(t,e){this._verify2(t,e);var i=t.add(e);return i.cmp(this.m)>=0&&i.isub(this.m),i._forceRed(this)},b.prototype.iadd=function(t,e){this._verify2(t,e);var i=t.iadd(e);return i.cmp(this.m)>=0&&i.isub(this.m),i},b.prototype.sub=function(t,e){this._verify2(t,e);var i=t.sub(e);return i.cmpn(0)<0&&i.iadd(this.m),i._forceRed(this)},b.prototype.isub=function(t,e){this._verify2(t,e);var i=t.isub(e);return i.cmpn(0)<0&&i.iadd(this.m),i},b.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},b.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},b.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},b.prototype.isqr=function(t){return this.imul(t,t.clone())},b.prototype.sqr=function(t){return this.mul(t,t)},b.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(i(e%2==1),3===e){var r=this.m.add(new n(1)).iushrn(2);return this.pow(t,r)}for(var s=this.m.subn(1),o=0;!s.isZero()&&0===s.andln(1);)o++,s.iushrn(1);i(!s.isZero());var a=new n(1).toRed(this),h=a.redNeg(),u=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new n(2*l*l).toRed(this);0!==this.pow(l,u).cmp(h);)l.redIAdd(h);for(var c=this.pow(l,s),d=this.pow(t,s.addn(1).iushrn(1)),f=this.pow(t,s),m=o;0!==f.cmp(a);){for(var g=f,p=0;0!==g.cmp(a);p++)g=g.redSqr();i(p<m);var w=this.pow(c,new n(1).iushln(m-p-1));d=d.redMul(w),c=w.redSqr(),f=f.redMul(c),m=p}return d},b.prototype.invm=function(t){var e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},b.prototype.pow=function(t,e){if(e.isZero())return new n(1);if(0===e.cmpn(1))return t.clone();var i=new Array(16);i[0]=new n(1).toRed(this),i[1]=t;for(var r=2;r<i.length;r++)i[r]=this.mul(i[r-1],t);var s=i[0],o=0,a=0,h=e.bitLength()%26;for(0===h&&(h=26),r=e.length-1;r>=0;r--){for(var u=e.words[r],l=h-1;l>=0;l--){var c=u>>l&1;s!==i[0]&&(s=this.sqr(s)),0!==c||0!==o?(o<<=1,o|=c,(4==++a||0===r&&0===l)&&(s=this.mul(s,i[o]),a=0,o=0)):a=0}h=26}return s},b.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},b.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},n.mont=function(t){return new A(t)},r(A,b),A.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},A.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},A.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var i=t.imul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),n=i.isub(r).iushrn(this.shift),s=n;return n.cmp(this.m)>=0?s=n.isub(this.m):n.cmpn(0)<0&&(s=n.iadd(this.m)),s._forceRed(this)},A.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new n(0)._forceRed(this);var i=t.mul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(r).iushrn(this.shift),o=s;return s.cmp(this.m)>=0?o=s.isub(this.m):s.cmpn(0)<0&&(o=s.iadd(this.m)),o._forceRed(this)},A.prototype.invm=function(t){return this.imod(t._invmp(this.m).mul(this.r2))._forceRed(this)}}(t,gt)}),yt=pt(function(t){!function(t,e){function i(t,e){if(!t)throw new Error(e||"Assertion failed")}function r(t,e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}function n(t,e,i){if(n.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(i=e,e=10),this._init(t||0,e||10,i||"be"))}var s;"object"==typeof t?t.exports=n:e.BN=n,n.BN=n,n.wordSize=26;try{s=wt("buffer").Buffer}catch(t){}function o(t,e,i){for(var r=0,n=Math.min(t.length,i),s=e;s<n;s++){var o=t.charCodeAt(s)-48;r<<=4,r|=o>=49&&o<=54?o-49+10:o>=17&&o<=22?o-17+10:15&o}return r}function a(t,e,i,r){for(var n=0,s=Math.min(t.length,i),o=e;o<s;o++){var a=t.charCodeAt(o)-48;n*=r,n+=a>=49?a-49+10:a>=17?a-17+10:a}return n}n.isBN=function(t){return t instanceof n||null!==t&&"object"==typeof t&&t.constructor.wordSize===n.wordSize&&Array.isArray(t.words)},n.max=function(t,e){return t.cmp(e)>0?t:e},n.min=function(t,e){return t.cmp(e)<0?t:e},n.prototype._init=function(t,e,r){if("number"==typeof t)return this._initNumber(t,e,r);if("object"==typeof t)return this._initArray(t,e,r);"hex"===e&&(e=16),i(e===(0|e)&&e>=2&&e<=36);var n=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&n++,16===e?this._parseHex(t,n):this._parseBase(t,e,n),"-"===t[0]&&(this.negative=1),this.strip(),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(i(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initArray=function(t,e,r){if(i("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,o,a=0;if("be"===r)for(n=t.length-1,s=0;n>=0;n-=3)this.words[s]|=(o=t[n]|t[n-1]<<8|t[n-2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);else if("le"===r)for(n=0,s=0;n<t.length;n+=3)this.words[s]|=(o=t[n]|t[n+1]<<8|t[n+2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);return this.strip()},n.prototype._parseHex=function(t,e){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var r,n,s=0;for(i=t.length-6,r=0;i>=e;i-=6)n=o(t,i,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303,(s+=24)>=26&&(s-=26,r++);i+6!==e&&(n=o(t,e,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303),this.strip()},n.prototype._parseBase=function(t,e,i){this.words=[0],this.length=1;for(var r=0,n=1;n<=67108863;n*=e)r++;r--,n=n/e|0;for(var s=t.length-i,o=s%r,h=Math.min(s,s-o)+i,u=0,l=i;l<h;l+=r)u=a(t,l,l+r,e),this.imuln(n),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(0!==o){var c=1;for(u=a(t,l,t.length,e),l=0;l<o;l++)c*=e;this.imuln(c),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}},n.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},n.prototype.clone=function(){var t=new n(null);return this.copy(t),t},n.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},n.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},n.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},n.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var h=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],u=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],l=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function c(t,e,i){i.negative=e.negative^t.negative;var r=t.length+e.length|0;i.length=r,r=r-1|0;var n=0|t.words[0],s=0|e.words[0],o=n*s,a=o/67108864|0;i.words[0]=67108863&o;for(var h=1;h<r;h++){for(var u=a>>>26,l=67108863&a,c=Math.min(h,e.length-1),d=Math.max(0,h-t.length+1);d<=c;d++)u+=(o=(n=0|t.words[h-d|0])*(s=0|e.words[d])+l)/67108864|0,l=67108863&o;i.words[h]=0|l,a=0|u}return 0!==a?i.words[h]=0|a:i.length--,i.strip()}n.prototype.toString=function(t,e){var r;if(e=0|e||1,16===(t=t||10)||"hex"===t){r="";for(var n=0,s=0,o=0;o<this.length;o++){var a=this.words[o],c=(16777215&(a<<n|s)).toString(16);r=0!=(s=a>>>24-n&16777215)||o!==this.length-1?h[6-c.length]+c+r:c+r,(n+=2)>=26&&(n-=26,o--)}for(0!==s&&(r=s.toString(16)+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}if(t===(0|t)&&t>=2&&t<=36){var d=u[t],f=l[t];r="";var m=this.clone();for(m.negative=0;!m.isZero();){var g=m.modn(f).toString(t);r=(m=m.idivn(f)).isZero()?g+r:h[d-g.length]+g+r}for(this.isZero()&&(r="0"+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}i(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&i(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},n.prototype.toJSON=function(){return this.toString(16)},n.prototype.toBuffer=function(t,e){return i(void 0!==s),this.toArrayLike(s,t,e)},n.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},n.prototype.toArrayLike=function(t,e,r){var n=this.byteLength(),s=r||Math.max(1,n);i(n<=s,"byte array longer than desired length"),i(s>0,"Requested array length <= 0"),this.strip();var o,a,h="le"===e,u=new t(s),l=this.clone();if(h){for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[a]=o;for(;a<s;a++)u[a]=0}else{for(a=0;a<s-n;a++)u[a]=0;for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[s-a-1]=o}return u},n.prototype._countBits=Math.clz32?function(t){return 32-Math.clz32(t)}:function(t){var e=t,i=0;return e>=4096&&(i+=13,e>>>=13),e>=64&&(i+=7,e>>>=7),e>=8&&(i+=4,e>>>=4),e>=2&&(i+=2,e>>>=2),i+e},n.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,i=0;return 0==(8191&e)&&(i+=13,e>>>=13),0==(127&e)&&(i+=7,e>>>=7),0==(15&e)&&(i+=4,e>>>=4),0==(3&e)&&(i+=2,e>>>=2),0==(1&e)&&i++,i},n.prototype.bitLength=function(){var t=this._countBits(this.words[this.length-1]);return 26*(this.length-1)+t},n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var i=this._zeroBits(this.words[e]);if(t+=i,26!==i)break}return t},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},n.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return 0!==this.negative},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this.strip()},n.prototype.ior=function(t){return i(0==(this.negative|t.negative)),this.iuor(t)},n.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},n.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},n.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var i=0;i<e.length;i++)this.words[i]=this.words[i]&t.words[i];return this.length=e.length,this.strip()},n.prototype.iand=function(t){return i(0==(this.negative|t.negative)),this.iuand(t)},n.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},n.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},n.prototype.iuxor=function(t){var e,i;this.length>t.length?(e=this,i=t):(e=t,i=this);for(var r=0;r<i.length;r++)this.words[r]=e.words[r]^i.words[r];if(this!==e)for(;r<e.length;r++)this.words[r]=e.words[r];return this.length=e.length,this.strip()},n.prototype.ixor=function(t){return i(0==(this.negative|t.negative)),this.iuxor(t)},n.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},n.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},n.prototype.inotn=function(t){i("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=67108863&~this.words[n];return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this.strip()},n.prototype.notn=function(t){return this.clone().inotn(t)},n.prototype.setn=function(t,e){i("number"==typeof t&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),this.words[r]=e?this.words[r]|1<<n:this.words[r]&~(1<<n),this.strip()},n.prototype.iadd=function(t){var e,i,r;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(i=this,r=t):(i=t,r=this);for(var n=0,s=0;s<r.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+(0|r.words[s])+n),n=e>>>26;for(;0!==n&&s<i.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+n),n=e>>>26;if(this.length=i.length,0!==n)this.words[this.length]=n,this.length++;else if(i!==this)for(;s<i.length;s++)this.words[s]=i.words[s];return this},n.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},n.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var i,r,n=this.cmp(t);if(0===n)return this.negative=0,this.length=1,this.words[0]=0,this;n>0?(i=this,r=t):(i=t,r=this);for(var s=0,o=0;o<r.length;o++)s=(e=(0|i.words[o])-(0|r.words[o])+s)>>26,this.words[o]=67108863&e;for(;0!==s&&o<i.length;o++)s=(e=(0|i.words[o])+s)>>26,this.words[o]=67108863&e;if(0===s&&o<i.length&&i!==this)for(;o<i.length;o++)this.words[o]=i.words[o];return this.length=Math.max(this.length,o),i!==this&&(this.negative=1),this.strip()},n.prototype.sub=function(t){return this.clone().isub(t)};var d=function(t,e,i){var r,n,s,o=t.words,a=e.words,h=i.words,u=0,l=0|o[0],c=8191&l,d=l>>>13,f=0|o[1],m=8191&f,g=f>>>13,p=0|o[2],w=8191&p,v=p>>>13,y=0|o[3],M=8191&y,b=y>>>13,A=0|o[4],x=8191&A,E=A>>>13,R=0|o[5],k=8191&R,S=R>>>13,I=0|o[6],T=8191&I,O=I>>>13,B=0|o[7],C=8191&B,_=B>>>13,D=0|o[8],P=8191&D,N=D>>>13,L=0|o[9],W=8191&L,F=L>>>13,U=0|a[0],$=8191&U,j=U>>>13,q=0|a[1],G=8191&q,z=q>>>13,H=0|a[2],Z=8191&H,V=H>>>13,J=0|a[3],K=8191&J,X=J>>>13,Q=0|a[4],Y=8191&Q,tt=Q>>>13,et=0|a[5],it=8191&et,rt=et>>>13,nt=0|a[6],st=8191&nt,ot=nt>>>13,at=0|a[7],ht=8191&at,ut=at>>>13,lt=0|a[8],ct=8191&lt,dt=lt>>>13,ft=0|a[9],mt=8191&ft,gt=ft>>>13;i.negative=t.negative^e.negative,i.length=19;var pt=(u+(r=Math.imul(c,$))|0)+((8191&(n=(n=Math.imul(c,j))+Math.imul(d,$)|0))<<13)|0;u=((s=Math.imul(d,j))+(n>>>13)|0)+(pt>>>26)|0,pt&=67108863,r=Math.imul(m,$),n=(n=Math.imul(m,j))+Math.imul(g,$)|0,s=Math.imul(g,j);var wt=(u+(r=r+Math.imul(c,G)|0)|0)+((8191&(n=(n=n+Math.imul(c,z)|0)+Math.imul(d,G)|0))<<13)|0;u=((s=s+Math.imul(d,z)|0)+(n>>>13)|0)+(wt>>>26)|0,wt&=67108863,r=Math.imul(w,$),n=(n=Math.imul(w,j))+Math.imul(v,$)|0,s=Math.imul(v,j),r=r+Math.imul(m,G)|0,n=(n=n+Math.imul(m,z)|0)+Math.imul(g,G)|0,s=s+Math.imul(g,z)|0;var vt=(u+(r=r+Math.imul(c,Z)|0)|0)+((8191&(n=(n=n+Math.imul(c,V)|0)+Math.imul(d,Z)|0))<<13)|0;u=((s=s+Math.imul(d,V)|0)+(n>>>13)|0)+(vt>>>26)|0,vt&=67108863,r=Math.imul(M,$),n=(n=Math.imul(M,j))+Math.imul(b,$)|0,s=Math.imul(b,j),r=r+Math.imul(w,G)|0,n=(n=n+Math.imul(w,z)|0)+Math.imul(v,G)|0,s=s+Math.imul(v,z)|0,r=r+Math.imul(m,Z)|0,n=(n=n+Math.imul(m,V)|0)+Math.imul(g,Z)|0,s=s+Math.imul(g,V)|0;var yt=(u+(r=r+Math.imul(c,K)|0)|0)+((8191&(n=(n=n+Math.imul(c,X)|0)+Math.imul(d,K)|0))<<13)|0;u=((s=s+Math.imul(d,X)|0)+(n>>>13)|0)+(yt>>>26)|0,yt&=67108863,r=Math.imul(x,$),n=(n=Math.imul(x,j))+Math.imul(E,$)|0,s=Math.imul(E,j),r=r+Math.imul(M,G)|0,n=(n=n+Math.imul(M,z)|0)+Math.imul(b,G)|0,s=s+Math.imul(b,z)|0,r=r+Math.imul(w,Z)|0,n=(n=n+Math.imul(w,V)|0)+Math.imul(v,Z)|0,s=s+Math.imul(v,V)|0,r=r+Math.imul(m,K)|0,n=(n=n+Math.imul(m,X)|0)+Math.imul(g,K)|0,s=s+Math.imul(g,X)|0;var Mt=(u+(r=r+Math.imul(c,Y)|0)|0)+((8191&(n=(n=n+Math.imul(c,tt)|0)+Math.imul(d,Y)|0))<<13)|0;u=((s=s+Math.imul(d,tt)|0)+(n>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,r=Math.imul(k,$),n=(n=Math.imul(k,j))+Math.imul(S,$)|0,s=Math.imul(S,j),r=r+Math.imul(x,G)|0,n=(n=n+Math.imul(x,z)|0)+Math.imul(E,G)|0,s=s+Math.imul(E,z)|0,r=r+Math.imul(M,Z)|0,n=(n=n+Math.imul(M,V)|0)+Math.imul(b,Z)|0,s=s+Math.imul(b,V)|0,r=r+Math.imul(w,K)|0,n=(n=n+Math.imul(w,X)|0)+Math.imul(v,K)|0,s=s+Math.imul(v,X)|0,r=r+Math.imul(m,Y)|0,n=(n=n+Math.imul(m,tt)|0)+Math.imul(g,Y)|0,s=s+Math.imul(g,tt)|0;var bt=(u+(r=r+Math.imul(c,it)|0)|0)+((8191&(n=(n=n+Math.imul(c,rt)|0)+Math.imul(d,it)|0))<<13)|0;u=((s=s+Math.imul(d,rt)|0)+(n>>>13)|0)+(bt>>>26)|0,bt&=67108863,r=Math.imul(T,$),n=(n=Math.imul(T,j))+Math.imul(O,$)|0,s=Math.imul(O,j),r=r+Math.imul(k,G)|0,n=(n=n+Math.imul(k,z)|0)+Math.imul(S,G)|0,s=s+Math.imul(S,z)|0,r=r+Math.imul(x,Z)|0,n=(n=n+Math.imul(x,V)|0)+Math.imul(E,Z)|0,s=s+Math.imul(E,V)|0,r=r+Math.imul(M,K)|0,n=(n=n+Math.imul(M,X)|0)+Math.imul(b,K)|0,s=s+Math.imul(b,X)|0,r=r+Math.imul(w,Y)|0,n=(n=n+Math.imul(w,tt)|0)+Math.imul(v,Y)|0,s=s+Math.imul(v,tt)|0,r=r+Math.imul(m,it)|0,n=(n=n+Math.imul(m,rt)|0)+Math.imul(g,it)|0,s=s+Math.imul(g,rt)|0;var At=(u+(r=r+Math.imul(c,st)|0)|0)+((8191&(n=(n=n+Math.imul(c,ot)|0)+Math.imul(d,st)|0))<<13)|0;u=((s=s+Math.imul(d,ot)|0)+(n>>>13)|0)+(At>>>26)|0,At&=67108863,r=Math.imul(C,$),n=(n=Math.imul(C,j))+Math.imul(_,$)|0,s=Math.imul(_,j),r=r+Math.imul(T,G)|0,n=(n=n+Math.imul(T,z)|0)+Math.imul(O,G)|0,s=s+Math.imul(O,z)|0,r=r+Math.imul(k,Z)|0,n=(n=n+Math.imul(k,V)|0)+Math.imul(S,Z)|0,s=s+Math.imul(S,V)|0,r=r+Math.imul(x,K)|0,n=(n=n+Math.imul(x,X)|0)+Math.imul(E,K)|0,s=s+Math.imul(E,X)|0,r=r+Math.imul(M,Y)|0,n=(n=n+Math.imul(M,tt)|0)+Math.imul(b,Y)|0,s=s+Math.imul(b,tt)|0,r=r+Math.imul(w,it)|0,n=(n=n+Math.imul(w,rt)|0)+Math.imul(v,it)|0,s=s+Math.imul(v,rt)|0,r=r+Math.imul(m,st)|0,n=(n=n+Math.imul(m,ot)|0)+Math.imul(g,st)|0,s=s+Math.imul(g,ot)|0;var xt=(u+(r=r+Math.imul(c,ht)|0)|0)+((8191&(n=(n=n+Math.imul(c,ut)|0)+Math.imul(d,ht)|0))<<13)|0;u=((s=s+Math.imul(d,ut)|0)+(n>>>13)|0)+(xt>>>26)|0,xt&=67108863,r=Math.imul(P,$),n=(n=Math.imul(P,j))+Math.imul(N,$)|0,s=Math.imul(N,j),r=r+Math.imul(C,G)|0,n=(n=n+Math.imul(C,z)|0)+Math.imul(_,G)|0,s=s+Math.imul(_,z)|0,r=r+Math.imul(T,Z)|0,n=(n=n+Math.imul(T,V)|0)+Math.imul(O,Z)|0,s=s+Math.imul(O,V)|0,r=r+Math.imul(k,K)|0,n=(n=n+Math.imul(k,X)|0)+Math.imul(S,K)|0,s=s+Math.imul(S,X)|0,r=r+Math.imul(x,Y)|0,n=(n=n+Math.imul(x,tt)|0)+Math.imul(E,Y)|0,s=s+Math.imul(E,tt)|0,r=r+Math.imul(M,it)|0,n=(n=n+Math.imul(M,rt)|0)+Math.imul(b,it)|0,s=s+Math.imul(b,rt)|0,r=r+Math.imul(w,st)|0,n=(n=n+Math.imul(w,ot)|0)+Math.imul(v,st)|0,s=s+Math.imul(v,ot)|0,r=r+Math.imul(m,ht)|0,n=(n=n+Math.imul(m,ut)|0)+Math.imul(g,ht)|0,s=s+Math.imul(g,ut)|0;var Et=(u+(r=r+Math.imul(c,ct)|0)|0)+((8191&(n=(n=n+Math.imul(c,dt)|0)+Math.imul(d,ct)|0))<<13)|0;u=((s=s+Math.imul(d,dt)|0)+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,r=Math.imul(W,$),n=(n=Math.imul(W,j))+Math.imul(F,$)|0,s=Math.imul(F,j),r=r+Math.imul(P,G)|0,n=(n=n+Math.imul(P,z)|0)+Math.imul(N,G)|0,s=s+Math.imul(N,z)|0,r=r+Math.imul(C,Z)|0,n=(n=n+Math.imul(C,V)|0)+Math.imul(_,Z)|0,s=s+Math.imul(_,V)|0,r=r+Math.imul(T,K)|0,n=(n=n+Math.imul(T,X)|0)+Math.imul(O,K)|0,s=s+Math.imul(O,X)|0,r=r+Math.imul(k,Y)|0,n=(n=n+Math.imul(k,tt)|0)+Math.imul(S,Y)|0,s=s+Math.imul(S,tt)|0,r=r+Math.imul(x,it)|0,n=(n=n+Math.imul(x,rt)|0)+Math.imul(E,it)|0,s=s+Math.imul(E,rt)|0,r=r+Math.imul(M,st)|0,n=(n=n+Math.imul(M,ot)|0)+Math.imul(b,st)|0,s=s+Math.imul(b,ot)|0,r=r+Math.imul(w,ht)|0,n=(n=n+Math.imul(w,ut)|0)+Math.imul(v,ht)|0,s=s+Math.imul(v,ut)|0,r=r+Math.imul(m,ct)|0,n=(n=n+Math.imul(m,dt)|0)+Math.imul(g,ct)|0,s=s+Math.imul(g,dt)|0;var Rt=(u+(r=r+Math.imul(c,mt)|0)|0)+((8191&(n=(n=n+Math.imul(c,gt)|0)+Math.imul(d,mt)|0))<<13)|0;u=((s=s+Math.imul(d,gt)|0)+(n>>>13)|0)+(Rt>>>26)|0,Rt&=67108863,r=Math.imul(W,G),n=(n=Math.imul(W,z))+Math.imul(F,G)|0,s=Math.imul(F,z),r=r+Math.imul(P,Z)|0,n=(n=n+Math.imul(P,V)|0)+Math.imul(N,Z)|0,s=s+Math.imul(N,V)|0,r=r+Math.imul(C,K)|0,n=(n=n+Math.imul(C,X)|0)+Math.imul(_,K)|0,s=s+Math.imul(_,X)|0,r=r+Math.imul(T,Y)|0,n=(n=n+Math.imul(T,tt)|0)+Math.imul(O,Y)|0,s=s+Math.imul(O,tt)|0,r=r+Math.imul(k,it)|0,n=(n=n+Math.imul(k,rt)|0)+Math.imul(S,it)|0,s=s+Math.imul(S,rt)|0,r=r+Math.imul(x,st)|0,n=(n=n+Math.imul(x,ot)|0)+Math.imul(E,st)|0,s=s+Math.imul(E,ot)|0,r=r+Math.imul(M,ht)|0,n=(n=n+Math.imul(M,ut)|0)+Math.imul(b,ht)|0,s=s+Math.imul(b,ut)|0,r=r+Math.imul(w,ct)|0,n=(n=n+Math.imul(w,dt)|0)+Math.imul(v,ct)|0,s=s+Math.imul(v,dt)|0;var kt=(u+(r=r+Math.imul(m,mt)|0)|0)+((8191&(n=(n=n+Math.imul(m,gt)|0)+Math.imul(g,mt)|0))<<13)|0;u=((s=s+Math.imul(g,gt)|0)+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,r=Math.imul(W,Z),n=(n=Math.imul(W,V))+Math.imul(F,Z)|0,s=Math.imul(F,V),r=r+Math.imul(P,K)|0,n=(n=n+Math.imul(P,X)|0)+Math.imul(N,K)|0,s=s+Math.imul(N,X)|0,r=r+Math.imul(C,Y)|0,n=(n=n+Math.imul(C,tt)|0)+Math.imul(_,Y)|0,s=s+Math.imul(_,tt)|0,r=r+Math.imul(T,it)|0,n=(n=n+Math.imul(T,rt)|0)+Math.imul(O,it)|0,s=s+Math.imul(O,rt)|0,r=r+Math.imul(k,st)|0,n=(n=n+Math.imul(k,ot)|0)+Math.imul(S,st)|0,s=s+Math.imul(S,ot)|0,r=r+Math.imul(x,ht)|0,n=(n=n+Math.imul(x,ut)|0)+Math.imul(E,ht)|0,s=s+Math.imul(E,ut)|0,r=r+Math.imul(M,ct)|0,n=(n=n+Math.imul(M,dt)|0)+Math.imul(b,ct)|0,s=s+Math.imul(b,dt)|0;var St=(u+(r=r+Math.imul(w,mt)|0)|0)+((8191&(n=(n=n+Math.imul(w,gt)|0)+Math.imul(v,mt)|0))<<13)|0;u=((s=s+Math.imul(v,gt)|0)+(n>>>13)|0)+(St>>>26)|0,St&=67108863,r=Math.imul(W,K),n=(n=Math.imul(W,X))+Math.imul(F,K)|0,s=Math.imul(F,X),r=r+Math.imul(P,Y)|0,n=(n=n+Math.imul(P,tt)|0)+Math.imul(N,Y)|0,s=s+Math.imul(N,tt)|0,r=r+Math.imul(C,it)|0,n=(n=n+Math.imul(C,rt)|0)+Math.imul(_,it)|0,s=s+Math.imul(_,rt)|0,r=r+Math.imul(T,st)|0,n=(n=n+Math.imul(T,ot)|0)+Math.imul(O,st)|0,s=s+Math.imul(O,ot)|0,r=r+Math.imul(k,ht)|0,n=(n=n+Math.imul(k,ut)|0)+Math.imul(S,ht)|0,s=s+Math.imul(S,ut)|0,r=r+Math.imul(x,ct)|0,n=(n=n+Math.imul(x,dt)|0)+Math.imul(E,ct)|0,s=s+Math.imul(E,dt)|0;var It=(u+(r=r+Math.imul(M,mt)|0)|0)+((8191&(n=(n=n+Math.imul(M,gt)|0)+Math.imul(b,mt)|0))<<13)|0;u=((s=s+Math.imul(b,gt)|0)+(n>>>13)|0)+(It>>>26)|0,It&=67108863,r=Math.imul(W,Y),n=(n=Math.imul(W,tt))+Math.imul(F,Y)|0,s=Math.imul(F,tt),r=r+Math.imul(P,it)|0,n=(n=n+Math.imul(P,rt)|0)+Math.imul(N,it)|0,s=s+Math.imul(N,rt)|0,r=r+Math.imul(C,st)|0,n=(n=n+Math.imul(C,ot)|0)+Math.imul(_,st)|0,s=s+Math.imul(_,ot)|0,r=r+Math.imul(T,ht)|0,n=(n=n+Math.imul(T,ut)|0)+Math.imul(O,ht)|0,s=s+Math.imul(O,ut)|0,r=r+Math.imul(k,ct)|0,n=(n=n+Math.imul(k,dt)|0)+Math.imul(S,ct)|0,s=s+Math.imul(S,dt)|0;var Tt=(u+(r=r+Math.imul(x,mt)|0)|0)+((8191&(n=(n=n+Math.imul(x,gt)|0)+Math.imul(E,mt)|0))<<13)|0;u=((s=s+Math.imul(E,gt)|0)+(n>>>13)|0)+(Tt>>>26)|0,Tt&=67108863,r=Math.imul(W,it),n=(n=Math.imul(W,rt))+Math.imul(F,it)|0,s=Math.imul(F,rt),r=r+Math.imul(P,st)|0,n=(n=n+Math.imul(P,ot)|0)+Math.imul(N,st)|0,s=s+Math.imul(N,ot)|0,r=r+Math.imul(C,ht)|0,n=(n=n+Math.imul(C,ut)|0)+Math.imul(_,ht)|0,s=s+Math.imul(_,ut)|0,r=r+Math.imul(T,ct)|0,n=(n=n+Math.imul(T,dt)|0)+Math.imul(O,ct)|0,s=s+Math.imul(O,dt)|0;var Ot=(u+(r=r+Math.imul(k,mt)|0)|0)+((8191&(n=(n=n+Math.imul(k,gt)|0)+Math.imul(S,mt)|0))<<13)|0;u=((s=s+Math.imul(S,gt)|0)+(n>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,r=Math.imul(W,st),n=(n=Math.imul(W,ot))+Math.imul(F,st)|0,s=Math.imul(F,ot),r=r+Math.imul(P,ht)|0,n=(n=n+Math.imul(P,ut)|0)+Math.imul(N,ht)|0,s=s+Math.imul(N,ut)|0,r=r+Math.imul(C,ct)|0,n=(n=n+Math.imul(C,dt)|0)+Math.imul(_,ct)|0,s=s+Math.imul(_,dt)|0;var Bt=(u+(r=r+Math.imul(T,mt)|0)|0)+((8191&(n=(n=n+Math.imul(T,gt)|0)+Math.imul(O,mt)|0))<<13)|0;u=((s=s+Math.imul(O,gt)|0)+(n>>>13)|0)+(Bt>>>26)|0,Bt&=67108863,r=Math.imul(W,ht),n=(n=Math.imul(W,ut))+Math.imul(F,ht)|0,s=Math.imul(F,ut),r=r+Math.imul(P,ct)|0,n=(n=n+Math.imul(P,dt)|0)+Math.imul(N,ct)|0,s=s+Math.imul(N,dt)|0;var Ct=(u+(r=r+Math.imul(C,mt)|0)|0)+((8191&(n=(n=n+Math.imul(C,gt)|0)+Math.imul(_,mt)|0))<<13)|0;u=((s=s+Math.imul(_,gt)|0)+(n>>>13)|0)+(Ct>>>26)|0,Ct&=67108863,r=Math.imul(W,ct),n=(n=Math.imul(W,dt))+Math.imul(F,ct)|0,s=Math.imul(F,dt);var _t=(u+(r=r+Math.imul(P,mt)|0)|0)+((8191&(n=(n=n+Math.imul(P,gt)|0)+Math.imul(N,mt)|0))<<13)|0;u=((s=s+Math.imul(N,gt)|0)+(n>>>13)|0)+(_t>>>26)|0,_t&=67108863;var Dt=(u+(r=Math.imul(W,mt))|0)+((8191&(n=(n=Math.imul(W,gt))+Math.imul(F,mt)|0))<<13)|0;return u=((s=Math.imul(F,gt))+(n>>>13)|0)+(Dt>>>26)|0,Dt&=67108863,h[0]=pt,h[1]=wt,h[2]=vt,h[3]=yt,h[4]=Mt,h[5]=bt,h[6]=At,h[7]=xt,h[8]=Et,h[9]=Rt,h[10]=kt,h[11]=St,h[12]=It,h[13]=Tt,h[14]=Ot,h[15]=Bt,h[16]=Ct,h[17]=_t,h[18]=Dt,0!==u&&(h[19]=u,i.length++),i};function f(t,e,i){return(new m).mulp(t,e,i)}function m(t,e){this.x=t,this.y=e}Math.imul||(d=c),n.prototype.mulTo=function(t,e){var i=this.length+t.length;return 10===this.length&&10===t.length?d(this,t,e):i<63?c(this,t,e):i<1024?function(t,e,i){i.negative=e.negative^t.negative,i.length=t.length+e.length;for(var r=0,n=0,s=0;s<i.length-1;s++){var o=n;n=0;for(var a=67108863&r,h=Math.min(s,e.length-1),u=Math.max(0,s-t.length+1);u<=h;u++){var l=(0|t.words[s-u])*(0|e.words[u]),c=67108863&l;a=67108863&(c=c+a|0),n+=(o=(o=o+(l/67108864|0)|0)+(c>>>26)|0)>>>26,o&=67108863}i.words[s]=a,r=o,o=n}return 0!==r?i.words[s]=r:i.length--,i.strip()}(this,t,e):f(this,t,e)},m.prototype.makeRBT=function(t){for(var e=new Array(t),i=n.prototype._countBits(t)-1,r=0;r<t;r++)e[r]=this.revBin(r,i,t);return e},m.prototype.revBin=function(t,e,i){if(0===t||t===i-1)return t;for(var r=0,n=0;n<e;n++)r|=(1&t)<<e-n-1,t>>=1;return r},m.prototype.permute=function(t,e,i,r,n,s){for(var o=0;o<s;o++)r[o]=e[t[o]],n[o]=i[t[o]]},m.prototype.transform=function(t,e,i,r,n,s){this.permute(s,t,e,i,r,n);for(var o=1;o<n;o<<=1)for(var a=o<<1,h=Math.cos(2*Math.PI/a),u=Math.sin(2*Math.PI/a),l=0;l<n;l+=a)for(var c=h,d=u,f=0;f<o;f++){var m=i[l+f],g=r[l+f],p=i[l+f+o],w=r[l+f+o],v=c*p-d*w;w=c*w+d*p,i[l+f]=m+(p=v),r[l+f]=g+w,i[l+f+o]=m-p,r[l+f+o]=g-w,f!==a&&(v=h*c-u*d,d=h*d+u*c,c=v)}},m.prototype.guessLen13b=function(t,e){var i=1|Math.max(e,t),r=1&i,n=0;for(i=i/2|0;i;i>>>=1)n++;return 1<<n+1+r},m.prototype.conjugate=function(t,e,i){if(!(i<=1))for(var r=0;r<i/2;r++){var n=t[r];t[r]=t[i-r-1],t[i-r-1]=n,n=e[r],e[r]=-e[i-r-1],e[i-r-1]=-n}},m.prototype.normalize13b=function(t,e){for(var i=0,r=0;r<e/2;r++){var n=8192*Math.round(t[2*r+1]/e)+Math.round(t[2*r]/e)+i;t[r]=67108863&n,i=n<67108864?0:n/67108864|0}return t},m.prototype.convert13b=function(t,e,r,n){for(var s=0,o=0;o<e;o++)r[2*o]=8191&(s+=0|t[o]),r[2*o+1]=8191&(s>>>=13),s>>>=13;for(o=2*e;o<n;++o)r[o]=0;i(0===s),i(0==(-8192&s))},m.prototype.stub=function(t){for(var e=new Array(t),i=0;i<t;i++)e[i]=0;return e},m.prototype.mulp=function(t,e,i){var r=2*this.guessLen13b(t.length,e.length),n=this.makeRBT(r),s=this.stub(r),o=new Array(r),a=new Array(r),h=new Array(r),u=new Array(r),l=new Array(r),c=new Array(r),d=i.words;d.length=r,this.convert13b(t.words,t.length,o,r),this.convert13b(e.words,e.length,u,r),this.transform(o,s,a,h,r,n),this.transform(u,s,l,c,r,n);for(var f=0;f<r;f++){var m=a[f]*l[f]-h[f]*c[f];h[f]=a[f]*c[f]+h[f]*l[f],a[f]=m}return this.conjugate(a,h,r),this.transform(a,h,d,s,r,n),this.conjugate(d,s,r),this.normalize13b(d,r),i.negative=t.negative^e.negative,i.length=t.length+e.length,i.strip()},n.prototype.mul=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},n.prototype.mulf=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),f(this,t,e)},n.prototype.imul=function(t){return this.clone().mulTo(t,this)},n.prototype.imuln=function(t){i("number"==typeof t),i(t<67108864);for(var e=0,r=0;r<this.length;r++){var n=(0|this.words[r])*t,s=(67108863&n)+(67108863&e);e>>=26,e+=n/67108864|0,e+=s>>>26,this.words[r]=67108863&s}return 0!==e&&(this.words[r]=e,this.length++),this},n.prototype.muln=function(t){return this.clone().imuln(t)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),i=0;i<e.length;i++){var r=i%26;e[i]=(t.words[i/26|0]&1<<r)>>>r}return e}(t);if(0===e.length)return new n(1);for(var i=this,r=0;r<e.length&&0===e[r];r++,i=i.sqr());if(++r<e.length)for(var s=i.sqr();r<e.length;r++,s=s.sqr())0!==e[r]&&(i=i.mul(s));return i},n.prototype.iushln=function(t){i("number"==typeof t&&t>=0);var e,r=t%26,n=(t-r)/26,s=67108863>>>26-r<<26-r;if(0!==r){var o=0;for(e=0;e<this.length;e++){var a=this.words[e]&s;this.words[e]=(0|this.words[e])-a<<r|o,o=a>>>26-r}o&&(this.words[e]=o,this.length++)}if(0!==n){for(e=this.length-1;e>=0;e--)this.words[e+n]=this.words[e];for(e=0;e<n;e++)this.words[e]=0;this.length+=n}return this.strip()},n.prototype.ishln=function(t){return i(0===this.negative),this.iushln(t)},n.prototype.iushrn=function(t,e,r){var n;i("number"==typeof t&&t>=0),n=e?(e-e%26)/26:0;var s=t%26,o=Math.min((t-s)/26,this.length),a=67108863^67108863>>>s<<s,h=r;if(n-=o,n=Math.max(0,n),h){for(var u=0;u<o;u++)h.words[u]=this.words[u];h.length=o}if(0===o);else if(this.length>o)for(this.length-=o,u=0;u<this.length;u++)this.words[u]=this.words[u+o];else this.words[0]=0,this.length=1;var l=0;for(u=this.length-1;u>=0&&(0!==l||u>=n);u--){var c=0|this.words[u];this.words[u]=l<<26-s|c>>>s,l=c&a}return h&&0!==l&&(h.words[h.length++]=l),0===this.length&&(this.words[0]=0,this.length=1),this.strip()},n.prototype.ishrn=function(t,e,r){return i(0===this.negative),this.iushrn(t,e,r)},n.prototype.shln=function(t){return this.clone().ishln(t)},n.prototype.ushln=function(t){return this.clone().iushln(t)},n.prototype.shrn=function(t){return this.clone().ishrn(t)},n.prototype.ushrn=function(t){return this.clone().iushrn(t)},n.prototype.testn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return!(this.length<=r||!(this.words[r]&1<<e))},n.prototype.imaskn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return i(0===this.negative,"imaskn works only with positive numbers"),this.length<=r?this:(0!==e&&r++,this.length=Math.min(r,this.length),0!==e&&(this.words[this.length-1]&=67108863^67108863>>>e<<e),this.strip())},n.prototype.maskn=function(t){return this.clone().imaskn(t)},n.prototype.iaddn=function(t){return i("number"==typeof t),i(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},n.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},n.prototype.isubn=function(t){if(i("number"==typeof t),i(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this.strip()},n.prototype.addn=function(t){return this.clone().iaddn(t)},n.prototype.subn=function(t){return this.clone().isubn(t)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(t,e,r){var n,s;this._expand(t.length+r);var o=0;for(n=0;n<t.length;n++){s=(0|this.words[n+r])+o;var a=(0|t.words[n])*e;o=((s-=67108863&a)>>26)-(a/67108864|0),this.words[n+r]=67108863&s}for(;n<this.length-r;n++)o=(s=(0|this.words[n+r])+o)>>26,this.words[n+r]=67108863&s;if(0===o)return this.strip();for(i(-1===o),o=0,n=0;n<this.length;n++)o=(s=-(0|this.words[n])+o)>>26,this.words[n]=67108863&s;return this.negative=1,this.strip()},n.prototype._wordDiv=function(t,e){var i,r=this.clone(),s=t,o=0|s.words[s.length-1];0!=(i=26-this._countBits(o))&&(s=s.ushln(i),r.iushln(i),o=0|s.words[s.length-1]);var a,h=r.length-s.length;if("mod"!==e){(a=new n(null)).length=h+1,a.words=new Array(a.length);for(var u=0;u<a.length;u++)a.words[u]=0}var l=r.clone()._ishlnsubmul(s,1,h);0===l.negative&&(r=l,a&&(a.words[h]=1));for(var c=h-1;c>=0;c--){var d=67108864*(0|r.words[s.length+c])+(0|r.words[s.length+c-1]);for(d=Math.min(d/o|0,67108863),r._ishlnsubmul(s,d,c);0!==r.negative;)d--,r.negative=0,r._ishlnsubmul(s,1,c),r.isZero()||(r.negative^=1);a&&(a.words[c]=d)}return a&&a.strip(),r.strip(),"div"!==e&&0!==i&&r.iushrn(i),{div:a||null,mod:r}},n.prototype.divmod=function(t,e,r){return i(!t.isZero()),this.isZero()?{div:new n(0),mod:new n(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(s=a.div.neg()),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.iadd(t)),{div:s,mod:o}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(s=a.div.neg()),{div:s,mod:a.mod}):0!=(this.negative&t.negative)?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.isub(t)),{div:a.div,mod:o}):t.length>this.length||this.cmp(t)<0?{div:new n(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new n(this.modn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new n(this.modn(t.words[0]))}:this._wordDiv(t,e);var s,o,a},n.prototype.div=function(t){return this.divmod(t,"div",!1).div},n.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},n.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},n.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var i=0!==e.div.negative?e.mod.isub(t):e.mod,r=t.ushrn(1),n=t.andln(1),s=i.cmp(r);return s<0||1===n&&0===s?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},n.prototype.modn=function(t){i(t<=67108863);for(var e=(1<<26)%t,r=0,n=this.length-1;n>=0;n--)r=(e*r+(0|this.words[n]))%t;return r},n.prototype.idivn=function(t){i(t<=67108863);for(var e=0,r=this.length-1;r>=0;r--){var n=(0|this.words[r])+67108864*e;this.words[r]=n/t|0,e=n%t}return this.strip()},n.prototype.divn=function(t){return this.clone().idivn(t)},n.prototype.egcd=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s=new n(1),o=new n(0),a=new n(0),h=new n(1),u=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++u;for(var l=r.clone(),c=e.clone();!e.isZero();){for(var d=0,f=1;0==(e.words[0]&f)&&d<26;++d,f<<=1);if(d>0)for(e.iushrn(d);d-- >0;)(s.isOdd()||o.isOdd())&&(s.iadd(l),o.isub(c)),s.iushrn(1),o.iushrn(1);for(var m=0,g=1;0==(r.words[0]&g)&&m<26;++m,g<<=1);if(m>0)for(r.iushrn(m);m-- >0;)(a.isOdd()||h.isOdd())&&(a.iadd(l),h.isub(c)),a.iushrn(1),h.iushrn(1);e.cmp(r)>=0?(e.isub(r),s.isub(a),o.isub(h)):(r.isub(e),a.isub(s),h.isub(o))}return{a:a,b:h,gcd:r.iushln(u)}},n.prototype._invmp=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s,o=new n(1),a=new n(0),h=r.clone();e.cmpn(1)>0&&r.cmpn(1)>0;){for(var u=0,l=1;0==(e.words[0]&l)&&u<26;++u,l<<=1);if(u>0)for(e.iushrn(u);u-- >0;)o.isOdd()&&o.iadd(h),o.iushrn(1);for(var c=0,d=1;0==(r.words[0]&d)&&c<26;++c,d<<=1);if(c>0)for(r.iushrn(c);c-- >0;)a.isOdd()&&a.iadd(h),a.iushrn(1);e.cmp(r)>=0?(e.isub(r),o.isub(a)):(r.isub(e),a.isub(o))}return(s=0===e.cmpn(1)?o:a).cmpn(0)<0&&s.iadd(t),s},n.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),i=t.clone();e.negative=0,i.negative=0;for(var r=0;e.isEven()&&i.isEven();r++)e.iushrn(1),i.iushrn(1);for(;;){for(;e.isEven();)e.iushrn(1);for(;i.isEven();)i.iushrn(1);var n=e.cmp(i);if(n<0){var s=e;e=i,i=s}else if(0===n||0===i.cmpn(1))break;e.isub(i)}return i.iushln(r)},n.prototype.invm=function(t){return this.egcd(t).a.umod(t)},n.prototype.isEven=function(){return 0==(1&this.words[0])},n.prototype.isOdd=function(){return 1==(1&this.words[0])},n.prototype.andln=function(t){return this.words[0]&t},n.prototype.bincn=function(t){i("number"==typeof t);var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return this._expand(r+1),this.words[r]|=n,this;for(var s=n,o=r;0!==s&&o<this.length;o++){var a=0|this.words[o];s=(a+=s)>>>26,this.words[o]=a&=67108863}return 0!==s&&(this.words[o]=s,this.length++),this},n.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},n.prototype.cmpn=function(t){var e,r=t<0;if(0!==this.negative&&!r)return-1;if(0===this.negative&&r)return 1;if(this.strip(),this.length>1)e=1;else{r&&(t=-t),i(t<=67108863,"Number is too big");var n=0|this.words[0];e=n===t?0:n<t?-1:1}return 0!==this.negative?0|-e:e},n.prototype.cmp=function(t){if(0!==this.negative&&0===t.negative)return-1;if(0===this.negative&&0!==t.negative)return 1;var e=this.ucmp(t);return 0!==this.negative?0|-e:e},n.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,i=this.length-1;i>=0;i--){var r=0|this.words[i],n=0|t.words[i];if(r!==n){r<n?e=-1:r>n&&(e=1);break}}return e},n.prototype.gtn=function(t){return 1===this.cmpn(t)},n.prototype.gt=function(t){return 1===this.cmp(t)},n.prototype.gten=function(t){return this.cmpn(t)>=0},n.prototype.gte=function(t){return this.cmp(t)>=0},n.prototype.ltn=function(t){return-1===this.cmpn(t)},n.prototype.lt=function(t){return-1===this.cmp(t)},n.prototype.lten=function(t){return this.cmpn(t)<=0},n.prototype.lte=function(t){return this.cmp(t)<=0},n.prototype.eqn=function(t){return 0===this.cmpn(t)},n.prototype.eq=function(t){return 0===this.cmp(t)},n.red=function(t){return new b(t)},n.prototype.toRed=function(t){return i(!this.red,"Already a number in reduction context"),i(0===this.negative,"red works only with positives"),t.convertTo(this)._forceRed(t)},n.prototype.fromRed=function(){return i(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(t){return this.red=t,this},n.prototype.forceRed=function(t){return i(!this.red,"Already a number in reduction context"),this._forceRed(t)},n.prototype.redAdd=function(t){return i(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},n.prototype.redIAdd=function(t){return i(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},n.prototype.redSub=function(t){return i(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},n.prototype.redISub=function(t){return i(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},n.prototype.redShl=function(t){return i(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},n.prototype.redMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},n.prototype.redIMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},n.prototype.redSqr=function(){return i(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return i(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return i(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return i(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return i(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(t){return i(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var g={k256:null,p224:null,p192:null,p25519:null};function p(t,e){this.name=t,this.p=new n(e,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function w(){p.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function v(){p.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function y(){p.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function M(){p.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function b(t){if("string"==typeof t){var e=n._prime(t);this.m=e.p,this.prime=e}else i(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}function A(t){b.call(this,t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}p.prototype._tmp=function(){var t=new n(null);return t.words=new Array(Math.ceil(this.n/13)),t},p.prototype.ireduce=function(t){var e,i=t;do{this.split(i,this.tmp),e=(i=(i=this.imulK(i)).iadd(this.tmp)).bitLength()}while(e>this.n);var r=e<this.n?-1:i.ucmp(this.p);return 0===r?(i.words[0]=0,i.length=1):r>0?i.isub(this.p):i.strip(),i},p.prototype.split=function(t,e){t.iushrn(this.n,0,e)},p.prototype.imulK=function(t){return t.imul(this.k)},r(w,p),w.prototype.split=function(t,e){for(var i=4194303,r=Math.min(t.length,9),n=0;n<r;n++)e.words[n]=t.words[n];if(e.length=r,t.length<=9)return t.words[0]=0,void(t.length=1);var s=t.words[9];for(e.words[e.length++]=s&i,n=10;n<t.length;n++){var o=0|t.words[n];t.words[n-10]=(o&i)<<4|s>>>22,s=o}t.words[n-10]=s>>>=22,t.length-=0===s&&t.length>10?10:9},w.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,i=0;i<t.length;i++){var r=0|t.words[i];t.words[i]=67108863&(e+=977*r),e=64*r+(e/67108864|0)}return 0===t.words[t.length-1]&&(t.length--,0===t.words[t.length-1]&&t.length--),t},r(v,p),r(y,p),r(M,p),M.prototype.imulK=function(t){for(var e=0,i=0;i<t.length;i++){var r=19*(0|t.words[i])+e,n=67108863&r;r>>>=26,t.words[i]=n,e=r}return 0!==e&&(t.words[t.length++]=e),t},n._prime=function(t){if(g[t])return g[t];var e;if("k256"===t)e=new w;else if("p224"===t)e=new v;else if("p192"===t)e=new y;else{if("p25519"!==t)throw new Error("Unknown prime "+t);e=new M}return g[t]=e,e},b.prototype._verify1=function(t){i(0===t.negative,"red works only with positives"),i(t.red,"red works only with red numbers")},b.prototype._verify2=function(t,e){i(0==(t.negative|e.negative),"red works only with positives"),i(t.red&&t.red===e.red,"red works only with red numbers")},b.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):t.umod(this.m)._forceRed(this)},b.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},b.prototype.add=function(t,e){this._verify2(t,e);var i=t.add(e);return i.cmp(this.m)>=0&&i.isub(this.m),i._forceRed(this)},b.prototype.iadd=function(t,e){this._verify2(t,e);var i=t.iadd(e);return i.cmp(this.m)>=0&&i.isub(this.m),i},b.prototype.sub=function(t,e){this._verify2(t,e);var i=t.sub(e);return i.cmpn(0)<0&&i.iadd(this.m),i._forceRed(this)},b.prototype.isub=function(t,e){this._verify2(t,e);var i=t.isub(e);return i.cmpn(0)<0&&i.iadd(this.m),i},b.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},b.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},b.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},b.prototype.isqr=function(t){return this.imul(t,t.clone())},b.prototype.sqr=function(t){return this.mul(t,t)},b.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(i(e%2==1),3===e){var r=this.m.add(new n(1)).iushrn(2);return this.pow(t,r)}for(var s=this.m.subn(1),o=0;!s.isZero()&&0===s.andln(1);)o++,s.iushrn(1);i(!s.isZero());var a=new n(1).toRed(this),h=a.redNeg(),u=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new n(2*l*l).toRed(this);0!==this.pow(l,u).cmp(h);)l.redIAdd(h);for(var c=this.pow(l,s),d=this.pow(t,s.addn(1).iushrn(1)),f=this.pow(t,s),m=o;0!==f.cmp(a);){for(var g=f,p=0;0!==g.cmp(a);p++)g=g.redSqr();i(p<m);var w=this.pow(c,new n(1).iushln(m-p-1));d=d.redMul(w),c=w.redSqr(),f=f.redMul(c),m=p}return d},b.prototype.invm=function(t){var e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},b.prototype.pow=function(t,e){if(e.isZero())return new n(1);if(0===e.cmpn(1))return t.clone();var i=new Array(16);i[0]=new n(1).toRed(this),i[1]=t;for(var r=2;r<i.length;r++)i[r]=this.mul(i[r-1],t);var s=i[0],o=0,a=0,h=e.bitLength()%26;for(0===h&&(h=26),r=e.length-1;r>=0;r--){for(var u=e.words[r],l=h-1;l>=0;l--){var c=u>>l&1;s!==i[0]&&(s=this.sqr(s)),0!==c||0!==o?(o<<=1,o|=c,(4==++a||0===r&&0===l)&&(s=this.mul(s,i[o]),a=0,o=0)):a=0}h=26}return s},b.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},b.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},n.mont=function(t){return new A(t)},r(A,b),A.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},A.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},A.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var i=t.imul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),n=i.isub(r).iushrn(this.shift),s=n;return n.cmp(this.m)>=0?s=n.isub(this.m):n.cmpn(0)<0&&(s=n.iadd(this.m)),s._forceRed(this)},A.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new n(0)._forceRed(this);var i=t.mul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(r).iushrn(this.shift),o=s;return s.cmp(this.m)>=0?o=s.isub(this.m):s.cmpn(0)<0&&(o=s.iadd(this.m)),o._forceRed(this)},A.prototype.invm=function(t){return this.imod(t._invmp(this.m).mul(this.r2))._forceRed(this)}}(t,gt)}),Mt=function(t){return"string"!=typeof t?t:function(t){if("string"!=typeof t)throw new Error("[is-hex-prefixed] value must be type 'string', is currently type "+typeof t+", while checking isHexPrefixed.");return"0x"===t.slice(0,2)}(t)?t.slice(2):t},bt=function(t){if("string"==typeof t||"number"==typeof t){var e=new yt(1),i=String(t).toLowerCase().trim(),r="0x"===i.substr(0,2)||"-0x"===i.substr(0,3),n=Mt(i);if("-"===n.substr(0,1)&&(n=Mt(n.slice(1)),e=new yt(-1,10)),!(n=""===n?"0":n).match(/^-?[0-9]+$/)&&n.match(/^[0-9A-Fa-f]+$/)||n.match(/^[a-fA-F]+$/)||!0===r&&n.match(/^[0-9A-Fa-f]+$/))return new yt(n,16).mul(e);if((n.match(/^-?[0-9]+$/)||""===n)&&!1===r)return new yt(n,10).mul(e)}else if("object"==typeof t&&t.toString&&!t.pop&&!t.push&&t.toString(10).match(/^-?[0-9]+$/)&&(t.mul||t.dividedToIntegerBy))return new yt(t.toString(10),10);throw new Error("[number-to-bn] while converting number "+JSON.stringify(t)+" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.")},At=new vt(0),xt=new vt(-1),Et={noether:"0",wei:"1",kwei:"1000",Kwei:"1000",babbage:"1000",femtoether:"1000",mwei:"1000000",Mwei:"1000000",lovelace:"1000000",picoether:"1000000",gwei:"1000000000",Gwei:"1000000000",shannon:"1000000000",nanoether:"1000000000",nano:"1000000000",szabo:"1000000000000",microether:"1000000000000",micro:"1000000000000",finney:"1000000000000000",milliether:"1000000000000000",milli:"1000000000000000",ether:"1000000000000000000",kether:"1000000000000000000000",grand:"1000000000000000000000",mether:"1000000000000000000000000",gether:"1000000000000000000000000000",tether:"1000000000000000000000000000000"};function Rt(t){var e=t?t.toLowerCase():"ether",i=Et[e];if("string"!=typeof i)throw new Error("[ethjs-unit] the unit provided "+t+" doesn't exists, please use the one of the following units "+JSON.stringify(Et,null,2));return new vt(i,10)}function kt(t){if("string"==typeof t){if(!t.match(/^-?[0-9.]+$/))throw new Error("while converting number to string, invalid number value '"+t+"', should be a number matching (^-?[0-9.]+).");return t}if("number"==typeof t)return String(t);if("object"==typeof t&&t.toString&&(t.toTwos||t.dividedToIntegerBy))return t.toPrecision?String(t.toPrecision()):t.toString(10);throw new Error("while converting number to string, invalid number value '"+t+"' type "+typeof t+".")}var St={unitMap:Et,numberToString:kt,getValueOfUnit:Rt,fromWei:function(t,e,i){var r=bt(t),n=r.lt(At),s=Rt(e),o=Et[e].length-1||1,a=i||{};n&&(r=r.mul(xt));for(var h=r.mod(s).toString(10);h.length<o;)h="0"+h;a.pad||(h=h.match(/^([0-9]*[1-9]|0)(0*)/)[1]);var u=r.div(s).toString(10);a.commify&&(u=u.replace(/\B(?=(\d{3})+(?!\d))/g,","));var l=u+("0"==h?"":"."+h);return n&&(l="-"+l),l},toWei:function(t,e){var i=kt(t),r=Rt(e),n=Et[e].length-1||1,s="-"===i.substring(0,1);if(s&&(i=i.substring(1)),"."===i)throw new Error("[ethjs-unit] while converting number "+t+" to wei, invalid value");var o=i.split(".");if(o.length>2)throw new Error("[ethjs-unit] while converting number "+t+" to wei, too many decimal points");var a=o[0],h=o[1];if(a||(a="0"),h||(h="0"),h.length>n)throw new Error("[ethjs-unit] while converting number "+t+" to wei, too many decimal places");for(;h.length<n;)h+="0";a=new vt(a),h=new vt(h);var u=a.mul(r).add(h);return s&&(u=u.mul(xt)),new vt(u.toString(10),10)}},It=pt(function(t){!function(t,e){function i(t,e){if(!t)throw new Error(e||"Assertion failed")}function r(t,e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}function n(t,e,i){if(n.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(i=e,e=10),this._init(t||0,e||10,i||"be"))}var s;"object"==typeof t?t.exports=n:e.BN=n,n.BN=n,n.wordSize=26;try{s=o.Buffer}catch(t){}function a(t,e,i){for(var r=0,n=Math.min(t.length,i),s=e;s<n;s++){var o=t.charCodeAt(s)-48;r<<=4,r|=o>=49&&o<=54?o-49+10:o>=17&&o<=22?o-17+10:15&o}return r}function h(t,e,i,r){for(var n=0,s=Math.min(t.length,i),o=e;o<s;o++){var a=t.charCodeAt(o)-48;n*=r,n+=a>=49?a-49+10:a>=17?a-17+10:a}return n}n.isBN=function(t){return t instanceof n||null!==t&&"object"==typeof t&&t.constructor.wordSize===n.wordSize&&Array.isArray(t.words)},n.max=function(t,e){return t.cmp(e)>0?t:e},n.min=function(t,e){return t.cmp(e)<0?t:e},n.prototype._init=function(t,e,r){if("number"==typeof t)return this._initNumber(t,e,r);if("object"==typeof t)return this._initArray(t,e,r);"hex"===e&&(e=16),i(e===(0|e)&&e>=2&&e<=36);var n=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&n++,16===e?this._parseHex(t,n):this._parseBase(t,e,n),"-"===t[0]&&(this.negative=1),this.strip(),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(i(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),e,r)},n.prototype._initArray=function(t,e,r){if(i("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,o,a=0;if("be"===r)for(n=t.length-1,s=0;n>=0;n-=3)this.words[s]|=(o=t[n]|t[n-1]<<8|t[n-2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);else if("le"===r)for(n=0,s=0;n<t.length;n+=3)this.words[s]|=(o=t[n]|t[n+1]<<8|t[n+2]<<16)<<a&67108863,this.words[s+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,s++);return this.strip()},n.prototype._parseHex=function(t,e){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var r,n,s=0;for(i=t.length-6,r=0;i>=e;i-=6)n=a(t,i,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303,(s+=24)>=26&&(s-=26,r++);i+6!==e&&(n=a(t,e,i+6),this.words[r]|=n<<s&67108863,this.words[r+1]|=n>>>26-s&4194303),this.strip()},n.prototype._parseBase=function(t,e,i){this.words=[0],this.length=1;for(var r=0,n=1;n<=67108863;n*=e)r++;r--,n=n/e|0;for(var s=t.length-i,o=s%r,a=Math.min(s,s-o)+i,u=0,l=i;l<a;l+=r)u=h(t,l,l+r,e),this.imuln(n),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(0!==o){var c=1;for(u=h(t,l,t.length,e),l=0;l<o;l++)c*=e;this.imuln(c),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}},n.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},n.prototype.clone=function(){var t=new n(null);return this.copy(t),t},n.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},n.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},n.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},n.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var u=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],l=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],c=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function d(t,e,i){i.negative=e.negative^t.negative;var r=t.length+e.length|0;i.length=r,r=r-1|0;var n=0|t.words[0],s=0|e.words[0],o=n*s,a=o/67108864|0;i.words[0]=67108863&o;for(var h=1;h<r;h++){for(var u=a>>>26,l=67108863&a,c=Math.min(h,e.length-1),d=Math.max(0,h-t.length+1);d<=c;d++)u+=(o=(n=0|t.words[h-d|0])*(s=0|e.words[d])+l)/67108864|0,l=67108863&o;i.words[h]=0|l,a=0|u}return 0!==a?i.words[h]=0|a:i.length--,i.strip()}n.prototype.toString=function(t,e){var r;if(e=0|e||1,16===(t=t||10)||"hex"===t){r="";for(var n=0,s=0,o=0;o<this.length;o++){var a=this.words[o],h=(16777215&(a<<n|s)).toString(16);r=0!=(s=a>>>24-n&16777215)||o!==this.length-1?u[6-h.length]+h+r:h+r,(n+=2)>=26&&(n-=26,o--)}for(0!==s&&(r=s.toString(16)+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}if(t===(0|t)&&t>=2&&t<=36){var d=l[t],f=c[t];r="";var m=this.clone();for(m.negative=0;!m.isZero();){var g=m.modn(f).toString(t);r=(m=m.idivn(f)).isZero()?g+r:u[d-g.length]+g+r}for(this.isZero()&&(r="0"+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}i(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&i(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},n.prototype.toJSON=function(){return this.toString(16)},n.prototype.toBuffer=function(t,e){return i(void 0!==s),this.toArrayLike(s,t,e)},n.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},n.prototype.toArrayLike=function(t,e,r){var n=this.byteLength(),s=r||Math.max(1,n);i(n<=s,"byte array longer than desired length"),i(s>0,"Requested array length <= 0"),this.strip();var o,a,h="le"===e,u=new t(s),l=this.clone();if(h){for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[a]=o;for(;a<s;a++)u[a]=0}else{for(a=0;a<s-n;a++)u[a]=0;for(a=0;!l.isZero();a++)o=l.andln(255),l.iushrn(8),u[s-a-1]=o}return u},n.prototype._countBits=Math.clz32?function(t){return 32-Math.clz32(t)}:function(t){var e=t,i=0;return e>=4096&&(i+=13,e>>>=13),e>=64&&(i+=7,e>>>=7),e>=8&&(i+=4,e>>>=4),e>=2&&(i+=2,e>>>=2),i+e},n.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,i=0;return 0==(8191&e)&&(i+=13,e>>>=13),0==(127&e)&&(i+=7,e>>>=7),0==(15&e)&&(i+=4,e>>>=4),0==(3&e)&&(i+=2,e>>>=2),0==(1&e)&&i++,i},n.prototype.bitLength=function(){var t=this._countBits(this.words[this.length-1]);return 26*(this.length-1)+t},n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var i=this._zeroBits(this.words[e]);if(t+=i,26!==i)break}return t},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},n.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return 0!==this.negative},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this.strip()},n.prototype.ior=function(t){return i(0==(this.negative|t.negative)),this.iuor(t)},n.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},n.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},n.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var i=0;i<e.length;i++)this.words[i]=this.words[i]&t.words[i];return this.length=e.length,this.strip()},n.prototype.iand=function(t){return i(0==(this.negative|t.negative)),this.iuand(t)},n.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},n.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},n.prototype.iuxor=function(t){var e,i;this.length>t.length?(e=this,i=t):(e=t,i=this);for(var r=0;r<i.length;r++)this.words[r]=e.words[r]^i.words[r];if(this!==e)for(;r<e.length;r++)this.words[r]=e.words[r];return this.length=e.length,this.strip()},n.prototype.ixor=function(t){return i(0==(this.negative|t.negative)),this.iuxor(t)},n.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},n.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},n.prototype.inotn=function(t){i("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=67108863&~this.words[n];return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this.strip()},n.prototype.notn=function(t){return this.clone().inotn(t)},n.prototype.setn=function(t,e){i("number"==typeof t&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),this.words[r]=e?this.words[r]|1<<n:this.words[r]&~(1<<n),this.strip()},n.prototype.iadd=function(t){var e,i,r;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(i=this,r=t):(i=t,r=this);for(var n=0,s=0;s<r.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+(0|r.words[s])+n),n=e>>>26;for(;0!==n&&s<i.length;s++)this.words[s]=67108863&(e=(0|i.words[s])+n),n=e>>>26;if(this.length=i.length,0!==n)this.words[this.length]=n,this.length++;else if(i!==this)for(;s<i.length;s++)this.words[s]=i.words[s];return this},n.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},n.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var i,r,n=this.cmp(t);if(0===n)return this.negative=0,this.length=1,this.words[0]=0,this;n>0?(i=this,r=t):(i=t,r=this);for(var s=0,o=0;o<r.length;o++)s=(e=(0|i.words[o])-(0|r.words[o])+s)>>26,this.words[o]=67108863&e;for(;0!==s&&o<i.length;o++)s=(e=(0|i.words[o])+s)>>26,this.words[o]=67108863&e;if(0===s&&o<i.length&&i!==this)for(;o<i.length;o++)this.words[o]=i.words[o];return this.length=Math.max(this.length,o),i!==this&&(this.negative=1),this.strip()},n.prototype.sub=function(t){return this.clone().isub(t)};var f=function(t,e,i){var r,n,s,o=t.words,a=e.words,h=i.words,u=0,l=0|o[0],c=8191&l,d=l>>>13,f=0|o[1],m=8191&f,g=f>>>13,p=0|o[2],w=8191&p,v=p>>>13,y=0|o[3],M=8191&y,b=y>>>13,A=0|o[4],x=8191&A,E=A>>>13,R=0|o[5],k=8191&R,S=R>>>13,I=0|o[6],T=8191&I,O=I>>>13,B=0|o[7],C=8191&B,_=B>>>13,D=0|o[8],P=8191&D,N=D>>>13,L=0|o[9],W=8191&L,F=L>>>13,U=0|a[0],$=8191&U,j=U>>>13,q=0|a[1],G=8191&q,z=q>>>13,H=0|a[2],Z=8191&H,V=H>>>13,J=0|a[3],K=8191&J,X=J>>>13,Q=0|a[4],Y=8191&Q,tt=Q>>>13,et=0|a[5],it=8191&et,rt=et>>>13,nt=0|a[6],st=8191&nt,ot=nt>>>13,at=0|a[7],ht=8191&at,ut=at>>>13,lt=0|a[8],ct=8191&lt,dt=lt>>>13,ft=0|a[9],mt=8191&ft,gt=ft>>>13;i.negative=t.negative^e.negative,i.length=19;var pt=(u+(r=Math.imul(c,$))|0)+((8191&(n=(n=Math.imul(c,j))+Math.imul(d,$)|0))<<13)|0;u=((s=Math.imul(d,j))+(n>>>13)|0)+(pt>>>26)|0,pt&=67108863,r=Math.imul(m,$),n=(n=Math.imul(m,j))+Math.imul(g,$)|0,s=Math.imul(g,j);var wt=(u+(r=r+Math.imul(c,G)|0)|0)+((8191&(n=(n=n+Math.imul(c,z)|0)+Math.imul(d,G)|0))<<13)|0;u=((s=s+Math.imul(d,z)|0)+(n>>>13)|0)+(wt>>>26)|0,wt&=67108863,r=Math.imul(w,$),n=(n=Math.imul(w,j))+Math.imul(v,$)|0,s=Math.imul(v,j),r=r+Math.imul(m,G)|0,n=(n=n+Math.imul(m,z)|0)+Math.imul(g,G)|0,s=s+Math.imul(g,z)|0;var vt=(u+(r=r+Math.imul(c,Z)|0)|0)+((8191&(n=(n=n+Math.imul(c,V)|0)+Math.imul(d,Z)|0))<<13)|0;u=((s=s+Math.imul(d,V)|0)+(n>>>13)|0)+(vt>>>26)|0,vt&=67108863,r=Math.imul(M,$),n=(n=Math.imul(M,j))+Math.imul(b,$)|0,s=Math.imul(b,j),r=r+Math.imul(w,G)|0,n=(n=n+Math.imul(w,z)|0)+Math.imul(v,G)|0,s=s+Math.imul(v,z)|0,r=r+Math.imul(m,Z)|0,n=(n=n+Math.imul(m,V)|0)+Math.imul(g,Z)|0,s=s+Math.imul(g,V)|0;var yt=(u+(r=r+Math.imul(c,K)|0)|0)+((8191&(n=(n=n+Math.imul(c,X)|0)+Math.imul(d,K)|0))<<13)|0;u=((s=s+Math.imul(d,X)|0)+(n>>>13)|0)+(yt>>>26)|0,yt&=67108863,r=Math.imul(x,$),n=(n=Math.imul(x,j))+Math.imul(E,$)|0,s=Math.imul(E,j),r=r+Math.imul(M,G)|0,n=(n=n+Math.imul(M,z)|0)+Math.imul(b,G)|0,s=s+Math.imul(b,z)|0,r=r+Math.imul(w,Z)|0,n=(n=n+Math.imul(w,V)|0)+Math.imul(v,Z)|0,s=s+Math.imul(v,V)|0,r=r+Math.imul(m,K)|0,n=(n=n+Math.imul(m,X)|0)+Math.imul(g,K)|0,s=s+Math.imul(g,X)|0;var Mt=(u+(r=r+Math.imul(c,Y)|0)|0)+((8191&(n=(n=n+Math.imul(c,tt)|0)+Math.imul(d,Y)|0))<<13)|0;u=((s=s+Math.imul(d,tt)|0)+(n>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,r=Math.imul(k,$),n=(n=Math.imul(k,j))+Math.imul(S,$)|0,s=Math.imul(S,j),r=r+Math.imul(x,G)|0,n=(n=n+Math.imul(x,z)|0)+Math.imul(E,G)|0,s=s+Math.imul(E,z)|0,r=r+Math.imul(M,Z)|0,n=(n=n+Math.imul(M,V)|0)+Math.imul(b,Z)|0,s=s+Math.imul(b,V)|0,r=r+Math.imul(w,K)|0,n=(n=n+Math.imul(w,X)|0)+Math.imul(v,K)|0,s=s+Math.imul(v,X)|0,r=r+Math.imul(m,Y)|0,n=(n=n+Math.imul(m,tt)|0)+Math.imul(g,Y)|0,s=s+Math.imul(g,tt)|0;var bt=(u+(r=r+Math.imul(c,it)|0)|0)+((8191&(n=(n=n+Math.imul(c,rt)|0)+Math.imul(d,it)|0))<<13)|0;u=((s=s+Math.imul(d,rt)|0)+(n>>>13)|0)+(bt>>>26)|0,bt&=67108863,r=Math.imul(T,$),n=(n=Math.imul(T,j))+Math.imul(O,$)|0,s=Math.imul(O,j),r=r+Math.imul(k,G)|0,n=(n=n+Math.imul(k,z)|0)+Math.imul(S,G)|0,s=s+Math.imul(S,z)|0,r=r+Math.imul(x,Z)|0,n=(n=n+Math.imul(x,V)|0)+Math.imul(E,Z)|0,s=s+Math.imul(E,V)|0,r=r+Math.imul(M,K)|0,n=(n=n+Math.imul(M,X)|0)+Math.imul(b,K)|0,s=s+Math.imul(b,X)|0,r=r+Math.imul(w,Y)|0,n=(n=n+Math.imul(w,tt)|0)+Math.imul(v,Y)|0,s=s+Math.imul(v,tt)|0,r=r+Math.imul(m,it)|0,n=(n=n+Math.imul(m,rt)|0)+Math.imul(g,it)|0,s=s+Math.imul(g,rt)|0;var At=(u+(r=r+Math.imul(c,st)|0)|0)+((8191&(n=(n=n+Math.imul(c,ot)|0)+Math.imul(d,st)|0))<<13)|0;u=((s=s+Math.imul(d,ot)|0)+(n>>>13)|0)+(At>>>26)|0,At&=67108863,r=Math.imul(C,$),n=(n=Math.imul(C,j))+Math.imul(_,$)|0,s=Math.imul(_,j),r=r+Math.imul(T,G)|0,n=(n=n+Math.imul(T,z)|0)+Math.imul(O,G)|0,s=s+Math.imul(O,z)|0,r=r+Math.imul(k,Z)|0,n=(n=n+Math.imul(k,V)|0)+Math.imul(S,Z)|0,s=s+Math.imul(S,V)|0,r=r+Math.imul(x,K)|0,n=(n=n+Math.imul(x,X)|0)+Math.imul(E,K)|0,s=s+Math.imul(E,X)|0,r=r+Math.imul(M,Y)|0,n=(n=n+Math.imul(M,tt)|0)+Math.imul(b,Y)|0,s=s+Math.imul(b,tt)|0,r=r+Math.imul(w,it)|0,n=(n=n+Math.imul(w,rt)|0)+Math.imul(v,it)|0,s=s+Math.imul(v,rt)|0,r=r+Math.imul(m,st)|0,n=(n=n+Math.imul(m,ot)|0)+Math.imul(g,st)|0,s=s+Math.imul(g,ot)|0;var xt=(u+(r=r+Math.imul(c,ht)|0)|0)+((8191&(n=(n=n+Math.imul(c,ut)|0)+Math.imul(d,ht)|0))<<13)|0;u=((s=s+Math.imul(d,ut)|0)+(n>>>13)|0)+(xt>>>26)|0,xt&=67108863,r=Math.imul(P,$),n=(n=Math.imul(P,j))+Math.imul(N,$)|0,s=Math.imul(N,j),r=r+Math.imul(C,G)|0,n=(n=n+Math.imul(C,z)|0)+Math.imul(_,G)|0,s=s+Math.imul(_,z)|0,r=r+Math.imul(T,Z)|0,n=(n=n+Math.imul(T,V)|0)+Math.imul(O,Z)|0,s=s+Math.imul(O,V)|0,r=r+Math.imul(k,K)|0,n=(n=n+Math.imul(k,X)|0)+Math.imul(S,K)|0,s=s+Math.imul(S,X)|0,r=r+Math.imul(x,Y)|0,n=(n=n+Math.imul(x,tt)|0)+Math.imul(E,Y)|0,s=s+Math.imul(E,tt)|0,r=r+Math.imul(M,it)|0,n=(n=n+Math.imul(M,rt)|0)+Math.imul(b,it)|0,s=s+Math.imul(b,rt)|0,r=r+Math.imul(w,st)|0,n=(n=n+Math.imul(w,ot)|0)+Math.imul(v,st)|0,s=s+Math.imul(v,ot)|0,r=r+Math.imul(m,ht)|0,n=(n=n+Math.imul(m,ut)|0)+Math.imul(g,ht)|0,s=s+Math.imul(g,ut)|0;var Et=(u+(r=r+Math.imul(c,ct)|0)|0)+((8191&(n=(n=n+Math.imul(c,dt)|0)+Math.imul(d,ct)|0))<<13)|0;u=((s=s+Math.imul(d,dt)|0)+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,r=Math.imul(W,$),n=(n=Math.imul(W,j))+Math.imul(F,$)|0,s=Math.imul(F,j),r=r+Math.imul(P,G)|0,n=(n=n+Math.imul(P,z)|0)+Math.imul(N,G)|0,s=s+Math.imul(N,z)|0,r=r+Math.imul(C,Z)|0,n=(n=n+Math.imul(C,V)|0)+Math.imul(_,Z)|0,s=s+Math.imul(_,V)|0,r=r+Math.imul(T,K)|0,n=(n=n+Math.imul(T,X)|0)+Math.imul(O,K)|0,s=s+Math.imul(O,X)|0,r=r+Math.imul(k,Y)|0,n=(n=n+Math.imul(k,tt)|0)+Math.imul(S,Y)|0,s=s+Math.imul(S,tt)|0,r=r+Math.imul(x,it)|0,n=(n=n+Math.imul(x,rt)|0)+Math.imul(E,it)|0,s=s+Math.imul(E,rt)|0,r=r+Math.imul(M,st)|0,n=(n=n+Math.imul(M,ot)|0)+Math.imul(b,st)|0,s=s+Math.imul(b,ot)|0,r=r+Math.imul(w,ht)|0,n=(n=n+Math.imul(w,ut)|0)+Math.imul(v,ht)|0,s=s+Math.imul(v,ut)|0,r=r+Math.imul(m,ct)|0,n=(n=n+Math.imul(m,dt)|0)+Math.imul(g,ct)|0,s=s+Math.imul(g,dt)|0;var Rt=(u+(r=r+Math.imul(c,mt)|0)|0)+((8191&(n=(n=n+Math.imul(c,gt)|0)+Math.imul(d,mt)|0))<<13)|0;u=((s=s+Math.imul(d,gt)|0)+(n>>>13)|0)+(Rt>>>26)|0,Rt&=67108863,r=Math.imul(W,G),n=(n=Math.imul(W,z))+Math.imul(F,G)|0,s=Math.imul(F,z),r=r+Math.imul(P,Z)|0,n=(n=n+Math.imul(P,V)|0)+Math.imul(N,Z)|0,s=s+Math.imul(N,V)|0,r=r+Math.imul(C,K)|0,n=(n=n+Math.imul(C,X)|0)+Math.imul(_,K)|0,s=s+Math.imul(_,X)|0,r=r+Math.imul(T,Y)|0,n=(n=n+Math.imul(T,tt)|0)+Math.imul(O,Y)|0,s=s+Math.imul(O,tt)|0,r=r+Math.imul(k,it)|0,n=(n=n+Math.imul(k,rt)|0)+Math.imul(S,it)|0,s=s+Math.imul(S,rt)|0,r=r+Math.imul(x,st)|0,n=(n=n+Math.imul(x,ot)|0)+Math.imul(E,st)|0,s=s+Math.imul(E,ot)|0,r=r+Math.imul(M,ht)|0,n=(n=n+Math.imul(M,ut)|0)+Math.imul(b,ht)|0,s=s+Math.imul(b,ut)|0,r=r+Math.imul(w,ct)|0,n=(n=n+Math.imul(w,dt)|0)+Math.imul(v,ct)|0,s=s+Math.imul(v,dt)|0;var kt=(u+(r=r+Math.imul(m,mt)|0)|0)+((8191&(n=(n=n+Math.imul(m,gt)|0)+Math.imul(g,mt)|0))<<13)|0;u=((s=s+Math.imul(g,gt)|0)+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,r=Math.imul(W,Z),n=(n=Math.imul(W,V))+Math.imul(F,Z)|0,s=Math.imul(F,V),r=r+Math.imul(P,K)|0,n=(n=n+Math.imul(P,X)|0)+Math.imul(N,K)|0,s=s+Math.imul(N,X)|0,r=r+Math.imul(C,Y)|0,n=(n=n+Math.imul(C,tt)|0)+Math.imul(_,Y)|0,s=s+Math.imul(_,tt)|0,r=r+Math.imul(T,it)|0,n=(n=n+Math.imul(T,rt)|0)+Math.imul(O,it)|0,s=s+Math.imul(O,rt)|0,r=r+Math.imul(k,st)|0,n=(n=n+Math.imul(k,ot)|0)+Math.imul(S,st)|0,s=s+Math.imul(S,ot)|0,r=r+Math.imul(x,ht)|0,n=(n=n+Math.imul(x,ut)|0)+Math.imul(E,ht)|0,s=s+Math.imul(E,ut)|0,r=r+Math.imul(M,ct)|0,n=(n=n+Math.imul(M,dt)|0)+Math.imul(b,ct)|0,s=s+Math.imul(b,dt)|0;var St=(u+(r=r+Math.imul(w,mt)|0)|0)+((8191&(n=(n=n+Math.imul(w,gt)|0)+Math.imul(v,mt)|0))<<13)|0;u=((s=s+Math.imul(v,gt)|0)+(n>>>13)|0)+(St>>>26)|0,St&=67108863,r=Math.imul(W,K),n=(n=Math.imul(W,X))+Math.imul(F,K)|0,s=Math.imul(F,X),r=r+Math.imul(P,Y)|0,n=(n=n+Math.imul(P,tt)|0)+Math.imul(N,Y)|0,s=s+Math.imul(N,tt)|0,r=r+Math.imul(C,it)|0,n=(n=n+Math.imul(C,rt)|0)+Math.imul(_,it)|0,s=s+Math.imul(_,rt)|0,r=r+Math.imul(T,st)|0,n=(n=n+Math.imul(T,ot)|0)+Math.imul(O,st)|0,s=s+Math.imul(O,ot)|0,r=r+Math.imul(k,ht)|0,n=(n=n+Math.imul(k,ut)|0)+Math.imul(S,ht)|0,s=s+Math.imul(S,ut)|0,r=r+Math.imul(x,ct)|0,n=(n=n+Math.imul(x,dt)|0)+Math.imul(E,ct)|0,s=s+Math.imul(E,dt)|0;var It=(u+(r=r+Math.imul(M,mt)|0)|0)+((8191&(n=(n=n+Math.imul(M,gt)|0)+Math.imul(b,mt)|0))<<13)|0;u=((s=s+Math.imul(b,gt)|0)+(n>>>13)|0)+(It>>>26)|0,It&=67108863,r=Math.imul(W,Y),n=(n=Math.imul(W,tt))+Math.imul(F,Y)|0,s=Math.imul(F,tt),r=r+Math.imul(P,it)|0,n=(n=n+Math.imul(P,rt)|0)+Math.imul(N,it)|0,s=s+Math.imul(N,rt)|0,r=r+Math.imul(C,st)|0,n=(n=n+Math.imul(C,ot)|0)+Math.imul(_,st)|0,s=s+Math.imul(_,ot)|0,r=r+Math.imul(T,ht)|0,n=(n=n+Math.imul(T,ut)|0)+Math.imul(O,ht)|0,s=s+Math.imul(O,ut)|0,r=r+Math.imul(k,ct)|0,n=(n=n+Math.imul(k,dt)|0)+Math.imul(S,ct)|0,s=s+Math.imul(S,dt)|0;var Tt=(u+(r=r+Math.imul(x,mt)|0)|0)+((8191&(n=(n=n+Math.imul(x,gt)|0)+Math.imul(E,mt)|0))<<13)|0;u=((s=s+Math.imul(E,gt)|0)+(n>>>13)|0)+(Tt>>>26)|0,Tt&=67108863,r=Math.imul(W,it),n=(n=Math.imul(W,rt))+Math.imul(F,it)|0,s=Math.imul(F,rt),r=r+Math.imul(P,st)|0,n=(n=n+Math.imul(P,ot)|0)+Math.imul(N,st)|0,s=s+Math.imul(N,ot)|0,r=r+Math.imul(C,ht)|0,n=(n=n+Math.imul(C,ut)|0)+Math.imul(_,ht)|0,s=s+Math.imul(_,ut)|0,r=r+Math.imul(T,ct)|0,n=(n=n+Math.imul(T,dt)|0)+Math.imul(O,ct)|0,s=s+Math.imul(O,dt)|0;var Ot=(u+(r=r+Math.imul(k,mt)|0)|0)+((8191&(n=(n=n+Math.imul(k,gt)|0)+Math.imul(S,mt)|0))<<13)|0;u=((s=s+Math.imul(S,gt)|0)+(n>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,r=Math.imul(W,st),n=(n=Math.imul(W,ot))+Math.imul(F,st)|0,s=Math.imul(F,ot),r=r+Math.imul(P,ht)|0,n=(n=n+Math.imul(P,ut)|0)+Math.imul(N,ht)|0,s=s+Math.imul(N,ut)|0,r=r+Math.imul(C,ct)|0,n=(n=n+Math.imul(C,dt)|0)+Math.imul(_,ct)|0,s=s+Math.imul(_,dt)|0;var Bt=(u+(r=r+Math.imul(T,mt)|0)|0)+((8191&(n=(n=n+Math.imul(T,gt)|0)+Math.imul(O,mt)|0))<<13)|0;u=((s=s+Math.imul(O,gt)|0)+(n>>>13)|0)+(Bt>>>26)|0,Bt&=67108863,r=Math.imul(W,ht),n=(n=Math.imul(W,ut))+Math.imul(F,ht)|0,s=Math.imul(F,ut),r=r+Math.imul(P,ct)|0,n=(n=n+Math.imul(P,dt)|0)+Math.imul(N,ct)|0,s=s+Math.imul(N,dt)|0;var Ct=(u+(r=r+Math.imul(C,mt)|0)|0)+((8191&(n=(n=n+Math.imul(C,gt)|0)+Math.imul(_,mt)|0))<<13)|0;u=((s=s+Math.imul(_,gt)|0)+(n>>>13)|0)+(Ct>>>26)|0,Ct&=67108863,r=Math.imul(W,ct),n=(n=Math.imul(W,dt))+Math.imul(F,ct)|0,s=Math.imul(F,dt);var _t=(u+(r=r+Math.imul(P,mt)|0)|0)+((8191&(n=(n=n+Math.imul(P,gt)|0)+Math.imul(N,mt)|0))<<13)|0;u=((s=s+Math.imul(N,gt)|0)+(n>>>13)|0)+(_t>>>26)|0,_t&=67108863;var Dt=(u+(r=Math.imul(W,mt))|0)+((8191&(n=(n=Math.imul(W,gt))+Math.imul(F,mt)|0))<<13)|0;return u=((s=Math.imul(F,gt))+(n>>>13)|0)+(Dt>>>26)|0,Dt&=67108863,h[0]=pt,h[1]=wt,h[2]=vt,h[3]=yt,h[4]=Mt,h[5]=bt,h[6]=At,h[7]=xt,h[8]=Et,h[9]=Rt,h[10]=kt,h[11]=St,h[12]=It,h[13]=Tt,h[14]=Ot,h[15]=Bt,h[16]=Ct,h[17]=_t,h[18]=Dt,0!==u&&(h[19]=u,i.length++),i};function m(t,e,i){return(new g).mulp(t,e,i)}function g(t,e){this.x=t,this.y=e}Math.imul||(f=d),n.prototype.mulTo=function(t,e){var i=this.length+t.length;return 10===this.length&&10===t.length?f(this,t,e):i<63?d(this,t,e):i<1024?function(t,e,i){i.negative=e.negative^t.negative,i.length=t.length+e.length;for(var r=0,n=0,s=0;s<i.length-1;s++){var o=n;n=0;for(var a=67108863&r,h=Math.min(s,e.length-1),u=Math.max(0,s-t.length+1);u<=h;u++){var l=(0|t.words[s-u])*(0|e.words[u]),c=67108863&l;a=67108863&(c=c+a|0),n+=(o=(o=o+(l/67108864|0)|0)+(c>>>26)|0)>>>26,o&=67108863}i.words[s]=a,r=o,o=n}return 0!==r?i.words[s]=r:i.length--,i.strip()}(this,t,e):m(this,t,e)},g.prototype.makeRBT=function(t){for(var e=new Array(t),i=n.prototype._countBits(t)-1,r=0;r<t;r++)e[r]=this.revBin(r,i,t);return e},g.prototype.revBin=function(t,e,i){if(0===t||t===i-1)return t;for(var r=0,n=0;n<e;n++)r|=(1&t)<<e-n-1,t>>=1;return r},g.prototype.permute=function(t,e,i,r,n,s){for(var o=0;o<s;o++)r[o]=e[t[o]],n[o]=i[t[o]]},g.prototype.transform=function(t,e,i,r,n,s){this.permute(s,t,e,i,r,n);for(var o=1;o<n;o<<=1)for(var a=o<<1,h=Math.cos(2*Math.PI/a),u=Math.sin(2*Math.PI/a),l=0;l<n;l+=a)for(var c=h,d=u,f=0;f<o;f++){var m=i[l+f],g=r[l+f],p=i[l+f+o],w=r[l+f+o],v=c*p-d*w;w=c*w+d*p,i[l+f]=m+(p=v),r[l+f]=g+w,i[l+f+o]=m-p,r[l+f+o]=g-w,f!==a&&(v=h*c-u*d,d=h*d+u*c,c=v)}},g.prototype.guessLen13b=function(t,e){var i=1|Math.max(e,t),r=1&i,n=0;for(i=i/2|0;i;i>>>=1)n++;return 1<<n+1+r},g.prototype.conjugate=function(t,e,i){if(!(i<=1))for(var r=0;r<i/2;r++){var n=t[r];t[r]=t[i-r-1],t[i-r-1]=n,n=e[r],e[r]=-e[i-r-1],e[i-r-1]=-n}},g.prototype.normalize13b=function(t,e){for(var i=0,r=0;r<e/2;r++){var n=8192*Math.round(t[2*r+1]/e)+Math.round(t[2*r]/e)+i;t[r]=67108863&n,i=n<67108864?0:n/67108864|0}return t},g.prototype.convert13b=function(t,e,r,n){for(var s=0,o=0;o<e;o++)r[2*o]=8191&(s+=0|t[o]),r[2*o+1]=8191&(s>>>=13),s>>>=13;for(o=2*e;o<n;++o)r[o]=0;i(0===s),i(0==(-8192&s))},g.prototype.stub=function(t){for(var e=new Array(t),i=0;i<t;i++)e[i]=0;return e},g.prototype.mulp=function(t,e,i){var r=2*this.guessLen13b(t.length,e.length),n=this.makeRBT(r),s=this.stub(r),o=new Array(r),a=new Array(r),h=new Array(r),u=new Array(r),l=new Array(r),c=new Array(r),d=i.words;d.length=r,this.convert13b(t.words,t.length,o,r),this.convert13b(e.words,e.length,u,r),this.transform(o,s,a,h,r,n),this.transform(u,s,l,c,r,n);for(var f=0;f<r;f++){var m=a[f]*l[f]-h[f]*c[f];h[f]=a[f]*c[f]+h[f]*l[f],a[f]=m}return this.conjugate(a,h,r),this.transform(a,h,d,s,r,n),this.conjugate(d,s,r),this.normalize13b(d,r),i.negative=t.negative^e.negative,i.length=t.length+e.length,i.strip()},n.prototype.mul=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},n.prototype.mulf=function(t){var e=new n(null);return e.words=new Array(this.length+t.length),m(this,t,e)},n.prototype.imul=function(t){return this.clone().mulTo(t,this)},n.prototype.imuln=function(t){i("number"==typeof t),i(t<67108864);for(var e=0,r=0;r<this.length;r++){var n=(0|this.words[r])*t,s=(67108863&n)+(67108863&e);e>>=26,e+=n/67108864|0,e+=s>>>26,this.words[r]=67108863&s}return 0!==e&&(this.words[r]=e,this.length++),this},n.prototype.muln=function(t){return this.clone().imuln(t)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),i=0;i<e.length;i++){var r=i%26;e[i]=(t.words[i/26|0]&1<<r)>>>r}return e}(t);if(0===e.length)return new n(1);for(var i=this,r=0;r<e.length&&0===e[r];r++,i=i.sqr());if(++r<e.length)for(var s=i.sqr();r<e.length;r++,s=s.sqr())0!==e[r]&&(i=i.mul(s));return i},n.prototype.iushln=function(t){i("number"==typeof t&&t>=0);var e,r=t%26,n=(t-r)/26,s=67108863>>>26-r<<26-r;if(0!==r){var o=0;for(e=0;e<this.length;e++){var a=this.words[e]&s;this.words[e]=(0|this.words[e])-a<<r|o,o=a>>>26-r}o&&(this.words[e]=o,this.length++)}if(0!==n){for(e=this.length-1;e>=0;e--)this.words[e+n]=this.words[e];for(e=0;e<n;e++)this.words[e]=0;this.length+=n}return this.strip()},n.prototype.ishln=function(t){return i(0===this.negative),this.iushln(t)},n.prototype.iushrn=function(t,e,r){var n;i("number"==typeof t&&t>=0),n=e?(e-e%26)/26:0;var s=t%26,o=Math.min((t-s)/26,this.length),a=67108863^67108863>>>s<<s,h=r;if(n-=o,n=Math.max(0,n),h){for(var u=0;u<o;u++)h.words[u]=this.words[u];h.length=o}if(0===o);else if(this.length>o)for(this.length-=o,u=0;u<this.length;u++)this.words[u]=this.words[u+o];else this.words[0]=0,this.length=1;var l=0;for(u=this.length-1;u>=0&&(0!==l||u>=n);u--){var c=0|this.words[u];this.words[u]=l<<26-s|c>>>s,l=c&a}return h&&0!==l&&(h.words[h.length++]=l),0===this.length&&(this.words[0]=0,this.length=1),this.strip()},n.prototype.ishrn=function(t,e,r){return i(0===this.negative),this.iushrn(t,e,r)},n.prototype.shln=function(t){return this.clone().ishln(t)},n.prototype.ushln=function(t){return this.clone().iushln(t)},n.prototype.shrn=function(t){return this.clone().ishrn(t)},n.prototype.ushrn=function(t){return this.clone().iushrn(t)},n.prototype.testn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return!(this.length<=r||!(this.words[r]&1<<e))},n.prototype.imaskn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;return i(0===this.negative,"imaskn works only with positive numbers"),this.length<=r?this:(0!==e&&r++,this.length=Math.min(r,this.length),0!==e&&(this.words[this.length-1]&=67108863^67108863>>>e<<e),this.strip())},n.prototype.maskn=function(t){return this.clone().imaskn(t)},n.prototype.iaddn=function(t){return i("number"==typeof t),i(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},n.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},n.prototype.isubn=function(t){if(i("number"==typeof t),i(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this.strip()},n.prototype.addn=function(t){return this.clone().iaddn(t)},n.prototype.subn=function(t){return this.clone().isubn(t)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(t,e,r){var n,s;this._expand(t.length+r);var o=0;for(n=0;n<t.length;n++){s=(0|this.words[n+r])+o;var a=(0|t.words[n])*e;o=((s-=67108863&a)>>26)-(a/67108864|0),this.words[n+r]=67108863&s}for(;n<this.length-r;n++)o=(s=(0|this.words[n+r])+o)>>26,this.words[n+r]=67108863&s;if(0===o)return this.strip();for(i(-1===o),o=0,n=0;n<this.length;n++)o=(s=-(0|this.words[n])+o)>>26,this.words[n]=67108863&s;return this.negative=1,this.strip()},n.prototype._wordDiv=function(t,e){var i,r=this.clone(),s=t,o=0|s.words[s.length-1];0!=(i=26-this._countBits(o))&&(s=s.ushln(i),r.iushln(i),o=0|s.words[s.length-1]);var a,h=r.length-s.length;if("mod"!==e){(a=new n(null)).length=h+1,a.words=new Array(a.length);for(var u=0;u<a.length;u++)a.words[u]=0}var l=r.clone()._ishlnsubmul(s,1,h);0===l.negative&&(r=l,a&&(a.words[h]=1));for(var c=h-1;c>=0;c--){var d=67108864*(0|r.words[s.length+c])+(0|r.words[s.length+c-1]);for(d=Math.min(d/o|0,67108863),r._ishlnsubmul(s,d,c);0!==r.negative;)d--,r.negative=0,r._ishlnsubmul(s,1,c),r.isZero()||(r.negative^=1);a&&(a.words[c]=d)}return a&&a.strip(),r.strip(),"div"!==e&&0!==i&&r.iushrn(i),{div:a||null,mod:r}},n.prototype.divmod=function(t,e,r){return i(!t.isZero()),this.isZero()?{div:new n(0),mod:new n(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(s=a.div.neg()),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.iadd(t)),{div:s,mod:o}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(s=a.div.neg()),{div:s,mod:a.mod}):0!=(this.negative&t.negative)?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.isub(t)),{div:a.div,mod:o}):t.length>this.length||this.cmp(t)<0?{div:new n(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new n(this.modn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new n(this.modn(t.words[0]))}:this._wordDiv(t,e);var s,o,a},n.prototype.div=function(t){return this.divmod(t,"div",!1).div},n.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},n.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},n.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var i=0!==e.div.negative?e.mod.isub(t):e.mod,r=t.ushrn(1),n=t.andln(1),s=i.cmp(r);return s<0||1===n&&0===s?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},n.prototype.modn=function(t){i(t<=67108863);for(var e=(1<<26)%t,r=0,n=this.length-1;n>=0;n--)r=(e*r+(0|this.words[n]))%t;return r},n.prototype.idivn=function(t){i(t<=67108863);for(var e=0,r=this.length-1;r>=0;r--){var n=(0|this.words[r])+67108864*e;this.words[r]=n/t|0,e=n%t}return this.strip()},n.prototype.divn=function(t){return this.clone().idivn(t)},n.prototype.egcd=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s=new n(1),o=new n(0),a=new n(0),h=new n(1),u=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++u;for(var l=r.clone(),c=e.clone();!e.isZero();){for(var d=0,f=1;0==(e.words[0]&f)&&d<26;++d,f<<=1);if(d>0)for(e.iushrn(d);d-- >0;)(s.isOdd()||o.isOdd())&&(s.iadd(l),o.isub(c)),s.iushrn(1),o.iushrn(1);for(var m=0,g=1;0==(r.words[0]&g)&&m<26;++m,g<<=1);if(m>0)for(r.iushrn(m);m-- >0;)(a.isOdd()||h.isOdd())&&(a.iadd(l),h.isub(c)),a.iushrn(1),h.iushrn(1);e.cmp(r)>=0?(e.isub(r),s.isub(a),o.isub(h)):(r.isub(e),a.isub(s),h.isub(o))}return{a:a,b:h,gcd:r.iushln(u)}},n.prototype._invmp=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var s,o=new n(1),a=new n(0),h=r.clone();e.cmpn(1)>0&&r.cmpn(1)>0;){for(var u=0,l=1;0==(e.words[0]&l)&&u<26;++u,l<<=1);if(u>0)for(e.iushrn(u);u-- >0;)o.isOdd()&&o.iadd(h),o.iushrn(1);for(var c=0,d=1;0==(r.words[0]&d)&&c<26;++c,d<<=1);if(c>0)for(r.iushrn(c);c-- >0;)a.isOdd()&&a.iadd(h),a.iushrn(1);e.cmp(r)>=0?(e.isub(r),o.isub(a)):(r.isub(e),a.isub(o))}return(s=0===e.cmpn(1)?o:a).cmpn(0)<0&&s.iadd(t),s},n.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),i=t.clone();e.negative=0,i.negative=0;for(var r=0;e.isEven()&&i.isEven();r++)e.iushrn(1),i.iushrn(1);for(;;){for(;e.isEven();)e.iushrn(1);for(;i.isEven();)i.iushrn(1);var n=e.cmp(i);if(n<0){var s=e;e=i,i=s}else if(0===n||0===i.cmpn(1))break;e.isub(i)}return i.iushln(r)},n.prototype.invm=function(t){return this.egcd(t).a.umod(t)},n.prototype.isEven=function(){return 0==(1&this.words[0])},n.prototype.isOdd=function(){return 1==(1&this.words[0])},n.prototype.andln=function(t){return this.words[0]&t},n.prototype.bincn=function(t){i("number"==typeof t);var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return this._expand(r+1),this.words[r]|=n,this;for(var s=n,o=r;0!==s&&o<this.length;o++){var a=0|this.words[o];s=(a+=s)>>>26,this.words[o]=a&=67108863}return 0!==s&&(this.words[o]=s,this.length++),this},n.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},n.prototype.cmpn=function(t){var e,r=t<0;if(0!==this.negative&&!r)return-1;if(0===this.negative&&r)return 1;if(this.strip(),this.length>1)e=1;else{r&&(t=-t),i(t<=67108863,"Number is too big");var n=0|this.words[0];e=n===t?0:n<t?-1:1}return 0!==this.negative?0|-e:e},n.prototype.cmp=function(t){if(0!==this.negative&&0===t.negative)return-1;if(0===this.negative&&0!==t.negative)return 1;var e=this.ucmp(t);return 0!==this.negative?0|-e:e},n.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,i=this.length-1;i>=0;i--){var r=0|this.words[i],n=0|t.words[i];if(r!==n){r<n?e=-1:r>n&&(e=1);break}}return e},n.prototype.gtn=function(t){return 1===this.cmpn(t)},n.prototype.gt=function(t){return 1===this.cmp(t)},n.prototype.gten=function(t){return this.cmpn(t)>=0},n.prototype.gte=function(t){return this.cmp(t)>=0},n.prototype.ltn=function(t){return-1===this.cmpn(t)},n.prototype.lt=function(t){return-1===this.cmp(t)},n.prototype.lten=function(t){return this.cmpn(t)<=0},n.prototype.lte=function(t){return this.cmp(t)<=0},n.prototype.eqn=function(t){return 0===this.cmpn(t)},n.prototype.eq=function(t){return 0===this.cmp(t)},n.red=function(t){return new A(t)},n.prototype.toRed=function(t){return i(!this.red,"Already a number in reduction context"),i(0===this.negative,"red works only with positives"),t.convertTo(this)._forceRed(t)},n.prototype.fromRed=function(){return i(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(t){return this.red=t,this},n.prototype.forceRed=function(t){return i(!this.red,"Already a number in reduction context"),this._forceRed(t)},n.prototype.redAdd=function(t){return i(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},n.prototype.redIAdd=function(t){return i(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},n.prototype.redSub=function(t){return i(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},n.prototype.redISub=function(t){return i(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},n.prototype.redShl=function(t){return i(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},n.prototype.redMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},n.prototype.redIMul=function(t){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},n.prototype.redSqr=function(){return i(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return i(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return i(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return i(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return i(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(t){return i(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var p={k256:null,p224:null,p192:null,p25519:null};function w(t,e){this.name=t,this.p=new n(e,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function v(){w.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function y(){w.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function M(){w.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function b(){w.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function A(t){if("string"==typeof t){var e=n._prime(t);this.m=e.p,this.prime=e}else i(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}function x(t){A.call(this,t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}w.prototype._tmp=function(){var t=new n(null);return t.words=new Array(Math.ceil(this.n/13)),t},w.prototype.ireduce=function(t){var e,i=t;do{this.split(i,this.tmp),e=(i=(i=this.imulK(i)).iadd(this.tmp)).bitLength()}while(e>this.n);var r=e<this.n?-1:i.ucmp(this.p);return 0===r?(i.words[0]=0,i.length=1):r>0?i.isub(this.p):void 0!==i.strip?i.strip():i._strip(),i},w.prototype.split=function(t,e){t.iushrn(this.n,0,e)},w.prototype.imulK=function(t){return t.imul(this.k)},r(v,w),v.prototype.split=function(t,e){for(var i=4194303,r=Math.min(t.length,9),n=0;n<r;n++)e.words[n]=t.words[n];if(e.length=r,t.length<=9)return t.words[0]=0,void(t.length=1);var s=t.words[9];for(e.words[e.length++]=s&i,n=10;n<t.length;n++){var o=0|t.words[n];t.words[n-10]=(o&i)<<4|s>>>22,s=o}t.words[n-10]=s>>>=22,t.length-=0===s&&t.length>10?10:9},v.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,i=0;i<t.length;i++){var r=0|t.words[i];t.words[i]=67108863&(e+=977*r),e=64*r+(e/67108864|0)}return 0===t.words[t.length-1]&&(t.length--,0===t.words[t.length-1]&&t.length--),t},r(y,w),r(M,w),r(b,w),b.prototype.imulK=function(t){for(var e=0,i=0;i<t.length;i++){var r=19*(0|t.words[i])+e,n=67108863&r;r>>>=26,t.words[i]=n,e=r}return 0!==e&&(t.words[t.length++]=e),t},n._prime=function(t){if(p[t])return p[t];var e;if("k256"===t)e=new v;else if("p224"===t)e=new y;else if("p192"===t)e=new M;else{if("p25519"!==t)throw new Error("Unknown prime "+t);e=new b}return p[t]=e,e},A.prototype._verify1=function(t){i(0===t.negative,"red works only with positives"),i(t.red,"red works only with red numbers")},A.prototype._verify2=function(t,e){i(0==(t.negative|e.negative),"red works only with positives"),i(t.red&&t.red===e.red,"red works only with red numbers")},A.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):t.umod(this.m)._forceRed(this)},A.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},A.prototype.add=function(t,e){this._verify2(t,e);var i=t.add(e);return i.cmp(this.m)>=0&&i.isub(this.m),i._forceRed(this)},A.prototype.iadd=function(t,e){this._verify2(t,e);var i=t.iadd(e);return i.cmp(this.m)>=0&&i.isub(this.m),i},A.prototype.sub=function(t,e){this._verify2(t,e);var i=t.sub(e);return i.cmpn(0)<0&&i.iadd(this.m),i._forceRed(this)},A.prototype.isub=function(t,e){this._verify2(t,e);var i=t.isub(e);return i.cmpn(0)<0&&i.iadd(this.m),i},A.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},A.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},A.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},A.prototype.isqr=function(t){return this.imul(t,t.clone())},A.prototype.sqr=function(t){return this.mul(t,t)},A.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(i(e%2==1),3===e){var r=this.m.add(new n(1)).iushrn(2);return this.pow(t,r)}for(var s=this.m.subn(1),o=0;!s.isZero()&&0===s.andln(1);)o++,s.iushrn(1);i(!s.isZero());var a=new n(1).toRed(this),h=a.redNeg(),u=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new n(2*l*l).toRed(this);0!==this.pow(l,u).cmp(h);)l.redIAdd(h);for(var c=this.pow(l,s),d=this.pow(t,s.addn(1).iushrn(1)),f=this.pow(t,s),m=o;0!==f.cmp(a);){for(var g=f,p=0;0!==g.cmp(a);p++)g=g.redSqr();i(p<m);var w=this.pow(c,new n(1).iushln(m-p-1));d=d.redMul(w),c=w.redSqr(),f=f.redMul(c),m=p}return d},A.prototype.invm=function(t){var e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},A.prototype.pow=function(t,e){if(e.isZero())return new n(1).toRed(this);if(0===e.cmpn(1))return t.clone();var i=new Array(16);i[0]=new n(1).toRed(this),i[1]=t;for(var r=2;r<i.length;r++)i[r]=this.mul(i[r-1],t);var s=i[0],o=0,a=0,h=e.bitLength()%26;for(0===h&&(h=26),r=e.length-1;r>=0;r--){for(var u=e.words[r],l=h-1;l>=0;l--){var c=u>>l&1;s!==i[0]&&(s=this.sqr(s)),0!==c||0!==o?(o<<=1,o|=c,(4==++a||0===r&&0===l)&&(s=this.mul(s,i[o]),a=0,o=0)):a=0}h=26}return s},A.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},A.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},n.mont=function(t){return new x(t)},r(x,A),x.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},x.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},x.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var i=t.imul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),n=i.isub(r).iushrn(this.shift),s=n;return n.cmp(this.m)>=0?s=n.isub(this.m):n.cmpn(0)<0&&(s=n.iadd(this.m)),s._forceRed(this)},x.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new n(0)._forceRed(this);var i=t.mul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(r).iushrn(this.shift),o=s;return s.cmp(this.m)>=0?o=s.isub(this.m):s.cmpn(0)<0&&(o=s.iadd(this.m)),o._forceRed(this)},x.prototype.invm=function(t){return this.imod(t._invmp(this.m).mul(this.r2))._forceRed(this)}}(t,gt)}),Tt=pt(function(t,e){!function(t){var e,i,r,n=String.fromCharCode;function s(t){for(var e,i,r=[],n=0,s=t.length;n<s;)(e=t.charCodeAt(n++))>=55296&&e<=56319&&n<s?56320==(64512&(i=t.charCodeAt(n++)))?r.push(((1023&e)<<10)+(1023&i)+65536):(r.push(e),n--):r.push(e);return r}function o(t){if(t>=55296&&t<=57343)throw Error("Lone surrogate U+"+t.toString(16).toUpperCase()+" is not a scalar value")}function a(t,e){return n(t>>e&63|128)}function h(t){if(0==(4294967168&t))return n(t);var e="";return 0==(4294965248&t)?e=n(t>>6&31|192):0==(4294901760&t)?(o(t),e=n(t>>12&15|224),e+=a(t,6)):0==(4292870144&t)&&(e=n(t>>18&7|240),e+=a(t,12),e+=a(t,6)),e+n(63&t|128)}function u(){if(r>=i)throw Error("Invalid byte index");var t=255&e[r];if(r++,128==(192&t))return 63&t;throw Error("Invalid continuation byte")}function l(){var t,n;if(r>i)throw Error("Invalid byte index");if(r==i)return!1;if(t=255&e[r],r++,0==(128&t))return t;if(192==(224&t)){if((n=(31&t)<<6|u())>=128)return n;throw Error("Invalid continuation byte")}if(224==(240&t)){if((n=(15&t)<<12|u()<<6|u())>=2048)return o(n),n;throw Error("Invalid continuation byte")}if(240==(248&t)&&(n=(7&t)<<18|u()<<12|u()<<6|u())>=65536&&n<=1114111)return n;throw Error("Invalid UTF-8 detected")}t.version="3.0.0",t.encode=function(t){for(var e=s(t),i=e.length,r=-1,n="";++r<i;)n+=h(e[r]);return n},t.decode=function(t){e=s(t),i=e.length,r=0;for(var o,a=[];!1!==(o=l());)a.push(o);return function(t){for(var e,i=t.length,r=-1,s="";++r<i;)(e=t[r])>65535&&(s+=n((e-=65536)>>>10&1023|55296),e=56320|1023&e),s+=n(e);return s}(a)}}(e)});const Ot="0123456789abcdef".split(""),Bt=[1,256,65536,16777216],Ct=[0,8,16,24],_t=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],Dt=t=>{var e,i,r,n,s,o,a,h,u,l,c,d,f,m,g,p,w,v,y,M,b,A,x,E,R,k,S,I,T,O,B,C,_,D,P,N,L,W,F,U,$,j,q,G,z,H,Z,V,J,K,X,Q,Y,tt,et,it,rt,nt,st,ot,at,ht,ut;for(r=0;r<48;r+=2)n=t[0]^t[10]^t[20]^t[30]^t[40],s=t[1]^t[11]^t[21]^t[31]^t[41],h=t[4]^t[14]^t[24]^t[34]^t[44],u=t[5]^t[15]^t[25]^t[35]^t[45],l=t[6]^t[16]^t[26]^t[36]^t[46],c=t[7]^t[17]^t[27]^t[37]^t[47],i=(f=t[9]^t[19]^t[29]^t[39]^t[49])^((a=t[3]^t[13]^t[23]^t[33]^t[43])<<1|(o=t[2]^t[12]^t[22]^t[32]^t[42])>>>31),t[0]^=e=(d=t[8]^t[18]^t[28]^t[38]^t[48])^(o<<1|a>>>31),t[1]^=i,t[10]^=e,t[11]^=i,t[20]^=e,t[21]^=i,t[30]^=e,t[31]^=i,t[40]^=e,t[41]^=i,i=s^(u<<1|h>>>31),t[2]^=e=n^(h<<1|u>>>31),t[3]^=i,t[12]^=e,t[13]^=i,t[22]^=e,t[23]^=i,t[32]^=e,t[33]^=i,t[42]^=e,t[43]^=i,i=a^(c<<1|l>>>31),t[4]^=e=o^(l<<1|c>>>31),t[5]^=i,t[14]^=e,t[15]^=i,t[24]^=e,t[25]^=i,t[34]^=e,t[35]^=i,t[44]^=e,t[45]^=i,i=u^(f<<1|d>>>31),t[6]^=e=h^(d<<1|f>>>31),t[7]^=i,t[16]^=e,t[17]^=i,t[26]^=e,t[27]^=i,t[36]^=e,t[37]^=i,t[46]^=e,t[47]^=i,i=c^(s<<1|n>>>31),t[8]^=e=l^(n<<1|s>>>31),t[9]^=i,t[18]^=e,t[19]^=i,t[28]^=e,t[29]^=i,t[38]^=e,t[39]^=i,t[48]^=e,t[49]^=i,g=t[1],H=t[11]<<4|t[10]>>>28,Z=t[10]<<4|t[11]>>>28,I=t[20]<<3|t[21]>>>29,T=t[21]<<3|t[20]>>>29,ot=t[31]<<9|t[30]>>>23,at=t[30]<<9|t[31]>>>23,j=t[40]<<18|t[41]>>>14,q=t[41]<<18|t[40]>>>14,D=t[2]<<1|t[3]>>>31,P=t[3]<<1|t[2]>>>31,w=t[12]<<12|t[13]>>>20,V=t[22]<<10|t[23]>>>22,J=t[23]<<10|t[22]>>>22,O=t[33]<<13|t[32]>>>19,B=t[32]<<13|t[33]>>>19,ht=t[42]<<2|t[43]>>>30,ut=t[43]<<2|t[42]>>>30,tt=t[5]<<30|t[4]>>>2,et=t[4]<<30|t[5]>>>2,N=t[14]<<6|t[15]>>>26,L=t[15]<<6|t[14]>>>26,y=t[24]<<11|t[25]>>>21,K=t[34]<<15|t[35]>>>17,X=t[35]<<15|t[34]>>>17,C=t[45]<<29|t[44]>>>3,_=t[44]<<29|t[45]>>>3,E=t[6]<<28|t[7]>>>4,R=t[7]<<28|t[6]>>>4,it=t[17]<<23|t[16]>>>9,rt=t[16]<<23|t[17]>>>9,W=t[26]<<25|t[27]>>>7,F=t[27]<<25|t[26]>>>7,M=t[36]<<21|t[37]>>>11,b=t[37]<<21|t[36]>>>11,Q=t[47]<<24|t[46]>>>8,Y=t[46]<<24|t[47]>>>8,G=t[8]<<27|t[9]>>>5,z=t[9]<<27|t[8]>>>5,k=t[18]<<20|t[19]>>>12,S=t[19]<<20|t[18]>>>12,nt=t[29]<<7|t[28]>>>25,st=t[28]<<7|t[29]>>>25,U=t[38]<<8|t[39]>>>24,$=t[39]<<8|t[38]>>>24,A=t[48]<<14|t[49]>>>18,x=t[49]<<14|t[48]>>>18,t[0]=(m=t[0])^~(p=t[13]<<12|t[12]>>>20)&(v=t[25]<<11|t[24]>>>21),t[1]=g^~w&y,t[10]=E^~k&I,t[11]=R^~S&T,t[20]=D^~N&W,t[21]=P^~L&F,t[30]=G^~H&V,t[31]=z^~Z&J,t[40]=tt^~it&nt,t[41]=et^~rt&st,t[2]=p^~v&M,t[3]=w^~y&b,t[12]=k^~I&O,t[13]=S^~T&B,t[22]=N^~W&U,t[23]=L^~F&$,t[32]=H^~V&K,t[33]=Z^~J&X,t[42]=it^~nt&ot,t[43]=rt^~st&at,t[4]=v^~M&A,t[5]=y^~b&x,t[14]=I^~O&C,t[15]=T^~B&_,t[24]=W^~U&j,t[25]=F^~$&q,t[34]=V^~K&Q,t[35]=J^~X&Y,t[44]=nt^~ot&ht,t[45]=st^~at&ut,t[6]=M^~A&m,t[7]=b^~x&g,t[16]=O^~C&E,t[17]=B^~_&R,t[26]=U^~j&D,t[27]=$^~q&P,t[36]=K^~Q&G,t[37]=X^~Y&z,t[46]=ot^~ht&tt,t[47]=at^~ut&et,t[8]=A^~m&p,t[9]=x^~g&w,t[18]=C^~E&k,t[19]=_^~R&S,t[28]=j^~D&N,t[29]=q^~P&L,t[38]=Q^~G&H,t[39]=Y^~z&Z,t[48]=ht^~tt&it,t[49]=ut^~et&rt,t[0]^=_t[r],t[1]^=_t[r+1]},Pt=t=>e=>{var i;if("0x"===e.slice(0,2)){i=[];for(var r=2,n=e.length;r<n;r+=2)i.push(parseInt(e.slice(r,r+2),16))}else i=e;return((t,e)=>{for(var i,r=e.length,n=t.blocks,s=t.blockCount<<2,o=t.blockCount,a=t.outputBlocks,h=t.s,u=0;u<r;){if(t.reset)for(t.reset=!1,n[0]=t.block,d=1;d<o+1;++d)n[d]=0;if("string"!=typeof e)for(d=t.start;u<r&&d<s;++u)n[d>>2]|=e[u]<<Ct[3&d++];else for(d=t.start;u<r&&d<s;++u)(i=e.charCodeAt(u))<128?n[d>>2]|=i<<Ct[3&d++]:i<2048?(n[d>>2]|=(192|i>>6)<<Ct[3&d++],n[d>>2]|=(128|63&i)<<Ct[3&d++]):i<55296||i>=57344?(n[d>>2]|=(224|i>>12)<<Ct[3&d++],n[d>>2]|=(128|i>>6&63)<<Ct[3&d++],n[d>>2]|=(128|63&i)<<Ct[3&d++]):(i=65536+((1023&i)<<10|1023&e.charCodeAt(++u)),n[d>>2]|=(240|i>>18)<<Ct[3&d++],n[d>>2]|=(128|i>>12&63)<<Ct[3&d++],n[d>>2]|=(128|i>>6&63)<<Ct[3&d++],n[d>>2]|=(128|63&i)<<Ct[3&d++]);if(t.lastByteIndex=d,d>=s){for(t.start=d-s,t.block=n[o],d=0;d<o;++d)h[d]^=n[d];Dt(h),t.reset=!0}else t.start=d}if(n[(d=t.lastByteIndex)>>2]|=Bt[3&d],t.lastByteIndex===s)for(n[0]=n[o],d=1;d<o+1;++d)n[d]=0;for(n[o-1]|=2147483648,d=0;d<o;++d)h[d]^=n[d];Dt(h);for(var l,c="",d=0,f=0;f<a;){for(d=0;d<o&&f<a;++d,++f)c+=Ot[(l=h[d])>>4&15]+Ot[15&l]+Ot[l>>12&15]+Ot[l>>8&15]+Ot[l>>20&15]+Ot[l>>16&15]+Ot[l>>28&15]+Ot[l>>24&15];f%o==0&&(Dt(h),d=0)}return"0x"+c})((t=>{return{blocks:[],reset:!0,block:0,start:0,blockCount:1600-(t<<1)>>5,outputBlocks:t>>5,s:(e=[0,0,0,0,0,0,0,0,0,0],[].concat(e,e,e,e,e))};var e})(t),i)};var Nt={keccak256:Pt(256),keccak512:Pt(512),keccak256s:Pt(256),keccak512s:Pt(512)},Lt=pt(function(t){!function(){var e="input is invalid type",i="object"==typeof window,r=i?window:{};r.JS_SHA3_NO_WINDOW&&(i=!1);var n=!i&&"object"==typeof self;!r.JS_SHA3_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node?r=gt:n&&(r=self);var s=!r.JS_SHA3_NO_COMMON_JS&&t.exports,o=!r.JS_SHA3_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,a="0123456789abcdef".split(""),h=[4,1024,262144,67108864],u=[0,8,16,24],l=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],c=[224,256,384,512],d=[128,256],f=["hex","buffer","arrayBuffer","array","digest"],m={128:168,256:136};!r.JS_SHA3_NO_NODE_JS&&Array.isArray||(Array.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)}),!o||!r.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW&&ArrayBuffer.isView||(ArrayBuffer.isView=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});for(var g=function(t,e,i){return function(r){return new O(t,e,t).update(r)[i]()}},p=function(t,e,i){return function(r,n){return new O(t,e,n).update(r)[i]()}},w=function(t,e,i){return function(e,r,n,s){return A["cshake"+t].update(e,r,n,s)[i]()}},v=function(t,e,i){return function(e,r,n,s){return A["kmac"+t].update(e,r,n,s)[i]()}},y=function(t,e,i,r){for(var n=0;n<f.length;++n){var s=f[n];t[s]=e(i,r,s)}return t},M=function(t,e){var i=g(t,e,"hex");return i.create=function(){return new O(t,e,t)},i.update=function(t){return i.create().update(t)},y(i,g,t,e)},b=[{name:"keccak",padding:[1,256,65536,16777216],bits:c,createMethod:M},{name:"sha3",padding:[6,1536,393216,100663296],bits:c,createMethod:M},{name:"shake",padding:[31,7936,2031616,520093696],bits:d,createMethod:function(t,e){var i=p(t,e,"hex");return i.create=function(i){return new O(t,e,i)},i.update=function(t,e){return i.create(e).update(t)},y(i,p,t,e)}},{name:"cshake",padding:h,bits:d,createMethod:function(t,e){var i=m[t],r=w(t,0,"hex");return r.create=function(r,n,s){return n||s?new O(t,e,r).bytepad([n,s],i):A["shake"+t].create(r)},r.update=function(t,e,i,n){return r.create(e,i,n).update(t)},y(r,w,t,e)}},{name:"kmac",padding:h,bits:d,createMethod:function(t,e){var i=m[t],r=v(t,0,"hex");return r.create=function(r,n,s){return new B(t,e,n).bytepad(["KMAC",s],i).bytepad([r],i)},r.update=function(t,e,i,n){return r.create(t,i,n).update(e)},y(r,v,t,e)}}],A={},x=[],E=0;E<b.length;++E)for(var R=b[E],k=R.bits,S=0;S<k.length;++S){var I=R.name+"_"+k[S];if(x.push(I),A[I]=R.createMethod(k[S],R.padding),"sha3"!==R.name){var T=R.name+k[S];x.push(T),A[T]=A[I]}}function O(t,e,i){this.blocks=[],this.s=[],this.padding=e,this.outputBits=i,this.reset=!0,this.finalized=!1,this.block=0,this.start=0,this.blockCount=1600-(t<<1)>>5,this.byteCount=this.blockCount<<2,this.outputBlocks=i>>5,this.extraBytes=(31&i)>>3;for(var r=0;r<50;++r)this.s[r]=0}function B(t,e,i){O.call(this,t,e,i)}O.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i,r=typeof t;if("string"!==r){if("object"!==r)throw new Error(e);if(null===t)throw new Error(e);if(o&&t.constructor===ArrayBuffer)t=new Uint8Array(t);else if(!(Array.isArray(t)||o&&ArrayBuffer.isView(t)))throw new Error(e);i=!0}for(var n,s,a=this.blocks,h=this.byteCount,l=t.length,c=this.blockCount,d=0,f=this.s;d<l;){if(this.reset)for(this.reset=!1,a[0]=this.block,n=1;n<c+1;++n)a[n]=0;if(i)for(n=this.start;d<l&&n<h;++d)a[n>>2]|=t[d]<<u[3&n++];else for(n=this.start;d<l&&n<h;++d)(s=t.charCodeAt(d))<128?a[n>>2]|=s<<u[3&n++]:s<2048?(a[n>>2]|=(192|s>>6)<<u[3&n++],a[n>>2]|=(128|63&s)<<u[3&n++]):s<55296||s>=57344?(a[n>>2]|=(224|s>>12)<<u[3&n++],a[n>>2]|=(128|s>>6&63)<<u[3&n++],a[n>>2]|=(128|63&s)<<u[3&n++]):(s=65536+((1023&s)<<10|1023&t.charCodeAt(++d)),a[n>>2]|=(240|s>>18)<<u[3&n++],a[n>>2]|=(128|s>>12&63)<<u[3&n++],a[n>>2]|=(128|s>>6&63)<<u[3&n++],a[n>>2]|=(128|63&s)<<u[3&n++]);if(this.lastByteIndex=n,n>=h){for(this.start=n-h,this.block=a[c],n=0;n<c;++n)f[n]^=a[n];C(f),this.reset=!0}else this.start=n}return this},O.prototype.encode=function(t,e){var i=255&t,r=1,n=[i];for(i=255&(t>>=8);i>0;)n.unshift(i),i=255&(t>>=8),++r;return e?n.push(r):n.unshift(r),this.update(n),n.length},O.prototype.encodeString=function(t){var i,r=typeof t;if("string"!==r){if("object"!==r)throw new Error(e);if(null===t)throw new Error(e);if(o&&t.constructor===ArrayBuffer)t=new Uint8Array(t);else if(!(Array.isArray(t)||o&&ArrayBuffer.isView(t)))throw new Error(e);i=!0}var n=0;if(i)n=t.length;else for(var s=0;s<t.length;++s){var a=t.charCodeAt(s);a<128?n+=1:a<2048?n+=2:a<55296||a>=57344?n+=3:(a=65536+((1023&a)<<10|1023&t.charCodeAt(++s)),n+=4)}return n+=this.encode(8*n),this.update(t),n},O.prototype.bytepad=function(t,e){for(var i=this.encode(e),r=0;r<t.length;++r)i+=this.encodeString(t[r]);var n=[];return n.length=e-i%e,this.update(n),this},O.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,e=this.lastByteIndex,i=this.blockCount,r=this.s;if(t[e>>2]|=this.padding[3&e],this.lastByteIndex===this.byteCount)for(t[0]=t[i],e=1;e<i+1;++e)t[e]=0;for(t[i-1]|=2147483648,e=0;e<i;++e)r[e]^=t[e];C(r)}},O.prototype.toString=O.prototype.hex=function(){this.finalize();for(var t,e=this.blockCount,i=this.s,r=this.outputBlocks,n=this.extraBytes,s=0,o=0,h="";o<r;){for(s=0;s<e&&o<r;++s,++o)h+=a[(t=i[s])>>4&15]+a[15&t]+a[t>>12&15]+a[t>>8&15]+a[t>>20&15]+a[t>>16&15]+a[t>>28&15]+a[t>>24&15];o%e==0&&(C(i),s=0)}return n&&(h+=a[(t=i[s])>>4&15]+a[15&t],n>1&&(h+=a[t>>12&15]+a[t>>8&15]),n>2&&(h+=a[t>>20&15]+a[t>>16&15])),h},O.prototype.buffer=O.prototype.arrayBuffer=function(){this.finalize();var t,e=this.blockCount,i=this.s,r=this.outputBlocks,n=this.extraBytes,s=0,o=0,a=this.outputBits>>3;t=n?new ArrayBuffer(r+1<<2):new ArrayBuffer(a);for(var h=new Uint32Array(t);o<r;){for(s=0;s<e&&o<r;++s,++o)h[o]=i[s];o%e==0&&C(i)}return n&&(h[s]=i[s],t=t.slice(0,a)),t},O.prototype.digest=O.prototype.array=function(){this.finalize();for(var t,e,i=this.blockCount,r=this.s,n=this.outputBlocks,s=this.extraBytes,o=0,a=0,h=[];a<n;){for(o=0;o<i&&a<n;++o,++a)h[t=a<<2]=255&(e=r[o]),h[t+1]=e>>8&255,h[t+2]=e>>16&255,h[t+3]=e>>24&255;a%i==0&&C(r)}return s&&(h[t=a<<2]=255&(e=r[o]),s>1&&(h[t+1]=e>>8&255),s>2&&(h[t+2]=e>>16&255)),h},(B.prototype=new O).finalize=function(){return this.encode(this.outputBits,!0),O.prototype.finalize.call(this)};var C=function(t){var e,i,r,n,s,o,a,h,u,c,d,f,m,g,p,w,v,y,M,b,A,x,E,R,k,S,I,T,O,B,C,_,D,P,N,L,W,F,U,$,j,q,G,z,H,Z,V,J,K,X,Q,Y,tt,et,it,rt,nt,st,ot,at,ht,ut,lt;for(r=0;r<48;r+=2)n=t[0]^t[10]^t[20]^t[30]^t[40],s=t[1]^t[11]^t[21]^t[31]^t[41],h=t[4]^t[14]^t[24]^t[34]^t[44],u=t[5]^t[15]^t[25]^t[35]^t[45],c=t[6]^t[16]^t[26]^t[36]^t[46],d=t[7]^t[17]^t[27]^t[37]^t[47],i=(m=t[9]^t[19]^t[29]^t[39]^t[49])^((a=t[3]^t[13]^t[23]^t[33]^t[43])<<1|(o=t[2]^t[12]^t[22]^t[32]^t[42])>>>31),t[0]^=e=(f=t[8]^t[18]^t[28]^t[38]^t[48])^(o<<1|a>>>31),t[1]^=i,t[10]^=e,t[11]^=i,t[20]^=e,t[21]^=i,t[30]^=e,t[31]^=i,t[40]^=e,t[41]^=i,i=s^(u<<1|h>>>31),t[2]^=e=n^(h<<1|u>>>31),t[3]^=i,t[12]^=e,t[13]^=i,t[22]^=e,t[23]^=i,t[32]^=e,t[33]^=i,t[42]^=e,t[43]^=i,i=a^(d<<1|c>>>31),t[4]^=e=o^(c<<1|d>>>31),t[5]^=i,t[14]^=e,t[15]^=i,t[24]^=e,t[25]^=i,t[34]^=e,t[35]^=i,t[44]^=e,t[45]^=i,i=u^(m<<1|f>>>31),t[6]^=e=h^(f<<1|m>>>31),t[7]^=i,t[16]^=e,t[17]^=i,t[26]^=e,t[27]^=i,t[36]^=e,t[37]^=i,t[46]^=e,t[47]^=i,i=d^(s<<1|n>>>31),t[8]^=e=c^(n<<1|s>>>31),t[9]^=i,t[18]^=e,t[19]^=i,t[28]^=e,t[29]^=i,t[38]^=e,t[39]^=i,t[48]^=e,t[49]^=i,p=t[1],Z=t[11]<<4|t[10]>>>28,V=t[10]<<4|t[11]>>>28,T=t[20]<<3|t[21]>>>29,O=t[21]<<3|t[20]>>>29,at=t[31]<<9|t[30]>>>23,ht=t[30]<<9|t[31]>>>23,q=t[40]<<18|t[41]>>>14,G=t[41]<<18|t[40]>>>14,P=t[2]<<1|t[3]>>>31,N=t[3]<<1|t[2]>>>31,v=t[12]<<12|t[13]>>>20,J=t[22]<<10|t[23]>>>22,K=t[23]<<10|t[22]>>>22,B=t[33]<<13|t[32]>>>19,C=t[32]<<13|t[33]>>>19,ut=t[42]<<2|t[43]>>>30,lt=t[43]<<2|t[42]>>>30,et=t[5]<<30|t[4]>>>2,it=t[4]<<30|t[5]>>>2,L=t[14]<<6|t[15]>>>26,W=t[15]<<6|t[14]>>>26,M=t[24]<<11|t[25]>>>21,X=t[34]<<15|t[35]>>>17,Q=t[35]<<15|t[34]>>>17,_=t[45]<<29|t[44]>>>3,D=t[44]<<29|t[45]>>>3,R=t[6]<<28|t[7]>>>4,k=t[7]<<28|t[6]>>>4,rt=t[17]<<23|t[16]>>>9,nt=t[16]<<23|t[17]>>>9,F=t[26]<<25|t[27]>>>7,U=t[27]<<25|t[26]>>>7,b=t[36]<<21|t[37]>>>11,A=t[37]<<21|t[36]>>>11,Y=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,z=t[8]<<27|t[9]>>>5,H=t[9]<<27|t[8]>>>5,S=t[18]<<20|t[19]>>>12,I=t[19]<<20|t[18]>>>12,st=t[29]<<7|t[28]>>>25,ot=t[28]<<7|t[29]>>>25,$=t[38]<<8|t[39]>>>24,j=t[39]<<8|t[38]>>>24,x=t[48]<<14|t[49]>>>18,E=t[49]<<14|t[48]>>>18,t[0]=(g=t[0])^~(w=t[13]<<12|t[12]>>>20)&(y=t[25]<<11|t[24]>>>21),t[1]=p^~v&M,t[10]=R^~S&T,t[11]=k^~I&O,t[20]=P^~L&F,t[21]=N^~W&U,t[30]=z^~Z&J,t[31]=H^~V&K,t[40]=et^~rt&st,t[41]=it^~nt&ot,t[2]=w^~y&b,t[3]=v^~M&A,t[12]=S^~T&B,t[13]=I^~O&C,t[22]=L^~F&$,t[23]=W^~U&j,t[32]=Z^~J&X,t[33]=V^~K&Q,t[42]=rt^~st&at,t[43]=nt^~ot&ht,t[4]=y^~b&x,t[5]=M^~A&E,t[14]=T^~B&_,t[15]=O^~C&D,t[24]=F^~$&q,t[25]=U^~j&G,t[34]=J^~X&Y,t[35]=K^~Q&tt,t[44]=st^~at&ut,t[45]=ot^~ht&lt,t[6]=b^~x&g,t[7]=A^~E&p,t[16]=B^~_&R,t[17]=C^~D&k,t[26]=$^~q&P,t[27]=j^~G&N,t[36]=X^~Y&z,t[37]=Q^~tt&H,t[46]=at^~ut&et,t[47]=ht^~lt&it,t[8]=x^~g&w,t[9]=E^~p&v,t[18]=_^~R&S,t[19]=D^~k&I,t[28]=q^~P&L,t[29]=G^~N&W,t[38]=Y^~z&Z,t[39]=tt^~H&V,t[48]=ut^~et&rt,t[49]=lt^~it&nt,t[0]^=l[r],t[1]^=l[r+1]};if(s)t.exports=A;else for(E=0;E<x.length;++E)r[x[E]]=A[x[E]]}()});function Wt(t){if(null==t)throw new Error("cannot convert null value to array");if("string"==typeof t){const e=t.match(/^(0x)?[0-9a-fA-F]*$/);if(!e)throw new Error("invalid hexidecimal string");if("0x"!==e[1])throw new Error("hex string must have 0x prefix");(t=t.substring(2)).length%2&&(t="0"+t);const i=[];for(let e=0;e<t.length;e+=2)i.push(parseInt(t.substr(e,2),16));return Ft(new Uint8Array(i))}if(function(t){if(!t||parseInt(String(t.length))!=t.length||"string"==typeof t)return!1;for(let e=0;e<t.length;e++){const i=t[e];if(i<0||i>=256||parseInt(String(i))!=i)return!1}return!0}(t))return Ft(new Uint8Array(t));throw new Error("invalid arrayify value")}function Ft(t){return void 0!==t.slice||(t.slice=()=>{const e=Array.prototype.slice.call(arguments);return Ft(new Uint8Array(Array.prototype.slice.apply(t,e)))}),t}var Ut=/*#__PURE__*/Object.defineProperty({keccak256:function(t){return"0x"+Lt.keccak_256(Wt(t))},padLeft:(t,e)=>{const i=/^0x/i.test(t)||"number"==typeof t;return t=t.toString().replace(/^0x/i,""),(i?"0x":"")+new Array(e-t.length+1>=0?e-t.length+1:0).join("0")+t},bytesToHex:function(t){const e=[];for(let i=0;i<t.length;i++)e.push((t[i]>>>4).toString(16)),e.push((15&t[i]).toString(16));return`0x${e.join("").replace(/^0+/,"")}`},toByteArray:Wt},"__esModule",{value:!0});function $t(t){return!("string"!=typeof t||!/^(0x)?[0-9a-f]{512}$/i.test(t)||!/^(0x)?[0-9a-f]{512}$/.test(t)&&!/^(0x)?[0-9A-F]{512}$/.test(t))}function jt(t,e){"object"==typeof e&&e.constructor===Uint8Array&&(e=Ut.bytesToHex(e));const i=Ut.keccak256(e).replace("0x","");for(let e=0;e<12;e+=4){const r=(parseInt(i.substr(e,2),16)<<8)+parseInt(i.substr(e+2,2),16)&2047,n=1<<r%4;if((qt(t.charCodeAt(t.length-1-Math.floor(r/4)))&n)!==n)return!1}return!0}function qt(t){if(t>=48&&t<=57)return t-48;if(t>=65&&t<=70)return t-55;if(t>=97&&t<=102)return t-87;throw new Error("invalid bloom")}function Gt(t){return!("string"!=typeof t||!/^(0x)?[0-9a-f]{64}$/i.test(t)||!/^(0x)?[0-9a-f]{64}$/.test(t)&&!/^(0x)?[0-9A-F]{64}$/.test(t))}function zt(t){return!("string"!=typeof t||!t.match(/^(0x)?[0-9a-fA-F]{40}$/)&&!t.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/))}var Ht=/*#__PURE__*/Object.defineProperty({isBloom:$t,isInBloom:jt,isUserEthereumAddressInBloom:function(t,e){if(!$t(t))throw new Error("Invalid bloom given");if(!zt(e))throw new Error(`Invalid ethereum address given: "${e}"`);return jt(t,Ut.padLeft(e,64))},isContractAddressInBloom:function(t,e){if(!$t(t))throw new Error("Invalid bloom given");if(!zt(e))throw new Error(`Invalid contract address given: "${e}"`);return jt(t,e)},isTopicInBloom:function(t,e){if(!$t(t))throw new Error("Invalid bloom given");if(!Gt(e))throw new Error("Invalid topic");return jt(t,e)},isTopic:Gt,isAddress:zt},"__esModule",{value:!0}),Zt=function(t){return It.isBN(t)},Vt=function(t){return t&&t.constructor&&"BigNumber"===t.constructor.name},Jt=function(t){try{return bt.apply(null,arguments)}catch(e){throw new Error(e+' Given value: "'+t+'"')}},Kt=function(t){return!!/^(0x)?[0-9a-f]{40}$/i.test(t)&&(!(!/^(0x|0X)?[0-9a-f]{40}$/.test(t)&&!/^(0x|0X)?[0-9A-F]{40}$/.test(t))||Xt(t))},Xt=function(t){t=t.replace(/^0x/i,"");for(var e=oe(t.toLowerCase()).replace(/^0x/i,""),i=0;i<40;i++)if(parseInt(e[i],16)>7&&t[i].toUpperCase()!==t[i]||parseInt(e[i],16)<=7&&t[i].toLowerCase()!==t[i])return!1;return!0},Qt=function(t){var e="";t=(t=(t=(t=(t=Tt.encode(t)).replace(/^(?:\u0000)*/,"")).split("").reverse().join("")).replace(/^(?:\u0000)*/,"")).split("").reverse().join("");for(var i=0;i<t.length;i++){var r=t.charCodeAt(i).toString(16);e+=r.length<2?"0"+r:r}return"0x"+e},Yt=function(t){if(!t)return t;if("string"==typeof t&&!re(t))throw new Error('Given value "'+t+'" is not a valid hex string.');return Jt(t).toNumber()},te=function(t){if(null==t)return t;if(!isFinite(t)&&!re(t))throw new Error('Given input "'+t+'" is not a number.');var e=Jt(t),i=e.toString(16);return e.lt(new It(0))?"-0x"+i.substr(1):"0x"+i},ee=function(t){if(t=t.toString(16),!re(t))throw new Error('Given value "'+t+'" is not a valid hex string.');t=t.replace(/^0x/i,"");for(var e=[],i=0;i<t.length;i+=2)e.push(parseInt(t.substr(i,2),16));return e},ie=function(t,e){if(Kt(t))return e?"address":"0x"+t.toLowerCase().replace(/^0x/i,"");if("boolean"==typeof t)return e?"bool":t?"0x01":"0x00";if(Buffer.isBuffer(t))return"0x"+t.toString("hex");if("object"==typeof t&&t&&!Vt(t)&&!Zt(t))return e?"string":Qt(JSON.stringify(t));if("string"==typeof t){if(0===t.indexOf("-0x")||0===t.indexOf("-0X"))return e?"int256":te(t);if(0===t.indexOf("0x")||0===t.indexOf("0X"))return e?"bytes":t;if(!isFinite(t))return e?"string":Qt(t)}return e?t<0?"int256":"uint256":te(t)},re=function(t){return("string"==typeof t||"number"==typeof t)&&/^(-)?0x[0-9a-f]*$/i.test(t)},ne=function(t){return("string"==typeof t||"number"==typeof t)&&/^(-0x|0x)?[0-9a-f]*$/i.test(t)},se="0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",oe=function(t){Zt(t)&&(t=t.toString()),re(t)&&/^0x/i.test(t.toString())&&(t=ee(t));var e=Nt.keccak256(t);return e===se?null:e};oe._Hash=Nt;var ae,he,ue,le={BN:It,isBN:Zt,isBigNumber:Vt,toBN:Jt,isAddress:Kt,isBloom:function(t){return Ht.isBloom(t)},isUserEthereumAddressInBloom:function(t,e){return Ht.isUserEthereumAddressInBloom(t,e)},isContractAddressInBloom:function(t,e){return Ht.isContractAddressInBloom(t,e)},isTopic:function(t){return Ht.isTopic(t)},isTopicInBloom:function(t,e){return Ht.isTopicInBloom(t,e)},isInBloom:function(t,e){return Ht.isInBloom(t,e)},checkAddressChecksum:Xt,utf8ToHex:Qt,hexToUtf8:function(t){if(!re(t))throw new Error('The parameter "'+t+'" must be a valid HEX string.');for(var e="",i=0,r=(t=(t=(t=(t=(t=t.replace(/^0x/i,"")).replace(/^(?:00)*/,"")).split("").reverse().join("")).replace(/^(?:00)*/,"")).split("").reverse().join("")).length,n=0;n<r;n+=2)i=parseInt(t.substr(n,2),16),e+=String.fromCharCode(i);return Tt.decode(e)},hexToNumber:Yt,hexToNumberString:function(t){if(!t)return t;if("string"==typeof t&&!re(t))throw new Error('Given value "'+t+'" is not a valid hex string.');return Jt(t).toString(10)},numberToHex:te,toHex:ie,hexToBytes:ee,bytesToHex:function(t){for(var e=[],i=0;i<t.length;i++)e.push((t[i]>>>4).toString(16)),e.push((15&t[i]).toString(16));return"0x"+e.join("")},isHex:ne,isHexStrict:re,stripHexPrefix:function(t){return 0!==t&&ne(t)?t.replace(/^(-)?0x/i,"$1"):t},leftPad:function(t,e,i){var r=/^0x/i.test(t)||"number"==typeof t;return t=t.toString(16).replace(/^0x/i,""),(r?"0x":"")+new Array(e-t.length+1>=0?e-t.length+1:0).join(i||"0")+t},rightPad:function(t,e,i){return(/^0x/i.test(t)||"number"==typeof t?"0x":"")+(t=t.toString(16).replace(/^0x/i,""))+new Array(e-t.length+1>=0?e-t.length+1:0).join(i||"0")},toTwosComplement:function(t){return"0x"+Jt(t).toTwos(256).toString(16,64)},sha3:oe,sha3Raw:function(t){return null===(t=oe(t))?se:t},toNumber:function(t){return"number"==typeof t?t:Yt(ie(t))}},ce=function(t){var e=typeof t;if("string"===e)return le.isHexStrict(t)?new It(t.replace(/0x/i,""),16):new It(t,10);if("number"===e)return new It(t);if(le.isBigNumber(t))return new It(t.toString(10));if(le.isBN(t))return t;throw new Error(t+" is not a number")},de=function(t,e,i){var r,n,s;if("bytes"===(t=(s=t).startsWith("int[")?"int256"+s.slice(3):"int"===s?"int256":s.startsWith("uint[")?"uint256"+s.slice(4):"uint"===s?"uint256":s.startsWith("fixed[")?"fixed128x128"+s.slice(5):"fixed"===s?"fixed128x128":s.startsWith("ufixed[")?"ufixed128x128"+s.slice(6):"ufixed"===s?"ufixed128x128":s)){if(e.replace(/^0x/i,"").length%2!=0)throw new Error("Invalid bytes characters "+e.length);return e}if("string"===t)return le.utf8ToHex(e);if("bool"===t)return e?"01":"00";if(t.startsWith("address")){if(r=i?64:40,!le.isAddress(e))throw new Error(e+" is not a valid address, or the checksum is invalid.");return le.leftPad(e.toLowerCase(),r)}if(r=function(t){var e=/^\D+(\d+).*$/.exec(t);return e?parseInt(e[1],10):null}(t),t.startsWith("bytes")){if(!r)throw new Error("bytes[] not yet supported in solidity");if(i&&(r=32),r<1||r>32||r<e.replace(/^0x/i,"").length/2)throw new Error("Invalid bytes"+r+" for "+e);return le.rightPad(e,2*r)}if(t.startsWith("uint")){if(r%8||r<8||r>256)throw new Error("Invalid uint"+r+" size");if((n=ce(e)).bitLength()>r)throw new Error("Supplied uint exceeds width: "+r+" vs "+n.bitLength());if(n.lt(new It(0)))throw new Error("Supplied uint "+n.toString()+" is negative");return r?le.leftPad(n.toString("hex"),r/8*2):n}if(t.startsWith("int")){if(r%8||r<8||r>256)throw new Error("Invalid int"+r+" size");if((n=ce(e)).bitLength()>r)throw new Error("Supplied int exceeds width: "+r+" vs "+n.bitLength());return n.lt(new It(0))?n.toTwos(r).toString("hex"):r?le.leftPad(n.toString("hex"),r/8*2):n}throw new Error("Unsupported or invalid type: "+t)},fe=function(t){if(Array.isArray(t))throw new Error("Autodetection of array types is not supported.");var e,i,r="";if(t&&"object"==typeof t&&(t.hasOwnProperty("v")||t.hasOwnProperty("t")||t.hasOwnProperty("value")||t.hasOwnProperty("type"))?(e=t.hasOwnProperty("t")?t.t:t.type,r=t.hasOwnProperty("v")?t.v:t.value):(e=le.toHex(t,!0),r=le.toHex(t),e.startsWith("int")||e.startsWith("uint")||(e="bytes")),!e.startsWith("int")&&!e.startsWith("uint")||"string"!=typeof r||/^(-)?0x/i.test(r)||(r=new It(r)),Array.isArray(r)){if((i=function(t){var e=/^\D+\d*\[(\d+)\]$/.exec(t);return e?parseInt(e[1],10):null}(e))&&r.length!==i)throw new Error(e+" is not matching the given array "+JSON.stringify(r));i=r.length}return Array.isArray(r)?r.map(function(t){return de(e,t,i).toString("hex").replace("0x","")}).join(""):de(e,r,i).toString("hex").replace("0x","")},me={soliditySha3:function(){var t=Array.prototype.slice.call(arguments),e=t.map(fe);return le.sha3("0x"+e.join(""))},soliditySha3Raw:function(){return le.sha3Raw("0x"+Array.prototype.slice.call(arguments).map(fe).join(""))},encodePacked:function(){var t=Array.prototype.slice.call(arguments),e=t.map(fe);return"0x"+e.join("").toLowerCase()}},ge=a.randomBytes,pe=function(t,e){var i=[];return e.forEach(function(e){if("object"==typeof e.components){if("tuple"!==e.type.substring(0,5))throw new Error("components found but type is not tuple; report on GitHub");var r="",n=e.type.indexOf("[");n>=0&&(r=e.type.substring(n));var s=pe(t,e.components);Array.isArray(s)&&t?i.push("tuple("+s.join(",")+")"+r):i.push(t?"("+s+")":"("+s.join(",")+")"+r)}else i.push(e.type)}),i},we=function(t){if(!le.isHexStrict(t))throw new Error("The parameter must be a valid HEX string.");var e="",i=0,r=t.length;for("0x"===t.substring(0,2)&&(i=2);i<r;i+=2){var n=parseInt(t.substr(i,2),16);e+=String.fromCharCode(n)}return e},ve=function(t){if(!t)return"0x00";for(var e="",i=0;i<t.length;i++){var r=t.charCodeAt(i).toString(16);e+=r.length<2?"0"+r:r}return"0x"+e},ye=function(t){if(t=t?t.toLowerCase():"ether",!St.unitMap[t])throw new Error('This unit "'+t+"\" doesn't exist, please use the one of the following units"+JSON.stringify(St.unitMap,null,2));return t},Me={_fireError:function(t,e,i,r,n){return!t||"object"!=typeof t||t instanceof Error||!t.data||((t.data&&"object"==typeof t.data||Array.isArray(t.data))&&(t.data=JSON.stringify(t.data,null,2)),t=t.message+"\n"+t.data),"string"==typeof t&&(t=new Error(t)),"function"==typeof r&&r(t,n),"function"==typeof i&&((e&&"function"==typeof e.listeners&&e.listeners("error").length||"function"==typeof r)&&e.catch(function(){}),setTimeout(function(){i(t)},1)),e&&"function"==typeof e.emit&&setTimeout(function(){e.emit("error",t,n),e.removeAllListeners()},1),e},_jsonInterfaceMethodToString:function(t){return t&&"object"==typeof t&&t.name&&-1!==t.name.indexOf("(")?t.name:t.name+"("+pe(!1,t.inputs).join(",")+")"},_flattenTypes:pe,randomHex:function(t){return"0x"+ge(t).toString("hex")},BN:le.BN,isBN:le.isBN,isBigNumber:le.isBigNumber,isHex:le.isHex,isHexStrict:le.isHexStrict,sha3:le.sha3,sha3Raw:le.sha3Raw,keccak256:le.sha3,soliditySha3:me.soliditySha3,soliditySha3Raw:me.soliditySha3Raw,encodePacked:me.encodePacked,isAddress:le.isAddress,checkAddressChecksum:le.checkAddressChecksum,toChecksumAddress:function(t){if(void 0===t)return"";if(!/^(0x)?[0-9a-f]{40}$/i.test(t))throw new Error('Given address "'+t+'" is not a valid Ethereum address.');t=t.toLowerCase().replace(/^0x/i,"");for(var e=le.sha3(t).replace(/^0x/i,""),i="0x",r=0;r<t.length;r++)parseInt(e[r],16)>7?i+=t[r].toUpperCase():i+=t[r];return i},toHex:le.toHex,toBN:le.toBN,bytesToHex:le.bytesToHex,hexToBytes:le.hexToBytes,hexToNumberString:le.hexToNumberString,hexToNumber:le.hexToNumber,toDecimal:le.hexToNumber,numberToHex:le.numberToHex,fromDecimal:le.numberToHex,hexToUtf8:le.hexToUtf8,hexToString:le.hexToUtf8,toUtf8:le.hexToUtf8,stripHexPrefix:le.stripHexPrefix,utf8ToHex:le.utf8ToHex,stringToHex:le.utf8ToHex,fromUtf8:le.utf8ToHex,hexToAscii:we,toAscii:we,asciiToHex:ve,fromAscii:ve,unitMap:St.unitMap,toWei:function(t,e){if(e=ye(e),!le.isBN(t)&&"string"!=typeof t)throw new Error("Please pass numbers as strings or BN objects to avoid precision errors.");return le.isBN(t)?St.toWei(t,e):St.toWei(t,e).toString(10)},fromWei:function(t,e){if(e=ye(e),!le.isBN(t)&&"string"!=typeof t)throw new Error("Please pass numbers as strings or BN objects to avoid precision errors.");return le.isBN(t)?St.fromWei(t,e):St.fromWei(t,e).toString(10)},padLeft:le.leftPad,leftPad:le.leftPad,padRight:le.rightPad,rightPad:le.rightPad,toTwosComplement:le.toTwosComplement,isBloom:le.isBloom,isUserEthereumAddressInBloom:le.isUserEthereumAddressInBloom,isContractAddressInBloom:le.isContractAddressInBloom,isTopic:le.isTopic,isTopicInBloom:le.isTopicInBloom,isInBloom:le.isInBloom,compareBlockNumbers:function(t,e){if(t==e)return 0;if("genesis"!=t&&"earliest"!=t&&0!=t||"genesis"!=e&&"earliest"!=e&&0!=e){if("genesis"==t||"earliest"==t)return-1;if("genesis"==e||"earliest"==e)return 1;if("latest"==t)return"pending"==e?-1:1;if("latest"===e)return"pending"==t?1:-1;if("pending"==t)return 1;if("pending"==e)return-1;{let i=new It(t),r=new It(e);return i.lt(r)?-1:i.eq(r)?0:1}}return 0},toNumber:le.toNumber};function be(t,e,i){let r=!1;if("allow"===i){if(t&&t.allow){const i=t.allow.find(t=>t.type===e);r=i&&i.values.length>0}return r}if(t&&t.deny){const i=t.deny.find(t=>t.type===e);r=i&&i.values.length>0}return r}function Ae(t,e,i){const r=be(t.credentials,e,i);return"allow"===i?(r&&(t.credentials.allow=t.credentials.allow.filter(t=>t.type!==e)),t.credentials&&!t.credentials.allow&&(t.credentials={deny:t.credentials&&t.credentials.deny})):(r&&(t.credentials.deny=t.credentials.deny.filter(t=>t.type!==e)),t.credentials&&!t.credentials.deny&&(t.credentials={allow:t.credentials&&t.credentials.allow})),t}function xe(t,e,i,r){const n=be(t.credentials,e,r);return"allow"===r?n?t.credentials.allow.find(t=>{t.type===e&&(t.values=i)}):t=Ee(t,e,i,r):n?t.credentials.deny.find(t=>{t.type===e&&(t.values=i)}):t=Ee(t,e,i,r),t}function Ee(t,e,i,r){const n={type:e,values:i};return"allow"===r?t.credentials&&t.credentials.allow?t.credentials.allow.push(n):t.credentials={allow:[n],deny:t.credentials&&t.credentials.deny}:t.credentials&&t.credentials.deny?t.credentials.deny.push(n):t.credentials={allow:t.credentials&&t.credentials.allow,deny:[n]},t}class Re extends st{constructor(...t){super(...t),this.baseUrl=void 0}static async getInstance(t){var e;const i=new Re;return i.setInstanceConfig(t),await i.setBaseUrl(null==(e=t.config)?void 0:e.rbacUri),i}async setBaseUrl(t){this.baseUrl=t}get url(){return this.baseUrl}getIsPermitArgs(t,e,i,r,n,s){return"consume"===e?{component:t,eventType:e,authService:i,did:s,credentials:{type:n,value:r}}:{component:t,eventType:e,authService:i,credentials:{type:n,value:r}}}async isPermit(t,e,i,r,n,s){if(!this.url)return!0;const o=this.getIsPermitArgs(t,e,i,r,n,s);try{const t=await this.ocean.utils.fetch.post(this.url,JSON.stringify(o));let e=await t.json();return e=JSON.stringify(e),"true"===e}catch(t){throw this.logger.error(t),new Error("ERROR: Asset URL not found or not available.")}}}!function(t){t[t.CreatingDataToken=0]="CreatingDataToken",t[t.DataTokenCreated=1]="DataTokenCreated",t[t.EncryptingFiles=2]="EncryptingFiles",t[t.FilesEncrypted=3]="FilesEncrypted",t[t.StoringDdo=4]="StoringDdo",t[t.DdoStored=5]="DdoStored"}(ae||(ae={})),function(t){t[t.TransferDataToken=0]="TransferDataToken"}(he||(he={}));class ke extends st{static async getInstance(t){const e=new ke;return e.setInstanceConfig(t),e}create(t,e,i=[],r,n,s,o,a){return r&&!Me.isAddress(r)?(this.logger.error(`Passed Data Token address ${r} is not valid. Aborting publishing.`),null):(this.logger.log("Creating asset"),new P(async h=>{0===i.length&&this.logger.log("You have no services. Are you sure about this?");const{datatokens:u}=this.ocean;if(!r){if(this.logger.log("Creating datatoken"),h.next(ae.CreatingDataToken),r=await u.create("",e.getId(),n,s,o),!Me.isAddress(r))return this.logger.error(`Created Data Token address ${r} is not valid. Aborting publishing.`),null;this.logger.log(`DataToken ${r} created`),h.next(ae.DataTokenCreated)}const l=at.generate(r);let c;this.logger.log("Encrypting files"),h.next(ae.EncryptingFiles),a?(c=await mt.getInstance(this.instanceConfig),await c.setBaseUrl(a)):c=this.ocean.provider;const d=await c.encrypt(l.getDid(),t.main.files,e);this.logger.log("Files encrypted"),h.next(ae.FilesEncrypted);let f=0;const m=new lt({id:l.getDid(),dataToken:r,authentication:[{type:"RsaSignatureAuthentication2018",publicKey:l.getDid()}],publicKey:[{id:l.getDid(),type:"EthereumECDSAKey",owner:e.getId()}],service:[{type:"metadata",attributes:A({status:{isListed:!0,isRetired:!1,isOrderDisabled:!1}},t,{encryptedFiles:d,main:A({},t.main,{files:t.main.files.map((t,e)=>A({},t,{index:e,url:void 0}))})})},...i].reverse().filter(({type:t},e,i)=>i.findIndex(({type:e})=>e===t)===e).reverse().map(t=>A({},t,{index:f++}))});return await m.addProof(this.ocean,e.getId()),m.dataTokenInfo={name:await u.getName(r),symbol:await u.getSymbol(r),address:r,cap:parseFloat(await u.getCap(r))},m}))}async resolve(t){return this.ocean.metadataCache.retrieveDDO(t)}async editMetadata(t,e){if(!t)return null;for(let r=0;r<t.service.length;r++){var i;"metadata"===t.service[r].type&&(e.title&&(t.service[r].attributes.main.name=e.title),e.author&&(t.service[r].attributes.main.author=e.author),t.service[r].attributes.additionalInformation||(t.service[r].attributes.additionalInformation=Object()),e.description&&(t.service[r].attributes.additionalInformation.description=e.description),t.service[r].attributes.additionalInformation.links=e.links?e.links:[],void 0!==(null==(i=e.status)?void 0:i.isOrderDisabled)&&(t.service[r].attributes.status?t.service[r].attributes.status.isOrderDisabled=e.status.isOrderDisabled:t.service[r].attributes.status={isOrderDisabled:e.status.isOrderDisabled}))}return t}async updateCredentials(t,e,i,r){let n;return n=i&&i.length>0?xe(t,e,i,"allow"):Ae(t,e,"allow"),n=r&&r.length>0?xe(t,e,r,"deny"):Ae(t,e,"deny"),n}checkCredential(t,e,i){let r=0,n="All good",s=!0;if(t.credentials){if(t.credentials.allow&&t.credentials.allow.length>0){const o=t.credentials.allow.find(t=>t.type===e);o&&!o.values.includes(i)&&(r=2,n="Access is denied, your wallet address is not found on allow list",s=!1)}if(t.credentials.deny&&t.credentials.deny.length>0){const o=t.credentials.deny.find(t=>t.type===e);o&&o.values.includes(i)&&(r=3,n="Access is denied, your wallet address is found on deny list",s=!1)}}return{status:r,message:n,result:s}}async publishDdo(t,e,i=!1){return await this.ocean.onChainMetadata.publish(t.id,t,e,i)}async updateMetadata(t,e){return await this.ocean.onChainMetadata.update(t.id,t,e)}async editServiceTimeout(t,e,i){return t?void 0===t.service[e]||i<0?null:(t.service[e].attributes.main.timeout=parseInt(i.toFixed()),t):null}async creator(t){const{did:e,ddo:i}=await et(t,this.ocean),r=i.getChecksum(),{creator:n,signatureValue:s}=i.proof,o=await this.ocean.utils.signature.verifyText(r,s);return o.toLowerCase()!==n.toLowerCase()&&this.logger.warn(`Owner of ${e} doesn't match. Expected ${n} instead of ${o}.`),n}async getServiceByType(t,e){const{ddo:i}=await et(t,this.ocean);let r;return i.service.forEach(t=>{t.type.toString()===e&&(r=t)}),r}async getServiceByIndex(t,e){const{ddo:i}=await et(t,this.ocean);let r;return i.service.forEach(t=>{t.index===e&&(r=t)}),r}async query(t){return this.ocean.metadataCache.queryMetadata(t)}async createAccessServiceAttributes(t,e,i,r=0,n,s){const o={type:"access",index:2,serviceEndpoint:n||this.ocean.provider.url,attributes:{main:{creator:t.getId(),datePublished:i,cost:e,timeout:r,name:"dataAssetAccess"}}};return null!=s&&s.userCustomParameters&&(o.attributes.userCustomParameters=s.userCustomParameters),null!=s&&s.algoCustomParameters&&(o.attributes.algoCustomParameters=s.algoCustomParameters),o}async initialize(t,e,i,r=-1,n,s){const o=await mt.getInstance(this.instanceConfig);await o.setBaseUrl(n);const a=await o.initialize(t,r,e,i,s);return null===a?null:JSON.parse(a)}async order(t,e,i,r=-1,n,s,o,a="json",h=!0){let u;const{ddo:l}=await et(t,this.ocean),c=await this.isConsumable(l,i,"address",a);if(!c.result)throw new Error("Order asset failed, "+c.message);if(s||(s=i),-1===r?(u=await this.getServiceByType(l,e),r=u.index):(u=await this.getServiceByIndex(l,r),e=u.type),!await this.isUserCustomParametersValid(u.attributes.userCustomParameters,o))throw new Error("Order asset failed, Missing required fiels in userCustomParameters");try{const t=await this.initialize(l,e,i,r,u.serviceEndpoint,o);if(!t)throw new Error("Order asset failed, Failed to initialize service to compute totalCost for ordering");if(h){const e=await this.ocean.datatokens.getPreviousValidOrders(t.dataToken,t.numTokens,r,u.attributes.main.timeout,s);if(e)return e}const a=new X(await this.ocean.datatokens.balance(t.dataToken,i)),c=new X(String(t.numTokens));if(a.isLessThan(c))throw this.logger.error("ERROR: Not enough funds Needed "+c.toString()+" but balance is "+a.toString()),new Error("ERROR: Not enough funds Needed "+c.toString()+" but balance is "+a.toString());const d=await this.ocean.datatokens.startOrder(t.dataToken,s,String(t.numTokens),r,n,i);if(d)return d.transactionHash}catch(t){throw this.logger.error(`ERROR: Failed to order a service : ${t.message}`),new Error(`${t.message}`)}}async download(t,e,i,r,n){const{did:s,ddo:o}=await et(t,this.ocean),{attributes:a}=o.findServiceByType("metadata"),h=o.findServiceByType("access"),{files:u}=a.main,{serviceEndpoint:l}=h;if(!l)throw new Error("Consume asset failed, service definition is missing the `serviceEndpoint`.");this.logger.log("Consuming files"),n=n?`${n}/datafile.${o.shortId()}.${h.index}/`:void 0;const c=await mt.getInstance(this.instanceConfig);return await c.setBaseUrl(l),await c.download(s,e,i,h.type,h.index.toString(),n,r,u),!0}async simpleDownload(t,e,i,r){let n=e;n+=`?consumerAddress=${r}`,n+=`&tokenAddress=${t}`,n+=`&transferTxId=${i}`;const s=new ft(this.logger);try{await s.downloadFile(n)}catch(t){throw this.logger.error("Error consuming assets"),this.logger.error(t),t}return e}async getOrderHistory(t,e,i){const r=[],n=t.getId().toLowerCase(),{datatokens:s}=this.ocean,o="0x000000000000000000000000"+n.substring(2),a=s.getStartOrderEventSignature(),h=await this.web3.eth.getPastLogs({topics:[a,null,o],fromBlock:i||0,toBlock:"latest"});for(let t=0;t<h.length;t++){const i={dtAddress:h[t].address,timestamp:0,transactionHash:h[t].transactionHash,amount:null,consumer:"0x"+h[t].topics[1].substring(h[t].topics[1].length-40),payer:"0x"+h[t].topics[2].substring(h[t].topics[2].length-40)};try{const n=this.web3.eth.abi.decodeParameters(["uint256","uint256","uint256","uint256"],h[t].data);i.serviceId=parseInt(n[1]),i.timestamp=parseInt(n[2]),i.amount=this.web3.utils.fromWei(n[0]),i.did=T(C(i.dtAddress));const s=await this.getServiceByIndex(i.did,i.serviceId);i.serviceType=s.type,(!e||e&&e===s.type)&&r.push(i)}catch(t){}}return r}async isConsumable(t,e,i,r){var n,s;if(!t)throw new Error("ERROR: DDO does not exist");const o={status:2,message:"Consume access is denied.",result:!1};if(null!=(n=t.findServiceByType("metadata").attributes.status)&&n.isOrderDisabled)return{status:1,message:"Ordering this asset has been temporarily disabled by the publisher.",result:!1};const a=this.instanceConfig;if(e&&null!=a&&null!=(s=a.config)&&s.rbacUri){const n=await Re.getInstance(this.instanceConfig);if(!await n.isPermit("market","consume",r,e,i,t.id))return o}return{status:0,message:"All good",result:!0}}async isUserCustomParametersValid(t,e){if(t)for(const i of t){const t=i.name;if(!e||!e[t])return this.logger.error("Missing key: "+t+" from customData"),!1}return!0}}!function(t){t.Loading="Loading",t.Unknown="Unknown",t.Stopped="Stopped",t.Working="Working"}(ue||(ue={}));class Se extends st{static async getInstance(t){const e=new Se;return e.setInstanceConfig(t),e}async get(){const t={};t.lib={name:"Lib",version:"0.20.0",commit:"f79194faf37e447c07963c1114f0481b0b18030b",status:ue.Working};try{const{software:e,version:i}=await this.ocean.metadataCache.getVersionInfo();t.metadataCache={name:e,status:ue.Working,version:i}}catch(e){t.metadataCache={name:"MetadataCache",status:ue.Stopped}}const e=Object.values(t);return t.status={ok:!e.find(({status:t})=>t!==ue.Working)},t}}class Ie{constructor(t,e){this.web3=void 0,this.logger=void 0,this.web3=t,this.logger=e}async signText(t,e,i){const r=this.web3&&this.web3.currentProvider&&this.web3.currentProvider.isMetaMask;try{return await this.web3.eth.personal.sign(t,e,i)}catch(i){if(r)throw i;this.logger.warn("Error on personal sign."),this.logger.warn(i);try{return await this.web3.eth.sign(t,e)}catch(t){throw this.logger.error("Error on sign."),this.logger.error(t),new Error("Error executing personal sign")}}}async signWithHash(t,e,i){const r=this.web3.utils.utf8ToHex(t),n=this.web3&&this.web3.currentProvider&&this.web3.currentProvider.isMetaMask;try{return await this.web3.eth.personal.sign(r,e,i)}catch(t){if(n)throw t;this.logger.warn("Error on personal sign."),this.logger.warn(t);try{return await this.web3.eth.sign(r,e)}catch(t){throw this.logger.error("Error on sign."),this.logger.error(t),new Error("Error executing personal sign")}}}async verifyText(t,e){return this.web3.eth.personal.ecRecover(t,e)}async getHash(t){let e="";for(let i=0;i<t.length;i++)e+=""+t.charCodeAt(i).toString(16);return"0x"+e}async signForAquarius(t,e){const i=await this.getHash(t),r=this.web3&&this.web3.currentProvider&&this.web3.currentProvider.isMetaMask;try{return this.web3.eth.personal.sign(i,e.getId(),e.getPassword())}catch(t){if(r)throw t;return this.logger.warn("Error on personal sign."),this.logger.warn(t),null}}}class Te extends st{constructor(...t){super(...t),this.signature=void 0,this.fetch=void 0}static async getInstance(t){const e=new Te;return e.setInstanceConfig(t),e.signature=new Ie(t.web3,t.logger),e.fetch=new ft(t.logger),e}}const Oe="/api/v1/aquarius/assets/ddo";class Be{get url(){return this.metadataCacheUri}constructor(t,e){this.fetch=void 0,this.logger=void 0,this.metadataCacheUri=void 0,this.fetch=new ft(e),this.logger=e,this.metadataCacheUri=t}async getVersionInfo(){return(await this.fetch.get(this.url)).json()}async getAccessUrl(t,e){return await this.fetch.post(`${t.service_endpoint}/${t.resource_id}`,e).then(t=>t.ok?t.text():(this.logger.error("Failed: ",t.status,t.statusText),null)).then(t=>(this.logger.error("Success accessing consume endpoint: ",t),t)).catch(t=>(this.logger.error("Error fetching the data asset consumption url: ",t),null))}async queryMetadata(t){return await this.fetch.post(`${this.url}/api/v1/aquarius/assets/query`,JSON.stringify(t)).then(t=>t.ok?t.json():(this.logger.error("queryMetadata failed:",t.status,t.statusText),null)).then(t=>t).catch(t=>(this.logger.error("Error fetching querying metadata: ",t),null))}async encryptDDO(t){const e=`${this.url}/api/v1/aquarius/assets/ddo/encryptashex `;return await this.fetch.postWithOctet(e,t).then(e=>e.ok?e.text():(this.logger.error("encryptDDO failed:",e.status,e.statusText,t),null)).catch(t=>(this.logger.error("Error encryptDDO: ",t),null))}async validateMetadata(t){const e={valid:!1},i=tt(t)?"/validate-remote":"/validate";try{const r=await this.fetch.post(`${this.url}${Oe}${i}`,JSON.stringify(t));if(r.ok){const t=await r.json();!0===t?e.valid=!0:e.errors=t}else this.logger.error("validate Metadata failed:",r.status,r.statusText)}catch(t){this.logger.error("Error validating metadata: ",t)}return e}async retrieveDDO(t,e){t=t&&at.parse(t);const i=e||`${this.url}${Oe}/${t.getDid()}`;return await this.fetch.get(i).then(e=>e.ok?e.json():(this.logger.log("retrieveDDO failed:",e.status,e.statusText,t),null)).then(t=>new lt(t)).catch(t=>(this.logger.error("Error fetching querying metadata: ",t),null))}async retrieveDDOByUrl(t){return this.retrieveDDO(void 0,t)}getServiceEndpoint(t){return`${this.url}/api/v1/aquarius/assets/ddo/did:op:${t.getId()}`}getURI(){return`${this.url}`}sleep(t){return new Promise(e=>{setTimeout(e,t)})}async waitForAqua(t,e){let i=0;do{try{const i=await h(this.getURI()+"/api/v1/aquarius/assets/ddo/"+t);if(i.ok){if(!e)break;{const t=await i.json();if(t.event&&t.event.txid===e)break}}}catch(t){}await this.sleep(1500),i++}while(i<100)}}class Ce{constructor(t,e,i=null,r=null,n,s){this.GASLIMIT_DEFAULT=1e6,this.DDOContractAddress=void 0,this.DDOContractABI=void 0,this.web3=void 0,this.DDOContract=null,this.logger=void 0,this.metadataCache=void 0,this.config=void 0,this.web3=t,this.config=s,this.DDOContractAddress=i,this.DDOContractABI=r||u.abi,t&&(this.DDOContract=Y(new this.web3.eth.Contract(this.DDOContractABI,this.DDOContractAddress),this.config)),this.logger=e,this.metadataCache=n}async compressDDO(t){return l.disableEndMark=!0,l.compress(t,9)}async publish(t,e,i,r=!1,n=!0){if(n&&!(await this.metadataCache.validateMetadata(e)).valid)throw new Error("DDO has failed validation");const s=await this.prepareRawData(e,r);if(s)return this.publishRaw(B(t),s.flags,s.data,i);throw new Error("Could not prepare raw data for publish")}async update(t,e,i,r=!1,n=!0){if(n&&!(await this.metadataCache.validateMetadata(e)).valid)throw new Error("DDO has failed validation");const s=await this.prepareRawData(e,r);if(s)return this.updateRaw(B(t),s.flags,s.data,i);throw new Error("Could not prepare raw data for udate")}async prepareRawData(t,e=!1){let i=0,r=lt.serialize(t);if(!1===e)r=await this.compressDDO(r),i|=1,r=this.getHex(r);else{if(r=await this.metadataCache.encryptDDO(r),!r)return null;i|=2}return{flags:i,data:r}}async publishRaw(t,e,i,r){if(!this.DDOContract)return this.logger.error("ERROR: Missing DDOContract"),null;const n=this.GASLIMIT_DEFAULT;let s;try{s=await this.DDOContract.methods.create(B(t),e,i).estimateGas({from:r},(t,e)=>t?n:e)}catch(t){s=n}try{return await this.DDOContract.methods.create(B(t),e,i).send({from:r,gas:s+1,gasPrice:await Q(this.web3,this.config)})}catch(t){return this.logger.error(`ERROR: Failed to publish raw DDO : ${t.message}`),null}}async updateRaw(t,e,i,r){if(!this.DDOContract)return this.logger.error("ERROR: Missing DDOContract"),null;const n=this.GASLIMIT_DEFAULT;let s;try{s=await this.DDOContract.methods.update(B(t),e,i).estimateGas({from:r},(t,e)=>t?n:e)}catch(t){s=n}try{return await this.DDOContract.methods.update(B(t),e,i).send({from:r,gas:s+1,gasPrice:await Q(this.web3,this.config)})}catch(t){return this.logger.error(`ERROR: Failed to update raw DDO : ${t.message}`),null}}async transferOwnership(t,e,i){if(!this.DDOContract)return null;try{return await this.DDOContract.methods.transferOwnership(B(t),e).send({from:i})}catch(t){return this.logger.error(`ERROR: Failed to transfer DDO ownership : ${t.message}`),null}}getHex(t){const e=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];let i="";try{for(let r=0;r<t.length;r++)i+=""+e[t[r]>>4&15]+e[15&t[r]]}catch(t){this.logger.error(`ERROR: Failed to get hex message value : ${t.message}`)}return"0x"+i}}class _e{constructor(t,e,i,r,n,s){this.GASLIMIT_DEFAULT=1e6,this.factoryAddress=void 0,this.factoryABI=void 0,this.datatokensABI=void 0,this.web3=void 0,this.logger=void 0,this.startBlock=void 0,this.config=void 0,this.factoryAddress=t,this.factoryABI=e||c.abi,this.datatokensABI=i||d.abi,this.web3=r,this.logger=n,this.config=s,this.startBlock=s&&s.startBlock||0}generateDtName(t){const{name:e,symbol:i}=rt(t);return{name:e,symbol:i}}async create(t,e,i,r,n){i||(i="1000"),r&&n||({name:r,symbol:n}=this.generateDtName());const s=Y(new this.web3.eth.Contract(this.factoryABI,this.factoryAddress,{from:e}),this.config),o=this.GASLIMIT_DEFAULT;let a;try{a=await s.methods.createToken(t,r,n,this.web3.utils.toWei(i)).estimateGas({from:e},(t,e)=>t?o:e)}catch(t){a=o}const h=await s.methods.createToken(t,r,n,this.web3.utils.toWei(i)).send({from:e,gas:a+1,gasPrice:await Q(this.web3,this.config)});let u=null;try{u=h.events.TokenCreated.returnValues[0]}catch(t){this.logger.error(`ERROR: Failed to create datatoken : ${t.message}`)}return u}async approve(t,e,i,r){const n=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:r}),this.config),s=this.GASLIMIT_DEFAULT;let o;try{o=await n.methods.approve(e,this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){o=s}return await n.methods.approve(e,this.web3.utils.toWei(i)).send({from:r,gas:o+1,gasPrice:await Q(this.web3,this.config)})}async mint(t,i,r,n){const s=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:i}),this.config),o=await this.getCap(t);if(new e(o).gte(r)){const t=this.GASLIMIT_DEFAULT;let e;try{e=await s.methods.mint(n||i,this.web3.utils.toWei(r)).estimateGas({from:i},(e,i)=>e?t:i)}catch(i){e=t}return await s.methods.mint(n||i,this.web3.utils.toWei(r)).send({from:i,gas:e+1,gasPrice:await Q(this.web3,this.config)})}throw new Error("Mint amount exceeds cap available")}async transfer(t,e,i,r){return this.transferToken(t,e,i,r)}async transferToken(t,e,i,r){const n=this.web3.utils.toWei(i);return this.transferWei(t,e,n,r)}async transferWei(t,e,i,r){const n=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:r}),this.config),s=this.GASLIMIT_DEFAULT;let o;try{o=await n.methods.transfer(e,i).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){o=s}return await n.methods.transfer(e,i).send({from:r,gas:o+1,gasPrice:await Q(this.web3,this.config)})}async transferFrom(t,e,i,r){const n=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:r}),this.config),s=this.GASLIMIT_DEFAULT;let o;try{o=await n.methods.transferFrom(e,r,this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){o=s}return await n.methods.transferFrom(e,r,this.web3.utils.toWei(i)).send({from:r,gas:o+1,gasPrice:await Q(this.web3,this.config)})}async balance(t,e){const i=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:e}),this.config),r=await i.methods.balanceOf(e).call();return this.web3.utils.fromWei(r)}async allowance(t,e,i){const r=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:i}),this.config),n=await r.methods.allowance(e,i).call();return this.web3.utils.fromWei(n)}async getBlob(t){const e=Y(new this.web3.eth.Contract(this.datatokensABI,t),this.config);return await e.methods.blob().call()}async getName(t){const e=Y(new this.web3.eth.Contract(this.datatokensABI,t),this.config);return await e.methods.name().call()}async getSymbol(t){const e=Y(new this.web3.eth.Contract(this.datatokensABI,t),this.config);return await e.methods.symbol().call()}async getCap(t){const e=Y(new this.web3.eth.Contract(this.datatokensABI,t),this.config),i=await e.methods.cap().call();return this.web3.utils.fromWei(i)}toWei(t){return this.web3.utils.toWei(t)}fromWei(t){return this.web3.utils.fromWei(t)}async startOrder(t,e,i,r,n,s){const o=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:s}),this.config);n||(n="0x0000000000000000000000000000000000000000");try{const t=this.GASLIMIT_DEFAULT;let a;try{a=await o.methods.startOrder(e,this.web3.utils.toWei(i),String(r),n).estimateGas({from:s},(e,i)=>e?t:i)}catch(e){a=t}return await o.methods.startOrder(e,this.web3.utils.toWei(i),String(r),n).send({from:s,gas:a+1,gasPrice:await Q(this.web3,this.config)})}catch(t){throw this.logger.error(`ERROR: Failed to start order : ${t.message}`),new Error(`Failed to start order: ${t.message}`)}}async getPreviousValidOrders(t,e,i,r,n){const s=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:n}),this.config);let o;r>0?(o=await this.web3.eth.getBlockNumber()-r,o<this.startBlock&&(o=this.startBlock)):o=this.startBlock;const a=await s.getPastEvents("OrderStarted",{filter:{consumer:n},fromBlock:o,toBlock:"latest"});for(let t=0;t<a.length;t++)if(String(a[t].returnValues.amount)===this.web3.utils.toWei(String(e))&&String(a[t].returnValues.serviceId)===String(i)&&a[t].returnValues.consumer.toLowerCase()===n.toLowerCase()){if(0===r)return a[t].transactionHash;const e=await this.web3.eth.getBlock(a[t].blockHash),i=new X(e.timestamp).plus(r);if(new X(Math.floor(Date.now()/1e3)).isLessThan(i))return a[t].transactionHash}return null}getStartOrderEventSignature(){const t=this.datatokensABI.find(function(t){if("OrderStarted"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}async proposeMinter(t,e,i){const r=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:i}),this.config),n=this.GASLIMIT_DEFAULT;let s;try{s=await r.methods.proposeMinter(e).estimateGas({from:i},(t,e)=>t?n:e)}catch(t){s=n}try{return await r.methods.proposeMinter(e).send({from:i,gas:s+1,gasPrice:await Q(this.web3,this.config)})}catch(t){return this.logger.error("ERROR: Propose minter failed"),null}}async approveMinter(t,e){const i=Y(new this.web3.eth.Contract(this.datatokensABI,t,{from:e}),this.config),r=this.GASLIMIT_DEFAULT;let n;try{n=await i.methods.approveMinter().estimateGas({from:e},(t,e)=>t?r:e)}catch(t){n=r}try{return await i.methods.approveMinter().send({from:e,gas:n+1,gasPrice:await Q(this.web3,this.config)})}catch(t){return null}}async isMinter(t,e){const i=Y(new this.web3.eth.Contract(this.datatokensABI,t),this.config);return await i.methods.isMinter(e).call()}}class De extends st{getNetworkId(){return this.web3.eth.net.getId()}async getNetworkName(){return this.web3.eth.net.getId().then(t=>{const e=f(t);return e&&e.name?e.name:"Development"})}}var Pe;!function(t){t[t.TransferDataToken=0]="TransferDataToken"}(Pe||(Pe={})),Object.freeze({WarmingUp:1,Started:10,ConfiguringVolumes:20,ProvisioningSuccess:30,DataProvisioningFailed:31,AlgorithmProvisioningFailed:32,RunningAlgorithm:40,FilteringResults:50,PublishingResult:60,Completed:70,Stopped:80,Deleted:90});class Ne extends st{static async getInstance(t){const e=new Ne;return e.setInstanceConfig(t),e}async getComputeAddress(t,e=-1){let i,r="compute";-1===e?(i=await this.ocean.assets.getServiceByType(t,r),e=i.index):(i=await this.ocean.assets.getServiceByIndex(t,e),r=i.type);const{serviceEndpoint:n}=i,s=await mt.getInstance(this.instanceConfig);return await s.setBaseUrl(n),s.computeAddress}async start(t,e,i,r,n,s,o,a,h){s=this.checkOutput(r,s);const{did:u,ddo:l}=await et(t,this.ocean),c=l.findServiceByType("compute"),{serviceEndpoint:d}=c;if(n.serviceIndex){const{ddo:t}=await et(n.did,this.ocean),e=t.findServiceById(n.serviceIndex);if(!await this.ocean.assets.isUserCustomParametersValid(e.attributes.algoCustomParameters,n.algoCustomParameters))return null}if(u&&e){const t=await mt.getInstance(this.instanceConfig);await t.setBaseUrl(d);const l=await t.computeStart(u,r,n,s,e,o,a,i,h);return l?l[0]:null}return null}async stop(t,e,i){const{did:r,ddo:n}=await et(e,this.ocean),s=n.findServiceByType("compute"),{serviceEndpoint:o}=s,a=await mt.getInstance(this.instanceConfig);await a.setBaseUrl(o);const h=await a.computeStop(r,t,i);return h?h[0]:null}async delete(t,e,i){const{did:r,ddo:n}=await et(e,this.ocean),s=n.findServiceByType("compute"),{serviceEndpoint:o}=s,a=await mt.getInstance(this.instanceConfig);await a.setBaseUrl(o);const h=await a.computeDelete(r,t,i);return h?h[0]:null}async status(t,e,i,r,n,s){let o;if(e||r||i){if(!r){if(!i&&!(i=await this.ocean.assets.resolve(e)))throw new Error(`Couldn't resolve the did ${e}`);if(!(r=i.findServiceByType("compute")))throw new Error(`Couldn't find a compute service on the asset with did ${e}`)}const{serviceEndpoint:t}=r;o=await mt.getInstance(this.instanceConfig),await o.setBaseUrl(t)}else o=this.ocean.provider;return await o.computeStatus(e,t,n,s)}async getResult(t,e,i,r,n,s,o){let a;if(n||o||s){if(!o){if(!s&&!(s=await this.ocean.assets.resolve(n)))throw new Error(`Couldn't resolve the did ${n}`);if(!(o=s.findServiceByType("compute")))throw new Error(`Couldn't find a compute service on the asset with did ${n}`)}const{serviceEndpoint:t}=o;a=await mt.getInstance(this.instanceConfig),await a.setBaseUrl(t)}else a=this.ocean.provider;return await a.computeResult(e,i,r,t)}createServerAttributes(t,e,i,r,n,s,o,a){return{serverId:t,serverType:e,cost:i,cpu:r,gpu:n,memory:s,disk:o,maxExecutionTime:a}}createContainerAttributes(t,e,i){return{image:t,tag:e,checksum:i}}createClusterAttributes(t,e){return{type:t,url:e}}createProviderAttributes(t,e,i,r,n){return{type:t,description:e,environment:{cluster:i,supportedServers:n,supportedContainers:r}}}createComputeService(t,e,i,r,n,s,o,a){s||(s=3600);const h={type:"compute",index:3,serviceEndpoint:o||this.ocean.provider.url,attributes:{main:{name:"dataAssetComputingService",creator:t.getId(),datePublished:i,cost:e,timeout:s,provider:r,privacy:{}}}};return n&&(h.attributes.main.privacy=n),null!=a&&a.userCustomParameters&&(h.attributes.userCustomParameters=a.userCustomParameters),null!=a&&a.algoCustomParameters&&(h.attributes.algoCustomParameters=a.algoCustomParameters),h}checkOutput(t,e){return e&&(e.publishAlgorithmLog||e.publishOutput)?{publishAlgorithmLog:e.publishAlgorithmLog,publishOutput:e.publishOutput,providerAddress:e.providerAddress||this.config.providerAddress,providerUri:e.providerUri||this.config.providerUri,metadataUri:e.metadataUri||this.config.metadataCacheUri,nodeUri:e.nodeUri||this.config.nodeUri,owner:e.owner||t.getId()}:{publishAlgorithmLog:!1,publishOutput:!1}}async isOrderable(t,e,i,r){const n=await et(t,this.ocean),s=n.ddo.findServiceById(e);if(!s)return!1;if("compute"===s.type){if(i.meta)return!(!s.attributes.main.privacy||!s.attributes.main.privacy.allowRawAlgorithm)||(this.logger.error("ERROR: This service does not allow raw algorithm"),!1);if(i.did){if(i.serviceIndex){if(!r&&!(r=await this.ocean.assets.resolve(i.did)))throw new Error(`Couldn't resolve the did ${i.did}`);const t=r.findServiceById(i.serviceIndex);if(t&&"compute"===t.type){const e=await mt.getInstance(this.instanceConfig);await e.setBaseUrl(t.serviceEndpoint);const i=await mt.getInstance(this.instanceConfig);if(await i.setBaseUrl(s.serviceEndpoint),e.providerAddress!==i.providerAddress)return this.logger.error("ERROR: Both assets with compute service are not served by the same provider"),!1}}if(s.attributes.main.privacy){if(s.attributes.main.privacy.allowAllPublishedAlgorithms)return!0;if(!s.attributes.main.privacy.publisherTrustedAlgorithms)return!1;let t;for(t of s.attributes.main.privacy.publisherTrustedAlgorithms)if(t.did===i.did){const e=await this.createPublisherTrustedAlgorithmfromDID(i.did);return t.containerSectionChecksum&&t.containerSectionChecksum!==e.containerSectionChecksum?(this.logger.error("ERROR: Algorithm container section was altered since it was added as trusted by "+n.did),!1):!t.filesChecksum||t.filesChecksum===e.filesChecksum||(this.logger.error("ERROR: Algorithm files section was altered since it was added as trusted by "+n.ddo),!1)}return this.logger.error("ERROR: Algorithm "+i.did+" is not allowed by "+n.did),!1}console.error("Algo Index:"+i.serviceIndex)}}return!0}orderAsset(t,e,i,r,n,s,o,a="json",h=!0){return new P(async u=>{const{ddo:l}=await et(e,this.ocean);if(!await this.isOrderable(l,i,r))throw new Error("Dataset order failed, dataset is not orderable with the specified algorithm");const c=l.findServiceById(i);if(!c)throw new Error("Dataset order failed, Could not find service for the DDO");try{return await this.ocean.assets.order(l,c.type,t,-1,n,s,o,a,h)}catch(t){throw this.logger.error(`ERROR: Failed to order: ${t.message}`),new Error(`Failed to order dataset: ${t.message}`)}})}async orderAlgorithm(t,e,i,r=-1,n,s,o,a="json",h=!0){try{return await this.ocean.assets.order(t,e,i,r,n,s,o,a,h)}catch(t){throw this.logger.error(`ERROR: Failed to orderAlgorithm: ${t.message}`),new Error(`Failed to order algorithm: ${t.message}`)}}async editComputePrivacy(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return null;e=i.index}return void 0===t.service[e]||"compute"!==t.service[e].type?null:(t.service[e].attributes.main.privacy.allowRawAlgorithm=i.allowRawAlgorithm,t.service[e].attributes.main.privacy.allowAllPublishedAlgorithms=i.allowAllPublishedAlgorithms,t.service[e].attributes.main.privacy.allowNetworkAccess=i.allowNetworkAccess,t.service[e].attributes.main.privacy.publisherTrustedAlgorithms=i.publisherTrustedAlgorithms,t)}async toggleAllowAllPublishedAlgorithms(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return null;e=i.index}return void 0===t.service[e]||"compute"!==t.service[e].type?null:(t.service[e].attributes.main.privacy.allowAllPublishedAlgorithms=i,t)}async createPublisherTrustedAlgorithmfromDID(t,e){if(!e&&!(e=await this.ocean.assets.resolve(t)))return null;const i=e.findServiceByType("metadata");return i&&i.attributes.main.algorithm&&i.attributes.encryptedFiles&&i.attributes.main.files?{did:t,containerSectionChecksum:m(JSON.stringify(i.attributes.main.algorithm.container)).toString(),filesChecksum:m(i.attributes.encryptedFiles+JSON.stringify(i.attributes.main.files)).toString()}:null}async addTrustedAlgorithmtoAsset(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return null;e=i.index}if(void 0===t.service[e])return null;if("compute"!==t.service[e].type)return null;t.service[e].attributes.main.privacy.publisherTrustedAlgorithms||(t.service[e].attributes.main.privacy.publisherTrustedAlgorithms=[]);const r=await this.createPublisherTrustedAlgorithmfromDID(i);return r&&t.service[e].attributes.main.privacy.publisherTrustedAlgorithms.push(r),t}async isAlgorithmTrusted(t,e,i){if(!t)return!1;if(-1===e){const i=t.findServiceByType("compute");if(!i)return!1;e=i.index}if(void 0===t.service[e])return!1;if("compute"!==t.service[e].type)return!1;if(t.service[e].attributes.main.privacy.allowAllPublishedAlgorithms)return!0;let r;for(r of t.service[e].attributes.main.privacy.publisherTrustedAlgorithms)if(r.did===i)return!0;return!1}async removeTrustedAlgorithmFromAsset(t,e,i){if(!t)return null;if(-1===e){const i=t.findServiceByType("compute");if(!i)return t;e=i.index}return void 0===t.service[e]||"compute"!==t.service[e].type?t:t.service[e].attributes.main.privacy.publisherTrustedAlgorithms?(t.service[e].attributes.main.privacy.publisherTrustedAlgorithms=t.service[e].attributes.main.privacy.publisherTrustedAlgorithms.filter(function(t){return t.did!==i}),t):t}}const Le="115792089237316195423570985008687907853269984665640564039457584007913129639934";var We,Fe,Ue,$e;!function(t){t[t.CreatingPool=0]="CreatingPool",t[t.ApprovingDatatoken=1]="ApprovingDatatoken",t[t.ApprovingOcean=2]="ApprovingOcean",t[t.SetupPool=3]="SetupPool"}(We||(We={}));class je extends class extends class{constructor(t,e,i=null,r=null,n){this.GASLIMIT_DEFAULT=1e6,this.web3=null,this.factoryABI=void 0,this.factoryAddress=void 0,this.logger=void 0,this.config=void 0,this.web3=t,this.factoryABI=i||p.abi,r&&(this.factoryAddress=r),this.logger=e,this.config=n}async createPool(t){if(null===this.web3)return this.logger.error("ERROR: Web3 object is null"),null;if(null===this.factoryAddress)return this.logger.error("ERROR: bfactoryAddress is null"),null;const e=Y(new this.web3.eth.Contract(this.factoryABI,this.factoryAddress,{from:t}),this.config);let i=null;const r=this.GASLIMIT_DEFAULT;let n;try{n=await e.methods.newBPool().estimateGas({from:t},(t,e)=>t?r:e)}catch(t){this.logger.log("Error estimate gas newBPool"),this.logger.log(t),n=r}try{i=await e.methods.newBPool().send({from:t,gas:n+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to create new pool: ${t.message}`)}return i}}{constructor(t,e,i=null,r=null,n=null,s){super(t,e,i,n,s),this.poolABI=void 0,this.poolABI=r||g.abi}async createPool(t){return await super.createPool(t)}async setup(t,e,i,r,n,s,o,a,h){const u=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let l=null;const c=this.GASLIMIT_DEFAULT;let d;try{d=await u.methods.setup(i,r,n,s,o,a,h).estimateGas({from:t},(t,e)=>t?c:e)}catch(t){d=c}try{l=await u.methods.setup(i,r,n,s,o,a,h).send({from:t,gas:d,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to setup a pool: ${t.message}`)}return l}async allowance(t,e,i){const r=Y(new this.web3.eth.Contract(d.abi,t,{from:i}),this.config),n=await r.methods.allowance(e,i).call();return this.web3.utils.fromWei(n)}async approve(t,i,r,n,s=!1){const o=Y(new this.web3.eth.Contract([{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"}],i,{from:t}),this.config);if(!s){const s=await this.allowance(i,t,r);if(new e(this.web3.utils.toWei(s)).greaterThanOrEqualTo(n))return s}let a=null;const h=this.GASLIMIT_DEFAULT;let u;try{u=await o.methods.approve(r,n).estimateGas({from:t},(t,e)=>t?h:e)}catch(t){u=h}try{a=await o.methods.approve(r,n).send({from:t,gas:u+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERRPR: Failed to approve spender to spend tokens : ${t.message}`)}return a}async sharesBalance(t,e){let i=null;try{const r=Y(new this.web3.eth.Contract(this.poolABI,e),this.config),n=await r.methods.balanceOf(t).call();i=this.web3.utils.fromWei(n)}catch(t){this.logger.error(`ERROR: Failed to get shares of pool : ${t.message}`)}return i}async addToPool(t,e,i){const r=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let n;for(n of i)try{await this.approve(t,n.address,e,this.web3.utils.toWei(`${n.amount}`)),await r.methods.bind(n.address,this.web3.utils.toWei(n.amount),this.web3.utils.toWei(n.weight)).send({from:t,gas:this.GASLIMIT_DEFAULT,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to add tokens to pool: ${t.message}`)}}async setSwapFee(t,e,i){const r=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let n=null;try{n=await r.methods.setSwapFee(this.web3.utils.toWei(i)).send({from:t,gas:this.GASLIMIT_DEFAULT,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to set pool swap fee: ${t.message}`)}return n}async finalize(t,e){const i=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let r=null;try{r=await i.methods.finalize().send({from:t,gas:this.GASLIMIT_DEFAULT,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to finalize pool: ${t.message}`)}return r}async getNumTokens(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{i=await e.methods.getNumTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get number of tokens: ${t.message}`)}return i}async getPoolSharesTotalSupply(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{const t=await e.methods.totalSupply().call();i=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get total supply of pool shares: ${t.message}`)}return i}async getCurrentTokens(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{i=await e.methods.getCurrentTokens().call()}catch(t){this.logger.error(`ERROR: Failed to get tokens composing this pool: ${t.message}`)}return i}async getFinalTokens(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{i=await e.methods.getFinalTokens().call()}catch(t){this.logger.error("ERROR: Failed to get the final tokens composing this pool")}return i}async getController(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{i=await e.methods.getController().call()}catch(t){this.logger.error(`ERROR: Failed to get pool controller address: ${t.message}`)}return i}async setController(t,e,i){const r=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let n=null;try{n=await r.methods.setController(i).send({from:t,gas:this.GASLIMIT_DEFAULT})}catch(t){this.logger.error(`ERROR: Failed to set pool controller: ${t.message}`)}return n}async isBound(t,e){const i=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let r=null;try{r=await i.methods.isBound(e).call()}catch(t){this.logger.error(`ERROR: Failed to check whether a token bounded to a pool. ${t.message}`)}return r}async getReserve(t,e){let i=null;try{const r=Y(new this.web3.eth.Contract(this.poolABI,t),this.config),n=await r.methods.getBalance(e).call();i=this.web3.utils.fromWei(n)}catch(t){this.logger.error(`ERROR: Failed to get how many tokens are in the pool: ${t.message}`)}return i}async isFinalized(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{i=await e.methods.isFinalized().call()}catch(t){this.logger.error(`ERROR: Failed to check whether pool is finalized: ${t.message}`)}return i}async getSwapFee(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{const t=await e.methods.getSwapFee().call();i=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get pool fee: ${t.message}`)}return i}async getNormalizedWeight(t,e){const i=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let r=null;try{const t=await i.methods.getNormalizedWeight(e).call();r=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to get normalized weight of a token: ${t.message}`)}return r}async getDenormalizedWeight(t,e){const i=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let r=null;try{const t=await i.methods.getDenormalizedWeight(e).call();r=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get denormalized weight of a token in pool")}return r}async getTotalDenormalizedWeight(t){const e=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let i=null;try{const t=await e.methods.getTotalDenormalizedWeight().call();i=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get total denormalized weight in pool")}return i}async swapExactAmountIn(t,e,i,r,n,s,o){const a=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let h=null;const u=this.GASLIMIT_DEFAULT;let l;try{l=await a.methods.swapExactAmountIn(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Le).estimateGas({from:t},(t,e)=>t?u:e)}catch(t){this.logger.log("Error estimate gas swapExactAmountIn"),this.logger.log(t),l=u}try{h=await a.methods.swapExactAmountIn(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Le).send({from:t,gas:l+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount in : ${t.message}`)}return h}async swapExactAmountOut(t,e,i,r,n,s,o){const a=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let h=null;const u=this.GASLIMIT_DEFAULT;let l;try{l=await a.methods.swapExactAmountOut(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Le).estimateGas({from:t},(t,e)=>t?u:e)}catch(t){l=u,this.logger.log("Error estimate gas swapExactAmountIn"),this.logger.log(t)}try{h=await a.methods.swapExactAmountOut(i,this.web3.utils.toWei(r),n,this.web3.utils.toWei(s),o?this.web3.utils.toWei(o):Le).send({from:t,gas:l+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to swap exact amount out: ${t.message}`)}return h}async joinPool(t,e,i,r){const n=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config),s=[];let o;for(o of r)s.push(this.web3.utils.toWei(o));let a=null;const h=this.GASLIMIT_DEFAULT;let u;try{u=await n.methods.joinPool(this.web3.utils.toWei(i),s).estimateGas({from:t},(t,e)=>t?h:e)}catch(t){u=h}try{a=await n.methods.joinPool(this.web3.utils.toWei(i),s).send({from:t,gas:u+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to join pool: ${t.message}`)}return a}async exitPool(t,e,i,r){const n=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config),s=[];let o;for(o of r)s.push(this.web3.utils.toWei(o));let a=null;const h=this.GASLIMIT_DEFAULT;let u;try{u=await n.methods.exitPool(this.web3.utils.toWei(i),s).estimateGas({from:t},(t,e)=>t?h:e)}catch(t){u=h}try{a=await n.methods.exitPool(this.web3.utils.toWei(i),s).send({from:t,gas:u,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to exit pool: ${t.message}`)}return a}async joinswapExternAmountIn(t,e,i,r,n){const s=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let o=null;const a=this.GASLIMIT_DEFAULT;let h;try{h=await s.methods.joinswapExternAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){h=a}try{o=await s.methods.joinswapExternAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:h+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to pay tokens in order to join the pool: ${t.message}`)}return o}async joinswapPoolAmountOut(t,e,i,r,n){const s=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let o=null;const a=this.GASLIMIT_DEFAULT;let h;try{h=await s.methods.joinswapPoolAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){h=a}try{o=await s.methods.joinswapPoolAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:h+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error("ERROR: Failed to join swap pool amount out")}return o}async exitswapPoolAmountIn(t,e,i,r,n){const s=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let o=null;const a=this.GASLIMIT_DEFAULT;let h;try{h=await s.methods.exitswapPoolAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?a:e)}catch(t){h=a}try{o=await s.methods.exitswapPoolAmountIn(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:h+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to pay pool shares into the pool: ${t.message}`)}return o}async exitswapExternAmountOut(t,e,i,r,n){const s=this.GASLIMIT_DEFAULT,o=Y(new this.web3.eth.Contract(this.poolABI,e,{from:t}),this.config);let a,h=null;try{a=await o.methods.exitswapExternAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).estimateGas({from:t},(t,e)=>t?s:e)}catch(t){a=s}try{h=await o.methods.exitswapExternAmountOut(i,this.web3.utils.toWei(r),this.web3.utils.toWei(n)).send({from:t,gas:a+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error("ERROR: Failed to exitswapExternAmountOut")}return h}async getSpotPrice(t,e,i){const r=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let n=null;try{const t=await r.methods.getSpotPrice(e,i).call();n=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to get spot price of swapping tokenIn to tokenOut")}return n}async getSpotPriceSansFee(t,e,i){const r=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let n=null;try{const t=await r.methods.getSpotPriceSansFee(e,i).call();n=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to getSpotPriceSansFee")}return n}async calcSpotPrice(t,e,i,r,n,s){const o=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let a="0";try{const t=await o.methods.calcSpotPrice(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s)).call();a=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to call calcSpotPrice")}return a}async calcInGivenOut(t,i,r,n,s,o,a){const h=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let u=null;if(new e(o).gte(n))return null;try{const t=await h.methods.calcInGivenOut(this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o),this.web3.utils.toWei(a)).call();u=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to calcInGivenOut")}return u}async calcOutGivenIn(t,e,i,r,n,s,o){const a=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let h=null;try{const t=await a.methods.calcOutGivenIn(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error("ERROR: Failed to calcOutGivenIn")}return h}async calcPoolOutGivenSingleIn(t,e,i,r,n,s,o){const a=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let h=null;try{const t=await a.methods.calcPoolOutGivenSingleIn(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${t.message}`)}return h}async calcSingleInGivenPoolOut(t,e,i,r,n,s,o){const a=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let h=null;try{const t=await a.methods.calcSingleInGivenPoolOut(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${t.message}`)}return h}async calcSingleOutGivenPoolIn(t,e,i,r,n,s,o){const a=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let h=null;try{const t=await a.methods.calcSingleOutGivenPoolIn(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${t.message}`)}return h}async calcPoolInGivenSingleOut(t,e,i,r,n,s,o){const a=Y(new this.web3.eth.Contract(this.poolABI,t),this.config);let h=null;try{const t=await a.methods.calcPoolInGivenSingleOut(this.web3.utils.toWei(e),this.web3.utils.toWei(i),this.web3.utils.toWei(r),this.web3.utils.toWei(n),this.web3.utils.toWei(s),this.web3.utils.toWei(o)).call();h=this.web3.utils.fromWei(t)}catch(t){this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${t.message}`)}return h}getSwapEventSignature(){const t=this.poolABI.find(function(t){if("LOG_SWAP"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getJoinEventSignature(){const t=this.poolABI.find(function(t){if("LOG_JOIN"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}getExitEventSignature(){const t=this.poolABI.find(function(t){if("LOG_EXIT"===t.name&&"event"===t.type)return t});return this.web3.eth.abi.encodeEventSignature(t)}}{constructor(t,e,i=null,r=null,n=null,s=null,o){super(t,e,i,r,n,o),this.oceanAddress=null,this.dtAddress=null,this.startBlock=void 0,s&&(this.oceanAddress=s),this.startBlock=o&&o.startBlock||0}create(t,e,i,r,n,s){if(null==this.oceanAddress)throw this.logger.error("ERROR: oceanAddress is not defined"),new Error("ERROR: oceanAddress is not defined");if(parseFloat(s)>.1)throw this.logger.error("ERROR: Swap fee too high. The maximum allowed swapFee is 10%"),new Error("ERROR: Swap fee too high. The maximum allowed swapFee is 10%");if(parseFloat(i)<2)throw this.logger.error("ERROR: Amount of DT is too low"),new Error("ERROR: Amount of DT is too low");if(parseFloat(r)>9||parseFloat(r)<1)throw this.logger.error("ERROR: Weight out of bounds (min 1, max9)"),new Error("ERROR: Weight out of bounds (min 1, max9)");return new P(async o=>{o.next(We.CreatingPool);const a=await super.createPool(t);if(!a)throw this.logger.error("ERROR: Failed to call create pool"),new Error("ERROR: Failed to call create pool");const h=a.events.BPoolRegistered.returnValues[0],u=10-parseFloat(r);let l;if(this.dtAddress=e,o.next(We.ApprovingDatatoken),l=await this.approve(t,e,h,this.web3.utils.toWei(String(i))),!l)throw this.logger.error("ERROR: Failed to call approve DT token"),new Error("ERROR: Failed to call approve DT token");if(o.next(We.ApprovingOcean),l=await this.approve(t,this.oceanAddress,h,this.web3.utils.toWei(String(n))),!l)throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");if(o.next(We.SetupPool),l=await super.setup(t,h,e,this.web3.utils.toWei(String(i)),this.web3.utils.toWei(String(r)),this.oceanAddress,this.web3.utils.toWei(String(n)),this.web3.utils.toWei(String(u)),this.web3.utils.toWei(s)),!l)throw this.logger.error("ERROR: Failed to create a new pool"),new Error("ERROR: Failed to create a new pool");return a})}async getDTAddress(t){this.dtAddress=null;const e=await this.getCurrentTokens(t);let i;if(null!=e)for(i of e)i.toLowerCase()!==this.oceanAddress.toLowerCase()&&(this.dtAddress=i);return this.dtAddress}async getOceanReserve(t){return null==this.oceanAddress?(this.logger.error("ERROR: oceanAddress is not defined"),null):super.getReserve(t,this.oceanAddress)}async getDTReserve(t){const e=await this.getDTAddress(t);return super.getReserve(t,e)}async getMaxBuyQuantity(t,i){const r=await super.getReserve(t,i);return new e(r).div(3).toString()}async getOceanMaxBuyQuantity(t){return this.getMaxBuyQuantity(t,this.oceanAddress)}async getDTMaxBuyQuantity(t){return this.getMaxBuyQuantity(t,await this.getDTAddress(t))}async calcInGivenOut(t,e,i,r){return await super.calcInGivenOut(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getReserve(t,i),await super.getDenormalizedWeight(t,i),r,await this.getSwapFee(t))}async calcOutGivenIn(t,e,i,r){return await super.calcOutGivenIn(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getReserve(t,i),await super.getDenormalizedWeight(t,i),r,await super.getSwapFee(t))}async calcPoolOutGivenSingleIn(t,e,i){return super.calcPoolOutGivenSingleIn(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async calcSingleInGivenPoolOut(t,e,i){return super.calcSingleInGivenPoolOut(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async calcSingleOutGivenPoolIn(t,e,i){return super.calcSingleOutGivenPoolIn(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async calcPoolInGivenSingleOut(t,e,i){return super.calcPoolInGivenSingleOut(t,await super.getReserve(t,e),await super.getDenormalizedWeight(t,e),await super.getPoolSharesTotalSupply(t),await super.getTotalDenormalizedWeight(t),i,await super.getSwapFee(t))}async getPoolSharesRequiredToRemoveDT(t,e){const i=await this.getDTAddress(t);return this.calcPoolInGivenSingleOut(t,i,e)}async getDTRemovedforPoolShares(t,e){const i=await this.getDTAddress(t);return this.calcSingleOutGivenPoolIn(t,i,e)}async getPoolSharesRequiredToRemoveOcean(t,e){return this.calcPoolInGivenSingleOut(t,this.oceanAddress,e)}async getOceanRemovedforPoolShares(t,e){return this.calcSingleOutGivenPoolIn(t,this.oceanAddress,e)}async getTokensRemovedforPoolShares(t,i){try{const r=await this.getPoolSharesTotalSupply(t),n=await this.getDTReserve(t),s=await this.getOceanReserve(t);return{dtAmount:new e(i).div(r).mul(n).toString(),oceanAmount:new e(i).div(r).mul(s).toString()}}catch(t){this.logger.error(`ERROR: Unable to get token info. ${t.message}`)}}async getDTMaxAddLiquidity(t){const e=await this.getDTAddress(t);return this.getMaxAddLiquidity(t,e)}async getOceanMaxAddLiquidity(t){return this.getMaxAddLiquidity(t,this.oceanAddress)}async getMaxAddLiquidity(t,i){const r=await super.getReserve(t,i);return parseFloat(r)>0?new e(r).mul(.25).toString():"0"}async getMaxRemoveLiquidity(t,i){const r=await super.getReserve(t,i);return parseFloat(r)>0?new e(r).mul(.25).toString():"0"}async getDTMaxRemoveLiquidity(t){const e=await this.getDTAddress(t);return this.getMaxRemoveLiquidity(t,e)}async getOceanMaxRemoveLiquidity(t){return this.getMaxRemoveLiquidity(t,this.oceanAddress)}async buyDT(t,i,r,n,s){if(null==this.oceanAddress)return this.logger.error("ERROR: undefined ocean token contract address"),null;const o=await this.getDTAddress(i);if(new e(r).greaterThan(await this.getDTMaxBuyQuantity(i)))return this.logger.error("ERROR: Buy quantity exceeds quantity allowed"),null;const a=await this.getOceanNeeded(i,r);if(new e(a).greaterThan(n))return this.logger.error("ERROR: Not enough Ocean Tokens"),null;if(!await super.approve(t,this.oceanAddress,i,this.web3.utils.toWei(n)))throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");return await super.swapExactAmountOut(t,i,this.oceanAddress,n,o,r,s)}async buyDTWithExactOcean(t,i,r,n,s){if(null==this.oceanAddress)return this.logger.error("ERROR: undefined ocean token contract address"),null;const o=await this.getDTAddress(i);if(new e(r).greaterThan(await this.getDTMaxBuyQuantity(i)))return this.logger.error("ERROR: Buy quantity exceeds quantity allowed"),null;const a=await this.getOceanNeeded(i,r);if(new e(a).greaterThan(n))return this.logger.error("ERROR: Not enough Ocean Tokens"),null;if(!await super.approve(t,this.oceanAddress,i,this.web3.utils.toWei(n)))throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");return await super.swapExactAmountIn(t,i,this.oceanAddress,n,o,r,s)}async sellDT(t,i,r,n,s){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const o=await this.getDTAddress(i);if(new e(n).greaterThan(await this.getOceanMaxBuyQuantity(i)))return this.logger.error("ERROR: Buy quantity exceeds quantity allowed"),null;const a=await this.getOceanReceived(i,r);if(new e(a).lessThan(n))return this.logger.error("ERROR: Not enough datatokens"),null;if(!await super.approve(t,o,i,this.web3.utils.toWei(r)))throw this.logger.error("ERROR: Failed to call approve DT token"),new Error("ERROR: Failed to call approve DT token");return await super.swapExactAmountIn(t,i,o,r,this.oceanAddress,n,s)}async addDTLiquidity(t,i,r){const n=await this.getDTAddress(i),s=await this.getMaxAddLiquidity(i,n);if(new e(r).greaterThan(s))return this.logger.error("ERROR: Too much reserve to add"),null;if(!await super.approve(t,n,i,this.web3.utils.toWei(r)))throw this.logger.error("ERROR: Failed to call approve DT token"),new Error("ERROR: Failed to call approve DT token");return await super.joinswapExternAmountIn(t,i,n,r,"0")}async removeDTLiquidity(t,i,r,n){const s=await this.getDTAddress(i),o=await this.getDTMaxRemoveLiquidity(i);if(new e(r).greaterThan(o))return this.logger.error("ERROR: Too much reserve to remove"),null;const a=await this.sharesBalance(t,i);if(new e(a).lessThan(n))return this.logger.error("ERROR: Not enough poolShares"),null;const h=await this.getPoolSharesRequiredToRemoveDT(i,r);return new e(n).lessThan(h)?(this.logger.error("ERROR: Not enough poolShares"),null):(new e(n).lessThan(h)&&(n=new e(n).mul(.9999).toString()),this.exitswapExternAmountOut(t,i,s,r,n))}async addOceanLiquidity(t,i,r){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const n=await this.getOceanMaxAddLiquidity(i);if(new e(r).greaterThan(n))return this.logger.error("ERROR: Too much reserve to add"),null;if(!await super.approve(t,this.oceanAddress,i,this.web3.utils.toWei(r)))throw this.logger.error("ERROR: Failed to call approve OCEAN token"),new Error("ERROR: Failed to call approve OCEAN token");return await super.joinswapExternAmountIn(t,i,this.oceanAddress,r,"0")}async removeOceanLiquidityWithMinimum(t,i,r,n){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const s=await this.sharesBalance(t,i);return new e(s).lessThan(r)?(this.logger.error("ERROR: Not enough poolShares"),null):super.exitswapPoolAmountIn(t,i,this.oceanAddress,r,n)}async removeOceanLiquidity(t,i,r,n){if(null==this.oceanAddress)return this.logger.error("ERROR: oceanAddress is not defined"),null;const s=await this.getOceanMaxRemoveLiquidity(i);if(new e(r).greaterThan(s))return this.logger.error("ERROR: Too much reserve to remove"),null;const o=await this.sharesBalance(t,i);if(new e(o).lessThan(n))return this.logger.error("ERROR: Not enough poolShares"),null;const a=await this.getPoolSharesRequiredToRemoveOcean(i,r);return new e(n).lessThan(a)?(this.logger.error("ERROR: Not enough poolShares"),null):(new e(n).lessThan(a)&&(n=new e(n).mul(.9999).toString()),super.exitswapExternAmountOut(t,i,this.oceanAddress,r,n))}async removePoolLiquidity(t,i,r,n="0",s="0"){const o=await this.sharesBalance(t,i);return new e(o).lessThan(r)?(this.logger.error("ERROR: Not enough poolShares"),null):(new e(o).equals(r)&&(r=new e(r).mul(.9999).toString()),this.exitPool(t,i,r,[n,s]))}async getDTPrice(t){return null==this.oceanAddress?(this.logger.error("ERROR: oceanAddress is not defined"),"0"):this.getOceanNeeded(t,"1")}async searchPoolforDT(t){const e=[],i=Y(new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),this.config),r=await i.getPastEvents("BPoolRegistered",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});r.sort((t,e)=>t.blockNumber>e.blockNumber?1:-1);for(let i=0;i<r.length;i++)(await super.getCurrentTokens(r[i].returnValues[0])).includes(t)&&e.push(r[i].returnValues[0]);return e}async getOceanNeeded(t,i){const r=await this.getDTAddress(t);return new e(i).greaterThan(await this.getDTMaxBuyQuantity(t))?"0":this.calcInGivenOut(t,this.oceanAddress,r,i)}async getOceanReceived(t,e){const i=await this.getDTAddress(t);return this.calcOutGivenIn(t,i,this.oceanAddress,e)}async getDTReceived(t,e){const i=await this.getDTAddress(t);return this.calcOutGivenIn(t,this.oceanAddress,i,e)}async getDTNeeded(t,i){const r=await this.getDTAddress(t);return new e(i).greaterThan(await this.getOceanMaxBuyQuantity(t))?"0":this.calcInGivenOut(t,r,this.oceanAddress,i)}async getPoolsbyCreator(t){const e=[],i=Y(new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),this.config),r=await i.getPastEvents("BPoolRegistered",{filter:t?{registeredBy:t}:{},fromBlock:this.startBlock,toBlock:"latest"});for(let i=0;i<r.length;i++)t&&r[i].returnValues[1].toLowerCase()!==t.toLowerCase()||e.push(await this.getPoolDetails(r[i].returnValues[0]));return e}async getResult(t,e){const i=await super.sharesBalance(t,e.returnValues[0]);if(parseFloat(i)>0){const t=await this.getDTAddress(e.returnValues[0]);if(t)return{shares:i,poolAddress:e.returnValues[0],did:T(C(t))}}}async getPoolSharesByAddress(t){const e=[],i=Y(new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),this.config),r=await i.getPastEvents("BPoolRegistered",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});let n=[];for(let i=0;i<r.length;i++)if(n.push(this.getResult(t,r[i])),n.length>10){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}if(n.length>0){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}return e.filter(t=>void 0!==t)}async getPoolDetails(t){return{poolAddress:t,tokens:await super.getFinalTokens(t)}}async getPoolLogs(t,e=0,i){const r=[],n=await this.getDTAddress(t);0===e&&(e=this.startBlock);const s=super.getSwapEventSignature(),o=super.getJoinEventSignature(),a=super.getExitEventSignature();let h;h=i?"0x000000000000000000000000"+i.substring(2).toLowerCase():null;const u=await this.web3.eth.getPastLogs({address:t,topics:[[s,o,a],h],fromBlock:e,toBlock:"latest"});let l=[];for(let e=0;e<u.length;e++)if(l.push(this.getEventData(t,n,u[e])),l.length>10){const t=await Promise.all(l);for(let e=0;e<t.length;e++)r.push(t[e]);l=[]}if(l.length>0){const t=await Promise.all(l);for(let e=0;e<t.length;e++)r.push(t[e]);l=[]}return r.filter(t=>void 0!==t)}async getAllPoolLogs(t){const e=[],i=Y(new this.web3.eth.Contract(this.factoryABI,this.factoryAddress),this.config),r=await i.getPastEvents("BPoolRegistered",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});let n=[];for(let i=0;i<r.length;i++)if(n.push(this.getPoolLogs(r[i].returnValues[0],r[i].blockNumber,t)),n.length>10){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}if(n.length>0){const t=await Promise.all(n);for(let i=0;i<t.length;i++)e.push(t[i]);n=[]}return e.reduce((t,e)=>t.concat(e))}async getEventData(t,e,i){const r=await this.web3.eth.getBlock(i.blockNumber),n=super.getSwapEventSignature(),s=super.getJoinEventSignature(),o=super.getExitEventSignature();let a;switch(i.topics[0]){case n:a="swap";break;case s:a="join";break;case o:a="exit"}let h,u={poolAddress:t,dtAddress:e,caller:i.topics[1],transactionHash:i.transactionHash,blockNumber:i.blockNumber,timestamp:parseInt(String(r.timestamp)),type:a};switch(a){case"swap":h=this.web3.eth.abi.decodeParameters(["uint256","uint256"],i.data),u=A({},u,{tokenIn:"0x"+i.topics[2].substring(i.topics[2].length-40),tokenOut:"0x"+i.topics[3].substring(i.topics[3].length-40),tokenAmountIn:this.web3.utils.fromWei(h[0]),tokenAmountOut:this.web3.utils.fromWei(h[1])});break;case"join":h=this.web3.eth.abi.decodeParameters(["uint256"],i.data),u=A({},u,{tokenIn:"0x"+i.topics[2].substring(i.topics[2].length-40),tokenAmountIn:this.web3.utils.fromWei(h[0])});break;case"exit":h=this.web3.eth.abi.decodeParameters(["uint256"],i.data),u=A({},u,{tokenOut:"0x"+i.topics[2].substring(i.topics[2].length-40),tokenAmountOut:this.web3.utils.fromWei(h[0])})}return u}async computeSlippage(t,i,r,n,s,o,a,h){const u=await super.calcSpotPrice(t,i,r,n,s,h),l=await super.calcSpotPrice(t,o,r,a,s,h);return new e(l).mul(100).div(u).minus(100).toString()}async computeBuySlippage(t,e){const i=await this.getDTAddress(t),r=await super.getDenormalizedWeight(t,i),n=await super.getDenormalizedWeight(t,this.oceanAddress),s=await super.getReserve(t,i),o=await super.getReserve(t,i),a=await super.getSwapFee(t),h=await super.calcOutGivenIn(t,o,n,s,r,e,a),u=new X(this.web3.utils.toWei(s)).minus(this.web3.utils.toWei(h)),l=new X(this.web3.utils.toWei(o)).plus(this.web3.utils.toWei(e));return await this.computeSlippage(t,o,n,s,r,this.web3.utils.fromWei(l.toString()),this.web3.utils.fromWei(u.toString()),a)}async computeSellSlippage(t,e){const i=await this.getDTAddress(t),r=await super.getDenormalizedWeight(t,i),n=await super.getDenormalizedWeight(t,this.oceanAddress),s=await super.getReserve(t,i),o=await super.getReserve(t,i),a=await super.getSwapFee(t),h=await super.calcOutGivenIn(t,s,r,o,n,e,a),u=new X(this.web3.utils.toWei(s)).plus(this.web3.utils.toWei(e)),l=new X(this.web3.utils.toWei(o)).minus(this.web3.utils.toWei(h));return await this.computeSlippage(t,s,r,o,n,this.web3.utils.fromWei(u.toString()),this.web3.utils.fromWei(l.toString()),a)}}!function(t){t[t.CreatingExchange=0]="CreatingExchange",t[t.ApprovingDatatoken=1]="ApprovingDatatoken"}(Fe||(Fe={}));class qe{constructor(t,e,i=null,r=null,n=null,s,o){this.GASLIMIT_DEFAULT=1e6,this.oceanAddress=null,this.fixedRateExchangeAddress=void 0,this.fixedRateExchangeABI=void 0,this.web3=void 0,this.contract=null,this.logger=void 0,this.datatokens=void 0,this.startBlock=void 0,this.config=void 0,this.web3=t,this.fixedRateExchangeAddress=i,this.config=o,this.startBlock=o&&o.startBlock||0,this.fixedRateExchangeABI=r||w.abi,this.oceanAddress=n,this.datatokens=s,t&&(this.contract=Y(new this.web3.eth.Contract(this.fixedRateExchangeABI,this.fixedRateExchangeAddress),this.config)),this.logger=e}create(t,e,i,r){return this.createExchange(this.oceanAddress,t,e,i,r)}createExchange(t,e,i,r,n){return new P(async s=>{let o;s.next(Fe.CreatingExchange);const a=this.GASLIMIT_DEFAULT;try{o=await this.contract.methods.create(t,e,this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?a:e)}catch(t){o=a}let h=null,u=null;try{u=await this.contract.methods.create(t,e,this.web3.utils.toWei(i)).send({from:r,gas:o+1,gasPrice:await Q(this.web3,this.config)}),h=u.events.ExchangeCreated.returnValues[0]}catch(t){this.logger.error(`ERROR: Failed to create new exchange: ${t.message}`)}return n&&h&&(s.next(Fe.ApprovingDatatoken),this.datatokens.approve(e,this.fixedRateExchangeAddress,n,r)),u})}async generateExchangeId(t,e){return await this.contract.methods.generateExchangeId(this.oceanAddress,t,e).call()}async buyDT(t,e,i){const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.swap(t,this.web3.utils.toWei(String(e))).estimateGas({from:i},(t,e)=>t?r:e)}catch(t){n=r}try{return await this.contract.methods.swap(t,this.web3.utils.toWei(String(e))).send({from:i,gas:n+1,gasPrice:await Q(this.web3,this.config)})}catch(t){return this.logger.error(`ERROR: Failed to buy datatokens: ${t.message}`),null}}async getNumberOfExchanges(){return await this.contract.methods.getNumberOfExchanges().call()}async setRate(t,e,i){const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.setRate(t,this.web3.utils.toWei(String(e))).estimateGas({from:i},(t,e)=>t?r:e)}catch(t){n=r}return await this.contract.methods.setRate(t,this.web3.utils.toWei(String(e))).send({from:i,gas:n+1,gasPrice:await Q(this.web3,this.config)})}async activate(t,e){const i=await this.getExchange(t);if(!i)return null;if(!0===i.active)return null;const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.toggleExchangeState(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){n=r}return await this.contract.methods.toggleExchangeState(t).send({from:e,gas:n+1,gasPrice:await Q(this.web3,this.config)})}async deactivate(t,e){const i=await this.getExchange(t);if(!i)return null;if(!1===i.active)return null;const r=this.GASLIMIT_DEFAULT;let n;try{n=await this.contract.methods.toggleExchangeState(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){n=r}return await this.contract.methods.toggleExchangeState(t).send({from:e,gas:n+1,gasPrice:await Q(this.web3,this.config)})}async getRate(t){const e=await this.contract.methods.getRate(t).call();return this.web3.utils.fromWei(e)}async getSupply(t){const e=await this.contract.methods.getSupply(t).call();return this.web3.utils.fromWei(e)}async getOceanNeeded(t,e){const i=await this.contract.methods.CalcInGivenOut(t,this.web3.utils.toWei(e)).call();return this.web3.utils.fromWei(i)}async getExchange(t){const e=await this.contract.methods.getExchange(t).call();return e.fixedRate=this.web3.utils.fromWei(e.fixedRate),e.supply=this.web3.utils.fromWei(e.supply),e.exchangeID=t,e}async getExchanges(){return await this.contract.methods.getExchanges().call()}async isActive(t){return await this.contract.methods.isActive(t).call()}async CalcInGivenOut(t,e){const i=await this.contract.methods.CalcInGivenOut(t,this.web3.utils.toWei(e)).call();return this.web3.utils.fromWei(i)}async searchforDT(t,e){const i=[],r=await this.contract.getPastEvents("ExchangeCreated",{filter:{datatoken:t.toLowerCase()},fromBlock:this.startBlock,toBlock:"latest"});let n=[];for(let s=0;s<r.length;s++)if(n.push(this.getExchange(r[s].returnValues[0])),n.length>10||s===r.length-1){const r=await Promise.all(n);for(let n=0;n<r.length;n++){const s=r[n];if(!0===s.active&&s.dataToken.toLowerCase()===t.toLowerCase()){const t=new X(s.supply),r=new X(e);t.gte(r)&&i.push(s)}}n=[]}return i}async getExchangesbyCreator(t){const e=[],i=await this.contract.getPastEvents("ExchangeCreated",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});for(let r=0;r<i.length;r++)t&&i[r].returnValues[3].toLowerCase()!==t.toLowerCase()||e.push(await this.getExchange(i[r].returnValues[0]));return e}async getExchangeSwaps(t,e){const i=[],r=await this.contract.getPastEvents("Swapped",{filter:{exchangeId:t},fromBlock:this.startBlock,toBlock:"latest"});for(let t=0;t<r.length;t++)e&&r[t].returnValues[1].toLowerCase()!==e.toLowerCase()||i.push(this.getEventData(r[t]));return i}async getAllExchangesSwaps(t){const e=[],i=await this.contract.getPastEvents("ExchangeCreated",{filter:{},fromBlock:this.startBlock,toBlock:"latest"});for(let r=0;r<i.length;r++)(await this.getExchangeSwaps(i[r].returnValues[0],t)).forEach(t=>{e.push(t)});return e}getEventData(t){return{exchangeID:t.returnValues[0],caller:t.returnValues[1],baseTokenAmount:this.web3.utils.fromWei(t.returnValues[2]),dataTokenAmount:this.web3.utils.fromWei(t.returnValues[3])}}}!function(t){t[t.MakeDispenserMinter=0]="MakeDispenserMinter",t[t.AcceptingNewMinter=1]="AcceptingNewMinter"}(Ue||(Ue={})),function(t){t[t.MakeOwnerMinter=0]="MakeOwnerMinter",t[t.AcceptingNewMinter=1]="AcceptingNewMinter"}($e||($e={}));class Ge{constructor(t,e,i=null,r=null,n,s){this.GASLIMIT_DEFAULT=1e6,this.dispenserAddress=void 0,this.dispenserABI=void 0,this.web3=void 0,this.contract=null,this.logger=void 0,this.datatokens=void 0,this.startBlock=void 0,this.config=void 0,this.web3=t,this.config=s,this.dispenserAddress=i,this.startBlock=s&&s.startBlock||0,this.dispenserABI=r||v.abi,this.datatokens=n,t&&(this.contract=Y(new this.web3.eth.Contract(this.dispenserABI,this.dispenserAddress),this.config)),this.logger=e}async status(t){try{const e=await this.contract.methods.status(t).call();return e.maxTokens=this.web3.utils.fromWei(e.maxTokens),e.maxBalance=this.web3.utils.fromWei(e.maxBalance),e.balance=this.web3.utils.fromWei(e.balance),e}catch(e){this.logger.warn(`No dispenser available for data token: ${t}`)}return null}async activate(t,e,i,r){let n;const s=this.GASLIMIT_DEFAULT;try{n=await this.contract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(i)).estimateGas({from:r},(t,e)=>t?s:e)}catch(t){n=s}let o=null;try{o=await this.contract.methods.activate(t,this.web3.utils.toWei(e),this.web3.utils.toWei(i)).send({from:r,gas:n+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to activate dispenser: ${t.message}`)}return o}async deactivate(t,e){let i;const r=this.GASLIMIT_DEFAULT;try{i=await this.contract.methods.deactivate(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){i=r}let n=null;try{n=await this.contract.methods.deactivate(t).send({from:e,gas:i+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to deactivate dispenser: ${t.message}`)}return n}makeMinter(t,e){return new P(async i=>{let r;i.next(Ue.MakeDispenserMinter);const n=this.GASLIMIT_DEFAULT;if(!await this.datatokens.proposeMinter(t,this.dispenserAddress,e))return null;i.next(Ue.AcceptingNewMinter);try{r=await this.contract.methods.acceptMinter(t).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}let s=null;try{s=await this.contract.methods.acceptMinter(t).send({from:e,gas:r+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to accept minter role: ${t.message}`)}return s})}cancelMinter(t,e){return new P(async i=>{let r;i.next($e.MakeOwnerMinter);const n=this.GASLIMIT_DEFAULT;try{r=await this.contract.methods.removeMinter(t).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}let s=null;try{s=await this.contract.methods.removeMinter(t).send({from:e,gas:r+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to remove minter role: ${t.message}`)}return s?(i.next($e.AcceptingNewMinter),await this.datatokens.approveMinter(t,e)):null})}async dispense(t,e,i="1"){let r;const n=this.GASLIMIT_DEFAULT;try{r=await this.contract.methods.dispense(t,this.web3.utils.toWei(i)).estimateGas({from:e},(t,e)=>t?n:e)}catch(t){r=n}let s=null;try{s=await this.contract.methods.dispense(t,this.web3.utils.toWei(i)).send({from:e,gas:r+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to dispense tokens: ${t.message}`)}return s}async ownerWithdraw(t,e){let i;const r=this.GASLIMIT_DEFAULT;try{i=await this.contract.methods.ownerWithdraw(t).estimateGas({from:e},(t,e)=>t?r:e)}catch(t){i=r}let n=null;try{n=await this.contract.methods.ownerWithdraw(t).send({from:e,gas:i+1,gasPrice:await Q(this.web3,this.config)})}catch(t){this.logger.error(`ERROR: Failed to withdraw tokens: ${t.message}`)}return n}async isDispensable(t,i,r="1"){const n=await this.status(t);return!(!n||!1===n.active||new e(await this.datatokens.balance(t,i)).greaterThanOrEqualTo(n.maxBalance)||new e(String(r)).greaterThan(n.maxTokens)||!new e(n.balance).greaterThanOrEqualTo(r)&&!0!==n.isTrueMinter)}}class ze extends st{constructor(...t){super(...t),this.network=void 0,this.provider=void 0,this.eventAccessControl=void 0,this.web3Provider=void 0,this.metadataCache=void 0,this.onChainMetadata=void 0,this.accounts=void 0,this.assets=void 0,this.compute=void 0,this.datatokens=void 0,this.pool=void 0,this.fixedRateExchange=void 0,this.OceanDispenser=void 0,this.versions=void 0,this.utils=void 0}static async getInstance(t){const e=new ze,i=A({},function(t){return{config:t,web3:t.web3Provider,logger:R}}(t),{ocean:e});return e.setInstanceConfig(i),e.utils=await Te.getInstance(i),e.provider=await mt.getInstance(i),e.eventAccessControl=await Re.getInstance(i),e.metadataCache=new Be(i.config.metadataCacheUri,i.logger),e.accounts=await ht.getInstance(i),e.assets=await ke.getInstance(i),e.compute=await Ne.getInstance(i),e.datatokens=new _e(i.config.factoryAddress,i.config.factoryABI,i.config.datatokensABI,i.config.web3Provider,i.logger,i.config),e.pool=new je(i.config.web3Provider,i.logger,i.config.poolFactoryABI,i.config.poolABI,i.config.poolFactoryAddress,i.config.oceanTokenAddress,i.config),e.fixedRateExchange=new qe(i.config.web3Provider,i.logger,i.config.fixedRateExchangeAddress,i.config.fixedRateExchangeAddressABI,i.config.oceanTokenAddress,e.datatokens,i.config),e.OceanDispenser=new Ge(i.config.web3Provider,i.logger,i.config.dispenserAddress,i.config.dispenserABI,e.datatokens,i.config),e.onChainMetadata=new Ce(i.config.web3Provider,i.logger,i.config.metadataContractAddress,i.config.metadataContractABI,e.metadataCache,i.config),e.versions=await Se.getInstance(i),e.network=new De,e}}const He={networkId:null,network:"unknown",metadataCacheUri:"https://aquarius.oceanprotocol.com",nodeUri:"http://localhost:8545",providerUri:"http://127.0.0.1:8030",subgraphUri:null,explorerUri:null,oceanTokenAddress:null,oceanTokenSymbol:"OCEAN",factoryAddress:"0x1234",poolFactoryAddress:null,fixedRateExchangeAddress:null,dispenserAddress:null,metadataContractAddress:null,startBlock:0,transactionBlockTimeout:50,transactionConfirmationBlocks:1,transactionPollingTimeout:750,gasFeeMultiplier:1},Ze=[A({},He),A({},He,{networkId:8996,network:"development",metadataCacheUri:"http://127.0.0.1:5000",rbacUri:"http://127.0.0.1:3000"}),A({},He,{networkId:3,network:"ropsten",nodeUri:"https://ropsten.infura.io/v3",providerUri:"https://provider.ropsten.oceanprotocol.com",subgraphUri:"https://subgraph.ropsten.oceanprotocol.com",explorerUri:"https://ropsten.etherscan.io",startBlock:9227563}),A({},He,{networkId:4,network:"rinkeby",nodeUri:"https://rinkeby.infura.io/v3",providerUri:"https://provider.rinkeby.oceanprotocol.com",subgraphUri:"https://subgraph.rinkeby.oceanprotocol.com",explorerUri:"https://rinkeby.etherscan.io",startBlock:7294090}),A({},He,{networkId:1,network:"mainnet",nodeUri:"https://mainnet.infura.io/v3",providerUri:"https://provider.mainnet.oceanprotocol.com",subgraphUri:"https://subgraph.mainnet.oceanprotocol.com",explorerUri:"https://etherscan.io",startBlock:11105459,transactionBlockTimeout:150,transactionConfirmationBlocks:5,transactionPollingTimeout:1750,gasFeeMultiplier:1.05}),A({},He,{networkId:137,network:"polygon",nodeUri:"https://polygon-mainnet.infura.io/v3",providerUri:"https://provider.polygon.oceanprotocol.com",subgraphUri:"https://subgraph.polygon.oceanprotocol.com",explorerUri:"https://polygonscan.com",oceanTokenSymbol:"mOCEAN",startBlock:11005222,gasFeeMultiplier:1.05}),A({},He,{networkId:1287,network:"moonbeamalpha",nodeUri:"https://rpc.testnet.moonbeam.network",providerUri:"https://provider.moonbeamalpha.oceanprotocol.com",subgraphUri:"https://subgraph.moonbeamalpha.oceanprotocol.com",explorerUri:"https://moonbase-blockscout.testnet.moonbeam.network/",startBlock:90707}),A({},He,{networkId:2021e3,network:"gaiaxtestnet",nodeUri:"https://rpc.gaiaxtestnet.oceanprotocol.com",providerUri:"https://provider.gaiaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.gaiaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.gaiaxtestnet.oceanprotocol.com"}),A({},He,{networkId:2021001,network:"catenaxtestnet",nodeUri:"https://rpc.catenaxtestnet.oceanprotocol.com",providerUri:"https://provider.catenaxtestnet.oceanprotocol.com",subgraphUri:"https://subgraph.catenaxtestnet.oceanprotocol.com",explorerUri:"https://blockscout.catenaxtestnet.oceanprotocol.com",metadataCacheUri:"https://aquarius.catenaxtestnet.oceanprotocol.com"}),A({},He,{networkId:80001,network:"mumbai",nodeUri:"https://polygon-mumbai.infura.io/v3",providerUri:"https://provider.mumbai.oceanprotocol.com",subgraphUri:"https://subgraph.mumbai.oceanprotocol.com",explorerUri:"https://mumbai.polygonscan.com"}),A({},He,{networkId:56,network:"bsc",nodeUri:"https://bsc-dataseed.binance.org",providerUri:"https://provider.bsc.oceanprotocol.com",subgraphUri:"https://subgraph.bsc.oceanprotocol.com",explorerUri:"https://bscscan.com/",gasFeeMultiplier:1.05}),A({},He,{networkId:44787,network:"celoalfajores",nodeUri:"https://alfajores-forno.celo-testnet.org",providerUri:"https://provider.celoalfajores.oceanprotocol.com",subgraphUri:"https://subgraph.celoalfajores.oceanprotocol.com",explorerUri:"https://alfajores-blockscout.celo-testnet.org"}),A({},He,{networkId:246,network:"energyweb",nodeUri:"https://rpc.energyweb.org",providerUri:"https://provider.energyweb.oceanprotocol.com",subgraphUri:"https://subgraph.energyweb.oceanprotocol.com",explorerUri:"https://explorer.energyweb.org",gasFeeMultiplier:1.05}),A({},He,{networkId:1285,network:"moonriver",nodeUri:"https://moonriver.api.onfinality.io/public",providerUri:"https://provider.moonriver.oceanprotocol.com",subgraphUri:"https://subgraph.moonriver.oceanprotocol.com",explorerUri:"https://blockscout.moonriver.moonbeam.network",gasFeeMultiplier:1.05})];class Ve{getAddressesFromEnv(t){let e;if(M[t]){const{DTFactory:i,BFactory:r,FixedRateExchange:n,Dispenser:s,Metadata:o,Ocean:a,chainId:h,startBlock:u}=M[t];e=A({factoryAddress:i,poolFactoryAddress:r,fixedRateExchangeAddress:n,dispenserAddress:s,metadataContractAddress:o,oceanTokenAddress:a,networkId:h,startBlock:u},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}if(r&&process.env.ADDRESS_FILE)try{const i=JSON.parse(r.readFileSync(process.env.ADDRESS_FILE||`${y}/.ocean/ocean-contracts/artifacts/address.json`,"utf8")),{DTFactory:n,BFactory:s,FixedRateExchange:o,Dispenser:a,Metadata:h,Ocean:u,chainId:l,startBlock:c}=i[t];e=A({factoryAddress:n,poolFactoryAddress:s,fixedRateExchangeAddress:o,dispenserAddress:a,metadataContractAddress:h,oceanTokenAddress:u,networkId:l,startBlock:c},process.env.AQUARIUS_URI&&{metadataCacheUri:process.env.AQUARIUS_URI})}catch(t){}return e}getConfig(t,e){const i="string"==typeof t?"network":"networkId";let r=Ze.find(e=>e[i]===t);return r?(r=A({},r,this.getAddressesFromEnv(r.network)),A({},r,{nodeUri:e?`${r.nodeUri}/${e}`:r.nodeUri})):(R.error(`No config found for given network '${t}'`),null)}}export{ot as Account,b as Config,Ve as ConfigHelper,ae as CreateProgressStep,lt as DDO,at as DID,_e as DataTokens,x as LogLevel,R as Logger,Be as MetadataCache,ze as Ocean,ue as OceanPlatformTechStatus,he as OrderProgressStep,mt as Provider,nt as utils};
2
2
  //# sourceMappingURL=lib.module.js.map