nostr-web-components
Version:
collection of web components that provide quick access to basic nostr things
7 lines (5 loc) • 113 kB
JavaScript
var is=Object.defineProperty;var rs=(pe,re,ve)=>re in pe?is(pe,re,{enumerable:!0,configurable:!0,writable:!0,value:ve}):pe[re]=ve;var h=(pe,re,ve)=>rs(pe,typeof re!="symbol"?re+"":re,ve);(function(){"use strict";var pe=Math.pow(2,8)-1,re=Math.pow(2,16)-1,ve=Math.pow(2,32)-1;const li=function(n){var e=n-1;if(e<=pe)return Uint8Array;if(e<=re)return Uint16Array;if(e<=ve)return Uint32Array;throw new Error("mnemonist: Pointer Array of size > 4294967295 is not supported.")};function ae(n){if(this.capacity=n,typeof this.capacity!="number"||this.capacity<=0)throw new Error("mnemonist/lru-cache: capacity should be positive number.");if(!isFinite(this.capacity)||Math.floor(this.capacity)!==this.capacity)throw new Error("mnemonist/lru-cache: capacity should be a finite positive integer.");var e=li(n);this.forward=new e(n),this.backward=new e(n),this.K=new Array(n),this.V=new Array(n),this.size=0,this.head=0,this.tail=0,this.items={}}ae.prototype.clear=function(){this.size=0,this.head=0,this.tail=0,this.items={}},ae.prototype.splayOnTop=function(n){var e=this.head;if(this.head===n)return this;var t=this.backward[n],i=this.forward[n];return this.tail===n?this.tail=t:this.backward[i]=t,this.forward[t]=i,this.backward[e]=n,this.head=n,this.forward[n]=e,this},ae.prototype.set=function(n,e){var t=this.items[n];if(typeof t<"u"){this.splayOnTop(t),this.V[t]=e;return}this.size<this.capacity?t=this.size++:(t=this.tail,this.tail=this.backward[t],delete this.items[this.K[t]]),this.items[n]=t,this.K[t]=n,this.V[t]=e,this.forward[t]=this.head,this.backward[this.head]=t,this.head=t},ae.prototype.setpop=function(n,e){var t=null,i=null,r=this.items[n];return typeof r<"u"?(this.splayOnTop(r),t=this.V[r],this.V[r]=e,{evicted:!1,key:n,value:t}):(this.size<this.capacity?r=this.size++:(r=this.tail,this.tail=this.backward[r],t=this.V[r],i=this.K[r],delete this.items[i]),this.items[n]=r,this.K[r]=n,this.V[r]=e,this.forward[r]=this.head,this.backward[this.head]=r,this.head=r,i?{evicted:!0,key:i,value:t}:null)},ae.prototype.has=function(n){return n in this.items},ae.prototype.get=function(n){var e=this.items[n];if(!(typeof e>"u"))return this.splayOnTop(e),this.V[e]},ae.prototype.peek=function(n){var e=this.items[n];if(!(typeof e>"u"))return this.V[e]};class Ut{constructor(e,t){h(this,"_batchLoadFn");h(this,"_maxBatchSize");h(this,"_cacheKeyFn");h(this,"_cacheMap");h(this,"_batch");if(typeof e!="function")throw new TypeError(`DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but got: ${e}.`);this._batchLoadFn=e,this._maxBatchSize=(t==null?void 0:t.maxBatchSize)||1/0,this._cacheKeyFn=t==null?void 0:t.cacheKeyFn;const i=new ae(2e3);this._cacheMap={get:t.transformCacheHit?r=>{var s;return(s=i.get(r))==null?void 0:s.then(t.transformCacheHit)}:r=>i.get(r),set(r,s){return i.set(r,s)},delete(r){i.set(r,void 0)},clear(){i.clear()}},this._batch=null}load(e){if(e==null)throw new TypeError(`The loader.load() function must be called with a value, but got: ${String(e)}.`);const t=ci(this),i=this._cacheKeyFn(e),r=this._cacheMap.get(i);if(r){const a=t.cacheHits||(t.cacheHits=[]);return new Promise(l=>{a.push(()=>{l(r)})})}t.keys.push(e);const s=new Promise((a,l)=>{t.callbacks.push({resolve:a,reject:l})});return this._cacheMap.set(i,s),s}clear(e){const t=this._cacheMap;if(t){const i=this._cacheKeyFn(e);t.delete(i)}return this}}function ci(n){const e=n._batch;if(e!==null&&!e.hasDispatched&&e.keys.length<n._maxBatchSize)return e;const t={hasDispatched:!1,keys:[],callbacks:[]};return n._batch=t,setTimeout(()=>{ui(n,t)}),t}function ui(n,e){if(e.hasDispatched=!0,e.keys.length===0){lt(e);return}let t;try{t=n._batchLoadFn(e.keys)}catch(i){return at(n,e,new TypeError(`DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function errored synchronously: ${String(i)}.`))}if(!t||typeof t.then!="function")return at(n,e,new TypeError(`DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise: ${String(t)}.`));t.then(i=>{if(i.length!==e.keys.length)throw new TypeError(`DataLoader must be constructed with a function which accepts Array<key> and returns Promise<Array<value>>, but the function did not return a Promise of an Array of the same length as the Array of keys.
Keys:
${String(e.keys)}
Values:
${String(i)}`);lt(e);for(let r=0;r<e.callbacks.length;r++){const s=i[r];s instanceof Error?e.callbacks[r].reject(s):e.callbacks[r].resolve(s)}}).catch(i=>{at(n,e,i)})}function at(n,e,t){lt(e);for(let i=0;i<e.keys.length;i++)n.clear(e.keys[i]),e.callbacks[i].reject(t)}function lt(n){if(n.cacheHits)for(let e=0;e<n.cacheHits.length;e++)n.cacheHits[e]()}/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function hi(n){return n instanceof Uint8Array||ArrayBuffer.isView(n)&&n.constructor.name==="Uint8Array"}function ct(n,e=""){if(!Number.isSafeInteger(n)||n<0){const t=e&&`"${e}" `;throw new Error(`${t}expected integer >= 0, got ${n}`)}}function V(n,e,t=""){const i=hi(n),r=n==null?void 0:n.length,s=e!==void 0;if(!i||s&&r!==e){const a=t&&`"${t}" `,l=s?` of length ${e}`:"",o=i?`length=${r}`:`type=${typeof n}`;throw new Error(a+"expected Uint8Array"+l+", got "+o)}return n}function Vt(n,e=!0){if(n.destroyed)throw new Error("Hash instance has been destroyed");if(e&&n.finished)throw new Error("Hash#digest() has already been called")}function di(n,e){V(n,void 0,"digestInto() output");const t=e.outputLen;if(n.length<t)throw new Error('"digestInto() output" expected to be of length >='+t)}function ut(...n){for(let e=0;e<n.length;e++)n[e].fill(0)}function ht(n){return new DataView(n.buffer,n.byteOffset,n.byteLength)}function G(n,e){return n<<32-e|n>>>e}const Wt=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",fi=Array.from({length:256},(n,e)=>e.toString(16).padStart(2,"0"));function L(n){if(V(n),Wt)return n.toHex();let e="";for(let t=0;t<n.length;t++)e+=fi[n[t]];return e}const se={_0:48,_9:57,A:65,F:70,a:97,f:102};function Ft(n){if(n>=se._0&&n<=se._9)return n-se._0;if(n>=se.A&&n<=se.F)return n-(se.A-10);if(n>=se.a&&n<=se.f)return n-(se.a-10)}function M(n){if(typeof n!="string")throw new Error("hex string expected, got "+typeof n);if(Wt)return Uint8Array.fromHex(n);const e=n.length,t=e/2;if(e%2)throw new Error("hex string expected, got unpadded hex of length "+e);const i=new Uint8Array(t);for(let r=0,s=0;r<t;r++,s+=2){const a=Ft(n.charCodeAt(s)),l=Ft(n.charCodeAt(s+1));if(a===void 0||l===void 0){const o=n[s]+n[s+1];throw new Error('hex string expected, got non-hex character "'+o+'" at index '+s)}i[r]=a*16+l}return i}function Ce(...n){let e=0;for(let i=0;i<n.length;i++){const r=n[i];V(r),e+=r.length}const t=new Uint8Array(e);for(let i=0,r=0;i<n.length;i++){const s=n[i];t.set(s,r),r+=s.length}return t}function pi(n,e={}){const t=(r,s)=>n(s).update(r).digest(),i=n(void 0);return t.outputLen=i.outputLen,t.blockLen=i.blockLen,t.create=r=>n(r),Object.assign(t,e),Object.freeze(t)}function jt(n=32){const e=typeof globalThis=="object"?globalThis.crypto:null;if(typeof(e==null?void 0:e.getRandomValues)!="function")throw new Error("crypto.getRandomValues must be defined");return e.getRandomValues(new Uint8Array(n))}const yi=n=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,n])});/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */function Zt(n){return n instanceof Uint8Array||ArrayBuffer.isView(n)&&n.constructor.name==="Uint8Array"}function Kt(n,e){return Array.isArray(e)?e.length===0?!0:n?e.every(t=>typeof t=="string"):e.every(t=>Number.isSafeInteger(t)):!1}function bi(n){if(typeof n!="function")throw new Error("function expected");return!0}function Pe(n,e){if(typeof e!="string")throw new Error(`${n}: string expected`);return!0}function zt(n){if(!Number.isSafeInteger(n))throw new Error(`invalid integer: ${n}`)}function dt(n){if(!Array.isArray(n))throw new Error("array expected")}function Yt(n,e){if(!Kt(!0,e))throw new Error(`${n}: array of strings expected`)}function Jt(n,e){if(!Kt(!1,e))throw new Error(`${n}: array of numbers expected`)}function gi(...n){const e=s=>s,t=(s,a)=>l=>s(a(l)),i=n.map(s=>s.encode).reduceRight(t,e),r=n.map(s=>s.decode).reduce(t,e);return{encode:i,decode:r}}function mi(n){const e=typeof n=="string"?n.split(""):n,t=e.length;Yt("alphabet",e);const i=new Map(e.map((r,s)=>[r,s]));return{encode:r=>(dt(r),r.map(s=>{if(!Number.isSafeInteger(s)||s<0||s>=t)throw new Error(`alphabet.encode: digit index outside alphabet "${s}". Allowed: ${n}`);return e[s]})),decode:r=>(dt(r),r.map(s=>{Pe("alphabet.decode",s);const a=i.get(s);if(a===void 0)throw new Error(`Unknown letter: "${s}". Allowed: ${n}`);return a}))}}function wi(n=""){return Pe("join",n),{encode:e=>(Yt("join.decode",e),e.join(n)),decode:e=>(Pe("join.decode",e),e.split(n))}}const Gt=(n,e)=>e===0?n:Gt(e,n%e),Me=(n,e)=>n+(e-Gt(n,e)),De=(()=>{let n=[];for(let e=0;e<40;e++)n.push(2**e);return n})();function ft(n,e,t,i){if(dt(n),e<=0||e>32)throw new Error(`convertRadix2: wrong from=${e}`);if(t<=0||t>32)throw new Error(`convertRadix2: wrong to=${t}`);if(Me(e,t)>32)throw new Error(`convertRadix2: carry overflow from=${e} to=${t} carryBits=${Me(e,t)}`);let r=0,s=0;const a=De[e],l=De[t]-1,o=[];for(const u of n){if(zt(u),u>=a)throw new Error(`convertRadix2: invalid data word=${u} from=${e}`);if(r=r<<e|u,s+e>32)throw new Error(`convertRadix2: carry overflow pos=${s} from=${e}`);for(s+=e;s>=t;s-=t)o.push((r>>s-t&l)>>>0);const d=De[s];if(d===void 0)throw new Error("invalid carry");r&=d-1}if(r=r<<t-s&l,!i&&s>=e)throw new Error("Excess padding");if(!i&&r>0)throw new Error(`Non-zero padding: ${r}`);return i&&s>0&&o.push(r>>>0),o}function Ei(n,e=!1){if(zt(n),n<=0||n>32)throw new Error("radix2: bits should be in (0..32]");if(Me(8,n)>32||Me(n,8)>32)throw new Error("radix2: carry overflow");return{encode:t=>{if(!Zt(t))throw new Error("radix2.encode input should be Uint8Array");return ft(Array.from(t),8,n,!e)},decode:t=>(Jt("radix2.decode",t),Uint8Array.from(ft(t,n,8,e)))}}function Xt(n){return bi(n),function(...e){try{return n.apply(null,e)}catch{}}}const pt=gi(mi("qpzry9x8gf2tvdw0s3jn54khce6mua7l"),wi("")),Qt=[996825010,642813549,513874426,1027748829,705979059];function Oe(n){const e=n>>25;let t=(n&33554431)<<5;for(let i=0;i<Qt.length;i++)(e>>i&1)===1&&(t^=Qt[i]);return t}function en(n,e,t=1){const i=n.length;let r=1;for(let s=0;s<i;s++){const a=n.charCodeAt(s);if(a<33||a>126)throw new Error(`Invalid prefix (${n})`);r=Oe(r)^a>>5}r=Oe(r);for(let s=0;s<i;s++)r=Oe(r)^n.charCodeAt(s)&31;for(let s of e)r=Oe(r)^s;for(let s=0;s<6;s++)r=Oe(r);return r^=t,pt.encode(ft([r%De[30]],30,5,!1))}function vi(n){const e=n==="bech32"?1:734539939,t=Ei(5),i=t.decode,r=t.encode,s=Xt(i);function a(c,p,f=90){Pe("bech32.encode prefix",c),Zt(p)&&(p=Array.from(p)),Jt("bech32.encode",p);const y=c.length;if(y===0)throw new TypeError(`Invalid prefix length ${y}`);const b=y+7+p.length;if(f!==!1&&b>f)throw new TypeError(`Length ${b} exceeds limit ${f}`);const w=c.toLowerCase(),k=en(w,p,e);return`${w}1${pt.encode(p)}${k}`}function l(c,p=90){Pe("bech32.decode input",c);const f=c.length;if(f<8||p!==!1&&f>p)throw new TypeError(`invalid string length: ${f} (${c}). Expected (8..${p})`);const y=c.toLowerCase();if(c!==y&&c!==c.toUpperCase())throw new Error("String must be lowercase or uppercase");const b=y.lastIndexOf("1");if(b===0||b===-1)throw new Error('Letter "1" must be present between prefix and data only');const w=y.slice(0,b),k=y.slice(b+1);if(k.length<6)throw new Error("Data must be at least 6 characters long");const B=pt.decode(k).slice(0,-6),S=en(w,B,e);if(!k.endsWith(S))throw new Error(`Invalid checksum in ${c}: expected "${S}"`);return{prefix:w,words:B}}const o=Xt(l);function u(c){const{prefix:p,words:f}=l(c,!1);return{prefix:p,words:f,bytes:i(f)}}function d(c,p){return a(c,r(p))}return{encode:a,decode:l,encodeFromBytes:d,decodeToBytes:u,decodeUnsafe:o,fromWords:i,fromWordsUnsafe:s,toWords:r}}const le=vi("bech32"),Ue=new TextDecoder("utf-8"),xi=new TextEncoder;function X(n){try{n.indexOf("://")===-1&&(n="wss://"+n);let e=new URL(n);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: ${n}`)}}const tn=5e3;function nn(n){var r,s,a,l,o,u,d;let{prefix:e,words:t}=le.decode(n,tn),i=new Uint8Array(le.fromWords(t));switch(e){case"nprofile":{let c=yt(i);if(!((r=c[0])!=null&&r[0]))throw new Error("missing TLV 0 for nprofile");if(c[0][0].length!==32)throw new Error("TLV 0 should be 32 bytes");return{type:"nprofile",data:{pubkey:L(c[0][0]),relays:c[1]?c[1].map(p=>Ue.decode(p)):[]}}}case"nevent":{let c=yt(i);if(!((s=c[0])!=null&&s[0]))throw new Error("missing TLV 0 for nevent");if(c[0][0].length!==32)throw new Error("TLV 0 should be 32 bytes");if(c[2]&&c[2][0].length!==32)throw new Error("TLV 2 should be 32 bytes");if(c[3]&&c[3][0].length!==4)throw new Error("TLV 3 should be 4 bytes");return{type:"nevent",data:{id:L(c[0][0]),relays:c[1]?c[1].map(p=>Ue.decode(p)):[],author:(a=c[2])!=null&&a[0]?L(c[2][0]):void 0,kind:(l=c[3])!=null&&l[0]?parseInt(L(c[3][0]),16):void 0}}}case"naddr":{let c=yt(i);if(!((o=c[0])!=null&&o[0]))throw new Error("missing TLV 0 for naddr");if(!((u=c[2])!=null&&u[0]))throw new Error("missing TLV 2 for naddr");if(c[2][0].length!==32)throw new Error("TLV 2 should be 32 bytes");if(!((d=c[3])!=null&&d[0]))throw new Error("missing TLV 3 for naddr");if(c[3][0].length!==4)throw new Error("TLV 3 should be 4 bytes");return{type:"naddr",data:{identifier:Ue.decode(c[0][0]),pubkey:L(c[2][0]),kind:parseInt(L(c[3][0]),16),relays:c[1]?c[1].map(p=>Ue.decode(p)):[]}}}case"nsec":return{type:e,data:i};case"npub":case"note":return{type:e,data:L(i)};default:throw new Error(`unknown prefix ${e}`)}}function yt(n){let e={},t=n;for(;t.length>0;){let i=t[0],r=t[1],s=t.slice(2,2+r);if(t=t.slice(2+r),s.length<r)throw new Error(`not enough data to read on TLV ${i}`);e[i]=e[i]||[],e[i].push(s)}return e}function rn(n){return ki("npub",M(n))}function Si(n,e){let t=le.toWords(e);return le.encode(n,t,tn)}function ki(n,e){return Si(n,e)}function Ai(n,e,t){return n&e^~n&t}function Ri(n,e,t){return n&e^n&t^e&t}class Ti{constructor(e,t,i,r){h(this,"blockLen");h(this,"outputLen");h(this,"padOffset");h(this,"isLE");h(this,"buffer");h(this,"view");h(this,"finished",!1);h(this,"length",0);h(this,"pos",0);h(this,"destroyed",!1);this.blockLen=e,this.outputLen=t,this.padOffset=i,this.isLE=r,this.buffer=new Uint8Array(e),this.view=ht(this.buffer)}update(e){Vt(this),V(e);const{view:t,buffer:i,blockLen:r}=this,s=e.length;for(let a=0;a<s;){const l=Math.min(r-this.pos,s-a);if(l===r){const o=ht(e);for(;r<=s-a;a+=r)this.process(o,a);continue}i.set(e.subarray(a,a+l),this.pos),this.pos+=l,a+=l,this.pos===r&&(this.process(t,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){Vt(this),di(e,this),this.finished=!0;const{buffer:t,view:i,blockLen:r,isLE:s}=this;let{pos:a}=this;t[a++]=128,ut(this.buffer.subarray(a)),this.padOffset>r-a&&(this.process(i,0),a=0);for(let c=a;c<r;c++)t[c]=0;i.setBigUint64(r-8,BigInt(this.length*8),s),this.process(i,0);const l=ht(e),o=this.outputLen;if(o%4)throw new Error("_sha2: outputLen must be aligned to 32bit");const u=o/4,d=this.get();if(u>d.length)throw new Error("_sha2: outputLen bigger than state");for(let c=0;c<u;c++)l.setUint32(4*c,d[c],s)}digest(){const{buffer:e,outputLen:t}=this;this.digestInto(e);const i=e.slice(0,t);return this.destroy(),i}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());const{blockLen:t,buffer:i,length:r,finished:s,destroyed:a,pos:l}=this;return e.destroyed=a,e.finished=s,e.length=r,e.pos=l,r%t&&e.buffer.set(i),e}clone(){return this._cloneInto()}}const ce=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),Ci=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]),ue=new Uint32Array(64);class Pi extends Ti{constructor(e){super(64,e,8,!1)}get(){const{A:e,B:t,C:i,D:r,E:s,F:a,G:l,H:o}=this;return[e,t,i,r,s,a,l,o]}set(e,t,i,r,s,a,l,o){this.A=e|0,this.B=t|0,this.C=i|0,this.D=r|0,this.E=s|0,this.F=a|0,this.G=l|0,this.H=o|0}process(e,t){for(let c=0;c<16;c++,t+=4)ue[c]=e.getUint32(t,!1);for(let c=16;c<64;c++){const p=ue[c-15],f=ue[c-2],y=G(p,7)^G(p,18)^p>>>3,b=G(f,17)^G(f,19)^f>>>10;ue[c]=b+ue[c-7]+y+ue[c-16]|0}let{A:i,B:r,C:s,D:a,E:l,F:o,G:u,H:d}=this;for(let c=0;c<64;c++){const p=G(l,6)^G(l,11)^G(l,25),f=d+p+Ai(l,o,u)+Ci[c]+ue[c]|0,b=(G(i,2)^G(i,13)^G(i,22))+Ri(i,r,s)|0;d=u,u=o,o=l,l=a+f|0,a=s,s=r,r=i,i=f+b|0}i=i+this.A|0,r=r+this.B|0,s=s+this.C|0,a=a+this.D|0,l=l+this.E|0,o=o+this.F|0,u=u+this.G|0,d=d+this.H|0,this.set(i,r,s,a,l,o,u,d)}roundClean(){ut(ue)}destroy(){this.set(0,0,0,0,0,0,0,0),ut(this.buffer)}}class Oi extends Pi{constructor(){super(32);h(this,"A",ce[0]|0);h(this,"B",ce[1]|0);h(this,"C",ce[2]|0);h(this,"D",ce[3]|0);h(this,"E",ce[4]|0);h(this,"F",ce[5]|0);h(this,"G",ce[6]|0);h(this,"H",ce[7]|0)}}const Ve=pi(()=>new Oi,yi(1));/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const bt=BigInt(0),gt=BigInt(1);function sn(n,e=""){if(typeof n!="boolean"){const t=e&&`"${e}" `;throw new Error(t+"expected boolean, got type="+typeof n)}return n}function _i(n){if(typeof n=="bigint"){if(!$i(n))throw new Error("positive bigint expected, got "+n)}else ct(n);return n}function on(n){if(typeof n!="string")throw new Error("hex string expected, got "+typeof n);return n===""?bt:BigInt("0x"+n)}function mt(n){return on(L(n))}function an(n){return on(L(Ii(V(n)).reverse()))}function wt(n,e){ct(e),n=_i(n);const t=M(n.toString(16).padStart(e*2,"0"));if(t.length!==e)throw new Error("number too large");return t}function ln(n,e){return wt(n,e).reverse()}function Ii(n){return Uint8Array.from(n)}function Bi(n){return Uint8Array.from(n,(e,t)=>{const i=e.charCodeAt(0);if(e.length!==1||i>127)throw new Error(`string contains non-ASCII character "${n[t]}" with code ${i} at position ${t}`);return i})}const $i=n=>typeof n=="bigint"&&bt<=n;function Li(n){let e;for(e=0;n>bt;n>>=gt,e+=1);return e}const cn=n=>(gt<<BigInt(n))-gt;function un(n,e={},t={}){if(!n||typeof n!="object")throw new Error("expected valid options object");function i(s,a,l){const o=n[s];if(l&&o===void 0)return;const u=typeof o;if(u!==a||o===null)throw new Error(`param "${s}" is invalid: expected ${a}, got ${u}`)}const r=(s,a)=>Object.entries(s).forEach(([l,o])=>i(l,o,a));r(e,!1),r(t,!0)}function hn(n){const e=new WeakMap;return(t,...i)=>{const r=e.get(t);if(r!==void 0)return r;const s=n(t,...i);return e.set(t,s),s}}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const W=BigInt(0),D=BigInt(1),ye=BigInt(2),dn=BigInt(3),fn=BigInt(4),pn=BigInt(5),Hi=BigInt(7),yn=BigInt(8),qi=BigInt(9),bn=BigInt(16);function K(n,e){const t=n%e;return t>=W?t:e+t}function F(n,e,t){let i=n;for(;e-- >W;)i*=i,i%=t;return i}function gn(n,e){if(n===W)throw new Error("invert: expected non-zero number");if(e<=W)throw new Error("invert: expected positive modulus, got "+e);let t=K(n,e),i=e,r=W,s=D;for(;t!==W;){const l=i/t,o=i%t,u=r-s*l;i=t,t=o,r=s,s=u}if(i!==D)throw new Error("invert: does not exist");return K(r,e)}function Et(n,e,t){if(!n.eql(n.sqr(e),t))throw new Error("Cannot find square root")}function mn(n,e){const t=(n.ORDER+D)/fn,i=n.pow(e,t);return Et(n,i,e),i}function Ni(n,e){const t=(n.ORDER-pn)/yn,i=n.mul(e,ye),r=n.pow(i,t),s=n.mul(e,r),a=n.mul(n.mul(s,ye),r),l=n.mul(s,n.sub(a,n.ONE));return Et(n,l,e),l}function Mi(n){const e=We(n),t=wn(n),i=t(e,e.neg(e.ONE)),r=t(e,i),s=t(e,e.neg(i)),a=(n+Hi)/bn;return(l,o)=>{let u=l.pow(o,a),d=l.mul(u,i);const c=l.mul(u,r),p=l.mul(u,s),f=l.eql(l.sqr(d),o),y=l.eql(l.sqr(c),o);u=l.cmov(u,d,f),d=l.cmov(p,c,y);const b=l.eql(l.sqr(d),o),w=l.cmov(u,d,b);return Et(l,w,o),w}}function wn(n){if(n<dn)throw new Error("sqrt is not defined for small field");let e=n-D,t=0;for(;e%ye===W;)e/=ye,t++;let i=ye;const r=We(n);for(;vn(r,i)===1;)if(i++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(t===1)return mn;let s=r.pow(i,e);const a=(e+D)/ye;return function(o,u){if(o.is0(u))return u;if(vn(o,u)!==1)throw new Error("Cannot find square root");let d=t,c=o.mul(o.ONE,s),p=o.pow(u,e),f=o.pow(u,a);for(;!o.eql(p,o.ONE);){if(o.is0(p))return o.ZERO;let y=1,b=o.sqr(p);for(;!o.eql(b,o.ONE);)if(y++,b=o.sqr(b),y===d)throw new Error("Cannot find square root");const w=D<<BigInt(d-y-1),k=o.pow(c,w);d=y,c=o.sqr(k),p=o.mul(p,c),f=o.mul(f,k)}return f}}function Di(n){return n%fn===dn?mn:n%yn===pn?Ni:n%bn===qi?Mi(n):wn(n)}const Ui=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Vi(n){const e={ORDER:"bigint",BYTES:"number",BITS:"number"},t=Ui.reduce((i,r)=>(i[r]="function",i),e);return un(n,t),n}function Wi(n,e,t){if(t<W)throw new Error("invalid exponent, negatives unsupported");if(t===W)return n.ONE;if(t===D)return e;let i=n.ONE,r=e;for(;t>W;)t&D&&(i=n.mul(i,r)),r=n.sqr(r),t>>=D;return i}function En(n,e,t=!1){const i=new Array(e.length).fill(t?n.ZERO:void 0),r=e.reduce((a,l,o)=>n.is0(l)?a:(i[o]=a,n.mul(a,l)),n.ONE),s=n.inv(r);return e.reduceRight((a,l,o)=>n.is0(l)?a:(i[o]=n.mul(a,i[o]),n.mul(a,l)),s),i}function vn(n,e){const t=(n.ORDER-D)/ye,i=n.pow(e,t),r=n.eql(i,n.ONE),s=n.eql(i,n.ZERO),a=n.eql(i,n.neg(n.ONE));if(!r&&!s&&!a)throw new Error("invalid Legendre symbol result");return r?1:s?0:-1}function Fi(n,e){e!==void 0&&ct(e);const t=e!==void 0?e:n.toString(2).length,i=Math.ceil(t/8);return{nBitLength:t,nByteLength:i}}class ji{constructor(e,t={}){h(this,"ORDER");h(this,"BITS");h(this,"BYTES");h(this,"isLE");h(this,"ZERO",W);h(this,"ONE",D);h(this,"_lengths");h(this,"_sqrt");h(this,"_mod");var a;if(e<=W)throw new Error("invalid field: expected ORDER > 0, got "+e);let i;this.isLE=!1,t!=null&&typeof t=="object"&&(typeof t.BITS=="number"&&(i=t.BITS),typeof t.sqrt=="function"&&(this.sqrt=t.sqrt),typeof t.isLE=="boolean"&&(this.isLE=t.isLE),t.allowedLengths&&(this._lengths=(a=t.allowedLengths)==null?void 0:a.slice()),typeof t.modFromBytes=="boolean"&&(this._mod=t.modFromBytes));const{nBitLength:r,nByteLength:s}=Fi(e,i);if(s>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");this.ORDER=e,this.BITS=r,this.BYTES=s,this._sqrt=void 0,Object.preventExtensions(this)}create(e){return K(e,this.ORDER)}isValid(e){if(typeof e!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof e);return W<=e&&e<this.ORDER}is0(e){return e===W}isValidNot0(e){return!this.is0(e)&&this.isValid(e)}isOdd(e){return(e&D)===D}neg(e){return K(-e,this.ORDER)}eql(e,t){return e===t}sqr(e){return K(e*e,this.ORDER)}add(e,t){return K(e+t,this.ORDER)}sub(e,t){return K(e-t,this.ORDER)}mul(e,t){return K(e*t,this.ORDER)}pow(e,t){return Wi(this,e,t)}div(e,t){return K(e*gn(t,this.ORDER),this.ORDER)}sqrN(e){return e*e}addN(e,t){return e+t}subN(e,t){return e-t}mulN(e,t){return e*t}inv(e){return gn(e,this.ORDER)}sqrt(e){return this._sqrt||(this._sqrt=Di(this.ORDER)),this._sqrt(this,e)}toBytes(e){return this.isLE?ln(e,this.BYTES):wt(e,this.BYTES)}fromBytes(e,t=!1){V(e);const{_lengths:i,BYTES:r,isLE:s,ORDER:a,_mod:l}=this;if(i){if(!i.includes(e.length)||e.length>r)throw new Error("Field.fromBytes: expected "+i+" bytes, got "+e.length);const u=new Uint8Array(r);u.set(e,s?0:u.length-e.length),e=u}if(e.length!==r)throw new Error("Field.fromBytes: expected "+r+" bytes, got "+e.length);let o=s?an(e):mt(e);if(l&&(o=K(o,a)),!t&&!this.isValid(o))throw new Error("invalid field element: outside of range 0..ORDER");return o}invertBatch(e){return En(this,e)}cmov(e,t,i){return i?t:e}}function We(n,e={}){return new ji(n,e)}function xn(n){if(typeof n!="bigint")throw new Error("field order must be bigint");const e=n.toString(2).length;return Math.ceil(e/8)}function Zi(n){const e=xn(n);return e+Math.ceil(e/2)}function Ki(n,e,t=!1){V(n);const i=n.length,r=xn(e),s=Zi(e);if(i<16||i<s||i>1024)throw new Error("expected "+s+"-1024 bytes of input, got "+i);const a=t?an(n):mt(n),l=K(a,e-D)+D;return t?ln(l,r):wt(l,r)}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const xe=BigInt(0),be=BigInt(1);function Fe(n,e){const t=e.negate();return n?t:e}function Sn(n,e){const t=En(n.Fp,e.map(i=>i.Z));return e.map((i,r)=>n.fromAffine(i.toAffine(t[r])))}function kn(n,e){if(!Number.isSafeInteger(n)||n<=0||n>e)throw new Error("invalid window size, expected [1.."+e+"], got W="+n)}function vt(n,e){kn(n,e);const t=Math.ceil(e/n)+1,i=2**(n-1),r=2**n,s=cn(n),a=BigInt(n);return{windows:t,windowSize:i,mask:s,maxNumber:r,shiftBy:a}}function An(n,e,t){const{windowSize:i,mask:r,maxNumber:s,shiftBy:a}=t;let l=Number(n&r),o=n>>a;l>i&&(l-=s,o+=be);const u=e*i,d=u+Math.abs(l)-1,c=l===0,p=l<0,f=e%2!==0;return{nextN:o,offset:d,isZero:c,isNeg:p,isNegF:f,offsetF:u}}const xt=new WeakMap,Rn=new WeakMap;function St(n){return Rn.get(n)||1}function Tn(n){if(n!==xe)throw new Error("invalid wNAF")}class zi{constructor(e,t){h(this,"BASE");h(this,"ZERO");h(this,"Fn");h(this,"bits");this.BASE=e.BASE,this.ZERO=e.ZERO,this.Fn=e.Fn,this.bits=t}_unsafeLadder(e,t,i=this.ZERO){let r=e;for(;t>xe;)t&be&&(i=i.add(r)),r=r.double(),t>>=be;return i}precomputeWindow(e,t){const{windows:i,windowSize:r}=vt(t,this.bits),s=[];let a=e,l=a;for(let o=0;o<i;o++){l=a,s.push(l);for(let u=1;u<r;u++)l=l.add(a),s.push(l);a=l.double()}return s}wNAF(e,t,i){if(!this.Fn.isValid(i))throw new Error("invalid scalar");let r=this.ZERO,s=this.BASE;const a=vt(e,this.bits);for(let l=0;l<a.windows;l++){const{nextN:o,offset:u,isZero:d,isNeg:c,isNegF:p,offsetF:f}=An(i,l,a);i=o,d?s=s.add(Fe(p,t[f])):r=r.add(Fe(c,t[u]))}return Tn(i),{p:r,f:s}}wNAFUnsafe(e,t,i,r=this.ZERO){const s=vt(e,this.bits);for(let a=0;a<s.windows&&i!==xe;a++){const{nextN:l,offset:o,isZero:u,isNeg:d}=An(i,a,s);if(i=l,!u){const c=t[o];r=r.add(d?c.negate():c)}}return Tn(i),r}getPrecomputes(e,t,i){let r=xt.get(t);return r||(r=this.precomputeWindow(t,e),e!==1&&(typeof i=="function"&&(r=i(r)),xt.set(t,r))),r}cached(e,t,i){const r=St(e);return this.wNAF(r,this.getPrecomputes(r,e,i),t)}unsafe(e,t,i,r){const s=St(e);return s===1?this._unsafeLadder(e,t,r):this.wNAFUnsafe(s,this.getPrecomputes(s,e,i),t,r)}createCache(e,t){kn(t,this.bits),Rn.set(e,t),xt.delete(e)}hasCache(e){return St(e)!==1}}function Yi(n,e,t,i){let r=e,s=n.ZERO,a=n.ZERO;for(;t>xe||i>xe;)t&be&&(s=s.add(r)),i&be&&(a=a.add(r)),r=r.double(),t>>=be,i>>=be;return{p1:s,p2:a}}function Cn(n,e,t){if(e){if(e.ORDER!==n)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return Vi(e),e}else return We(n,{isLE:t})}function Ji(n,e,t={},i){if(i===void 0&&(i=n==="edwards"),!e||typeof e!="object")throw new Error(`expected valid ${n} CURVE object`);for(const o of["p","n","h"]){const u=e[o];if(!(typeof u=="bigint"&&u>xe))throw new Error(`CURVE.${o} must be positive bigint`)}const r=Cn(e.p,t.Fp,i),s=Cn(e.n,t.Fn,i),l=["Gx","Gy","a","b"];for(const o of l)if(!r.isValid(e[o]))throw new Error(`CURVE.${o} must be valid field element of CURVE.Fp`);return e=Object.freeze(Object.assign({},e)),{CURVE:e,Fp:r,Fn:s}}function Gi(n,e){return function(i){const r=n(i);return{secretKey:r,publicKey:e(r)}}}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Pn=(n,e)=>(n+(n>=0?e:-e)/Qi)/e;function Xi(n,e,t){const[[i,r],[s,a]]=e,l=Pn(a*n,t),o=Pn(-r*n,t);let u=n-l*i-o*s,d=-l*r-o*a;const c=u<_e,p=d<_e;c&&(u=-u),p&&(d=-d);const f=cn(Math.ceil(Li(t)/2))+je;if(u<_e||u>=f||d<_e||d>=f)throw new Error("splitScalar (endomorphism): failed, k="+n);return{k1neg:c,k1:u,k2neg:p,k2:d}}const _e=BigInt(0),je=BigInt(1),Qi=BigInt(2),Ze=BigInt(3),er=BigInt(4);function tr(n,e={}){const t=Ji("weierstrass",n,e),{Fp:i,Fn:r}=t;let s=t.CURVE;const{h:a,n:l}=s;un(e,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object"});const{endo:o}=e;if(o&&(!i.is0(s.a)||typeof o.beta!="bigint"||!Array.isArray(o.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');const u=ir(i,r);function d(){if(!i.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function c(O,m,E){const{x:g,y:x}=m.toAffine(),P=i.toBytes(g);if(sn(E,"isCompressed"),E){d();const A=!i.isOdd(x);return Ce(nr(A),P)}else return Ce(Uint8Array.of(4),P,i.toBytes(x))}function p(O){V(O,void 0,"Point");const{publicKey:m,publicKeyUncompressed:E}=u,g=O.length,x=O[0],P=O.subarray(1);if(g===m&&(x===2||x===3)){const A=i.fromBytes(P);if(!i.isValid(A))throw new Error("bad point: is not on curve, wrong x");const R=b(A);let v;try{v=i.sqrt(R)}catch(Z){const N=Z instanceof Error?": "+Z.message:"";throw new Error("bad point: is not on curve, sqrt error"+N)}d();const T=i.isOdd(v);return(x&1)===1!==T&&(v=i.neg(v)),{x:A,y:v}}else if(g===E&&x===4){const A=i.BYTES,R=i.fromBytes(P.subarray(0,A)),v=i.fromBytes(P.subarray(A,A*2));if(!w(R,v))throw new Error("bad point: is not on curve");return{x:R,y:v}}else throw new Error(`bad point: got length ${g}, expected compressed=${m} or uncompressed=${E}`)}const f=e.toBytes||c,y=e.fromBytes||p;function b(O){const m=i.sqr(O),E=i.mul(m,O);return i.add(i.add(E,i.mul(O,s.a)),s.b)}function w(O,m){const E=i.sqr(m),g=b(O);return i.eql(E,g)}if(!w(s.Gx,s.Gy))throw new Error("bad curve params: generator point");const k=i.mul(i.pow(s.a,Ze),er),B=i.mul(i.sqr(s.b),BigInt(27));if(i.is0(i.add(k,B)))throw new Error("bad curve params: a or b");function S(O,m,E=!1){if(!i.isValid(m)||E&&i.is0(m))throw new Error(`bad point coordinate ${O}`);return m}function q(O){if(!(O instanceof ne))throw new Error("Weierstrass Point expected")}function C(O){if(!o||!o.basises)throw new Error("no endo");return Xi(O,o.basises,r.ORDER)}const H=hn((O,m)=>{const{X:E,Y:g,Z:x}=O;if(i.eql(x,i.ONE))return{x:E,y:g};const P=O.is0();m==null&&(m=P?i.ONE:i.inv(x));const A=i.mul(E,m),R=i.mul(g,m),v=i.mul(x,m);if(P)return{x:i.ZERO,y:i.ZERO};if(!i.eql(v,i.ONE))throw new Error("invZ was invalid");return{x:A,y:R}}),ot=hn(O=>{if(O.is0()){if(e.allowInfinityPoint&&!i.is0(O.Y))return;throw new Error("bad point: ZERO")}const{x:m,y:E}=O.toAffine();if(!i.isValid(m)||!i.isValid(E))throw new Error("bad point: x or y not field elements");if(!w(m,E))throw new Error("bad point: equation left != right");if(!O.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function qe(O,m,E,g,x){return E=new ne(i.mul(E.X,O),E.Y,E.Z),m=Fe(g,m),E=Fe(x,E),m.add(E)}const I=class I{constructor(m,E,g){h(this,"X");h(this,"Y");h(this,"Z");this.X=S("x",m),this.Y=S("y",E,!0),this.Z=S("z",g),Object.freeze(this)}static CURVE(){return s}static fromAffine(m){const{x:E,y:g}=m||{};if(!m||!i.isValid(E)||!i.isValid(g))throw new Error("invalid affine point");if(m instanceof I)throw new Error("projective point not allowed");return i.is0(E)&&i.is0(g)?I.ZERO:new I(E,g,i.ONE)}static fromBytes(m){const E=I.fromAffine(y(V(m,void 0,"point")));return E.assertValidity(),E}static fromHex(m){return I.fromBytes(M(m))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(m=8,E=!0){return fe.createCache(this,m),E||this.multiply(Ze),this}assertValidity(){ot(this)}hasEvenY(){const{y:m}=this.toAffine();if(!i.isOdd)throw new Error("Field doesn't support isOdd");return!i.isOdd(m)}equals(m){q(m);const{X:E,Y:g,Z:x}=this,{X:P,Y:A,Z:R}=m,v=i.eql(i.mul(E,R),i.mul(P,x)),T=i.eql(i.mul(g,R),i.mul(A,x));return v&&T}negate(){return new I(this.X,i.neg(this.Y),this.Z)}double(){const{a:m,b:E}=s,g=i.mul(E,Ze),{X:x,Y:P,Z:A}=this;let R=i.ZERO,v=i.ZERO,T=i.ZERO,_=i.mul(x,x),Z=i.mul(P,P),N=i.mul(A,A),$=i.mul(x,P);return $=i.add($,$),T=i.mul(x,A),T=i.add(T,T),R=i.mul(m,T),v=i.mul(g,N),v=i.add(R,v),R=i.sub(Z,v),v=i.add(Z,v),v=i.mul(R,v),R=i.mul($,R),T=i.mul(g,T),N=i.mul(m,N),$=i.sub(_,N),$=i.mul(m,$),$=i.add($,T),T=i.add(_,_),_=i.add(T,_),_=i.add(_,N),_=i.mul(_,$),v=i.add(v,_),N=i.mul(P,A),N=i.add(N,N),_=i.mul(N,$),R=i.sub(R,_),T=i.mul(N,Z),T=i.add(T,T),T=i.add(T,T),new I(R,v,T)}add(m){q(m);const{X:E,Y:g,Z:x}=this,{X:P,Y:A,Z:R}=m;let v=i.ZERO,T=i.ZERO,_=i.ZERO;const Z=s.a,N=i.mul(s.b,Ze);let $=i.mul(E,P),Y=i.mul(g,A),J=i.mul(x,R),Te=i.add(E,g),U=i.add(P,A);Te=i.mul(Te,U),U=i.add($,Y),Te=i.sub(Te,U),U=i.add(E,x);let ie=i.add(P,R);return U=i.mul(U,ie),ie=i.add($,J),U=i.sub(U,ie),ie=i.add(g,x),v=i.add(A,R),ie=i.mul(ie,v),v=i.add(Y,J),ie=i.sub(ie,v),_=i.mul(Z,U),v=i.mul(N,J),_=i.add(v,_),v=i.sub(Y,_),_=i.add(Y,_),T=i.mul(v,_),Y=i.add($,$),Y=i.add(Y,$),J=i.mul(Z,J),U=i.mul(N,U),Y=i.add(Y,J),J=i.sub($,J),J=i.mul(Z,J),U=i.add(U,J),$=i.mul(Y,U),T=i.add(T,$),$=i.mul(ie,U),v=i.mul(Te,v),v=i.sub(v,$),$=i.mul(Te,Y),_=i.mul(ie,_),_=i.add(_,$),new I(v,T,_)}subtract(m){return this.add(m.negate())}is0(){return this.equals(I.ZERO)}multiply(m){const{endo:E}=e;if(!r.isValidNot0(m))throw new Error("invalid scalar: out of range");let g,x;const P=A=>fe.cached(this,A,R=>Sn(I,R));if(E){const{k1neg:A,k1:R,k2neg:v,k2:T}=C(m),{p:_,f:Z}=P(R),{p:N,f:$}=P(T);x=Z.add($),g=qe(E.beta,_,N,A,v)}else{const{p:A,f:R}=P(m);g=A,x=R}return Sn(I,[g,x])[0]}multiplyUnsafe(m){const{endo:E}=e,g=this;if(!r.isValid(m))throw new Error("invalid scalar: out of range");if(m===_e||g.is0())return I.ZERO;if(m===je)return g;if(fe.hasCache(this))return this.multiply(m);if(E){const{k1neg:x,k1:P,k2neg:A,k2:R}=C(m),{p1:v,p2:T}=Yi(I,g,P,R);return qe(E.beta,v,T,x,A)}else return fe.unsafe(g,m)}toAffine(m){return H(this,m)}isTorsionFree(){const{isTorsionFree:m}=e;return a===je?!0:m?m(I,this):fe.unsafe(this,l).is0()}clearCofactor(){const{clearCofactor:m}=e;return a===je?this:m?m(I,this):this.multiplyUnsafe(a)}isSmallOrder(){return this.multiplyUnsafe(a).is0()}toBytes(m=!0){return sn(m,"isCompressed"),this.assertValidity(),f(I,this,m)}toHex(m=!0){return L(this.toBytes(m))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}};h(I,"BASE",new I(s.Gx,s.Gy,i.ONE)),h(I,"ZERO",new I(i.ZERO,i.ONE,i.ZERO)),h(I,"Fp",i),h(I,"Fn",r);let ne=I;const Ne=r.BITS,fe=new zi(ne,e.endo?Math.ceil(Ne/2):Ne);return ne.BASE.precompute(8),ne}function nr(n){return Uint8Array.of(n?2:3)}function ir(n,e){return{secretKey:e.BYTES,publicKey:1+n.BYTES,publicKeyUncompressed:1+2*n.BYTES,publicKeyHasPrefix:!0,signature:2*e.BYTES}}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Ke={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},rr={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]},sr=BigInt(0),kt=BigInt(2);function or(n){const e=Ke.p,t=BigInt(3),i=BigInt(6),r=BigInt(11),s=BigInt(22),a=BigInt(23),l=BigInt(44),o=BigInt(88),u=n*n*n%e,d=u*u*n%e,c=F(d,t,e)*d%e,p=F(c,t,e)*d%e,f=F(p,kt,e)*u%e,y=F(f,r,e)*f%e,b=F(y,s,e)*y%e,w=F(b,l,e)*b%e,k=F(w,o,e)*w%e,B=F(k,l,e)*b%e,S=F(B,t,e)*d%e,q=F(S,a,e)*y%e,C=F(q,i,e)*u%e,H=F(C,kt,e);if(!ze.eql(ze.sqr(H),n))throw new Error("Cannot find square root");return H}const ze=We(Ke.p,{sqrt:or}),Se=tr(Ke,{Fp:ze,endo:rr}),On={};function Ye(n,...e){let t=On[n];if(t===void 0){const i=Ve(Bi(n));t=Ce(i,i),On[n]=t}return Ve(Ce(t,...e))}const At=n=>n.toBytes(!0).slice(1),Rt=n=>n%kt===sr;function Tt(n){const{Fn:e,BASE:t}=Se,i=e.fromBytes(n),r=t.multiply(i);return{scalar:Rt(r.y)?i:e.neg(i),bytes:At(r)}}function _n(n){const e=ze;if(!e.isValidNot0(n))throw new Error("invalid x: Fail if x ≥ p");const t=e.create(n*n),i=e.create(t*n+BigInt(7));let r=e.sqrt(i);Rt(r)||(r=e.neg(r));const s=Se.fromAffine({x:n,y:r});return s.assertValidity(),s}const Ie=mt;function In(...n){return Se.Fn.create(Ie(Ye("BIP0340/challenge",...n)))}function Bn(n){return Tt(n).bytes}function ar(n,e,t=jt(32)){const{Fn:i}=Se,r=V(n,void 0,"message"),{bytes:s,scalar:a}=Tt(e),l=V(t,32,"auxRand"),o=i.toBytes(a^Ie(Ye("BIP0340/aux",l))),u=Ye("BIP0340/nonce",o,s,r),{bytes:d,scalar:c}=Tt(u),p=In(d,s,r),f=new Uint8Array(64);if(f.set(d,0),f.set(i.toBytes(i.create(c+p*a)),32),!$n(f,r,s))throw new Error("sign: Invalid signature produced");return f}function $n(n,e,t){const{Fp:i,Fn:r,BASE:s}=Se,a=V(n,64,"signature"),l=V(e,void 0,"message"),o=V(t,32,"publicKey");try{const u=_n(Ie(o)),d=Ie(a.subarray(0,32));if(!i.isValidNot0(d))return!1;const c=Ie(a.subarray(32,64));if(!r.isValidNot0(c))return!1;const p=In(r.toBytes(d),At(u),l),f=s.multiplyUnsafe(c).add(u.multiplyUnsafe(r.neg(p))),{x:y,y:b}=f.toAffine();return!(f.is0()||!Rt(b)||y!==d)}catch{return!1}}const Q=(()=>{const t=(i=jt(48))=>Ki(i,Ke.n);return{keygen:Gi(t,Bn),getPublicKey:Bn,sign:ar,verify:$n,Point:Se,utils:{randomSecretKey:t,taggedHash:Ye,lift_x:_n,pointToBytes:At},lengths:{secretKey:32,publicKey:32,publicKeyHasPrefix:!1,signature:64,seed:48}}})(),ge=Symbol("verified"),lr=n=>n instanceof Object;function cr(n){if(!lr(n)||typeof n.kind!="number"||typeof n.content!="string"||typeof n.created_at!="number"||typeof n.pubkey!="string"||!n.pubkey.match(/^[a-f0-9]{64}$/)||!Array.isArray(n.tags))return!1;for(let e=0;e<n.tags.length;e++){let t=n.tags[e];if(!Array.isArray(t))return!1;for(let i=0;i<t.length;i++)if(typeof t[i]!="string")return!1}return!0}let ur=class{generateSecretKey(){return Q.utils.randomSecretKey()}getPublicKey(e){return L(Q.getPublicKey(e))}finalizeEvent(e,t){const i=e;return i.pubkey=L(Q.getPublicKey(t)),i.id=Ct(i),i.sig=L(Q.sign(M(Ct(i)),t)),i[ge]=!0,i}verifyEvent(e){if(typeof e[ge]=="boolean")return e[ge];const t=Ct(e);if(t!==e.id)return e[ge]=!1,!1;try{const i=Q.verify(M(e.sig),M(t),M(e.pubkey));return e[ge]=i,i}catch{return e[ge]=!1,!1}}};function hr(n){if(!cr(n))throw new Error("can't serialize event with wrong or missing properties");return JSON.stringify([0,n.pubkey,n.created_at,n.kind,n.tags,n.content])}function Ct(n){let e=Ve(xi.encode(hr(n)));return L(e)}const Je=new ur;Je.generateSecretKey,Je.getPublicKey,Je.finalizeEvent;const dr=Je.verifyEvent,fr=22242;function pr(n,e){if(n.ids&&n.ids.indexOf(e.id)===-1||n.kinds&&n.kinds.indexOf(e.kind)===-1||n.authors&&n.authors.indexOf(e.pubkey)===-1)return!1;for(let t in n)if(t[0]==="#"){let i=t.slice(1),r=n[`#${i}`];if(r&&!e.tags.find(([s,a])=>s===t.slice(1)&&r.indexOf(a)!==-1))return!1}return!(n.since&&e.created_at<n.since||n.until&&e.created_at>n.until)}function yr(n,e){for(let t=0;t<n.length;t++)if(pr(n[t],e))return!0;return!1}function br(n,e){let t=e.length+3,i=n.indexOf(`"${e}":`)+t,r=n.slice(i).indexOf('"')+i+1;return n.slice(r,r+64)}function gr(n){let e=n.slice(0,22).indexOf('"EVENT"');if(e===-1)return null;let t=n.slice(e+7+1).indexOf('"');if(t===-1)return null;let i=e+7+1+t,r=n.slice(i+1,80).indexOf('"');if(r===-1)return null;let s=i+1+r;return n.slice(i+1,s)}function mr(n,e){return{kind:fr,created_at:Math.floor(Date.now()/1e3),tags:[["relay",n],["challenge",e]],content:""}}let Ln=class extends Error{constructor(e,t){super(`Tried to send message '${e} on a closed connection to ${t}.`),this.name="SendingOnClosedConnection"}},wr=class ai{constructor(e,t){h(this,"url");h(this,"_connected",!1);h(this,"onclose",null);h(this,"onnotice",e=>console.debug(`NOTICE from ${this.url}: ${e}`));h(this,"onauth");h(this,"baseEoseTimeout",4400);h(this,"publishTimeout",4400);h(this,"pingFrequency",29e3);h(this,"pingTimeout",2e4);h(this,"resubscribeBackoff",[1e4,1e4,1e4,2e4,2e4,3e4,6e4]);h(this,"openSubs",new Map);h(this,"enablePing");h(this,"enableReconnect");h(this,"idleSince",Date.now());h(this,"ongoingOperations",0);h(this,"reconnectTimeoutHandle");h(this,"pingIntervalHandle");h(this,"reconnectAttempts",0);h(this,"skipReconnection",!1);h(this,"connectionPromise");h(this,"openCountRequests",new Map);h(this,"openEventPublishes",new Map);h(this,"ws");h(this,"challenge");h(this,"authPromise");h(this,"serial",0);h(this,"verifyEvent");h(this,"_WebSocket");this.url=X(e),this.verifyEvent=t.verifyEvent,this._WebSocket=t.websocketImplementation||WebSocket,this.enablePing=t.enablePing,this.enableReconnect=t.enableReconnect||!1}static async connect(e,t){const i=new ai(e,t);return await i.connect(t),i}closeAllSubscriptions(e){for(let[t,i]of this.openSubs)i.close(e);this.openSubs.clear();for(let[t,i]of this.openEventPublishes)i.reject(new Error(e));this.openEventPublishes.clear();for(let[t,i]of this.openCountRequests)i.reject(new Error(e));this.openCountRequests.clear()}get connected(){return this._connected}async reconnect(){const e=this.resubscribeBackoff[Math.min(this.reconnectAttempts,this.resubscribeBackoff.length-1)];this.reconnectAttempts++,this.reconnectTimeoutHandle=setTimeout(async()=>{try{await this.connect()}catch{}},e)}handleHardClose(e){var t;this.pingIntervalHandle&&(clearInterval(this.pingIntervalHandle),this.pingIntervalHandle=void 0),this._connected=!1,this.connectionPromise=void 0,this.idleSince=void 0,this.enableReconnect&&!this.skipReconnection?this.reconnect():((t=this.onclose)==null||t.call(this),this.closeAllSubscriptions(e))}async connect(e){let t;return this.connectionPromise?this.connectionPromise:(this.challenge=void 0,this.authPromise=void 0,this.skipReconnection=!1,this.connectionPromise=new Promise((i,r)=>{e!=null&&e.timeout&&(t=setTimeout(()=>{var s;r("connection timed out"),this.connectionPromise=void 0,this.skipReconnection=!0,(s=this.onclose)==null||s.call(this),this.handleHardClose("relay connection timed out")},e.timeout)),e!=null&&e.abort&&(e.abort.onabort=r);try{this.ws=new this._WebSocket(this.url)}catch(s){clearTimeout(t),r(s);return}this.ws.onopen=()=>{this.reconnectTimeoutHandle&&(clearTimeout(this.reconnectTimeoutHandle),this.reconnectTimeoutHandle=void 0),clearTimeout(t),this._connected=!0;const s=this.reconnectAttempts>0;this.reconnectAttempts=0;for(const a of this.openSubs.values()){if(a.eosed=!1,s)for(let l=0;l<a.filters.length;l++)a.lastEmitted&&(a.filters[l].since=a.lastEmitted+1);a.fire()}this.enablePing&&(this.pingIntervalHandle=setInterval(()=>this.pingpong(),this.pingFrequency)),i()},this.ws.onerror=()=>{var s;clearTimeout(t),r("connection failed"),this.connectionPromise=void 0,this.skipReconnection=!0,(s=this.onclose)==null||s.call(this),this.handleHardClose("relay connection failed")},this.ws.onclose=s=>{clearTimeout(t),r(s.message||"websocket closed"),this.handleHardClose("relay connection closed")},this.ws.onmessage=this._onmessage.bind(this)}),this.connectionPromise)}waitForPingPong(){return new Promise(e=>{this.ws.once("pong",()=>e(!0)),this.ws.ping()})}waitForDummyReq(){return new Promise((e,t)=>{if(!this.connectionPromise)return t(new Error(`no connection to ${this.url}, can't ping`));try{const i=this.subscribe([{ids:["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"],limit:0}],{label:"<forced-ping>",oneose:()=>{e(!0),i.close()},onclose(){e(!0)},eoseTimeout:this.pingTimeout+1e3})}catch(i){t(i)}})}async pingpong(){var e,t,i;((e=this.ws)==null?void 0:e.readyState)===1&&(await Promise.any([this.ws&&this.ws.ping&&this.ws.once?this.waitForPingPong():this.waitForDummyReq(),new Promise(s=>setTimeout(()=>s(!1),this.pingTimeout))])||((t=this.ws)==null?void 0:t.readyState)===this._WebSocket.OPEN&&((i=this.ws)==null||i.close()))}async send(e){if(!this.connectionPromise)throw new Ln(e,this.url);this.connectionPromise.then(()=>{var t;(t=this.ws)==null||t.send(e)})}async auth(e){const t=this.challenge;if(!t)throw new Error("can't perform auth, no challenge was received");return this.authPromise?this.authPromise:(this.authPromise=new Promise(async(i,r)=>{try{let s=await e(mr(this.url,t)),a=setTimeout(()=>{let l=this.openEventPublishes.get(s.id);l&&(l.reject(new Error("auth timed out")),this.openEventPublishes.delete(s.id))},this.publishTimeout);this.openEventPublishes.set(s.id,{resolve:i,reject:r,timeout:a}),this.send('["AUTH",'+JSON.stringify(s)+"]")}catch(s){console.warn("subscribe auth function failed:",s)}}),this.authPromise)}async publish(e){this.idleSince=void 0,this.ongoingOperations++;const t=new Promise((i,r)=>{const s=setTimeout(()=>{const a=this.openEventPublishes.get(e.id);a&&(a.reject(new Error("publish timed out")),this.openEventPublishes.delete(e.id))},this.publishTimeout);this.openEventPublishes.set(e.id,{resolve:i,reject:r,timeout:s})});return this.send('["EVENT",'+JSON.stringify(e)+"]"),this.ongoingOperations--,this.ongoingOperations===0&&(this.idleSince=Date.now()),t}async count(e,t){this.serial++;const i=(t==null?void 0:t.id)||"count:"+this.serial,r=new Promise((s,a)=>{this.openCountRequests.set(i,{resolve:s,reject:a})});return this.send('["COUNT","'+i+'",'+JSON.stringify(e).substring(1)),r}subscribe(e,t){t.label!=="<forced-ping>"&&(this.idleSince=void 0,this.ongoingOperations++);const i=this.prepareSubscription(e,t);return i.fire(),t.abort&&(t.abort.onabort=()=>i.close(String(t.abort.reason||"<aborted>"))),i}prepareSubscription(e,t){this.serial++;const i=t.id||(t.label?t.label+":":"sub:")+this.serial,r=new Er(this,i,e,t);return this.openSubs.set(i,r),r}close(){var e,t,i;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.idleSince=void 0,(e=this.onclose)==null||e.call(this),((t=this.ws)==null?void 0:t.readyState)===this._WebSocket.OPEN&&((i=this.ws)==null||i.close())}_onmessage(e){var r,s,a;const t=e.data;if(!t)return;const i=gr(t);if(i){const l=this.openSubs.get(i);if(!l)return;const o=br(t,"id"),u=(r=l.alreadyHaveEvent)==null?void 0:r.call(l,o);if((s=l.receivedEvent)==null||s.call(l,this,o),u)return}try{let l=JSON.parse(t);switch(l[0]){case"EVENT":{const o=this.openSubs.get(l[1]),u=l[2];this.verifyEvent(u)&&yr(o.filters,u)&&o.onevent(u),(!o.lastEmitted||o.lastEmitted<u.created_at)&&(o.lastEmitted=u.created_at);return}case"COUNT":{const o=l[1],u=l[2],d=this.openCountRequests.get(o);d&&(d.resolve(u.count),this.openCountRequests.delete(o));return}case"EOSE":{const o=this.openSubs.get(l[1]);if(!o)return;o.receivedEose();return}case"OK":{const o=l[1],u=l[2],d=l[3],c=this.openEventPublishes.get(o);c&&(clearTimeout(c.timeout),u?c.resolve(d):c.reject(new Error(d)),this.openEventPublishes.delete(o));return}case"CLOSED":{const o=l[1],u=this.openSubs.get(o);if(!u)return;u.closed=!0,u.close(l[2]);return}case"NOTICE":{this.onnotice(l[1]);return}case"AUTH":{this.challenge=l[1],this.onauth&&this.auth(this.onauth);return}default:{const o=this.openSubs.get(l[1]);(a=o==null?void 0:o.oncustom)==null||a.call(o,l);return}}}catch(l){const[o,u,d]=JSON.parse(t);window.printer.maybe(d.pubkey,":: caught err",d,this.url,l);return}}},Er=class{constructor(e,t,i,r){h(this,"relay");h(this,"id");h(this,"lastEmitted");h(this,"closed",!1);h(this,"eosed",!1);h(this,"filters");h(this,"alreadyHaveEvent");h(this,"receivedEvent");h(this,"onevent");h(this,"oneose");h(this,"onclose");h(this,"oncustom");h(this,"eoseTimeout");h(this,"eoseTimeoutHandle");if(i.length===0)throw new Error("subscription can't be created with zero filters");this.relay=e,this.filters=i,this.id=t,this.alreadyHaveEvent=r.alreadyHaveEvent,this.receivedEvent=r.receivedEvent,this.eoseTimeout=r.eoseTimeout||e.baseEoseTimeout,this.oneose=r.oneose,this.onclose=r.onclose,this.onevent=r.onevent||(s=>{console.warn(`onevent() callback not defined for subscription '${this.id}' in relay ${this.relay.url}. event received:`,s)})}fire(){this.relay.send('["REQ","'+this.id+'",'+JSON.stringify(this.filters).substring(1)),this.eoseTimeoutHandle=setTimeout(this.receivedEose.bind(this),this.eoseTimeout)}receivedEose(){var e;this.eosed||(clearTimeout(this.eoseTimeoutHandle),this.eosed=!0,(e=this.oneose)==null||e.call(this))}close(e="closed by caller"){var t;if(!this.closed&&this.relay.connected){try{this.relay.send('["CLOSE",'+JSON.stringify(this.id)+"]")}catch(i){if(!(i instanceof Ln))throw i}this.closed=!0}this.relay.openSubs.delete(this.id),this.relay.ongoingOperations--,this.relay.ongoingOperations===0&&(this.relay.idleSince=Date.now()),(t=this.onclose)==null||t.call(this,e)}};const vr=n=>(n[ge]=!0,!0);let xr=class{constructor(e){h(this,"relays",new Map);h(this,"seenOn",new Map);h(this,"trackRelays",!1);h(this,"verifyEvent");h(this,"enablePing");h(this,"enableReconnect");h(this,"automaticallyAuth");h(this,"trustedRelayURLs",new Set);h(this,"onRelayConnectionFailure");h(this,"onRelayConnectionSuccess");h(this,"allowConnectingToRelay");h(this,"maxWaitForConnection");h(this,"_WebSocket");this.verifyEvent=e.verifyEvent,this._WebSocket=e.websocketImplementation,this.enablePing=e.enablePing,this.enableReconnect=e.enableReconnect||!1,this.automaticallyAuth=e.automaticallyAuth,this.onRelayConnectionFailure=e.onRelayConnectionFailure,this.onRelayConnectionSuccess=e.onRelayConnectionSuccess,this.allowConnectingToRelay=e.allowConnectingToRelay,this.maxWaitForConnection=e.maxWaitForConnection||3e3}async ensureRelay(e,t){e=X(e);let i=this.rel