@gala-chain/gswap-sdk 0.0.2 → 0.0.4

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.
@@ -5,7 +5,7 @@ Use Chrome, Firefox or Internet Explorer 11`)}var Dy=It().Buffer,la=global.crypt
5
5
  `)}});var F2=I(O2=>{"use strict";var L2=O2;L2.der=Kh();L2.pem=D2()});var tf=I(U2=>{"use strict";var nf=U2;nf.bignum=s2();nf.define=l2().define;nf.base=rf();nf.constants=Nh();nf.decoders=I2();nf.encoders=F2()});var H2=I((BA,$2)=>{"use strict";var Cr=tf(),z2=Cr.define("Time",function(){this.choice({utcTime:this.utctime(),generalTime:this.gentime()})}),_x=Cr.define("AttributeTypeValue",function(){this.seq().obj(this.key("type").objid(),this.key("value").any())}),Gh=Cr.define("AlgorithmIdentifier",function(){this.seq().obj(this.key("algorithm").objid(),this.key("parameters").optional(),this.key("curve").objid().optional())}),Sx=Cr.define("SubjectPublicKeyInfo",function(){this.seq().obj(this.key("algorithm").use(Gh),this.key("subjectPublicKey").bitstr())}),Ax=Cr.define("RelativeDistinguishedName",function(){this.setof(_x)}),Bx=Cr.define("RDNSequence",function(){this.seqof(Ax)}),K2=Cr.define("Name",function(){this.choice({rdnSequence:this.use(Bx)})}),Ex=Cr.define("Validity",function(){this.seq().obj(this.key("notBefore").use(z2),this.key("notAfter").use(z2))}),kx=Cr.define("Extension",function(){this.seq().obj(this.key("extnID").objid(),this.key("critical").bool().def(!1),this.key("extnValue").octstr())}),qx=Cr.define("TBSCertificate",function(){this.seq().obj(this.key("version").explicit(0).int().optional(),this.key("serialNumber").int(),this.key("signature").use(Gh),this.key("issuer").use(K2),this.key("validity").use(Ex),this.key("subject").use(K2),this.key("subjectPublicKeyInfo").use(Sx),this.key("issuerUniqueID").implicit(1).bitstr().optional(),this.key("subjectUniqueID").implicit(2).bitstr().optional(),this.key("extensions").explicit(3).seqof(kx).optional())}),Ix=Cr.define("X509Certificate",function(){this.seq().obj(this.key("tbsCertificate").use(qx),this.key("signatureAlgorithm").use(Gh),this.key("signatureValue").bitstr())});$2.exports=Ix});var Z2=I(Dr=>{"use strict";var Nr=tf();Dr.certificate=H2();var Rx=Nr.define("RSAPrivateKey",function(){this.seq().obj(this.key("version").int(),this.key("modulus").int(),this.key("publicExponent").int(),this.key("privateExponent").int(),this.key("prime1").int(),this.key("prime2").int(),this.key("exponent1").int(),this.key("exponent2").int(),this.key("coefficient").int())});Dr.RSAPrivateKey=Rx;var Tx=Nr.define("RSAPublicKey",function(){this.seq().obj(this.key("modulus").int(),this.key("publicExponent").int())});Dr.RSAPublicKey=Tx;var G2=Nr.define("AlgorithmIdentifier",function(){this.seq().obj(this.key("algorithm").objid(),this.key("none").null_().optional(),this.key("curve").objid().optional(),this.key("params").seq().obj(this.key("p").int(),this.key("q").int(),this.key("g").int()).optional())}),Px=Nr.define("SubjectPublicKeyInfo",function(){this.seq().obj(this.key("algorithm").use(G2),this.key("subjectPublicKey").bitstr())});Dr.PublicKey=Px;var Cx=Nr.define("PrivateKeyInfo",function(){this.seq().obj(this.key("version").int(),this.key("algorithm").use(G2),this.key("subjectPrivateKey").octstr())});Dr.PrivateKey=Cx;var Nx=Nr.define("EncryptedPrivateKeyInfo",function(){this.seq().obj(this.key("algorithm").seq().obj(this.key("id").objid(),this.key("decrypt").seq().obj(this.key("kde").seq().obj(this.key("id").objid(),this.key("kdeparams").seq().obj(this.key("salt").octstr(),this.key("iters").int())),this.key("cipher").seq().obj(this.key("algo").objid(),this.key("iv").octstr()))),this.key("subjectPrivateKey").octstr())});Dr.EncryptedPrivateKey=Nx;var Dx=Nr.define("DSAPrivateKey",function(){this.seq().obj(this.key("version").int(),this.key("p").int(),this.key("q").int(),this.key("g").int(),this.key("pub_key").int(),this.key("priv_key").int())});Dr.DSAPrivateKey=Dx;Dr.DSAparam=Nr.define("DSAparam",function(){this.int()});var Lx=Nr.define("ECParameters",function(){this.choice({namedCurve:this.objid()})}),Ox=Nr.define("ECPrivateKey",function(){this.seq().obj(this.key("version").int(),this.key("privateKey").octstr(),this.key("parameters").optional().explicit(0).use(Lx),this.key("publicKey").optional().explicit(1).bitstr())});Dr.ECPrivateKey=Ox;Dr.signature=Nr.define("signature",function(){this.seq().obj(this.key("r").int(),this.key("s").int())})});var W2=I((kA,Fx)=>{Fx.exports={"2.16.840.1.101.3.4.1.1":"aes-128-ecb","2.16.840.1.101.3.4.1.2":"aes-128-cbc","2.16.840.1.101.3.4.1.3":"aes-128-ofb","2.16.840.1.101.3.4.1.4":"aes-128-cfb","2.16.840.1.101.3.4.1.21":"aes-192-ecb","2.16.840.1.101.3.4.1.22":"aes-192-cbc","2.16.840.1.101.3.4.1.23":"aes-192-ofb","2.16.840.1.101.3.4.1.24":"aes-192-cfb","2.16.840.1.101.3.4.1.41":"aes-256-ecb","2.16.840.1.101.3.4.1.42":"aes-256-cbc","2.16.840.1.101.3.4.1.43":"aes-256-ofb","2.16.840.1.101.3.4.1.44":"aes-256-cfb"}});var j2=I((qA,V2)=>{"use strict";var Ux=/Proc-Type: 4,ENCRYPTED[\n\r]+DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)[\n\r]+([0-9A-z\n\r+/=]+)[\n\r]+/m,zx=/^-----BEGIN ((?:.*? KEY)|CERTIFICATE)-----/m,Kx=/^-----BEGIN ((?:.*? KEY)|CERTIFICATE)-----([0-9A-z\n\r+/=]+)-----END \1-----$/m,$x=Cf(),Hx=e0(),K0=It().Buffer;V2.exports=function(i,t){var r=i.toString(),s=r.match(Ux),f;if(s){var g="aes"+s[1],b=K0.from(s[2],"hex"),w=K0.from(s[3].replace(/[\r\n]/g,""),"base64"),_=$x(t,b.slice(0,8),parseInt(s[1],10)).key,S=[],A=Hx.createDecipheriv(g,_,b);S.push(A.update(w)),S.push(A.final()),f=K0.concat(S)}else{var c=r.match(Kx);f=K0.from(c[2].replace(/[\r\n]/g,""),"base64")}var q=r.match(zx)[1];return{tag:q,data:f}}});var Jf=I((IA,X2)=>{"use strict";var sr=Z2(),Gx=W2(),Zx=j2(),Wx=e0(),Vx=us(),Zh=It().Buffer;function jx(i,t){var r=i.algorithm.decrypt.kde.kdeparams.salt,s=parseInt(i.algorithm.decrypt.kde.kdeparams.iters.toString(),10),f=Gx[i.algorithm.decrypt.cipher.algo.join(".")],c=i.algorithm.decrypt.cipher.iv,g=i.subjectPrivateKey,b=parseInt(f.split("-")[1],10)/8,w=Vx.pbkdf2Sync(t,r,s,b,"sha1"),_=Wx.createDecipheriv(f,w,c),S=[];return S.push(_.update(g)),S.push(_.final()),Zh.concat(S)}function J2(i){var t;typeof i=="object"&&!Zh.isBuffer(i)&&(t=i.passphrase,i=i.key),typeof i=="string"&&(i=Zh.from(i));var r=Zx(i,t),s=r.tag,f=r.data,c,g;switch(s){case"CERTIFICATE":g=sr.certificate.decode(f,"der").tbsCertificate.subjectPublicKeyInfo;case"PUBLIC KEY":switch(g||(g=sr.PublicKey.decode(f,"der")),c=g.algorithm.algorithm.join("."),c){case"1.2.840.113549.1.1.1":return sr.RSAPublicKey.decode(g.subjectPublicKey.data,"der");case"1.2.840.10045.2.1":return g.subjectPrivateKey=g.subjectPublicKey,{type:"ec",data:g};case"1.2.840.10040.4.1":return g.algorithm.params.pub_key=sr.DSAparam.decode(g.subjectPublicKey.data,"der"),{type:"dsa",data:g.algorithm.params};default:throw new Error("unknown key id "+c)}case"ENCRYPTED PRIVATE KEY":f=sr.EncryptedPrivateKey.decode(f,"der"),f=jx(f,t);case"PRIVATE KEY":switch(g=sr.PrivateKey.decode(f,"der"),c=g.algorithm.algorithm.join("."),c){case"1.2.840.113549.1.1.1":return sr.RSAPrivateKey.decode(g.subjectPrivateKey,"der");case"1.2.840.10045.2.1":return{curve:g.algorithm.curve,privateKey:sr.ECPrivateKey.decode(g.subjectPrivateKey,"der").privateKey};case"1.2.840.10040.4.1":return g.algorithm.params.priv_key=sr.DSAparam.decode(g.subjectPrivateKey,"der"),{type:"dsa",params:g.algorithm.params};default:throw new Error("unknown key id "+c)}case"RSA PUBLIC KEY":return sr.RSAPublicKey.decode(f,"der");case"RSA PRIVATE KEY":return sr.RSAPrivateKey.decode(f,"der");case"DSA PRIVATE KEY":return{type:"dsa",params:sr.DSAPrivateKey.decode(f,"der")};case"EC PRIVATE KEY":return f=sr.ECPrivateKey.decode(f,"der"),{curve:f.parameters.value,privateKey:f.privateKey};default:throw new Error("unknown key type "+s)}}J2.signature=sr.signature;X2.exports=J2});var Wh=I((RA,Jx)=>{Jx.exports={"1.3.132.0.10":"secp256k1","1.3.132.0.33":"p224","1.2.840.10045.3.1.1":"p192","1.2.840.10045.3.1.7":"p256","1.3.132.0.34":"p384","1.3.132.0.35":"p521"}});var tg=I((TA,H0)=>{"use strict";var er=It().Buffer,hn=Co(),Xx=w0(),Yx=L0().ec,$0=b0(),Qx=Jf(),t7=Wh(),e7=1;function r7(i,t,r,s,f){var c=Qx(t);if(c.curve){if(s!=="ecdsa"&&s!=="ecdsa/rsa")throw new Error("wrong private key type");return i7(i,c)}else if(c.type==="dsa"){if(s!=="dsa")throw new Error("wrong private key type");return n7(i,c,r)}if(s!=="rsa"&&s!=="ecdsa/rsa")throw new Error("wrong private key type");if(t.padding!==void 0&&t.padding!==e7)throw new Error("illegal or unsupported padding mode");i=er.concat([f,i]);for(var g=c.modulus.byteLength(),b=[0,1];i.length+b.length+1<g;)b.push(255);b.push(0);for(var w=-1;++w<i.length;)b.push(i[w]);var _=Xx(b,c);return _}function i7(i,t){var r=t7[t.curve.join(".")];if(!r)throw new Error("unknown curve "+t.curve.join("."));var s=new Yx(r),f=s.keyFromPrivate(t.privateKey),c=f.sign(i);return er.from(c.toDER())}function n7(i,t,r){for(var s=t.params.priv_key,f=t.params.p,c=t.params.q,g=t.params.g,b=new $0(0),w,_=Vh(i,c).mod(c),S=!1,A=Y2(s,c,i,r);S===!1;)w=Q2(c,A,r),b=o7(g,w,f,c),S=w.invm(c).imul(_.add(s.mul(b))).mod(c),S.cmpn(0)===0&&(S=!1,b=new $0(0));return f7(b,S)}function f7(i,t){i=i.toArray(),t=t.toArray(),i[0]&128&&(i=[0].concat(i)),t[0]&128&&(t=[0].concat(t));var r=i.length+t.length+4,s=[48,r,2,i.length];return s=s.concat(i,[2,t.length],t),er.from(s)}function Y2(i,t,r,s){if(i=er.from(i.toArray()),i.length<t.byteLength()){var f=er.alloc(t.byteLength()-i.length);i=er.concat([f,i])}var c=r.length,g=a7(r,t),b=er.alloc(c);b.fill(1);var w=er.alloc(c);return w=hn(s,w).update(b).update(er.from([0])).update(i).update(g).digest(),b=hn(s,w).update(b).digest(),w=hn(s,w).update(b).update(er.from([1])).update(i).update(g).digest(),b=hn(s,w).update(b).digest(),{k:w,v:b}}function Vh(i,t){var r=new $0(i),s=(i.length<<3)-t.bitLength();return s>0&&r.ishrn(s),r}function a7(i,t){i=Vh(i,t),i=i.mod(t);var r=er.from(i.toArray());if(r.length<t.byteLength()){var s=er.alloc(t.byteLength()-r.length);r=er.concat([s,r])}return r}function Q2(i,t,r){var s,f;do{for(s=er.alloc(0);s.length*8<i.bitLength();)t.v=hn(r,t.k).update(t.v).digest(),s=er.concat([s,t.v]);f=Vh(s,i),t.k=hn(r,t.k).update(t.v).update(er.from([0])).digest(),t.v=hn(r,t.k).update(t.v).digest()}while(f.cmp(i)!==-1);return f}function o7(i,t,r,s){return i.toRed($0.mont(r)).redPow(t).fromRed().mod(s)}H0.exports=r7;H0.exports.getKey=Y2;H0.exports.makeKey=Q2});var ng=I((PA,ig)=>{"use strict";var jh=It().Buffer,Xf=b0(),s7=L0().ec,rg=Jf(),h7=Wh();function u7(i,t,r,s,f){var c=rg(r);if(c.type==="ec"){if(s!=="ecdsa"&&s!=="ecdsa/rsa")throw new Error("wrong public key type");return l7(i,t,c)}else if(c.type==="dsa"){if(s!=="dsa")throw new Error("wrong public key type");return d7(i,t,c)}if(s!=="rsa"&&s!=="ecdsa/rsa")throw new Error("wrong public key type");t=jh.concat([f,t]);for(var g=c.modulus.byteLength(),b=[1],w=0;t.length+b.length+2<g;)b.push(255),w+=1;b.push(0);for(var _=-1;++_<t.length;)b.push(t[_]);b=jh.from(b);var S=Xf.mont(c.modulus);i=new Xf(i).toRed(S),i=i.redPow(new Xf(c.publicExponent)),i=jh.from(i.fromRed().toArray());var A=w<8?1:0;for(g=Math.min(i.length,b.length),i.length!==b.length&&(A=1),_=-1;++_<g;)A|=i[_]^b[_];return A===0}function l7(i,t,r){var s=h7[r.data.algorithm.curve.join(".")];if(!s)throw new Error("unknown curve "+r.data.algorithm.curve.join("."));var f=new s7(s),c=r.data.subjectPrivateKey.data;return f.verify(t,i,c)}function d7(i,t,r){var s=r.data.p,f=r.data.q,c=r.data.g,g=r.data.pub_key,b=rg.signature.decode(i,"der"),w=b.s,_=b.r;eg(w,f),eg(_,f);var S=Xf.mont(s),A=w.invm(f),q=c.toRed(S).redPow(new Xf(t).mul(A).mod(f)).fromRed().mul(g.toRed(S).redPow(_.mul(A).mod(f)).fromRed()).mod(s).mod(f);return q.cmp(_)===0}function eg(i,t){if(i.cmpn(0)<=0)throw new Error("invalid sig");if(i.cmp(t)>=0)throw new Error("invalid sig")}ig.exports=u7});var ug=I((CA,hg)=>{"use strict";var G0=It().Buffer,og=bn(),Z0=Tv(),sg=Rt(),c7=tg(),p7=ng(),un=No();Object.keys(un).forEach(function(i){un[i].id=G0.from(un[i].id,"hex"),un[i.toLowerCase()]=un[i]});function Yf(i){Z0.Writable.call(this);var t=un[i];if(!t)throw new Error("Unknown message digest");this._hashType=t.hash,this._hash=og(t.hash),this._tag=t.id,this._signType=t.sign}sg(Yf,Z0.Writable);Yf.prototype._write=function(t,r,s){this._hash.update(t),s()};Yf.prototype.update=function(t,r){return this._hash.update(typeof t=="string"?G0.from(t,r):t),this};Yf.prototype.sign=function(t,r){this.end();var s=this._hash.digest(),f=c7(s,t,this._hashType,this._signType,this._tag);return r?f.toString(r):f};function Qf(i){Z0.Writable.call(this);var t=un[i];if(!t)throw new Error("Unknown message digest");this._hash=og(t.hash),this._tag=t.id,this._signType=t.sign}sg(Qf,Z0.Writable);Qf.prototype._write=function(t,r,s){this._hash.update(t),s()};Qf.prototype.update=function(t,r){return this._hash.update(typeof t=="string"?G0.from(t,r):t),this};Qf.prototype.verify=function(t,r,s){var f=typeof r=="string"?G0.from(r,s):r;this.end();var c=this._hash.digest();return p7(f,c,t,this._signType,this._tag)};function fg(i){return new Yf(i)}function ag(i){return new Qf(i)}hg.exports={Sign:fg,Verify:ag,createSign:fg,createVerify:ag}});var dg=I((lg,Jh)=>{"use strict";(function(i,t){"use strict";function r(v,e){if(!v)throw new Error(e||"Assertion failed")}function s(v,e){v.super_=e;var a=function(){};a.prototype=e.prototype,v.prototype=new a,v.prototype.constructor=v}function f(v,e,a){if(f.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((e==="le"||e==="be")&&(a=e,e=10),this._init(v||0,e||10,a||"be"))}typeof i=="object"?i.exports=f:t.BN=f,f.BN=f,f.wordSize=26;var c;try{typeof window<"u"&&typeof window.Buffer<"u"?c=window.Buffer:c=qi().Buffer}catch{}f.isBN=function(e){return e instanceof f?!0:e!==null&&typeof e=="object"&&e.constructor.wordSize===f.wordSize&&Array.isArray(e.words)},f.max=function(e,a){return e.cmp(a)>0?e:a},f.min=function(e,a){return e.cmp(a)<0?e:a},f.prototype._init=function(e,a,h){if(typeof e=="number")return this._initNumber(e,a,h);if(typeof e=="object")return this._initArray(e,a,h);a==="hex"&&(a=16),r(a===(a|0)&&a>=2&&a<=36),e=e.toString().replace(/\s+/g,"");var o=0;e[0]==="-"&&(o++,this.negative=1),o<e.length&&(a===16?this._parseHex(e,o,h):(this._parseBase(e,a,o),h==="le"&&this._initArray(this.toArray(),a,h)))},f.prototype._initNumber=function(e,a,h){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[e&67108863],this.length=1):e<4503599627370496?(this.words=[e&67108863,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[e&67108863,e/67108864&67108863,1],this.length=3),h==="le"&&this._initArray(this.toArray(),a,h)},f.prototype._initArray=function(e,a,h){if(r(typeof e.length=="number"),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var o=0;o<this.length;o++)this.words[o]=0;var u,p,m=0;if(h==="be")for(o=e.length-1,u=0;o>=0;o-=3)p=e[o]|e[o-1]<<8|e[o-2]<<16,this.words[u]|=p<<m&67108863,this.words[u+1]=p>>>26-m&67108863,m+=24,m>=26&&(m-=26,u++);else if(h==="le")for(o=0,u=0;o<e.length;o+=3)p=e[o]|e[o+1]<<8|e[o+2]<<16,this.words[u]|=p<<m&67108863,this.words[u+1]=p>>>26-m&67108863,m+=24,m>=26&&(m-=26,u++);return this.strip()};function g(v,e){var a=v.charCodeAt(e);return a>=65&&a<=70?a-55:a>=97&&a<=102?a-87:a-48&15}function b(v,e,a){var h=g(v,a);return a-1>=e&&(h|=g(v,a-1)<<4),h}f.prototype._parseHex=function(e,a,h){this.length=Math.ceil((e.length-a)/6),this.words=new Array(this.length);for(var o=0;o<this.length;o++)this.words[o]=0;var u=0,p=0,m;if(h==="be")for(o=e.length-1;o>=a;o-=2)m=b(e,a,o)<<u,this.words[p]|=m&67108863,u>=18?(u-=18,p+=1,this.words[p]|=m>>>26):u+=8;else{var l=e.length-a;for(o=l%2===0?a+1:a;o<e.length;o+=2)m=b(e,a,o)<<u,this.words[p]|=m&67108863,u>=18?(u-=18,p+=1,this.words[p]|=m>>>26):u+=8}this.strip()};function w(v,e,a,h){for(var o=0,u=Math.min(v.length,a),p=e;p<u;p++){var m=v.charCodeAt(p)-48;o*=h,m>=49?o+=m-49+10:m>=17?o+=m-17+10:o+=m}return o}f.prototype._parseBase=function(e,a,h){this.words=[0],this.length=1;for(var o=0,u=1;u<=67108863;u*=a)o++;o--,u=u/a|0;for(var p=e.length-h,m=p%o,l=Math.min(p,p-m)+h,n=0,d=h;d<l;d+=o)n=w(e,d,d+o,a),this.imuln(u),this.words[0]+n<67108864?this.words[0]+=n:this._iaddn(n);if(m!==0){var M=1;for(n=w(e,d,e.length,a),d=0;d<m;d++)M*=a;this.imuln(M),this.words[0]+n<67108864?this.words[0]+=n:this._iaddn(n)}this.strip()},f.prototype.copy=function(e){e.words=new Array(this.length);for(var a=0;a<this.length;a++)e.words[a]=this.words[a];e.length=this.length,e.negative=this.negative,e.red=this.red},f.prototype.clone=function(){var e=new f(null);return this.copy(e),e},f.prototype._expand=function(e){for(;this.length<e;)this.words[this.length++]=0;return this},f.prototype.strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},f.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},f.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var _=["","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"],S=[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],A=[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];f.prototype.toString=function(e,a){e=e||10,a=a|0||1;var h;if(e===16||e==="hex"){h="";for(var o=0,u=0,p=0;p<this.length;p++){var m=this.words[p],l=((m<<o|u)&16777215).toString(16);u=m>>>24-o&16777215,o+=2,o>=26&&(o-=26,p--),u!==0||p!==this.length-1?h=_[6-l.length]+l+h:h=l+h}for(u!==0&&(h=u.toString(16)+h);h.length%a!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}if(e===(e|0)&&e>=2&&e<=36){var n=S[e],d=A[e];h="";var M=this.clone();for(M.negative=0;!M.isZero();){var y=M.modn(d).toString(e);M=M.idivn(d),M.isZero()?h=y+h:h=_[n-y.length]+y+h}for(this.isZero()&&(h="0"+h);h.length%a!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}r(!1,"Base should be between 2 and 36")},f.prototype.toNumber=function(){var e=this.words[0];return this.length===2?e+=this.words[1]*67108864:this.length===3&&this.words[2]===1?e+=4503599627370496+this.words[1]*67108864:this.length>2&&r(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-e:e},f.prototype.toJSON=function(){return this.toString(16)},f.prototype.toBuffer=function(e,a){return r(typeof c<"u"),this.toArrayLike(c,e,a)},f.prototype.toArray=function(e,a){return this.toArrayLike(Array,e,a)},f.prototype.toArrayLike=function(e,a,h){var o=this.byteLength(),u=h||Math.max(1,o);r(o<=u,"byte array longer than desired length"),r(u>0,"Requested array length <= 0"),this.strip();var p=a==="le",m=new e(u),l,n,d=this.clone();if(p){for(n=0;!d.isZero();n++)l=d.andln(255),d.iushrn(8),m[n]=l;for(;n<u;n++)m[n]=0}else{for(n=0;n<u-o;n++)m[n]=0;for(n=0;!d.isZero();n++)l=d.andln(255),d.iushrn(8),m[u-n-1]=l}return m},Math.clz32?f.prototype._countBits=function(e){return 32-Math.clz32(e)}:f.prototype._countBits=function(e){var a=e,h=0;return a>=4096&&(h+=13,a>>>=13),a>=64&&(h+=7,a>>>=7),a>=8&&(h+=4,a>>>=4),a>=2&&(h+=2,a>>>=2),h+a},f.prototype._zeroBits=function(e){if(e===0)return 26;var a=e,h=0;return(a&8191)===0&&(h+=13,a>>>=13),(a&127)===0&&(h+=7,a>>>=7),(a&15)===0&&(h+=4,a>>>=4),(a&3)===0&&(h+=2,a>>>=2),(a&1)===0&&h++,h},f.prototype.bitLength=function(){var e=this.words[this.length-1],a=this._countBits(e);return(this.length-1)*26+a};function q(v){for(var e=new Array(v.bitLength()),a=0;a<e.length;a++){var h=a/26|0,o=a%26;e[a]=(v.words[h]&1<<o)>>>o}return e}f.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,a=0;a<this.length;a++){var h=this._zeroBits(this.words[a]);if(e+=h,h!==26)break}return e},f.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},f.prototype.toTwos=function(e){return this.negative!==0?this.abs().inotn(e).iaddn(1):this.clone()},f.prototype.fromTwos=function(e){return this.testn(e-1)?this.notn(e).iaddn(1).ineg():this.clone()},f.prototype.isNeg=function(){return this.negative!==0},f.prototype.neg=function(){return this.clone().ineg()},f.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},f.prototype.iuor=function(e){for(;this.length<e.length;)this.words[this.length++]=0;for(var a=0;a<e.length;a++)this.words[a]=this.words[a]|e.words[a];return this.strip()},f.prototype.ior=function(e){return r((this.negative|e.negative)===0),this.iuor(e)},f.prototype.or=function(e){return this.length>e.length?this.clone().ior(e):e.clone().ior(this)},f.prototype.uor=function(e){return this.length>e.length?this.clone().iuor(e):e.clone().iuor(this)},f.prototype.iuand=function(e){var a;this.length>e.length?a=e:a=this;for(var h=0;h<a.length;h++)this.words[h]=this.words[h]&e.words[h];return this.length=a.length,this.strip()},f.prototype.iand=function(e){return r((this.negative|e.negative)===0),this.iuand(e)},f.prototype.and=function(e){return this.length>e.length?this.clone().iand(e):e.clone().iand(this)},f.prototype.uand=function(e){return this.length>e.length?this.clone().iuand(e):e.clone().iuand(this)},f.prototype.iuxor=function(e){var a,h;this.length>e.length?(a=this,h=e):(a=e,h=this);for(var o=0;o<h.length;o++)this.words[o]=a.words[o]^h.words[o];if(this!==a)for(;o<a.length;o++)this.words[o]=a.words[o];return this.length=a.length,this.strip()},f.prototype.ixor=function(e){return r((this.negative|e.negative)===0),this.iuxor(e)},f.prototype.xor=function(e){return this.length>e.length?this.clone().ixor(e):e.clone().ixor(this)},f.prototype.uxor=function(e){return this.length>e.length?this.clone().iuxor(e):e.clone().iuxor(this)},f.prototype.inotn=function(e){r(typeof e=="number"&&e>=0);var a=Math.ceil(e/26)|0,h=e%26;this._expand(a),h>0&&a--;for(var o=0;o<a;o++)this.words[o]=~this.words[o]&67108863;return h>0&&(this.words[o]=~this.words[o]&67108863>>26-h),this.strip()},f.prototype.notn=function(e){return this.clone().inotn(e)},f.prototype.setn=function(e,a){r(typeof e=="number"&&e>=0);var h=e/26|0,o=e%26;return this._expand(h+1),a?this.words[h]=this.words[h]|1<<o:this.words[h]=this.words[h]&~(1<<o),this.strip()},f.prototype.iadd=function(e){var a;if(this.negative!==0&&e.negative===0)return this.negative=0,a=this.isub(e),this.negative^=1,this._normSign();if(this.negative===0&&e.negative!==0)return e.negative=0,a=this.isub(e),e.negative=1,a._normSign();var h,o;this.length>e.length?(h=this,o=e):(h=e,o=this);for(var u=0,p=0;p<o.length;p++)a=(h.words[p]|0)+(o.words[p]|0)+u,this.words[p]=a&67108863,u=a>>>26;for(;u!==0&&p<h.length;p++)a=(h.words[p]|0)+u,this.words[p]=a&67108863,u=a>>>26;if(this.length=h.length,u!==0)this.words[this.length]=u,this.length++;else if(h!==this)for(;p<h.length;p++)this.words[p]=h.words[p];return this},f.prototype.add=function(e){var a;return e.negative!==0&&this.negative===0?(e.negative=0,a=this.sub(e),e.negative^=1,a):e.negative===0&&this.negative!==0?(this.negative=0,a=e.sub(this),this.negative=1,a):this.length>e.length?this.clone().iadd(e):e.clone().iadd(this)},f.prototype.isub=function(e){if(e.negative!==0){e.negative=0;var a=this.iadd(e);return e.negative=1,a._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(e),this.negative=1,this._normSign();var h=this.cmp(e);if(h===0)return this.negative=0,this.length=1,this.words[0]=0,this;var o,u;h>0?(o=this,u=e):(o=e,u=this);for(var p=0,m=0;m<u.length;m++)a=(o.words[m]|0)-(u.words[m]|0)+p,p=a>>26,this.words[m]=a&67108863;for(;p!==0&&m<o.length;m++)a=(o.words[m]|0)+p,p=a>>26,this.words[m]=a&67108863;if(p===0&&m<o.length&&o!==this)for(;m<o.length;m++)this.words[m]=o.words[m];return this.length=Math.max(this.length,m),o!==this&&(this.negative=1),this.strip()},f.prototype.sub=function(e){return this.clone().isub(e)};function R(v,e,a){a.negative=e.negative^v.negative;var h=v.length+e.length|0;a.length=h,h=h-1|0;var o=v.words[0]|0,u=e.words[0]|0,p=o*u,m=p&67108863,l=p/67108864|0;a.words[0]=m;for(var n=1;n<h;n++){for(var d=l>>>26,M=l&67108863,y=Math.min(n,e.length-1),x=Math.max(0,n-v.length+1);x<=y;x++){var B=n-x|0;o=v.words[B]|0,u=e.words[x]|0,p=o*u+M,d+=p/67108864|0,M=p&67108863}a.words[n]=M|0,l=d|0}return l!==0?a.words[n]=l|0:a.length--,a.strip()}var D=function(e,a,h){var o=e.words,u=a.words,p=h.words,m=0,l,n,d,M=o[0]|0,y=M&8191,x=M>>>13,B=o[1]|0,E=B&8191,k=B>>>13,wt=o[2]|0,T=wt&8191,P=wt>>>13,he=o[3]|0,N=he&8191,L=he>>>13,ve=o[4]|0,F=ve&8191,U=ve>>>13,me=o[5]|0,z=me&8191,K=me>>>13,ge=o[6]|0,$=ge&8191,H=ge>>>13,ye=o[7]|0,G=ye&8191,Z=ye>>>13,be=o[8]|0,W=be&8191,V=be>>>13,we=o[9]|0,j=we&8191,J=we>>>13,Me=u[0]|0,X=Me&8191,Y=Me>>>13,xe=u[1]|0,Q=xe&8191,tt=xe>>>13,_e=u[2]|0,et=_e&8191,rt=_e>>>13,Se=u[3]|0,it=Se&8191,nt=Se>>>13,Ae=u[4]|0,ft=Ae&8191,at=Ae>>>13,Be=u[5]|0,ot=Be&8191,st=Be>>>13,Ee=u[6]|0,ht=Ee&8191,ut=Ee>>>13,ke=u[7]|0,lt=ke&8191,dt=ke>>>13,qe=u[8]|0,ct=qe&8191,pt=qe>>>13,Ie=u[9]|0,vt=Ie&8191,mt=Ie>>>13;h.negative=e.negative^a.negative,h.length=19,l=Math.imul(y,X),n=Math.imul(y,Y),n=n+Math.imul(x,X)|0,d=Math.imul(x,Y);var ue=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ue>>>26)|0,ue&=67108863,l=Math.imul(E,X),n=Math.imul(E,Y),n=n+Math.imul(k,X)|0,d=Math.imul(k,Y),l=l+Math.imul(y,Q)|0,n=n+Math.imul(y,tt)|0,n=n+Math.imul(x,Q)|0,d=d+Math.imul(x,tt)|0;var Et=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,l=Math.imul(T,X),n=Math.imul(T,Y),n=n+Math.imul(P,X)|0,d=Math.imul(P,Y),l=l+Math.imul(E,Q)|0,n=n+Math.imul(E,tt)|0,n=n+Math.imul(k,Q)|0,d=d+Math.imul(k,tt)|0,l=l+Math.imul(y,et)|0,n=n+Math.imul(y,rt)|0,n=n+Math.imul(x,et)|0,d=d+Math.imul(x,rt)|0;var kt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,l=Math.imul(N,X),n=Math.imul(N,Y),n=n+Math.imul(L,X)|0,d=Math.imul(L,Y),l=l+Math.imul(T,Q)|0,n=n+Math.imul(T,tt)|0,n=n+Math.imul(P,Q)|0,d=d+Math.imul(P,tt)|0,l=l+Math.imul(E,et)|0,n=n+Math.imul(E,rt)|0,n=n+Math.imul(k,et)|0,d=d+Math.imul(k,rt)|0,l=l+Math.imul(y,it)|0,n=n+Math.imul(y,nt)|0,n=n+Math.imul(x,it)|0,d=d+Math.imul(x,nt)|0;var Zt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Zt>>>26)|0,Zt&=67108863,l=Math.imul(F,X),n=Math.imul(F,Y),n=n+Math.imul(U,X)|0,d=Math.imul(U,Y),l=l+Math.imul(N,Q)|0,n=n+Math.imul(N,tt)|0,n=n+Math.imul(L,Q)|0,d=d+Math.imul(L,tt)|0,l=l+Math.imul(T,et)|0,n=n+Math.imul(T,rt)|0,n=n+Math.imul(P,et)|0,d=d+Math.imul(P,rt)|0,l=l+Math.imul(E,it)|0,n=n+Math.imul(E,nt)|0,n=n+Math.imul(k,it)|0,d=d+Math.imul(k,nt)|0,l=l+Math.imul(y,ft)|0,n=n+Math.imul(y,at)|0,n=n+Math.imul(x,ft)|0,d=d+Math.imul(x,at)|0;var Wt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Wt>>>26)|0,Wt&=67108863,l=Math.imul(z,X),n=Math.imul(z,Y),n=n+Math.imul(K,X)|0,d=Math.imul(K,Y),l=l+Math.imul(F,Q)|0,n=n+Math.imul(F,tt)|0,n=n+Math.imul(U,Q)|0,d=d+Math.imul(U,tt)|0,l=l+Math.imul(N,et)|0,n=n+Math.imul(N,rt)|0,n=n+Math.imul(L,et)|0,d=d+Math.imul(L,rt)|0,l=l+Math.imul(T,it)|0,n=n+Math.imul(T,nt)|0,n=n+Math.imul(P,it)|0,d=d+Math.imul(P,nt)|0,l=l+Math.imul(E,ft)|0,n=n+Math.imul(E,at)|0,n=n+Math.imul(k,ft)|0,d=d+Math.imul(k,at)|0,l=l+Math.imul(y,ot)|0,n=n+Math.imul(y,st)|0,n=n+Math.imul(x,ot)|0,d=d+Math.imul(x,st)|0;var Vt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Vt>>>26)|0,Vt&=67108863,l=Math.imul($,X),n=Math.imul($,Y),n=n+Math.imul(H,X)|0,d=Math.imul(H,Y),l=l+Math.imul(z,Q)|0,n=n+Math.imul(z,tt)|0,n=n+Math.imul(K,Q)|0,d=d+Math.imul(K,tt)|0,l=l+Math.imul(F,et)|0,n=n+Math.imul(F,rt)|0,n=n+Math.imul(U,et)|0,d=d+Math.imul(U,rt)|0,l=l+Math.imul(N,it)|0,n=n+Math.imul(N,nt)|0,n=n+Math.imul(L,it)|0,d=d+Math.imul(L,nt)|0,l=l+Math.imul(T,ft)|0,n=n+Math.imul(T,at)|0,n=n+Math.imul(P,ft)|0,d=d+Math.imul(P,at)|0,l=l+Math.imul(E,ot)|0,n=n+Math.imul(E,st)|0,n=n+Math.imul(k,ot)|0,d=d+Math.imul(k,st)|0,l=l+Math.imul(y,ht)|0,n=n+Math.imul(y,ut)|0,n=n+Math.imul(x,ht)|0,d=d+Math.imul(x,ut)|0;var jt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(jt>>>26)|0,jt&=67108863,l=Math.imul(G,X),n=Math.imul(G,Y),n=n+Math.imul(Z,X)|0,d=Math.imul(Z,Y),l=l+Math.imul($,Q)|0,n=n+Math.imul($,tt)|0,n=n+Math.imul(H,Q)|0,d=d+Math.imul(H,tt)|0,l=l+Math.imul(z,et)|0,n=n+Math.imul(z,rt)|0,n=n+Math.imul(K,et)|0,d=d+Math.imul(K,rt)|0,l=l+Math.imul(F,it)|0,n=n+Math.imul(F,nt)|0,n=n+Math.imul(U,it)|0,d=d+Math.imul(U,nt)|0,l=l+Math.imul(N,ft)|0,n=n+Math.imul(N,at)|0,n=n+Math.imul(L,ft)|0,d=d+Math.imul(L,at)|0,l=l+Math.imul(T,ot)|0,n=n+Math.imul(T,st)|0,n=n+Math.imul(P,ot)|0,d=d+Math.imul(P,st)|0,l=l+Math.imul(E,ht)|0,n=n+Math.imul(E,ut)|0,n=n+Math.imul(k,ht)|0,d=d+Math.imul(k,ut)|0,l=l+Math.imul(y,lt)|0,n=n+Math.imul(y,dt)|0,n=n+Math.imul(x,lt)|0,d=d+Math.imul(x,dt)|0;var Jt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,l=Math.imul(W,X),n=Math.imul(W,Y),n=n+Math.imul(V,X)|0,d=Math.imul(V,Y),l=l+Math.imul(G,Q)|0,n=n+Math.imul(G,tt)|0,n=n+Math.imul(Z,Q)|0,d=d+Math.imul(Z,tt)|0,l=l+Math.imul($,et)|0,n=n+Math.imul($,rt)|0,n=n+Math.imul(H,et)|0,d=d+Math.imul(H,rt)|0,l=l+Math.imul(z,it)|0,n=n+Math.imul(z,nt)|0,n=n+Math.imul(K,it)|0,d=d+Math.imul(K,nt)|0,l=l+Math.imul(F,ft)|0,n=n+Math.imul(F,at)|0,n=n+Math.imul(U,ft)|0,d=d+Math.imul(U,at)|0,l=l+Math.imul(N,ot)|0,n=n+Math.imul(N,st)|0,n=n+Math.imul(L,ot)|0,d=d+Math.imul(L,st)|0,l=l+Math.imul(T,ht)|0,n=n+Math.imul(T,ut)|0,n=n+Math.imul(P,ht)|0,d=d+Math.imul(P,ut)|0,l=l+Math.imul(E,lt)|0,n=n+Math.imul(E,dt)|0,n=n+Math.imul(k,lt)|0,d=d+Math.imul(k,dt)|0,l=l+Math.imul(y,ct)|0,n=n+Math.imul(y,pt)|0,n=n+Math.imul(x,ct)|0,d=d+Math.imul(x,pt)|0;var Xt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,l=Math.imul(j,X),n=Math.imul(j,Y),n=n+Math.imul(J,X)|0,d=Math.imul(J,Y),l=l+Math.imul(W,Q)|0,n=n+Math.imul(W,tt)|0,n=n+Math.imul(V,Q)|0,d=d+Math.imul(V,tt)|0,l=l+Math.imul(G,et)|0,n=n+Math.imul(G,rt)|0,n=n+Math.imul(Z,et)|0,d=d+Math.imul(Z,rt)|0,l=l+Math.imul($,it)|0,n=n+Math.imul($,nt)|0,n=n+Math.imul(H,it)|0,d=d+Math.imul(H,nt)|0,l=l+Math.imul(z,ft)|0,n=n+Math.imul(z,at)|0,n=n+Math.imul(K,ft)|0,d=d+Math.imul(K,at)|0,l=l+Math.imul(F,ot)|0,n=n+Math.imul(F,st)|0,n=n+Math.imul(U,ot)|0,d=d+Math.imul(U,st)|0,l=l+Math.imul(N,ht)|0,n=n+Math.imul(N,ut)|0,n=n+Math.imul(L,ht)|0,d=d+Math.imul(L,ut)|0,l=l+Math.imul(T,lt)|0,n=n+Math.imul(T,dt)|0,n=n+Math.imul(P,lt)|0,d=d+Math.imul(P,dt)|0,l=l+Math.imul(E,ct)|0,n=n+Math.imul(E,pt)|0,n=n+Math.imul(k,ct)|0,d=d+Math.imul(k,pt)|0,l=l+Math.imul(y,vt)|0,n=n+Math.imul(y,mt)|0,n=n+Math.imul(x,vt)|0,d=d+Math.imul(x,mt)|0;var Yt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,l=Math.imul(j,Q),n=Math.imul(j,tt),n=n+Math.imul(J,Q)|0,d=Math.imul(J,tt),l=l+Math.imul(W,et)|0,n=n+Math.imul(W,rt)|0,n=n+Math.imul(V,et)|0,d=d+Math.imul(V,rt)|0,l=l+Math.imul(G,it)|0,n=n+Math.imul(G,nt)|0,n=n+Math.imul(Z,it)|0,d=d+Math.imul(Z,nt)|0,l=l+Math.imul($,ft)|0,n=n+Math.imul($,at)|0,n=n+Math.imul(H,ft)|0,d=d+Math.imul(H,at)|0,l=l+Math.imul(z,ot)|0,n=n+Math.imul(z,st)|0,n=n+Math.imul(K,ot)|0,d=d+Math.imul(K,st)|0,l=l+Math.imul(F,ht)|0,n=n+Math.imul(F,ut)|0,n=n+Math.imul(U,ht)|0,d=d+Math.imul(U,ut)|0,l=l+Math.imul(N,lt)|0,n=n+Math.imul(N,dt)|0,n=n+Math.imul(L,lt)|0,d=d+Math.imul(L,dt)|0,l=l+Math.imul(T,ct)|0,n=n+Math.imul(T,pt)|0,n=n+Math.imul(P,ct)|0,d=d+Math.imul(P,pt)|0,l=l+Math.imul(E,vt)|0,n=n+Math.imul(E,mt)|0,n=n+Math.imul(k,vt)|0,d=d+Math.imul(k,mt)|0;var Qt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,l=Math.imul(j,et),n=Math.imul(j,rt),n=n+Math.imul(J,et)|0,d=Math.imul(J,rt),l=l+Math.imul(W,it)|0,n=n+Math.imul(W,nt)|0,n=n+Math.imul(V,it)|0,d=d+Math.imul(V,nt)|0,l=l+Math.imul(G,ft)|0,n=n+Math.imul(G,at)|0,n=n+Math.imul(Z,ft)|0,d=d+Math.imul(Z,at)|0,l=l+Math.imul($,ot)|0,n=n+Math.imul($,st)|0,n=n+Math.imul(H,ot)|0,d=d+Math.imul(H,st)|0,l=l+Math.imul(z,ht)|0,n=n+Math.imul(z,ut)|0,n=n+Math.imul(K,ht)|0,d=d+Math.imul(K,ut)|0,l=l+Math.imul(F,lt)|0,n=n+Math.imul(F,dt)|0,n=n+Math.imul(U,lt)|0,d=d+Math.imul(U,dt)|0,l=l+Math.imul(N,ct)|0,n=n+Math.imul(N,pt)|0,n=n+Math.imul(L,ct)|0,d=d+Math.imul(L,pt)|0,l=l+Math.imul(T,vt)|0,n=n+Math.imul(T,mt)|0,n=n+Math.imul(P,vt)|0,d=d+Math.imul(P,mt)|0;var te=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(te>>>26)|0,te&=67108863,l=Math.imul(j,it),n=Math.imul(j,nt),n=n+Math.imul(J,it)|0,d=Math.imul(J,nt),l=l+Math.imul(W,ft)|0,n=n+Math.imul(W,at)|0,n=n+Math.imul(V,ft)|0,d=d+Math.imul(V,at)|0,l=l+Math.imul(G,ot)|0,n=n+Math.imul(G,st)|0,n=n+Math.imul(Z,ot)|0,d=d+Math.imul(Z,st)|0,l=l+Math.imul($,ht)|0,n=n+Math.imul($,ut)|0,n=n+Math.imul(H,ht)|0,d=d+Math.imul(H,ut)|0,l=l+Math.imul(z,lt)|0,n=n+Math.imul(z,dt)|0,n=n+Math.imul(K,lt)|0,d=d+Math.imul(K,dt)|0,l=l+Math.imul(F,ct)|0,n=n+Math.imul(F,pt)|0,n=n+Math.imul(U,ct)|0,d=d+Math.imul(U,pt)|0,l=l+Math.imul(N,vt)|0,n=n+Math.imul(N,mt)|0,n=n+Math.imul(L,vt)|0,d=d+Math.imul(L,mt)|0;var ee=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ee>>>26)|0,ee&=67108863,l=Math.imul(j,ft),n=Math.imul(j,at),n=n+Math.imul(J,ft)|0,d=Math.imul(J,at),l=l+Math.imul(W,ot)|0,n=n+Math.imul(W,st)|0,n=n+Math.imul(V,ot)|0,d=d+Math.imul(V,st)|0,l=l+Math.imul(G,ht)|0,n=n+Math.imul(G,ut)|0,n=n+Math.imul(Z,ht)|0,d=d+Math.imul(Z,ut)|0,l=l+Math.imul($,lt)|0,n=n+Math.imul($,dt)|0,n=n+Math.imul(H,lt)|0,d=d+Math.imul(H,dt)|0,l=l+Math.imul(z,ct)|0,n=n+Math.imul(z,pt)|0,n=n+Math.imul(K,ct)|0,d=d+Math.imul(K,pt)|0,l=l+Math.imul(F,vt)|0,n=n+Math.imul(F,mt)|0,n=n+Math.imul(U,vt)|0,d=d+Math.imul(U,mt)|0;var re=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(re>>>26)|0,re&=67108863,l=Math.imul(j,ot),n=Math.imul(j,st),n=n+Math.imul(J,ot)|0,d=Math.imul(J,st),l=l+Math.imul(W,ht)|0,n=n+Math.imul(W,ut)|0,n=n+Math.imul(V,ht)|0,d=d+Math.imul(V,ut)|0,l=l+Math.imul(G,lt)|0,n=n+Math.imul(G,dt)|0,n=n+Math.imul(Z,lt)|0,d=d+Math.imul(Z,dt)|0,l=l+Math.imul($,ct)|0,n=n+Math.imul($,pt)|0,n=n+Math.imul(H,ct)|0,d=d+Math.imul(H,pt)|0,l=l+Math.imul(z,vt)|0,n=n+Math.imul(z,mt)|0,n=n+Math.imul(K,vt)|0,d=d+Math.imul(K,mt)|0;var ie=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ie>>>26)|0,ie&=67108863,l=Math.imul(j,ht),n=Math.imul(j,ut),n=n+Math.imul(J,ht)|0,d=Math.imul(J,ut),l=l+Math.imul(W,lt)|0,n=n+Math.imul(W,dt)|0,n=n+Math.imul(V,lt)|0,d=d+Math.imul(V,dt)|0,l=l+Math.imul(G,ct)|0,n=n+Math.imul(G,pt)|0,n=n+Math.imul(Z,ct)|0,d=d+Math.imul(Z,pt)|0,l=l+Math.imul($,vt)|0,n=n+Math.imul($,mt)|0,n=n+Math.imul(H,vt)|0,d=d+Math.imul(H,mt)|0;var ne=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ne>>>26)|0,ne&=67108863,l=Math.imul(j,lt),n=Math.imul(j,dt),n=n+Math.imul(J,lt)|0,d=Math.imul(J,dt),l=l+Math.imul(W,ct)|0,n=n+Math.imul(W,pt)|0,n=n+Math.imul(V,ct)|0,d=d+Math.imul(V,pt)|0,l=l+Math.imul(G,vt)|0,n=n+Math.imul(G,mt)|0,n=n+Math.imul(Z,vt)|0,d=d+Math.imul(Z,mt)|0;var fe=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(fe>>>26)|0,fe&=67108863,l=Math.imul(j,ct),n=Math.imul(j,pt),n=n+Math.imul(J,ct)|0,d=Math.imul(J,pt),l=l+Math.imul(W,vt)|0,n=n+Math.imul(W,mt)|0,n=n+Math.imul(V,vt)|0,d=d+Math.imul(V,mt)|0;var ae=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ae>>>26)|0,ae&=67108863,l=Math.imul(j,vt),n=Math.imul(j,mt),n=n+Math.imul(J,vt)|0,d=Math.imul(J,mt);var oe=(m+l|0)+((n&8191)<<13)|0;return m=(d+(n>>>13)|0)+(oe>>>26)|0,oe&=67108863,p[0]=ue,p[1]=Et,p[2]=kt,p[3]=Zt,p[4]=Wt,p[5]=Vt,p[6]=jt,p[7]=Jt,p[8]=Xt,p[9]=Yt,p[10]=Qt,p[11]=te,p[12]=ee,p[13]=re,p[14]=ie,p[15]=ne,p[16]=fe,p[17]=ae,p[18]=oe,m!==0&&(p[19]=m,h.length++),h};Math.imul||(D=R);function bt(v,e,a){a.negative=e.negative^v.negative,a.length=v.length+e.length;for(var h=0,o=0,u=0;u<a.length-1;u++){var p=o;o=0;for(var m=h&67108863,l=Math.min(u,e.length-1),n=Math.max(0,u-v.length+1);n<=l;n++){var d=u-n,M=v.words[d]|0,y=e.words[n]|0,x=M*y,B=x&67108863;p=p+(x/67108864|0)|0,B=B+m|0,m=B&67108863,p=p+(B>>>26)|0,o+=p>>>26,p&=67108863}a.words[u]=m,h=p,p=o}return h!==0?a.words[u]=h:a.length--,a.strip()}function _t(v,e,a){var h=new gt;return h.mulp(v,e,a)}f.prototype.mulTo=function(e,a){var h,o=this.length+e.length;return this.length===10&&e.length===10?h=D(this,e,a):o<63?h=R(this,e,a):o<1024?h=bt(this,e,a):h=_t(this,e,a),h};function gt(v,e){this.x=v,this.y=e}gt.prototype.makeRBT=function(e){for(var a=new Array(e),h=f.prototype._countBits(e)-1,o=0;o<e;o++)a[o]=this.revBin(o,h,e);return a},gt.prototype.revBin=function(e,a,h){if(e===0||e===h-1)return e;for(var o=0,u=0;u<a;u++)o|=(e&1)<<a-u-1,e>>=1;return o},gt.prototype.permute=function(e,a,h,o,u,p){for(var m=0;m<p;m++)o[m]=a[e[m]],u[m]=h[e[m]]},gt.prototype.transform=function(e,a,h,o,u,p){this.permute(p,e,a,h,o,u);for(var m=1;m<u;m<<=1)for(var l=m<<1,n=Math.cos(2*Math.PI/l),d=Math.sin(2*Math.PI/l),M=0;M<u;M+=l)for(var y=n,x=d,B=0;B<m;B++){var E=h[M+B],k=o[M+B],wt=h[M+B+m],T=o[M+B+m],P=y*wt-x*T;T=y*T+x*wt,wt=P,h[M+B]=E+wt,o[M+B]=k+T,h[M+B+m]=E-wt,o[M+B+m]=k-T,B!==l&&(P=n*y-d*x,x=n*x+d*y,y=P)}},gt.prototype.guessLen13b=function(e,a){var h=Math.max(a,e)|1,o=h&1,u=0;for(h=h/2|0;h;h=h>>>1)u++;return 1<<u+1+o},gt.prototype.conjugate=function(e,a,h){if(!(h<=1))for(var o=0;o<h/2;o++){var u=e[o];e[o]=e[h-o-1],e[h-o-1]=u,u=a[o],a[o]=-a[h-o-1],a[h-o-1]=-u}},gt.prototype.normalize13b=function(e,a){for(var h=0,o=0;o<a/2;o++){var u=Math.round(e[2*o+1]/a)*8192+Math.round(e[2*o]/a)+h;e[o]=u&67108863,u<67108864?h=0:h=u/67108864|0}return e},gt.prototype.convert13b=function(e,a,h,o){for(var u=0,p=0;p<a;p++)u=u+(e[p]|0),h[2*p]=u&8191,u=u>>>13,h[2*p+1]=u&8191,u=u>>>13;for(p=2*a;p<o;++p)h[p]=0;r(u===0),r((u&-8192)===0)},gt.prototype.stub=function(e){for(var a=new Array(e),h=0;h<e;h++)a[h]=0;return a},gt.prototype.mulp=function(e,a,h){var o=2*this.guessLen13b(e.length,a.length),u=this.makeRBT(o),p=this.stub(o),m=new Array(o),l=new Array(o),n=new Array(o),d=new Array(o),M=new Array(o),y=new Array(o),x=h.words;x.length=o,this.convert13b(e.words,e.length,m,o),this.convert13b(a.words,a.length,d,o),this.transform(m,p,l,n,o,u),this.transform(d,p,M,y,o,u);for(var B=0;B<o;B++){var E=l[B]*M[B]-n[B]*y[B];n[B]=l[B]*y[B]+n[B]*M[B],l[B]=E}return this.conjugate(l,n,o),this.transform(l,n,x,p,o,u),this.conjugate(x,p,o),this.normalize13b(x,o),h.negative=e.negative^a.negative,h.length=e.length+a.length,h.strip()},f.prototype.mul=function(e){var a=new f(null);return a.words=new Array(this.length+e.length),this.mulTo(e,a)},f.prototype.mulf=function(e){var a=new f(null);return a.words=new Array(this.length+e.length),_t(this,e,a)},f.prototype.imul=function(e){return this.clone().mulTo(e,this)},f.prototype.imuln=function(e){r(typeof e=="number"),r(e<67108864);for(var a=0,h=0;h<this.length;h++){var o=(this.words[h]|0)*e,u=(o&67108863)+(a&67108863);a>>=26,a+=o/67108864|0,a+=u>>>26,this.words[h]=u&67108863}return a!==0&&(this.words[h]=a,this.length++),this.length=e===0?1:this.length,this},f.prototype.muln=function(e){return this.clone().imuln(e)},f.prototype.sqr=function(){return this.mul(this)},f.prototype.isqr=function(){return this.imul(this.clone())},f.prototype.pow=function(e){var a=q(e);if(a.length===0)return new f(1);for(var h=this,o=0;o<a.length&&a[o]===0;o++,h=h.sqr());if(++o<a.length)for(var u=h.sqr();o<a.length;o++,u=u.sqr())a[o]!==0&&(h=h.mul(u));return h},f.prototype.iushln=function(e){r(typeof e=="number"&&e>=0);var a=e%26,h=(e-a)/26,o=67108863>>>26-a<<26-a,u;if(a!==0){var p=0;for(u=0;u<this.length;u++){var m=this.words[u]&o,l=(this.words[u]|0)-m<<a;this.words[u]=l|p,p=m>>>26-a}p&&(this.words[u]=p,this.length++)}if(h!==0){for(u=this.length-1;u>=0;u--)this.words[u+h]=this.words[u];for(u=0;u<h;u++)this.words[u]=0;this.length+=h}return this.strip()},f.prototype.ishln=function(e){return r(this.negative===0),this.iushln(e)},f.prototype.iushrn=function(e,a,h){r(typeof e=="number"&&e>=0);var o;a?o=(a-a%26)/26:o=0;var u=e%26,p=Math.min((e-u)/26,this.length),m=67108863^67108863>>>u<<u,l=h;if(o-=p,o=Math.max(0,o),l){for(var n=0;n<p;n++)l.words[n]=this.words[n];l.length=p}if(p!==0)if(this.length>p)for(this.length-=p,n=0;n<this.length;n++)this.words[n]=this.words[n+p];else this.words[0]=0,this.length=1;var d=0;for(n=this.length-1;n>=0&&(d!==0||n>=o);n--){var M=this.words[n]|0;this.words[n]=d<<26-u|M>>>u,d=M&m}return l&&d!==0&&(l.words[l.length++]=d),this.length===0&&(this.words[0]=0,this.length=1),this.strip()},f.prototype.ishrn=function(e,a,h){return r(this.negative===0),this.iushrn(e,a,h)},f.prototype.shln=function(e){return this.clone().ishln(e)},f.prototype.ushln=function(e){return this.clone().iushln(e)},f.prototype.shrn=function(e){return this.clone().ishrn(e)},f.prototype.ushrn=function(e){return this.clone().iushrn(e)},f.prototype.testn=function(e){r(typeof e=="number"&&e>=0);var a=e%26,h=(e-a)/26,o=1<<a;if(this.length<=h)return!1;var u=this.words[h];return!!(u&o)},f.prototype.imaskn=function(e){r(typeof e=="number"&&e>=0);var a=e%26,h=(e-a)/26;if(r(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(a!==0&&h++,this.length=Math.min(h,this.length),a!==0){var o=67108863^67108863>>>a<<a;this.words[this.length-1]&=o}return this.strip()},f.prototype.maskn=function(e){return this.clone().imaskn(e)},f.prototype.iaddn=function(e){return r(typeof e=="number"),r(e<67108864),e<0?this.isubn(-e):this.negative!==0?this.length===1&&(this.words[0]|0)<e?(this.words[0]=e-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(e),this.negative=1,this):this._iaddn(e)},f.prototype._iaddn=function(e){this.words[0]+=e;for(var a=0;a<this.length&&this.words[a]>=67108864;a++)this.words[a]-=67108864,a===this.length-1?this.words[a+1]=1:this.words[a+1]++;return this.length=Math.max(this.length,a+1),this},f.prototype.isubn=function(e){if(r(typeof e=="number"),r(e<67108864),e<0)return this.iaddn(-e);if(this.negative!==0)return this.negative=0,this.iaddn(e),this.negative=1,this;if(this.words[0]-=e,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var a=0;a<this.length&&this.words[a]<0;a++)this.words[a]+=67108864,this.words[a+1]-=1;return this.strip()},f.prototype.addn=function(e){return this.clone().iaddn(e)},f.prototype.subn=function(e){return this.clone().isubn(e)},f.prototype.iabs=function(){return this.negative=0,this},f.prototype.abs=function(){return this.clone().iabs()},f.prototype._ishlnsubmul=function(e,a,h){var o=e.length+h,u;this._expand(o);var p,m=0;for(u=0;u<e.length;u++){p=(this.words[u+h]|0)+m;var l=(e.words[u]|0)*a;p-=l&67108863,m=(p>>26)-(l/67108864|0),this.words[u+h]=p&67108863}for(;u<this.length-h;u++)p=(this.words[u+h]|0)+m,m=p>>26,this.words[u+h]=p&67108863;if(m===0)return this.strip();for(r(m===-1),m=0,u=0;u<this.length;u++)p=-(this.words[u]|0)+m,m=p>>26,this.words[u]=p&67108863;return this.negative=1,this.strip()},f.prototype._wordDiv=function(e,a){var h=this.length-e.length,o=this.clone(),u=e,p=u.words[u.length-1]|0,m=this._countBits(p);h=26-m,h!==0&&(u=u.ushln(h),o.iushln(h),p=u.words[u.length-1]|0);var l=o.length-u.length,n;if(a!=="mod"){n=new f(null),n.length=l+1,n.words=new Array(n.length);for(var d=0;d<n.length;d++)n.words[d]=0}var M=o.clone()._ishlnsubmul(u,1,l);M.negative===0&&(o=M,n&&(n.words[l]=1));for(var y=l-1;y>=0;y--){var x=(o.words[u.length+y]|0)*67108864+(o.words[u.length+y-1]|0);for(x=Math.min(x/p|0,67108863),o._ishlnsubmul(u,x,y);o.negative!==0;)x--,o.negative=0,o._ishlnsubmul(u,1,y),o.isZero()||(o.negative^=1);n&&(n.words[y]=x)}return n&&n.strip(),o.strip(),a!=="div"&&h!==0&&o.iushrn(h),{div:n||null,mod:o}},f.prototype.divmod=function(e,a,h){if(r(!e.isZero()),this.isZero())return{div:new f(0),mod:new f(0)};var o,u,p;return this.negative!==0&&e.negative===0?(p=this.neg().divmod(e,a),a!=="mod"&&(o=p.div.neg()),a!=="div"&&(u=p.mod.neg(),h&&u.negative!==0&&u.iadd(e)),{div:o,mod:u}):this.negative===0&&e.negative!==0?(p=this.divmod(e.neg(),a),a!=="mod"&&(o=p.div.neg()),{div:o,mod:p.mod}):(this.negative&e.negative)!==0?(p=this.neg().divmod(e.neg(),a),a!=="div"&&(u=p.mod.neg(),h&&u.negative!==0&&u.isub(e)),{div:p.div,mod:u}):e.length>this.length||this.cmp(e)<0?{div:new f(0),mod:this}:e.length===1?a==="div"?{div:this.divn(e.words[0]),mod:null}:a==="mod"?{div:null,mod:new f(this.modn(e.words[0]))}:{div:this.divn(e.words[0]),mod:new f(this.modn(e.words[0]))}:this._wordDiv(e,a)},f.prototype.div=function(e){return this.divmod(e,"div",!1).div},f.prototype.mod=function(e){return this.divmod(e,"mod",!1).mod},f.prototype.umod=function(e){return this.divmod(e,"mod",!0).mod},f.prototype.divRound=function(e){var a=this.divmod(e);if(a.mod.isZero())return a.div;var h=a.div.negative!==0?a.mod.isub(e):a.mod,o=e.ushrn(1),u=e.andln(1),p=h.cmp(o);return p<0||u===1&&p===0?a.div:a.div.negative!==0?a.div.isubn(1):a.div.iaddn(1)},f.prototype.modn=function(e){r(e<=67108863);for(var a=(1<<26)%e,h=0,o=this.length-1;o>=0;o--)h=(a*h+(this.words[o]|0))%e;return h},f.prototype.idivn=function(e){r(e<=67108863);for(var a=0,h=this.length-1;h>=0;h--){var o=(this.words[h]|0)+a*67108864;this.words[h]=o/e|0,a=o%e}return this.strip()},f.prototype.divn=function(e){return this.clone().idivn(e)},f.prototype.egcd=function(e){r(e.negative===0),r(!e.isZero());var a=this,h=e.clone();a.negative!==0?a=a.umod(e):a=a.clone();for(var o=new f(1),u=new f(0),p=new f(0),m=new f(1),l=0;a.isEven()&&h.isEven();)a.iushrn(1),h.iushrn(1),++l;for(var n=h.clone(),d=a.clone();!a.isZero();){for(var M=0,y=1;(a.words[0]&y)===0&&M<26;++M,y<<=1);if(M>0)for(a.iushrn(M);M-- >0;)(o.isOdd()||u.isOdd())&&(o.iadd(n),u.isub(d)),o.iushrn(1),u.iushrn(1);for(var x=0,B=1;(h.words[0]&B)===0&&x<26;++x,B<<=1);if(x>0)for(h.iushrn(x);x-- >0;)(p.isOdd()||m.isOdd())&&(p.iadd(n),m.isub(d)),p.iushrn(1),m.iushrn(1);a.cmp(h)>=0?(a.isub(h),o.isub(p),u.isub(m)):(h.isub(a),p.isub(o),m.isub(u))}return{a:p,b:m,gcd:h.iushln(l)}},f.prototype._invmp=function(e){r(e.negative===0),r(!e.isZero());var a=this,h=e.clone();a.negative!==0?a=a.umod(e):a=a.clone();for(var o=new f(1),u=new f(0),p=h.clone();a.cmpn(1)>0&&h.cmpn(1)>0;){for(var m=0,l=1;(a.words[0]&l)===0&&m<26;++m,l<<=1);if(m>0)for(a.iushrn(m);m-- >0;)o.isOdd()&&o.iadd(p),o.iushrn(1);for(var n=0,d=1;(h.words[0]&d)===0&&n<26;++n,d<<=1);if(n>0)for(h.iushrn(n);n-- >0;)u.isOdd()&&u.iadd(p),u.iushrn(1);a.cmp(h)>=0?(a.isub(h),o.isub(u)):(h.isub(a),u.isub(o))}var M;return a.cmpn(1)===0?M=o:M=u,M.cmpn(0)<0&&M.iadd(e),M},f.prototype.gcd=function(e){if(this.isZero())return e.abs();if(e.isZero())return this.abs();var a=this.clone(),h=e.clone();a.negative=0,h.negative=0;for(var o=0;a.isEven()&&h.isEven();o++)a.iushrn(1),h.iushrn(1);do{for(;a.isEven();)a.iushrn(1);for(;h.isEven();)h.iushrn(1);var u=a.cmp(h);if(u<0){var p=a;a=h,h=p}else if(u===0||h.cmpn(1)===0)break;a.isub(h)}while(!0);return h.iushln(o)},f.prototype.invm=function(e){return this.egcd(e).a.umod(e)},f.prototype.isEven=function(){return(this.words[0]&1)===0},f.prototype.isOdd=function(){return(this.words[0]&1)===1},f.prototype.andln=function(e){return this.words[0]&e},f.prototype.bincn=function(e){r(typeof e=="number");var a=e%26,h=(e-a)/26,o=1<<a;if(this.length<=h)return this._expand(h+1),this.words[h]|=o,this;for(var u=o,p=h;u!==0&&p<this.length;p++){var m=this.words[p]|0;m+=u,u=m>>>26,m&=67108863,this.words[p]=m}return u!==0&&(this.words[p]=u,this.length++),this},f.prototype.isZero=function(){return this.length===1&&this.words[0]===0},f.prototype.cmpn=function(e){var a=e<0;if(this.negative!==0&&!a)return-1;if(this.negative===0&&a)return 1;this.strip();var h;if(this.length>1)h=1;else{a&&(e=-e),r(e<=67108863,"Number is too big");var o=this.words[0]|0;h=o===e?0:o<e?-1:1}return this.negative!==0?-h|0:h},f.prototype.cmp=function(e){if(this.negative!==0&&e.negative===0)return-1;if(this.negative===0&&e.negative!==0)return 1;var a=this.ucmp(e);return this.negative!==0?-a|0:a},f.prototype.ucmp=function(e){if(this.length>e.length)return 1;if(this.length<e.length)return-1;for(var a=0,h=this.length-1;h>=0;h--){var o=this.words[h]|0,u=e.words[h]|0;if(o!==u){o<u?a=-1:o>u&&(a=1);break}}return a},f.prototype.gtn=function(e){return this.cmpn(e)===1},f.prototype.gt=function(e){return this.cmp(e)===1},f.prototype.gten=function(e){return this.cmpn(e)>=0},f.prototype.gte=function(e){return this.cmp(e)>=0},f.prototype.ltn=function(e){return this.cmpn(e)===-1},f.prototype.lt=function(e){return this.cmp(e)===-1},f.prototype.lten=function(e){return this.cmpn(e)<=0},f.prototype.lte=function(e){return this.cmp(e)<=0},f.prototype.eqn=function(e){return this.cmpn(e)===0},f.prototype.eq=function(e){return this.cmp(e)===0},f.red=function(e){return new O(e)},f.prototype.toRed=function(e){return r(!this.red,"Already a number in reduction context"),r(this.negative===0,"red works only with positives"),e.convertTo(this)._forceRed(e)},f.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},f.prototype._forceRed=function(e){return this.red=e,this},f.prototype.forceRed=function(e){return r(!this.red,"Already a number in reduction context"),this._forceRed(e)},f.prototype.redAdd=function(e){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},f.prototype.redIAdd=function(e){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},f.prototype.redSub=function(e){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},f.prototype.redISub=function(e){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},f.prototype.redShl=function(e){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},f.prototype.redMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},f.prototype.redIMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},f.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},f.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},f.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},f.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},f.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},f.prototype.redPow=function(e){return r(this.red&&!e.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,e)};var xt={k256:null,p224:null,p192:null,p25519:null};function Mt(v,e){this.name=v,this.p=new f(e,16),this.n=this.p.bitLength(),this.k=new f(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}Mt.prototype._tmp=function(){var e=new f(null);return e.words=new Array(Math.ceil(this.n/13)),e},Mt.prototype.ireduce=function(e){var a=e,h;do this.split(a,this.tmp),a=this.imulK(a),a=a.iadd(this.tmp),h=a.bitLength();while(h>this.n);var o=h<this.n?-1:a.ucmp(this.p);return o===0?(a.words[0]=0,a.length=1):o>0?a.isub(this.p):a.strip!==void 0?a.strip():a._strip(),a},Mt.prototype.split=function(e,a){e.iushrn(this.n,0,a)},Mt.prototype.imulK=function(e){return e.imul(this.k)};function At(){Mt.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}s(At,Mt),At.prototype.split=function(e,a){for(var h=4194303,o=Math.min(e.length,9),u=0;u<o;u++)a.words[u]=e.words[u];if(a.length=o,e.length<=9){e.words[0]=0,e.length=1;return}var p=e.words[9];for(a.words[a.length++]=p&h,u=10;u<e.length;u++){var m=e.words[u]|0;e.words[u-10]=(m&h)<<4|p>>>22,p=m}p>>>=22,e.words[u-10]=p,p===0&&e.length>10?e.length-=10:e.length-=9},At.prototype.imulK=function(e){e.words[e.length]=0,e.words[e.length+1]=0,e.length+=2;for(var a=0,h=0;h<e.length;h++){var o=e.words[h]|0;a+=o*977,e.words[h]=a&67108863,a=o*64+(a/67108864|0)}return e.words[e.length-1]===0&&(e.length--,e.words[e.length-1]===0&&e.length--),e};function Tt(){Mt.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}s(Tt,Mt);function Bt(){Mt.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}s(Bt,Mt);function Pt(){Mt.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}s(Pt,Mt),Pt.prototype.imulK=function(e){for(var a=0,h=0;h<e.length;h++){var o=(e.words[h]|0)*19+a,u=o&67108863;o>>>=26,e.words[h]=u,a=o}return a!==0&&(e.words[e.length++]=a),e},f._prime=function(e){if(xt[e])return xt[e];var a;if(e==="k256")a=new At;else if(e==="p224")a=new Tt;else if(e==="p192")a=new Bt;else if(e==="p25519")a=new Pt;else throw new Error("Unknown prime "+e);return xt[e]=a,a};function O(v){if(typeof v=="string"){var e=f._prime(v);this.m=e.p,this.prime=e}else r(v.gtn(1),"modulus must be greater than 1"),this.m=v,this.prime=null}O.prototype._verify1=function(e){r(e.negative===0,"red works only with positives"),r(e.red,"red works only with red numbers")},O.prototype._verify2=function(e,a){r((e.negative|a.negative)===0,"red works only with positives"),r(e.red&&e.red===a.red,"red works only with red numbers")},O.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):e.umod(this.m)._forceRed(this)},O.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},O.prototype.add=function(e,a){this._verify2(e,a);var h=e.add(a);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},O.prototype.iadd=function(e,a){this._verify2(e,a);var h=e.iadd(a);return h.cmp(this.m)>=0&&h.isub(this.m),h},O.prototype.sub=function(e,a){this._verify2(e,a);var h=e.sub(a);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},O.prototype.isub=function(e,a){this._verify2(e,a);var h=e.isub(a);return h.cmpn(0)<0&&h.iadd(this.m),h},O.prototype.shl=function(e,a){return this._verify1(e),this.imod(e.ushln(a))},O.prototype.imul=function(e,a){return this._verify2(e,a),this.imod(e.imul(a))},O.prototype.mul=function(e,a){return this._verify2(e,a),this.imod(e.mul(a))},O.prototype.isqr=function(e){return this.imul(e,e.clone())},O.prototype.sqr=function(e){return this.mul(e,e)},O.prototype.sqrt=function(e){if(e.isZero())return e.clone();var a=this.m.andln(3);if(r(a%2===1),a===3){var h=this.m.add(new f(1)).iushrn(2);return this.pow(e,h)}for(var o=this.m.subn(1),u=0;!o.isZero()&&o.andln(1)===0;)u++,o.iushrn(1);r(!o.isZero());var p=new f(1).toRed(this),m=p.redNeg(),l=this.m.subn(1).iushrn(1),n=this.m.bitLength();for(n=new f(2*n*n).toRed(this);this.pow(n,l).cmp(m)!==0;)n.redIAdd(m);for(var d=this.pow(n,o),M=this.pow(e,o.addn(1).iushrn(1)),y=this.pow(e,o),x=u;y.cmp(p)!==0;){for(var B=y,E=0;B.cmp(p)!==0;E++)B=B.redSqr();r(E<x);var k=this.pow(d,new f(1).iushln(x-E-1));M=M.redMul(k),d=k.redSqr(),y=y.redMul(d),x=E}return M},O.prototype.invm=function(e){var a=e._invmp(this.m);return a.negative!==0?(a.negative=0,this.imod(a).redNeg()):this.imod(a)},O.prototype.pow=function(e,a){if(a.isZero())return new f(1).toRed(this);if(a.cmpn(1)===0)return e.clone();var h=4,o=new Array(1<<h);o[0]=new f(1).toRed(this),o[1]=e;for(var u=2;u<o.length;u++)o[u]=this.mul(o[u-1],e);var p=o[0],m=0,l=0,n=a.bitLength()%26;for(n===0&&(n=26),u=a.length-1;u>=0;u--){for(var d=a.words[u],M=n-1;M>=0;M--){var y=d>>M&1;if(p!==o[0]&&(p=this.sqr(p)),y===0&&m===0){l=0;continue}m<<=1,m|=y,l++,!(l!==h&&(u!==0||M!==0))&&(p=this.mul(p,o[m]),l=0,m=0)}n=26}return p},O.prototype.convertTo=function(e){var a=e.umod(this.m);return a===e?a.clone():a},O.prototype.convertFrom=function(e){var a=e.clone();return a.red=null,a},f.mont=function(e){return new St(e)};function St(v){O.call(this,v),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new f(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)}s(St,O),St.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},St.prototype.convertFrom=function(e){var a=this.imod(e.mul(this.rinv));return a.red=null,a},St.prototype.imul=function(e,a){if(e.isZero()||a.isZero())return e.words[0]=0,e.length=1,e;var h=e.imul(a),o=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),u=h.isub(o).iushrn(this.shift),p=u;return u.cmp(this.m)>=0?p=u.isub(this.m):u.cmpn(0)<0&&(p=u.iadd(this.m)),p._forceRed(this)},St.prototype.mul=function(e,a){if(e.isZero()||a.isZero())return new f(0)._forceRed(this);var h=e.mul(a),o=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),u=h.isub(o).iushrn(this.shift),p=u;return u.cmp(this.m)>=0?p=u.isub(this.m):u.cmpn(0)<0&&(p=u.iadd(this.m)),p._forceRed(this)},St.prototype.invm=function(e){var a=this.imod(e._invmp(this.m).mul(this.r2));return a._forceRed(this)}})(typeof Jh>"u"||Jh,lg)});var pg=I((NA,cg)=>{"use strict";var v7=L0(),m7=dg();cg.exports=function(t){return new ln(t)};var cr={secp256k1:{name:"secp256k1",byteLength:32},secp224r1:{name:"p224",byteLength:28},prime256v1:{name:"p256",byteLength:32},prime192v1:{name:"p192",byteLength:24},ed25519:{name:"ed25519",byteLength:32},secp384r1:{name:"p384",byteLength:48},secp521r1:{name:"p521",byteLength:66}};cr.p224=cr.secp224r1;cr.p256=cr.secp256r1=cr.prime256v1;cr.p192=cr.secp192r1=cr.prime192v1;cr.p384=cr.secp384r1;cr.p521=cr.secp521r1;function ln(i){this.curveType=cr[i],this.curveType||(this.curveType={name:i}),this.curve=new v7.ec(this.curveType.name),this.keys=void 0}ln.prototype.generateKeys=function(i,t){return this.keys=this.curve.genKeyPair(),this.getPublicKey(i,t)};ln.prototype.computeSecret=function(i,t,r){t=t||"utf8",Buffer.isBuffer(i)||(i=new Buffer(i,t));var s=this.curve.keyFromPublic(i).getPublic(),f=s.mul(this.keys.getPrivate()).getX();return Xh(f,r,this.curveType.byteLength)};ln.prototype.getPublicKey=function(i,t){var r=this.keys.getPublic(t==="compressed",!0);return t==="hybrid"&&(r[r.length-1]%2?r[0]=7:r[0]=6),Xh(r,i)};ln.prototype.getPrivateKey=function(i){return Xh(this.keys.getPrivate(),i)};ln.prototype.setPublicKey=function(i,t){return t=t||"utf8",Buffer.isBuffer(i)||(i=new Buffer(i,t)),this.keys._importPublic(i),this};ln.prototype.setPrivateKey=function(i,t){t=t||"utf8",Buffer.isBuffer(i)||(i=new Buffer(i,t));var r=new m7(i);return r=r.toString(16),this.keys=this.curve.genKeyPair(),this.keys._importPrivate(r),this};function Xh(i,t,r){Array.isArray(i)||(i=i.toArray());var s=new Buffer(i);if(r&&s.length<r){var f=new Buffer(r-s.length);f.fill(0),s=Buffer.concat([f,s])}return t?s.toString(t):s}});var Qh=I((DA,vg)=>{"use strict";var g7=bn(),Yh=It().Buffer;vg.exports=function(i,t){for(var r=Yh.alloc(0),s=0,f;r.length<t;)f=y7(s++),r=Yh.concat([r,g7("sha1").update(i).update(f).digest()]);return r.slice(0,t)};function y7(i){var t=Yh.allocUnsafe(4);return t.writeUInt32BE(i,0),t}});var tu=I((LA,mg)=>{"use strict";mg.exports=function(t,r){for(var s=t.length,f=-1;++f<s;)t[f]^=r[f];return t}});var W0=I((gg,eu)=>{"use strict";(function(i,t){"use strict";function r(v,e){if(!v)throw new Error(e||"Assertion failed")}function s(v,e){v.super_=e;var a=function(){};a.prototype=e.prototype,v.prototype=new a,v.prototype.constructor=v}function f(v,e,a){if(f.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((e==="le"||e==="be")&&(a=e,e=10),this._init(v||0,e||10,a||"be"))}typeof i=="object"?i.exports=f:t.BN=f,f.BN=f,f.wordSize=26;var c;try{typeof window<"u"&&typeof window.Buffer<"u"?c=window.Buffer:c=qi().Buffer}catch{}f.isBN=function(e){return e instanceof f?!0:e!==null&&typeof e=="object"&&e.constructor.wordSize===f.wordSize&&Array.isArray(e.words)},f.max=function(e,a){return e.cmp(a)>0?e:a},f.min=function(e,a){return e.cmp(a)<0?e:a},f.prototype._init=function(e,a,h){if(typeof e=="number")return this._initNumber(e,a,h);if(typeof e=="object")return this._initArray(e,a,h);a==="hex"&&(a=16),r(a===(a|0)&&a>=2&&a<=36),e=e.toString().replace(/\s+/g,"");var o=0;e[0]==="-"&&(o++,this.negative=1),o<e.length&&(a===16?this._parseHex(e,o,h):(this._parseBase(e,a,o),h==="le"&&this._initArray(this.toArray(),a,h)))},f.prototype._initNumber=function(e,a,h){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[e&67108863],this.length=1):e<4503599627370496?(this.words=[e&67108863,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[e&67108863,e/67108864&67108863,1],this.length=3),h==="le"&&this._initArray(this.toArray(),a,h)},f.prototype._initArray=function(e,a,h){if(r(typeof e.length=="number"),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var o=0;o<this.length;o++)this.words[o]=0;var u,p,m=0;if(h==="be")for(o=e.length-1,u=0;o>=0;o-=3)p=e[o]|e[o-1]<<8|e[o-2]<<16,this.words[u]|=p<<m&67108863,this.words[u+1]=p>>>26-m&67108863,m+=24,m>=26&&(m-=26,u++);else if(h==="le")for(o=0,u=0;o<e.length;o+=3)p=e[o]|e[o+1]<<8|e[o+2]<<16,this.words[u]|=p<<m&67108863,this.words[u+1]=p>>>26-m&67108863,m+=24,m>=26&&(m-=26,u++);return this.strip()};function g(v,e){var a=v.charCodeAt(e);return a>=65&&a<=70?a-55:a>=97&&a<=102?a-87:a-48&15}function b(v,e,a){var h=g(v,a);return a-1>=e&&(h|=g(v,a-1)<<4),h}f.prototype._parseHex=function(e,a,h){this.length=Math.ceil((e.length-a)/6),this.words=new Array(this.length);for(var o=0;o<this.length;o++)this.words[o]=0;var u=0,p=0,m;if(h==="be")for(o=e.length-1;o>=a;o-=2)m=b(e,a,o)<<u,this.words[p]|=m&67108863,u>=18?(u-=18,p+=1,this.words[p]|=m>>>26):u+=8;else{var l=e.length-a;for(o=l%2===0?a+1:a;o<e.length;o+=2)m=b(e,a,o)<<u,this.words[p]|=m&67108863,u>=18?(u-=18,p+=1,this.words[p]|=m>>>26):u+=8}this.strip()};function w(v,e,a,h){for(var o=0,u=Math.min(v.length,a),p=e;p<u;p++){var m=v.charCodeAt(p)-48;o*=h,m>=49?o+=m-49+10:m>=17?o+=m-17+10:o+=m}return o}f.prototype._parseBase=function(e,a,h){this.words=[0],this.length=1;for(var o=0,u=1;u<=67108863;u*=a)o++;o--,u=u/a|0;for(var p=e.length-h,m=p%o,l=Math.min(p,p-m)+h,n=0,d=h;d<l;d+=o)n=w(e,d,d+o,a),this.imuln(u),this.words[0]+n<67108864?this.words[0]+=n:this._iaddn(n);if(m!==0){var M=1;for(n=w(e,d,e.length,a),d=0;d<m;d++)M*=a;this.imuln(M),this.words[0]+n<67108864?this.words[0]+=n:this._iaddn(n)}this.strip()},f.prototype.copy=function(e){e.words=new Array(this.length);for(var a=0;a<this.length;a++)e.words[a]=this.words[a];e.length=this.length,e.negative=this.negative,e.red=this.red},f.prototype.clone=function(){var e=new f(null);return this.copy(e),e},f.prototype._expand=function(e){for(;this.length<e;)this.words[this.length++]=0;return this},f.prototype.strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},f.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},f.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var _=["","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"],S=[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],A=[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];f.prototype.toString=function(e,a){e=e||10,a=a|0||1;var h;if(e===16||e==="hex"){h="";for(var o=0,u=0,p=0;p<this.length;p++){var m=this.words[p],l=((m<<o|u)&16777215).toString(16);u=m>>>24-o&16777215,o+=2,o>=26&&(o-=26,p--),u!==0||p!==this.length-1?h=_[6-l.length]+l+h:h=l+h}for(u!==0&&(h=u.toString(16)+h);h.length%a!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}if(e===(e|0)&&e>=2&&e<=36){var n=S[e],d=A[e];h="";var M=this.clone();for(M.negative=0;!M.isZero();){var y=M.modn(d).toString(e);M=M.idivn(d),M.isZero()?h=y+h:h=_[n-y.length]+y+h}for(this.isZero()&&(h="0"+h);h.length%a!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}r(!1,"Base should be between 2 and 36")},f.prototype.toNumber=function(){var e=this.words[0];return this.length===2?e+=this.words[1]*67108864:this.length===3&&this.words[2]===1?e+=4503599627370496+this.words[1]*67108864:this.length>2&&r(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-e:e},f.prototype.toJSON=function(){return this.toString(16)},f.prototype.toBuffer=function(e,a){return r(typeof c<"u"),this.toArrayLike(c,e,a)},f.prototype.toArray=function(e,a){return this.toArrayLike(Array,e,a)},f.prototype.toArrayLike=function(e,a,h){var o=this.byteLength(),u=h||Math.max(1,o);r(o<=u,"byte array longer than desired length"),r(u>0,"Requested array length <= 0"),this.strip();var p=a==="le",m=new e(u),l,n,d=this.clone();if(p){for(n=0;!d.isZero();n++)l=d.andln(255),d.iushrn(8),m[n]=l;for(;n<u;n++)m[n]=0}else{for(n=0;n<u-o;n++)m[n]=0;for(n=0;!d.isZero();n++)l=d.andln(255),d.iushrn(8),m[u-n-1]=l}return m},Math.clz32?f.prototype._countBits=function(e){return 32-Math.clz32(e)}:f.prototype._countBits=function(e){var a=e,h=0;return a>=4096&&(h+=13,a>>>=13),a>=64&&(h+=7,a>>>=7),a>=8&&(h+=4,a>>>=4),a>=2&&(h+=2,a>>>=2),h+a},f.prototype._zeroBits=function(e){if(e===0)return 26;var a=e,h=0;return(a&8191)===0&&(h+=13,a>>>=13),(a&127)===0&&(h+=7,a>>>=7),(a&15)===0&&(h+=4,a>>>=4),(a&3)===0&&(h+=2,a>>>=2),(a&1)===0&&h++,h},f.prototype.bitLength=function(){var e=this.words[this.length-1],a=this._countBits(e);return(this.length-1)*26+a};function q(v){for(var e=new Array(v.bitLength()),a=0;a<e.length;a++){var h=a/26|0,o=a%26;e[a]=(v.words[h]&1<<o)>>>o}return e}f.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,a=0;a<this.length;a++){var h=this._zeroBits(this.words[a]);if(e+=h,h!==26)break}return e},f.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},f.prototype.toTwos=function(e){return this.negative!==0?this.abs().inotn(e).iaddn(1):this.clone()},f.prototype.fromTwos=function(e){return this.testn(e-1)?this.notn(e).iaddn(1).ineg():this.clone()},f.prototype.isNeg=function(){return this.negative!==0},f.prototype.neg=function(){return this.clone().ineg()},f.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},f.prototype.iuor=function(e){for(;this.length<e.length;)this.words[this.length++]=0;for(var a=0;a<e.length;a++)this.words[a]=this.words[a]|e.words[a];return this.strip()},f.prototype.ior=function(e){return r((this.negative|e.negative)===0),this.iuor(e)},f.prototype.or=function(e){return this.length>e.length?this.clone().ior(e):e.clone().ior(this)},f.prototype.uor=function(e){return this.length>e.length?this.clone().iuor(e):e.clone().iuor(this)},f.prototype.iuand=function(e){var a;this.length>e.length?a=e:a=this;for(var h=0;h<a.length;h++)this.words[h]=this.words[h]&e.words[h];return this.length=a.length,this.strip()},f.prototype.iand=function(e){return r((this.negative|e.negative)===0),this.iuand(e)},f.prototype.and=function(e){return this.length>e.length?this.clone().iand(e):e.clone().iand(this)},f.prototype.uand=function(e){return this.length>e.length?this.clone().iuand(e):e.clone().iuand(this)},f.prototype.iuxor=function(e){var a,h;this.length>e.length?(a=this,h=e):(a=e,h=this);for(var o=0;o<h.length;o++)this.words[o]=a.words[o]^h.words[o];if(this!==a)for(;o<a.length;o++)this.words[o]=a.words[o];return this.length=a.length,this.strip()},f.prototype.ixor=function(e){return r((this.negative|e.negative)===0),this.iuxor(e)},f.prototype.xor=function(e){return this.length>e.length?this.clone().ixor(e):e.clone().ixor(this)},f.prototype.uxor=function(e){return this.length>e.length?this.clone().iuxor(e):e.clone().iuxor(this)},f.prototype.inotn=function(e){r(typeof e=="number"&&e>=0);var a=Math.ceil(e/26)|0,h=e%26;this._expand(a),h>0&&a--;for(var o=0;o<a;o++)this.words[o]=~this.words[o]&67108863;return h>0&&(this.words[o]=~this.words[o]&67108863>>26-h),this.strip()},f.prototype.notn=function(e){return this.clone().inotn(e)},f.prototype.setn=function(e,a){r(typeof e=="number"&&e>=0);var h=e/26|0,o=e%26;return this._expand(h+1),a?this.words[h]=this.words[h]|1<<o:this.words[h]=this.words[h]&~(1<<o),this.strip()},f.prototype.iadd=function(e){var a;if(this.negative!==0&&e.negative===0)return this.negative=0,a=this.isub(e),this.negative^=1,this._normSign();if(this.negative===0&&e.negative!==0)return e.negative=0,a=this.isub(e),e.negative=1,a._normSign();var h,o;this.length>e.length?(h=this,o=e):(h=e,o=this);for(var u=0,p=0;p<o.length;p++)a=(h.words[p]|0)+(o.words[p]|0)+u,this.words[p]=a&67108863,u=a>>>26;for(;u!==0&&p<h.length;p++)a=(h.words[p]|0)+u,this.words[p]=a&67108863,u=a>>>26;if(this.length=h.length,u!==0)this.words[this.length]=u,this.length++;else if(h!==this)for(;p<h.length;p++)this.words[p]=h.words[p];return this},f.prototype.add=function(e){var a;return e.negative!==0&&this.negative===0?(e.negative=0,a=this.sub(e),e.negative^=1,a):e.negative===0&&this.negative!==0?(this.negative=0,a=e.sub(this),this.negative=1,a):this.length>e.length?this.clone().iadd(e):e.clone().iadd(this)},f.prototype.isub=function(e){if(e.negative!==0){e.negative=0;var a=this.iadd(e);return e.negative=1,a._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(e),this.negative=1,this._normSign();var h=this.cmp(e);if(h===0)return this.negative=0,this.length=1,this.words[0]=0,this;var o,u;h>0?(o=this,u=e):(o=e,u=this);for(var p=0,m=0;m<u.length;m++)a=(o.words[m]|0)-(u.words[m]|0)+p,p=a>>26,this.words[m]=a&67108863;for(;p!==0&&m<o.length;m++)a=(o.words[m]|0)+p,p=a>>26,this.words[m]=a&67108863;if(p===0&&m<o.length&&o!==this)for(;m<o.length;m++)this.words[m]=o.words[m];return this.length=Math.max(this.length,m),o!==this&&(this.negative=1),this.strip()},f.prototype.sub=function(e){return this.clone().isub(e)};function R(v,e,a){a.negative=e.negative^v.negative;var h=v.length+e.length|0;a.length=h,h=h-1|0;var o=v.words[0]|0,u=e.words[0]|0,p=o*u,m=p&67108863,l=p/67108864|0;a.words[0]=m;for(var n=1;n<h;n++){for(var d=l>>>26,M=l&67108863,y=Math.min(n,e.length-1),x=Math.max(0,n-v.length+1);x<=y;x++){var B=n-x|0;o=v.words[B]|0,u=e.words[x]|0,p=o*u+M,d+=p/67108864|0,M=p&67108863}a.words[n]=M|0,l=d|0}return l!==0?a.words[n]=l|0:a.length--,a.strip()}var D=function(e,a,h){var o=e.words,u=a.words,p=h.words,m=0,l,n,d,M=o[0]|0,y=M&8191,x=M>>>13,B=o[1]|0,E=B&8191,k=B>>>13,wt=o[2]|0,T=wt&8191,P=wt>>>13,he=o[3]|0,N=he&8191,L=he>>>13,ve=o[4]|0,F=ve&8191,U=ve>>>13,me=o[5]|0,z=me&8191,K=me>>>13,ge=o[6]|0,$=ge&8191,H=ge>>>13,ye=o[7]|0,G=ye&8191,Z=ye>>>13,be=o[8]|0,W=be&8191,V=be>>>13,we=o[9]|0,j=we&8191,J=we>>>13,Me=u[0]|0,X=Me&8191,Y=Me>>>13,xe=u[1]|0,Q=xe&8191,tt=xe>>>13,_e=u[2]|0,et=_e&8191,rt=_e>>>13,Se=u[3]|0,it=Se&8191,nt=Se>>>13,Ae=u[4]|0,ft=Ae&8191,at=Ae>>>13,Be=u[5]|0,ot=Be&8191,st=Be>>>13,Ee=u[6]|0,ht=Ee&8191,ut=Ee>>>13,ke=u[7]|0,lt=ke&8191,dt=ke>>>13,qe=u[8]|0,ct=qe&8191,pt=qe>>>13,Ie=u[9]|0,vt=Ie&8191,mt=Ie>>>13;h.negative=e.negative^a.negative,h.length=19,l=Math.imul(y,X),n=Math.imul(y,Y),n=n+Math.imul(x,X)|0,d=Math.imul(x,Y);var ue=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ue>>>26)|0,ue&=67108863,l=Math.imul(E,X),n=Math.imul(E,Y),n=n+Math.imul(k,X)|0,d=Math.imul(k,Y),l=l+Math.imul(y,Q)|0,n=n+Math.imul(y,tt)|0,n=n+Math.imul(x,Q)|0,d=d+Math.imul(x,tt)|0;var Et=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,l=Math.imul(T,X),n=Math.imul(T,Y),n=n+Math.imul(P,X)|0,d=Math.imul(P,Y),l=l+Math.imul(E,Q)|0,n=n+Math.imul(E,tt)|0,n=n+Math.imul(k,Q)|0,d=d+Math.imul(k,tt)|0,l=l+Math.imul(y,et)|0,n=n+Math.imul(y,rt)|0,n=n+Math.imul(x,et)|0,d=d+Math.imul(x,rt)|0;var kt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,l=Math.imul(N,X),n=Math.imul(N,Y),n=n+Math.imul(L,X)|0,d=Math.imul(L,Y),l=l+Math.imul(T,Q)|0,n=n+Math.imul(T,tt)|0,n=n+Math.imul(P,Q)|0,d=d+Math.imul(P,tt)|0,l=l+Math.imul(E,et)|0,n=n+Math.imul(E,rt)|0,n=n+Math.imul(k,et)|0,d=d+Math.imul(k,rt)|0,l=l+Math.imul(y,it)|0,n=n+Math.imul(y,nt)|0,n=n+Math.imul(x,it)|0,d=d+Math.imul(x,nt)|0;var Zt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Zt>>>26)|0,Zt&=67108863,l=Math.imul(F,X),n=Math.imul(F,Y),n=n+Math.imul(U,X)|0,d=Math.imul(U,Y),l=l+Math.imul(N,Q)|0,n=n+Math.imul(N,tt)|0,n=n+Math.imul(L,Q)|0,d=d+Math.imul(L,tt)|0,l=l+Math.imul(T,et)|0,n=n+Math.imul(T,rt)|0,n=n+Math.imul(P,et)|0,d=d+Math.imul(P,rt)|0,l=l+Math.imul(E,it)|0,n=n+Math.imul(E,nt)|0,n=n+Math.imul(k,it)|0,d=d+Math.imul(k,nt)|0,l=l+Math.imul(y,ft)|0,n=n+Math.imul(y,at)|0,n=n+Math.imul(x,ft)|0,d=d+Math.imul(x,at)|0;var Wt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Wt>>>26)|0,Wt&=67108863,l=Math.imul(z,X),n=Math.imul(z,Y),n=n+Math.imul(K,X)|0,d=Math.imul(K,Y),l=l+Math.imul(F,Q)|0,n=n+Math.imul(F,tt)|0,n=n+Math.imul(U,Q)|0,d=d+Math.imul(U,tt)|0,l=l+Math.imul(N,et)|0,n=n+Math.imul(N,rt)|0,n=n+Math.imul(L,et)|0,d=d+Math.imul(L,rt)|0,l=l+Math.imul(T,it)|0,n=n+Math.imul(T,nt)|0,n=n+Math.imul(P,it)|0,d=d+Math.imul(P,nt)|0,l=l+Math.imul(E,ft)|0,n=n+Math.imul(E,at)|0,n=n+Math.imul(k,ft)|0,d=d+Math.imul(k,at)|0,l=l+Math.imul(y,ot)|0,n=n+Math.imul(y,st)|0,n=n+Math.imul(x,ot)|0,d=d+Math.imul(x,st)|0;var Vt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Vt>>>26)|0,Vt&=67108863,l=Math.imul($,X),n=Math.imul($,Y),n=n+Math.imul(H,X)|0,d=Math.imul(H,Y),l=l+Math.imul(z,Q)|0,n=n+Math.imul(z,tt)|0,n=n+Math.imul(K,Q)|0,d=d+Math.imul(K,tt)|0,l=l+Math.imul(F,et)|0,n=n+Math.imul(F,rt)|0,n=n+Math.imul(U,et)|0,d=d+Math.imul(U,rt)|0,l=l+Math.imul(N,it)|0,n=n+Math.imul(N,nt)|0,n=n+Math.imul(L,it)|0,d=d+Math.imul(L,nt)|0,l=l+Math.imul(T,ft)|0,n=n+Math.imul(T,at)|0,n=n+Math.imul(P,ft)|0,d=d+Math.imul(P,at)|0,l=l+Math.imul(E,ot)|0,n=n+Math.imul(E,st)|0,n=n+Math.imul(k,ot)|0,d=d+Math.imul(k,st)|0,l=l+Math.imul(y,ht)|0,n=n+Math.imul(y,ut)|0,n=n+Math.imul(x,ht)|0,d=d+Math.imul(x,ut)|0;var jt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(jt>>>26)|0,jt&=67108863,l=Math.imul(G,X),n=Math.imul(G,Y),n=n+Math.imul(Z,X)|0,d=Math.imul(Z,Y),l=l+Math.imul($,Q)|0,n=n+Math.imul($,tt)|0,n=n+Math.imul(H,Q)|0,d=d+Math.imul(H,tt)|0,l=l+Math.imul(z,et)|0,n=n+Math.imul(z,rt)|0,n=n+Math.imul(K,et)|0,d=d+Math.imul(K,rt)|0,l=l+Math.imul(F,it)|0,n=n+Math.imul(F,nt)|0,n=n+Math.imul(U,it)|0,d=d+Math.imul(U,nt)|0,l=l+Math.imul(N,ft)|0,n=n+Math.imul(N,at)|0,n=n+Math.imul(L,ft)|0,d=d+Math.imul(L,at)|0,l=l+Math.imul(T,ot)|0,n=n+Math.imul(T,st)|0,n=n+Math.imul(P,ot)|0,d=d+Math.imul(P,st)|0,l=l+Math.imul(E,ht)|0,n=n+Math.imul(E,ut)|0,n=n+Math.imul(k,ht)|0,d=d+Math.imul(k,ut)|0,l=l+Math.imul(y,lt)|0,n=n+Math.imul(y,dt)|0,n=n+Math.imul(x,lt)|0,d=d+Math.imul(x,dt)|0;var Jt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,l=Math.imul(W,X),n=Math.imul(W,Y),n=n+Math.imul(V,X)|0,d=Math.imul(V,Y),l=l+Math.imul(G,Q)|0,n=n+Math.imul(G,tt)|0,n=n+Math.imul(Z,Q)|0,d=d+Math.imul(Z,tt)|0,l=l+Math.imul($,et)|0,n=n+Math.imul($,rt)|0,n=n+Math.imul(H,et)|0,d=d+Math.imul(H,rt)|0,l=l+Math.imul(z,it)|0,n=n+Math.imul(z,nt)|0,n=n+Math.imul(K,it)|0,d=d+Math.imul(K,nt)|0,l=l+Math.imul(F,ft)|0,n=n+Math.imul(F,at)|0,n=n+Math.imul(U,ft)|0,d=d+Math.imul(U,at)|0,l=l+Math.imul(N,ot)|0,n=n+Math.imul(N,st)|0,n=n+Math.imul(L,ot)|0,d=d+Math.imul(L,st)|0,l=l+Math.imul(T,ht)|0,n=n+Math.imul(T,ut)|0,n=n+Math.imul(P,ht)|0,d=d+Math.imul(P,ut)|0,l=l+Math.imul(E,lt)|0,n=n+Math.imul(E,dt)|0,n=n+Math.imul(k,lt)|0,d=d+Math.imul(k,dt)|0,l=l+Math.imul(y,ct)|0,n=n+Math.imul(y,pt)|0,n=n+Math.imul(x,ct)|0,d=d+Math.imul(x,pt)|0;var Xt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,l=Math.imul(j,X),n=Math.imul(j,Y),n=n+Math.imul(J,X)|0,d=Math.imul(J,Y),l=l+Math.imul(W,Q)|0,n=n+Math.imul(W,tt)|0,n=n+Math.imul(V,Q)|0,d=d+Math.imul(V,tt)|0,l=l+Math.imul(G,et)|0,n=n+Math.imul(G,rt)|0,n=n+Math.imul(Z,et)|0,d=d+Math.imul(Z,rt)|0,l=l+Math.imul($,it)|0,n=n+Math.imul($,nt)|0,n=n+Math.imul(H,it)|0,d=d+Math.imul(H,nt)|0,l=l+Math.imul(z,ft)|0,n=n+Math.imul(z,at)|0,n=n+Math.imul(K,ft)|0,d=d+Math.imul(K,at)|0,l=l+Math.imul(F,ot)|0,n=n+Math.imul(F,st)|0,n=n+Math.imul(U,ot)|0,d=d+Math.imul(U,st)|0,l=l+Math.imul(N,ht)|0,n=n+Math.imul(N,ut)|0,n=n+Math.imul(L,ht)|0,d=d+Math.imul(L,ut)|0,l=l+Math.imul(T,lt)|0,n=n+Math.imul(T,dt)|0,n=n+Math.imul(P,lt)|0,d=d+Math.imul(P,dt)|0,l=l+Math.imul(E,ct)|0,n=n+Math.imul(E,pt)|0,n=n+Math.imul(k,ct)|0,d=d+Math.imul(k,pt)|0,l=l+Math.imul(y,vt)|0,n=n+Math.imul(y,mt)|0,n=n+Math.imul(x,vt)|0,d=d+Math.imul(x,mt)|0;var Yt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,l=Math.imul(j,Q),n=Math.imul(j,tt),n=n+Math.imul(J,Q)|0,d=Math.imul(J,tt),l=l+Math.imul(W,et)|0,n=n+Math.imul(W,rt)|0,n=n+Math.imul(V,et)|0,d=d+Math.imul(V,rt)|0,l=l+Math.imul(G,it)|0,n=n+Math.imul(G,nt)|0,n=n+Math.imul(Z,it)|0,d=d+Math.imul(Z,nt)|0,l=l+Math.imul($,ft)|0,n=n+Math.imul($,at)|0,n=n+Math.imul(H,ft)|0,d=d+Math.imul(H,at)|0,l=l+Math.imul(z,ot)|0,n=n+Math.imul(z,st)|0,n=n+Math.imul(K,ot)|0,d=d+Math.imul(K,st)|0,l=l+Math.imul(F,ht)|0,n=n+Math.imul(F,ut)|0,n=n+Math.imul(U,ht)|0,d=d+Math.imul(U,ut)|0,l=l+Math.imul(N,lt)|0,n=n+Math.imul(N,dt)|0,n=n+Math.imul(L,lt)|0,d=d+Math.imul(L,dt)|0,l=l+Math.imul(T,ct)|0,n=n+Math.imul(T,pt)|0,n=n+Math.imul(P,ct)|0,d=d+Math.imul(P,pt)|0,l=l+Math.imul(E,vt)|0,n=n+Math.imul(E,mt)|0,n=n+Math.imul(k,vt)|0,d=d+Math.imul(k,mt)|0;var Qt=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,l=Math.imul(j,et),n=Math.imul(j,rt),n=n+Math.imul(J,et)|0,d=Math.imul(J,rt),l=l+Math.imul(W,it)|0,n=n+Math.imul(W,nt)|0,n=n+Math.imul(V,it)|0,d=d+Math.imul(V,nt)|0,l=l+Math.imul(G,ft)|0,n=n+Math.imul(G,at)|0,n=n+Math.imul(Z,ft)|0,d=d+Math.imul(Z,at)|0,l=l+Math.imul($,ot)|0,n=n+Math.imul($,st)|0,n=n+Math.imul(H,ot)|0,d=d+Math.imul(H,st)|0,l=l+Math.imul(z,ht)|0,n=n+Math.imul(z,ut)|0,n=n+Math.imul(K,ht)|0,d=d+Math.imul(K,ut)|0,l=l+Math.imul(F,lt)|0,n=n+Math.imul(F,dt)|0,n=n+Math.imul(U,lt)|0,d=d+Math.imul(U,dt)|0,l=l+Math.imul(N,ct)|0,n=n+Math.imul(N,pt)|0,n=n+Math.imul(L,ct)|0,d=d+Math.imul(L,pt)|0,l=l+Math.imul(T,vt)|0,n=n+Math.imul(T,mt)|0,n=n+Math.imul(P,vt)|0,d=d+Math.imul(P,mt)|0;var te=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(te>>>26)|0,te&=67108863,l=Math.imul(j,it),n=Math.imul(j,nt),n=n+Math.imul(J,it)|0,d=Math.imul(J,nt),l=l+Math.imul(W,ft)|0,n=n+Math.imul(W,at)|0,n=n+Math.imul(V,ft)|0,d=d+Math.imul(V,at)|0,l=l+Math.imul(G,ot)|0,n=n+Math.imul(G,st)|0,n=n+Math.imul(Z,ot)|0,d=d+Math.imul(Z,st)|0,l=l+Math.imul($,ht)|0,n=n+Math.imul($,ut)|0,n=n+Math.imul(H,ht)|0,d=d+Math.imul(H,ut)|0,l=l+Math.imul(z,lt)|0,n=n+Math.imul(z,dt)|0,n=n+Math.imul(K,lt)|0,d=d+Math.imul(K,dt)|0,l=l+Math.imul(F,ct)|0,n=n+Math.imul(F,pt)|0,n=n+Math.imul(U,ct)|0,d=d+Math.imul(U,pt)|0,l=l+Math.imul(N,vt)|0,n=n+Math.imul(N,mt)|0,n=n+Math.imul(L,vt)|0,d=d+Math.imul(L,mt)|0;var ee=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ee>>>26)|0,ee&=67108863,l=Math.imul(j,ft),n=Math.imul(j,at),n=n+Math.imul(J,ft)|0,d=Math.imul(J,at),l=l+Math.imul(W,ot)|0,n=n+Math.imul(W,st)|0,n=n+Math.imul(V,ot)|0,d=d+Math.imul(V,st)|0,l=l+Math.imul(G,ht)|0,n=n+Math.imul(G,ut)|0,n=n+Math.imul(Z,ht)|0,d=d+Math.imul(Z,ut)|0,l=l+Math.imul($,lt)|0,n=n+Math.imul($,dt)|0,n=n+Math.imul(H,lt)|0,d=d+Math.imul(H,dt)|0,l=l+Math.imul(z,ct)|0,n=n+Math.imul(z,pt)|0,n=n+Math.imul(K,ct)|0,d=d+Math.imul(K,pt)|0,l=l+Math.imul(F,vt)|0,n=n+Math.imul(F,mt)|0,n=n+Math.imul(U,vt)|0,d=d+Math.imul(U,mt)|0;var re=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(re>>>26)|0,re&=67108863,l=Math.imul(j,ot),n=Math.imul(j,st),n=n+Math.imul(J,ot)|0,d=Math.imul(J,st),l=l+Math.imul(W,ht)|0,n=n+Math.imul(W,ut)|0,n=n+Math.imul(V,ht)|0,d=d+Math.imul(V,ut)|0,l=l+Math.imul(G,lt)|0,n=n+Math.imul(G,dt)|0,n=n+Math.imul(Z,lt)|0,d=d+Math.imul(Z,dt)|0,l=l+Math.imul($,ct)|0,n=n+Math.imul($,pt)|0,n=n+Math.imul(H,ct)|0,d=d+Math.imul(H,pt)|0,l=l+Math.imul(z,vt)|0,n=n+Math.imul(z,mt)|0,n=n+Math.imul(K,vt)|0,d=d+Math.imul(K,mt)|0;var ie=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ie>>>26)|0,ie&=67108863,l=Math.imul(j,ht),n=Math.imul(j,ut),n=n+Math.imul(J,ht)|0,d=Math.imul(J,ut),l=l+Math.imul(W,lt)|0,n=n+Math.imul(W,dt)|0,n=n+Math.imul(V,lt)|0,d=d+Math.imul(V,dt)|0,l=l+Math.imul(G,ct)|0,n=n+Math.imul(G,pt)|0,n=n+Math.imul(Z,ct)|0,d=d+Math.imul(Z,pt)|0,l=l+Math.imul($,vt)|0,n=n+Math.imul($,mt)|0,n=n+Math.imul(H,vt)|0,d=d+Math.imul(H,mt)|0;var ne=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ne>>>26)|0,ne&=67108863,l=Math.imul(j,lt),n=Math.imul(j,dt),n=n+Math.imul(J,lt)|0,d=Math.imul(J,dt),l=l+Math.imul(W,ct)|0,n=n+Math.imul(W,pt)|0,n=n+Math.imul(V,ct)|0,d=d+Math.imul(V,pt)|0,l=l+Math.imul(G,vt)|0,n=n+Math.imul(G,mt)|0,n=n+Math.imul(Z,vt)|0,d=d+Math.imul(Z,mt)|0;var fe=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(fe>>>26)|0,fe&=67108863,l=Math.imul(j,ct),n=Math.imul(j,pt),n=n+Math.imul(J,ct)|0,d=Math.imul(J,pt),l=l+Math.imul(W,vt)|0,n=n+Math.imul(W,mt)|0,n=n+Math.imul(V,vt)|0,d=d+Math.imul(V,mt)|0;var ae=(m+l|0)+((n&8191)<<13)|0;m=(d+(n>>>13)|0)+(ae>>>26)|0,ae&=67108863,l=Math.imul(j,vt),n=Math.imul(j,mt),n=n+Math.imul(J,vt)|0,d=Math.imul(J,mt);var oe=(m+l|0)+((n&8191)<<13)|0;return m=(d+(n>>>13)|0)+(oe>>>26)|0,oe&=67108863,p[0]=ue,p[1]=Et,p[2]=kt,p[3]=Zt,p[4]=Wt,p[5]=Vt,p[6]=jt,p[7]=Jt,p[8]=Xt,p[9]=Yt,p[10]=Qt,p[11]=te,p[12]=ee,p[13]=re,p[14]=ie,p[15]=ne,p[16]=fe,p[17]=ae,p[18]=oe,m!==0&&(p[19]=m,h.length++),h};Math.imul||(D=R);function bt(v,e,a){a.negative=e.negative^v.negative,a.length=v.length+e.length;for(var h=0,o=0,u=0;u<a.length-1;u++){var p=o;o=0;for(var m=h&67108863,l=Math.min(u,e.length-1),n=Math.max(0,u-v.length+1);n<=l;n++){var d=u-n,M=v.words[d]|0,y=e.words[n]|0,x=M*y,B=x&67108863;p=p+(x/67108864|0)|0,B=B+m|0,m=B&67108863,p=p+(B>>>26)|0,o+=p>>>26,p&=67108863}a.words[u]=m,h=p,p=o}return h!==0?a.words[u]=h:a.length--,a.strip()}function _t(v,e,a){var h=new gt;return h.mulp(v,e,a)}f.prototype.mulTo=function(e,a){var h,o=this.length+e.length;return this.length===10&&e.length===10?h=D(this,e,a):o<63?h=R(this,e,a):o<1024?h=bt(this,e,a):h=_t(this,e,a),h};function gt(v,e){this.x=v,this.y=e}gt.prototype.makeRBT=function(e){for(var a=new Array(e),h=f.prototype._countBits(e)-1,o=0;o<e;o++)a[o]=this.revBin(o,h,e);return a},gt.prototype.revBin=function(e,a,h){if(e===0||e===h-1)return e;for(var o=0,u=0;u<a;u++)o|=(e&1)<<a-u-1,e>>=1;return o},gt.prototype.permute=function(e,a,h,o,u,p){for(var m=0;m<p;m++)o[m]=a[e[m]],u[m]=h[e[m]]},gt.prototype.transform=function(e,a,h,o,u,p){this.permute(p,e,a,h,o,u);for(var m=1;m<u;m<<=1)for(var l=m<<1,n=Math.cos(2*Math.PI/l),d=Math.sin(2*Math.PI/l),M=0;M<u;M+=l)for(var y=n,x=d,B=0;B<m;B++){var E=h[M+B],k=o[M+B],wt=h[M+B+m],T=o[M+B+m],P=y*wt-x*T;T=y*T+x*wt,wt=P,h[M+B]=E+wt,o[M+B]=k+T,h[M+B+m]=E-wt,o[M+B+m]=k-T,B!==l&&(P=n*y-d*x,x=n*x+d*y,y=P)}},gt.prototype.guessLen13b=function(e,a){var h=Math.max(a,e)|1,o=h&1,u=0;for(h=h/2|0;h;h=h>>>1)u++;return 1<<u+1+o},gt.prototype.conjugate=function(e,a,h){if(!(h<=1))for(var o=0;o<h/2;o++){var u=e[o];e[o]=e[h-o-1],e[h-o-1]=u,u=a[o],a[o]=-a[h-o-1],a[h-o-1]=-u}},gt.prototype.normalize13b=function(e,a){for(var h=0,o=0;o<a/2;o++){var u=Math.round(e[2*o+1]/a)*8192+Math.round(e[2*o]/a)+h;e[o]=u&67108863,u<67108864?h=0:h=u/67108864|0}return e},gt.prototype.convert13b=function(e,a,h,o){for(var u=0,p=0;p<a;p++)u=u+(e[p]|0),h[2*p]=u&8191,u=u>>>13,h[2*p+1]=u&8191,u=u>>>13;for(p=2*a;p<o;++p)h[p]=0;r(u===0),r((u&-8192)===0)},gt.prototype.stub=function(e){for(var a=new Array(e),h=0;h<e;h++)a[h]=0;return a},gt.prototype.mulp=function(e,a,h){var o=2*this.guessLen13b(e.length,a.length),u=this.makeRBT(o),p=this.stub(o),m=new Array(o),l=new Array(o),n=new Array(o),d=new Array(o),M=new Array(o),y=new Array(o),x=h.words;x.length=o,this.convert13b(e.words,e.length,m,o),this.convert13b(a.words,a.length,d,o),this.transform(m,p,l,n,o,u),this.transform(d,p,M,y,o,u);for(var B=0;B<o;B++){var E=l[B]*M[B]-n[B]*y[B];n[B]=l[B]*y[B]+n[B]*M[B],l[B]=E}return this.conjugate(l,n,o),this.transform(l,n,x,p,o,u),this.conjugate(x,p,o),this.normalize13b(x,o),h.negative=e.negative^a.negative,h.length=e.length+a.length,h.strip()},f.prototype.mul=function(e){var a=new f(null);return a.words=new Array(this.length+e.length),this.mulTo(e,a)},f.prototype.mulf=function(e){var a=new f(null);return a.words=new Array(this.length+e.length),_t(this,e,a)},f.prototype.imul=function(e){return this.clone().mulTo(e,this)},f.prototype.imuln=function(e){r(typeof e=="number"),r(e<67108864);for(var a=0,h=0;h<this.length;h++){var o=(this.words[h]|0)*e,u=(o&67108863)+(a&67108863);a>>=26,a+=o/67108864|0,a+=u>>>26,this.words[h]=u&67108863}return a!==0&&(this.words[h]=a,this.length++),this.length=e===0?1:this.length,this},f.prototype.muln=function(e){return this.clone().imuln(e)},f.prototype.sqr=function(){return this.mul(this)},f.prototype.isqr=function(){return this.imul(this.clone())},f.prototype.pow=function(e){var a=q(e);if(a.length===0)return new f(1);for(var h=this,o=0;o<a.length&&a[o]===0;o++,h=h.sqr());if(++o<a.length)for(var u=h.sqr();o<a.length;o++,u=u.sqr())a[o]!==0&&(h=h.mul(u));return h},f.prototype.iushln=function(e){r(typeof e=="number"&&e>=0);var a=e%26,h=(e-a)/26,o=67108863>>>26-a<<26-a,u;if(a!==0){var p=0;for(u=0;u<this.length;u++){var m=this.words[u]&o,l=(this.words[u]|0)-m<<a;this.words[u]=l|p,p=m>>>26-a}p&&(this.words[u]=p,this.length++)}if(h!==0){for(u=this.length-1;u>=0;u--)this.words[u+h]=this.words[u];for(u=0;u<h;u++)this.words[u]=0;this.length+=h}return this.strip()},f.prototype.ishln=function(e){return r(this.negative===0),this.iushln(e)},f.prototype.iushrn=function(e,a,h){r(typeof e=="number"&&e>=0);var o;a?o=(a-a%26)/26:o=0;var u=e%26,p=Math.min((e-u)/26,this.length),m=67108863^67108863>>>u<<u,l=h;if(o-=p,o=Math.max(0,o),l){for(var n=0;n<p;n++)l.words[n]=this.words[n];l.length=p}if(p!==0)if(this.length>p)for(this.length-=p,n=0;n<this.length;n++)this.words[n]=this.words[n+p];else this.words[0]=0,this.length=1;var d=0;for(n=this.length-1;n>=0&&(d!==0||n>=o);n--){var M=this.words[n]|0;this.words[n]=d<<26-u|M>>>u,d=M&m}return l&&d!==0&&(l.words[l.length++]=d),this.length===0&&(this.words[0]=0,this.length=1),this.strip()},f.prototype.ishrn=function(e,a,h){return r(this.negative===0),this.iushrn(e,a,h)},f.prototype.shln=function(e){return this.clone().ishln(e)},f.prototype.ushln=function(e){return this.clone().iushln(e)},f.prototype.shrn=function(e){return this.clone().ishrn(e)},f.prototype.ushrn=function(e){return this.clone().iushrn(e)},f.prototype.testn=function(e){r(typeof e=="number"&&e>=0);var a=e%26,h=(e-a)/26,o=1<<a;if(this.length<=h)return!1;var u=this.words[h];return!!(u&o)},f.prototype.imaskn=function(e){r(typeof e=="number"&&e>=0);var a=e%26,h=(e-a)/26;if(r(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(a!==0&&h++,this.length=Math.min(h,this.length),a!==0){var o=67108863^67108863>>>a<<a;this.words[this.length-1]&=o}return this.strip()},f.prototype.maskn=function(e){return this.clone().imaskn(e)},f.prototype.iaddn=function(e){return r(typeof e=="number"),r(e<67108864),e<0?this.isubn(-e):this.negative!==0?this.length===1&&(this.words[0]|0)<e?(this.words[0]=e-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(e),this.negative=1,this):this._iaddn(e)},f.prototype._iaddn=function(e){this.words[0]+=e;for(var a=0;a<this.length&&this.words[a]>=67108864;a++)this.words[a]-=67108864,a===this.length-1?this.words[a+1]=1:this.words[a+1]++;return this.length=Math.max(this.length,a+1),this},f.prototype.isubn=function(e){if(r(typeof e=="number"),r(e<67108864),e<0)return this.iaddn(-e);if(this.negative!==0)return this.negative=0,this.iaddn(e),this.negative=1,this;if(this.words[0]-=e,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var a=0;a<this.length&&this.words[a]<0;a++)this.words[a]+=67108864,this.words[a+1]-=1;return this.strip()},f.prototype.addn=function(e){return this.clone().iaddn(e)},f.prototype.subn=function(e){return this.clone().isubn(e)},f.prototype.iabs=function(){return this.negative=0,this},f.prototype.abs=function(){return this.clone().iabs()},f.prototype._ishlnsubmul=function(e,a,h){var o=e.length+h,u;this._expand(o);var p,m=0;for(u=0;u<e.length;u++){p=(this.words[u+h]|0)+m;var l=(e.words[u]|0)*a;p-=l&67108863,m=(p>>26)-(l/67108864|0),this.words[u+h]=p&67108863}for(;u<this.length-h;u++)p=(this.words[u+h]|0)+m,m=p>>26,this.words[u+h]=p&67108863;if(m===0)return this.strip();for(r(m===-1),m=0,u=0;u<this.length;u++)p=-(this.words[u]|0)+m,m=p>>26,this.words[u]=p&67108863;return this.negative=1,this.strip()},f.prototype._wordDiv=function(e,a){var h=this.length-e.length,o=this.clone(),u=e,p=u.words[u.length-1]|0,m=this._countBits(p);h=26-m,h!==0&&(u=u.ushln(h),o.iushln(h),p=u.words[u.length-1]|0);var l=o.length-u.length,n;if(a!=="mod"){n=new f(null),n.length=l+1,n.words=new Array(n.length);for(var d=0;d<n.length;d++)n.words[d]=0}var M=o.clone()._ishlnsubmul(u,1,l);M.negative===0&&(o=M,n&&(n.words[l]=1));for(var y=l-1;y>=0;y--){var x=(o.words[u.length+y]|0)*67108864+(o.words[u.length+y-1]|0);for(x=Math.min(x/p|0,67108863),o._ishlnsubmul(u,x,y);o.negative!==0;)x--,o.negative=0,o._ishlnsubmul(u,1,y),o.isZero()||(o.negative^=1);n&&(n.words[y]=x)}return n&&n.strip(),o.strip(),a!=="div"&&h!==0&&o.iushrn(h),{div:n||null,mod:o}},f.prototype.divmod=function(e,a,h){if(r(!e.isZero()),this.isZero())return{div:new f(0),mod:new f(0)};var o,u,p;return this.negative!==0&&e.negative===0?(p=this.neg().divmod(e,a),a!=="mod"&&(o=p.div.neg()),a!=="div"&&(u=p.mod.neg(),h&&u.negative!==0&&u.iadd(e)),{div:o,mod:u}):this.negative===0&&e.negative!==0?(p=this.divmod(e.neg(),a),a!=="mod"&&(o=p.div.neg()),{div:o,mod:p.mod}):(this.negative&e.negative)!==0?(p=this.neg().divmod(e.neg(),a),a!=="div"&&(u=p.mod.neg(),h&&u.negative!==0&&u.isub(e)),{div:p.div,mod:u}):e.length>this.length||this.cmp(e)<0?{div:new f(0),mod:this}:e.length===1?a==="div"?{div:this.divn(e.words[0]),mod:null}:a==="mod"?{div:null,mod:new f(this.modn(e.words[0]))}:{div:this.divn(e.words[0]),mod:new f(this.modn(e.words[0]))}:this._wordDiv(e,a)},f.prototype.div=function(e){return this.divmod(e,"div",!1).div},f.prototype.mod=function(e){return this.divmod(e,"mod",!1).mod},f.prototype.umod=function(e){return this.divmod(e,"mod",!0).mod},f.prototype.divRound=function(e){var a=this.divmod(e);if(a.mod.isZero())return a.div;var h=a.div.negative!==0?a.mod.isub(e):a.mod,o=e.ushrn(1),u=e.andln(1),p=h.cmp(o);return p<0||u===1&&p===0?a.div:a.div.negative!==0?a.div.isubn(1):a.div.iaddn(1)},f.prototype.modn=function(e){r(e<=67108863);for(var a=(1<<26)%e,h=0,o=this.length-1;o>=0;o--)h=(a*h+(this.words[o]|0))%e;return h},f.prototype.idivn=function(e){r(e<=67108863);for(var a=0,h=this.length-1;h>=0;h--){var o=(this.words[h]|0)+a*67108864;this.words[h]=o/e|0,a=o%e}return this.strip()},f.prototype.divn=function(e){return this.clone().idivn(e)},f.prototype.egcd=function(e){r(e.negative===0),r(!e.isZero());var a=this,h=e.clone();a.negative!==0?a=a.umod(e):a=a.clone();for(var o=new f(1),u=new f(0),p=new f(0),m=new f(1),l=0;a.isEven()&&h.isEven();)a.iushrn(1),h.iushrn(1),++l;for(var n=h.clone(),d=a.clone();!a.isZero();){for(var M=0,y=1;(a.words[0]&y)===0&&M<26;++M,y<<=1);if(M>0)for(a.iushrn(M);M-- >0;)(o.isOdd()||u.isOdd())&&(o.iadd(n),u.isub(d)),o.iushrn(1),u.iushrn(1);for(var x=0,B=1;(h.words[0]&B)===0&&x<26;++x,B<<=1);if(x>0)for(h.iushrn(x);x-- >0;)(p.isOdd()||m.isOdd())&&(p.iadd(n),m.isub(d)),p.iushrn(1),m.iushrn(1);a.cmp(h)>=0?(a.isub(h),o.isub(p),u.isub(m)):(h.isub(a),p.isub(o),m.isub(u))}return{a:p,b:m,gcd:h.iushln(l)}},f.prototype._invmp=function(e){r(e.negative===0),r(!e.isZero());var a=this,h=e.clone();a.negative!==0?a=a.umod(e):a=a.clone();for(var o=new f(1),u=new f(0),p=h.clone();a.cmpn(1)>0&&h.cmpn(1)>0;){for(var m=0,l=1;(a.words[0]&l)===0&&m<26;++m,l<<=1);if(m>0)for(a.iushrn(m);m-- >0;)o.isOdd()&&o.iadd(p),o.iushrn(1);for(var n=0,d=1;(h.words[0]&d)===0&&n<26;++n,d<<=1);if(n>0)for(h.iushrn(n);n-- >0;)u.isOdd()&&u.iadd(p),u.iushrn(1);a.cmp(h)>=0?(a.isub(h),o.isub(u)):(h.isub(a),u.isub(o))}var M;return a.cmpn(1)===0?M=o:M=u,M.cmpn(0)<0&&M.iadd(e),M},f.prototype.gcd=function(e){if(this.isZero())return e.abs();if(e.isZero())return this.abs();var a=this.clone(),h=e.clone();a.negative=0,h.negative=0;for(var o=0;a.isEven()&&h.isEven();o++)a.iushrn(1),h.iushrn(1);do{for(;a.isEven();)a.iushrn(1);for(;h.isEven();)h.iushrn(1);var u=a.cmp(h);if(u<0){var p=a;a=h,h=p}else if(u===0||h.cmpn(1)===0)break;a.isub(h)}while(!0);return h.iushln(o)},f.prototype.invm=function(e){return this.egcd(e).a.umod(e)},f.prototype.isEven=function(){return(this.words[0]&1)===0},f.prototype.isOdd=function(){return(this.words[0]&1)===1},f.prototype.andln=function(e){return this.words[0]&e},f.prototype.bincn=function(e){r(typeof e=="number");var a=e%26,h=(e-a)/26,o=1<<a;if(this.length<=h)return this._expand(h+1),this.words[h]|=o,this;for(var u=o,p=h;u!==0&&p<this.length;p++){var m=this.words[p]|0;m+=u,u=m>>>26,m&=67108863,this.words[p]=m}return u!==0&&(this.words[p]=u,this.length++),this},f.prototype.isZero=function(){return this.length===1&&this.words[0]===0},f.prototype.cmpn=function(e){var a=e<0;if(this.negative!==0&&!a)return-1;if(this.negative===0&&a)return 1;this.strip();var h;if(this.length>1)h=1;else{a&&(e=-e),r(e<=67108863,"Number is too big");var o=this.words[0]|0;h=o===e?0:o<e?-1:1}return this.negative!==0?-h|0:h},f.prototype.cmp=function(e){if(this.negative!==0&&e.negative===0)return-1;if(this.negative===0&&e.negative!==0)return 1;var a=this.ucmp(e);return this.negative!==0?-a|0:a},f.prototype.ucmp=function(e){if(this.length>e.length)return 1;if(this.length<e.length)return-1;for(var a=0,h=this.length-1;h>=0;h--){var o=this.words[h]|0,u=e.words[h]|0;if(o!==u){o<u?a=-1:o>u&&(a=1);break}}return a},f.prototype.gtn=function(e){return this.cmpn(e)===1},f.prototype.gt=function(e){return this.cmp(e)===1},f.prototype.gten=function(e){return this.cmpn(e)>=0},f.prototype.gte=function(e){return this.cmp(e)>=0},f.prototype.ltn=function(e){return this.cmpn(e)===-1},f.prototype.lt=function(e){return this.cmp(e)===-1},f.prototype.lten=function(e){return this.cmpn(e)<=0},f.prototype.lte=function(e){return this.cmp(e)<=0},f.prototype.eqn=function(e){return this.cmpn(e)===0},f.prototype.eq=function(e){return this.cmp(e)===0},f.red=function(e){return new O(e)},f.prototype.toRed=function(e){return r(!this.red,"Already a number in reduction context"),r(this.negative===0,"red works only with positives"),e.convertTo(this)._forceRed(e)},f.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},f.prototype._forceRed=function(e){return this.red=e,this},f.prototype.forceRed=function(e){return r(!this.red,"Already a number in reduction context"),this._forceRed(e)},f.prototype.redAdd=function(e){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},f.prototype.redIAdd=function(e){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},f.prototype.redSub=function(e){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},f.prototype.redISub=function(e){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},f.prototype.redShl=function(e){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},f.prototype.redMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},f.prototype.redIMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},f.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},f.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},f.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},f.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},f.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},f.prototype.redPow=function(e){return r(this.red&&!e.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,e)};var xt={k256:null,p224:null,p192:null,p25519:null};function Mt(v,e){this.name=v,this.p=new f(e,16),this.n=this.p.bitLength(),this.k=new f(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}Mt.prototype._tmp=function(){var e=new f(null);return e.words=new Array(Math.ceil(this.n/13)),e},Mt.prototype.ireduce=function(e){var a=e,h;do this.split(a,this.tmp),a=this.imulK(a),a=a.iadd(this.tmp),h=a.bitLength();while(h>this.n);var o=h<this.n?-1:a.ucmp(this.p);return o===0?(a.words[0]=0,a.length=1):o>0?a.isub(this.p):a.strip!==void 0?a.strip():a._strip(),a},Mt.prototype.split=function(e,a){e.iushrn(this.n,0,a)},Mt.prototype.imulK=function(e){return e.imul(this.k)};function At(){Mt.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}s(At,Mt),At.prototype.split=function(e,a){for(var h=4194303,o=Math.min(e.length,9),u=0;u<o;u++)a.words[u]=e.words[u];if(a.length=o,e.length<=9){e.words[0]=0,e.length=1;return}var p=e.words[9];for(a.words[a.length++]=p&h,u=10;u<e.length;u++){var m=e.words[u]|0;e.words[u-10]=(m&h)<<4|p>>>22,p=m}p>>>=22,e.words[u-10]=p,p===0&&e.length>10?e.length-=10:e.length-=9},At.prototype.imulK=function(e){e.words[e.length]=0,e.words[e.length+1]=0,e.length+=2;for(var a=0,h=0;h<e.length;h++){var o=e.words[h]|0;a+=o*977,e.words[h]=a&67108863,a=o*64+(a/67108864|0)}return e.words[e.length-1]===0&&(e.length--,e.words[e.length-1]===0&&e.length--),e};function Tt(){Mt.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}s(Tt,Mt);function Bt(){Mt.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}s(Bt,Mt);function Pt(){Mt.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}s(Pt,Mt),Pt.prototype.imulK=function(e){for(var a=0,h=0;h<e.length;h++){var o=(e.words[h]|0)*19+a,u=o&67108863;o>>>=26,e.words[h]=u,a=o}return a!==0&&(e.words[e.length++]=a),e},f._prime=function(e){if(xt[e])return xt[e];var a;if(e==="k256")a=new At;else if(e==="p224")a=new Tt;else if(e==="p192")a=new Bt;else if(e==="p25519")a=new Pt;else throw new Error("Unknown prime "+e);return xt[e]=a,a};function O(v){if(typeof v=="string"){var e=f._prime(v);this.m=e.p,this.prime=e}else r(v.gtn(1),"modulus must be greater than 1"),this.m=v,this.prime=null}O.prototype._verify1=function(e){r(e.negative===0,"red works only with positives"),r(e.red,"red works only with red numbers")},O.prototype._verify2=function(e,a){r((e.negative|a.negative)===0,"red works only with positives"),r(e.red&&e.red===a.red,"red works only with red numbers")},O.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):e.umod(this.m)._forceRed(this)},O.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},O.prototype.add=function(e,a){this._verify2(e,a);var h=e.add(a);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},O.prototype.iadd=function(e,a){this._verify2(e,a);var h=e.iadd(a);return h.cmp(this.m)>=0&&h.isub(this.m),h},O.prototype.sub=function(e,a){this._verify2(e,a);var h=e.sub(a);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},O.prototype.isub=function(e,a){this._verify2(e,a);var h=e.isub(a);return h.cmpn(0)<0&&h.iadd(this.m),h},O.prototype.shl=function(e,a){return this._verify1(e),this.imod(e.ushln(a))},O.prototype.imul=function(e,a){return this._verify2(e,a),this.imod(e.imul(a))},O.prototype.mul=function(e,a){return this._verify2(e,a),this.imod(e.mul(a))},O.prototype.isqr=function(e){return this.imul(e,e.clone())},O.prototype.sqr=function(e){return this.mul(e,e)},O.prototype.sqrt=function(e){if(e.isZero())return e.clone();var a=this.m.andln(3);if(r(a%2===1),a===3){var h=this.m.add(new f(1)).iushrn(2);return this.pow(e,h)}for(var o=this.m.subn(1),u=0;!o.isZero()&&o.andln(1)===0;)u++,o.iushrn(1);r(!o.isZero());var p=new f(1).toRed(this),m=p.redNeg(),l=this.m.subn(1).iushrn(1),n=this.m.bitLength();for(n=new f(2*n*n).toRed(this);this.pow(n,l).cmp(m)!==0;)n.redIAdd(m);for(var d=this.pow(n,o),M=this.pow(e,o.addn(1).iushrn(1)),y=this.pow(e,o),x=u;y.cmp(p)!==0;){for(var B=y,E=0;B.cmp(p)!==0;E++)B=B.redSqr();r(E<x);var k=this.pow(d,new f(1).iushln(x-E-1));M=M.redMul(k),d=k.redSqr(),y=y.redMul(d),x=E}return M},O.prototype.invm=function(e){var a=e._invmp(this.m);return a.negative!==0?(a.negative=0,this.imod(a).redNeg()):this.imod(a)},O.prototype.pow=function(e,a){if(a.isZero())return new f(1).toRed(this);if(a.cmpn(1)===0)return e.clone();var h=4,o=new Array(1<<h);o[0]=new f(1).toRed(this),o[1]=e;for(var u=2;u<o.length;u++)o[u]=this.mul(o[u-1],e);var p=o[0],m=0,l=0,n=a.bitLength()%26;for(n===0&&(n=26),u=a.length-1;u>=0;u--){for(var d=a.words[u],M=n-1;M>=0;M--){var y=d>>M&1;if(p!==o[0]&&(p=this.sqr(p)),y===0&&m===0){l=0;continue}m<<=1,m|=y,l++,!(l!==h&&(u!==0||M!==0))&&(p=this.mul(p,o[m]),l=0,m=0)}n=26}return p},O.prototype.convertTo=function(e){var a=e.umod(this.m);return a===e?a.clone():a},O.prototype.convertFrom=function(e){var a=e.clone();return a.red=null,a},f.mont=function(e){return new St(e)};function St(v){O.call(this,v),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new f(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)}s(St,O),St.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},St.prototype.convertFrom=function(e){var a=this.imod(e.mul(this.rinv));return a.red=null,a},St.prototype.imul=function(e,a){if(e.isZero()||a.isZero())return e.words[0]=0,e.length=1,e;var h=e.imul(a),o=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),u=h.isub(o).iushrn(this.shift),p=u;return u.cmp(this.m)>=0?p=u.isub(this.m):u.cmpn(0)<0&&(p=u.iadd(this.m)),p._forceRed(this)},St.prototype.mul=function(e,a){if(e.isZero()||a.isZero())return new f(0)._forceRed(this);var h=e.mul(a),o=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),u=h.isub(o).iushrn(this.shift),p=u;return u.cmp(this.m)>=0?p=u.isub(this.m):u.cmpn(0)<0&&(p=u.iadd(this.m)),p._forceRed(this)},St.prototype.invm=function(e){var a=this.imod(e._invmp(this.m).mul(this.r2));return a._forceRed(this)}})(typeof eu>"u"||eu,gg)});var ru=I((OA,bg)=>{"use strict";var yg=W0(),b7=It().Buffer;function w7(i,t){return b7.from(i.toRed(yg.mont(t.modulus)).redPow(new yg(t.publicExponent)).fromRed().toArray())}bg.exports=w7});var _g=I((FA,xg)=>{"use strict";var M7=Jf(),iu=Ui(),x7=bn(),wg=Qh(),Mg=tu(),nu=W0(),_7=ru(),S7=w0(),Lr=It().Buffer;xg.exports=function(t,r,s){var f;t.padding?f=t.padding:s?f=1:f=4;var c=M7(t),g;if(f===4)g=A7(c,r);else if(f===1)g=B7(c,r,s);else if(f===3){if(g=new nu(r),g.cmp(c.modulus)>=0)throw new Error("data too long for modulus")}else throw new Error("unknown padding");return s?S7(g,c):_7(g,c)};function A7(i,t){var r=i.modulus.byteLength(),s=t.length,f=x7("sha1").update(Lr.alloc(0)).digest(),c=f.length,g=2*c;if(s>r-g-2)throw new Error("message too long");var b=Lr.alloc(r-s-g-2),w=r-c-1,_=iu(c),S=Mg(Lr.concat([f,b,Lr.alloc(1,1),t],w),wg(_,w)),A=Mg(_,wg(S,c));return new nu(Lr.concat([Lr.alloc(1),A,S],r))}function B7(i,t,r){var s=t.length,f=i.modulus.byteLength();if(s>f-11)throw new Error("message too long");var c;return r?c=Lr.alloc(f-s-3,255):c=E7(f-s-3),new nu(Lr.concat([Lr.from([0,r?1:2]),c,Lr.alloc(1),t],f))}function E7(i){for(var t=Lr.allocUnsafe(i),r=0,s=iu(i*2),f=0,c;r<i;)f===s.length&&(s=iu(i*2),f=0),c=s[f++],c&&(t[r++]=c);return t}});var kg=I((UA,Eg)=>{"use strict";var k7=Jf(),Sg=Qh(),Ag=tu(),Bg=W0(),q7=w0(),I7=bn(),R7=ru(),ta=It().Buffer;Eg.exports=function(t,r,s){var f;t.padding?f=t.padding:s?f=1:f=4;var c=k7(t),g=c.modulus.byteLength();if(r.length>g||new Bg(r).cmp(c.modulus)>=0)throw new Error("decryption error");var b;s?b=R7(new Bg(r),c):b=q7(r,c);var w=ta.alloc(g-b.length);if(b=ta.concat([w,b],g),f===4)return T7(c,b);if(f===1)return P7(c,b,s);if(f===3)return b;throw new Error("unknown padding")};function T7(i,t){var r=i.modulus.byteLength(),s=I7("sha1").update(ta.alloc(0)).digest(),f=s.length;if(t[0]!==0)throw new Error("decryption error");var c=t.slice(1,f+1),g=t.slice(f+1),b=Ag(c,Sg(g,f)),w=Ag(g,Sg(b,r-f-1));if(C7(s,w.slice(0,f)))throw new Error("decryption error");for(var _=f;w[_]===0;)_++;if(w[_++]!==1)throw new Error("decryption error");return w.slice(_)}function P7(i,t,r){for(var s=t.slice(0,2),f=2,c=0;t[f++]!==0;)if(f>=t.length){c++;break}var g=t.slice(2,f-1);if((s.toString("hex")!=="0002"&&!r||s.toString("hex")!=="0001"&&r)&&c++,g.length<8&&c++,c)throw new Error("decryption error");return t.slice(f)}function C7(i,t){i=ta.from(i),t=ta.from(t);var r=0,s=i.length;i.length!==t.length&&(r++,s=Math.min(i.length,t.length));for(var f=-1;++f<s;)r+=i[f]^t[f];return r}});var qg=I(dn=>{"use strict";dn.publicEncrypt=_g();dn.privateDecrypt=kg();dn.privateEncrypt=function(t,r){return dn.publicEncrypt(t,r,!0)};dn.publicDecrypt=function(t,r){return dn.privateDecrypt(t,r,!0)}});var Fg=I(ea=>{"use strict";function Ig(){throw new Error(`secure random number generation not supported by this browser
6
6
  use chrome, FireFox or Internet Explorer 11`)}var Tg=It(),Rg=Ui(),Pg=Tg.Buffer,Cg=Tg.kMaxLength,fu=global.crypto||global.msCrypto,Ng=Math.pow(2,32)-1;function Dg(i,t){if(typeof i!="number"||i!==i)throw new TypeError("offset must be a number");if(i>Ng||i<0)throw new TypeError("offset must be a uint32");if(i>Cg||i>t)throw new RangeError("offset out of range")}function Lg(i,t,r){if(typeof i!="number"||i!==i)throw new TypeError("size must be a number");if(i>Ng||i<0)throw new TypeError("size must be a uint32");if(i+t>r||i>Cg)throw new RangeError("buffer too small")}fu&&fu.getRandomValues||!process.browser?(ea.randomFill=N7,ea.randomFillSync=D7):(ea.randomFill=Ig,ea.randomFillSync=Ig);function N7(i,t,r,s){if(!Pg.isBuffer(i)&&!(i instanceof global.Uint8Array))throw new TypeError('"buf" argument must be a Buffer or Uint8Array');if(typeof t=="function")s=t,t=0,r=i.length;else if(typeof r=="function")s=r,r=i.length-t;else if(typeof s!="function")throw new TypeError('"cb" argument must be a function');return Dg(t,i.length),Lg(r,t,i.length),Og(i,t,r,s)}function Og(i,t,r,s){if(process.browser){var f=i.buffer,c=new Uint8Array(f,t,r);if(fu.getRandomValues(c),s){process.nextTick(function(){s(null,i)});return}return i}if(s){Rg(r,function(b,w){if(b)return s(b);w.copy(i,t),s(null,i)});return}var g=Rg(r);return g.copy(i,t),i}function D7(i,t,r){if(typeof t>"u"&&(t=0),!Pg.isBuffer(i)&&!(i instanceof global.Uint8Array))throw new TypeError('"buf" argument must be a Buffer or Uint8Array');return Dg(t,i.length),r===void 0&&(r=i.length-t),Lg(r,t,i.length),Og(i,t,r)}});var Ns=I(Ct=>{"use strict";Ct.randomBytes=Ct.rng=Ct.pseudoRandomBytes=Ct.prng=Ui();Ct.createHash=Ct.Hash=bn();Ct.createHmac=Ct.Hmac=Co();var L7=Rl(),O7=Object.keys(L7),F7=["sha1","sha224","sha256","sha384","sha512","md5","rmd160"].concat(O7);Ct.getHashes=function(){return F7};var Ug=us();Ct.pbkdf2=Ug.pbkdf2;Ct.pbkdf2Sync=Ug.pbkdf2Sync;var ei=yp();Ct.Cipher=ei.Cipher;Ct.createCipher=ei.createCipher;Ct.Cipheriv=ei.Cipheriv;Ct.createCipheriv=ei.createCipheriv;Ct.Decipher=ei.Decipher;Ct.createDecipher=ei.createDecipher;Ct.Decipheriv=ei.Decipheriv;Ct.createDecipheriv=ei.createDecipheriv;Ct.getCiphers=ei.getCiphers;Ct.listCiphers=ei.listCiphers;var ra=Pp();Ct.DiffieHellmanGroup=ra.DiffieHellmanGroup;Ct.createDiffieHellmanGroup=ra.createDiffieHellmanGroup;Ct.getDiffieHellman=ra.getDiffieHellman;Ct.createDiffieHellman=ra.createDiffieHellman;Ct.DiffieHellman=ra.DiffieHellman;var V0=ug();Ct.createSign=V0.createSign;Ct.Sign=V0.Sign;Ct.createVerify=V0.createVerify;Ct.Verify=V0.Verify;Ct.createECDH=pg();var j0=qg();Ct.publicEncrypt=j0.publicEncrypt;Ct.privateEncrypt=j0.privateEncrypt;Ct.publicDecrypt=j0.publicDecrypt;Ct.privateDecrypt=j0.privateDecrypt;var zg=Fg();Ct.randomFill=zg.randomFill;Ct.randomFillSync=zg.randomFillSync;Ct.createCredentials=function(){throw new Error(`sorry, createCredentials is not implemented yet
7
7
  we accept pull requests
8
- https://github.com/browserify/crypto-browserify`)};Ct.constants={DH_CHECK_P_NOT_SAFE_PRIME:2,DH_CHECK_P_NOT_PRIME:1,DH_UNABLE_TO_CHECK_GENERATOR:4,DH_NOT_SUITABLE_GENERATOR:8,NPN_ENABLED:1,ALPN_ENABLED:1,RSA_PKCS1_PADDING:1,RSA_SSLV23_PADDING:2,RSA_NO_PADDING:3,RSA_PKCS1_OAEP_PADDING:4,RSA_X931_PADDING:5,RSA_PKCS1_PSS_PADDING:6,POINT_CONVERSION_COMPRESSED:2,POINT_CONVERSION_UNCOMPRESSED:4,POINT_CONVERSION_HYBRID:6}});import sf from"bignumber.js";var qt=class i extends Error{constructor(r,s,f){super(r);this.code=s;this.details=f;this.name="GSwapSDKError"}static noSignerError(){return new i("This method requires a signer. Please provide a signer to the GSwap constructor.","NO_SIGNER")}static async fromErrorResponse(r,s){let f=await s.text(),c=(()=>{try{return JSON.parse(f)}catch{return}})(),g=typeof c=="object"&&c&&"error"in c&&typeof c.error=="object"&&c.error&&"ErrorKey"in c.error&&typeof c.error.ErrorKey=="string"?c.error.ErrorKey:void 0,b=typeof c=="object"&&c&&"error"in c&&typeof c.error=="object"&&c.error&&"Message"in c.error&&typeof c.error.Message=="string"?c.error.Message:void 0;return g&&b?new i(`GalaChain Error ${g} from ${r}: ${b}`,g,{message:b,errorKey:g,status:s.status,body:c,url:r}):new i(`Unexpected HTTP Error ${s.status} from ${r}`,"HTTP_ERROR",{status:s.status,body:c??f,url:r})}static incorrectTokenOrderingError(r,s){return new i("Token ordering is incorrect. token0 should sort below token1.","INCORRECT_TOKEN_ORDERING",{specifiedToken0:r,specifiedToken1:s})}static transactionWaitTimeoutError(r){return new i("Transaction wait timed out.","TRANSACTION_WAIT_TIMEOUT",{txId:r})}static transactionWaitFailedError(r,s){return new i("Transaction wait failed.","TRANSACTION_WAIT_FAILED",{txId:r,...s})}static socketConnectionRequiredError(){return new i("This method requires a socket connection. Did you call connectSocket()?","SOCKET_CONNECTION_REQUIRED")}static noPoolAvailableError(r,s,f){let c=f!==void 0?`No pool available for the specified token pair at fee tier ${f}`:"No pools available for the specified token pair";return new i(c,"NO_POOL_AVAILABLE",{tokenIn:r,tokenOut:s,fee:f})}};function $t(i,t,r=!1){let s=sf(i);if(!s.isFinite())throw new qt(`Invalid ${t}: must be a finite number`,"VALIDATION_ERROR",{type:"INVALID_NUMERIC_AMOUNT",parameterName:t,value:i,reason:"not_finite"});if(!r&&s.isZero())throw new qt(`Invalid ${t}: must be positive`,"VALIDATION_ERROR",{type:"INVALID_NUMERIC_AMOUNT",parameterName:t,value:i,reason:"zero_not_allowed"});if(s.isNegative())throw new qt(`Invalid ${t}: must be ${r?"non-negative":"positive"}`,"VALIDATION_ERROR",{type:"INVALID_NUMERIC_AMOUNT",parameterName:t,value:i,reason:"negative"})}function hu(i,t,r){let s=sf(i),f=sf(t),c=sf(r);if(c=c.isFinite()?c:sf(1e18),!s.isFinite()||!f.isFinite()||!c.isFinite()||!s.isPositive()||!f.isPositive()||!c.isPositive())throw new qt("Invalid price values: all prices must be finite and positive","VALIDATION_ERROR",{type:"INVALID_PRICE_VALUES",spotPrice:i,lowerPrice:t,upperPrice:r});if(f.isGreaterThan(c))throw new qt("Invalid price range: lower price must be less than or equal to upper price","VALIDATION_ERROR",{type:"INVALID_PRICE_RANGE",lowerPrice:t,upperPrice:r})}function oo(i,t){if(i<0||!Number.isInteger(i))throw new qt(`Invalid ${t}: must be a non-negative integer`,"VALIDATION_ERROR",{type:"INVALID_TOKEN_DECIMALS",parameterName:t,value:i})}function na(i,t){if(!Number.isInteger(i)||!Number.isInteger(t))throw new qt("Invalid tick values: ticks must be integers","VALIDATION_ERROR",{type:"INVALID_TICK_VALUES",tickLower:i,tickUpper:t});if(i>=t)throw new qt("Invalid tick range: tickLower must be less than tickUpper","VALIDATION_ERROR",{type:"INVALID_TICK_RANGE",tickLower:i,tickUpper:t});if(i<-886800||t>886800)throw new qt("Invalid tick range: ticks must be between -886800 and 886800","VALIDATION_ERROR",{type:"INVALID_TICK_BOUNDS",tickLower:i,tickUpper:t,minTick:-886800,maxTick:886800})}function ri(i){if(!Number.isInteger(i)||i<0)throw new qt("Invalid fee: must be a non-negative integer","VALIDATION_ERROR",{type:"INVALID_FEE",value:i})}function fa(i){if(!Number.isInteger(i)||i<=0)throw new qt("Invalid tick spacing: must be a positive integer","VALIDATION_ERROR",{type:"INVALID_TICK_SPACING",value:i})}function ii(i){if(i===void 0)throw new qt("Invalid wallet address: No wallet address provided","VALIDATION_ERROR",{type:"MISSING_WALLET_ADDRESS",hint:"Either provide a wallet address to the function you are calling, or set one when instantiating GSwapSDK"});if(!i||typeof i!="string"||i.trim().length===0)throw new qt("Invalid wallet address: must be a non-empty string","VALIDATION_ERROR",{type:"INVALID_WALLET_ADDRESS",value:i})}var aa=class{constructor(t,r){this.dexBackendBaseUrl=t;this.httpClient=r}async getUserAssets(t,r=1,s=10){if(ii(t),!Number.isInteger(r)||r<1)throw new Error("Invalid page: must be a positive integer");if(!Number.isInteger(s)||s<1||s>100)throw new Error("Invalid limit: must be an integer between 1 and 100");let f=await this.httpClient.sendGetRequest(this.dexBackendBaseUrl,"/user/assets","",{address:t,page:r.toString(),limit:s.toString()});return{tokens:f.data.token.map(c=>({...c,decimals:parseInt(c.decimals,10)}))||[],count:f.data.count||0}}};var $g=su(Ns(),1);import U7 from"debug";var xi=U7("GSWAP_SDK");var Kg=su(o0(),1);import{io as z7}from"socket.io-client";var au=class extends Kg.EventEmitter{},J0=class extends au{constructor(r){super();Nt(this,"socket",null);Nt(this,"bundlerUrl");this.bundlerUrl=r}connect(){return new Promise((r,s)=>{if(this.socket?.connected){r();return}this.socket=z7(this.bundlerUrl,{transports:["websocket"],autoConnect:!0}),this.socket.on("connect",()=>{console.log("Connected to bundler socket:",this.bundlerUrl),this.emit("connect"),r()}),this.socket.on("connect_error",f=>{console.error("Failed to connect to bundler socket:",f),this.emit("error",f),s(f)}),this.socket.on("disconnect",f=>{console.log("Disconnected from bundler socket:",f),this.emit("disconnect",f)}),this.socket.onAny((f,...c)=>{if(typeof f=="string"){let[g]=c;this.emit("transaction",f,g)}})})}disconnect(){this.socket&&(this.socket.disconnect(),this.socket=null)}isConnected(){return this.socket?.connected??!1}};var ia=class{constructor(){Nt(this,"enabled",!1);Nt(this,"promiseInfoForTxId",new Map)}setEnabled(t){if(this.enabled=t,!t){for(let[r,s]of this.promiseInfoForTxId.entries())clearTimeout(s.timeoutId),s.reject(qt.transactionWaitFailedError(r,{message:"Transaction waiter disabled"}));this.promiseInfoForTxId.clear()}}registerTxId(t,r){if(this.promiseInfoForTxId.has(t))throw new qt("Transaction ID is already registered","TRANSACTION_ID_ALREADY_REGISTERED",{txId:t});if(!this.enabled)return;let s,f,c=new Promise((b,w)=>{s=b,f=w}),g=setTimeout(()=>{let b=this.promiseInfoForTxId.get(t);b&&(b.waited?b.reject(qt.transactionWaitTimeoutError(t)):b.resolve(),this.promiseInfoForTxId.delete(t))},r);this.promiseInfoForTxId.set(t,{promise:c,resolve:s,reject:f,timeoutId:g,waited:!1})}wait(t){let r=this.promiseInfoForTxId.get(t);if(!r)throw new qt("Transaction ID is not registered for waiting","TRANSACTION_ID_NOT_REGISTERED",{txId:t});return r.waited=!0,r.promise}notifySuccess(t){let r=this.promiseInfoForTxId.get(t);r&&(clearTimeout(r.timeoutId),r.resolve(),this.promiseInfoForTxId.delete(t))}notifyFailure(t,r){let s=this.promiseInfoForTxId.get(t);s&&(clearTimeout(s.timeoutId),s.waited?s.reject(qt.transactionWaitFailedError(t,r)):s.resolve(),this.promiseInfoForTxId.delete(t))}};var ff=class ff{constructor(){Nt(this,"globalSocketClient");Nt(this,"globalWaitHelper",new ia);Nt(this,"connectionPromise")}async connectEventSocket(t){if(this.connectionPromise)return this.connectionPromise;if(this.globalSocketClient?.isConnected())return this.globalSocketClient;let r=t??"https://bundle-backend-test1.defi.gala.com";return this.connectionPromise=(async()=>{let s=new ff.tradeEventEmitterConstructor(r);return await s.connect(),this.globalWaitHelper||(this.globalWaitHelper=new ia),s.on("transaction",this.internalHandleSocketMessage.bind(this)),s})(),this.globalSocketClient=await this.connectionPromise,this.connectionPromise=void 0,this.globalWaitHelper.setEnabled(!0),this.globalSocketClient}disconnectEventSocket(){this.globalSocketClient&&(this.globalSocketClient.off("transaction",this.internalHandleSocketMessage.bind(this)),this.globalSocketClient.disconnect(),this.globalSocketClient=void 0,this.globalWaitHelper.setEnabled(!1))}eventSocketConnected(){return this.globalSocketClient?.isConnected()??!1}registerTxId(t,r){this.globalWaitHelper.registerTxId(t,r)}async wait(t){if(!this.eventSocketConnected())throw qt.socketConnectionRequiredError();await this.globalWaitHelper.wait(t)}internalHandleSocketMessage(t,r){r.status==="PROCESSED"?this.globalWaitHelper.notifySuccess(t):r.status==="FAILED"?this.globalWaitHelper.notifyFailure(t,{error:r.error,status:r.status,data:r.data}):r.status==="PENDING"||xi(`Unknown response status for transaction ${t}:`,r.status)}};Nt(ff,"instance",new ff),Nt(ff,"tradeEventEmitterConstructor",J0);var cn=ff;var X0=class{constructor(t,r,s,f){this.transactionId=t;this.message=r;this.error=s;this.waitDelegate=f}wait(){return this.waitDelegate()}};function K7(){return globalThis.crypto?globalThis.crypto.randomUUID():$g.default.randomUUID()}var Y0=class{constructor(t,r,s,f,c){this.bundlerBaseUrl=t;this.bundlingAPIBasePath=r;this.transactionWaitTimeoutMs=s;this.signer=f;this.httpClient=c}async signObject(t,r){if(!this.signer)throw qt.noSignerError();let s={...r,uniqueKey:r.uniqueKey??`galaswap - operation - ${K7()}`};return xi("Signing object for method",t,s),this.signer.signObject(t,s)}async sendBundlerRequest(t,r,s){if(!this.hasSigner())throw qt.noSignerError();let f={method:t,signedDto:await this.signObject(t,r),stringsInstructions:s};xi("Sending bundler request",t,f);let c=await this.httpClient.sendPostRequest(this.bundlerBaseUrl,this.bundlingAPIBasePath,"",f);return xi("Received bundler response",t,c),cn.instance.registerTxId(c.data,this.transactionWaitTimeoutMs),new X0(c.data,c.message,c.error,()=>cn.instance.wait(c.data))}hasSigner(){return this.signer!==void 0}};var af=class{constructor(t=fetch){this.httpRequestor=t}async sendRequest(t,r,s,f,c){let g=`${r}${s}${f}`;xi(`Sending request to ${g} with body:`,c);let b=await this.httpRequestor(g,{method:t,headers:{"Content-Type":"application/json","User-Agent":"GalaChain-SDK/0.0"},...c?{body:JSON.stringify(c)}:{}});if(!b.ok)throw await qt.fromErrorResponse(g,b);let w=await b.json();return xi(`Response from ${r}${s}${f}:`,w),w}async sendPostRequest(t,r,s,f){return this.sendRequest("POST",t,r,s,f)}async sendGetRequest(t,r,s,f){let c=f?new URLSearchParams(f):void 0,g=c?`${s}?${c.toString()}`:s;return this.sendRequest("GET",t,r,g,void 0)}};import hr from"bignumber.js";function rr(i,t="|"){return typeof i=="string"?i:`${i.collection}${t}${i.category}${t}${i.type}${t}${i.additionalKey}`}function Pe(i){if(typeof i=="object")return{collection:i.collection,category:i.category,type:i.type,additionalKey:i.additionalKey};let[t,r,s,f]=i.split("|");if(!t||!r||!s||!f)throw new qt("Invalid token class key","INVALID_TOKEN_CLASS_KEY",{tokenClassKey:i});return{collection:t,category:r,type:s,additionalKey:f}}function $7(i,t){return rr(i).localeCompare(rr(t))}function Sr(i,t,r,s,f){let c=$7(i,t)<0;if(c)return{token0:i,token1:t,zeroForOne:c,token0Attributes:s,token1Attributes:f};if(r)throw qt.incorrectTokenOrderingError(i,t);return{token0:t,token1:i,zeroForOne:c,token0Attributes:f,token1Attributes:s}}var Q0=class{constructor(t,r,s){this.gatewayBaseUrl=t;this.dexContractBasePath=r;this.httpClient=s}async getPoolData(t,r,s){ri(s);let f=Pe(t),c=Pe(r),g=Sr(f,c,!1),w=(await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,"/GetPoolData",{token0:g.token0,token1:g.token1,fee:s})).Data;return{bitmap:w.bitmap,fee:w.fee,feeGrowthGlobal0:hr(w.feeGrowthGlobal0),feeGrowthGlobal1:hr(w.feeGrowthGlobal1),grossPoolLiquidity:hr(w.grossPoolLiquidity),liquidity:hr(w.liquidity),maxLiquidityPerTick:hr(w.maxLiquidityPerTick),protocolFees:w.protocolFees,protocolFeesToken0:hr(w.protocolFeesToken0),protocolFeesToken1:hr(w.protocolFeesToken1),sqrtPrice:hr(w.sqrtPrice),tickSpacing:w.tickSpacing,token0:w.token0,token0ClassKey:w.token0ClassKey,token1:w.token1,token1ClassKey:w.token1ClassKey}}calculateTicksForPrice(t,r){$t(t,"price",!0),fa(r);let s=hr(t).toNumber();if(s===0)return-886800;if(s===1/0)return 886800;let f=Math.round(Math.log(s)/Math.log(1.0001)),c=Math.floor(f/r)*r;return Math.min(Math.max(c,-886800),886800)}calculatePriceForTicks(t){if(t===-886800)return hr("0");if(t===886800)return hr(1/0);{let r=Math.pow(1.0001,t);return hr(r)}}calculateSpotPrice(t,r,s){$t(s,"poolSqrtPrice");let f=Sr(t,r,!1),c=hr(s).pow(2);return f.zeroForOne?c:hr(1).div(c)}};import ce from"bignumber.js";var to=class{constructor(t,r,s,f,c,g){this.gatewayBaseUrl=t;this.dexContractBasePath=r;this.options=g;Nt(this,"httpClient");Nt(this,"bundlerService");Nt(this,"poolService");this.httpClient=c,this.bundlerService=s,this.poolService=f}async getUserPositions(t,r,s){let f=await this.sendUserPositionsRequest("/GetUserPositions",{user:t,limit:r,bookmark:s});return{bookmark:f.nextBookMark,positions:f.positions}}async getPosition(t,r){return await this.sendPositionRequest("/GetPositions",{owner:t,token0:Pe(r.token0ClassKey),token1:Pe(r.token1ClassKey),fee:r.fee,tickLower:r.tickLower,tickUpper:r.tickUpper})}async getPositionById(t,r){let f=(await this.getUserPositions(t)).positions.find(c=>c.positionId===r);if(f)return this.getPosition(t,f)}async addLiquidityByTicks(t){let r=t.walletAddress??this.options?.walletAddress;ii(r),ri(t.fee),na(t.tickLower,t.tickUpper),$t(t.amount0Desired,"amount0Desired"),$t(t.amount1Desired,"amount1Desired"),$t(t.amount0Min,"amount0Min",!0),$t(t.amount1Min,"amount1Min",!0);let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Desired,t.amount0Min],[t.amount1Desired,t.amount1Min]),g={token0:c.token0,token1:c.token1,fee:t.fee,owner:t.walletAddress,tickLower:t.tickLower,tickUpper:t.tickUpper,amount0Desired:ce(c.token0Attributes[0]).toFixed(),amount1Desired:ce(c.token1Attributes[0]).toFixed(),amount0Min:ce(c.token0Attributes[1]).toFixed(),amount1Min:ce(c.token1Attributes[1]).toFixed(),positionId:t.positionId},b=rr(c.token0,"$"),w=rr(c.token1,"$"),_=`$pool$${b}$${w}$${t.fee}`,S=`$userPosition$${t.walletAddress}`,A=`$tokenBalance$${b}$${t.walletAddress}`,q=`$tokenBalance$${w}$${t.walletAddress}`,R=`$tokenBalance$${b}$${_}`,D=`$tokenBalance$${w}$${_}`,bt=[_,S,A,q,R,D];return this.bundlerService.sendBundlerRequest("AddLiquidity",g,bt)}async addLiquidityByPrice(t){let r=t.walletAddress??this.options?.walletAddress;if(ii(r),ri(t.fee),fa(t.tickSpacing),$t(t.minPrice,"minPrice",!0),$t(t.maxPrice,"maxPrice"),$t(t.amount0Desired,"amount0Desired"),$t(t.amount1Desired,"amount1Desired"),$t(t.amount0Min,"amount0Min",!0),$t(t.amount1Min,"amount1Min",!0),ce(t.minPrice).isGreaterThan(ce(t.maxPrice)))throw new Error("Invalid price range: minPrice must be less than or equal to maxPrice");let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Desired,t.amount0Min],[t.amount1Desired,t.amount1Min]),g=this.poolService.calculateTicksForPrice(t.minPrice,t.tickSpacing),b=this.poolService.calculateTicksForPrice(t.maxPrice,t.tickSpacing),w=c.zeroForOne?g:b*-1,_=c.zeroForOne?b:g*-1,S={token0:c.token0,token1:c.token1,fee:t.fee,owner:t.walletAddress,tickLower:w,tickUpper:_,amount0Desired:c?.token0Attributes?.[0],amount1Desired:c?.token1Attributes?.[0],amount0Min:c?.token0Attributes?.[1],amount1Min:c?.token1Attributes?.[1],positionId:t.positionId},A=rr(c.token0,"$"),q=rr(c.token1,"$"),R=`$pool$${A}$${q}$${t.fee}`,D=`$userPosition$${t.walletAddress}`,bt=`$tokenBalance$${A}$${t.walletAddress}`,_t=`$tokenBalance$${q}$${t.walletAddress}`,gt=`$tokenBalance$${A}$${R}`,xt=`$tokenBalance$${q}$${R}`,Mt=[R,D,bt,_t,gt,xt];return this.bundlerService.sendBundlerRequest("AddLiquidity",S,Mt)}async removeLiquidity(t){let r=t.walletAddress??this.options?.walletAddress;ii(r),ri(t.fee),na(t.tickLower,t.tickUpper),$t(t.amount,"amount"),t.amount0Min!==void 0&&$t(t.amount0Min,"amount0Min",!0),t.amount1Min!==void 0&&$t(t.amount1Min,"amount1Min",!0);let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Min??0],[t.amount1Min??0]),g={token0:c.token0,token1:c.token1,fee:t.fee,tickLower:t.tickLower,tickUpper:t.tickUpper,amount:ce(t.amount).toFixed(),amount0Min:c?.token0Attributes?.[0]||"0",amount1Min:c?.token1Attributes?.[0]||"0",positionId:t.positionId},b=rr(c.token0,"$"),w=rr(c.token1,"$"),_=`$pool$${b}$${w}$${t.fee}`,S=`$userPosition$${t.walletAddress}`,A=`$tokenBalance$${b}$${t.walletAddress}`,q=`$tokenBalance$${w}$${t.walletAddress}`,R=`$tokenBalance$${b}$${_}`,D=`$tokenBalance$${w}$${_}`,bt=[_,S,A,q,R,D];return this.bundlerService.sendBundlerRequest("RemoveLiquidity",g,bt)}async collectPositionFees(t){let r=t.walletAddress??this.options?.walletAddress;ii(r),ri(t.fee),na(t.tickLower,t.tickUpper),$t(t.amount0Requested,"amount0Requested",!0),$t(t.amount1Requested,"amount1Requested",!0);let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Requested],[t.amount1Requested]),g={token0:c.token0,token1:c.token1,fee:t.fee,amount0Requested:ce(c.token0Attributes[0]).toFixed(),amount1Requested:ce(c.token1Attributes[0]).toFixed(),tickLower:t.tickLower,tickUpper:t.tickUpper,positionId:t.positionId},b=rr(c.token0,"$"),w=rr(c.token1,"$"),_=`$pool$${b}$${w}$${t.fee}`,S=`$userPosition$${t.walletAddress}`,A=`$tokenBalance$${b}$${t.walletAddress}`,q=`$tokenBalance$${w}$${t.walletAddress}`,R=`$tokenBalance$${b}$${_}`,D=`$tokenBalance$${w}$${_}`,bt=[_,S,A,q,R,D];return this.bundlerService.sendBundlerRequest("CollectPositionFees",g,bt)}calculateOptimalPositionSize(t,r,s,f,c,g){$t(t,"tokenAmount"),hu(r,s,f),oo(c,"tokenDecimals"),oo(g,"otherTokenDecimals");let b=ce(t),w=ce(r),_=ce(s),S=ce(f);S=S.isFinite()?S:ce(1e18);let A=b.times(10**(c-g)).times(w.sqrt()).times(S.sqrt()).div(S.sqrt().minus(w.sqrt())),R=ce(A).times(w.sqrt().minus(_.sqrt())).div(ce(10).pow(c-g));return ce(R.toFixed(g,ce.ROUND_DOWN))}async sendUserPositionsRequest(t,r){let s=await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,t,r);return{nextBookMark:s.Data.nextBookMark,positions:s.Data.positions.map(f=>({...f,liquidity:ce(f.liquidity)}))}}async sendPositionRequest(t,r){let s=await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,t,r);return{...s.Data,feeGrowthInside0Last:ce(s.Data.feeGrowthInside0Last),feeGrowthInside1Last:ce(s.Data.feeGrowthInside1Last),liquidity:ce(s.Data.liquidity),tokensOwed0:ce(s.Data.tokensOwed0),tokensOwed1:ce(s.Data.tokensOwed1)}}};import Di from"bignumber.js";var Hg=(s=>(s[s.PERCENT_00_05=500]="PERCENT_00_05",s[s.PERCENT_00_30=3e3]="PERCENT_00_30",s[s.PERCENT_01_00=1e4]="PERCENT_01_00",s))(Hg||{});var eo=class{constructor(t,r,s){this.gatewayBaseUrl=t;this.dexContractBasePath=r;Nt(this,"httpClient");this.httpClient=s??new af}async quoteExactInput(t,r,s,f){if($t(s,"amountIn"),f!==void 0)return await this.getSingleQuoteExactInput(t,r,f,s);let g=[500,3e3,1e4].map(async _=>{try{return await this.getSingleQuoteExactInput(t,r,_,s)}catch(S){if(S instanceof qt&&(S.code==="CONFLICT"||S.code==="OBJECT_NOT_FOUND"))return;throw S}}),w=(await Promise.all(g)).filter(_=>_!==void 0);if(w.length===0)throw qt.noPoolAvailableError(t,r);return w.reduce((_,S)=>S.outTokenAmount.isGreaterThan(_.outTokenAmount)?S:_)}async quoteExactOutput(t,r,s,f){if($t(s,"amountOut"),f!==void 0)return await this.getSingleQuoteExactOutput(t,r,s,f);let g=[500,3e3,1e4].map(async _=>{try{return await this.getSingleQuoteExactOutput(t,r,s,_)}catch(S){if(S instanceof qt&&(S.code==="CONFLICT"||S.code==="OBJECT_NOT_FOUND"))return;throw S}}),w=(await Promise.all(g)).filter(_=>_!==void 0);if(w.length===0)throw qt.noPoolAvailableError(t,r);return w.reduce((_,S)=>S.inTokenAmount.isLessThan(_.inTokenAmount)?S:_)}async getSingleQuote(t,r,s,f,c){let g=Pe(t),b=Pe(r),w=Sr(g,b,!1),_=c?Di(f).toFixed():Di(f).multipliedBy(-1).toFixed(),S=await this.sendQuoteRequest("/QuoteExactAmount",{...w,fee:s,amount:_}),A=w.zeroForOne?S.amount0:S.amount1,q=w.zeroForOne?S.amount1:S.amount0,R=S.currentSqrtPrice.pow(2),D=S.newSqrtPrice.pow(2);w.zeroForOne||(R=Di(1).dividedBy(R),D=Di(1).dividedBy(D));let bt=D.minus(R).dividedBy(R);return{amount0:S.amount0,amount1:S.amount1,currentPoolSqrtPrice:S.currentSqrtPrice,newPoolSqrtPrice:S.newSqrtPrice,currentPrice:R,newPrice:D,inTokenAmount:A.abs(),outTokenAmount:q.abs(),priceImpact:bt,feeTier:s}}async getSingleQuoteExactInput(t,r,s,f){return this.getSingleQuote(t,r,s,f,!0)}async getSingleQuoteExactOutput(t,r,s,f){return this.getSingleQuote(t,r,f,s,!1)}async sendQuoteRequest(t,r){let f=await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,t,r);return{amount0:Di(f.Data.amount0),amount1:Di(f.Data.amount1),currentSqrtPrice:Di(f.Data.currentSqrtPrice),newSqrtPrice:Di(f.Data.newSqrtPrice)}}};import of from"bignumber.js";var H7="0.000000000000000000094212147",G7="18446050999999999999",ro=class{constructor(t,r){this.bundlerService=t;this.options=r}async swap(t,r,s,f,c){c=c??this.options?.walletAddress,ii(c),ri(s),"exactIn"in f?($t(f.exactIn,"exactIn"),f.amountOutMinimum!==void 0&&$t(f.amountOutMinimum,"amountOutMinimum",!0)):($t(f.exactOut,"exactOut"),f.amountInMaximum!==void 0&&$t(f.amountInMaximum,"amountInMaximum"));let g=Sr(t,r,!1),b=rr(t)===rr(g.token0),w="exactIn"in f?of(f.exactIn).toFixed():of(f.exactOut).multipliedBy(-1).toFixed(),_="exactIn"in f?f.amountOutMinimum?.toString()?of(f.amountOutMinimum).multipliedBy(-1).toFixed():void 0:of(f.exactOut??0).multipliedBy(-1).toFixed(),S="exactIn"in f?of(f.exactIn).toFixed():f.amountInMaximum?of(f.amountInMaximum).toFixed():void 0,A={token0:Pe(g.token0),token1:Pe(g.token1),fee:s,amount:w,zeroForOne:b,sqrtPriceLimit:g.zeroForOne?H7:G7,recipient:c,amountOutMinimum:_,amountInMaximum:S},q=rr(g.token0,"$"),R=rr(g.token1,"$"),D=`$pool$${q}$${R}$${s}`,bt=`$tokenBalance$${q}$${c}`,_t=`$tokenBalance$${R}$${c}`,gt=`$tokenBalance$${q}$${D}`,xt=`$tokenBalance$${R}$${D}`,Mt=[D,bt,_t,gt,xt];return this.bundlerService.sendBundlerRequest("Swap",A,Mt)}};var ou=class{constructor(t){Nt(this,"gatewayBaseUrl");Nt(this,"dexContractBasePath");Nt(this,"tokenContractBasePath");Nt(this,"bundlerBaseUrl");Nt(this,"bundlingAPIBasePath");Nt(this,"dexBackendBaseUrl");Nt(this,"transactionWaitTimeoutMs");Nt(this,"signer");Nt(this,"quoting");Nt(this,"positions");Nt(this,"swaps");Nt(this,"assets");Nt(this,"httpClient");Nt(this,"httpRequestor");Nt(this,"bundler");Nt(this,"pools");this.gatewayBaseUrl=t?.gatewayBaseUrl?.replace(/\/$/,"")??"https://gateway-mainnet.galachain.com",this.dexContractBasePath=t?.dexContractBasePath??"/api/asset/dexv3-contract",this.tokenContractBasePath=t?.tokenContractBasePath??"/api/asset/token-contract",this.bundlerBaseUrl=t?.bundlerBaseUrl??"https://bundle-backend-prod1.defi.gala.com",this.bundlingAPIBasePath=t?.bundlingAPIBasePath??"/bundle",this.dexBackendBaseUrl=t?.dexBackendBaseUrl??"https://dex-backend-prod1.defi.gala.com",this.signer=t?.signer,this.transactionWaitTimeoutMs=t?.transactionWaitTimeoutMs??3e5,this.httpRequestor=t?.httpRequestor??fetch.bind(globalThis),this.httpClient=new af(this.httpRequestor),this.bundler=new Y0(this.bundlerBaseUrl,this.bundlingAPIBasePath,this.transactionWaitTimeoutMs,this.signer,this.httpClient),this.pools=new Q0(this.gatewayBaseUrl,this.dexContractBasePath,this.httpClient),this.quoting=new eo(this.gatewayBaseUrl,this.dexContractBasePath,this.httpClient),this.positions=new to(this.gatewayBaseUrl,this.dexContractBasePath,this.bundler,this.pools,this.httpClient,{walletAddress:t?.walletAddress}),this.swaps=new ro(this.bundler,{walletAddress:t?.walletAddress}),this.assets=new aa(this.dexBackendBaseUrl,this.httpClient)}};Nt(ou,"events",cn.instance);import{signatures as Gg}from"@gala-chain/api";import{calculatePersonalSignPrefix as Z7}from"@gala-chain/connect";var Zg=class{constructor(t){Nt(this,"keyBuffer");this.keyBuffer=Gg.normalizePrivateKey(t)}async signObject(t,r){let s=Gg.getSignature(r,this.keyBuffer);return{...r,signature:s}}},Wg=class i{constructor(t){this.walletAddress=t}async signObject(t,r){let s=global.gala;if(!s)throw new qt("Gala wallet is not available. Please ensure the Gala wallet is connected.","GALA_WALLET_NOT_AVAILABLE");let f={name:"ethereum",chainId:1},c=i.generateEIP712Types(t,r),g={domain:f,types:c,Primary_type:t,message:{...r,prefix:Z7(r)}};await s.setAddress(this.walletAddress);let b=await s.request({method:"eth_signTypedData",params:[JSON.stringify(g),this.walletAddress]});return{...r,domain:f,types:c,signature:b}}static generateEIP712Types(t,r){let s={};s[t]=[];function f(c,g,b,w=!1){if(g!==void 0)if(Array.isArray(g)){let _=f(c,g[0],b,!0);w||s[b].push({name:c,type:(_??c)+"[]"})}else if(typeof g=="object"&&g!==null){if(s[c])throw new qt("Name collisions not yet supported in EIP712 type generation","EIP712_NAME_COLLISION",{name:c});s[c]=[],Object.entries(g).forEach(([_,S])=>{f(_,S,c)}),w||s[b].push({name:c,type:c})}else{let _;switch(typeof g){case"string":_="string";break;case"number":_="int256";break;case"boolean":_="bool";break;default:throw new qt(`Unsupported type for EIP712 signing: ${typeof g}`,"EIP712_UNSUPPORTED_TYPE",{type:typeof g,value:g})}if(w)return _;s[b].push({name:c,type:_})}}return Object.entries(r).forEach(([c,g])=>{f(c,g,t)}),s}};export{aa as Assets,Y0 as Bundler,J0 as EventSocketClient,cn as Events,Hg as FEE_TIER,ou as GSwap,qt as GSwapSDKError,Wg as GalaWalletSigner,X0 as PendingTransaction,Q0 as Pools,to as Positions,Zg as PrivateKeySigner,eo as Quoting,ro as Swaps,au as TradeEventEmitter,$7 as compareTokens,Sr as getTokenOrdering,Pe as parseTokenClassKey,rr as stringifyTokenClassKey,ri as validateFee,$t as validateNumericAmount,hu as validatePriceValues,na as validateTickRange,fa as validateTickSpacing,oo as validateTokenDecimals,ii as validateWalletAddress};
8
+ https://github.com/browserify/crypto-browserify`)};Ct.constants={DH_CHECK_P_NOT_SAFE_PRIME:2,DH_CHECK_P_NOT_PRIME:1,DH_UNABLE_TO_CHECK_GENERATOR:4,DH_NOT_SUITABLE_GENERATOR:8,NPN_ENABLED:1,ALPN_ENABLED:1,RSA_PKCS1_PADDING:1,RSA_SSLV23_PADDING:2,RSA_NO_PADDING:3,RSA_PKCS1_OAEP_PADDING:4,RSA_X931_PADDING:5,RSA_PKCS1_PSS_PADDING:6,POINT_CONVERSION_COMPRESSED:2,POINT_CONVERSION_UNCOMPRESSED:4,POINT_CONVERSION_HYBRID:6}});import sf from"bignumber.js";var qt=class i extends Error{constructor(r,s,f){super(r);this.code=s;this.details=f;this.name="GSwapSDKError"}static noSignerError(){return new i("This method requires a signer. Please provide a signer to the GSwap constructor.","NO_SIGNER")}static async fromErrorResponse(r,s){let f=await s.text(),c=(()=>{try{return JSON.parse(f)}catch{return}})(),g=typeof c=="object"&&c&&"error"in c&&typeof c.error=="object"&&c.error&&"ErrorKey"in c.error&&typeof c.error.ErrorKey=="string"?c.error.ErrorKey:void 0,b=typeof c=="object"&&c&&"error"in c&&typeof c.error=="object"&&c.error&&"Message"in c.error&&typeof c.error.Message=="string"?c.error.Message:void 0;return g&&b?new i(`GalaChain Error ${g} from ${r}: ${b}`,g,{message:b,errorKey:g,status:s.status,body:c,url:r}):new i(`Unexpected HTTP Error ${s.status} from ${r}`,"HTTP_ERROR",{status:s.status,body:c??f,url:r})}static incorrectTokenOrderingError(r,s){return new i("Token ordering is incorrect. token0 should sort below token1.","INCORRECT_TOKEN_ORDERING",{specifiedToken0:r,specifiedToken1:s})}static transactionWaitTimeoutError(r){return new i("Transaction wait timed out.","TRANSACTION_WAIT_TIMEOUT",{txId:r})}static transactionWaitFailedError(r,s){return new i("Transaction wait failed.","TRANSACTION_WAIT_FAILED",{txId:r,...s})}static socketConnectionRequiredError(){return new i("This method requires a socket connection. Did you call connectSocket()?","SOCKET_CONNECTION_REQUIRED")}static noPoolAvailableError(r,s,f){let c=f!==void 0?`No pool available for the specified token pair at fee tier ${f}`:"No pools available for the specified token pair";return new i(c,"NO_POOL_AVAILABLE",{tokenIn:r,tokenOut:s,fee:f})}};function $t(i,t,r=!1){let s=sf(i);if(!s.isFinite())throw new qt(`Invalid ${t}: must be a finite number`,"VALIDATION_ERROR",{type:"INVALID_NUMERIC_AMOUNT",parameterName:t,value:i,reason:"not_finite"});if(!r&&s.isZero())throw new qt(`Invalid ${t}: must be positive`,"VALIDATION_ERROR",{type:"INVALID_NUMERIC_AMOUNT",parameterName:t,value:i,reason:"zero_not_allowed"});if(s.isNegative())throw new qt(`Invalid ${t}: must be ${r?"non-negative":"positive"}`,"VALIDATION_ERROR",{type:"INVALID_NUMERIC_AMOUNT",parameterName:t,value:i,reason:"negative"})}function hu(i,t,r){let s=sf(i),f=sf(t),c=sf(r);if(c=c.isFinite()?c:sf(1e18),!s.isFinite()||!f.isFinite()||!c.isFinite()||!s.isPositive()||!f.isPositive()||!c.isPositive())throw new qt("Invalid price values: all prices must be finite and positive","VALIDATION_ERROR",{type:"INVALID_PRICE_VALUES",spotPrice:i,lowerPrice:t,upperPrice:r});if(f.isGreaterThan(c))throw new qt("Invalid price range: lower price must be less than or equal to upper price","VALIDATION_ERROR",{type:"INVALID_PRICE_RANGE",lowerPrice:t,upperPrice:r})}function oo(i,t){if(i<0||!Number.isInteger(i))throw new qt(`Invalid ${t}: must be a non-negative integer`,"VALIDATION_ERROR",{type:"INVALID_TOKEN_DECIMALS",parameterName:t,value:i})}function na(i,t){if(!Number.isInteger(i)||!Number.isInteger(t))throw new qt("Invalid tick values: ticks must be integers","VALIDATION_ERROR",{type:"INVALID_TICK_VALUES",tickLower:i,tickUpper:t});if(i>=t)throw new qt("Invalid tick range: tickLower must be less than tickUpper","VALIDATION_ERROR",{type:"INVALID_TICK_RANGE",tickLower:i,tickUpper:t});if(i<-886800||t>886800)throw new qt("Invalid tick range: ticks must be between -886800 and 886800","VALIDATION_ERROR",{type:"INVALID_TICK_BOUNDS",tickLower:i,tickUpper:t,minTick:-886800,maxTick:886800})}function ri(i){if(!Number.isInteger(i)||i<0)throw new qt("Invalid fee: must be a non-negative integer","VALIDATION_ERROR",{type:"INVALID_FEE",value:i})}function fa(i){if(!Number.isInteger(i)||i<=0)throw new qt("Invalid tick spacing: must be a positive integer","VALIDATION_ERROR",{type:"INVALID_TICK_SPACING",value:i})}function ii(i){if(i===void 0)throw new qt("Invalid wallet address: No wallet address provided","VALIDATION_ERROR",{type:"MISSING_WALLET_ADDRESS",hint:"Either provide a wallet address to the function you are calling, or set one when instantiating GSwapSDK"});if(!i||typeof i!="string"||i.trim().length===0)throw new qt("Invalid wallet address: must be a non-empty string","VALIDATION_ERROR",{type:"INVALID_WALLET_ADDRESS",value:i})}var aa=class{constructor(t,r){this.dexBackendBaseUrl=t;this.httpClient=r}async getUserAssets(t,r=1,s=10){if(ii(t),!Number.isInteger(r)||r<1)throw new Error("Invalid page: must be a positive integer");if(!Number.isInteger(s)||s<1||s>100)throw new Error("Invalid limit: must be an integer between 1 and 100");let f=await this.httpClient.sendGetRequest(this.dexBackendBaseUrl,"/user/assets","",{address:t,page:r.toString(),limit:s.toString()});return{tokens:f.data.token.map(c=>({...c,decimals:parseInt(c.decimals,10)}))||[],count:f.data.count||0}}};var $g=su(Ns(),1);import U7 from"debug";var xi=U7("GSWAP_SDK");var Kg=su(o0(),1);import{io as z7}from"socket.io-client";var au=class extends Kg.EventEmitter{},J0=class extends au{constructor(r){super();Nt(this,"socket",null);Nt(this,"bundlerUrl");this.bundlerUrl=r}connect(){return new Promise((r,s)=>{if(this.socket?.connected){r();return}this.socket=z7(this.bundlerUrl,{transports:["websocket"],autoConnect:!0}),this.socket.on("connect",()=>{console.log("Connected to bundler socket:",this.bundlerUrl),this.emit("connect"),r()}),this.socket.on("connect_error",f=>{console.error("Failed to connect to bundler socket:",f),this.emit("error",f),s(f)}),this.socket.on("disconnect",f=>{console.log("Disconnected from bundler socket:",f),this.emit("disconnect",f)}),this.socket.onAny((f,...c)=>{if(typeof f=="string"){let[g]=c;this.emit("transaction",f,g)}})})}disconnect(){this.socket&&(this.socket.disconnect(),this.socket=null)}isConnected(){return this.socket?.connected??!1}};var ia=class{constructor(){Nt(this,"enabled",!1);Nt(this,"promiseInfoForTxId",new Map)}setEnabled(t){if(this.enabled=t,!t){for(let[r,s]of this.promiseInfoForTxId.entries())clearTimeout(s.timeoutId),s.reject(qt.transactionWaitFailedError(r,{message:"Transaction waiter disabled"}));this.promiseInfoForTxId.clear()}}registerTxId(t,r){if(this.promiseInfoForTxId.has(t))throw new qt("Transaction ID is already registered","TRANSACTION_ID_ALREADY_REGISTERED",{txId:t});if(!this.enabled)return;let s,f,c=new Promise((b,w)=>{s=b,f=w}),g=setTimeout(()=>{let b=this.promiseInfoForTxId.get(t);b&&(b.waited?b.reject(qt.transactionWaitTimeoutError(t)):b.resolve(),this.promiseInfoForTxId.delete(t))},r);this.promiseInfoForTxId.set(t,{promise:c,resolve:s,reject:f,timeoutId:g,waited:!1})}wait(t){let r=this.promiseInfoForTxId.get(t);if(!r)throw new qt("Transaction ID is not registered for waiting","TRANSACTION_ID_NOT_REGISTERED",{txId:t});return r.waited=!0,r.promise}notifySuccess(t){let r=this.promiseInfoForTxId.get(t);r&&(clearTimeout(r.timeoutId),r.resolve(),this.promiseInfoForTxId.delete(t))}notifyFailure(t,r){let s=this.promiseInfoForTxId.get(t);s&&(clearTimeout(s.timeoutId),s.waited?s.reject(qt.transactionWaitFailedError(t,r)):s.resolve(),this.promiseInfoForTxId.delete(t))}};var ff=class ff{constructor(){Nt(this,"globalSocketClient");Nt(this,"globalWaitHelper",new ia);Nt(this,"connectionPromise")}async connectEventSocket(t){if(this.connectionPromise)return this.connectionPromise;if(this.globalSocketClient?.isConnected())return this.globalSocketClient;let r=t??"https://bundle-backend-test1.defi.gala.com";return this.connectionPromise=(async()=>{let s=new ff.tradeEventEmitterConstructor(r);return await s.connect(),this.globalWaitHelper||(this.globalWaitHelper=new ia),s.on("transaction",this.internalHandleSocketMessage.bind(this)),s})(),this.globalSocketClient=await this.connectionPromise,this.connectionPromise=void 0,this.globalWaitHelper.setEnabled(!0),this.globalSocketClient}disconnectEventSocket(){this.globalSocketClient&&(this.globalSocketClient.off("transaction",this.internalHandleSocketMessage.bind(this)),this.globalSocketClient.disconnect(),this.globalSocketClient=void 0,this.globalWaitHelper.setEnabled(!1))}eventSocketConnected(){return this.globalSocketClient?.isConnected()??!1}registerTxId(t,r){this.globalWaitHelper.registerTxId(t,r)}async wait(t){if(!this.eventSocketConnected())throw qt.socketConnectionRequiredError();await this.globalWaitHelper.wait(t)}internalHandleSocketMessage(t,r){r.status==="PROCESSED"?this.globalWaitHelper.notifySuccess(t):r.status==="FAILED"?this.globalWaitHelper.notifyFailure(t,{error:r.error,status:r.status,data:r.data}):r.status==="PENDING"||xi(`Unknown response status for transaction ${t}:`,r.status)}};Nt(ff,"instance",new ff),Nt(ff,"tradeEventEmitterConstructor",J0);var cn=ff;var X0=class{constructor(t,r,s,f){this.transactionId=t;this.message=r;this.error=s;this.waitDelegate=f}wait(){return this.waitDelegate()}};function K7(){return globalThis.crypto?globalThis.crypto.randomUUID():$g.default.randomUUID()}var Y0=class{constructor(t,r,s,f,c){this.bundlerBaseUrl=t;this.bundlingAPIBasePath=r;this.transactionWaitTimeoutMs=s;this.signer=f;this.httpClient=c}async signObject(t,r){if(!this.signer)throw qt.noSignerError();let s={...r,uniqueKey:r.uniqueKey??`galaswap - operation - ${K7()}`};return xi("Signing object for method",t,s),this.signer.signObject(t,s)}async sendBundlerRequest(t,r,s){if(!this.hasSigner())throw qt.noSignerError();let f={method:t,signedDto:await this.signObject(t,r),stringsInstructions:s};xi("Sending bundler request",t,f);let c=await this.httpClient.sendPostRequest(this.bundlerBaseUrl,this.bundlingAPIBasePath,"",f);return xi("Received bundler response",t,c),cn.instance.registerTxId(c.data,this.transactionWaitTimeoutMs),new X0(c.data,c.message,c.error,()=>cn.instance.wait(c.data))}hasSigner(){return this.signer!==void 0}};var af=class{constructor(t=fetch){this.httpRequestor=t}async sendRequest(t,r,s,f,c){let g=`${r}${s}${f}`;xi(`Sending request to ${g} with body:`,c);let b=await this.httpRequestor(g,{method:t,headers:{"Content-Type":"application/json","User-Agent":"GalaChain-SDK/0.0"},...c?{body:JSON.stringify(c)}:{}});if(!b.ok)throw await qt.fromErrorResponse(g,b);let w=await b.json();return xi(`Response from ${r}${s}${f}:`,w),w}async sendPostRequest(t,r,s,f){return this.sendRequest("POST",t,r,s,f)}async sendGetRequest(t,r,s,f){let c=f?new URLSearchParams(f):void 0,g=c?`${s}?${c.toString()}`:s;return this.sendRequest("GET",t,r,g,void 0)}};import hr from"bignumber.js";function rr(i,t="|"){return typeof i=="string"?i:`${i.collection}${t}${i.category}${t}${i.type}${t}${i.additionalKey}`}function Pe(i){if(typeof i=="object")return{collection:i.collection,category:i.category,type:i.type,additionalKey:i.additionalKey};let[t,r,s,f]=i.split("|");if(!t||!r||!s||!f)throw new qt("Invalid token class key","INVALID_TOKEN_CLASS_KEY",{tokenClassKey:i});return{collection:t,category:r,type:s,additionalKey:f}}function $7(i,t){return rr(i).localeCompare(rr(t))}function Sr(i,t,r,s,f){let c=$7(i,t)<0;if(c)return{token0:i,token1:t,zeroForOne:c,token0Attributes:s,token1Attributes:f};if(r)throw qt.incorrectTokenOrderingError(i,t);return{token0:t,token1:i,zeroForOne:c,token0Attributes:f,token1Attributes:s}}var Q0=class{constructor(t,r,s){this.gatewayBaseUrl=t;this.dexContractBasePath=r;this.httpClient=s}async getPoolData(t,r,s){ri(s);let f=Pe(t),c=Pe(r),g=Sr(f,c,!1),w=(await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,"/GetPoolData",{token0:g.token0,token1:g.token1,fee:s})).Data;return{bitmap:w.bitmap,fee:w.fee,feeGrowthGlobal0:hr(w.feeGrowthGlobal0),feeGrowthGlobal1:hr(w.feeGrowthGlobal1),grossPoolLiquidity:hr(w.grossPoolLiquidity),liquidity:hr(w.liquidity),maxLiquidityPerTick:hr(w.maxLiquidityPerTick),protocolFees:w.protocolFees,protocolFeesToken0:hr(w.protocolFeesToken0),protocolFeesToken1:hr(w.protocolFeesToken1),sqrtPrice:hr(w.sqrtPrice),tickSpacing:w.tickSpacing,token0:w.token0,token0ClassKey:w.token0ClassKey,token1:w.token1,token1ClassKey:w.token1ClassKey}}calculateTicksForPrice(t,r){$t(t,"price",!0),fa(r);let s=hr(t).toNumber();if(s===0)return-886800;if(s===1/0)return 886800;let f=Math.round(Math.log(s)/Math.log(1.0001)),c=Math.floor(f/r)*r;return Math.min(Math.max(c,-886800),886800)}calculatePriceForTicks(t){if(t===-886800)return hr("0");if(t===886800)return hr(1/0);{let r=Math.pow(1.0001,t);return hr(r)}}calculateSpotPrice(t,r,s){$t(s,"poolSqrtPrice");let f=Sr(t,r,!1),c=hr(s).pow(2);return f.zeroForOne?c:hr(1).div(c)}};import ce from"bignumber.js";var to=class{constructor(t,r,s,f,c,g){this.gatewayBaseUrl=t;this.dexContractBasePath=r;this.options=g;Nt(this,"httpClient");Nt(this,"bundlerService");Nt(this,"poolService");this.httpClient=c,this.bundlerService=s,this.poolService=f}async getUserPositions(t,r,s){let f=await this.sendUserPositionsRequest("/GetUserPositions",{user:t,limit:r,bookmark:s});return{bookmark:f.nextBookMark,positions:f.positions}}async getPosition(t,r){return await this.sendPositionRequest("/GetPositions",{owner:t,token0:Pe(r.token0ClassKey),token1:Pe(r.token1ClassKey),fee:r.fee,tickLower:r.tickLower,tickUpper:r.tickUpper})}async getPositionById(t,r){let f=(await this.getUserPositions(t)).positions.find(c=>c.positionId===r);if(f)return this.getPosition(t,f)}async addLiquidityByTicks(t){let r=t.walletAddress??this.options?.walletAddress;ii(r),ri(t.fee),na(t.tickLower,t.tickUpper),$t(t.amount0Desired,"amount0Desired"),$t(t.amount1Desired,"amount1Desired"),$t(t.amount0Min,"amount0Min",!0),$t(t.amount1Min,"amount1Min",!0);let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Desired,t.amount0Min],[t.amount1Desired,t.amount1Min]),g={token0:c.token0,token1:c.token1,fee:t.fee,owner:t.walletAddress,tickLower:t.tickLower,tickUpper:t.tickUpper,amount0Desired:ce(c.token0Attributes[0]).toFixed(),amount1Desired:ce(c.token1Attributes[0]).toFixed(),amount0Min:ce(c.token0Attributes[1]).toFixed(),amount1Min:ce(c.token1Attributes[1]).toFixed(),positionId:t.positionId},b=rr(c.token0,"$"),w=rr(c.token1,"$"),_=`$pool$${b}$${w}$${t.fee}`,S=`$userPosition$${t.walletAddress}`,A=`$tokenBalance$${b}$${t.walletAddress}`,q=`$tokenBalance$${w}$${t.walletAddress}`,R=`$tokenBalance$${b}$${_}`,D=`$tokenBalance$${w}$${_}`,bt=[_,S,A,q,R,D];return this.bundlerService.sendBundlerRequest("AddLiquidity",g,bt)}async addLiquidityByPrice(t){let r=t.walletAddress??this.options?.walletAddress;if(ii(r),ri(t.fee),fa(t.tickSpacing),$t(t.minPrice,"minPrice",!0),$t(t.maxPrice,"maxPrice"),$t(t.amount0Desired,"amount0Desired"),$t(t.amount1Desired,"amount1Desired"),$t(t.amount0Min,"amount0Min",!0),$t(t.amount1Min,"amount1Min",!0),ce(t.minPrice).isGreaterThan(ce(t.maxPrice)))throw new Error("Invalid price range: minPrice must be less than or equal to maxPrice");let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Desired,t.amount0Min],[t.amount1Desired,t.amount1Min]),g=this.poolService.calculateTicksForPrice(t.minPrice,t.tickSpacing),b=this.poolService.calculateTicksForPrice(t.maxPrice,t.tickSpacing),w=c.zeroForOne?g:b*-1,_=c.zeroForOne?b:g*-1,S={token0:c.token0,token1:c.token1,fee:t.fee,owner:t.walletAddress,tickLower:w,tickUpper:_,amount0Desired:c?.token0Attributes?.[0],amount1Desired:c?.token1Attributes?.[0],amount0Min:c?.token0Attributes?.[1],amount1Min:c?.token1Attributes?.[1],positionId:t.positionId},A=rr(c.token0,"$"),q=rr(c.token1,"$"),R=`$pool$${A}$${q}$${t.fee}`,D=`$userPosition$${t.walletAddress}`,bt=`$tokenBalance$${A}$${t.walletAddress}`,_t=`$tokenBalance$${q}$${t.walletAddress}`,gt=`$tokenBalance$${A}$${R}`,xt=`$tokenBalance$${q}$${R}`,Mt=[R,D,bt,_t,gt,xt];return this.bundlerService.sendBundlerRequest("AddLiquidity",S,Mt)}async removeLiquidity(t){let r=t.walletAddress??this.options?.walletAddress;ii(r),ri(t.fee),na(t.tickLower,t.tickUpper),$t(t.amount,"amount"),t.amount0Min!==void 0&&$t(t.amount0Min,"amount0Min",!0),t.amount1Min!==void 0&&$t(t.amount1Min,"amount1Min",!0);let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Min??0],[t.amount1Min??0]),g={token0:c.token0,token1:c.token1,fee:t.fee,tickLower:t.tickLower,tickUpper:t.tickUpper,amount:ce(t.amount).toFixed(),amount0Min:c?.token0Attributes?.[0]||"0",amount1Min:c?.token1Attributes?.[0]||"0",positionId:t.positionId},b=rr(c.token0,"$"),w=rr(c.token1,"$"),_=`$pool$${b}$${w}$${t.fee}`,S=`$userPosition$${t.walletAddress}`,A=`$tokenBalance$${b}$${t.walletAddress}`,q=`$tokenBalance$${w}$${t.walletAddress}`,R=`$tokenBalance$${b}$${_}`,D=`$tokenBalance$${w}$${_}`,bt=[_,S,A,q,R,D];return this.bundlerService.sendBundlerRequest("RemoveLiquidity",g,bt)}async collectPositionFees(t){let r=t.walletAddress??this.options?.walletAddress;ii(r),ri(t.fee),na(t.tickLower,t.tickUpper),$t(t.amount0Requested,"amount0Requested",!0),$t(t.amount1Requested,"amount1Requested",!0);let s=Pe(t.token0),f=Pe(t.token1),c=Sr(s,f,!0,[t.amount0Requested],[t.amount1Requested]),g={token0:c.token0,token1:c.token1,fee:t.fee,amount0Requested:ce(c.token0Attributes[0]).toFixed(),amount1Requested:ce(c.token1Attributes[0]).toFixed(),tickLower:t.tickLower,tickUpper:t.tickUpper,positionId:t.positionId},b=rr(c.token0,"$"),w=rr(c.token1,"$"),_=`$pool$${b}$${w}$${t.fee}`,S=`$userPosition$${t.walletAddress}`,A=`$tokenBalance$${b}$${t.walletAddress}`,q=`$tokenBalance$${w}$${t.walletAddress}`,R=`$tokenBalance$${b}$${_}`,D=`$tokenBalance$${w}$${_}`,bt=[_,S,A,q,R,D];return this.bundlerService.sendBundlerRequest("CollectPositionFees",g,bt)}calculateOptimalPositionSize(t,r,s,f,c,g){$t(t,"tokenAmount"),hu(r,s,f),oo(c,"tokenDecimals"),oo(g,"otherTokenDecimals");let b=ce(t),w=ce(r),_=ce(s),S=ce(f);S=S.isFinite()?S:ce(1e18);let A=b.times(10**(c-g)).times(w.sqrt()).times(S.sqrt()).div(S.sqrt().minus(w.sqrt())),R=ce(A).times(w.sqrt().minus(_.sqrt())).div(ce(10).pow(c-g));return ce.max(ce(R.toFixed(g,ce.ROUND_DOWN)),0)}async sendUserPositionsRequest(t,r){let s=await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,t,r);return{nextBookMark:s.Data.nextBookMark,positions:s.Data.positions.map(f=>({...f,liquidity:ce(f.liquidity)}))}}async sendPositionRequest(t,r){let s=await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,t,r);return{...s.Data,feeGrowthInside0Last:ce(s.Data.feeGrowthInside0Last),feeGrowthInside1Last:ce(s.Data.feeGrowthInside1Last),liquidity:ce(s.Data.liquidity),tokensOwed0:ce(s.Data.tokensOwed0),tokensOwed1:ce(s.Data.tokensOwed1)}}};import Di from"bignumber.js";var Hg=(s=>(s[s.PERCENT_00_05=500]="PERCENT_00_05",s[s.PERCENT_00_30=3e3]="PERCENT_00_30",s[s.PERCENT_01_00=1e4]="PERCENT_01_00",s))(Hg||{});var eo=class{constructor(t,r,s){this.gatewayBaseUrl=t;this.dexContractBasePath=r;Nt(this,"httpClient");this.httpClient=s??new af}async quoteExactInput(t,r,s,f){if($t(s,"amountIn"),f!==void 0)return await this.getSingleQuoteExactInput(t,r,f,s);let g=[500,3e3,1e4].map(async _=>{try{return await this.getSingleQuoteExactInput(t,r,_,s)}catch(S){if(S instanceof qt&&(S.code==="CONFLICT"||S.code==="OBJECT_NOT_FOUND"))return;throw S}}),w=(await Promise.all(g)).filter(_=>_!==void 0);if(w.length===0)throw qt.noPoolAvailableError(t,r);return w.reduce((_,S)=>S.outTokenAmount.isGreaterThan(_.outTokenAmount)?S:_)}async quoteExactOutput(t,r,s,f){if($t(s,"amountOut"),f!==void 0)return await this.getSingleQuoteExactOutput(t,r,s,f);let g=[500,3e3,1e4].map(async _=>{try{return await this.getSingleQuoteExactOutput(t,r,s,_)}catch(S){if(S instanceof qt&&(S.code==="CONFLICT"||S.code==="OBJECT_NOT_FOUND"))return;throw S}}),w=(await Promise.all(g)).filter(_=>_!==void 0);if(w.length===0)throw qt.noPoolAvailableError(t,r);return w.reduce((_,S)=>S.inTokenAmount.isLessThan(_.inTokenAmount)?S:_)}async getSingleQuote(t,r,s,f,c){let g=Pe(t),b=Pe(r),w=Sr(g,b,!1),_=c?Di(f).toFixed():Di(f).multipliedBy(-1).toFixed(),S=await this.sendQuoteRequest("/QuoteExactAmount",{...w,fee:s,amount:_}),A=w.zeroForOne?S.amount0:S.amount1,q=w.zeroForOne?S.amount1:S.amount0,R=S.currentSqrtPrice.pow(2),D=S.newSqrtPrice.pow(2);w.zeroForOne||(R=Di(1).dividedBy(R),D=Di(1).dividedBy(D));let bt=D.minus(R).dividedBy(R);return{amount0:S.amount0,amount1:S.amount1,currentPoolSqrtPrice:S.currentSqrtPrice,newPoolSqrtPrice:S.newSqrtPrice,currentPrice:R,newPrice:D,inTokenAmount:A.abs(),outTokenAmount:q.abs(),priceImpact:bt,feeTier:s}}async getSingleQuoteExactInput(t,r,s,f){return this.getSingleQuote(t,r,s,f,!0)}async getSingleQuoteExactOutput(t,r,s,f){return this.getSingleQuote(t,r,f,s,!1)}async sendQuoteRequest(t,r){let f=await this.httpClient.sendPostRequest(this.gatewayBaseUrl,this.dexContractBasePath,t,r);return{amount0:Di(f.Data.amount0),amount1:Di(f.Data.amount1),currentSqrtPrice:Di(f.Data.currentSqrtPrice),newSqrtPrice:Di(f.Data.newSqrtPrice)}}};import of from"bignumber.js";var H7="0.000000000000000000094212147",G7="18446050999999999999",ro=class{constructor(t,r){this.bundlerService=t;this.options=r}async swap(t,r,s,f,c){c=c??this.options?.walletAddress,ii(c),ri(s),"exactIn"in f?($t(f.exactIn,"exactIn"),f.amountOutMinimum!==void 0&&$t(f.amountOutMinimum,"amountOutMinimum",!0)):($t(f.exactOut,"exactOut"),f.amountInMaximum!==void 0&&$t(f.amountInMaximum,"amountInMaximum"));let g=Sr(t,r,!1),b=rr(t)===rr(g.token0),w="exactIn"in f?of(f.exactIn).toFixed():of(f.exactOut).multipliedBy(-1).toFixed(),_="exactIn"in f?f.amountOutMinimum?.toString()?of(f.amountOutMinimum).multipliedBy(-1).toFixed():void 0:of(f.exactOut??0).multipliedBy(-1).toFixed(),S="exactIn"in f?of(f.exactIn).toFixed():f.amountInMaximum?of(f.amountInMaximum).toFixed():void 0,A={token0:Pe(g.token0),token1:Pe(g.token1),fee:s,amount:w,zeroForOne:b,sqrtPriceLimit:g.zeroForOne?H7:G7,recipient:c,amountOutMinimum:_,amountInMaximum:S},q=rr(g.token0,"$"),R=rr(g.token1,"$"),D=`$pool$${q}$${R}$${s}`,bt=`$tokenBalance$${q}$${c}`,_t=`$tokenBalance$${R}$${c}`,gt=`$tokenBalance$${q}$${D}`,xt=`$tokenBalance$${R}$${D}`,Mt=[D,bt,_t,gt,xt];return this.bundlerService.sendBundlerRequest("Swap",A,Mt)}};var ou=class{constructor(t){Nt(this,"gatewayBaseUrl");Nt(this,"dexContractBasePath");Nt(this,"tokenContractBasePath");Nt(this,"bundlerBaseUrl");Nt(this,"bundlingAPIBasePath");Nt(this,"dexBackendBaseUrl");Nt(this,"transactionWaitTimeoutMs");Nt(this,"signer");Nt(this,"quoting");Nt(this,"positions");Nt(this,"swaps");Nt(this,"assets");Nt(this,"httpClient");Nt(this,"httpRequestor");Nt(this,"bundler");Nt(this,"pools");this.gatewayBaseUrl=t?.gatewayBaseUrl?.replace(/\/$/,"")??"https://gateway-mainnet.galachain.com",this.dexContractBasePath=t?.dexContractBasePath??"/api/asset/dexv3-contract",this.tokenContractBasePath=t?.tokenContractBasePath??"/api/asset/token-contract",this.bundlerBaseUrl=t?.bundlerBaseUrl??"https://bundle-backend-prod1.defi.gala.com",this.bundlingAPIBasePath=t?.bundlingAPIBasePath??"/bundle",this.dexBackendBaseUrl=t?.dexBackendBaseUrl??"https://dex-backend-prod1.defi.gala.com",this.signer=t?.signer,this.transactionWaitTimeoutMs=t?.transactionWaitTimeoutMs??3e5,this.httpRequestor=t?.httpRequestor??fetch.bind(globalThis),this.httpClient=new af(this.httpRequestor),this.bundler=new Y0(this.bundlerBaseUrl,this.bundlingAPIBasePath,this.transactionWaitTimeoutMs,this.signer,this.httpClient),this.pools=new Q0(this.gatewayBaseUrl,this.dexContractBasePath,this.httpClient),this.quoting=new eo(this.gatewayBaseUrl,this.dexContractBasePath,this.httpClient),this.positions=new to(this.gatewayBaseUrl,this.dexContractBasePath,this.bundler,this.pools,this.httpClient,{walletAddress:t?.walletAddress}),this.swaps=new ro(this.bundler,{walletAddress:t?.walletAddress}),this.assets=new aa(this.dexBackendBaseUrl,this.httpClient)}};Nt(ou,"events",cn.instance);import{signatures as Gg}from"@gala-chain/api";import{calculatePersonalSignPrefix as Z7}from"@gala-chain/connect";var Zg=class{constructor(t){Nt(this,"keyBuffer");this.keyBuffer=Gg.normalizePrivateKey(t)}async signObject(t,r){let s=Gg.getSignature(r,this.keyBuffer);return{...r,signature:s}}},Wg=class i{constructor(t){this.walletAddress=t}async signObject(t,r){let s=global.gala;if(!s)throw new qt("Gala wallet is not available. Please ensure the Gala wallet is connected.","GALA_WALLET_NOT_AVAILABLE");let f={name:"ethereum",chainId:1},c=i.generateEIP712Types(t,r),g={domain:f,types:c,Primary_type:t,message:{...r,prefix:Z7(r)}};await s.setAddress(this.walletAddress);let b=await s.request({method:"eth_signTypedData",params:[JSON.stringify(g),this.walletAddress]});return{...r,domain:f,types:c,signature:b}}static generateEIP712Types(t,r){let s={};s[t]=[];function f(c,g,b,w=!1){if(g!==void 0)if(Array.isArray(g)){let _=f(c,g[0],b,!0);w||s[b].push({name:c,type:(_??c)+"[]"})}else if(typeof g=="object"&&g!==null){if(s[c])throw new qt("Name collisions not yet supported in EIP712 type generation","EIP712_NAME_COLLISION",{name:c});s[c]=[],Object.entries(g).forEach(([_,S])=>{f(_,S,c)}),w||s[b].push({name:c,type:c})}else{let _;switch(typeof g){case"string":_="string";break;case"number":_="int256";break;case"boolean":_="bool";break;default:throw new qt(`Unsupported type for EIP712 signing: ${typeof g}`,"EIP712_UNSUPPORTED_TYPE",{type:typeof g,value:g})}if(w)return _;s[b].push({name:c,type:_})}}return Object.entries(r).forEach(([c,g])=>{f(c,g,t)}),s}};export{aa as Assets,Y0 as Bundler,J0 as EventSocketClient,cn as Events,Hg as FEE_TIER,ou as GSwap,qt as GSwapSDKError,Wg as GalaWalletSigner,X0 as PendingTransaction,Q0 as Pools,to as Positions,Zg as PrivateKeySigner,eo as Quoting,ro as Swaps,au as TradeEventEmitter,$7 as compareTokens,Sr as getTokenOrdering,Pe as parseTokenClassKey,rr as stringifyTokenClassKey,ri as validateFee,$t as validateNumericAmount,hu as validatePriceValues,na as validateTickRange,fa as validateTickSpacing,oo as validateTokenDecimals,ii as validateWalletAddress};
9
9
  /*! Bundled license information:
10
10
 
11
11
  ieee754/index.js: