clone

nostr relays on demand at <name>.bind.ws: claim one with your key, pay in zaps past the free allowance

main
bindws / src / gen / signer.ts
signer.ts 108.3K
1// Generated by scripts/build/build-signer.mjs from scripts/build/signer-entry.js. Do not edit; run npm run build:signer.2export const SIGNER_HASH = "91778b87037e";3export const SIGNER_JS = "\"use strict\";(()=>{var Bo=Object.defineProperty;var Ao=(t,e,n)=>e in t?Bo(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var f=(t,e,n)=>Ao(t,typeof e!=\"symbol\"?e+\"\":e,n);function ze(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name===\"Uint8Array\"}function fe(t,e=\"\"){if(!Number.isSafeInteger(t)||t<0){let n=e&&`\"${e}\" `;throw new Error(`${n}expected integer >= 0, got ${t}`)}}function P(t,e,n=\"\"){let r=ze(t),o=t?.length,s=e!==void 0;if(!r||s&&o!==e){let i=n&&`\"${n}\" `,c=s?` of length ${e}`:\"\",a=r?`length=${o}`:`type=${typeof t}`;throw new Error(i+\"expected Uint8Array\"+c+\", got \"+a)}return t}function nt(t){if(typeof t!=\"function\"||typeof t.create!=\"function\")throw new Error(\"Hash must wrapped by utils.createHasher\");fe(t.outputLen),fe(t.blockLen)}function Ce(t,e=!0){if(t.destroyed)throw new Error(\"Hash instance has been destroyed\");if(e&&t.finished)throw new Error(\"Hash#digest() has already been called\")}function Un(t,e){P(t,void 0,\"digestInto() output\");let n=e.outputLen;if(t.length<n)throw new Error('\"digestInto() output\" expected to be of length >='+n)}function Te(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function rt(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function ie(t,e){return t<<32-e|t>>>e}var Pn=typeof Uint8Array.from([]).toHex==\"function\"&&typeof Uint8Array.fromHex==\"function\",To=Array.from({length:256},(t,e)=>e.toString(16).padStart(2,\"0\"));function He(t){if(P(t),Pn)return t.toHex();let e=\"\";for(let n=0;n<t.length;n++)e+=To[t[n]];return e}var he={_0:48,_9:57,A:65,F:70,a:97,f:102};function Cn(t){if(t>=he._0&&t<=he._9)return t-he._0;if(t>=he.A&&t<=he.F)return t-(he.A-10);if(t>=he.a&&t<=he.f)return t-(he.a-10)}function Re(t){if(typeof t!=\"string\")throw new Error(\"hex string expected, got \"+typeof t);if(Pn)return Uint8Array.fromHex(t);let e=t.length,n=e/2;if(e%2)throw new Error(\"hex string expected, got unpadded hex of length \"+e);let r=new Uint8Array(n);for(let o=0,s=0;o<n;o++,s+=2){let i=Cn(t.charCodeAt(s)),c=Cn(t.charCodeAt(s+1));if(i===void 0||c===void 0){let a=t[s]+t[s+1];throw new Error('hex string expected, got non-hex character \"'+a+'\" at index '+s)}r[o]=i*16+c}return r}function oe(...t){let e=0;for(let r=0;r<t.length;r++){let o=t[r];P(o),e+=o.length}let n=new Uint8Array(e);for(let r=0,o=0;r<t.length;r++){let s=t[r];n.set(s,o),o+=s.length}return n}function Nn(t,e={}){let n=(o,s)=>t(s).update(o).digest(),r=t(void 0);return n.outputLen=r.outputLen,n.blockLen=r.blockLen,n.create=o=>t(o),Object.assign(n,e),Object.freeze(n)}function Le(t=32){let e=typeof globalThis==\"object\"?globalThis.crypto:null;if(typeof e?.getRandomValues!=\"function\")throw new Error(\"crypto.getRandomValues must be defined\");return e.getRandomValues(new Uint8Array(t))}var qn=t=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,t])});function Dn(t,e,n){return t&e^~t&n}function $n(t,e,n){return t&e^t&n^e&n}var ot=class{constructor(e,n,r,o){f(this,\"blockLen\");f(this,\"outputLen\");f(this,\"padOffset\");f(this,\"isLE\");f(this,\"buffer\");f(this,\"view\");f(this,\"finished\",!1);f(this,\"length\",0);f(this,\"pos\",0);f(this,\"destroyed\",!1);this.blockLen=e,this.outputLen=n,this.padOffset=r,this.isLE=o,this.buffer=new Uint8Array(e),this.view=rt(this.buffer)}update(e){Ce(this),P(e);let{view:n,buffer:r,blockLen:o}=this,s=e.length;for(let i=0;i<s;){let c=Math.min(o-this.pos,s-i);if(c===o){let a=rt(e);for(;o<=s-i;i+=o)this.process(a,i);continue}r.set(e.subarray(i,i+c),this.pos),this.pos+=c,i+=c,this.pos===o&&(this.process(n,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){Ce(this),Un(e,this),this.finished=!0;let{buffer:n,view:r,blockLen:o,isLE:s}=this,{pos:i}=this;n[i++]=128,Te(this.buffer.subarray(i)),this.padOffset>o-i&&(this.process(r,0),i=0);for(let d=i;d<o;d++)n[d]=0;r.setBigUint64(o-8,BigInt(this.length*8),s),this.process(r,0);let c=rt(e),a=this.outputLen;if(a%4)throw new Error(\"_sha2: outputLen must be aligned to 32bit\");let l=a/4,u=this.get();if(l>u.length)throw new Error(\"_sha2: outputLen bigger than state\");for(let d=0;d<l;d++)c.setUint32(4*d,u[d],s)}digest(){let{buffer:e,outputLen:n}=this;this.digestInto(e);let r=e.slice(0,n);return this.destroy(),r}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:n,buffer:r,length:o,finished:s,destroyed:i,pos:c}=this;return e.destroyed=i,e.finished=s,e.length=o,e.pos=c,o%n&&e.buffer.set(r),e}clone(){return this._cloneInto()}},de=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var Ho=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),ve=new Uint32Array(64),It=class extends ot{constructor(e){super(64,e,8,!1)}get(){let{A:e,B:n,C:r,D:o,E:s,F:i,G:c,H:a}=this;return[e,n,r,o,s,i,c,a]}set(e,n,r,o,s,i,c,a){this.A=e|0,this.B=n|0,this.C=r|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=c|0,this.H=a|0}process(e,n){for(let d=0;d<16;d++,n+=4)ve[d]=e.getUint32(n,!1);for(let d=16;d<64;d++){let h=ve[d-15],b=ve[d-2],x=ie(h,7)^ie(h,18)^h>>>3,w=ie(b,17)^ie(b,19)^b>>>10;ve[d]=w+ve[d-7]+x+ve[d-16]|0}let{A:r,B:o,C:s,D:i,E:c,F:a,G:l,H:u}=this;for(let d=0;d<64;d++){let h=ie(c,6)^ie(c,11)^ie(c,25),b=u+h+Dn(c,a,l)+Ho[d]+ve[d]|0,w=(ie(r,2)^ie(r,13)^ie(r,22))+$n(r,o,s)|0;u=l,l=a,a=c,c=i+b|0,i=s,s=o,o=r,r=b+w|0}r=r+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,c=c+this.E|0,a=a+this.F|0,l=l+this.G|0,u=u+this.H|0,this.set(r,o,s,i,c,a,l,u)}roundClean(){Te(ve)}destroy(){this.set(0,0,0,0,0,0,0,0),Te(this.buffer)}},Ct=class extends It{constructor(){super(32);f(this,\"A\",de[0]|0);f(this,\"B\",de[1]|0);f(this,\"C\",de[2]|0);f(this,\"D\",de[3]|0);f(this,\"E\",de[4]|0);f(this,\"F\",de[5]|0);f(this,\"G\",de[6]|0);f(this,\"H\",de[7]|0)}};var st=Nn(()=>new Ct,qn(1));var Pt=BigInt(0),Ut=BigInt(1);function Ze(t,e=\"\"){if(typeof t!=\"boolean\"){let n=e&&`\"${e}\" `;throw new Error(n+\"expected boolean, got type=\"+typeof t)}return t}function Mn(t){if(typeof t==\"bigint\"){if(!it(t))throw new Error(\"positive bigint expected, got \"+t)}else fe(t);return t}function Fe(t){let e=Mn(t).toString(16);return e.length&1?\"0\"+e:e}function Vn(t){if(typeof t!=\"string\")throw new Error(\"hex string expected, got \"+typeof t);return t===\"\"?Pt:BigInt(\"0x\"+t)}function Se(t){return Vn(He(t))}function Nt(t){return Vn(He(Ro(P(t)).reverse()))}function ct(t,e){fe(e),t=Mn(t);let n=Re(t.toString(16).padStart(e*2,\"0\"));if(n.length!==e)throw new Error(\"number too large\");return n}function qt(t,e){return ct(t,e).reverse()}function Ro(t){return Uint8Array.from(t)}function Wn(t){return Uint8Array.from(t,(e,n)=>{let r=e.charCodeAt(0);if(e.length!==1||r>127)throw new Error(`string contains non-ASCII character \"${t[n]}\" with code ${r} at position ${n}`);return r})}var it=t=>typeof t==\"bigint\"&&Pt<=t;function Lo(t,e,n){return it(t)&&it(e)&&it(n)&&e<=t&&t<n}function Kn(t,e,n,r){if(!Lo(e,n,r))throw new Error(\"expected valid \"+t+\": \"+n+\" <= n < \"+r+\", got \"+e)}function Dt(t){let e;for(e=0;t>Pt;t>>=Ut,e+=1);return e}var Ye=t=>(Ut<<BigInt(t))-Ut;function jn(t,e,n){if(fe(t,\"hashLen\"),fe(e,\"qByteLen\"),typeof n!=\"function\")throw new Error(\"hmacFn must be a function\");let r=E=>new Uint8Array(E),o=Uint8Array.of(),s=Uint8Array.of(0),i=Uint8Array.of(1),c=1e3,a=r(t),l=r(t),u=0,d=()=>{a.fill(1),l.fill(0),u=0},h=(...E)=>n(l,oe(a,...E)),b=(E=o)=>{l=h(s,E),a=h(),E.length!==0&&(l=h(i,E),a=h())},x=()=>{if(u++>=c)throw new Error(\"drbg: tried max amount of iterations\");let E=0,B=[];for(;E<e;){a=h();let k=a.slice();B.push(k),E+=a.length}return oe(...B)};return(E,B)=>{d(),b(E);let k;for(;!(k=B(x()));)b();return d(),k}}function Je(t,e={},n={}){if(!t||typeof t!=\"object\")throw new Error(\"expected valid options object\");function r(s,i,c){let a=t[s];if(c&&a===void 0)return;let l=typeof a;if(l!==i||a===null)throw new Error(`param \"${s}\" is invalid: expected ${i}, got ${l}`)}let o=(s,i)=>Object.entries(s).forEach(([c,a])=>r(c,a,i));o(e,!1),o(n,!0)}function $t(t){let e=new WeakMap;return(n,...r)=>{let o=e.get(n);if(o!==void 0)return o;let s=t(n,...r);return e.set(n,s),s}}var re=BigInt(0),ee=BigInt(1),Oe=BigInt(2),Zn=BigInt(3),Fn=BigInt(4),Yn=BigInt(5),Oo=BigInt(7),Jn=BigInt(8),ko=BigInt(9),Xn=BigInt(16);function ce(t,e){let n=t%e;return n>=re?n:e+n}function se(t,e,n){let r=t;for(;e-- >re;)r*=r,r%=n;return r}function Gn(t,e){if(t===re)throw new Error(\"invert: expected non-zero number\");if(e<=re)throw new Error(\"invert: expected positive modulus, got \"+e);let n=ce(t,e),r=e,o=re,s=ee,i=ee,c=re;for(;n!==re;){let l=r/n,u=r%n,d=o-i*l,h=s-c*l;r=n,n=u,o=i,s=c,i=d,c=h}if(r!==ee)throw new Error(\"invert: does not exist\");return ce(o,e)}function Vt(t,e,n){if(!t.eql(t.sqr(e),n))throw new Error(\"Cannot find square root\")}function Qn(t,e){let n=(t.ORDER+ee)/Fn,r=t.pow(e,n);return Vt(t,r,e),r}function _o(t,e){let n=(t.ORDER-Yn)/Jn,r=t.mul(e,Oe),o=t.pow(r,n),s=t.mul(e,o),i=t.mul(t.mul(s,Oe),o),c=t.mul(s,t.sub(i,t.ONE));return Vt(t,c,e),c}function Io(t){let e=Ue(t),n=er(t),r=n(e,e.neg(e.ONE)),o=n(e,r),s=n(e,e.neg(r)),i=(t+Oo)/Xn;return(c,a)=>{let l=c.pow(a,i),u=c.mul(l,r),d=c.mul(l,o),h=c.mul(l,s),b=c.eql(c.sqr(u),a),x=c.eql(c.sqr(d),a);l=c.cmov(l,u,b),u=c.cmov(h,d,x);let w=c.eql(c.sqr(u),a),E=c.cmov(l,u,w);return Vt(c,E,a),E}}function er(t){if(t<Zn)throw new Error(\"sqrt is not defined for small field\");let e=t-ee,n=0;for(;e%Oe===re;)e/=Oe,n++;let r=Oe,o=Ue(t);for(;zn(o,r)===1;)if(r++>1e3)throw new Error(\"Cannot find square root: probably non-prime P\");if(n===1)return Qn;let s=o.pow(r,e),i=(e+ee)/Oe;return function(a,l){if(a.is0(l))return l;if(zn(a,l)!==1)throw new Error(\"Cannot find square root\");let u=n,d=a.mul(a.ONE,s),h=a.pow(l,e),b=a.pow(l,i);for(;!a.eql(h,a.ONE);){if(a.is0(h))return a.ZERO;let x=1,w=a.sqr(h);for(;!a.eql(w,a.ONE);)if(x++,w=a.sqr(w),x===u)throw new Error(\"Cannot find square root\");let E=ee<<BigInt(u-x-1),B=a.pow(d,E);u=x,d=a.sqr(B),h=a.mul(h,d),b=a.mul(b,B)}return b}}function Co(t){return t%Fn===Zn?Qn:t%Jn===Yn?_o:t%Xn===ko?Io(t):er(t)}var Uo=[\"create\",\"isValid\",\"is0\",\"neg\",\"inv\",\"sqrt\",\"sqr\",\"eql\",\"add\",\"sub\",\"mul\",\"pow\",\"div\",\"addN\",\"subN\",\"mulN\",\"sqrN\"];function Wt(t){let e={ORDER:\"bigint\",BYTES:\"number\",BITS:\"number\"},n=Uo.reduce((r,o)=>(r[o]=\"function\",r),e);return Je(t,n),t}function Po(t,e,n){if(n<re)throw new Error(\"invalid exponent, negatives unsupported\");if(n===re)return t.ONE;if(n===ee)return e;let r=t.ONE,o=e;for(;n>re;)n&ee&&(r=t.mul(r,o)),o=t.sqr(o),n>>=ee;return r}function at(t,e,n=!1){let r=new Array(e.length).fill(n?t.ZERO:void 0),o=e.reduce((i,c,a)=>t.is0(c)?i:(r[a]=i,t.mul(i,c)),t.ONE),s=t.inv(o);return e.reduceRight((i,c,a)=>t.is0(c)?i:(r[a]=t.mul(i,r[a]),t.mul(i,c)),s),r}function zn(t,e){let n=(t.ORDER-ee)/Oe,r=t.pow(e,n),o=t.eql(r,t.ONE),s=t.eql(r,t.ZERO),i=t.eql(r,t.neg(t.ONE));if(!o&&!s&&!i)throw new Error(\"invalid Legendre symbol result\");return o?1:s?0:-1}function No(t,e){e!==void 0&&fe(e);let n=e!==void 0?e:t.toString(2).length,r=Math.ceil(n/8);return{nBitLength:n,nByteLength:r}}var Mt=class{constructor(e,n={}){f(this,\"ORDER\");f(this,\"BITS\");f(this,\"BYTES\");f(this,\"isLE\");f(this,\"ZERO\",re);f(this,\"ONE\",ee);f(this,\"_lengths\");f(this,\"_sqrt\");f(this,\"_mod\");if(e<=re)throw new Error(\"invalid field: expected ORDER > 0, got \"+e);let r;this.isLE=!1,n!=null&&typeof n==\"object\"&&(typeof n.BITS==\"number\"&&(r=n.BITS),typeof n.sqrt==\"function\"&&(this.sqrt=n.sqrt),typeof n.isLE==\"boolean\"&&(this.isLE=n.isLE),n.allowedLengths&&(this._lengths=n.allowedLengths?.slice()),typeof n.modFromBytes==\"boolean\"&&(this._mod=n.modFromBytes));let{nBitLength:o,nByteLength:s}=No(e,r);if(s>2048)throw new Error(\"invalid field: expected ORDER of <= 2048 bytes\");this.ORDER=e,this.BITS=o,this.BYTES=s,this._sqrt=void 0,Object.preventExtensions(this)}create(e){return ce(e,this.ORDER)}isValid(e){if(typeof e!=\"bigint\")throw new Error(\"invalid field element: expected bigint, got \"+typeof e);return re<=e&&e<this.ORDER}is0(e){return e===re}isValidNot0(e){return!this.is0(e)&&this.isValid(e)}isOdd(e){return(e&ee)===ee}neg(e){return ce(-e,this.ORDER)}eql(e,n){return e===n}sqr(e){return ce(e*e,this.ORDER)}add(e,n){return ce(e+n,this.ORDER)}sub(e,n){return ce(e-n,this.ORDER)}mul(e,n){return ce(e*n,this.ORDER)}pow(e,n){return Po(this,e,n)}div(e,n){return ce(e*Gn(n,this.ORDER),this.ORDER)}sqrN(e){return e*e}addN(e,n){return e+n}subN(e,n){return e-n}mulN(e,n){return e*n}inv(e){return Gn(e,this.ORDER)}sqrt(e){return this._sqrt||(this._sqrt=Co(this.ORDER)),this._sqrt(this,e)}toBytes(e){return this.isLE?qt(e,this.BYTES):ct(e,this.BYTES)}fromBytes(e,n=!1){P(e);let{_lengths:r,BYTES:o,isLE:s,ORDER:i,_mod:c}=this;if(r){if(!r.includes(e.length)||e.length>o)throw new Error(\"Field.fromBytes: expected \"+r+\" bytes, got \"+e.length);let l=new Uint8Array(o);l.set(e,s?0:l.length-e.length),e=l}if(e.length!==o)throw new Error(\"Field.fromBytes: expected \"+o+\" bytes, got \"+e.length);let a=s?Nt(e):Se(e);if(c&&(a=ce(a,i)),!n&&!this.isValid(a))throw new Error(\"invalid field element: outside of range 0..ORDER\");return a}invertBatch(e){return at(this,e)}cmov(e,n,r){return r?n:e}};function Ue(t,e={}){return new Mt(t,e)}function tr(t){if(typeof t!=\"bigint\")throw new Error(\"field order must be bigint\");let e=t.toString(2).length;return Math.ceil(e/8)}function Kt(t){let e=tr(t);return e+Math.ceil(e/2)}function ft(t,e,n=!1){P(t);let r=t.length,o=tr(e),s=Kt(e);if(r<16||r<s||r>1024)throw new Error(\"expected \"+s+\"-1024 bytes of input, got \"+r);let i=n?Nt(t):Se(t),c=ce(i,e-ee)+ee;return n?qt(c,o):ct(c,o)}var Pe=BigInt(0),ke=BigInt(1);function Xe(t,e){let n=e.negate();return t?n:e}function Zt(t,e){let n=at(t.Fp,e.map(r=>r.Z));return e.map((r,o)=>t.fromAffine(r.toAffine(n[o])))}function sr(t,e){if(!Number.isSafeInteger(t)||t<=0||t>e)throw new Error(\"invalid window size, expected [1..\"+e+\"], got W=\"+t)}function jt(t,e){sr(t,e);let n=Math.ceil(e/t)+1,r=2**(t-1),o=2**t,s=Ye(t),i=BigInt(t);return{windows:n,windowSize:r,mask:s,maxNumber:o,shiftBy:i}}function nr(t,e,n){let{windowSize:r,mask:o,maxNumber:s,shiftBy:i}=n,c=Number(t&o),a=t>>i;c>r&&(c-=s,a+=ke);let l=e*r,u=l+Math.abs(c)-1,d=c===0,h=c<0,b=e%2!==0;return{nextN:a,offset:u,isZero:d,isNeg:h,isNegF:b,offsetF:l}}var Gt=new WeakMap,ir=new WeakMap;function zt(t){return ir.get(t)||1}function rr(t){if(t!==Pe)throw new Error(\"invalid wNAF\")}var lt=class{constructor(e,n){f(this,\"BASE\");f(this,\"ZERO\");f(this,\"Fn\");f(this,\"bits\");this.BASE=e.BASE,this.ZERO=e.ZERO,this.Fn=e.Fn,this.bits=n}_unsafeLadder(e,n,r=this.ZERO){let o=e;for(;n>Pe;)n&ke&&(r=r.add(o)),o=o.double(),n>>=ke;return r}precomputeWindow(e,n){let{windows:r,windowSize:o}=jt(n,this.bits),s=[],i=e,c=i;for(let a=0;a<r;a++){c=i,s.push(c);for(let l=1;l<o;l++)c=c.add(i),s.push(c);i=c.double()}return s}wNAF(e,n,r){if(!this.Fn.isValid(r))throw new Error(\"invalid scalar\");let o=this.ZERO,s=this.BASE,i=jt(e,this.bits);for(let c=0;c<i.windows;c++){let{nextN:a,offset:l,isZero:u,isNeg:d,isNegF:h,offsetF:b}=nr(r,c,i);r=a,u?s=s.add(Xe(h,n[b])):o=o.add(Xe(d,n[l]))}return rr(r),{p:o,f:s}}wNAFUnsafe(e,n,r,o=this.ZERO){let s=jt(e,this.bits);for(let i=0;i<s.windows&&r!==Pe;i++){let{nextN:c,offset:a,isZero:l,isNeg:u}=nr(r,i,s);if(r=c,!l){let d=n[a];o=o.add(u?d.negate():d)}}return rr(r),o}getPrecomputes(e,n,r){let o=Gt.get(n);return o||(o=this.precomputeWindow(n,e),e!==1&&(typeof r==\"function\"&&(o=r(o)),Gt.set(n,o))),o}cached(e,n,r){let o=zt(e);return this.wNAF(o,this.getPrecomputes(o,e,r),n)}unsafe(e,n,r,o){let s=zt(e);return s===1?this._unsafeLadder(e,n,o):this.wNAFUnsafe(s,this.getPrecomputes(s,e,r),n,o)}createCache(e,n){sr(n,this.bits),ir.set(e,n),Gt.delete(e)}hasCache(e){return zt(e)!==1}};function cr(t,e,n,r){let o=e,s=t.ZERO,i=t.ZERO;for(;n>Pe||r>Pe;)n&ke&&(s=s.add(o)),r&ke&&(i=i.add(o)),o=o.double(),n>>=ke,r>>=ke;return{p1:s,p2:i}}function or(t,e,n){if(e){if(e.ORDER!==t)throw new Error(\"Field.ORDER must match order: Fp == p, Fn == n\");return Wt(e),e}else return Ue(t,{isLE:n})}function ar(t,e,n={},r){if(r===void 0&&(r=t===\"edwards\"),!e||typeof e!=\"object\")throw new Error(`expected valid ${t} CURVE object`);for(let a of[\"p\",\"n\",\"h\"]){let l=e[a];if(!(typeof l==\"bigint\"&&l>Pe))throw new Error(`CURVE.${a} must be positive bigint`)}let o=or(e.p,n.Fp,r),s=or(e.n,n.Fn,r),c=[\"Gx\",\"Gy\",\"a\",t===\"weierstrass\"?\"b\":\"d\"];for(let a of c)if(!o.isValid(e[a]))throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`);return e=Object.freeze(Object.assign({},e)),{CURVE:e,Fp:o,Fn:s}}function ut(t,e){return function(r){let o=t(r);return{secretKey:o,publicKey:e(o)}}}var ht=class{constructor(e,n){f(this,\"oHash\");f(this,\"iHash\");f(this,\"blockLen\");f(this,\"outputLen\");f(this,\"finished\",!1);f(this,\"destroyed\",!1);if(nt(e),P(n,void 0,\"key\"),this.iHash=e.create(),typeof this.iHash.update!=\"function\")throw new Error(\"Expected instance of class which extends utils.Hash\");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let r=this.blockLen,o=new Uint8Array(r);o.set(n.length>r?e.create().update(n).digest():n);for(let s=0;s<o.length;s++)o[s]^=54;this.iHash.update(o),this.oHash=e.create();for(let s=0;s<o.length;s++)o[s]^=106;this.oHash.update(o),Te(o)}update(e){return Ce(this),this.iHash.update(e),this}digestInto(e){Ce(this),P(e,this.outputLen,\"output\"),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){let e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));let{oHash:n,iHash:r,finished:o,destroyed:s,blockLen:i,outputLen:c}=this;return e=e,e.finished=o,e.destroyed=s,e.blockLen=i,e.outputLen=c,e.oHash=n._cloneInto(e.oHash),e.iHash=r._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},Ft=(t,e,n)=>new ht(t,e).update(n).digest();Ft.create=(t,e)=>new ht(t,e);var fr=(t,e)=>(t+(t>=0?e:-e)/lr)/e;function qo(t,e,n){let[[r,o],[s,i]]=e,c=fr(i*t,n),a=fr(-o*t,n),l=t-c*r-a*s,u=-c*o-a*i,d=l<be,h=u<be;d&&(l=-l),h&&(u=-u);let b=Ye(Math.ceil(Dt(n)/2))+Ne;if(l<be||l>=b||u<be||u>=b)throw new Error(\"splitScalar (endomorphism): failed, k=\"+t);return{k1neg:d,k1:l,k2neg:h,k2:u}}function Jt(t){if(![\"compact\",\"recovered\",\"der\"].includes(t))throw new Error('Signature format must be \"compact\", \"recovered\", or \"der\"');return t}function Yt(t,e){let n={};for(let r of Object.keys(e))n[r]=t[r]===void 0?e[r]:t[r];return Ze(n.lowS,\"lowS\"),Ze(n.prehash,\"prehash\"),n.format!==void 0&&Jt(n.format),n}var Xt=class extends Error{constructor(e=\"\"){super(e)}},Be={Err:Xt,_tlv:{encode:(t,e)=>{let{Err:n}=Be;if(t<0||t>256)throw new n(\"tlv.encode: wrong tag\");if(e.length&1)throw new n(\"tlv.encode: unpadded data\");let r=e.length/2,o=Fe(r);if(o.length/2&128)throw new n(\"tlv.encode: long form length too big\");let s=r>127?Fe(o.length/2|128):\"\";return Fe(t)+s+o+e},decode(t,e){let{Err:n}=Be,r=0;if(t<0||t>256)throw new n(\"tlv.encode: wrong tag\");if(e.length<2||e[r++]!==t)throw new n(\"tlv.decode: wrong tlv\");let o=e[r++],s=!!(o&128),i=0;if(!s)i=o;else{let a=o&127;if(!a)throw new n(\"tlv.decode(long): indefinite length not supported\");if(a>4)throw new n(\"tlv.decode(long): byte length is too big\");let l=e.subarray(r,r+a);if(l.length!==a)throw new n(\"tlv.decode: length bytes not complete\");if(l[0]===0)throw new n(\"tlv.decode(long): zero leftmost byte\");for(let u of l)i=i<<8|u;if(r+=a,i<128)throw new n(\"tlv.decode(long): not minimal encoding\")}let c=e.subarray(r,r+i);if(c.length!==i)throw new n(\"tlv.decode: wrong value length\");return{v:c,l:e.subarray(r+i)}}},_int:{encode(t){let{Err:e}=Be;if(t<be)throw new e(\"integer: negative integers are not allowed\");let n=Fe(t);if(Number.parseInt(n[0],16)&8&&(n=\"00\"+n),n.length&1)throw new e(\"unexpected DER parsing assertion: unpadded hex\");return n},decode(t){let{Err:e}=Be;if(t[0]&128)throw new e(\"invalid signature integer: negative\");if(t[0]===0&&!(t[1]&128))throw new e(\"invalid signature integer: unnecessary leading zero\");return Se(t)}},toSig(t){let{Err:e,_int:n,_tlv:r}=Be,o=P(t,void 0,\"signature\"),{v:s,l:i}=r.decode(48,o);if(i.length)throw new e(\"invalid signature: left bytes after parsing\");let{v:c,l:a}=r.decode(2,s),{v:l,l:u}=r.decode(2,a);if(u.length)throw new e(\"invalid signature: left bytes after parsing\");return{r:n.decode(c),s:n.decode(l)}},hexFromSig(t){let{_tlv:e,_int:n}=Be,r=e.encode(2,n.encode(t.r)),o=e.encode(2,n.encode(t.s)),s=r+o;return e.encode(48,s)}},be=BigInt(0),Ne=BigInt(1),lr=BigInt(2),dt=BigInt(3),Do=BigInt(4);function ur(t,e={}){let n=ar(\"weierstrass\",t,e),{Fp:r,Fn:o}=n,s=n.CURVE,{h:i,n:c}=s;Je(e,{},{allowInfinityPoint:\"boolean\",clearCofactor:\"function\",isTorsionFree:\"function\",fromBytes:\"function\",toBytes:\"function\",endo:\"object\"});let{endo:a}=e;if(a&&(!r.is0(s.a)||typeof a.beta!=\"bigint\"||!Array.isArray(a.basises)))throw new Error('invalid endo: expected \"beta\": bigint and \"basises\": array');let l=dr(r,o);function u(){if(!r.isOdd)throw new Error(\"compression is not supported: Field does not have .isOdd()\")}function d(L,y,p){let{x:m,y:v}=y.toAffine(),A=r.toBytes(m);if(Ze(p,\"isCompressed\"),p){u();let g=!r.isOdd(v);return oe(hr(g),A)}else return oe(Uint8Array.of(4),A,r.toBytes(v))}function h(L){P(L,void 0,\"Point\");let{publicKey:y,publicKeyUncompressed:p}=l,m=L.length,v=L[0],A=L.subarray(1);if(m===y&&(v===2||v===3)){let g=r.fromBytes(A);if(!r.isValid(g))throw new Error(\"bad point: is not on curve, wrong x\");let T=w(g),S;try{S=r.sqrt(T)}catch(V){let D=V instanceof Error?\": \"+V.message:\"\";throw new Error(\"bad point: is not on curve, sqrt error\"+D)}u();let H=r.isOdd(S);return(v&1)===1!==H&&(S=r.neg(S)),{x:g,y:S}}else if(m===p&&v===4){let g=r.BYTES,T=r.fromBytes(A.subarray(0,g)),S=r.fromBytes(A.subarray(g,g*2));if(!E(T,S))throw new Error(\"bad point: is not on curve\");return{x:T,y:S}}else throw new Error(`bad point: got length ${m}, expected compressed=${y} or uncompressed=${p}`)}let b=e.toBytes||d,x=e.fromBytes||h;function w(L){let y=r.sqr(L),p=r.mul(y,L);return r.add(r.add(p,r.mul(L,s.a)),s.b)}function E(L,y){let p=r.sqr(y),m=w(L);return r.eql(p,m)}if(!E(s.Gx,s.Gy))throw new Error(\"bad curve params: generator point\");let B=r.mul(r.pow(s.a,dt),Do),k=r.mul(r.sqr(s.b),BigInt(27));if(r.is0(r.add(B,k)))throw new Error(\"bad curve params: a or b\");function U(L,y,p=!1){if(!r.isValid(y)||p&&r.is0(y))throw new Error(`bad point coordinate ${L}`);return y}function N(L){if(!(L instanceof C))throw new Error(\"Weierstrass Point expected\")}function X(L){if(!a||!a.basises)throw new Error(\"no endo\");return qo(L,a.basises,o.ORDER)}let M=$t((L,y)=>{let{X:p,Y:m,Z:v}=L;if(r.eql(v,r.ONE))return{x:p,y:m};let A=L.is0();y==null&&(y=A?r.ONE:r.inv(v));let g=r.mul(p,y),T=r.mul(m,y),S=r.mul(v,y);if(A)return{x:r.ZERO,y:r.ZERO};if(!r.eql(S,r.ONE))throw new Error(\"invZ was invalid\");return{x:g,y:T}}),z=$t(L=>{if(L.is0()){if(e.allowInfinityPoint&&!r.is0(L.Y))return;throw new Error(\"bad point: ZERO\")}let{x:y,y:p}=L.toAffine();if(!r.isValid(y)||!r.isValid(p))throw new Error(\"bad point: x or y not field elements\");if(!E(y,p))throw new Error(\"bad point: equation left != right\");if(!L.isTorsionFree())throw new Error(\"bad point: not in prime-order subgroup\");return!0});function K(L,y,p,m,v){return p=new C(r.mul(p.X,L),p.Y,p.Z),y=Xe(m,y),p=Xe(v,p),y.add(p)}let O=class O{constructor(y,p,m){f(this,\"X\");f(this,\"Y\");f(this,\"Z\");this.X=U(\"x\",y),this.Y=U(\"y\",p,!0),this.Z=U(\"z\",m),Object.freeze(this)}static CURVE(){return s}static fromAffine(y){let{x:p,y:m}=y||{};if(!y||!r.isValid(p)||!r.isValid(m))throw new Error(\"invalid affine point\");if(y instanceof O)throw new Error(\"projective point not allowed\");return r.is0(p)&&r.is0(m)?O.ZERO:new O(p,m,r.ONE)}static fromBytes(y){let p=O.fromAffine(x(P(y,void 0,\"point\")));return p.assertValidity(),p}static fromHex(y){return O.fromBytes(Re(y))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(y=8,p=!0){return q.createCache(this,y),p||this.multiply(dt),this}assertValidity(){z(this)}hasEvenY(){let{y}=this.toAffine();if(!r.isOdd)throw new Error(\"Field doesn't support isOdd\");return!r.isOdd(y)}equals(y){N(y);let{X:p,Y:m,Z:v}=this,{X:A,Y:g,Z:T}=y,S=r.eql(r.mul(p,T),r.mul(A,v)),H=r.eql(r.mul(m,T),r.mul(g,v));return S&&H}negate(){return new O(this.X,r.neg(this.Y),this.Z)}double(){let{a:y,b:p}=s,m=r.mul(p,dt),{X:v,Y:A,Z:g}=this,T=r.ZERO,S=r.ZERO,H=r.ZERO,_=r.mul(v,v),V=r.mul(A,A),D=r.mul(g,g),I=r.mul(v,A);return I=r.add(I,I),H=r.mul(v,g),H=r.add(H,H),T=r.mul(y,H),S=r.mul(m,D),S=r.add(T,S),T=r.sub(V,S),S=r.add(V,S),S=r.mul(T,S),T=r.mul(I,T),H=r.mul(m,H),D=r.mul(y,D),I=r.sub(_,D),I=r.mul(y,I),I=r.add(I,H),H=r.add(_,_),_=r.add(H,_),_=r.add(_,D),_=r.mul(_,I),S=r.add(S,_),D=r.mul(A,g),D=r.add(D,D),_=r.mul(D,I),T=r.sub(T,_),H=r.mul(D,V),H=r.add(H,H),H=r.add(H,H),new O(T,S,H)}add(y){N(y);let{X:p,Y:m,Z:v}=this,{X:A,Y:g,Z:T}=y,S=r.ZERO,H=r.ZERO,_=r.ZERO,V=s.a,D=r.mul(s.b,dt),I=r.mul(p,A),W=r.mul(m,g),j=r.mul(v,T),Z=r.add(p,m),G=r.add(A,g);Z=r.mul(Z,G),G=r.add(I,W),Z=r.sub(Z,G),G=r.add(p,v);let ne=r.add(A,T);return G=r.mul(G,ne),ne=r.add(I,j),G=r.sub(G,ne),ne=r.add(m,v),S=r.add(g,T),ne=r.mul(ne,S),S=r.add(W,j),ne=r.sub(ne,S),_=r.mul(V,G),S=r.mul(D,j),_=r.add(S,_),S=r.sub(W,_),_=r.add(W,_),H=r.mul(S,_),W=r.add(I,I),W=r.add(W,I),j=r.mul(V,j),G=r.mul(D,G),W=r.add(W,j),j=r.sub(I,j),j=r.mul(V,j),G=r.add(G,j),I=r.mul(W,G),H=r.add(H,I),I=r.mul(ne,G),S=r.mul(Z,S),S=r.sub(S,I),I=r.mul(Z,W),_=r.mul(ne,_),_=r.add(_,I),new O(S,H,_)}subtract(y){return this.add(y.negate())}is0(){return this.equals(O.ZERO)}multiply(y){let{endo:p}=e;if(!o.isValidNot0(y))throw new Error(\"invalid scalar: out of range\");let m,v,A=g=>q.cached(this,g,T=>Zt(O,T));if(p){let{k1neg:g,k1:T,k2neg:S,k2:H}=X(y),{p:_,f:V}=A(T),{p:D,f:I}=A(H);v=V.add(I),m=K(p.beta,_,D,g,S)}else{let{p:g,f:T}=A(y);m=g,v=T}return Zt(O,[m,v])[0]}multiplyUnsafe(y){let{endo:p}=e,m=this;if(!o.isValid(y))throw new Error(\"invalid scalar: out of range\");if(y===be||m.is0())return O.ZERO;if(y===Ne)return m;if(q.hasCache(this))return this.multiply(y);if(p){let{k1neg:v,k1:A,k2neg:g,k2:T}=X(y),{p1:S,p2:H}=cr(O,m,A,T);return K(p.beta,S,H,v,g)}else return q.unsafe(m,y)}toAffine(y){return M(this,y)}isTorsionFree(){let{isTorsionFree:y}=e;return i===Ne?!0:y?y(O,this):q.unsafe(this,c).is0()}clearCofactor(){let{clearCofactor:y}=e;return i===Ne?this:y?y(O,this):this.multiplyUnsafe(i)}isSmallOrder(){return this.multiplyUnsafe(i).is0()}toBytes(y=!0){return Ze(y,\"isCompressed\"),this.assertValidity(),b(O,this,y)}toHex(y=!0){return He(this.toBytes(y))}toString(){return`<Point ${this.is0()?\"ZERO\":this.toHex()}>`}};f(O,\"BASE\",new O(s.Gx,s.Gy,r.ONE)),f(O,\"ZERO\",new O(r.ZERO,r.ONE,r.ZERO)),f(O,\"Fp\",r),f(O,\"Fn\",o);let C=O,$=o.BITS,q=new lt(C,e.endo?Math.ceil($/2):$);return C.BASE.precompute(8),C}function hr(t){return Uint8Array.of(t?2:3)}function dr(t,e){return{secretKey:e.BYTES,publicKey:1+t.BYTES,publicKeyUncompressed:1+2*t.BYTES,publicKeyHasPrefix:!0,signature:2*e.BYTES}}function $o(t,e={}){let{Fn:n}=t,r=e.randomBytes||Le,o=Object.assign(dr(t.Fp,n),{seed:Kt(n.ORDER)});function s(b){try{let x=n.fromBytes(b);return n.isValidNot0(x)}catch{return!1}}function i(b,x){let{publicKey:w,publicKeyUncompressed:E}=o;try{let B=b.length;return x===!0&&B!==w||x===!1&&B!==E?!1:!!t.fromBytes(b)}catch{return!1}}function c(b=r(o.seed)){return ft(P(b,o.seed,\"seed\"),n.ORDER)}function a(b,x=!0){return t.BASE.multiply(n.fromBytes(b)).toBytes(x)}function l(b){let{secretKey:x,publicKey:w,publicKeyUncompressed:E}=o;if(!ze(b)||\"_lengths\"in n&&n._lengths||x===w)return;let B=P(b,void 0,\"key\").length;return B===w||B===E}function u(b,x,w=!0){if(l(b)===!0)throw new Error(\"first arg must be private key\");if(l(x)===!1)throw new Error(\"second arg must be public key\");let E=n.fromBytes(b);return t.fromBytes(x).multiply(E).toBytes(w)}let d={isValidSecretKey:s,isValidPublicKey:i,randomSecretKey:c},h=ut(c,a);return Object.freeze({getPublicKey:a,getSharedSecret:u,keygen:h,Point:t,utils:d,lengths:o})}function br(t,e,n={}){nt(e),Je(n,{},{hmac:\"function\",lowS:\"boolean\",randomBytes:\"function\",bits2int:\"function\",bits2int_modN:\"function\"}),n=Object.assign({},n);let r=n.randomBytes||Le,o=n.hmac||((y,p)=>Ft(e,y,p)),{Fp:s,Fn:i}=t,{ORDER:c,BITS:a}=i,{keygen:l,getPublicKey:u,getSharedSecret:d,utils:h,lengths:b}=$o(t,n),x={prehash:!0,lowS:typeof n.lowS==\"boolean\"?n.lowS:!0,format:\"compact\",extraEntropy:!1},w=c*lr<s.ORDER;function E(y){let p=c>>Ne;return y>p}function B(y,p){if(!i.isValidNot0(p))throw new Error(`invalid signature ${y}: out of range 1..Point.Fn.ORDER`);return p}function k(){if(w)throw new Error('\"recovered\" sig type is not supported for cofactor >2 curves')}function U(y,p){Jt(p);let m=b.signature,v=p===\"compact\"?m:p===\"recovered\"?m+1:void 0;return P(y,v)}class N{constructor(p,m,v){f(this,\"r\");f(this,\"s\");f(this,\"recovery\");if(this.r=B(\"r\",p),this.s=B(\"s\",m),v!=null){if(k(),![0,1,2,3].includes(v))throw new Error(\"invalid recovery id\");this.recovery=v}Object.freeze(this)}static fromBytes(p,m=x.format){U(p,m);let v;if(m===\"der\"){let{r:S,s:H}=Be.toSig(P(p));return new N(S,H)}m===\"recovered\"&&(v=p[0],m=\"compact\",p=p.subarray(1));let A=b.signature/2,g=p.subarray(0,A),T=p.subarray(A,A*2);return new N(i.fromBytes(g),i.fromBytes(T),v)}static fromHex(p,m){return this.fromBytes(Re(p),m)}assertRecovery(){let{recovery:p}=this;if(p==null)throw new Error(\"invalid recovery id: must be present\");return p}addRecoveryBit(p){return new N(this.r,this.s,p)}recoverPublicKey(p){let{r:m,s:v}=this,A=this.assertRecovery(),g=A===2||A===3?m+c:m;if(!s.isValid(g))throw new Error(\"invalid recovery id: sig.r+curve.n != R.x\");let T=s.toBytes(g),S=t.fromBytes(oe(hr((A&1)===0),T)),H=i.inv(g),_=M(P(p,void 0,\"msgHash\")),V=i.create(-_*H),D=i.create(v*H),I=t.BASE.multiplyUnsafe(V).add(S.multiplyUnsafe(D));if(I.is0())throw new Error(\"invalid recovery: point at infinify\");return I.assertValidity(),I}hasHighS(){return E(this.s)}toBytes(p=x.format){if(Jt(p),p===\"der\")return Re(Be.hexFromSig(this));let{r:m,s:v}=this,A=i.toBytes(m),g=i.toBytes(v);return p===\"recovered\"?(k(),oe(Uint8Array.of(this.assertRecovery()),A,g)):oe(A,g)}toHex(p){return He(this.toBytes(p))}}let X=n.bits2int||function(p){if(p.length>8192)throw new Error(\"input is too large\");let m=Se(p),v=p.length*8-a;return v>0?m>>BigInt(v):m},M=n.bits2int_modN||function(p){return i.create(X(p))},z=Ye(a);function K(y){return Kn(\"num < 2^\"+a,y,be,z),i.toBytes(y)}function C(y,p){return P(y,void 0,\"message\"),p?P(e(y),void 0,\"prehashed message\"):y}function $(y,p,m){let{lowS:v,prehash:A,extraEntropy:g}=Yt(m,x);y=C(y,A);let T=M(y),S=i.fromBytes(p);if(!i.isValidNot0(S))throw new Error(\"invalid private key\");let H=[K(S),K(T)];if(g!=null&&g!==!1){let I=g===!0?r(b.secretKey):g;H.push(P(I,void 0,\"extraEntropy\"))}let _=oe(...H),V=T;function D(I){let W=X(I);if(!i.isValidNot0(W))return;let j=i.inv(W),Z=t.BASE.multiply(W).toAffine(),G=i.create(Z.x);if(G===be)return;let ne=i.create(j*i.create(V+G*S));if(ne===be)return;let _n=(Z.x===G?0:2)|Number(Z.y&Ne),In=ne;return v&&E(ne)&&(In=i.neg(ne),_n^=1),new N(G,In,w?void 0:_n)}return{seed:_,k2sig:D}}function q(y,p,m={}){let{seed:v,k2sig:A}=$(y,p,m);return jn(e.outputLen,i.BYTES,o)(v,A).toBytes(m.format)}function O(y,p,m,v={}){let{lowS:A,prehash:g,format:T}=Yt(v,x);if(m=P(m,void 0,\"publicKey\"),p=C(p,g),!ze(y)){let S=y instanceof N?\", use sig.toBytes()\":\"\";throw new Error(\"verify expects Uint8Array signature\"+S)}U(y,T);try{let S=N.fromBytes(y,T),H=t.fromBytes(m);if(A&&S.hasHighS())return!1;let{r:_,s:V}=S,D=M(p),I=i.inv(V),W=i.create(D*I),j=i.create(_*I),Z=t.BASE.multiplyUnsafe(W).add(H.multiplyUnsafe(j));return Z.is0()?!1:i.create(Z.x)===_}catch{return!1}}function L(y,p,m={}){let{prehash:v}=Yt(m,x);return p=C(p,v),N.fromBytes(y,\"recovered\").recoverPublicKey(p).toBytes()}return Object.freeze({keygen:l,getPublicKey:u,getSharedSecret:d,utils:h,lengths:b,Point:t,sign:q,verify:O,recoverPublicKey:L,Signature:N,hash:e})}var yt={p:BigInt(\"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f\"),n:BigInt(\"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141\"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt(\"0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\"),Gy:BigInt(\"0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8\")},Mo={beta:BigInt(\"0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee\"),basises:[[BigInt(\"0x3086d221a7d46bcde86c90e49284eb15\"),-BigInt(\"0xe4437ed6010e88286f547fa90abfe4c3\")],[BigInt(\"0x114ca50f7a8e2f3f657c1108d9d44cfd8\"),BigInt(\"0x3086d221a7d46bcde86c90e49284eb15\")]]},Vo=BigInt(0),Qt=BigInt(2);function Wo(t){let e=yt.p,n=BigInt(3),r=BigInt(6),o=BigInt(11),s=BigInt(22),i=BigInt(23),c=BigInt(44),a=BigInt(88),l=t*t*t%e,u=l*l*t%e,d=se(u,n,e)*u%e,h=se(d,n,e)*u%e,b=se(h,Qt,e)*l%e,x=se(b,o,e)*b%e,w=se(x,s,e)*x%e,E=se(w,c,e)*w%e,B=se(E,a,e)*E%e,k=se(B,c,e)*w%e,U=se(k,n,e)*u%e,N=se(U,i,e)*x%e,X=se(N,r,e)*l%e,M=se(X,Qt,e);if(!bt.eql(bt.sqr(M),t))throw new Error(\"Cannot find square root\");return M}var bt=Ue(yt.p,{sqrt:Wo}),_e=ur(yt,{Fp:bt,endo:Mo}),xr=br(_e,st),pr={};function pt(t,...e){let n=pr[t];if(n===void 0){let r=st(Wn(t));n=oe(r,r),pr[t]=n}return st(oe(n,...e))}var tn=t=>t.toBytes(!0).slice(1),nn=t=>t%Qt===Vo;function en(t){let{Fn:e,BASE:n}=_e,r=e.fromBytes(t),o=n.multiply(r);return{scalar:nn(o.y)?r:e.neg(r),bytes:tn(o)}}function gr(t){let e=bt;if(!e.isValidNot0(t))throw new Error(\"invalid x: Fail if x \\u2265 p\");let n=e.create(t*t),r=e.create(n*t+BigInt(7)),o=e.sqrt(r);nn(o)||(o=e.neg(o));let s=_e.fromAffine({x:t,y:o});return s.assertValidity(),s}var Qe=Se;function wr(...t){return _e.Fn.create(Qe(pt(\"BIP0340/challenge\",...t)))}function yr(t){return en(t).bytes}function Ko(t,e,n=Le(32)){let{Fn:r}=_e,o=P(t,void 0,\"message\"),{bytes:s,scalar:i}=en(e),c=P(n,32,\"auxRand\"),a=r.toBytes(i^Qe(pt(\"BIP0340/aux\",c))),l=pt(\"BIP0340/nonce\",a,s,o),{bytes:u,scalar:d}=en(l),h=wr(u,s,o),b=new Uint8Array(64);if(b.set(u,0),b.set(r.toBytes(r.create(d+h*i)),32),!mr(b,o,s))throw new Error(\"sign: Invalid signature produced\");return b}function mr(t,e,n){let{Fp:r,Fn:o,BASE:s}=_e,i=P(t,64,\"signature\"),c=P(e,void 0,\"message\"),a=P(n,32,\"publicKey\");try{let l=gr(Qe(a)),u=Qe(i.subarray(0,32));if(!r.isValidNot0(u))return!1;let d=Qe(i.subarray(32,64));if(!o.isValidNot0(d))return!1;let h=wr(o.toBytes(u),tn(l),c),b=s.multiplyUnsafe(d).add(l.multiplyUnsafe(o.neg(h))),{x,y:w}=b.toAffine();return!(b.is0()||!nn(w)||x!==u)}catch{return!1}}var Y=(()=>{let n=(r=Le(48))=>ft(r,yt.n);return{keygen:ut(n,yr),getPublicKey:yr,sign:Ko,verify:mr,Point:_e,utils:{randomSecretKey:n,taggedHash:pt,lift_x:gr,pointToBytes:tn},lengths:{secretKey:32,publicKey:32,publicKeyHasPrefix:!1,signature:64,seed:48}}})();function jo(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name===\"Uint8Array\"}function xt(t,e=\"\"){if(!Number.isSafeInteger(t)||t<0){let n=e&&`\"${e}\" `;throw new Error(`${n}expected integer >= 0, got ${t}`)}}function le(t,e,n=\"\"){let r=jo(t),o=t?.length,s=e!==void 0;if(!r||s&&o!==e){let i=n&&`\"${n}\" `,c=s?` of length ${e}`:\"\",a=r?`length=${o}`:`type=${typeof t}`;throw new Error(i+\"expected Uint8Array\"+c+\", got \"+a)}return t}function et(t){if(typeof t!=\"function\"||typeof t.create!=\"function\")throw new Error(\"Hash must wrapped by utils.createHasher\");xt(t.outputLen),xt(t.blockLen)}function qe(t,e=!0){if(t.destroyed)throw new Error(\"Hash instance has been destroyed\");if(e&&t.finished)throw new Error(\"Hash#digest() has already been called\")}function vr(t,e){le(t,void 0,\"digestInto() output\");let n=e.outputLen;if(t.length<n)throw new Error('\"digestInto() output\" expected to be of length >='+n)}function ye(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function gt(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function ae(t,e){return t<<32-e|t>>>e}var Sr=typeof Uint8Array.from([]).toHex==\"function\"&&typeof Uint8Array.fromHex==\"function\",Go=Array.from({length:256},(t,e)=>e.toString(16).padStart(2,\"0\"));function te(t){if(le(t),Sr)return t.toHex();let e=\"\";for(let n=0;n<t.length;n++)e+=Go[t[n]];return e}var pe={_0:48,_9:57,A:65,F:70,a:97,f:102};function Er(t){if(t>=pe._0&&t<=pe._9)return t-pe._0;if(t>=pe.A&&t<=pe.F)return t-(pe.A-10);if(t>=pe.a&&t<=pe.f)return t-(pe.a-10)}function F(t){if(typeof t!=\"string\")throw new Error(\"hex string expected, got \"+typeof t);if(Sr)return Uint8Array.fromHex(t);let e=t.length,n=e/2;if(e%2)throw new Error(\"hex string expected, got unpadded hex of length \"+e);let r=new Uint8Array(n);for(let o=0,s=0;o<n;o++,s+=2){let i=Er(t.charCodeAt(s)),c=Er(t.charCodeAt(s+1));if(i===void 0||c===void 0){let a=t[s]+t[s+1];throw new Error('hex string expected, got non-hex character \"'+a+'\" at index '+s)}r[o]=i*16+c}return r}function De(...t){let e=0;for(let r=0;r<t.length;r++){let o=t[r];le(o),e+=o.length}let n=new Uint8Array(e);for(let r=0,o=0;r<t.length;r++){let s=t[r];n.set(s,o),o+=s.length}return n}function Br(t,e={}){let n=(o,s)=>t(s).update(o).digest(),r=t(void 0);return n.outputLen=r.outputLen,n.blockLen=r.blockLen,n.create=o=>t(o),Object.assign(n,e),Object.freeze(n)}function Ar(t=32){let e=typeof globalThis==\"object\"?globalThis.crypto:null;if(typeof e?.getRandomValues!=\"function\")throw new Error(\"crypto.getRandomValues must be defined\");return e.getRandomValues(new Uint8Array(t))}var Tr=t=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,t])});function Hr(t,e,n){return t&e^~t&n}function Rr(t,e,n){return t&e^t&n^e&n}var wt=class{constructor(e,n,r,o){f(this,\"blockLen\");f(this,\"outputLen\");f(this,\"padOffset\");f(this,\"isLE\");f(this,\"buffer\");f(this,\"view\");f(this,\"finished\",!1);f(this,\"length\",0);f(this,\"pos\",0);f(this,\"destroyed\",!1);this.blockLen=e,this.outputLen=n,this.padOffset=r,this.isLE=o,this.buffer=new Uint8Array(e),this.view=gt(this.buffer)}update(e){qe(this),le(e);let{view:n,buffer:r,blockLen:o}=this,s=e.length;for(let i=0;i<s;){let c=Math.min(o-this.pos,s-i);if(c===o){let a=gt(e);for(;o<=s-i;i+=o)this.process(a,i);continue}r.set(e.subarray(i,i+c),this.pos),this.pos+=c,i+=c,this.pos===o&&(this.process(n,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){qe(this),vr(e,this),this.finished=!0;let{buffer:n,view:r,blockLen:o,isLE:s}=this,{pos:i}=this;n[i++]=128,ye(this.buffer.subarray(i)),this.padOffset>o-i&&(this.process(r,0),i=0);for(let d=i;d<o;d++)n[d]=0;r.setBigUint64(o-8,BigInt(this.length*8),s),this.process(r,0);let c=gt(e),a=this.outputLen;if(a%4)throw new Error(\"_sha2: outputLen must be aligned to 32bit\");let l=a/4,u=this.get();if(l>u.length)throw new Error(\"_sha2: outputLen bigger than state\");for(let d=0;d<l;d++)c.setUint32(4*d,u[d],s)}digest(){let{buffer:e,outputLen:n}=this;this.digestInto(e);let r=e.slice(0,n);return this.destroy(),r}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:n,buffer:r,length:o,finished:s,destroyed:i,pos:c}=this;return e.destroyed=i,e.finished=s,e.length=o,e.pos=c,o%n&&e.buffer.set(r),e}clone(){return this._cloneInto()}},xe=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var zo=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Ae=new Uint32Array(64),rn=class extends wt{constructor(e){super(64,e,8,!1)}get(){let{A:e,B:n,C:r,D:o,E:s,F:i,G:c,H:a}=this;return[e,n,r,o,s,i,c,a]}set(e,n,r,o,s,i,c,a){this.A=e|0,this.B=n|0,this.C=r|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=c|0,this.H=a|0}process(e,n){for(let d=0;d<16;d++,n+=4)Ae[d]=e.getUint32(n,!1);for(let d=16;d<64;d++){let h=Ae[d-15],b=Ae[d-2],x=ae(h,7)^ae(h,18)^h>>>3,w=ae(b,17)^ae(b,19)^b>>>10;Ae[d]=w+Ae[d-7]+x+Ae[d-16]|0}let{A:r,B:o,C:s,D:i,E:c,F:a,G:l,H:u}=this;for(let d=0;d<64;d++){let h=ae(c,6)^ae(c,11)^ae(c,25),b=u+h+Hr(c,a,l)+zo[d]+Ae[d]|0,w=(ae(r,2)^ae(r,13)^ae(r,22))+Rr(r,o,s)|0;u=l,l=a,a=c,c=i+b|0,i=s,s=o,o=r,r=b+w|0}r=r+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,c=c+this.E|0,a=a+this.F|0,l=l+this.G|0,u=u+this.H|0,this.set(r,o,s,i,c,a,l,u)}roundClean(){ye(Ae)}destroy(){this.set(0,0,0,0,0,0,0,0),ye(this.buffer)}},on=class extends rn{constructor(){super(32);f(this,\"A\",xe[0]|0);f(this,\"B\",xe[1]|0);f(this,\"C\",xe[2]|0);f(this,\"D\",xe[3]|0);f(this,\"E\",xe[4]|0);f(this,\"F\",xe[5]|0);f(this,\"G\",xe[6]|0);f(this,\"H\",xe[7]|0)}};var ue=Br(()=>new on,Tr(1));var Ac=new TextDecoder(\"utf-8\"),Zo=new TextEncoder;function Fo(t){if(t.length!==64)return!1;for(let e=0;e<64;e++){let n=t.charCodeAt(e);if(isNaN(n)||n<48||n>102||n>57&&n<97)return!1}return!0}var $e=Symbol(\"verified\"),Yo=t=>t instanceof Object;function Jo(t){if(!Yo(t)||typeof t.kind!=\"number\"||typeof t.content!=\"string\"||typeof t.created_at!=\"number\"||typeof t.pubkey!=\"string\"||!Fo(t.pubkey)||!Array.isArray(t.tags))return!1;for(let e=0;e<t.tags.length;e++){let n=t.tags[e];if(!Array.isArray(n))return!1;for(let r=0;r<n.length;r++)if(typeof n[r]!=\"string\")return!1}return!0}var Xo=class{generateSecretKey(){return Y.utils.randomSecretKey()}getPublicKey(t){return te(Y.getPublicKey(t))}finalizeEvent(t,e){let n=t;return n.pubkey=te(Y.getPublicKey(e)),n.id=sn(n),n.sig=te(Y.sign(F(sn(n)),e)),n[$e]=!0,n}verifyEvent(t){if(typeof t[$e]==\"boolean\")return t[$e];try{let e=sn(t);if(e!==t.id)return t[$e]=!1,!1;let n=Y.verify(F(t.sig),F(e),F(t.pubkey));return t[$e]=n,n}catch{return t[$e]=!1,!1}}};function Qo(t){if(!Jo(t))throw new Error(\"can't serialize event with wrong or missing properties\");return JSON.stringify([0,t.pubkey,t.created_at,t.kind,t.tags,t.content])}function sn(t){let e=ue(Zo.encode(Qo(t)));return te(e)}var mt=new Xo,Lr=mt.generateSecretKey,Or=mt.getPublicKey,kr=mt.finalizeEvent,_r=mt.verifyEvent;function es(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name===\"Uint8Array\"}function Et(t){if(typeof t!=\"boolean\")throw new Error(`boolean expected, not ${t}`)}function vt(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(\"positive integer expected, got \"+t)}function J(t,e,n=\"\"){let r=es(t),o=t?.length,s=e!==void 0;if(!r||s&&o!==e){let i=n&&`\"${n}\" `,c=s?` of length ${e}`:\"\",a=r?`length=${o}`:`type=${typeof t}`;throw new Error(i+\"expected Uint8Array\"+c+\", got \"+a)}return t}function cn(t,e=!0){if(t.destroyed)throw new Error(\"Hash instance has been destroyed\");if(e&&t.finished)throw new Error(\"Hash#digest() has already been called\")}function Ir(t,e){J(t,void 0,\"output\");let n=e.outputLen;if(t.length<n)throw new Error(\"digestInto() expects output buffer of length at least \"+n)}function ge(t){return new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4))}function we(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function ts(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}var ns=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function Cr(t,e){if(e==null||typeof e!=\"object\")throw new Error(\"options must be defined\");return Object.assign(t,e)}function St(t,e){if(t.length!==e.length)return!1;let n=0;for(let r=0;r<t.length;r++)n|=t[r]^e[r];return n===0}var an=(t,e)=>{function n(r,...o){if(J(r,void 0,\"key\"),!ns)throw new Error(\"Non little-endian hardware is not yet supported\");if(t.nonceLength!==void 0){let u=o[0];J(u,t.varSizeNonce?void 0:t.nonceLength,\"nonce\")}let s=t.tagLength;s&&o[1]!==void 0&&J(o[1],void 0,\"AAD\");let i=e(r,...o),c=(u,d)=>{if(d!==void 0){if(u!==2)throw new Error(\"cipher output not supported\");J(d,void 0,\"output\")}},a=!1;return{encrypt(u,d){if(a)throw new Error(\"cannot encrypt() twice with same key + nonce\");return a=!0,J(u),c(i.encrypt.length,d),i.encrypt(u,d)},decrypt(u,d){if(J(u),s&&u.length<s)throw new Error('\"ciphertext\" expected length bigger than tagLength='+s);return c(i.decrypt.length,d),i.decrypt(u,d)}}}return Object.assign(n,t),n};function fn(t,e,n=!0){if(e===void 0)return new Uint8Array(t);if(e.length!==t)throw new Error('\"output\" expected Uint8Array of length '+t+\", got: \"+e.length);if(n&&!rs(e))throw new Error(\"invalid output, must be aligned\");return e}function Ur(t,e,n){Et(n);let r=new Uint8Array(16),o=ts(r);return o.setBigUint64(0,BigInt(e),n),o.setBigUint64(8,BigInt(t),n),r}function rs(t){return t.byteOffset%4===0}function Me(t){return Uint8Array.from(t)}var Nr=t=>Uint8Array.from(t.split(\"\"),e=>e.charCodeAt(0)),os=Nr(\"expand 16-byte k\"),ss=Nr(\"expand 32-byte k\"),is=ge(os),cs=ge(ss);function R(t,e){return t<<e|t>>>32-e}function ln(t){return t.byteOffset%4===0}var Bt=64,as=16,qr=2**32-1,Pr=Uint32Array.of();function fs(t,e,n,r,o,s,i,c){let a=o.length,l=new Uint8Array(Bt),u=ge(l),d=ln(o)&&ln(s),h=d?ge(o):Pr,b=d?ge(s):Pr;for(let x=0;x<a;i++){if(t(e,n,r,u,i,c),i>=qr)throw new Error(\"arx: counter overflow\");let w=Math.min(Bt,a-x);if(d&&w===Bt){let E=x/4;if(x%4!==0)throw new Error(\"arx: invalid block position\");for(let B=0,k;B<as;B++)k=E+B,b[k]=h[k]^u[B];x+=Bt;continue}for(let E=0,B;E<w;E++)B=x+E,s[B]=o[B]^l[E];x+=w}}function un(t,e){let{allowShortKeys:n,extendNonceFn:r,counterLength:o,counterRight:s,rounds:i}=Cr({allowShortKeys:!1,counterLength:8,counterRight:!1,rounds:20},e);if(typeof t!=\"function\")throw new Error(\"core must be a function\");return vt(o),vt(i),Et(s),Et(n),(c,a,l,u,d=0)=>{J(c,void 0,\"key\"),J(a,void 0,\"nonce\"),J(l,void 0,\"data\");let h=l.length;if(u===void 0&&(u=new Uint8Array(h)),J(u,void 0,\"output\"),vt(d),d<0||d>=qr)throw new Error(\"arx: counter overflow\");if(u.length<h)throw new Error(`arx: output (${u.length}) is shorter than data (${h})`);let b=[],x=c.length,w,E;if(x===32)b.push(w=Me(c)),E=cs;else if(x===16&&n)w=new Uint8Array(32),w.set(c),w.set(c,16),E=is,b.push(w);else throw J(c,32,\"arx key\"),new Error(\"invalid key size\");ln(a)||b.push(a=Me(a));let B=ge(w);if(r){if(a.length!==24)throw new Error(\"arx: extended nonce must be 24 bytes\");r(E,B,ge(a.subarray(0,16)),B),a=a.subarray(16)}let k=16-o;if(k!==a.length)throw new Error(`arx: nonce must be ${k} or 16 bytes`);if(k!==12){let N=new Uint8Array(12);N.set(a,s?0:12-a.length),a=N,b.push(a)}let U=ge(a);return fs(t,E,B,U,l,u,d,i),we(...b),u}}function Q(t,e){return t[e++]&255|(t[e++]&255)<<8}var hn=class{constructor(e){f(this,\"blockLen\",16);f(this,\"outputLen\",16);f(this,\"buffer\",new Uint8Array(16));f(this,\"r\",new Uint16Array(10));f(this,\"h\",new Uint16Array(10));f(this,\"pad\",new Uint16Array(8));f(this,\"pos\",0);f(this,\"finished\",!1);e=Me(J(e,32,\"key\"));let n=Q(e,0),r=Q(e,2),o=Q(e,4),s=Q(e,6),i=Q(e,8),c=Q(e,10),a=Q(e,12),l=Q(e,14);this.r[0]=n&8191,this.r[1]=(n>>>13|r<<3)&8191,this.r[2]=(r>>>10|o<<6)&7939,this.r[3]=(o>>>7|s<<9)&8191,this.r[4]=(s>>>4|i<<12)&255,this.r[5]=i>>>1&8190,this.r[6]=(i>>>14|c<<2)&8191,this.r[7]=(c>>>11|a<<5)&8065,this.r[8]=(a>>>8|l<<8)&8191,this.r[9]=l>>>5&127;for(let u=0;u<8;u++)this.pad[u]=Q(e,16+2*u)}process(e,n,r=!1){let o=r?0:2048,{h:s,r:i}=this,c=i[0],a=i[1],l=i[2],u=i[3],d=i[4],h=i[5],b=i[6],x=i[7],w=i[8],E=i[9],B=Q(e,n+0),k=Q(e,n+2),U=Q(e,n+4),N=Q(e,n+6),X=Q(e,n+8),M=Q(e,n+10),z=Q(e,n+12),K=Q(e,n+14),C=s[0]+(B&8191),$=s[1]+((B>>>13|k<<3)&8191),q=s[2]+((k>>>10|U<<6)&8191),O=s[3]+((U>>>7|N<<9)&8191),L=s[4]+((N>>>4|X<<12)&8191),y=s[5]+(X>>>1&8191),p=s[6]+((X>>>14|M<<2)&8191),m=s[7]+((M>>>11|z<<5)&8191),v=s[8]+((z>>>8|K<<8)&8191),A=s[9]+(K>>>5|o),g=0,T=g+C*c+$*(5*E)+q*(5*w)+O*(5*x)+L*(5*b);g=T>>>13,T&=8191,T+=y*(5*h)+p*(5*d)+m*(5*u)+v*(5*l)+A*(5*a),g+=T>>>13,T&=8191;let S=g+C*a+$*c+q*(5*E)+O*(5*w)+L*(5*x);g=S>>>13,S&=8191,S+=y*(5*b)+p*(5*h)+m*(5*d)+v*(5*u)+A*(5*l),g+=S>>>13,S&=8191;let H=g+C*l+$*a+q*c+O*(5*E)+L*(5*w);g=H>>>13,H&=8191,H+=y*(5*x)+p*(5*b)+m*(5*h)+v*(5*d)+A*(5*u),g+=H>>>13,H&=8191;let _=g+C*u+$*l+q*a+O*c+L*(5*E);g=_>>>13,_&=8191,_+=y*(5*w)+p*(5*x)+m*(5*b)+v*(5*h)+A*(5*d),g+=_>>>13,_&=8191;let V=g+C*d+$*u+q*l+O*a+L*c;g=V>>>13,V&=8191,V+=y*(5*E)+p*(5*w)+m*(5*x)+v*(5*b)+A*(5*h),g+=V>>>13,V&=8191;let D=g+C*h+$*d+q*u+O*l+L*a;g=D>>>13,D&=8191,D+=y*c+p*(5*E)+m*(5*w)+v*(5*x)+A*(5*b),g+=D>>>13,D&=8191;let I=g+C*b+$*h+q*d+O*u+L*l;g=I>>>13,I&=8191,I+=y*a+p*c+m*(5*E)+v*(5*w)+A*(5*x),g+=I>>>13,I&=8191;let W=g+C*x+$*b+q*h+O*d+L*u;g=W>>>13,W&=8191,W+=y*l+p*a+m*c+v*(5*E)+A*(5*w),g+=W>>>13,W&=8191;let j=g+C*w+$*x+q*b+O*h+L*d;g=j>>>13,j&=8191,j+=y*u+p*l+m*a+v*c+A*(5*E),g+=j>>>13,j&=8191;let Z=g+C*E+$*w+q*x+O*b+L*h;g=Z>>>13,Z&=8191,Z+=y*d+p*u+m*l+v*a+A*c,g+=Z>>>13,Z&=8191,g=(g<<2)+g|0,g=g+T|0,T=g&8191,g=g>>>13,S+=g,s[0]=T,s[1]=S,s[2]=H,s[3]=_,s[4]=V,s[5]=D,s[6]=I,s[7]=W,s[8]=j,s[9]=Z}finalize(){let{h:e,pad:n}=this,r=new Uint16Array(10),o=e[1]>>>13;e[1]&=8191;for(let c=2;c<10;c++)e[c]+=o,o=e[c]>>>13,e[c]&=8191;e[0]+=o*5,o=e[0]>>>13,e[0]&=8191,e[1]+=o,o=e[1]>>>13,e[1]&=8191,e[2]+=o,r[0]=e[0]+5,o=r[0]>>>13,r[0]&=8191;for(let c=1;c<10;c++)r[c]=e[c]+o,o=r[c]>>>13,r[c]&=8191;r[9]-=8192;let s=(o^1)-1;for(let c=0;c<10;c++)r[c]&=s;s=~s;for(let c=0;c<10;c++)e[c]=e[c]&s|r[c];e[0]=(e[0]|e[1]<<13)&65535,e[1]=(e[1]>>>3|e[2]<<10)&65535,e[2]=(e[2]>>>6|e[3]<<7)&65535,e[3]=(e[3]>>>9|e[4]<<4)&65535,e[4]=(e[4]>>>12|e[5]<<1|e[6]<<14)&65535,e[5]=(e[6]>>>2|e[7]<<11)&65535,e[6]=(e[7]>>>5|e[8]<<8)&65535,e[7]=(e[8]>>>8|e[9]<<5)&65535;let i=e[0]+n[0];e[0]=i&65535;for(let c=1;c<8;c++)i=(e[c]+n[c]|0)+(i>>>16)|0,e[c]=i&65535;we(r)}update(e){cn(this),J(e),e=Me(e);let{buffer:n,blockLen:r}=this,o=e.length;for(let s=0;s<o;){let i=Math.min(r-this.pos,o-s);if(i===r){for(;r<=o-s;s+=r)this.process(e,s);continue}n.set(e.subarray(s,s+i),this.pos),this.pos+=i,s+=i,this.pos===r&&(this.process(n,0,!1),this.pos=0)}return this}destroy(){we(this.h,this.r,this.buffer,this.pad)}digestInto(e){cn(this),Ir(e,this),this.finished=!0;let{buffer:n,h:r}=this,{pos:o}=this;if(o){for(n[o++]=1;o<16;o++)n[o]=0;this.process(n,0,!0)}this.finalize();let s=0;for(let i=0;i<8;i++)e[s++]=r[i]>>>0,e[s++]=r[i]>>>8;return e}digest(){let{buffer:e,outputLen:n}=this;this.digestInto(e);let r=e.slice(0,n);return this.destroy(),r}};function ls(t){let e=(r,o)=>t(o).update(r).digest(),n=t(new Uint8Array(32));return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=r=>t(r),e}var Dr=ls(t=>new hn(t));function Vr(t,e,n,r,o,s=20){let i=t[0],c=t[1],a=t[2],l=t[3],u=e[0],d=e[1],h=e[2],b=e[3],x=e[4],w=e[5],E=e[6],B=e[7],k=o,U=n[0],N=n[1],X=n[2],M=i,z=c,K=a,C=l,$=u,q=d,O=h,L=b,y=x,p=w,m=E,v=B,A=k,g=U,T=N,S=X;for(let _=0;_<s;_+=2)M=M+$|0,A=R(A^M,16),y=y+A|0,$=R($^y,12),M=M+$|0,A=R(A^M,8),y=y+A|0,$=R($^y,7),z=z+q|0,g=R(g^z,16),p=p+g|0,q=R(q^p,12),z=z+q|0,g=R(g^z,8),p=p+g|0,q=R(q^p,7),K=K+O|0,T=R(T^K,16),m=m+T|0,O=R(O^m,12),K=K+O|0,T=R(T^K,8),m=m+T|0,O=R(O^m,7),C=C+L|0,S=R(S^C,16),v=v+S|0,L=R(L^v,12),C=C+L|0,S=R(S^C,8),v=v+S|0,L=R(L^v,7),M=M+q|0,S=R(S^M,16),m=m+S|0,q=R(q^m,12),M=M+q|0,S=R(S^M,8),m=m+S|0,q=R(q^m,7),z=z+O|0,A=R(A^z,16),v=v+A|0,O=R(O^v,12),z=z+O|0,A=R(A^z,8),v=v+A|0,O=R(O^v,7),K=K+L|0,g=R(g^K,16),y=y+g|0,L=R(L^y,12),K=K+L|0,g=R(g^K,8),y=y+g|0,L=R(L^y,7),C=C+$|0,T=R(T^C,16),p=p+T|0,$=R($^p,12),C=C+$|0,T=R(T^C,8),p=p+T|0,$=R($^p,7);let H=0;r[H++]=i+M|0,r[H++]=c+z|0,r[H++]=a+K|0,r[H++]=l+C|0,r[H++]=u+$|0,r[H++]=d+q|0,r[H++]=h+O|0,r[H++]=b+L|0,r[H++]=x+y|0,r[H++]=w+p|0,r[H++]=E+m|0,r[H++]=B+v|0,r[H++]=k+A|0,r[H++]=U+g|0,r[H++]=N+T|0,r[H++]=X+S|0}function us(t,e,n,r){let o=t[0],s=t[1],i=t[2],c=t[3],a=e[0],l=e[1],u=e[2],d=e[3],h=e[4],b=e[5],x=e[6],w=e[7],E=n[0],B=n[1],k=n[2],U=n[3];for(let X=0;X<20;X+=2)o=o+a|0,E=R(E^o,16),h=h+E|0,a=R(a^h,12),o=o+a|0,E=R(E^o,8),h=h+E|0,a=R(a^h,7),s=s+l|0,B=R(B^s,16),b=b+B|0,l=R(l^b,12),s=s+l|0,B=R(B^s,8),b=b+B|0,l=R(l^b,7),i=i+u|0,k=R(k^i,16),x=x+k|0,u=R(u^x,12),i=i+u|0,k=R(k^i,8),x=x+k|0,u=R(u^x,7),c=c+d|0,U=R(U^c,16),w=w+U|0,d=R(d^w,12),c=c+d|0,U=R(U^c,8),w=w+U|0,d=R(d^w,7),o=o+l|0,U=R(U^o,16),x=x+U|0,l=R(l^x,12),o=o+l|0,U=R(U^o,8),x=x+U|0,l=R(l^x,7),s=s+u|0,E=R(E^s,16),w=w+E|0,u=R(u^w,12),s=s+u|0,E=R(E^s,8),w=w+E|0,u=R(u^w,7),i=i+d|0,B=R(B^i,16),h=h+B|0,d=R(d^h,12),i=i+d|0,B=R(B^i,8),h=h+B|0,d=R(d^h,7),c=c+a|0,k=R(k^c,16),b=b+k|0,a=R(a^b,12),c=c+a|0,k=R(k^c,8),b=b+k|0,a=R(a^b,7);let N=0;r[N++]=o,r[N++]=s,r[N++]=i,r[N++]=c,r[N++]=E,r[N++]=B,r[N++]=k,r[N++]=U}var At=un(Vr,{counterRight:!1,counterLength:4,allowShortKeys:!1}),hs=un(Vr,{counterRight:!1,counterLength:8,extendNonceFn:us,allowShortKeys:!1});var ds=new Uint8Array(16),$r=(t,e)=>{t.update(e);let n=e.length%16;n&&t.update(ds.subarray(n))},bs=new Uint8Array(32);function Mr(t,e,n,r,o){o!==void 0&&J(o,void 0,\"AAD\");let s=t(e,n,bs),i=Ur(r.length,o?o.length:0,!0),c=Dr.create(s);o&&$r(c,o),$r(c,r),c.update(i);let a=c.digest();return we(s,i),a}var Wr=t=>(e,n,r)=>({encrypt(s,i){let c=s.length;i=fn(c+16,i,!1),i.set(s);let a=i.subarray(0,-16);t(e,n,a,a,1);let l=Mr(t,e,n,a,r);return i.set(l,c),we(l),i},decrypt(s,i){i=fn(s.length-16,i,!1);let c=s.subarray(0,-16),a=s.subarray(-16),l=Mr(t,e,n,c,r);if(!St(a,l))throw new Error(\"invalid tag\");return i.set(s.subarray(0,-16)),t(e,n,i,i,1),we(l),i}}),jc=an({blockSize:64,nonceLength:12,tagLength:16},Wr(At)),Gc=an({blockSize:64,nonceLength:24,tagLength:16},Wr(hs));var Tt=class{constructor(e,n){f(this,\"oHash\");f(this,\"iHash\");f(this,\"blockLen\");f(this,\"outputLen\");f(this,\"finished\",!1);f(this,\"destroyed\",!1);if(et(e),le(n,void 0,\"key\"),this.iHash=e.create(),typeof this.iHash.update!=\"function\")throw new Error(\"Expected instance of class which extends utils.Hash\");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let r=this.blockLen,o=new Uint8Array(r);o.set(n.length>r?e.create().update(n).digest():n);for(let s=0;s<o.length;s++)o[s]^=54;this.iHash.update(o),this.oHash=e.create();for(let s=0;s<o.length;s++)o[s]^=106;this.oHash.update(o),ye(o)}update(e){return qe(this),this.iHash.update(e),this}digestInto(e){qe(this),le(e,this.outputLen,\"output\"),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){let e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));let{oHash:n,iHash:r,finished:o,destroyed:s,blockLen:i,outputLen:c}=this;return e=e,e.finished=o,e.destroyed=s,e.blockLen=i,e.outputLen=c,e.oHash=n._cloneInto(e.oHash),e.iHash=r._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},Ve=(t,e,n)=>new Tt(t,e).update(n).digest();Ve.create=(t,e)=>new Tt(t,e);function jr(t,e,n){return et(t),n===void 0&&(n=new Uint8Array(t.outputLen)),Ve(t,n,e)}var dn=Uint8Array.of(0),Kr=Uint8Array.of();function Gr(t,e,n,r=32){et(t),xt(r,\"length\");let o=t.outputLen;if(r>255*o)throw new Error(\"Length must be <= 255*HashLen\");let s=Math.ceil(r/o);n===void 0?n=Kr:le(n,void 0,\"info\");let i=new Uint8Array(s*o),c=Ve.create(t,e),a=c._cloneInto(),l=new Uint8Array(c.outputLen);for(let u=0;u<s;u++)dn[0]=u+1,a.update(u===0?Kr:l).update(n).update(dn).digestInto(l),i.set(l,o*u),c._cloneInto(a);return c.destroy(),a.destroy(),ye(l,dn),i.slice(0,r)}function xn(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name===\"Uint8Array\"}function ps(t){if(!xn(t))throw new Error(\"Uint8Array expected\")}function Yr(t,e){return Array.isArray(e)?e.length===0?!0:t?e.every(n=>typeof n==\"string\"):e.every(n=>Number.isSafeInteger(n)):!1}function ys(t){if(typeof t!=\"function\")throw new Error(\"function expected\");return!0}function Ie(t,e){if(typeof e!=\"string\")throw new Error(`${t}: string expected`);return!0}function gn(t){if(!Number.isSafeInteger(t))throw new Error(`invalid integer: ${t}`)}function bn(t){if(!Array.isArray(t))throw new Error(\"array expected\")}function Rt(t,e){if(!Yr(!0,e))throw new Error(`${t}: array of strings expected`)}function Jr(t,e){if(!Yr(!1,e))throw new Error(`${t}: array of numbers expected`)}function Xr(...t){let e=s=>s,n=(s,i)=>c=>s(i(c)),r=t.map(s=>s.encode).reduceRight(n,e),o=t.map(s=>s.decode).reduce(n,e);return{encode:r,decode:o}}function Qr(t){let e=typeof t==\"string\"?t.split(\"\"):t,n=e.length;Rt(\"alphabet\",e);let r=new Map(e.map((o,s)=>[o,s]));return{encode:o=>(bn(o),o.map(s=>{if(!Number.isSafeInteger(s)||s<0||s>=n)throw new Error(`alphabet.encode: digit index outside alphabet \"${s}\". Allowed: ${t}`);return e[s]})),decode:o=>(bn(o),o.map(s=>{Ie(\"alphabet.decode\",s);let i=r.get(s);if(i===void 0)throw new Error(`Unknown letter: \"${s}\". Allowed: ${t}`);return i}))}}function eo(t=\"\"){return Ie(\"join\",t),{encode:e=>(Rt(\"join.decode\",e),e.join(t)),decode:e=>(Ie(\"join.decode\",e),e.split(t))}}function xs(t,e=\"=\"){return gn(t),Ie(\"padding\",e),{encode(n){for(Rt(\"padding.encode\",n);n.length*t%8;)n.push(e);return n},decode(n){Rt(\"padding.decode\",n);let r=n.length;if(r*t%8)throw new Error(\"padding: invalid, string should have whole number of bytes\");for(;r>0&&n[r-1]===e;r--)if((r-1)*t%8===0)throw new Error(\"padding: invalid, string has too much padding\");return n.slice(0,r)}}}var to=(t,e)=>e===0?t:to(e,t%e),Lt=(t,e)=>t+(e-to(t,e)),Ht=(()=>{let t=[];for(let e=0;e<40;e++)t.push(2**e);return t})();function pn(t,e,n,r){if(bn(t),e<=0||e>32)throw new Error(`convertRadix2: wrong from=${e}`);if(n<=0||n>32)throw new Error(`convertRadix2: wrong to=${n}`);if(Lt(e,n)>32)throw new Error(`convertRadix2: carry overflow from=${e} to=${n} carryBits=${Lt(e,n)}`);let o=0,s=0,i=Ht[e],c=Ht[n]-1,a=[];for(let l of t){if(gn(l),l>=i)throw new Error(`convertRadix2: invalid data word=${l} from=${e}`);if(o=o<<e|l,s+e>32)throw new Error(`convertRadix2: carry overflow pos=${s} from=${e}`);for(s+=e;s>=n;s-=n)a.push((o>>s-n&c)>>>0);let u=Ht[s];if(u===void 0)throw new Error(\"invalid carry\");o&=u-1}if(o=o<<n-s&c,!r&&s>=e)throw new Error(\"Excess padding\");if(!r&&o>0)throw new Error(`Non-zero padding: ${o}`);return r&&s>0&&a.push(o>>>0),a}function no(t,e=!1){if(gn(t),t<=0||t>32)throw new Error(\"radix2: bits should be in (0..32]\");if(Lt(8,t)>32||Lt(t,8)>32)throw new Error(\"radix2: carry overflow\");return{encode:n=>{if(!xn(n))throw new Error(\"radix2.encode input should be Uint8Array\");return pn(Array.from(n),8,t,!e)},decode:n=>(Jr(\"radix2.decode\",n),Uint8Array.from(pn(n,t,8,e)))}}function zr(t){return ys(t),function(...e){try{return t.apply(null,e)}catch{}}}var gs=typeof Uint8Array.from([]).toBase64==\"function\"&&typeof Uint8Array.fromBase64==\"function\",ws=(t,e)=>{Ie(\"base64\",t);let n=e?/^[A-Za-z0-9=_-]+$/:/^[A-Za-z0-9=+/]+$/,r=e?\"base64url\":\"base64\";if(t.length>0&&!n.test(t))throw new Error(\"invalid base64\");return Uint8Array.fromBase64(t,{alphabet:r,lastChunkHandling:\"strict\"})},wn=gs?{encode(t){return ps(t),t.toBase64()},decode(t){return ws(t,!1)}}:Xr(no(6),Qr(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"),xs(6),eo(\"\"));var yn=Xr(Qr(\"qpzry9x8gf2tvdw0s3jn54khce6mua7l\"),eo(\"\")),Zr=[996825010,642813549,513874426,1027748829,705979059];function tt(t){let e=t>>25,n=(t&33554431)<<5;for(let r=0;r<Zr.length;r++)(e>>r&1)===1&&(n^=Zr[r]);return n}function Fr(t,e,n=1){let r=t.length,o=1;for(let s=0;s<r;s++){let i=t.charCodeAt(s);if(i<33||i>126)throw new Error(`Invalid prefix (${t})`);o=tt(o)^i>>5}o=tt(o);for(let s=0;s<r;s++)o=tt(o)^t.charCodeAt(s)&31;for(let s of e)o=tt(o)^s;for(let s=0;s<6;s++)o=tt(o);return o^=n,yn.encode(pn([o%Ht[30]],30,5,!1))}function ms(t){let e=t===\"bech32\"?1:734539939,n=no(5),r=n.decode,o=n.encode,s=zr(r);function i(d,h,b=90){Ie(\"bech32.encode prefix\",d),xn(h)&&(h=Array.from(h)),Jr(\"bech32.encode\",h);let x=d.length;if(x===0)throw new TypeError(`Invalid prefix length ${x}`);let w=x+7+h.length;if(b!==!1&&w>b)throw new TypeError(`Length ${w} exceeds limit ${b}`);let E=d.toLowerCase(),B=Fr(E,h,e);return`${E}1${yn.encode(h)}${B}`}function c(d,h=90){Ie(\"bech32.decode input\",d);let b=d.length;if(b<8||h!==!1&&b>h)throw new TypeError(`invalid string length: ${b} (${d}). Expected (8..${h})`);let x=d.toLowerCase();if(d!==x&&d!==d.toUpperCase())throw new Error(\"String must be lowercase or uppercase\");let w=x.lastIndexOf(\"1\");if(w===0||w===-1)throw new Error('Letter \"1\" must be present between prefix and data only');let E=x.slice(0,w),B=x.slice(w+1);if(B.length<6)throw new Error(\"Data must be at least 6 characters long\");let k=yn.decode(B).slice(0,-6),U=Fr(E,k,e);if(!B.endsWith(U))throw new Error(`Invalid checksum in ${d}: expected \"${U}\"`);return{prefix:E,words:k}}let a=zr(c);function l(d){let{prefix:h,words:b}=c(d,!1);return{prefix:h,words:b,bytes:r(b)}}function u(d,h){return i(d,o(h))}return{encode:i,decode:c,encodeFromBytes:u,decodeToBytes:l,decodeUnsafe:a,fromWords:r,fromWordsUnsafe:s,toWords:o}}var mn=ms(\"bech32\");var Es=new TextDecoder(\"utf-8\"),An=new TextEncoder;function Ke(t){try{t.indexOf(\"://\")===-1&&(t=\"wss://\"+t);let e=new URL(t);return e.protocol===\"http:\"?e.protocol=\"ws:\":e.protocol===\"https:\"&&(e.protocol=\"wss:\"),e.pathname=e.pathname.replace(/\\/+/g,\"/\"),e.pathname.endsWith(\"/\")&&(e.pathname=e.pathname.slice(0,-1)),(e.port===\"80\"&&e.protocol===\"ws:\"||e.port===\"443\"&&e.protocol===\"wss:\")&&(e.port=\"\"),e.searchParams.sort(),e.hash=\"\",e.toString()}catch{throw new Error(`Invalid URL: ${t}`)}}function vs(t){if(t.length!==64)return!1;for(let e=0;e<64;e++){let n=t.charCodeAt(e);if(isNaN(n)||n<48||n>102||n>57&&n<97)return!1}return!0}var We=Symbol(\"verified\"),Ss=t=>t instanceof Object;function Bs(t){if(!Ss(t)||typeof t.kind!=\"number\"||typeof t.content!=\"string\"||typeof t.created_at!=\"number\"||typeof t.pubkey!=\"string\"||!vs(t.pubkey)||!Array.isArray(t.tags))return!1;for(let e=0;e<t.tags.length;e++){let n=t.tags[e];if(!Array.isArray(n))return!1;for(let r=0;r<n.length;r++)if(typeof n[r]!=\"string\")return!1}return!0}var As=class{generateSecretKey(){return Y.utils.randomSecretKey()}getPublicKey(t){return te(Y.getPublicKey(t))}finalizeEvent(t,e){let n=t;return n.pubkey=te(Y.getPublicKey(e)),n.id=En(n),n.sig=te(Y.sign(F(En(n)),e)),n[We]=!0,n}verifyEvent(t){if(typeof t[We]==\"boolean\")return t[We];try{let e=En(t);if(e!==t.id)return t[We]=!1,!1;let n=Y.verify(F(t.sig),F(e),F(t.pubkey));return t[We]=n,n}catch{return t[We]=!1,!1}}};function Ts(t){if(!Bs(t))throw new Error(\"can't serialize event with wrong or missing properties\");return JSON.stringify([0,t.pubkey,t.created_at,t.kind,t.tags,t.content])}function En(t){let e=ue(An.encode(Ts(t)));return te(e)}var kt=new As,oa=kt.generateSecretKey,ro=kt.getPublicKey,Hs=kt.finalizeEvent,so=kt.verifyEvent,Tn=1,Hn=4294967295,Rn=65536;function vn(t,e){let n=xr.getSharedSecret(t,F(\"02\"+e)).subarray(1,33);return jr(ue,n,An.encode(\"nip44-v2\"))}function io(t,e){let n=Gr(ue,t,e,76);return{chacha_key:n.subarray(0,32),chacha_nonce:n.subarray(32,44),hmac_key:n.subarray(44,76)}}function co(t){if(!Number.isSafeInteger(t)||t<1)throw new Error(\"expected positive integer\");if(t<=32)return 32;let e=2**(Math.floor(Math.log2(t-1))+1),n=e<=256?32:e/8;return n*(Math.floor((t-1)/n)+1)}function Rs(t){if(!Number.isSafeInteger(t)||t<Tn||t>65535)throw new Error(\"invalid plaintext size: must be between 1 and 65535 bytes\");let e=new Uint8Array(2);return new DataView(e.buffer).setUint16(0,t,!1),e}function Ls(t){if(!Number.isSafeInteger(t)||t<Rn||t>Hn)throw new Error(\"invalid plaintext size: must be between 65536 and 4294967295 bytes\");let e=new Uint8Array(4);return new DataView(e.buffer).setUint32(0,t,!1),e}function Os(t){let e=An.encode(t),n=e.length;if(n<Tn||n>Hn)throw new Error(\"invalid plaintext size: must be between 1 and 4294967295 bytes\");let r=n>=Rn?De(new Uint8Array([0,0]),Ls(n)):Rs(n),o=new Uint8Array(co(n)-n);return De(r,e,o)}function ks(t){let e=new DataView(t.buffer,t.byteOffset,t.byteLength),n=e.getUint16(0),r,o;if(n===0){if(r=e.getUint32(2),r<Rn)throw new Error(\"invalid padding\");o=6}else r=n,o=2;let s=t.subarray(o,o+r);if(r<Tn||r>Hn||s.length!==r||t.length!==o+co(r))throw new Error(\"invalid padding\");return Es.decode(s)}function ao(t,e,n){if(n.length!==32)throw new Error(\"AAD associated data must be 32 bytes\");let r=De(n,e);return Ve(ue,t,r)}function _s(t){if(typeof t!=\"string\")throw new Error(\"payload must be a valid string\");let e=t.length;if(e<132)throw new Error(\"invalid payload length: \"+e);if(t[0]===\"#\")throw new Error(\"unknown encryption version\");let n;try{n=wn.decode(t)}catch(s){throw new Error(\"invalid base64: \"+s.message)}let r=n.length;if(r<99)throw new Error(\"invalid data length: \"+r);let o=n[0];if(o!==2)throw new Error(\"unknown encryption version \"+o);return{nonce:n.subarray(1,33),ciphertext:n.subarray(33,-32),mac:n.subarray(-32)}}function Is(t,e,n=Ar(32)){let{chacha_key:r,chacha_nonce:o,hmac_key:s}=io(e,n),i=Os(t),c=At(r,o,i),a=ao(s,c,n);return wn.encode(De(new Uint8Array([2]),n,c,a))}function oo(t,e){let{nonce:n,ciphertext:r,mac:o}=_s(t),{chacha_key:s,chacha_nonce:i,hmac_key:c}=io(e,n),a=ao(c,r,n);if(!St(a,o))throw new Error(\"invalid MAC\");let l=At(s,i,r);return ks(l)}var Cs=/^(?:([\\w.+-]+)@)?([\\w_-]+(\\.[\\w_-]+)+)$/,Us;try{Us=fetch}catch{}var Ps=22242,Sn=24133;function Ns(t,e){if(t.ids&&t.ids.indexOf(e.id)===-1||t.kinds&&t.kinds.indexOf(e.kind)===-1||t.authors&&t.authors.indexOf(e.pubkey)===-1)return!1;for(let n in t)if(n[0]===\"#\"){let r=n.slice(1),o=t[`#${r}`];if(o&&!e.tags.find(([s,i])=>s===n.slice(1)&&o.indexOf(i)!==-1))return!1}return!(t.since&&e.created_at<t.since||t.until&&e.created_at>t.until)}function qs(t,e){for(let n=0;n<t.length;n++)if(Ns(t[n],e))return!0;return!1}function Ds(t,e){let n=e.length+3,r=t.indexOf(`\"${e}\":`)+n,o=t.slice(r).indexOf('\"')+r+1;return t.slice(o,o+64)}function $s(t){let e=t.slice(0,22).indexOf('\"EVENT\"');if(e===-1)return null;let n=t.slice(e+7+1).indexOf('\"');if(n===-1)return null;let r=e+7+1+n,o=t.slice(r+1,80).indexOf('\"');if(o===-1)return null;let s=r+1+o;return t.slice(r+1,s)}function Ms(t,e){return{kind:Ps,created_at:Math.floor(Date.now()/1e3),tags:[[\"relay\",t],[\"challenge\",e]],content:\"\"}}var Bn=class extends Error{constructor(t,e){super(`Tried to send message '${t} on a closed connection to ${e}.`),this.name=\"SendingOnClosedConnection\"}},fo=class{constructor(t,e){f(this,\"url\");f(this,\"_connected\",!1);f(this,\"onclose\",null);f(this,\"onnotice\",t=>console.debug(`NOTICE from ${this.url}: ${t}`));f(this,\"onauth\");f(this,\"baseEoseTimeout\",4400);f(this,\"publishTimeout\",4400);f(this,\"pingFrequency\",29e3);f(this,\"pingTimeout\",2e4);f(this,\"resubscribeBackoff\",[1e4,1e4,1e4,2e4,2e4,3e4,6e4]);f(this,\"openSubs\",new Map);f(this,\"enablePing\");f(this,\"enableReconnect\");f(this,\"idleTimeout\",0);f(this,\"idleSince\",Date.now());f(this,\"ongoingOperations\",0);f(this,\"reconnectTimeoutHandle\");f(this,\"pingIntervalHandle\");f(this,\"reconnectAttempts\",0);f(this,\"skipReconnection\",!1);f(this,\"idleTimeoutHandle\");f(this,\"connectionPromise\");f(this,\"openCountRequests\",new Map);f(this,\"openEventPublishes\",new Map);f(this,\"ws\");f(this,\"challenge\");f(this,\"authPromise\");f(this,\"serial\",0);f(this,\"verifyEvent\");f(this,\"_WebSocket\");this.url=Ke(t),this.verifyEvent=e.verifyEvent,this._WebSocket=e.websocketImplementation||WebSocket,this.enablePing=e.enablePing,this.enableReconnect=e.enableReconnect||!1,e.idleTimeout&&(this.idleTimeout=e.idleTimeout)}static async connect(t,e){let n=new fo(t,e);return await n.connect(e),n}closeAllSubscriptions(t){for(let[e,n]of this.openSubs)n.close(t);this.openSubs.clear();for(let[e,n]of this.openEventPublishes)n.reject(new Error(t));this.openEventPublishes.clear();for(let[e,n]of this.openCountRequests)n.reject(new Error(t));this.openCountRequests.clear()}get connected(){return this._connected}clearIdleTimeout(){this.idleTimeoutHandle&&(clearTimeout(this.idleTimeoutHandle),this.idleTimeoutHandle=void 0)}scheduleIdleClose(){this.clearIdleTimeout(),this.idleTimeout>0&&(this.idleTimeoutHandle=setTimeout(()=>{this.ongoingOperations===0&&this.idleSince&&this.close()},this.idleTimeout))}async reconnect(){let t=this.resubscribeBackoff[Math.min(this.reconnectAttempts,this.resubscribeBackoff.length-1)];this.reconnectAttempts++,this.reconnectTimeoutHandle=setTimeout(async()=>{try{await this.connect()}catch{}},t)}handleHardClose(t){this.ws&&(this.ws.onopen=null,this.ws.onerror=null,this.ws.onclose=null),this.pingIntervalHandle&&(clearInterval(this.pingIntervalHandle),this.pingIntervalHandle=void 0),this._connected=!1,this.connectionPromise=void 0,this.idleSince=void 0,this.clearIdleTimeout(),this.enableReconnect&&!this.skipReconnection?this.reconnect():(this.onclose?.(),this.closeAllSubscriptions(t))}async connect(t){let e;return this.connectionPromise?this.connectionPromise:(this.challenge=void 0,this.authPromise=void 0,this.skipReconnection=!1,this.connectionPromise=new Promise((n,r)=>{t?.timeout&&(e=setTimeout(()=>{r(\"connection timed out\"),this.connectionPromise=void 0,this.reconnectAttempts===0&&(this.skipReconnection=!0),this.handleHardClose(\"relay connection timed out\")},t.timeout)),t?.abort&&(t.abort.onabort=r);try{this.ws=new this._WebSocket(this.url)}catch(o){clearTimeout(e),r(o);return}this.ws.onopen=()=>{this.reconnectTimeoutHandle&&(clearTimeout(this.reconnectTimeoutHandle),this.reconnectTimeoutHandle=void 0),clearTimeout(e),this._connected=!0;let o=this.reconnectAttempts>0;this.reconnectAttempts=0;for(let s of this.openSubs.values()){if(s.eosed=!1,o)for(let i=0;i<s.filters.length;i++)s.lastEmitted&&(s.filters[i].since=s.lastEmitted+1);s.fire()}this.enablePing&&(this.pingIntervalHandle=setInterval(()=>this.pingpong(),this.pingFrequency)),n()},this.ws.onerror=()=>{clearTimeout(e),r(\"connection failed\"),this.connectionPromise=void 0,this.reconnectAttempts===0&&(this.skipReconnection=!0),this.handleHardClose(\"relay connection failed\")},this.ws.onclose=o=>{clearTimeout(e),r(o.message||\"websocket closed\"),this.handleHardClose(\"relay connection closed\")},this.ws.onmessage=this._onmessage.bind(this)}),this.connectionPromise)}waitForPingPong(){return new Promise(t=>{this.ws.once(\"pong\",()=>t(!0)),this.ws.ping()})}waitForDummyReq(){return new Promise((t,e)=>{if(!this.connectionPromise)return e(new Error(`no connection to ${this.url}, can't ping`));try{let n=this.subscribe([{ids:[\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"],limit:0}],{label:\"<forced-ping>\",oneose:()=>{t(!0),n.close()},onclose(){t(!0)},eoseTimeout:this.pingTimeout+1e3})}catch(n){e(n)}})}async pingpong(){this.ws?.readyState===1&&(await Promise.any([this.ws&&this.ws.ping&&this.ws.once?this.waitForPingPong():this.waitForDummyReq(),new Promise(e=>setTimeout(()=>e(!1),this.pingTimeout))])||this.ws?.readyState===this._WebSocket.OPEN&&this.ws?.close())}async send(t){if(!this.connectionPromise)throw new Bn(t,this.url);this.connectionPromise.then(()=>{this.ws?.send(t)})}async auth(t){let e=this.challenge;if(!e)throw new Error(\"can't perform auth, no challenge was received\");return this.authPromise?this.authPromise:(this.authPromise=new Promise(async(n,r)=>{try{let o=await t(Ms(this.url,e)),s=setTimeout(()=>{let i=this.openEventPublishes.get(o.id);i&&(i.reject(new Error(\"auth timed out\")),this.openEventPublishes.delete(o.id))},this.publishTimeout);this.openEventPublishes.set(o.id,{resolve:n,reject:r,timeout:s}),this.send('[\"AUTH\",'+JSON.stringify(o)+\"]\")}catch(o){console.warn(\"subscribe auth function failed:\",o)}}),this.authPromise)}async publish(t){this.idleSince=void 0,this.clearIdleTimeout(),this.ongoingOperations++;let e=new Promise((n,r)=>{let o=setTimeout(()=>{let s=this.openEventPublishes.get(t.id);s&&(s.reject(new Error(\"publish timed out\")),this.openEventPublishes.delete(t.id))},this.publishTimeout);this.openEventPublishes.set(t.id,{resolve:n,reject:r,timeout:o})});try{await this.send('[\"EVENT\",'+JSON.stringify(t)+\"]\")}catch(n){let r=this.openEventPublishes.get(t.id);r&&(r.reject(n),this.openEventPublishes.delete(t.id))}return this.ongoingOperations--,this.ongoingOperations===0&&(this.idleSince=Date.now(),this.scheduleIdleClose()),e}async count(t,e){return(await this.countWithHLL(t,e)).count}async countWithHLL(t,e){this.serial++;let n=e?.id||\"count:\"+this.serial,r=new Promise((o,s)=>{this.openCountRequests.set(n,{resolve:o,reject:s})});try{await this.send('[\"COUNT\",\"'+n+'\",'+JSON.stringify(t).substring(1))}catch(o){let s=this.openCountRequests.get(n);s&&(s.reject(o),this.openCountRequests.delete(n))}return r}subscribe(t,e){e.label!==\"<forced-ping>\"&&(this.idleSince=void 0,this.clearIdleTimeout(),this.ongoingOperations++);let n=this.prepareSubscription(t,e);return n.fire(),e.abort&&(e.abort.onabort=()=>n.close(String(e.abort.reason||\"<aborted>\"))),n}prepareSubscription(t,e){this.serial++;let n=e.id||(e.label?e.label+\":\":\"sub:\")+this.serial,r=new Vs(this,n,t,e);return this.openSubs.set(n,r),r}close(){this.skipReconnection=!0,this.reconnectTimeoutHandle&&(clearTimeout(this.reconnectTimeoutHandle),this.reconnectTimeoutHandle=void 0),this.pingIntervalHandle&&(clearInterval(this.pingIntervalHandle),this.pingIntervalHandle=void 0),this.closeAllSubscriptions(\"relay connection closed by us\"),this._connected=!1,this.connectionPromise=void 0,this.idleSince=void 0,this.clearIdleTimeout(),this.onclose?.(),this.ws&&(this.ws.onopen=null,this.ws.onerror=null,this.ws.onclose=null,this.ws.readyState!==this._WebSocket.CLOSING&&this.ws.readyState!==this._WebSocket.CLOSED&&this.ws.close())}_onmessage(t){let e=t.data;if(!e)return;let n=$s(e);if(n){let r=this.openSubs.get(n);if(!r)return;let o=Ds(e,\"id\"),s=r.alreadyHaveEvent?.(o);if(r.receivedEvent?.(this,o),s)return}try{let r=JSON.parse(e);switch(r[0]){case\"EVENT\":{let o=this.openSubs.get(r[1]),s=r[2];qs(o.filters,s)&&this.verifyEvent(s,this.url)?o.onevent(s):o.oninvalidevent?.(s),(!o.lastEmitted||o.lastEmitted<s.created_at)&&(o.lastEmitted=s.created_at);return}case\"COUNT\":{let o=r[1],s=r[2],i=this.openCountRequests.get(o);i&&(i.resolve(s),this.openCountRequests.delete(o));return}case\"EOSE\":{let o=this.openSubs.get(r[1]);if(!o)return;o.receivedEose();return}case\"OK\":{let o=r[1],s=r[2],i=r[3],c=this.openEventPublishes.get(o);c&&(clearTimeout(c.timeout),s?c.resolve(i):c.reject(new Error(i)),this.openEventPublishes.delete(o));return}case\"CLOSED\":{let o=r[1],s=this.openSubs.get(o);if(!s){let i=this.openCountRequests.get(o);i&&(i.reject(new Error(r[2])),this.openCountRequests.delete(o));return}s.closed=!0,s.close(r[2]);return}case\"NOTICE\":{this.onnotice(r[1]);return}case\"AUTH\":{this.challenge=r[1],this.onauth&&this.auth(this.onauth).catch(o=>{if(!(o instanceof Bn))throw o});return}default:{this.openSubs.get(r[1])?.oncustom?.(r);return}}}catch(r){try{let[o,s,i]=JSON.parse(e);console.warn(`[nostr] relay ${this.url} error processing message:`,r,i)}catch{console.warn(`[nostr] relay ${this.url} error processing message:`,r)}return}}},Vs=class{constructor(t,e,n,r){f(this,\"relay\");f(this,\"id\");f(this,\"lastEmitted\");f(this,\"closed\",!1);f(this,\"eosed\",!1);f(this,\"filters\");f(this,\"alreadyHaveEvent\");f(this,\"receivedEvent\");f(this,\"onevent\");f(this,\"oninvalidevent\");f(this,\"oneose\");f(this,\"onclose\");f(this,\"oncustom\");f(this,\"eoseTimeout\");f(this,\"eoseTimeoutHandle\");if(n.length===0)throw new Error(\"subscription can't be created with zero filters\");this.relay=t,this.filters=n,this.id=e,this.alreadyHaveEvent=r.alreadyHaveEvent,this.receivedEvent=r.receivedEvent,this.eoseTimeout=r.eoseTimeout||t.baseEoseTimeout,this.oneose=r.oneose,this.onclose=r.onclose,this.oninvalidevent=r.oninvalidevent,this.onevent=r.onevent||(o=>{console.warn(`onevent() callback not defined for subscription '${this.id}' in relay ${this.relay.url}. event received:`,o)})}fire(){this.relay.send('[\"REQ\",\"'+this.id+'\",'+JSON.stringify(this.filters).substring(1)),this.eoseTimeoutHandle=setTimeout(this.receivedEose.bind(this),this.eoseTimeout)}receivedEose(){this.eosed||(clearTimeout(this.eoseTimeoutHandle),this.eosed=!0,this.oneose?.())}close(t=\"closed by caller\"){if(!this.closed&&this.relay.connected){try{this.relay.send('[\"CLOSE\",'+JSON.stringify(this.id)+\"]\")}catch(e){if(!(e instanceof Bn))throw e}this.closed=!0}this.relay.openSubs.delete(this.id),this.id.startsWith(\"<forced-ping>\")||(this.relay.ongoingOperations--,this.relay.ongoingOperations===0&&(this.relay.idleSince=Date.now(),this.relay.scheduleIdleClose())),this.onclose?.(t)}},me=256,Ws=me*2,da=new TextEncoder;function Ks(t,e){switch(e){case\"reactions\":return{\"#e\":[t],kinds:[7]};case\"reposts\":return{\"#e\":[t],kinds:[6]};case\"quotes\":return{\"#q\":[t],kinds:[1,1111]};case\"replies\":return{\"#e\":[t],kinds:[1]};case\"comments\":return{\"#E\":[t],kinds:[1111]};case\"followers\":return{\"#p\":[t],kinds:[3]}}}function js(){return new Uint8Array(me)}function Gs(t){if(t.length!==Ws||!/^[0-9a-f]+$/.test(t))return;let e=new Uint8Array(me);for(let n=0;n<me;n++)e[n]=parseInt(t.slice(n*2,n*2+2),16);return e}function zs(t){if(t.length!==me)throw new Error(`invalid number of registers ${t.length}`);let e=\"\";for(let n=0;n<me;n++)e+=t[n].toString(16).padStart(2,\"0\");return e}function Zs(t,e){if(t.length===0&&(t=js()),t.length!==me)throw new Error(`invalid number of registers ${t.length}`);if(e.length!==me)throw new Error(`invalid number of registers ${e.length}`);for(let n=0;n<me;n++)e[n]>t[n]&&(t[n]=e[n]);return t}var Fs=class{constructor(t){f(this,\"relays\",new Map);f(this,\"seenOn\",new Map);f(this,\"trackRelays\",!1);f(this,\"verifyEvent\");f(this,\"enablePing\");f(this,\"enableReconnect\");f(this,\"idleTimeout\",2e4);f(this,\"automaticallyAuth\");f(this,\"onRelayConnectionFailure\");f(this,\"onRelayConnectionSuccess\");f(this,\"allowConnectingToRelay\");f(this,\"maxWaitForConnection\");f(this,\"_WebSocket\");this.verifyEvent=t.verifyEvent,this._WebSocket=t.websocketImplementation,this.enablePing=t.enablePing,this.enableReconnect=t.enableReconnect||!1,t.idleTimeout&&(this.idleTimeout=t.idleTimeout),this.automaticallyAuth=t.automaticallyAuth,this.onRelayConnectionFailure=t.onRelayConnectionFailure,this.onRelayConnectionSuccess=t.onRelayConnectionSuccess,this.allowConnectingToRelay=t.allowConnectingToRelay,this.maxWaitForConnection=t.maxWaitForConnection||3e3}async ensureRelay(t,e){t=Ke(t);let n=this.relays.get(t);if(n||(n=new fo(t,{verifyEvent:this.verifyEvent,websocketImplementation:this._WebSocket,enablePing:this.enablePing,enableReconnect:this.enableReconnect,idleTimeout:this.idleTimeout}),n.onclose=()=>{this.relays.delete(t)},this.relays.set(t,n)),this.automaticallyAuth){let r=this.automaticallyAuth(t);r&&(n.onauth=r)}try{await n.connect({timeout:e?.connectionTimeout,abort:e?.abort})}catch(r){throw this.relays.delete(t),r}return n}close(t){t.map(Ke).forEach(e=>{this.relays.get(e)?.close(),this.relays.delete(e)})}subscribe(t,e,n){let r=[],o=[];for(let s=0;s<t.length;s++){let i=Ke(t[s]);r.find(c=>c.url===i)||o.indexOf(i)===-1&&(o.push(i),r.push({url:i,filter:e}))}return this.subscribeMap(r,n)}subscribeMany(t,e,n){return this.subscribe(t,e,n)}subscribeMap(t,e){let n=new Map;for(let h of t){let{url:b,filter:x}=h;n.has(b)||n.set(b,[]),n.get(b).push(x)}let r=Array.from(n.entries()).map(([h,b])=>({url:h,filters:b}));this.trackRelays&&(e.receivedEvent=(h,b)=>{let x=this.seenOn.get(b);x||(x=new Set,this.seenOn.set(b,x)),x.add(h)});let o=new Set,s=[],i=[],c=h=>{i[h]||(i[h]=!0,i.filter(b=>b).length===r.length&&(e.oneose?.(),c=()=>{}))},a=[],l=(h,b,x)=>{a[h]||(c(h),a[h]={url:b,reason:x},a.filter(w=>w).length===r.length&&(e.onclose?.(a),l=()=>{}))},u=h=>{if(e.alreadyHaveEvent?.(h))return!0;let b=o.has(h);return o.add(h),b},d=Promise.all(r.map(async({url:h,filters:b},x)=>{if(this.allowConnectingToRelay?.(h,[\"read\",b])===!1){l(x,h,\"connection skipped by allowConnectingToRelay\");return}let w;try{w=await this.ensureRelay(h,{connectionTimeout:this.maxWaitForConnection<(e.maxWait||0)?Math.max(e.maxWait*.8,e.maxWait-1e3):this.maxWaitForConnection,abort:e.abort})}catch(B){this.onRelayConnectionFailure?.(h),l(x,h,B?.message||String(B));return}this.onRelayConnectionSuccess?.(h);let E=w.subscribe(b,{...e,oneose:()=>c(x),onclose:B=>{B.startsWith(\"auth-required: \")&&e.onauth?w.auth(e.onauth).then(()=>{w.subscribe(b,{...e,oneose:()=>c(x),onclose:k=>{l(x,h,k)},alreadyHaveEvent:u,eoseTimeout:e.maxWait,abort:e.abort})}).catch(k=>{l(x,h,`auth was required and attempted, but failed with: ${k}`)}):l(x,h,B)},alreadyHaveEvent:u,eoseTimeout:e.maxWait,abort:e.abort});s.push(E)}));return{async close(h){await d,s.forEach(b=>{b.close(h)})}}}subscribeEose(t,e,n){let r;return r=this.subscribe(t,e,{...n,oneose(){let o=\"closed automatically on eose\";r?r.close(o):n.onclose?.(t.map(s=>({url:s,reason:o})))}}),r}subscribeManyEose(t,e,n){return this.subscribeEose(t,e,n)}async querySync(t,e,n){return new Promise(async r=>{let o=[];this.subscribeEose(t,e,{...n,onevent(s){o.push(s)},onclose(s){r(o)}})})}async get(t,e,n){e.limit=1;let r=await this.querySync(t,e,n);return r.sort((o,s)=>s.created_at-o.created_at),r[0]||null}async countMany(t,e,n,r){let o=Ks(e,n),s=[];for(let l=0;l<t.length;l++){let u=Ke(t[l]);s.indexOf(u)===-1&&s.push(u)}let i=await Promise.all(s.map(async l=>{if(this.allowConnectingToRelay?.(l,[\"read\",[o]])===!1)return null;let u;try{u=await this.ensureRelay(l,{connectionTimeout:this.maxWaitForConnection<(r?.maxWait||0)?Math.max(r.maxWait*.8,r.maxWait-1e3):this.maxWaitForConnection,abort:r?.abort})}catch{return this.onRelayConnectionFailure?.(l),null}return this.onRelayConnectionSuccess?.(l),u.countWithHLL([o],{id:r?.id}).catch(()=>null)})),c=0,a;for(let l of i){if(!l||(l.count>c&&(c=l.count),!l.hll||l.hll.length!==512))continue;let u=Gs(l.hll);u&&(a=Zs(a||new Uint8Array(0),u))}return a?{count:c,hll:zs(a)}:{count:c}}publish(t,e,n){return t.map(Ke).map(async(r,o,s)=>{if(s.indexOf(r)!==o)return Promise.reject(\"duplicate url\");if(this.allowConnectingToRelay?.(r,[\"write\",e])===!1)return Promise.reject(\"connection skipped by allowConnectingToRelay\");let i;try{i=await this.ensureRelay(r,{connectionTimeout:this.maxWaitForConnection<(n?.maxWait||0)?Math.max(n.maxWait*.8,n.maxWait-1e3):this.maxWaitForConnection,abort:n?.abort})}catch(c){return this.onRelayConnectionFailure?.(r),Promise.reject(\"connection failure: \"+String(c))}return i.publish(e).catch(async c=>{if(c instanceof Error&&c.message.startsWith(\"auth-required: \")&&n?.onauth)return await i.auth(n.onauth),i.publish(e);throw c}).then(c=>{if(this.trackRelays){let a=this.seenOn.get(e.id);a||(a=new Set,this.seenOn.set(e.id,a)),a.add(i)}return c})})}listConnectionStatus(){let t=new Map;return this.relays.forEach((e,n)=>t.set(n,e.connected)),t}destroy(){this.relays.forEach(t=>t.close()),this.relays=new Map}pruneIdleRelays(t=1e4){let e=[];for(let[n,r]of this.relays)r.idleSince&&Date.now()-r.idleSince>=t&&(this.relays.delete(n),e.push(n),r.close());return e}},lo;try{lo=WebSocket}catch{}var Ys=class extends Fs{constructor(t){super({verifyEvent:so,websocketImplementation:lo,maxWaitForConnection:3e3,...t})}},uo;try{uo=fetch}catch{}var Js=/^bunker:\\/\\/([0-9a-f]{64})\\??([?\\/\\w:.=&%-]*)$/;async function ho(t){let e=t.match(Js);if(e)try{let n=e[1],r=new URLSearchParams(e[2]);return{pubkey:n,relays:r.getAll(\"relay\"),secret:r.get(\"secret\")}}catch{}return Xs(t)}async function Xs(t){let e=t.match(Cs);if(!e)return null;let[n,r=\"_\",o]=e;try{let s=`https://${o}/.well-known/nostr.json?name=${r}`,i=await(await uo(s,{redirect:\"error\"})).json(),c=i.names[r],a=i.nip46[c]||[];return{pubkey:c,relays:a,secret:null}}catch{return null}}function bo(t){let e=new URLSearchParams;return t.relays.forEach(n=>{e.append(\"relay\",n)}),e.append(\"secret\",t.secret),t.perms&&t.perms.length>0&&e.append(\"perms\",t.perms.join(\",\")),t.name&&e.append(\"name\",t.name),t.url&&e.append(\"url\",t.url),t.image&&e.append(\"image\",t.image),`nostrconnect://${t.clientPubkey}?${e.toString()}`}var Ot=class{constructor(t,e){f(this,\"params\");f(this,\"pool\");f(this,\"subCloser\");f(this,\"isOpen\");f(this,\"serial\");f(this,\"idPrefix\");f(this,\"listeners\");f(this,\"waitingForAuth\");f(this,\"secretKey\");f(this,\"conversationKey\");f(this,\"bp\");f(this,\"cachedPubKey\");this.params=e,this.pool=e.pool||new Ys,this.secretKey=t,this.isOpen=!1,this.idPrefix=Math.random().toString(36).substring(7),this.serial=0,this.listeners={},this.waitingForAuth={}}static fromBunker(t,e,n={}){if(e.relays.length===0)throw new Error(\"no relays specified for this bunker\");let r=new Ot(t,n);return r.conversationKey=vn(t,e.pubkey),r.bp=e,r.setupSubscription(),r}static async fromURI(t,e,n={},r=3e5){let o=new URL(e),s=o.searchParams.get(\"secret\");if(!s)throw new Error(\"nostrconnect:// URI has no secret\");let i=new Ot(t,n),c=ro(t);return new Promise((a,l)=>{let u=!1,d=i.pool.subscribe(o.searchParams.getAll(\"relay\"),{kinds:[Sn],\"#p\":[c],limit:0},{onevent:async h=>{try{let b=vn(t,h.pubkey),x=oo(h.content,b);JSON.parse(x).result===s&&(d.close(),i.bp={pubkey:h.pubkey,relays:o.searchParams.getAll(\"relay\"),secret:s},i.conversationKey=vn(t,h.pubkey),i.setupSubscription(),u=!0,n.skipSwitchRelays||await Promise.race([new Promise(E=>setTimeout(E,1e3)),i.switchRelays()]),a(i))}catch(b){console.warn(\"failed to process potential connection event\",b)}},onclose:()=>{u||l(new Error(\"subscription closed before connection was established.\"))},maxWait:typeof r==\"number\"?r:void 0,abort:typeof r!=\"number\"?r:void 0})})}setupSubscription(){let t=this.listeners,e=this.waitingForAuth,n=this.conversationKey;this.subCloser=this.pool.subscribe(this.bp.relays,{kinds:[Sn],authors:[this.bp.pubkey],\"#p\":[ro(this.secretKey)],limit:0},{onevent:async r=>{let o=JSON.parse(oo(r.content,n)),{id:s,result:i,error:c}=o;if(i===\"auth_url\"&&e[s]){delete e[s],this.params.onauth?this.params.onauth(c):console.warn(`nostr-tools/nip46: remote signer ${this.bp.pubkey} tried to send an \"auth_url\"='${c}' but there was no onauth() callback configured.`);return}let a=t[s];a&&(c?a.reject(c):i&&a.resolve(i),delete t[s])},onclose:()=>{this.subCloser=void 0}}),this.isOpen=!0}async switchRelays(){try{let t=await this.sendRequest(\"switch_relays\",[]),e=JSON.parse(t);if(!e||JSON.stringify(e.sort())===JSON.stringify(this.bp.relays))return!1;this.bp.relays=e;let n=this.subCloser;return setTimeout(()=>{n.close()},5e3),this.subCloser=void 0,this.setupSubscription(),!0}catch{return!1}}async close(){this.isOpen=!1,this.subCloser?.close(),this.subCloser=void 0}async sendRequest(t,e){return new Promise(async(n,r)=>{try{if(!this.isOpen)throw new Error(\"this signer is not open anymore, create a new one\");this.subCloser||this.setupSubscription(),this.serial++;let o=`${this.idPrefix}-${this.serial}`,s=Is(JSON.stringify({id:o,method:t,params:e}),this.conversationKey),i=Hs({kind:Sn,tags:[[\"p\",this.bp.pubkey]],content:s,created_at:Math.floor(Date.now()/1e3)},this.secretKey);this.listeners[o]={resolve:n,reject:r},this.waitingForAuth[o]=!0,await Promise.any(this.pool.publish(this.bp.relays,i))}catch(o){r(o)}})}async ping(){let t=await this.sendRequest(\"ping\",[]);if(t!==\"pong\")throw new Error(`result is not pong: ${t}`)}async connect(t){let e=[this.bp.pubkey,this.bp.secret||\"\"];t&&(e.push(\"\"),e.push(JSON.stringify(t))),await this.sendRequest(\"connect\",e)}async logout(){let t=await this.sendRequest(\"logout\",[]);if(t!==\"ack\")throw new Error(`result is not ack: ${t}`);await this.close()}async getPublicKey(){return this.cachedPubKey||(this.cachedPubKey=await this.sendRequest(\"get_public_key\",[])),this.cachedPubKey}async signEvent(t){let e=await this.sendRequest(\"sign_event\",[JSON.stringify(t)]),n=JSON.parse(e);if(so(n))return n;throw new Error(`event returned from bunker is improperly signed: ${JSON.stringify(n)}`)}async nip04Encrypt(t,e){return await this.sendRequest(\"nip04_encrypt\",[t,e])}async nip04Decrypt(t,e){return await this.sendRequest(\"nip04_decrypt\",[t,e])}async nip44Encrypt(t,e){return await this.sendRequest(\"nip44_encrypt\",[t,e])}async nip44Decrypt(t,e){return await this.sendRequest(\"nip44_decrypt\",[t,e])}};var Aa=new TextDecoder(\"utf-8\"),Qs=new TextEncoder;function Ge(t){try{t.indexOf(\"://\")===-1&&(t=\"wss://\"+t);let e=new URL(t);return e.protocol===\"http:\"?e.protocol=\"ws:\":e.protocol===\"https:\"&&(e.protocol=\"wss:\"),e.pathname=e.pathname.replace(/\\/+/g,\"/\"),e.pathname.endsWith(\"/\")&&(e.pathname=e.pathname.slice(0,-1)),(e.port===\"80\"&&e.protocol===\"ws:\"||e.port===\"443\"&&e.protocol===\"wss:\")&&(e.port=\"\"),e.searchParams.sort(),e.hash=\"\",e.toString()}catch{throw new Error(`Invalid URL: ${t}`)}}function ei(t){if(t.length!==64)return!1;for(let e=0;e<64;e++){let n=t.charCodeAt(e);if(isNaN(n)||n<48||n>102||n>57&&n<97)return!1}return!0}var je=Symbol(\"verified\"),ti=t=>t instanceof Object;function ni(t){if(!ti(t)||typeof t.kind!=\"number\"||typeof t.content!=\"string\"||typeof t.created_at!=\"number\"||typeof t.pubkey!=\"string\"||!ei(t.pubkey)||!Array.isArray(t.tags))return!1;for(let e=0;e<t.tags.length;e++){let n=t.tags[e];if(!Array.isArray(n))return!1;for(let r=0;r<n.length;r++)if(typeof n[r]!=\"string\")return!1}return!0}var ri=class{generateSecretKey(){return Y.utils.randomSecretKey()}getPublicKey(t){return te(Y.getPublicKey(t))}finalizeEvent(t,e){let n=t;return n.pubkey=te(Y.getPublicKey(e)),n.id=Ln(n),n.sig=te(Y.sign(F(Ln(n)),e)),n[je]=!0,n}verifyEvent(t){if(typeof t[je]==\"boolean\")return t[je];try{let e=Ln(t);if(e!==t.id)return t[je]=!1,!1;let n=Y.verify(F(t.sig),F(e),F(t.pubkey));return t[je]=n,n}catch{return t[je]=!1,!1}}};function oi(t){if(!ni(t))throw new Error(\"can't serialize event with wrong or missing properties\");return JSON.stringify([0,t.pubkey,t.created_at,t.kind,t.tags,t.content])}function Ln(t){let e=ue(Qs.encode(oi(t)));return te(e)}var _t=new ri,Ha=_t.generateSecretKey,Ra=_t.getPublicKey,La=_t.finalizeEvent,si=_t.verifyEvent,ii=22242;function ci(t,e){if(t.ids&&t.ids.indexOf(e.id)===-1||t.kinds&&t.kinds.indexOf(e.kind)===-1||t.authors&&t.authors.indexOf(e.pubkey)===-1)return!1;for(let n in t)if(n[0]===\"#\"){let r=n.slice(1),o=t[`#${r}`];if(o&&!e.tags.find(([s,i])=>s===n.slice(1)&&o.indexOf(i)!==-1))return!1}return!(t.since&&e.created_at<t.since||t.until&&e.created_at>t.until)}function ai(t,e){for(let n=0;n<t.length;n++)if(ci(t[n],e))return!0;return!1}function fi(t,e){let n=e.length+3,r=t.indexOf(`\"${e}\":`)+n,o=t.slice(r).indexOf('\"')+r+1;return t.slice(o,o+64)}function li(t){let e=t.slice(0,22).indexOf('\"EVENT\"');if(e===-1)return null;let n=t.slice(e+7+1).indexOf('\"');if(n===-1)return null;let r=e+7+1+n,o=t.slice(r+1,80).indexOf('\"');if(o===-1)return null;let s=r+1+o;return t.slice(r+1,s)}function ui(t,e){return{kind:ii,created_at:Math.floor(Date.now()/1e3),tags:[[\"relay\",t],[\"challenge\",e]],content:\"\"}}var On=class extends Error{constructor(t,e){super(`Tried to send message '${t} on a closed connection to ${e}.`),this.name=\"SendingOnClosedConnection\"}},po=class{constructor(t,e){f(this,\"url\");f(this,\"_connected\",!1);f(this,\"onclose\",null);f(this,\"onnotice\",t=>console.debug(`NOTICE from ${this.url}: ${t}`));f(this,\"onauth\");f(this,\"baseEoseTimeout\",4400);f(this,\"publishTimeout\",4400);f(this,\"pingFrequency\",29e3);f(this,\"pingTimeout\",2e4);f(this,\"resubscribeBackoff\",[1e4,1e4,1e4,2e4,2e4,3e4,6e4]);f(this,\"openSubs\",new Map);f(this,\"enablePing\");f(this,\"enableReconnect\");f(this,\"idleTimeout\",0);f(this,\"idleSince\",Date.now());f(this,\"ongoingOperations\",0);f(this,\"reconnectTimeoutHandle\");f(this,\"pingIntervalHandle\");f(this,\"reconnectAttempts\",0);f(this,\"skipReconnection\",!1);f(this,\"idleTimeoutHandle\");f(this,\"connectionPromise\");f(this,\"openCountRequests\",new Map);f(this,\"openEventPublishes\",new Map);f(this,\"ws\");f(this,\"challenge\");f(this,\"authPromise\");f(this,\"serial\",0);f(this,\"verifyEvent\");f(this,\"_WebSocket\");this.url=Ge(t),this.verifyEvent=e.verifyEvent,this._WebSocket=e.websocketImplementation||WebSocket,this.enablePing=e.enablePing,this.enableReconnect=e.enableReconnect||!1,e.idleTimeout&&(this.idleTimeout=e.idleTimeout)}static async connect(t,e){let n=new po(t,e);return await n.connect(e),n}closeAllSubscriptions(t){for(let[e,n]of this.openSubs)n.close(t);this.openSubs.clear();for(let[e,n]of this.openEventPublishes)n.reject(new Error(t));this.openEventPublishes.clear();for(let[e,n]of this.openCountRequests)n.reject(new Error(t));this.openCountRequests.clear()}get connected(){return this._connected}clearIdleTimeout(){this.idleTimeoutHandle&&(clearTimeout(this.idleTimeoutHandle),this.idleTimeoutHandle=void 0)}scheduleIdleClose(){this.clearIdleTimeout(),this.idleTimeout>0&&(this.idleTimeoutHandle=setTimeout(()=>{this.ongoingOperations===0&&this.idleSince&&this.close()},this.idleTimeout))}async reconnect(){let t=this.resubscribeBackoff[Math.min(this.reconnectAttempts,this.resubscribeBackoff.length-1)];this.reconnectAttempts++,this.reconnectTimeoutHandle=setTimeout(async()=>{try{await this.connect()}catch{}},t)}handleHardClose(t){this.ws&&(this.ws.onopen=null,this.ws.onerror=null,this.ws.onclose=null),this.pingIntervalHandle&&(clearInterval(this.pingIntervalHandle),this.pingIntervalHandle=void 0),this._connected=!1,this.connectionPromise=void 0,this.idleSince=void 0,this.clearIdleTimeout(),this.enableReconnect&&!this.skipReconnection?this.reconnect():(this.onclose?.(),this.closeAllSubscriptions(t))}async connect(t){let e;return this.connectionPromise?this.connectionPromise:(this.challenge=void 0,this.authPromise=void 0,this.skipReconnection=!1,this.connectionPromise=new Promise((n,r)=>{t?.timeout&&(e=setTimeout(()=>{r(\"connection timed out\"),this.connectionPromise=void 0,this.reconnectAttempts===0&&(this.skipReconnection=!0),this.handleHardClose(\"relay connection timed out\")},t.timeout)),t?.abort&&(t.abort.onabort=r);try{this.ws=new this._WebSocket(this.url)}catch(o){clearTimeout(e),r(o);return}this.ws.onopen=()=>{this.reconnectTimeoutHandle&&(clearTimeout(this.reconnectTimeoutHandle),this.reconnectTimeoutHandle=void 0),clearTimeout(e),this._connected=!0;let o=this.reconnectAttempts>0;this.reconnectAttempts=0;for(let s of this.openSubs.values()){if(s.eosed=!1,o)for(let i=0;i<s.filters.length;i++)s.lastEmitted&&(s.filters[i].since=s.lastEmitted+1);s.fire()}this.enablePing&&(this.pingIntervalHandle=setInterval(()=>this.pingpong(),this.pingFrequency)),n()},this.ws.onerror=()=>{clearTimeout(e),r(\"connection failed\"),this.connectionPromise=void 0,this.reconnectAttempts===0&&(this.skipReconnection=!0),this.handleHardClose(\"relay connection failed\")},this.ws.onclose=o=>{clearTimeout(e),r(o.message||\"websocket closed\"),this.handleHardClose(\"relay connection closed\")},this.ws.onmessage=this._onmessage.bind(this)}),this.connectionPromise)}waitForPingPong(){return new Promise(t=>{this.ws.once(\"pong\",()=>t(!0)),this.ws.ping()})}waitForDummyReq(){return new Promise((t,e)=>{if(!this.connectionPromise)return e(new Error(`no connection to ${this.url}, can't ping`));try{let n=this.subscribe([{ids:[\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"],limit:0}],{label:\"<forced-ping>\",oneose:()=>{t(!0),n.close()},onclose(){t(!0)},eoseTimeout:this.pingTimeout+1e3})}catch(n){e(n)}})}async pingpong(){this.ws?.readyState===1&&(await Promise.any([this.ws&&this.ws.ping&&this.ws.once?this.waitForPingPong():this.waitForDummyReq(),new Promise(e=>setTimeout(()=>e(!1),this.pingTimeout))])||this.ws?.readyState===this._WebSocket.OPEN&&this.ws?.close())}async send(t){if(!this.connectionPromise)throw new On(t,this.url);this.connectionPromise.then(()=>{this.ws?.send(t)})}async auth(t){let e=this.challenge;if(!e)throw new Error(\"can't perform auth, no challenge was received\");return this.authPromise?this.authPromise:(this.authPromise=new Promise(async(n,r)=>{try{let o=await t(ui(this.url,e)),s=setTimeout(()=>{let i=this.openEventPublishes.get(o.id);i&&(i.reject(new Error(\"auth timed out\")),this.openEventPublishes.delete(o.id))},this.publishTimeout);this.openEventPublishes.set(o.id,{resolve:n,reject:r,timeout:s}),this.send('[\"AUTH\",'+JSON.stringify(o)+\"]\")}catch(o){console.warn(\"subscribe auth function failed:\",o)}}),this.authPromise)}async publish(t){this.idleSince=void 0,this.clearIdleTimeout(),this.ongoingOperations++;let e=new Promise((n,r)=>{let o=setTimeout(()=>{let s=this.openEventPublishes.get(t.id);s&&(s.reject(new Error(\"publish timed out\")),this.openEventPublishes.delete(t.id))},this.publishTimeout);this.openEventPublishes.set(t.id,{resolve:n,reject:r,timeout:o})});try{await this.send('[\"EVENT\",'+JSON.stringify(t)+\"]\")}catch(n){let r=this.openEventPublishes.get(t.id);r&&(r.reject(n),this.openEventPublishes.delete(t.id))}return this.ongoingOperations--,this.ongoingOperations===0&&(this.idleSince=Date.now(),this.scheduleIdleClose()),e}async count(t,e){return(await this.countWithHLL(t,e)).count}async countWithHLL(t,e){this.serial++;let n=e?.id||\"count:\"+this.serial,r=new Promise((o,s)=>{this.openCountRequests.set(n,{resolve:o,reject:s})});try{await this.send('[\"COUNT\",\"'+n+'\",'+JSON.stringify(t).substring(1))}catch(o){let s=this.openCountRequests.get(n);s&&(s.reject(o),this.openCountRequests.delete(n))}return r}subscribe(t,e){e.label!==\"<forced-ping>\"&&(this.idleSince=void 0,this.clearIdleTimeout(),this.ongoingOperations++);let n=this.prepareSubscription(t,e);return n.fire(),e.abort&&(e.abort.onabort=()=>n.close(String(e.abort.reason||\"<aborted>\"))),n}prepareSubscription(t,e){this.serial++;let n=e.id||(e.label?e.label+\":\":\"sub:\")+this.serial,r=new hi(this,n,t,e);return this.openSubs.set(n,r),r}close(){this.skipReconnection=!0,this.reconnectTimeoutHandle&&(clearTimeout(this.reconnectTimeoutHandle),this.reconnectTimeoutHandle=void 0),this.pingIntervalHandle&&(clearInterval(this.pingIntervalHandle),this.pingIntervalHandle=void 0),this.closeAllSubscriptions(\"relay connection closed by us\"),this._connected=!1,this.connectionPromise=void 0,this.idleSince=void 0,this.clearIdleTimeout(),this.onclose?.(),this.ws&&(this.ws.onopen=null,this.ws.onerror=null,this.ws.onclose=null,this.ws.readyState!==this._WebSocket.CLOSING&&this.ws.readyState!==this._WebSocket.CLOSED&&this.ws.close())}_onmessage(t){let e=t.data;if(!e)return;let n=li(e);if(n){let r=this.openSubs.get(n);if(!r)return;let o=fi(e,\"id\"),s=r.alreadyHaveEvent?.(o);if(r.receivedEvent?.(this,o),s)return}try{let r=JSON.parse(e);switch(r[0]){case\"EVENT\":{let o=this.openSubs.get(r[1]),s=r[2];ai(o.filters,s)&&this.verifyEvent(s,this.url)?o.onevent(s):o.oninvalidevent?.(s),(!o.lastEmitted||o.lastEmitted<s.created_at)&&(o.lastEmitted=s.created_at);return}case\"COUNT\":{let o=r[1],s=r[2],i=this.openCountRequests.get(o);i&&(i.resolve(s),this.openCountRequests.delete(o));return}case\"EOSE\":{let o=this.openSubs.get(r[1]);if(!o)return;o.receivedEose();return}case\"OK\":{let o=r[1],s=r[2],i=r[3],c=this.openEventPublishes.get(o);c&&(clearTimeout(c.timeout),s?c.resolve(i):c.reject(new Error(i)),this.openEventPublishes.delete(o));return}case\"CLOSED\":{let o=r[1],s=this.openSubs.get(o);if(!s){let i=this.openCountRequests.get(o);i&&(i.reject(new Error(r[2])),this.openCountRequests.delete(o));return}s.closed=!0,s.close(r[2]);return}case\"NOTICE\":{this.onnotice(r[1]);return}case\"AUTH\":{this.challenge=r[1],this.onauth&&this.auth(this.onauth).catch(o=>{if(!(o instanceof On))throw o});return}default:{this.openSubs.get(r[1])?.oncustom?.(r);return}}}catch(r){try{let[o,s,i]=JSON.parse(e);console.warn(`[nostr] relay ${this.url} error processing message:`,r,i)}catch{console.warn(`[nostr] relay ${this.url} error processing message:`,r)}return}}},hi=class{constructor(t,e,n,r){f(this,\"relay\");f(this,\"id\");f(this,\"lastEmitted\");f(this,\"closed\",!1);f(this,\"eosed\",!1);f(this,\"filters\");f(this,\"alreadyHaveEvent\");f(this,\"receivedEvent\");f(this,\"onevent\");f(this,\"oninvalidevent\");f(this,\"oneose\");f(this,\"onclose\");f(this,\"oncustom\");f(this,\"eoseTimeout\");f(this,\"eoseTimeoutHandle\");if(n.length===0)throw new Error(\"subscription can't be created with zero filters\");this.relay=t,this.filters=n,this.id=e,this.alreadyHaveEvent=r.alreadyHaveEvent,this.receivedEvent=r.receivedEvent,this.eoseTimeout=r.eoseTimeout||t.baseEoseTimeout,this.oneose=r.oneose,this.onclose=r.onclose,this.oninvalidevent=r.oninvalidevent,this.onevent=r.onevent||(o=>{console.warn(`onevent() callback not defined for subscription '${this.id}' in relay ${this.relay.url}. event received:`,o)})}fire(){this.relay.send('[\"REQ\",\"'+this.id+'\",'+JSON.stringify(this.filters).substring(1)),this.eoseTimeoutHandle=setTimeout(this.receivedEose.bind(this),this.eoseTimeout)}receivedEose(){this.eosed||(clearTimeout(this.eoseTimeoutHandle),this.eosed=!0,this.oneose?.())}close(t=\"closed by caller\"){if(!this.closed&&this.relay.connected){try{this.relay.send('[\"CLOSE\",'+JSON.stringify(this.id)+\"]\")}catch(e){if(!(e instanceof On))throw e}this.closed=!0}this.relay.openSubs.delete(this.id),this.id.startsWith(\"<forced-ping>\")||(this.relay.ongoingOperations--,this.relay.ongoingOperations===0&&(this.relay.idleSince=Date.now(),this.relay.scheduleIdleClose())),this.onclose?.(t)}},Ee=256,di=Ee*2,Oa=new TextEncoder;function bi(t,e){switch(e){case\"reactions\":return{\"#e\":[t],kinds:[7]};case\"reposts\":return{\"#e\":[t],kinds:[6]};case\"quotes\":return{\"#q\":[t],kinds:[1,1111]};case\"replies\":return{\"#e\":[t],kinds:[1]};case\"comments\":return{\"#E\":[t],kinds:[1111]};case\"followers\":return{\"#p\":[t],kinds:[3]}}}function pi(){return new Uint8Array(Ee)}function yi(t){if(t.length!==di||!/^[0-9a-f]+$/.test(t))return;let e=new Uint8Array(Ee);for(let n=0;n<Ee;n++)e[n]=parseInt(t.slice(n*2,n*2+2),16);return e}function xi(t){if(t.length!==Ee)throw new Error(`invalid number of registers ${t.length}`);let e=\"\";for(let n=0;n<Ee;n++)e+=t[n].toString(16).padStart(2,\"0\");return e}function gi(t,e){if(t.length===0&&(t=pi()),t.length!==Ee)throw new Error(`invalid number of registers ${t.length}`);if(e.length!==Ee)throw new Error(`invalid number of registers ${e.length}`);for(let n=0;n<Ee;n++)e[n]>t[n]&&(t[n]=e[n]);return t}var wi=class{constructor(t){f(this,\"relays\",new Map);f(this,\"seenOn\",new Map);f(this,\"trackRelays\",!1);f(this,\"verifyEvent\");f(this,\"enablePing\");f(this,\"enableReconnect\");f(this,\"idleTimeout\",2e4);f(this,\"automaticallyAuth\");f(this,\"onRelayConnectionFailure\");f(this,\"onRelayConnectionSuccess\");f(this,\"allowConnectingToRelay\");f(this,\"maxWaitForConnection\");f(this,\"_WebSocket\");this.verifyEvent=t.verifyEvent,this._WebSocket=t.websocketImplementation,this.enablePing=t.enablePing,this.enableReconnect=t.enableReconnect||!1,t.idleTimeout&&(this.idleTimeout=t.idleTimeout),this.automaticallyAuth=t.automaticallyAuth,this.onRelayConnectionFailure=t.onRelayConnectionFailure,this.onRelayConnectionSuccess=t.onRelayConnectionSuccess,this.allowConnectingToRelay=t.allowConnectingToRelay,this.maxWaitForConnection=t.maxWaitForConnection||3e3}async ensureRelay(t,e){t=Ge(t);let n=this.relays.get(t);if(n||(n=new po(t,{verifyEvent:this.verifyEvent,websocketImplementation:this._WebSocket,enablePing:this.enablePing,enableReconnect:this.enableReconnect,idleTimeout:this.idleTimeout}),n.onclose=()=>{this.relays.delete(t)},this.relays.set(t,n)),this.automaticallyAuth){let r=this.automaticallyAuth(t);r&&(n.onauth=r)}try{await n.connect({timeout:e?.connectionTimeout,abort:e?.abort})}catch(r){throw this.relays.delete(t),r}return n}close(t){t.map(Ge).forEach(e=>{this.relays.get(e)?.close(),this.relays.delete(e)})}subscribe(t,e,n){let r=[],o=[];for(let s=0;s<t.length;s++){let i=Ge(t[s]);r.find(c=>c.url===i)||o.indexOf(i)===-1&&(o.push(i),r.push({url:i,filter:e}))}return this.subscribeMap(r,n)}subscribeMany(t,e,n){return this.subscribe(t,e,n)}subscribeMap(t,e){let n=new Map;for(let h of t){let{url:b,filter:x}=h;n.has(b)||n.set(b,[]),n.get(b).push(x)}let r=Array.from(n.entries()).map(([h,b])=>({url:h,filters:b}));this.trackRelays&&(e.receivedEvent=(h,b)=>{let x=this.seenOn.get(b);x||(x=new Set,this.seenOn.set(b,x)),x.add(h)});let o=new Set,s=[],i=[],c=h=>{i[h]||(i[h]=!0,i.filter(b=>b).length===r.length&&(e.oneose?.(),c=()=>{}))},a=[],l=(h,b,x)=>{a[h]||(c(h),a[h]={url:b,reason:x},a.filter(w=>w).length===r.length&&(e.onclose?.(a),l=()=>{}))},u=h=>{if(e.alreadyHaveEvent?.(h))return!0;let b=o.has(h);return o.add(h),b},d=Promise.all(r.map(async({url:h,filters:b},x)=>{if(this.allowConnectingToRelay?.(h,[\"read\",b])===!1){l(x,h,\"connection skipped by allowConnectingToRelay\");return}let w;try{w=await this.ensureRelay(h,{connectionTimeout:this.maxWaitForConnection<(e.maxWait||0)?Math.max(e.maxWait*.8,e.maxWait-1e3):this.maxWaitForConnection,abort:e.abort})}catch(B){this.onRelayConnectionFailure?.(h),l(x,h,B?.message||String(B));return}this.onRelayConnectionSuccess?.(h);let E=w.subscribe(b,{...e,oneose:()=>c(x),onclose:B=>{B.startsWith(\"auth-required: \")&&e.onauth?w.auth(e.onauth).then(()=>{w.subscribe(b,{...e,oneose:()=>c(x),onclose:k=>{l(x,h,k)},alreadyHaveEvent:u,eoseTimeout:e.maxWait,abort:e.abort})}).catch(k=>{l(x,h,`auth was required and attempted, but failed with: ${k}`)}):l(x,h,B)},alreadyHaveEvent:u,eoseTimeout:e.maxWait,abort:e.abort});s.push(E)}));return{async close(h){await d,s.forEach(b=>{b.close(h)})}}}subscribeEose(t,e,n){let r;return r=this.subscribe(t,e,{...n,oneose(){let o=\"closed automatically on eose\";r?r.close(o):n.onclose?.(t.map(s=>({url:s,reason:o})))}}),r}subscribeManyEose(t,e,n){return this.subscribeEose(t,e,n)}async querySync(t,e,n){return new Promise(async r=>{let o=[];this.subscribeEose(t,e,{...n,onevent(s){o.push(s)},onclose(s){r(o)}})})}async get(t,e,n){e.limit=1;let r=await this.querySync(t,e,n);return r.sort((o,s)=>s.created_at-o.created_at),r[0]||null}async countMany(t,e,n,r){let o=bi(e,n),s=[];for(let l=0;l<t.length;l++){let u=Ge(t[l]);s.indexOf(u)===-1&&s.push(u)}let i=await Promise.all(s.map(async l=>{if(this.allowConnectingToRelay?.(l,[\"read\",[o]])===!1)return null;let u;try{u=await this.ensureRelay(l,{connectionTimeout:this.maxWaitForConnection<(r?.maxWait||0)?Math.max(r.maxWait*.8,r.maxWait-1e3):this.maxWaitForConnection,abort:r?.abort})}catch{return this.onRelayConnectionFailure?.(l),null}return this.onRelayConnectionSuccess?.(l),u.countWithHLL([o],{id:r?.id}).catch(()=>null)})),c=0,a;for(let l of i){if(!l||(l.count>c&&(c=l.count),!l.hll||l.hll.length!==512))continue;let u=yi(l.hll);u&&(a=gi(a||new Uint8Array(0),u))}return a?{count:c,hll:xi(a)}:{count:c}}publish(t,e,n){return t.map(Ge).map(async(r,o,s)=>{if(s.indexOf(r)!==o)return Promise.reject(\"duplicate url\");if(this.allowConnectingToRelay?.(r,[\"write\",e])===!1)return Promise.reject(\"connection skipped by allowConnectingToRelay\");let i;try{i=await this.ensureRelay(r,{connectionTimeout:this.maxWaitForConnection<(n?.maxWait||0)?Math.max(n.maxWait*.8,n.maxWait-1e3):this.maxWaitForConnection,abort:n?.abort})}catch(c){return this.onRelayConnectionFailure?.(r),Promise.reject(\"connection failure: \"+String(c))}return i.publish(e).catch(async c=>{if(c instanceof Error&&c.message.startsWith(\"auth-required: \")&&n?.onauth)return await i.auth(n.onauth),i.publish(e);throw c}).then(c=>{if(this.trackRelays){let a=this.seenOn.get(e.id);a||(a=new Set,this.seenOn.set(e.id,a)),a.add(i)}return c})})}listConnectionStatus(){let t=new Map;return this.relays.forEach((e,n)=>t.set(n,e.connected)),t}destroy(){this.relays.forEach(t=>t.close()),this.relays=new Map}pruneIdleRelays(t=1e4){let e=[];for(let[n,r]of this.relays)r.idleSince&&Date.now()-r.idleSince>=t&&(this.relays.delete(n),e.push(n),r.close());return e}},yo;try{yo=WebSocket}catch{}var xo=class extends wi{constructor(t){super({verifyEvent:si,websocketImplementation:yo,maxWaitForConnection:3e3,...t})}};var Ma=new TextDecoder(\"utf-8\"),Va=new TextEncoder;var mi=5e3;function go(t){return vi(\"npub\",F(t))}function Ei(t,e){let n=mn.toWords(e);return mn.encode(t,n,mi)}function vi(t,e){return Ei(t,e)}function wo(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name===\"Uint8Array\"&&\"BYTES_PER_ELEMENT\"in t&&t.BYTES_PER_ELEMENT===1}var kn=t=>t?`\"${t}\" `:\"\";function Si(t,e=\"\"){if(typeof t!=\"number\")throw new TypeError(kn(e)+\"expected number, got \"+typeof t);if(!Number.isSafeInteger(t)||t<0)throw new RangeError(kn(e)+\"expected integer >= 0, got \"+t);return t}function Bi(t,e,n=\"\"){if(wo(t)&&(e===void 0||t.length===e))return t;e!==void 0&&Si(e,\"length\");let r=wo(t),o=e!==void 0?` of length ${e}`:\"\",s=r?`length=${t.length}`:`type=${typeof t}`,i=kn(n)+\"expected Uint8Array\"+o+\", got \"+s;throw r?new RangeError(i):new TypeError(i)}var Eo=typeof Uint8Array.from([]).toHex==\"function\"&&typeof Uint8Array.fromHex==\"function\",Ai=Array.from({length:256},(t,e)=>e.toString(16).padStart(2,\"0\"));function vo(t){if(Bi(t),Eo)return t.toHex();let e=\"\";for(let n=0;n<t.length;n++)e+=Ai[t[n]];return e}function mo(t){return t>=48&&t<=57?t-48:t>=65&&t<=70?t-55:t>=97&&t<=102?t-87:void 0}function So(t){if(typeof t!=\"string\")throw new TypeError(\"hex string expected, got \"+typeof t);if(Eo)try{return Uint8Array.fromHex(t)}catch(o){throw o instanceof SyntaxError?new RangeError(o.message):o}let e=t.length,n=e/2;if(e%2)throw new RangeError(\"hex string expected, got unpadded hex of length \"+e);let r=new Uint8Array(n);for(let o=0,s=0;o<n;o++,s+=2){let i=mo(t.charCodeAt(s)),c=mo(t.charCodeAt(s+1));if(i===void 0||c===void 0){let a=t[s]+t[s+1];throw new RangeError('hex string expected, got non-hex character \"'+a+'\" at index '+s)}r[o]=i*16+c}return r}window.NostrSigner={generateSecretKey:Lr,getPublicKey:Or,finalizeEvent:kr,verifyEvent:_r,SimplePool:xo,BunkerSigner:Ot,parseBunkerInput:ho,createNostrConnectURI:bo,npubEncode:go,bytesToHex:vo,hexToBytes:So};})();\n";