signet-verify 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 forgesworn
3
+ Copyright (c) 2026 ForgeSworn
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -72,6 +72,26 @@ export interface WaitForAuthOptions {
72
72
  expectedOrigin: string;
73
73
  /** Timeout in milliseconds. Clamped to [5_000, 600_000]. Default 120_000. */
74
74
  timeout?: number;
75
+ /**
76
+ * Unix seconds. Only responses published at or after this are asked for.
77
+ * Defaults to 60 seconds before this call.
78
+ *
79
+ * Pass it whenever the wait is being RESTARTED for a sign-in that was already
80
+ * issued — a mobile consumer resuming after the user approved in the signer
81
+ * app, or a retry after an attempt timed out. The default anchors the window
82
+ * to the restart, which silently excludes a response published while the
83
+ * consumer was in the background, and the response is never asked for again.
84
+ * Anchor it to when the sign-in URL was opened instead:
85
+ *
86
+ * const startedAt = Math.floor(Date.now() / 1000);
87
+ * // …open the auth URL, user approves in the signer app…
88
+ * waitForAuthResponse({ …, since: startedAt - 60 });
89
+ *
90
+ * Staleness is bounded regardless: a response whose rumor is older than five
91
+ * minutes is rejected after unwrapping, and every response must carry this
92
+ * request's session tag and a signature over this challenge.
93
+ */
94
+ since?: number;
75
95
  }
76
96
  /** Full signed Kind-21236 auth event as carried in the AuthResponse. */
77
97
  export interface SignetAuthEvent {
@@ -1,22 +1,17 @@
1
- "use strict";var Signet=(()=>{var Le=Object.defineProperty;var Fo=Object.getOwnPropertyDescriptor;var ts=Object.getOwnPropertyNames;var es=Object.prototype.hasOwnProperty;var ns=(e,t,r)=>t in e?Le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var rs=(e,t)=>{for(var r in t)Le(e,r,{get:t[r],enumerable:!0})},os=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ts(t))!es.call(e,o)&&o!==r&&Le(e,o,{get:()=>t[o],enumerable:!(n=Fo(t,o))||n.enumerable});return e};var ss=e=>os(Le({},"__esModule",{value:!0}),e);var T=(e,t,r)=>ns(e,typeof t!="symbol"?t+"":t,r);var Ni={};rs(Ni,{verifyAge:()=>Jo,waitForAuthResponse:()=>Qo});var qt=typeof globalThis=="object"&&"crypto"in globalThis?globalThis.crypto:void 0;function Ct(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function ue(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function lt(e,...t){if(!Ct(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error("Uint8Array expected of length "+t+", got length="+e.length)}function _e(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash should be wrapped by utils.createHasher");ue(e.outputLen),ue(e.blockLen)}function Yt(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function ar(e,t){lt(e);let r=t.outputLen;if(e.length<r)throw new Error("digestInto() expects output buffer of length at least "+r)}function Vt(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function Ue(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function dt(e,t){return e<<32-t|e>>>t}var ur=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",is=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function st(e){if(lt(e),ur)return e.toHex();let t="";for(let r=0;r<e.length;r++)t+=is[e[r]];return t}var gt={_0:48,_9:57,A:65,F:70,a:97,f:102};function fr(e){if(e>=gt._0&&e<=gt._9)return e-gt._0;if(e>=gt.A&&e<=gt.F)return e-(gt.A-10);if(e>=gt.a&&e<=gt.f)return e-(gt.a-10)}function Dt(e){if(typeof e!="string")throw new Error("hex string expected, got "+typeof e);if(ur)return Uint8Array.fromHex(e);let t=e.length,r=t/2;if(t%2)throw new Error("hex string expected, got unpadded hex of length "+t);let n=new Uint8Array(r);for(let o=0,s=0;o<r;o++,s+=2){let i=fr(e.charCodeAt(s)),c=fr(e.charCodeAt(s+1));if(i===void 0||c===void 0){let f=e[s]+e[s+1];throw new Error('hex string expected, got non-hex character "'+f+'" at index '+s)}n[o]=i*16+c}return n}function Re(e){if(typeof e!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(e))}function le(e){return typeof e=="string"&&(e=Re(e)),lt(e),e}function ot(...e){let t=0;for(let n=0;n<e.length;n++){let o=e[n];lt(o),t+=o.length}let r=new Uint8Array(t);for(let n=0,o=0;n<e.length;n++){let s=e[n];r.set(s,o),o+=s.length}return r}var zt=class{};function lr(e){let t=n=>e().update(le(n)).digest(),r=e();return t.outputLen=r.outputLen,t.blockLen=r.blockLen,t.create=()=>e(),t}function Mt(e=32){if(qt&&typeof qt.getRandomValues=="function")return qt.getRandomValues(new Uint8Array(e));if(qt&&typeof qt.randomBytes=="function")return Uint8Array.from(qt.randomBytes(e));throw new Error("crypto.getRandomValues must be defined")}function cs(e,t,r,n){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,r,n);let o=BigInt(32),s=BigInt(4294967295),i=Number(r>>o&s),c=Number(r&s),f=n?4:0,u=n?0:4;e.setUint32(t+f,i,n),e.setUint32(t+u,c,n)}function dr(e,t,r){return e&t^~e&r}function hr(e,t,r){return e&t^e&r^t&r}var Oe=class extends zt{constructor(t,r,n,o){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=t,this.outputLen=r,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(t),this.view=Ue(this.buffer)}update(t){Yt(this),t=le(t),lt(t);let{view:r,buffer:n,blockLen:o}=this,s=t.length;for(let i=0;i<s;){let c=Math.min(o-this.pos,s-i);if(c===o){let f=Ue(t);for(;o<=s-i;i+=o)this.process(f,i);continue}n.set(t.subarray(i,i+c),this.pos),this.pos+=c,i+=c,this.pos===o&&(this.process(r,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){Yt(this),ar(t,this),this.finished=!0;let{buffer:r,view:n,blockLen:o,isLE:s}=this,{pos:i}=this;r[i++]=128,Vt(this.buffer.subarray(i)),this.padOffset>o-i&&(this.process(n,0),i=0);for(let g=i;g<o;g++)r[g]=0;cs(n,o-8,BigInt(this.length*8),s),this.process(n,0);let c=Ue(t),f=this.outputLen;if(f%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let u=f/4,h=this.get();if(u>h.length)throw new Error("_sha2: outputLen bigger than state");for(let g=0;g<u;g++)c.setUint32(4*g,h[g],s)}digest(){let{buffer:t,outputLen:r}=this;this.digestInto(t);let n=t.slice(0,r);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:r,buffer:n,length:o,finished:s,destroyed:i,pos:c}=this;return t.destroyed=i,t.finished=s,t.length=o,t.pos=c,o%r&&t.buffer.set(n),t}clone(){return this._cloneInto()}},xt=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var fs=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]),_t=new Uint32Array(64),He=class extends Oe{constructor(t=32){super(64,t,8,!1),this.A=xt[0]|0,this.B=xt[1]|0,this.C=xt[2]|0,this.D=xt[3]|0,this.E=xt[4]|0,this.F=xt[5]|0,this.G=xt[6]|0,this.H=xt[7]|0}get(){let{A:t,B:r,C:n,D:o,E:s,F:i,G:c,H:f}=this;return[t,r,n,o,s,i,c,f]}set(t,r,n,o,s,i,c,f){this.A=t|0,this.B=r|0,this.C=n|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=c|0,this.H=f|0}process(t,r){for(let g=0;g<16;g++,r+=4)_t[g]=t.getUint32(r,!1);for(let g=16;g<64;g++){let b=_t[g-15],d=_t[g-2],y=dt(b,7)^dt(b,18)^b>>>3,B=dt(d,17)^dt(d,19)^d>>>10;_t[g]=B+_t[g-7]+y+_t[g-16]|0}let{A:n,B:o,C:s,D:i,E:c,F:f,G:u,H:h}=this;for(let g=0;g<64;g++){let b=dt(c,6)^dt(c,11)^dt(c,25),d=h+b+dr(c,f,u)+fs[g]+_t[g]|0,B=(dt(n,2)^dt(n,13)^dt(n,22))+hr(n,o,s)|0;h=u,u=f,f=c,c=i+d|0,i=s,s=o,o=n,n=d+B|0}n=n+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,c=c+this.E|0,f=f+this.F|0,u=u+this.G|0,h=h+this.H|0,this.set(n,o,s,i,c,f,u,h)}roundClean(){Vt(_t)}destroy(){this.set(0,0,0,0,0,0,0,0),Vt(this.buffer)}};var Wt=lr(()=>new He);var ke=class extends zt{constructor(t,r){super(),this.finished=!1,this.destroyed=!1,_e(t);let n=le(r);if(this.iHash=t.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 o=this.blockLen,s=new Uint8Array(o);s.set(n.length>o?t.create().update(n).digest():n);for(let i=0;i<s.length;i++)s[i]^=54;this.iHash.update(s),this.oHash=t.create();for(let i=0;i<s.length;i++)s[i]^=106;this.oHash.update(s),Vt(s)}update(t){return Yt(this),this.iHash.update(t),this}digestInto(t){Yt(this),lt(t,this.outputLen),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){let t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));let{oHash:r,iHash:n,finished:o,destroyed:s,blockLen:i,outputLen:c}=this;return t=t,t.finished=o,t.destroyed=s,t.blockLen=i,t.outputLen=c,t.oHash=r._cloneInto(t.oHash),t.iHash=n._cloneInto(t.iHash),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},on=(e,t,r)=>new ke(e,t).update(r).digest();on.create=(e,t)=>new ke(e,t);var fn=BigInt(0),cn=BigInt(1);function de(e,t=""){if(typeof e!="boolean"){let r=t&&`"${t}"`;throw new Error(r+"expected boolean, got type="+typeof e)}return e}function Ut(e,t,r=""){let n=Ct(e),o=e?.length,s=t!==void 0;if(!n||s&&o!==t){let i=r&&`"${r}" `,c=s?` of length ${t}`:"",f=n?`length=${o}`:`type=${typeof e}`;throw new Error(i+"expected Uint8Array"+c+", got "+f)}return e}function he(e){let t=e.toString(16);return t.length&1?"0"+t:t}function br(e){if(typeof e!="string")throw new Error("hex string expected, got "+typeof e);return e===""?fn:BigInt("0x"+e)}function pt(e){return br(st(e))}function an(e){return lt(e),br(st(Uint8Array.from(e).reverse()))}function Xt(e,t){return Dt(e.toString(16).padStart(t*2,"0"))}function un(e,t){return Xt(e,t).reverse()}function X(e,t,r){let n;if(typeof t=="string")try{n=Dt(t)}catch(s){throw new Error(e+" must be hex string or Uint8Array, cause: "+s)}else if(Ct(t))n=Uint8Array.from(t);else throw new Error(e+" must be hex string or Uint8Array");let o=n.length;if(typeof r=="number"&&o!==r)throw new Error(e+" of length "+r+" expected, got "+o);return n}var sn=e=>typeof e=="bigint"&&fn<=e;function be(e,t,r){return sn(e)&&sn(t)&&sn(r)&&t<=e&&e<r}function gr(e,t,r,n){if(!be(t,r,n))throw new Error("expected valid "+e+": "+r+" <= n < "+n+", got "+t)}function Te(e){let t;for(t=0;e>fn;e>>=cn,t+=1);return t}var Rt=e=>(cn<<BigInt(e))-cn;function xr(e,t,r){if(typeof e!="number"||e<2)throw new Error("hashLen must be a number");if(typeof t!="number"||t<2)throw new Error("qByteLen must be a number");if(typeof r!="function")throw new Error("hmacFn must be a function");let n=d=>new Uint8Array(d),o=d=>Uint8Array.of(d),s=n(e),i=n(e),c=0,f=()=>{s.fill(1),i.fill(0),c=0},u=(...d)=>r(i,s,...d),h=(d=n(0))=>{i=u(o(0),d),s=u(),d.length!==0&&(i=u(o(1),d),s=u())},g=()=>{if(c++>=1e3)throw new Error("drbg: tried 1000 values");let d=0,y=[];for(;d<t;){s=u();let B=s.slice();y.push(B),d+=s.length}return ot(...y)};return(d,y)=>{f(),h(d);let B;for(;!(B=y(g()));)h();return f(),B}}function ge(e,t,r={}){if(!e||typeof e!="object")throw new Error("expected valid options object");function n(o,s,i){let c=e[o];if(i&&c===void 0)return;let f=typeof c;if(f!==s||c===null)throw new Error(`param "${o}" is invalid: expected ${s}, got ${f}`)}Object.entries(t).forEach(([o,s])=>n(o,s,!1)),Object.entries(r).forEach(([o,s])=>n(o,s,!0))}function ln(e){let t=new WeakMap;return(r,...n)=>{let o=t.get(r);if(o!==void 0)return o;let s=e(r,...n);return t.set(r,s),s}}var nt=BigInt(0),tt=BigInt(1),Zt=BigInt(2),wr=BigInt(3),mr=BigInt(4),Er=BigInt(5),as=BigInt(7),Br=BigInt(8),us=BigInt(9),vr=BigInt(16);function it(e,t){let r=e%t;return r>=nt?r:t+r}function ct(e,t,r){let n=e;for(;t-- >nt;)n*=n,n%=r;return n}function pr(e,t){if(e===nt)throw new Error("invert: expected non-zero number");if(t<=nt)throw new Error("invert: expected positive modulus, got "+t);let r=it(e,t),n=t,o=nt,s=tt,i=tt,c=nt;for(;r!==nt;){let u=n/r,h=n%r,g=o-i*u,b=s-c*u;n=r,r=h,o=i,s=c,i=g,c=b}if(n!==tt)throw new Error("invert: does not exist");return it(o,t)}function dn(e,t,r){if(!e.eql(e.sqr(t),r))throw new Error("Cannot find square root")}function Ar(e,t){let r=(e.ORDER+tt)/mr,n=e.pow(t,r);return dn(e,n,t),n}function ls(e,t){let r=(e.ORDER-Er)/Br,n=e.mul(t,Zt),o=e.pow(n,r),s=e.mul(t,o),i=e.mul(e.mul(s,Zt),o),c=e.mul(s,e.sub(i,e.ONE));return dn(e,c,t),c}function ds(e){let t=Ot(e),r=Sr(e),n=r(t,t.neg(t.ONE)),o=r(t,n),s=r(t,t.neg(n)),i=(e+as)/vr;return(c,f)=>{let u=c.pow(f,i),h=c.mul(u,n),g=c.mul(u,o),b=c.mul(u,s),d=c.eql(c.sqr(h),f),y=c.eql(c.sqr(g),f);u=c.cmov(u,h,d),h=c.cmov(b,g,y);let B=c.eql(c.sqr(h),f),S=c.cmov(u,h,B);return dn(c,S,f),S}}function Sr(e){if(e<wr)throw new Error("sqrt is not defined for small field");let t=e-tt,r=0;for(;t%Zt===nt;)t/=Zt,r++;let n=Zt,o=Ot(e);for(;yr(o,n)===1;)if(n++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(r===1)return Ar;let s=o.pow(n,t),i=(t+tt)/Zt;return function(f,u){if(f.is0(u))return u;if(yr(f,u)!==1)throw new Error("Cannot find square root");let h=r,g=f.mul(f.ONE,s),b=f.pow(u,t),d=f.pow(u,i);for(;!f.eql(b,f.ONE);){if(f.is0(b))return f.ZERO;let y=1,B=f.sqr(b);for(;!f.eql(B,f.ONE);)if(y++,B=f.sqr(B),y===h)throw new Error("Cannot find square root");let S=tt<<BigInt(h-y-1),_=f.pow(g,S);h=y,g=f.sqr(_),b=f.mul(b,g),d=f.mul(d,_)}return d}}function hs(e){return e%mr===wr?Ar:e%Br===Er?ls:e%vr===us?ds(e):Sr(e)}var bs=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function hn(e){let t={ORDER:"bigint",MASK:"bigint",BYTES:"number",BITS:"number"},r=bs.reduce((n,o)=>(n[o]="function",n),t);return ge(e,r),e}function gs(e,t,r){if(r<nt)throw new Error("invalid exponent, negatives unsupported");if(r===nt)return e.ONE;if(r===tt)return t;let n=e.ONE,o=t;for(;r>nt;)r&tt&&(n=e.mul(n,o)),o=e.sqr(o),r>>=tt;return n}function Ne(e,t,r=!1){let n=new Array(t.length).fill(r?e.ZERO:void 0),o=t.reduce((i,c,f)=>e.is0(c)?i:(n[f]=i,e.mul(i,c)),e.ONE),s=e.inv(o);return t.reduceRight((i,c,f)=>e.is0(c)?i:(n[f]=e.mul(i,n[f]),e.mul(i,c)),s),n}function yr(e,t){let r=(e.ORDER-tt)/Zt,n=e.pow(t,r),o=e.eql(n,e.ONE),s=e.eql(n,e.ZERO),i=e.eql(n,e.neg(e.ONE));if(!o&&!s&&!i)throw new Error("invalid Legendre symbol result");return o?1:s?0:-1}function qe(e,t){t!==void 0&&ue(t);let r=t!==void 0?t:e.toString(2).length,n=Math.ceil(r/8);return{nBitLength:r,nByteLength:n}}function Ot(e,t,r=!1,n={}){if(e<=nt)throw new Error("invalid field: expected ORDER > 0, got "+e);let o,s,i=!1,c;if(typeof t=="object"&&t!=null){if(n.sqrt||r)throw new Error("cannot specify opts in two arguments");let b=t;b.BITS&&(o=b.BITS),b.sqrt&&(s=b.sqrt),typeof b.isLE=="boolean"&&(r=b.isLE),typeof b.modFromBytes=="boolean"&&(i=b.modFromBytes),c=b.allowedLengths}else typeof t=="number"&&(o=t),n.sqrt&&(s=n.sqrt);let{nBitLength:f,nByteLength:u}=qe(e,o);if(u>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");let h,g=Object.freeze({ORDER:e,isLE:r,BITS:f,BYTES:u,MASK:Rt(f),ZERO:nt,ONE:tt,allowedLengths:c,create:b=>it(b,e),isValid:b=>{if(typeof b!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof b);return nt<=b&&b<e},is0:b=>b===nt,isValidNot0:b=>!g.is0(b)&&g.isValid(b),isOdd:b=>(b&tt)===tt,neg:b=>it(-b,e),eql:(b,d)=>b===d,sqr:b=>it(b*b,e),add:(b,d)=>it(b+d,e),sub:(b,d)=>it(b-d,e),mul:(b,d)=>it(b*d,e),pow:(b,d)=>gs(g,b,d),div:(b,d)=>it(b*pr(d,e),e),sqrN:b=>b*b,addN:(b,d)=>b+d,subN:(b,d)=>b-d,mulN:(b,d)=>b*d,inv:b=>pr(b,e),sqrt:s||(b=>(h||(h=hs(e)),h(g,b))),toBytes:b=>r?un(b,u):Xt(b,u),fromBytes:(b,d=!0)=>{if(c){if(!c.includes(b.length)||b.length>u)throw new Error("Field.fromBytes: expected "+c+" bytes, got "+b.length);let B=new Uint8Array(u);B.set(b,r?0:B.length-b.length),b=B}if(b.length!==u)throw new Error("Field.fromBytes: expected "+u+" bytes, got "+b.length);let y=r?an(b):pt(b);if(i&&(y=it(y,e)),!d&&!g.isValid(y))throw new Error("invalid field element: outside of range 0..ORDER");return y},invertBatch:b=>Ne(g,b),cmov:(b,d,y)=>y?d:b});return Object.freeze(g)}function Ir(e){if(typeof e!="bigint")throw new Error("field order must be bigint");let t=e.toString(2).length;return Math.ceil(t/8)}function bn(e){let t=Ir(e);return t+Math.ceil(t/2)}function Ce(e,t,r=!1){let n=e.length,o=Ir(t),s=bn(t);if(n<16||n<s||n>1024)throw new Error("expected "+s+"-1024 bytes of input, got "+n);let i=r?an(e):pt(e),c=it(i,t-tt)+tt;return r?un(c,o):Xt(c,o)}var Jt=BigInt(0),jt=BigInt(1);function xe(e,t){let r=t.negate();return e?r:t}function De(e,t){let r=Ne(e.Fp,t.map(n=>n.Z));return t.map((n,o)=>e.fromAffine(n.toAffine(r[o])))}function Rr(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+e)}function gn(e,t){Rr(e,t);let r=Math.ceil(t/e)+1,n=2**(e-1),o=2**e,s=Rt(e),i=BigInt(e);return{windows:r,windowSize:n,mask:s,maxNumber:o,shiftBy:i}}function Lr(e,t,r){let{windowSize:n,mask:o,maxNumber:s,shiftBy:i}=r,c=Number(e&o),f=e>>i;c>n&&(c-=s,f+=jt);let u=t*n,h=u+Math.abs(c)-1,g=c===0,b=c<0,d=t%2!==0;return{nextN:f,offset:h,isZero:g,isNeg:b,isNegF:d,offsetF:u}}function xs(e,t){if(!Array.isArray(e))throw new Error("array expected");e.forEach((r,n)=>{if(!(r instanceof t))throw new Error("invalid point at index "+n)})}function ps(e,t){if(!Array.isArray(e))throw new Error("array of scalars expected");e.forEach((r,n)=>{if(!t.isValid(r))throw new Error("invalid scalar at index "+n)})}var xn=new WeakMap,Or=new WeakMap;function pn(e){return Or.get(e)||1}function _r(e){if(e!==Jt)throw new Error("invalid wNAF")}var Ve=class{constructor(t,r){this.BASE=t.BASE,this.ZERO=t.ZERO,this.Fn=t.Fn,this.bits=r}_unsafeLadder(t,r,n=this.ZERO){let o=t;for(;r>Jt;)r&jt&&(n=n.add(o)),o=o.double(),r>>=jt;return n}precomputeWindow(t,r){let{windows:n,windowSize:o}=gn(r,this.bits),s=[],i=t,c=i;for(let f=0;f<n;f++){c=i,s.push(c);for(let u=1;u<o;u++)c=c.add(i),s.push(c);i=c.double()}return s}wNAF(t,r,n){if(!this.Fn.isValid(n))throw new Error("invalid scalar");let o=this.ZERO,s=this.BASE,i=gn(t,this.bits);for(let c=0;c<i.windows;c++){let{nextN:f,offset:u,isZero:h,isNeg:g,isNegF:b,offsetF:d}=Lr(n,c,i);n=f,h?s=s.add(xe(b,r[d])):o=o.add(xe(g,r[u]))}return _r(n),{p:o,f:s}}wNAFUnsafe(t,r,n,o=this.ZERO){let s=gn(t,this.bits);for(let i=0;i<s.windows&&n!==Jt;i++){let{nextN:c,offset:f,isZero:u,isNeg:h}=Lr(n,i,s);if(n=c,!u){let g=r[f];o=o.add(h?g.negate():g)}}return _r(n),o}getPrecomputes(t,r,n){let o=xn.get(r);return o||(o=this.precomputeWindow(r,t),t!==1&&(typeof n=="function"&&(o=n(o)),xn.set(r,o))),o}cached(t,r,n){let o=pn(t);return this.wNAF(o,this.getPrecomputes(o,t,n),r)}unsafe(t,r,n,o){let s=pn(t);return s===1?this._unsafeLadder(t,r,o):this.wNAFUnsafe(s,this.getPrecomputes(s,t,n),r,o)}createCache(t,r){Rr(r,this.bits),Or.set(t,r),xn.delete(t)}hasCache(t){return pn(t)!==1}};function Hr(e,t,r,n){let o=t,s=e.ZERO,i=e.ZERO;for(;r>Jt||n>Jt;)r&jt&&(s=s.add(o)),n&jt&&(i=i.add(o)),o=o.double(),r>>=jt,n>>=jt;return{p1:s,p2:i}}function kr(e,t,r,n){xs(r,e),ps(n,t);let o=r.length,s=n.length;if(o!==s)throw new Error("arrays of points and scalars must have equal length");let i=e.ZERO,c=Te(BigInt(o)),f=1;c>12?f=c-3:c>4?f=c-2:c>0&&(f=2);let u=Rt(f),h=new Array(Number(u)+1).fill(i),g=Math.floor((t.BITS-1)/f)*f,b=i;for(let d=g;d>=0;d-=f){h.fill(i);for(let B=0;B<s;B++){let S=n[B],_=Number(S>>BigInt(d)&u);h[_]=h[_].add(r[B])}let y=i;for(let B=h.length-1,S=i;B>0;B--)S=S.add(h[B]),y=y.add(S);if(b=b.add(y),d!==0)for(let B=0;B<f;B++)b=b.double()}return b}function Ur(e,t,r){if(t){if(t.ORDER!==e)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return hn(t),t}else return Ot(e,{isLE:r})}function Tr(e,t,r={},n){if(n===void 0&&(n=e==="edwards"),!t||typeof t!="object")throw new Error(`expected valid ${e} CURVE object`);for(let f of["p","n","h"]){let u=t[f];if(!(typeof u=="bigint"&&u>Jt))throw new Error(`CURVE.${f} must be positive bigint`)}let o=Ur(t.p,r.Fp,n),s=Ur(t.n,r.Fn,n),c=["Gx","Gy","a",e==="weierstrass"?"b":"d"];for(let f of c)if(!o.isValid(t[f]))throw new Error(`CURVE.${f} must be valid field element of CURVE.Fp`);return t=Object.freeze(Object.assign({},t)),{CURVE:t,Fp:o,Fn:s}}var Nr=(e,t)=>(e+(e>=0?t:-t)/qr)/t;function ys(e,t,r){let[[n,o],[s,i]]=t,c=Nr(i*e,r),f=Nr(-o*e,r),u=e-c*n-f*s,h=-c*o-f*i,g=u<wt,b=h<wt;g&&(u=-u),b&&(h=-h);let d=Rt(Math.ceil(Te(r)/2))+Qt;if(u<wt||u>=d||h<wt||h>=d)throw new Error("splitScalar (endomorphism): failed, k="+e);return{k1neg:g,k1:u,k2neg:b,k2:h}}function wn(e){if(!["compact","recovered","der"].includes(e))throw new Error('Signature format must be "compact", "recovered", or "der"');return e}function yn(e,t){let r={};for(let n of Object.keys(t))r[n]=e[n]===void 0?t[n]:e[n];return de(r.lowS,"lowS"),de(r.prehash,"prehash"),r.format!==void 0&&wn(r.format),r}var mn=class extends Error{constructor(t=""){super(t)}},yt={Err:mn,_tlv:{encode:(e,t)=>{let{Err:r}=yt;if(e<0||e>256)throw new r("tlv.encode: wrong tag");if(t.length&1)throw new r("tlv.encode: unpadded data");let n=t.length/2,o=he(n);if(o.length/2&128)throw new r("tlv.encode: long form length too big");let s=n>127?he(o.length/2|128):"";return he(e)+s+o+t},decode(e,t){let{Err:r}=yt,n=0;if(e<0||e>256)throw new r("tlv.encode: wrong tag");if(t.length<2||t[n++]!==e)throw new r("tlv.decode: wrong tlv");let o=t[n++],s=!!(o&128),i=0;if(!s)i=o;else{let f=o&127;if(!f)throw new r("tlv.decode(long): indefinite length not supported");if(f>4)throw new r("tlv.decode(long): byte length is too big");let u=t.subarray(n,n+f);if(u.length!==f)throw new r("tlv.decode: length bytes not complete");if(u[0]===0)throw new r("tlv.decode(long): zero leftmost byte");for(let h of u)i=i<<8|h;if(n+=f,i<128)throw new r("tlv.decode(long): not minimal encoding")}let c=t.subarray(n,n+i);if(c.length!==i)throw new r("tlv.decode: wrong value length");return{v:c,l:t.subarray(n+i)}}},_int:{encode(e){let{Err:t}=yt;if(e<wt)throw new t("integer: negative integers are not allowed");let r=he(e);if(Number.parseInt(r[0],16)&8&&(r="00"+r),r.length&1)throw new t("unexpected DER parsing assertion: unpadded hex");return r},decode(e){let{Err:t}=yt;if(e[0]&128)throw new t("invalid signature integer: negative");if(e[0]===0&&!(e[1]&128))throw new t("invalid signature integer: unnecessary leading zero");return pt(e)}},toSig(e){let{Err:t,_int:r,_tlv:n}=yt,o=X("signature",e),{v:s,l:i}=n.decode(48,o);if(i.length)throw new t("invalid signature: left bytes after parsing");let{v:c,l:f}=n.decode(2,s),{v:u,l:h}=n.decode(2,f);if(h.length)throw new t("invalid signature: left bytes after parsing");return{r:r.decode(c),s:r.decode(u)}},hexFromSig(e){let{_tlv:t,_int:r}=yt,n=t.encode(2,r.encode(e.r)),o=t.encode(2,r.encode(e.s)),s=n+o;return t.encode(48,s)}},wt=BigInt(0),Qt=BigInt(1),qr=BigInt(2),Me=BigInt(3),ws=BigInt(4);function Ht(e,t){let{BYTES:r}=e,n;if(typeof t=="bigint")n=t;else{let o=X("private key",t);try{n=e.fromBytes(o)}catch{throw new Error(`invalid private key: expected ui8a of size ${r}, got ${typeof t}`)}}if(!e.isValidNot0(n))throw new Error("invalid private key: out of range [1..N-1]");return n}function ms(e,t={}){let r=Tr("weierstrass",e,t),{Fp:n,Fn:o}=r,s=r.CURVE,{h:i,n:c}=s;ge(t,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object",wrapPrivateKey:"boolean"});let{endo:f}=t;if(f&&(!n.is0(s.a)||typeof f.beta!="bigint"||!Array.isArray(f.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');let u=Vr(n,o);function h(){if(!n.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function g(L,w,l){let{x:a,y:x}=w.toAffine(),m=n.toBytes(a);if(de(l,"isCompressed"),l){h();let v=!n.isOdd(x);return ot(Cr(v),m)}else return ot(Uint8Array.of(4),m,n.toBytes(x))}function b(L){Ut(L,void 0,"Point");let{publicKey:w,publicKeyUncompressed:l}=u,a=L.length,x=L[0],m=L.subarray(1);if(a===w&&(x===2||x===3)){let v=n.fromBytes(m);if(!n.isValid(v))throw new Error("bad point: is not on curve, wrong x");let p=B(v),A;try{A=n.sqrt(p)}catch(O){let D=O instanceof Error?": "+O.message:"";throw new Error("bad point: is not on curve, sqrt error"+D)}h();let E=n.isOdd(A);return(x&1)===1!==E&&(A=n.neg(A)),{x:v,y:A}}else if(a===l&&x===4){let v=n.BYTES,p=n.fromBytes(m.subarray(0,v)),A=n.fromBytes(m.subarray(v,v*2));if(!S(p,A))throw new Error("bad point: is not on curve");return{x:p,y:A}}else throw new Error(`bad point: got length ${a}, expected compressed=${w} or uncompressed=${l}`)}let d=t.toBytes||g,y=t.fromBytes||b;function B(L){let w=n.sqr(L),l=n.mul(w,L);return n.add(n.add(l,n.mul(L,s.a)),s.b)}function S(L,w){let l=n.sqr(w),a=B(L);return n.eql(l,a)}if(!S(s.Gx,s.Gy))throw new Error("bad curve params: generator point");let _=n.mul(n.pow(s.a,Me),ws),V=n.mul(n.sqr(s.b),BigInt(27));if(n.is0(n.add(_,V)))throw new Error("bad curve params: a or b");function q(L,w,l=!1){if(!n.isValid(w)||l&&n.is0(w))throw new Error(`bad point coordinate ${L}`);return w}function N(L){if(!(L instanceof U))throw new Error("ProjectivePoint expected")}function C(L){if(!f||!f.basises)throw new Error("no endo");return ys(L,f.basises,o.ORDER)}let P=ln((L,w)=>{let{X:l,Y:a,Z:x}=L;if(n.eql(x,n.ONE))return{x:l,y:a};let m=L.is0();w==null&&(w=m?n.ONE:n.inv(x));let v=n.mul(l,w),p=n.mul(a,w),A=n.mul(x,w);if(m)return{x:n.ZERO,y:n.ZERO};if(!n.eql(A,n.ONE))throw new Error("invZ was invalid");return{x:v,y:p}}),Y=ln(L=>{if(L.is0()){if(t.allowInfinityPoint&&!n.is0(L.Y))return;throw new Error("bad point: ZERO")}let{x:w,y:l}=L.toAffine();if(!n.isValid(w)||!n.isValid(l))throw new Error("bad point: x or y not field elements");if(!S(w,l))throw new Error("bad point: equation left != right");if(!L.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function z(L,w,l,a,x){return l=new U(n.mul(l.X,L),l.Y,l.Z),w=xe(a,w),l=xe(x,l),w.add(l)}class U{constructor(w,l,a){this.X=q("x",w),this.Y=q("y",l,!0),this.Z=q("z",a),Object.freeze(this)}static CURVE(){return s}static fromAffine(w){let{x:l,y:a}=w||{};if(!w||!n.isValid(l)||!n.isValid(a))throw new Error("invalid affine point");if(w instanceof U)throw new Error("projective point not allowed");return n.is0(l)&&n.is0(a)?U.ZERO:new U(l,a,n.ONE)}static fromBytes(w){let l=U.fromAffine(y(Ut(w,void 0,"point")));return l.assertValidity(),l}static fromHex(w){return U.fromBytes(X("pointHex",w))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(w=8,l=!0){return M.createCache(this,w),l||this.multiply(Me),this}assertValidity(){Y(this)}hasEvenY(){let{y:w}=this.toAffine();if(!n.isOdd)throw new Error("Field doesn't support isOdd");return!n.isOdd(w)}equals(w){N(w);let{X:l,Y:a,Z:x}=this,{X:m,Y:v,Z:p}=w,A=n.eql(n.mul(l,p),n.mul(m,x)),E=n.eql(n.mul(a,p),n.mul(v,x));return A&&E}negate(){return new U(this.X,n.neg(this.Y),this.Z)}double(){let{a:w,b:l}=s,a=n.mul(l,Me),{X:x,Y:m,Z:v}=this,p=n.ZERO,A=n.ZERO,E=n.ZERO,I=n.mul(x,x),O=n.mul(m,m),D=n.mul(v,v),k=n.mul(x,m);return k=n.add(k,k),E=n.mul(x,v),E=n.add(E,E),p=n.mul(w,E),A=n.mul(a,D),A=n.add(p,A),p=n.sub(O,A),A=n.add(O,A),A=n.mul(p,A),p=n.mul(k,p),E=n.mul(a,E),D=n.mul(w,D),k=n.sub(I,D),k=n.mul(w,k),k=n.add(k,E),E=n.add(I,I),I=n.add(E,I),I=n.add(I,D),I=n.mul(I,k),A=n.add(A,I),D=n.mul(m,v),D=n.add(D,D),I=n.mul(D,k),p=n.sub(p,I),E=n.mul(D,O),E=n.add(E,E),E=n.add(E,E),new U(p,A,E)}add(w){N(w);let{X:l,Y:a,Z:x}=this,{X:m,Y:v,Z:p}=w,A=n.ZERO,E=n.ZERO,I=n.ZERO,O=s.a,D=n.mul(s.b,Me),k=n.mul(l,m),R=n.mul(a,v),Z=n.mul(x,p),G=n.add(l,a),j=n.add(m,v);G=n.mul(G,j),j=n.add(k,R),G=n.sub(G,j),j=n.add(l,x);let $=n.add(m,p);return j=n.mul(j,$),$=n.add(k,Z),j=n.sub(j,$),$=n.add(a,x),A=n.add(v,p),$=n.mul($,A),A=n.add(R,Z),$=n.sub($,A),I=n.mul(O,j),A=n.mul(D,Z),I=n.add(A,I),A=n.sub(R,I),I=n.add(R,I),E=n.mul(A,I),R=n.add(k,k),R=n.add(R,k),Z=n.mul(O,Z),j=n.mul(D,j),R=n.add(R,Z),Z=n.sub(k,Z),Z=n.mul(O,Z),j=n.add(j,Z),k=n.mul(R,j),E=n.add(E,k),k=n.mul($,j),A=n.mul(G,A),A=n.sub(A,k),k=n.mul(G,R),I=n.mul($,I),I=n.add(I,k),new U(A,E,I)}subtract(w){return this.add(w.negate())}is0(){return this.equals(U.ZERO)}multiply(w){let{endo:l}=t;if(!o.isValidNot0(w))throw new Error("invalid scalar: out of range");let a,x,m=v=>M.cached(this,v,p=>De(U,p));if(l){let{k1neg:v,k1:p,k2neg:A,k2:E}=C(w),{p:I,f:O}=m(p),{p:D,f:k}=m(E);x=O.add(k),a=z(l.beta,I,D,v,A)}else{let{p:v,f:p}=m(w);a=v,x=p}return De(U,[a,x])[0]}multiplyUnsafe(w){let{endo:l}=t,a=this;if(!o.isValid(w))throw new Error("invalid scalar: out of range");if(w===wt||a.is0())return U.ZERO;if(w===Qt)return a;if(M.hasCache(this))return this.multiply(w);if(l){let{k1neg:x,k1:m,k2neg:v,k2:p}=C(w),{p1:A,p2:E}=Hr(U,a,m,p);return z(l.beta,A,E,x,v)}else return M.unsafe(a,w)}multiplyAndAddUnsafe(w,l,a){let x=this.multiplyUnsafe(l).add(w.multiplyUnsafe(a));return x.is0()?void 0:x}toAffine(w){return P(this,w)}isTorsionFree(){let{isTorsionFree:w}=t;return i===Qt?!0:w?w(U,this):M.unsafe(this,c).is0()}clearCofactor(){let{clearCofactor:w}=t;return i===Qt?this:w?w(U,this):this.multiplyUnsafe(i)}isSmallOrder(){return this.multiplyUnsafe(i).is0()}toBytes(w=!0){return de(w,"isCompressed"),this.assertValidity(),d(U,this,w)}toHex(w=!0){return st(this.toBytes(w))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}get px(){return this.X}get py(){return this.X}get pz(){return this.Z}toRawBytes(w=!0){return this.toBytes(w)}_setWindowSize(w){this.precompute(w)}static normalizeZ(w){return De(U,w)}static msm(w,l){return kr(U,o,w,l)}static fromPrivateKey(w){return U.BASE.multiply(Ht(o,w))}}U.BASE=new U(s.Gx,s.Gy,n.ONE),U.ZERO=new U(n.ZERO,n.ONE,n.ZERO),U.Fp=n,U.Fn=o;let K=o.BITS,M=new Ve(U,t.endo?Math.ceil(K/2):K);return U.BASE.precompute(8),U}function Cr(e){return Uint8Array.of(e?2:3)}function Vr(e,t){return{secretKey:t.BYTES,publicKey:1+e.BYTES,publicKeyUncompressed:1+2*e.BYTES,publicKeyHasPrefix:!0,signature:2*t.BYTES}}function Es(e,t={}){let{Fn:r}=e,n=t.randomBytes||Mt,o=Object.assign(Vr(e.Fp,r),{seed:bn(r.ORDER)});function s(d){try{return!!Ht(r,d)}catch{return!1}}function i(d,y){let{publicKey:B,publicKeyUncompressed:S}=o;try{let _=d.length;return y===!0&&_!==B||y===!1&&_!==S?!1:!!e.fromBytes(d)}catch{return!1}}function c(d=n(o.seed)){return Ce(Ut(d,o.seed,"seed"),r.ORDER)}function f(d,y=!0){return e.BASE.multiply(Ht(r,d)).toBytes(y)}function u(d){let y=c(d);return{secretKey:y,publicKey:f(y)}}function h(d){if(typeof d=="bigint")return!1;if(d instanceof e)return!0;let{secretKey:y,publicKey:B,publicKeyUncompressed:S}=o;if(r.allowedLengths||y===B)return;let _=X("key",d).length;return _===B||_===S}function g(d,y,B=!0){if(h(d)===!0)throw new Error("first arg must be private key");if(h(y)===!1)throw new Error("second arg must be public key");let S=Ht(r,d);return e.fromHex(y).multiply(S).toBytes(B)}return Object.freeze({getPublicKey:f,getSharedSecret:g,keygen:u,Point:e,utils:{isValidSecretKey:s,isValidPublicKey:i,randomSecretKey:c,isValidPrivateKey:s,randomPrivateKey:c,normPrivateKeyToScalar:d=>Ht(r,d),precompute(d=8,y=e.BASE){return y.precompute(d,!1)}},lengths:o})}function Bs(e,t,r={}){_e(t),ge(r,{},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"});let n=r.randomBytes||Mt,o=r.hmac||((l,...a)=>on(t,l,ot(...a))),{Fp:s,Fn:i}=e,{ORDER:c,BITS:f}=i,{keygen:u,getPublicKey:h,getSharedSecret:g,utils:b,lengths:d}=Es(e,r),y={prehash:!1,lowS:typeof r.lowS=="boolean"?r.lowS:!1,format:void 0,extraEntropy:!1},B="compact";function S(l){let a=c>>Qt;return l>a}function _(l,a){if(!i.isValidNot0(a))throw new Error(`invalid signature ${l}: out of range 1..Point.Fn.ORDER`);return a}function V(l,a){wn(a);let x=d.signature,m=a==="compact"?x:a==="recovered"?x+1:void 0;return Ut(l,m,`${a} signature`)}class q{constructor(a,x,m){this.r=_("r",a),this.s=_("s",x),m!=null&&(this.recovery=m),Object.freeze(this)}static fromBytes(a,x=B){V(a,x);let m;if(x==="der"){let{r:E,s:I}=yt.toSig(Ut(a));return new q(E,I)}x==="recovered"&&(m=a[0],x="compact",a=a.subarray(1));let v=i.BYTES,p=a.subarray(0,v),A=a.subarray(v,v*2);return new q(i.fromBytes(p),i.fromBytes(A),m)}static fromHex(a,x){return this.fromBytes(Dt(a),x)}addRecoveryBit(a){return new q(this.r,this.s,a)}recoverPublicKey(a){let x=s.ORDER,{r:m,s:v,recovery:p}=this;if(p==null||![0,1,2,3].includes(p))throw new Error("recovery id invalid");if(c*qr<x&&p>1)throw new Error("recovery id is ambiguous for h>1 curve");let E=p===2||p===3?m+c:m;if(!s.isValid(E))throw new Error("recovery id 2 or 3 invalid");let I=s.toBytes(E),O=e.fromBytes(ot(Cr((p&1)===0),I)),D=i.inv(E),k=C(X("msgHash",a)),R=i.create(-k*D),Z=i.create(v*D),G=e.BASE.multiplyUnsafe(R).add(O.multiplyUnsafe(Z));if(G.is0())throw new Error("point at infinify");return G.assertValidity(),G}hasHighS(){return S(this.s)}toBytes(a=B){if(wn(a),a==="der")return Dt(yt.hexFromSig(this));let x=i.toBytes(this.r),m=i.toBytes(this.s);if(a==="recovered"){if(this.recovery==null)throw new Error("recovery bit must be present");return ot(Uint8Array.of(this.recovery),x,m)}return ot(x,m)}toHex(a){return st(this.toBytes(a))}assertValidity(){}static fromCompact(a){return q.fromBytes(X("sig",a),"compact")}static fromDER(a){return q.fromBytes(X("sig",a),"der")}normalizeS(){return this.hasHighS()?new q(this.r,i.neg(this.s),this.recovery):this}toDERRawBytes(){return this.toBytes("der")}toDERHex(){return st(this.toBytes("der"))}toCompactRawBytes(){return this.toBytes("compact")}toCompactHex(){return st(this.toBytes("compact"))}}let N=r.bits2int||function(a){if(a.length>8192)throw new Error("input is too large");let x=pt(a),m=a.length*8-f;return m>0?x>>BigInt(m):x},C=r.bits2int_modN||function(a){return i.create(N(a))},P=Rt(f);function Y(l){return gr("num < 2^"+f,l,wt,P),i.toBytes(l)}function z(l,a){return Ut(l,void 0,"message"),a?Ut(t(l),void 0,"prehashed message"):l}function U(l,a,x){if(["recovered","canonical"].some(R=>R in x))throw new Error("sign() legacy options not supported");let{lowS:m,prehash:v,extraEntropy:p}=yn(x,y);l=z(l,v);let A=C(l),E=Ht(i,a),I=[Y(E),Y(A)];if(p!=null&&p!==!1){let R=p===!0?n(d.secretKey):p;I.push(X("extraEntropy",R))}let O=ot(...I),D=A;function k(R){let Z=N(R);if(!i.isValidNot0(Z))return;let G=i.inv(Z),j=e.BASE.multiply(Z).toAffine(),$=i.create(j.x);if($===wt)return;let J=i.create(G*i.create(D+$*E));if(J===wt)return;let fe=(j.x===$?0:2)|Number(j.y&Qt),ae=J;return m&&S(J)&&(ae=i.neg(J),fe^=1),new q($,ae,fe)}return{seed:O,k2sig:k}}function K(l,a,x={}){l=X("message",l);let{seed:m,k2sig:v}=U(l,a,x);return xr(t.outputLen,i.BYTES,o)(m,v)}function M(l){let a,x=typeof l=="string"||Ct(l),m=!x&&l!==null&&typeof l=="object"&&typeof l.r=="bigint"&&typeof l.s=="bigint";if(!x&&!m)throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");if(m)a=new q(l.r,l.s);else if(x){try{a=q.fromBytes(X("sig",l),"der")}catch(v){if(!(v instanceof yt.Err))throw v}if(!a)try{a=q.fromBytes(X("sig",l),"compact")}catch{return!1}}return a||!1}function L(l,a,x,m={}){let{lowS:v,prehash:p,format:A}=yn(m,y);if(x=X("publicKey",x),a=z(X("message",a),p),"strict"in m)throw new Error("options.strict was renamed to lowS");let E=A===void 0?M(l):q.fromBytes(X("sig",l),A);if(E===!1)return!1;try{let I=e.fromBytes(x);if(v&&E.hasHighS())return!1;let{r:O,s:D}=E,k=C(a),R=i.inv(D),Z=i.create(k*R),G=i.create(O*R),j=e.BASE.multiplyUnsafe(Z).add(I.multiplyUnsafe(G));return j.is0()?!1:i.create(j.x)===O}catch{return!1}}function w(l,a,x={}){let{prehash:m}=yn(x,y);return a=z(a,m),q.fromBytes(l,"recovered").recoverPublicKey(a).toBytes()}return Object.freeze({keygen:u,getPublicKey:h,getSharedSecret:g,utils:b,lengths:d,Point:e,sign:K,verify:L,recoverPublicKey:w,Signature:q,hash:t})}function vs(e){let t={a:e.a,b:e.b,p:e.Fp.ORDER,n:e.n,h:e.h,Gx:e.Gx,Gy:e.Gy},r=e.Fp,n=e.allowedPrivateKeyLengths?Array.from(new Set(e.allowedPrivateKeyLengths.map(i=>Math.ceil(i/2)))):void 0,o=Ot(t.n,{BITS:e.nBitLength,allowedLengths:n,modFromBytes:e.wrapPrivateKey}),s={Fp:r,Fn:o,allowInfinityPoint:e.allowInfinityPoint,endo:e.endo,isTorsionFree:e.isTorsionFree,clearCofactor:e.clearCofactor,fromBytes:e.fromBytes,toBytes:e.toBytes};return{CURVE:t,curveOpts:s}}function As(e){let{CURVE:t,curveOpts:r}=vs(e),n={hmac:e.hmac,randomBytes:e.randomBytes,lowS:e.lowS,bits2int:e.bits2int,bits2int_modN:e.bits2int_modN};return{CURVE:t,curveOpts:r,hash:e.hash,ecdsaOpts:n}}function Ss(e,t){let r=t.Point;return Object.assign({},t,{ProjectivePoint:r,CURVE:Object.assign({},e,qe(r.Fn.ORDER,r.Fn.BITS))})}function Dr(e){let{CURVE:t,curveOpts:r,hash:n,ecdsaOpts:o}=As(e),s=ms(t,r),i=Bs(s,n,o);return Ss(e,i)}function Mr(e,t){let r=n=>Dr({...e,hash:n});return{...r(t),create:r}}var Ft={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},Is={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]},Ls=BigInt(0),Zr=BigInt(1),En=BigInt(2);function _s(e){let t=Ft.p,r=BigInt(3),n=BigInt(6),o=BigInt(11),s=BigInt(22),i=BigInt(23),c=BigInt(44),f=BigInt(88),u=e*e*e%t,h=u*u*e%t,g=ct(h,r,t)*h%t,b=ct(g,r,t)*h%t,d=ct(b,En,t)*u%t,y=ct(d,o,t)*d%t,B=ct(y,s,t)*y%t,S=ct(B,c,t)*B%t,_=ct(S,f,t)*S%t,V=ct(_,c,t)*B%t,q=ct(V,r,t)*h%t,N=ct(q,i,t)*y%t,C=ct(N,n,t)*u%t,P=ct(C,En,t);if(!Ze.eql(Ze.sqr(P),e))throw new Error("Cannot find square root");return P}var Ze=Ot(Ft.p,{sqrt:_s}),$r=Mr({...Ft,Fp:Ze,lowS:!0,endo:Is},Wt),jr={};function je(e,...t){let r=jr[e];if(r===void 0){let n=Wt(Re(e));r=ot(n,n),jr[e]=r}return Wt(ot(r,...t))}var vn=e=>e.toBytes(!0).slice(1),te=$r.Point,An=e=>e%En===Ls;function Bn(e){let{Fn:t,BASE:r}=te,n=Ht(t,e),o=r.multiply(n);return{scalar:An(o.y)?n:t.neg(n),bytes:vn(o)}}function Pr(e){let t=Ze;if(!t.isValidNot0(e))throw new Error("invalid x: Fail if x \u2265 p");let r=t.create(e*e),n=t.create(r*e+BigInt(7)),o=t.sqrt(n);An(o)||(o=t.neg(o));let s=te.fromAffine({x:e,y:o});return s.assertValidity(),s}var pe=pt;function Gr(...e){return te.Fn.create(pe(je("BIP0340/challenge",...e)))}function Kr(e){return Bn(e).bytes}function Us(e,t,r=Mt(32)){let{Fn:n}=te,o=X("message",e),{bytes:s,scalar:i}=Bn(t),c=X("auxRand",r,32),f=n.toBytes(i^pe(je("BIP0340/aux",c))),u=je("BIP0340/nonce",f,s,o),{bytes:h,scalar:g}=Bn(u),b=Gr(h,s,o),d=new Uint8Array(64);if(d.set(h,0),d.set(n.toBytes(n.create(g+b*i)),32),!zr(d,o,s))throw new Error("sign: Invalid signature produced");return d}function zr(e,t,r){let{Fn:n,BASE:o}=te,s=X("signature",e,64),i=X("message",t),c=X("publicKey",r,32);try{let f=Pr(pe(c)),u=pe(s.subarray(0,32));if(!be(u,Zr,Ft.p))return!1;let h=pe(s.subarray(32,64));if(!be(h,Zr,Ft.n))return!1;let g=Gr(n.toBytes(u),vn(f),i),b=o.multiplyUnsafe(h).add(f.multiplyUnsafe(n.neg(g))),{x:d,y}=b.toAffine();return!(b.is0()||!An(y)||d!==u)}catch{return!1}}var ye=(()=>{let r=(o=Mt(48))=>Ce(o,Ft.n);$r.utils.randomSecretKey;function n(o){let s=r(o);return{secretKey:s,publicKey:Kr(s)}}return{keygen:n,getPublicKey:Kr,sign:Us,verify:zr,Point:te,utils:{randomSecretKey:r,randomPrivateKey:r,taggedHash:je,lift_x:Pr,pointToBytes:vn,numberToBytesBE:Xt,bytesToNumberBE:pt,mod:it},lengths:{secretKey:32,publicKey:32,publicKeyHasPrefix:!1,signature:64,seed:48}}})();var Sn=Wt;function Rs(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function Ke(e){if(typeof e!="boolean")throw new Error(`boolean expected, not ${e}`)}function $e(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function Q(e,t,r=""){let n=Rs(e),o=e?.length,s=t!==void 0;if(!n||s&&o!==t){let i=r&&`"${r}" `,c=s?` of length ${t}`:"",f=n?`length=${o}`:`type=${typeof e}`;throw new Error(i+"expected Uint8Array"+c+", got "+f)}return e}function In(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function Yr(e,t){Q(e,void 0,"output");let r=t.outputLen;if(e.length<r)throw new Error("digestInto() expects output buffer of length at least "+r)}function mt(e){return new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4))}function Et(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function Os(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}var Hs=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function Wr(e,t){if(t==null||typeof t!="object")throw new Error("options must be defined");return Object.assign(e,t)}function Pe(e,t){if(e.length!==t.length)return!1;let r=0;for(let n=0;n<e.length;n++)r|=e[n]^t[n];return r===0}var Ln=(e,t)=>{function r(n,...o){if(Q(n,void 0,"key"),!Hs)throw new Error("Non little-endian hardware is not yet supported");if(e.nonceLength!==void 0){let h=o[0];Q(h,e.varSizeNonce?void 0:e.nonceLength,"nonce")}let s=e.tagLength;s&&o[1]!==void 0&&Q(o[1],void 0,"AAD");let i=t(n,...o),c=(h,g)=>{if(g!==void 0){if(h!==2)throw new Error("cipher output not supported");Q(g,void 0,"output")}},f=!1;return{encrypt(h,g){if(f)throw new Error("cannot encrypt() twice with same key + nonce");return f=!0,Q(h),c(i.encrypt.length,g),i.encrypt(h,g)},decrypt(h,g){if(Q(h),s&&h.length<s)throw new Error('"ciphertext" expected length bigger than tagLength='+s);return c(i.decrypt.length,g),i.decrypt(h,g)}}}return Object.assign(r,e),r};function _n(e,t,r=!0){if(t===void 0)return new Uint8Array(e);if(t.length!==e)throw new Error('"output" expected Uint8Array of length '+e+", got: "+t.length);if(r&&!ks(t))throw new Error("invalid output, must be aligned");return t}function Xr(e,t,r){Ke(r);let n=new Uint8Array(16),o=Os(n);return o.setBigUint64(0,BigInt(t),r),o.setBigUint64(8,BigInt(e),r),n}function ks(e){return e.byteOffset%4===0}function ee(e){return Uint8Array.from(e)}var Qr=e=>Uint8Array.from(e.split(""),t=>t.charCodeAt(0)),Ts=Qr("expand 16-byte k"),Ns=Qr("expand 32-byte k"),qs=mt(Ts),Cs=mt(Ns);function H(e,t){return e<<t|e>>>32-t}function Un(e){return e.byteOffset%4===0}var Ge=64,Vs=16,Fr=2**32-1,Jr=Uint32Array.of();function Ds(e,t,r,n,o,s,i,c){let f=o.length,u=new Uint8Array(Ge),h=mt(u),g=Un(o)&&Un(s),b=g?mt(o):Jr,d=g?mt(s):Jr;for(let y=0;y<f;i++){if(e(t,r,n,h,i,c),i>=Fr)throw new Error("arx: counter overflow");let B=Math.min(Ge,f-y);if(g&&B===Ge){let S=y/4;if(y%4!==0)throw new Error("arx: invalid block position");for(let _=0,V;_<Vs;_++)V=S+_,d[V]=b[V]^h[_];y+=Ge;continue}for(let S=0,_;S<B;S++)_=y+S,s[_]=o[_]^u[S];y+=B}}function Rn(e,t){let{allowShortKeys:r,extendNonceFn:n,counterLength:o,counterRight:s,rounds:i}=Wr({allowShortKeys:!1,counterLength:8,counterRight:!1,rounds:20},t);if(typeof e!="function")throw new Error("core must be a function");return $e(o),$e(i),Ke(s),Ke(r),(c,f,u,h,g=0)=>{Q(c,void 0,"key"),Q(f,void 0,"nonce"),Q(u,void 0,"data");let b=u.length;if(h===void 0&&(h=new Uint8Array(b)),Q(h,void 0,"output"),$e(g),g<0||g>=Fr)throw new Error("arx: counter overflow");if(h.length<b)throw new Error(`arx: output (${h.length}) is shorter than data (${b})`);let d=[],y=c.length,B,S;if(y===32)d.push(B=ee(c)),S=Cs;else if(y===16&&r)B=new Uint8Array(32),B.set(c),B.set(c,16),S=qs,d.push(B);else throw Q(c,32,"arx key"),new Error("invalid key size");Un(f)||d.push(f=ee(f));let _=mt(B);if(n){if(f.length!==24)throw new Error("arx: extended nonce must be 24 bytes");n(S,_,mt(f.subarray(0,16)),_),f=f.subarray(16)}let V=16-o;if(V!==f.length)throw new Error(`arx: nonce must be ${V} or 16 bytes`);if(V!==12){let N=new Uint8Array(12);N.set(f,s?0:12-f.length),f=N,d.push(f)}let q=mt(f);return Ds(e,S,_,q,u,h,g,i),Et(...d),h}}function F(e,t){return e[t++]&255|(e[t++]&255)<<8}var On=class{constructor(t){T(this,"blockLen",16);T(this,"outputLen",16);T(this,"buffer",new Uint8Array(16));T(this,"r",new Uint16Array(10));T(this,"h",new Uint16Array(10));T(this,"pad",new Uint16Array(8));T(this,"pos",0);T(this,"finished",!1);t=ee(Q(t,32,"key"));let r=F(t,0),n=F(t,2),o=F(t,4),s=F(t,6),i=F(t,8),c=F(t,10),f=F(t,12),u=F(t,14);this.r[0]=r&8191,this.r[1]=(r>>>13|n<<3)&8191,this.r[2]=(n>>>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|f<<5)&8065,this.r[8]=(f>>>8|u<<8)&8191,this.r[9]=u>>>5&127;for(let h=0;h<8;h++)this.pad[h]=F(t,16+2*h)}process(t,r,n=!1){let o=n?0:2048,{h:s,r:i}=this,c=i[0],f=i[1],u=i[2],h=i[3],g=i[4],b=i[5],d=i[6],y=i[7],B=i[8],S=i[9],_=F(t,r+0),V=F(t,r+2),q=F(t,r+4),N=F(t,r+6),C=F(t,r+8),P=F(t,r+10),Y=F(t,r+12),z=F(t,r+14),U=s[0]+(_&8191),K=s[1]+((_>>>13|V<<3)&8191),M=s[2]+((V>>>10|q<<6)&8191),L=s[3]+((q>>>7|N<<9)&8191),w=s[4]+((N>>>4|C<<12)&8191),l=s[5]+(C>>>1&8191),a=s[6]+((C>>>14|P<<2)&8191),x=s[7]+((P>>>11|Y<<5)&8191),m=s[8]+((Y>>>8|z<<8)&8191),v=s[9]+(z>>>5|o),p=0,A=p+U*c+K*(5*S)+M*(5*B)+L*(5*y)+w*(5*d);p=A>>>13,A&=8191,A+=l*(5*b)+a*(5*g)+x*(5*h)+m*(5*u)+v*(5*f),p+=A>>>13,A&=8191;let E=p+U*f+K*c+M*(5*S)+L*(5*B)+w*(5*y);p=E>>>13,E&=8191,E+=l*(5*d)+a*(5*b)+x*(5*g)+m*(5*h)+v*(5*u),p+=E>>>13,E&=8191;let I=p+U*u+K*f+M*c+L*(5*S)+w*(5*B);p=I>>>13,I&=8191,I+=l*(5*y)+a*(5*d)+x*(5*b)+m*(5*g)+v*(5*h),p+=I>>>13,I&=8191;let O=p+U*h+K*u+M*f+L*c+w*(5*S);p=O>>>13,O&=8191,O+=l*(5*B)+a*(5*y)+x*(5*d)+m*(5*b)+v*(5*g),p+=O>>>13,O&=8191;let D=p+U*g+K*h+M*u+L*f+w*c;p=D>>>13,D&=8191,D+=l*(5*S)+a*(5*B)+x*(5*y)+m*(5*d)+v*(5*b),p+=D>>>13,D&=8191;let k=p+U*b+K*g+M*h+L*u+w*f;p=k>>>13,k&=8191,k+=l*c+a*(5*S)+x*(5*B)+m*(5*y)+v*(5*d),p+=k>>>13,k&=8191;let R=p+U*d+K*b+M*g+L*h+w*u;p=R>>>13,R&=8191,R+=l*f+a*c+x*(5*S)+m*(5*B)+v*(5*y),p+=R>>>13,R&=8191;let Z=p+U*y+K*d+M*b+L*g+w*h;p=Z>>>13,Z&=8191,Z+=l*u+a*f+x*c+m*(5*S)+v*(5*B),p+=Z>>>13,Z&=8191;let G=p+U*B+K*y+M*d+L*b+w*g;p=G>>>13,G&=8191,G+=l*h+a*u+x*f+m*c+v*(5*S),p+=G>>>13,G&=8191;let j=p+U*S+K*B+M*y+L*d+w*b;p=j>>>13,j&=8191,j+=l*g+a*h+x*u+m*f+v*c,p+=j>>>13,j&=8191,p=(p<<2)+p|0,p=p+A|0,A=p&8191,p=p>>>13,E+=p,s[0]=A,s[1]=E,s[2]=I,s[3]=O,s[4]=D,s[5]=k,s[6]=R,s[7]=Z,s[8]=G,s[9]=j}finalize(){let{h:t,pad:r}=this,n=new Uint16Array(10),o=t[1]>>>13;t[1]&=8191;for(let c=2;c<10;c++)t[c]+=o,o=t[c]>>>13,t[c]&=8191;t[0]+=o*5,o=t[0]>>>13,t[0]&=8191,t[1]+=o,o=t[1]>>>13,t[1]&=8191,t[2]+=o,n[0]=t[0]+5,o=n[0]>>>13,n[0]&=8191;for(let c=1;c<10;c++)n[c]=t[c]+o,o=n[c]>>>13,n[c]&=8191;n[9]-=8192;let s=(o^1)-1;for(let c=0;c<10;c++)n[c]&=s;s=~s;for(let c=0;c<10;c++)t[c]=t[c]&s|n[c];t[0]=(t[0]|t[1]<<13)&65535,t[1]=(t[1]>>>3|t[2]<<10)&65535,t[2]=(t[2]>>>6|t[3]<<7)&65535,t[3]=(t[3]>>>9|t[4]<<4)&65535,t[4]=(t[4]>>>12|t[5]<<1|t[6]<<14)&65535,t[5]=(t[6]>>>2|t[7]<<11)&65535,t[6]=(t[7]>>>5|t[8]<<8)&65535,t[7]=(t[8]>>>8|t[9]<<5)&65535;let i=t[0]+r[0];t[0]=i&65535;for(let c=1;c<8;c++)i=(t[c]+r[c]|0)+(i>>>16)|0,t[c]=i&65535;Et(n)}update(t){In(this),Q(t),t=ee(t);let{buffer:r,blockLen:n}=this,o=t.length;for(let s=0;s<o;){let i=Math.min(n-this.pos,o-s);if(i===n){for(;n<=o-s;s+=n)this.process(t,s);continue}r.set(t.subarray(s,s+i),this.pos),this.pos+=i,s+=i,this.pos===n&&(this.process(r,0,!1),this.pos=0)}return this}destroy(){Et(this.h,this.r,this.buffer,this.pad)}digestInto(t){In(this),Yr(t,this),this.finished=!0;let{buffer:r,h:n}=this,{pos:o}=this;if(o){for(r[o++]=1;o<16;o++)r[o]=0;this.process(r,0,!0)}this.finalize();let s=0;for(let i=0;i<8;i++)t[s++]=n[i]>>>0,t[s++]=n[i]>>>8;return t}digest(){let{buffer:t,outputLen:r}=this;this.digestInto(t);let n=t.slice(0,r);return this.destroy(),n}};function Ms(e){let t=(n,o)=>e(o).update(n).digest(),r=e(new Uint8Array(32));return t.outputLen=r.outputLen,t.blockLen=r.blockLen,t.create=n=>e(n),t}var to=Ms(e=>new On(e));function ro(e,t,r,n,o,s=20){let i=e[0],c=e[1],f=e[2],u=e[3],h=t[0],g=t[1],b=t[2],d=t[3],y=t[4],B=t[5],S=t[6],_=t[7],V=o,q=r[0],N=r[1],C=r[2],P=i,Y=c,z=f,U=u,K=h,M=g,L=b,w=d,l=y,a=B,x=S,m=_,v=V,p=q,A=N,E=C;for(let O=0;O<s;O+=2)P=P+K|0,v=H(v^P,16),l=l+v|0,K=H(K^l,12),P=P+K|0,v=H(v^P,8),l=l+v|0,K=H(K^l,7),Y=Y+M|0,p=H(p^Y,16),a=a+p|0,M=H(M^a,12),Y=Y+M|0,p=H(p^Y,8),a=a+p|0,M=H(M^a,7),z=z+L|0,A=H(A^z,16),x=x+A|0,L=H(L^x,12),z=z+L|0,A=H(A^z,8),x=x+A|0,L=H(L^x,7),U=U+w|0,E=H(E^U,16),m=m+E|0,w=H(w^m,12),U=U+w|0,E=H(E^U,8),m=m+E|0,w=H(w^m,7),P=P+M|0,E=H(E^P,16),x=x+E|0,M=H(M^x,12),P=P+M|0,E=H(E^P,8),x=x+E|0,M=H(M^x,7),Y=Y+L|0,v=H(v^Y,16),m=m+v|0,L=H(L^m,12),Y=Y+L|0,v=H(v^Y,8),m=m+v|0,L=H(L^m,7),z=z+w|0,p=H(p^z,16),l=l+p|0,w=H(w^l,12),z=z+w|0,p=H(p^z,8),l=l+p|0,w=H(w^l,7),U=U+K|0,A=H(A^U,16),a=a+A|0,K=H(K^a,12),U=U+K|0,A=H(A^U,8),a=a+A|0,K=H(K^a,7);let I=0;n[I++]=i+P|0,n[I++]=c+Y|0,n[I++]=f+z|0,n[I++]=u+U|0,n[I++]=h+K|0,n[I++]=g+M|0,n[I++]=b+L|0,n[I++]=d+w|0,n[I++]=y+l|0,n[I++]=B+a|0,n[I++]=S+x|0,n[I++]=_+m|0,n[I++]=V+v|0,n[I++]=q+p|0,n[I++]=N+A|0,n[I++]=C+E|0}function Zs(e,t,r,n){let o=e[0],s=e[1],i=e[2],c=e[3],f=t[0],u=t[1],h=t[2],g=t[3],b=t[4],d=t[5],y=t[6],B=t[7],S=r[0],_=r[1],V=r[2],q=r[3];for(let C=0;C<20;C+=2)o=o+f|0,S=H(S^o,16),b=b+S|0,f=H(f^b,12),o=o+f|0,S=H(S^o,8),b=b+S|0,f=H(f^b,7),s=s+u|0,_=H(_^s,16),d=d+_|0,u=H(u^d,12),s=s+u|0,_=H(_^s,8),d=d+_|0,u=H(u^d,7),i=i+h|0,V=H(V^i,16),y=y+V|0,h=H(h^y,12),i=i+h|0,V=H(V^i,8),y=y+V|0,h=H(h^y,7),c=c+g|0,q=H(q^c,16),B=B+q|0,g=H(g^B,12),c=c+g|0,q=H(q^c,8),B=B+q|0,g=H(g^B,7),o=o+u|0,q=H(q^o,16),y=y+q|0,u=H(u^y,12),o=o+u|0,q=H(q^o,8),y=y+q|0,u=H(u^y,7),s=s+h|0,S=H(S^s,16),B=B+S|0,h=H(h^B,12),s=s+h|0,S=H(S^s,8),B=B+S|0,h=H(h^B,7),i=i+g|0,_=H(_^i,16),b=b+_|0,g=H(g^b,12),i=i+g|0,_=H(_^i,8),b=b+_|0,g=H(g^b,7),c=c+f|0,V=H(V^c,16),d=d+V|0,f=H(f^d,12),c=c+f|0,V=H(V^c,8),d=d+V|0,f=H(f^d,7);let N=0;n[N++]=o,n[N++]=s,n[N++]=i,n[N++]=c,n[N++]=S,n[N++]=_,n[N++]=V,n[N++]=q}var Hn=Rn(ro,{counterRight:!1,counterLength:4,allowShortKeys:!1}),js=Rn(ro,{counterRight:!1,counterLength:8,extendNonceFn:Zs,allowShortKeys:!1});var Ks=new Uint8Array(16),eo=(e,t)=>{e.update(t);let r=t.length%16;r&&e.update(Ks.subarray(r))},$s=new Uint8Array(32);function no(e,t,r,n,o){o!==void 0&&Q(o,void 0,"AAD");let s=e(t,r,$s),i=Xr(n.length,o?o.length:0,!0),c=to.create(s);o&&eo(c,o),eo(c,n),c.update(i);let f=c.digest();return Et(s,i),f}var oo=e=>(t,r,n)=>({encrypt(s,i){let c=s.length;i=_n(c+16,i,!1),i.set(s);let f=i.subarray(0,-16);e(t,r,f,f,1);let u=no(e,t,r,f,n);return i.set(u,c),Et(u),i},decrypt(s,i){i=_n(s.length-16,i,!1);let c=s.subarray(0,-16),f=s.subarray(-16),u=no(e,t,r,c,n);if(!Pe(f,u))throw new Error("invalid tag");return i.set(s.subarray(0,-16)),e(t,r,i,i,1),Et(u),i}}),jc=Ln({blockSize:64,nonceLength:12,tagLength:16},oo(Hn)),Kc=Ln({blockSize:64,nonceLength:24,tagLength:16},oo(js));function we(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function ut(e,t=""){if(!Number.isSafeInteger(e)||e<0){let r=t&&`"${t}" `;throw new Error(`${r}expected integer >= 0, got ${e}`)}}function W(e,t,r=""){let n=we(e),o=e?.length,s=t!==void 0;if(!n||s&&o!==t){let i=r&&`"${r}" `,c=s?` of length ${t}`:"",f=n?`length=${o}`:`type=${typeof e}`;throw new Error(i+"expected Uint8Array"+c+", got "+f)}return e}function Kt(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash must wrapped by utils.createHasher");ut(e.outputLen),ut(e.blockLen)}function ne(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function io(e,t){W(e,void 0,"digestInto() output");let r=t.outputLen;if(e.length<r)throw new Error('"digestInto() output" expected to be of length >='+r)}function vt(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function ze(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function ht(e,t){return e<<32-t|e>>>t}var co=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",Ps=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function $t(e){if(W(e),co)return e.toHex();let t="";for(let r=0;r<e.length;r++)t+=Ps[e[r]];return t}var Bt={_0:48,_9:57,A:65,F:70,a:97,f:102};function so(e){if(e>=Bt._0&&e<=Bt._9)return e-Bt._0;if(e>=Bt.A&&e<=Bt.F)return e-(Bt.A-10);if(e>=Bt.a&&e<=Bt.f)return e-(Bt.a-10)}function At(e){if(typeof e!="string")throw new Error("hex string expected, got "+typeof e);if(co)return Uint8Array.fromHex(e);let t=e.length,r=t/2;if(t%2)throw new Error("hex string expected, got unpadded hex of length "+t);let n=new Uint8Array(r);for(let o=0,s=0;o<r;o++,s+=2){let i=so(e.charCodeAt(s)),c=so(e.charCodeAt(s+1));if(i===void 0||c===void 0){let f=e[s]+e[s+1];throw new Error('hex string expected, got non-hex character "'+f+'" at index '+s)}n[o]=i*16+c}return n}function ft(...e){let t=0;for(let n=0;n<e.length;n++){let o=e[n];W(o),t+=o.length}let r=new Uint8Array(t);for(let n=0,o=0;n<e.length;n++){let s=e[n];r.set(s,o),o+=s.length}return r}function fo(e,t={}){let r=(o,s)=>e(s).update(o).digest(),n=e(void 0);return r.outputLen=n.outputLen,r.blockLen=n.blockLen,r.create=o=>e(o),Object.assign(r,t),Object.freeze(r)}function me(e=32){let t=typeof globalThis=="object"?globalThis.crypto:null;if(typeof t?.getRandomValues!="function")throw new Error("crypto.getRandomValues must be defined");return t.getRandomValues(new Uint8Array(e))}var ao=e=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,e])});function uo(e,t,r){return e&t^~e&r}function lo(e,t,r){return e&t^e&r^t&r}var Ye=class{constructor(t,r,n,o){T(this,"blockLen");T(this,"outputLen");T(this,"padOffset");T(this,"isLE");T(this,"buffer");T(this,"view");T(this,"finished",!1);T(this,"length",0);T(this,"pos",0);T(this,"destroyed",!1);this.blockLen=t,this.outputLen=r,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(t),this.view=ze(this.buffer)}update(t){ne(this),W(t);let{view:r,buffer:n,blockLen:o}=this,s=t.length;for(let i=0;i<s;){let c=Math.min(o-this.pos,s-i);if(c===o){let f=ze(t);for(;o<=s-i;i+=o)this.process(f,i);continue}n.set(t.subarray(i,i+c),this.pos),this.pos+=c,i+=c,this.pos===o&&(this.process(r,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){ne(this),io(t,this),this.finished=!0;let{buffer:r,view:n,blockLen:o,isLE:s}=this,{pos:i}=this;r[i++]=128,vt(this.buffer.subarray(i)),this.padOffset>o-i&&(this.process(n,0),i=0);for(let g=i;g<o;g++)r[g]=0;n.setBigUint64(o-8,BigInt(this.length*8),s),this.process(n,0);let c=ze(t),f=this.outputLen;if(f%4)throw new Error("_sha2: outputLen must be aligned to 32bit");let u=f/4,h=this.get();if(u>h.length)throw new Error("_sha2: outputLen bigger than state");for(let g=0;g<u;g++)c.setUint32(4*g,h[g],s)}digest(){let{buffer:t,outputLen:r}=this;this.digestInto(t);let n=t.slice(0,r);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:r,buffer:n,length:o,finished:s,destroyed:i,pos:c}=this;return t.destroyed=i,t.finished=s,t.length=o,t.pos=c,o%r&&t.buffer.set(n),t}clone(){return this._cloneInto()}},St=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var Gs=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]),kt=new Uint32Array(64),kn=class extends Ye{constructor(t){super(64,t,8,!1)}get(){let{A:t,B:r,C:n,D:o,E:s,F:i,G:c,H:f}=this;return[t,r,n,o,s,i,c,f]}set(t,r,n,o,s,i,c,f){this.A=t|0,this.B=r|0,this.C=n|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=c|0,this.H=f|0}process(t,r){for(let g=0;g<16;g++,r+=4)kt[g]=t.getUint32(r,!1);for(let g=16;g<64;g++){let b=kt[g-15],d=kt[g-2],y=ht(b,7)^ht(b,18)^b>>>3,B=ht(d,17)^ht(d,19)^d>>>10;kt[g]=B+kt[g-7]+y+kt[g-16]|0}let{A:n,B:o,C:s,D:i,E:c,F:f,G:u,H:h}=this;for(let g=0;g<64;g++){let b=ht(c,6)^ht(c,11)^ht(c,25),d=h+b+uo(c,f,u)+Gs[g]+kt[g]|0,B=(ht(n,2)^ht(n,13)^ht(n,22))+lo(n,o,s)|0;h=u,u=f,f=c,c=i+d|0,i=s,s=o,o=n,n=d+B|0}n=n+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,c=c+this.E|0,f=f+this.F|0,u=u+this.G|0,h=h+this.H|0,this.set(n,o,s,i,c,f,u,h)}roundClean(){vt(kt)}destroy(){this.set(0,0,0,0,0,0,0,0),vt(this.buffer)}},Tn=class extends kn{constructor(){super(32);T(this,"A",St[0]|0);T(this,"B",St[1]|0);T(this,"C",St[2]|0);T(this,"D",St[3]|0);T(this,"E",St[4]|0);T(this,"F",St[5]|0);T(this,"G",St[6]|0);T(this,"H",St[7]|0)}};var re=fo(()=>new Tn,ao(1));var qn=BigInt(0),Nn=BigInt(1);function Ee(e,t=""){if(typeof e!="boolean"){let r=t&&`"${t}" `;throw new Error(r+"expected boolean, got type="+typeof e)}return e}function ho(e){if(typeof e=="bigint"){if(!We(e))throw new Error("positive bigint expected, got "+e)}else ut(e);return e}function Be(e){let t=ho(e).toString(16);return t.length&1?"0"+t:t}function bo(e){if(typeof e!="string")throw new Error("hex string expected, got "+typeof e);return e===""?qn:BigInt("0x"+e)}function oe(e){return bo($t(e))}function Cn(e){return bo($t(zs(W(e)).reverse()))}function Xe(e,t){ut(t),e=ho(e);let r=At(e.toString(16).padStart(t*2,"0"));if(r.length!==t)throw new Error("number too large");return r}function Vn(e,t){return Xe(e,t).reverse()}function zs(e){return Uint8Array.from(e)}var We=e=>typeof e=="bigint"&&qn<=e;function Ys(e,t,r){return We(e)&&We(t)&&We(r)&&t<=e&&e<r}function go(e,t,r,n){if(!Ys(t,r,n))throw new Error("expected valid "+e+": "+r+" <= n < "+n+", got "+t)}function Dn(e){let t;for(t=0;e>qn;e>>=Nn,t+=1);return t}var ve=e=>(Nn<<BigInt(e))-Nn;function xo(e,t,r){if(ut(e,"hashLen"),ut(t,"qByteLen"),typeof r!="function")throw new Error("hmacFn must be a function");let n=S=>new Uint8Array(S),o=Uint8Array.of(),s=Uint8Array.of(0),i=Uint8Array.of(1),c=1e3,f=n(e),u=n(e),h=0,g=()=>{f.fill(1),u.fill(0),h=0},b=(...S)=>r(u,ft(f,...S)),d=(S=o)=>{u=b(s,S),f=b(),S.length!==0&&(u=b(i,S),f=b())},y=()=>{if(h++>=c)throw new Error("drbg: tried max amount of iterations");let S=0,_=[];for(;S<t;){f=b();let V=f.slice();_.push(V),S+=f.length}return ft(..._)};return(S,_)=>{g(),d(S);let V;for(;!(V=_(y()));)d();return g(),V}}function Ae(e,t={},r={}){if(!e||typeof e!="object")throw new Error("expected valid options object");function n(s,i,c){let f=e[s];if(c&&f===void 0)return;let u=typeof f;if(u!==i||f===null)throw new Error(`param "${s}" is invalid: expected ${i}, got ${u}`)}let o=(s,i)=>Object.entries(s).forEach(([c,f])=>n(c,f,i));o(t,!1),o(r,!0)}function Mn(e){let t=new WeakMap;return(r,...n)=>{let o=t.get(r);if(o!==void 0)return o;let s=e(r,...n);return t.set(r,s),s}}var rt=BigInt(0),et=BigInt(1),Pt=BigInt(2),wo=BigInt(3),mo=BigInt(4),Eo=BigInt(5),Ws=BigInt(7),Bo=BigInt(8),Xs=BigInt(9),vo=BigInt(16);function bt(e,t){let r=e%t;return r>=rt?r:t+r}function at(e,t,r){let n=e;for(;t-- >rt;)n*=n,n%=r;return n}function po(e,t){if(e===rt)throw new Error("invert: expected non-zero number");if(t<=rt)throw new Error("invert: expected positive modulus, got "+t);let r=bt(e,t),n=t,o=rt,s=et,i=et,c=rt;for(;r!==rt;){let u=n/r,h=n%r,g=o-i*u,b=s-c*u;n=r,r=h,o=i,s=c,i=g,c=b}if(n!==et)throw new Error("invert: does not exist");return bt(o,t)}function jn(e,t,r){if(!e.eql(e.sqr(t),r))throw new Error("Cannot find square root")}function Ao(e,t){let r=(e.ORDER+et)/mo,n=e.pow(t,r);return jn(e,n,t),n}function Js(e,t){let r=(e.ORDER-Eo)/Bo,n=e.mul(t,Pt),o=e.pow(n,r),s=e.mul(t,o),i=e.mul(e.mul(s,Pt),o),c=e.mul(s,e.sub(i,e.ONE));return jn(e,c,t),c}function Qs(e){let t=se(e),r=So(e),n=r(t,t.neg(t.ONE)),o=r(t,n),s=r(t,t.neg(n)),i=(e+Ws)/vo;return(c,f)=>{let u=c.pow(f,i),h=c.mul(u,n),g=c.mul(u,o),b=c.mul(u,s),d=c.eql(c.sqr(h),f),y=c.eql(c.sqr(g),f);u=c.cmov(u,h,d),h=c.cmov(b,g,y);let B=c.eql(c.sqr(h),f),S=c.cmov(u,h,B);return jn(c,S,f),S}}function So(e){if(e<wo)throw new Error("sqrt is not defined for small field");let t=e-et,r=0;for(;t%Pt===rt;)t/=Pt,r++;let n=Pt,o=se(e);for(;yo(o,n)===1;)if(n++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(r===1)return Ao;let s=o.pow(n,t),i=(t+et)/Pt;return function(f,u){if(f.is0(u))return u;if(yo(f,u)!==1)throw new Error("Cannot find square root");let h=r,g=f.mul(f.ONE,s),b=f.pow(u,t),d=f.pow(u,i);for(;!f.eql(b,f.ONE);){if(f.is0(b))return f.ZERO;let y=1,B=f.sqr(b);for(;!f.eql(B,f.ONE);)if(y++,B=f.sqr(B),y===h)throw new Error("Cannot find square root");let S=et<<BigInt(h-y-1),_=f.pow(g,S);h=y,g=f.sqr(_),b=f.mul(b,g),d=f.mul(d,_)}return d}}function Fs(e){return e%mo===wo?Ao:e%Bo===Eo?Js:e%vo===Xs?Qs(e):So(e)}var ti=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Kn(e){let t={ORDER:"bigint",BYTES:"number",BITS:"number"},r=ti.reduce((n,o)=>(n[o]="function",n),t);return Ae(e,r),e}function ei(e,t,r){if(r<rt)throw new Error("invalid exponent, negatives unsupported");if(r===rt)return e.ONE;if(r===et)return t;let n=e.ONE,o=t;for(;r>rt;)r&et&&(n=e.mul(n,o)),o=e.sqr(o),r>>=et;return n}function Je(e,t,r=!1){let n=new Array(t.length).fill(r?e.ZERO:void 0),o=t.reduce((i,c,f)=>e.is0(c)?i:(n[f]=i,e.mul(i,c)),e.ONE),s=e.inv(o);return t.reduceRight((i,c,f)=>e.is0(c)?i:(n[f]=e.mul(i,n[f]),e.mul(i,c)),s),n}function yo(e,t){let r=(e.ORDER-et)/Pt,n=e.pow(t,r),o=e.eql(n,e.ONE),s=e.eql(n,e.ZERO),i=e.eql(n,e.neg(e.ONE));if(!o&&!s&&!i)throw new Error("invalid Legendre symbol result");return o?1:s?0:-1}function ni(e,t){t!==void 0&&ut(t);let r=t!==void 0?t:e.toString(2).length,n=Math.ceil(r/8);return{nBitLength:r,nByteLength:n}}var Zn=class{constructor(t,r={}){T(this,"ORDER");T(this,"BITS");T(this,"BYTES");T(this,"isLE");T(this,"ZERO",rt);T(this,"ONE",et);T(this,"_lengths");T(this,"_sqrt");T(this,"_mod");if(t<=rt)throw new Error("invalid field: expected ORDER > 0, got "+t);let n;this.isLE=!1,r!=null&&typeof r=="object"&&(typeof r.BITS=="number"&&(n=r.BITS),typeof r.sqrt=="function"&&(this.sqrt=r.sqrt),typeof r.isLE=="boolean"&&(this.isLE=r.isLE),r.allowedLengths&&(this._lengths=r.allowedLengths?.slice()),typeof r.modFromBytes=="boolean"&&(this._mod=r.modFromBytes));let{nBitLength:o,nByteLength:s}=ni(t,n);if(s>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");this.ORDER=t,this.BITS=o,this.BYTES=s,this._sqrt=void 0,Object.preventExtensions(this)}create(t){return bt(t,this.ORDER)}isValid(t){if(typeof t!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof t);return rt<=t&&t<this.ORDER}is0(t){return t===rt}isValidNot0(t){return!this.is0(t)&&this.isValid(t)}isOdd(t){return(t&et)===et}neg(t){return bt(-t,this.ORDER)}eql(t,r){return t===r}sqr(t){return bt(t*t,this.ORDER)}add(t,r){return bt(t+r,this.ORDER)}sub(t,r){return bt(t-r,this.ORDER)}mul(t,r){return bt(t*r,this.ORDER)}pow(t,r){return ei(this,t,r)}div(t,r){return bt(t*po(r,this.ORDER),this.ORDER)}sqrN(t){return t*t}addN(t,r){return t+r}subN(t,r){return t-r}mulN(t,r){return t*r}inv(t){return po(t,this.ORDER)}sqrt(t){return this._sqrt||(this._sqrt=Fs(this.ORDER)),this._sqrt(this,t)}toBytes(t){return this.isLE?Vn(t,this.BYTES):Xe(t,this.BYTES)}fromBytes(t,r=!1){W(t);let{_lengths:n,BYTES:o,isLE:s,ORDER:i,_mod:c}=this;if(n){if(!n.includes(t.length)||t.length>o)throw new Error("Field.fromBytes: expected "+n+" bytes, got "+t.length);let u=new Uint8Array(o);u.set(t,s?0:u.length-t.length),t=u}if(t.length!==o)throw new Error("Field.fromBytes: expected "+o+" bytes, got "+t.length);let f=s?Cn(t):oe(t);if(c&&(f=bt(f,i)),!r&&!this.isValid(f))throw new Error("invalid field element: outside of range 0..ORDER");return f}invertBatch(t){return Je(this,t)}cmov(t,r,n){return n?r:t}};function se(e,t={}){return new Zn(e,t)}function Io(e){if(typeof e!="bigint")throw new Error("field order must be bigint");let t=e.toString(2).length;return Math.ceil(t/8)}function $n(e){let t=Io(e);return t+Math.ceil(t/2)}function Pn(e,t,r=!1){W(e);let n=e.length,o=Io(t),s=$n(t);if(n<16||n<s||n>1024)throw new Error("expected "+s+"-1024 bytes of input, got "+n);let i=r?Cn(e):oe(e),c=bt(i,t-et)+et;return r?Vn(c,o):Xe(c,o)}var ie=BigInt(0),Gt=BigInt(1);function Se(e,t){let r=t.negate();return e?r:t}function Wn(e,t){let r=Je(e.Fp,t.map(n=>n.Z));return t.map((n,o)=>e.fromAffine(n.toAffine(r[o])))}function Ro(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+e)}function Gn(e,t){Ro(e,t);let r=Math.ceil(t/e)+1,n=2**(e-1),o=2**e,s=ve(e),i=BigInt(e);return{windows:r,windowSize:n,mask:s,maxNumber:o,shiftBy:i}}function Lo(e,t,r){let{windowSize:n,mask:o,maxNumber:s,shiftBy:i}=r,c=Number(e&o),f=e>>i;c>n&&(c-=s,f+=Gt);let u=t*n,h=u+Math.abs(c)-1,g=c===0,b=c<0,d=t%2!==0;return{nextN:f,offset:h,isZero:g,isNeg:b,isNegF:d,offsetF:u}}var zn=new WeakMap,Oo=new WeakMap;function Yn(e){return Oo.get(e)||1}function _o(e){if(e!==ie)throw new Error("invalid wNAF")}var Qe=class{constructor(t,r){T(this,"BASE");T(this,"ZERO");T(this,"Fn");T(this,"bits");this.BASE=t.BASE,this.ZERO=t.ZERO,this.Fn=t.Fn,this.bits=r}_unsafeLadder(t,r,n=this.ZERO){let o=t;for(;r>ie;)r&Gt&&(n=n.add(o)),o=o.double(),r>>=Gt;return n}precomputeWindow(t,r){let{windows:n,windowSize:o}=Gn(r,this.bits),s=[],i=t,c=i;for(let f=0;f<n;f++){c=i,s.push(c);for(let u=1;u<o;u++)c=c.add(i),s.push(c);i=c.double()}return s}wNAF(t,r,n){if(!this.Fn.isValid(n))throw new Error("invalid scalar");let o=this.ZERO,s=this.BASE,i=Gn(t,this.bits);for(let c=0;c<i.windows;c++){let{nextN:f,offset:u,isZero:h,isNeg:g,isNegF:b,offsetF:d}=Lo(n,c,i);n=f,h?s=s.add(Se(b,r[d])):o=o.add(Se(g,r[u]))}return _o(n),{p:o,f:s}}wNAFUnsafe(t,r,n,o=this.ZERO){let s=Gn(t,this.bits);for(let i=0;i<s.windows&&n!==ie;i++){let{nextN:c,offset:f,isZero:u,isNeg:h}=Lo(n,i,s);if(n=c,!u){let g=r[f];o=o.add(h?g.negate():g)}}return _o(n),o}getPrecomputes(t,r,n){let o=zn.get(r);return o||(o=this.precomputeWindow(r,t),t!==1&&(typeof n=="function"&&(o=n(o)),zn.set(r,o))),o}cached(t,r,n){let o=Yn(t);return this.wNAF(o,this.getPrecomputes(o,t,n),r)}unsafe(t,r,n,o){let s=Yn(t);return s===1?this._unsafeLadder(t,r,o):this.wNAFUnsafe(s,this.getPrecomputes(s,t,n),r,o)}createCache(t,r){Ro(r,this.bits),Oo.set(t,r),zn.delete(t)}hasCache(t){return Yn(t)!==1}};function Ho(e,t,r,n){let o=t,s=e.ZERO,i=e.ZERO;for(;r>ie||n>ie;)r&Gt&&(s=s.add(o)),n&Gt&&(i=i.add(o)),o=o.double(),r>>=Gt,n>>=Gt;return{p1:s,p2:i}}function Uo(e,t,r){if(t){if(t.ORDER!==e)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return Kn(t),t}else return se(e,{isLE:r})}function ko(e,t,r={},n){if(n===void 0&&(n=e==="edwards"),!t||typeof t!="object")throw new Error(`expected valid ${e} CURVE object`);for(let f of["p","n","h"]){let u=t[f];if(!(typeof u=="bigint"&&u>ie))throw new Error(`CURVE.${f} must be positive bigint`)}let o=Uo(t.p,r.Fp,n),s=Uo(t.n,r.Fn,n),c=["Gx","Gy","a",e==="weierstrass"?"b":"d"];for(let f of c)if(!o.isValid(t[f]))throw new Error(`CURVE.${f} must be valid field element of CURVE.Fp`);return t=Object.freeze(Object.assign({},t)),{CURVE:t,Fp:o,Fn:s}}function To(e,t){return function(n){let o=e(n);return{secretKey:o,publicKey:t(o)}}}var Fe=class{constructor(t,r){T(this,"oHash");T(this,"iHash");T(this,"blockLen");T(this,"outputLen");T(this,"finished",!1);T(this,"destroyed",!1);if(Kt(t),W(r,void 0,"key"),this.iHash=t.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 n=this.blockLen,o=new Uint8Array(n);o.set(r.length>n?t.create().update(r).digest():r);for(let s=0;s<o.length;s++)o[s]^=54;this.iHash.update(o),this.oHash=t.create();for(let s=0;s<o.length;s++)o[s]^=106;this.oHash.update(o),vt(o)}update(t){return ne(this),this.iHash.update(t),this}digestInto(t){ne(this),W(t,this.outputLen,"output"),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){let t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));let{oHash:r,iHash:n,finished:o,destroyed:s,blockLen:i,outputLen:c}=this;return t=t,t.finished=o,t.destroyed=s,t.blockLen=i,t.outputLen=c,t.oHash=r._cloneInto(t.oHash),t.iHash=n._cloneInto(t.iHash),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},Tt=(e,t,r)=>new Fe(e,t).update(r).digest();Tt.create=(e,t)=>new Fe(e,t);var No=(e,t)=>(e+(e>=0?t:-t)/qo)/t;function ri(e,t,r){let[[n,o],[s,i]]=t,c=No(i*e,r),f=No(-o*e,r),u=e-c*n-f*s,h=-c*o-f*i,g=u<It,b=h<It;g&&(u=-u),b&&(h=-h);let d=ve(Math.ceil(Dn(r)/2))+ce;if(u<It||u>=d||h<It||h>=d)throw new Error("splitScalar (endomorphism): failed, k="+e);return{k1neg:g,k1:u,k2neg:b,k2:h}}function Jn(e){if(!["compact","recovered","der"].includes(e))throw new Error('Signature format must be "compact", "recovered", or "der"');return e}function Xn(e,t){let r={};for(let n of Object.keys(t))r[n]=e[n]===void 0?t[n]:e[n];return Ee(r.lowS,"lowS"),Ee(r.prehash,"prehash"),r.format!==void 0&&Jn(r.format),r}var Qn=class extends Error{constructor(t=""){super(t)}},Nt={Err:Qn,_tlv:{encode:(e,t)=>{let{Err:r}=Nt;if(e<0||e>256)throw new r("tlv.encode: wrong tag");if(t.length&1)throw new r("tlv.encode: unpadded data");let n=t.length/2,o=Be(n);if(o.length/2&128)throw new r("tlv.encode: long form length too big");let s=n>127?Be(o.length/2|128):"";return Be(e)+s+o+t},decode(e,t){let{Err:r}=Nt,n=0;if(e<0||e>256)throw new r("tlv.encode: wrong tag");if(t.length<2||t[n++]!==e)throw new r("tlv.decode: wrong tlv");let o=t[n++],s=!!(o&128),i=0;if(!s)i=o;else{let f=o&127;if(!f)throw new r("tlv.decode(long): indefinite length not supported");if(f>4)throw new r("tlv.decode(long): byte length is too big");let u=t.subarray(n,n+f);if(u.length!==f)throw new r("tlv.decode: length bytes not complete");if(u[0]===0)throw new r("tlv.decode(long): zero leftmost byte");for(let h of u)i=i<<8|h;if(n+=f,i<128)throw new r("tlv.decode(long): not minimal encoding")}let c=t.subarray(n,n+i);if(c.length!==i)throw new r("tlv.decode: wrong value length");return{v:c,l:t.subarray(n+i)}}},_int:{encode(e){let{Err:t}=Nt;if(e<It)throw new t("integer: negative integers are not allowed");let r=Be(e);if(Number.parseInt(r[0],16)&8&&(r="00"+r),r.length&1)throw new t("unexpected DER parsing assertion: unpadded hex");return r},decode(e){let{Err:t}=Nt;if(e[0]&128)throw new t("invalid signature integer: negative");if(e[0]===0&&!(e[1]&128))throw new t("invalid signature integer: unnecessary leading zero");return oe(e)}},toSig(e){let{Err:t,_int:r,_tlv:n}=Nt,o=W(e,void 0,"signature"),{v:s,l:i}=n.decode(48,o);if(i.length)throw new t("invalid signature: left bytes after parsing");let{v:c,l:f}=n.decode(2,s),{v:u,l:h}=n.decode(2,f);if(h.length)throw new t("invalid signature: left bytes after parsing");return{r:r.decode(c),s:r.decode(u)}},hexFromSig(e){let{_tlv:t,_int:r}=Nt,n=t.encode(2,r.encode(e.r)),o=t.encode(2,r.encode(e.s)),s=n+o;return t.encode(48,s)}},It=BigInt(0),ce=BigInt(1),qo=BigInt(2),tn=BigInt(3),oi=BigInt(4);function Co(e,t={}){let r=ko("weierstrass",e,t),{Fp:n,Fn:o}=r,s=r.CURVE,{h:i,n:c}=s;Ae(t,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object"});let{endo:f}=t;if(f&&(!n.is0(s.a)||typeof f.beta!="bigint"||!Array.isArray(f.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');let u=Do(n,o);function h(){if(!n.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function g(w,l,a){let{x,y:m}=l.toAffine(),v=n.toBytes(x);if(Ee(a,"isCompressed"),a){h();let p=!n.isOdd(m);return ft(Vo(p),v)}else return ft(Uint8Array.of(4),v,n.toBytes(m))}function b(w){W(w,void 0,"Point");let{publicKey:l,publicKeyUncompressed:a}=u,x=w.length,m=w[0],v=w.subarray(1);if(x===l&&(m===2||m===3)){let p=n.fromBytes(v);if(!n.isValid(p))throw new Error("bad point: is not on curve, wrong x");let A=B(p),E;try{E=n.sqrt(A)}catch(D){let k=D instanceof Error?": "+D.message:"";throw new Error("bad point: is not on curve, sqrt error"+k)}h();let I=n.isOdd(E);return(m&1)===1!==I&&(E=n.neg(E)),{x:p,y:E}}else if(x===a&&m===4){let p=n.BYTES,A=n.fromBytes(v.subarray(0,p)),E=n.fromBytes(v.subarray(p,p*2));if(!S(A,E))throw new Error("bad point: is not on curve");return{x:A,y:E}}else throw new Error(`bad point: got length ${x}, expected compressed=${l} or uncompressed=${a}`)}let d=t.toBytes||g,y=t.fromBytes||b;function B(w){let l=n.sqr(w),a=n.mul(l,w);return n.add(n.add(a,n.mul(w,s.a)),s.b)}function S(w,l){let a=n.sqr(l),x=B(w);return n.eql(a,x)}if(!S(s.Gx,s.Gy))throw new Error("bad curve params: generator point");let _=n.mul(n.pow(s.a,tn),oi),V=n.mul(n.sqr(s.b),BigInt(27));if(n.is0(n.add(_,V)))throw new Error("bad curve params: a or b");function q(w,l,a=!1){if(!n.isValid(l)||a&&n.is0(l))throw new Error(`bad point coordinate ${w}`);return l}function N(w){if(!(w instanceof U))throw new Error("Weierstrass Point expected")}function C(w){if(!f||!f.basises)throw new Error("no endo");return ri(w,f.basises,o.ORDER)}let P=Mn((w,l)=>{let{X:a,Y:x,Z:m}=w;if(n.eql(m,n.ONE))return{x:a,y:x};let v=w.is0();l==null&&(l=v?n.ONE:n.inv(m));let p=n.mul(a,l),A=n.mul(x,l),E=n.mul(m,l);if(v)return{x:n.ZERO,y:n.ZERO};if(!n.eql(E,n.ONE))throw new Error("invZ was invalid");return{x:p,y:A}}),Y=Mn(w=>{if(w.is0()){if(t.allowInfinityPoint&&!n.is0(w.Y))return;throw new Error("bad point: ZERO")}let{x:l,y:a}=w.toAffine();if(!n.isValid(l)||!n.isValid(a))throw new Error("bad point: x or y not field elements");if(!S(l,a))throw new Error("bad point: equation left != right");if(!w.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function z(w,l,a,x,m){return a=new U(n.mul(a.X,w),a.Y,a.Z),l=Se(x,l),a=Se(m,a),l.add(a)}let L=class L{constructor(l,a,x){T(this,"X");T(this,"Y");T(this,"Z");this.X=q("x",l),this.Y=q("y",a,!0),this.Z=q("z",x),Object.freeze(this)}static CURVE(){return s}static fromAffine(l){let{x:a,y:x}=l||{};if(!l||!n.isValid(a)||!n.isValid(x))throw new Error("invalid affine point");if(l instanceof L)throw new Error("projective point not allowed");return n.is0(a)&&n.is0(x)?L.ZERO:new L(a,x,n.ONE)}static fromBytes(l){let a=L.fromAffine(y(W(l,void 0,"point")));return a.assertValidity(),a}static fromHex(l){return L.fromBytes(At(l))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(l=8,a=!0){return M.createCache(this,l),a||this.multiply(tn),this}assertValidity(){Y(this)}hasEvenY(){let{y:l}=this.toAffine();if(!n.isOdd)throw new Error("Field doesn't support isOdd");return!n.isOdd(l)}equals(l){N(l);let{X:a,Y:x,Z:m}=this,{X:v,Y:p,Z:A}=l,E=n.eql(n.mul(a,A),n.mul(v,m)),I=n.eql(n.mul(x,A),n.mul(p,m));return E&&I}negate(){return new L(this.X,n.neg(this.Y),this.Z)}double(){let{a:l,b:a}=s,x=n.mul(a,tn),{X:m,Y:v,Z:p}=this,A=n.ZERO,E=n.ZERO,I=n.ZERO,O=n.mul(m,m),D=n.mul(v,v),k=n.mul(p,p),R=n.mul(m,v);return R=n.add(R,R),I=n.mul(m,p),I=n.add(I,I),A=n.mul(l,I),E=n.mul(x,k),E=n.add(A,E),A=n.sub(D,E),E=n.add(D,E),E=n.mul(A,E),A=n.mul(R,A),I=n.mul(x,I),k=n.mul(l,k),R=n.sub(O,k),R=n.mul(l,R),R=n.add(R,I),I=n.add(O,O),O=n.add(I,O),O=n.add(O,k),O=n.mul(O,R),E=n.add(E,O),k=n.mul(v,p),k=n.add(k,k),O=n.mul(k,R),A=n.sub(A,O),I=n.mul(k,D),I=n.add(I,I),I=n.add(I,I),new L(A,E,I)}add(l){N(l);let{X:a,Y:x,Z:m}=this,{X:v,Y:p,Z:A}=l,E=n.ZERO,I=n.ZERO,O=n.ZERO,D=s.a,k=n.mul(s.b,tn),R=n.mul(a,v),Z=n.mul(x,p),G=n.mul(m,A),j=n.add(a,x),$=n.add(v,p);j=n.mul(j,$),$=n.add(R,Z),j=n.sub(j,$),$=n.add(a,m);let J=n.add(v,A);return $=n.mul($,J),J=n.add(R,G),$=n.sub($,J),J=n.add(x,m),E=n.add(p,A),J=n.mul(J,E),E=n.add(Z,G),J=n.sub(J,E),O=n.mul(D,$),E=n.mul(k,G),O=n.add(E,O),E=n.sub(Z,O),O=n.add(Z,O),I=n.mul(E,O),Z=n.add(R,R),Z=n.add(Z,R),G=n.mul(D,G),$=n.mul(k,$),Z=n.add(Z,G),G=n.sub(R,G),G=n.mul(D,G),$=n.add($,G),R=n.mul(Z,$),I=n.add(I,R),R=n.mul(J,$),E=n.mul(j,E),E=n.sub(E,R),R=n.mul(j,Z),O=n.mul(J,O),O=n.add(O,R),new L(E,I,O)}subtract(l){return this.add(l.negate())}is0(){return this.equals(L.ZERO)}multiply(l){let{endo:a}=t;if(!o.isValidNot0(l))throw new Error("invalid scalar: out of range");let x,m,v=p=>M.cached(this,p,A=>Wn(L,A));if(a){let{k1neg:p,k1:A,k2neg:E,k2:I}=C(l),{p:O,f:D}=v(A),{p:k,f:R}=v(I);m=D.add(R),x=z(a.beta,O,k,p,E)}else{let{p,f:A}=v(l);x=p,m=A}return Wn(L,[x,m])[0]}multiplyUnsafe(l){let{endo:a}=t,x=this;if(!o.isValid(l))throw new Error("invalid scalar: out of range");if(l===It||x.is0())return L.ZERO;if(l===ce)return x;if(M.hasCache(this))return this.multiply(l);if(a){let{k1neg:m,k1:v,k2neg:p,k2:A}=C(l),{p1:E,p2:I}=Ho(L,x,v,A);return z(a.beta,E,I,m,p)}else return M.unsafe(x,l)}toAffine(l){return P(this,l)}isTorsionFree(){let{isTorsionFree:l}=t;return i===ce?!0:l?l(L,this):M.unsafe(this,c).is0()}clearCofactor(){let{clearCofactor:l}=t;return i===ce?this:l?l(L,this):this.multiplyUnsafe(i)}isSmallOrder(){return this.multiplyUnsafe(i).is0()}toBytes(l=!0){return Ee(l,"isCompressed"),this.assertValidity(),d(L,this,l)}toHex(l=!0){return $t(this.toBytes(l))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}};T(L,"BASE",new L(s.Gx,s.Gy,n.ONE)),T(L,"ZERO",new L(n.ZERO,n.ONE,n.ZERO)),T(L,"Fp",n),T(L,"Fn",o);let U=L,K=o.BITS,M=new Qe(U,t.endo?Math.ceil(K/2):K);return U.BASE.precompute(8),U}function Vo(e){return Uint8Array.of(e?2:3)}function Do(e,t){return{secretKey:t.BYTES,publicKey:1+e.BYTES,publicKeyUncompressed:1+2*e.BYTES,publicKeyHasPrefix:!0,signature:2*t.BYTES}}function si(e,t={}){let{Fn:r}=e,n=t.randomBytes||me,o=Object.assign(Do(e.Fp,r),{seed:$n(r.ORDER)});function s(d){try{let y=r.fromBytes(d);return r.isValidNot0(y)}catch{return!1}}function i(d,y){let{publicKey:B,publicKeyUncompressed:S}=o;try{let _=d.length;return y===!0&&_!==B||y===!1&&_!==S?!1:!!e.fromBytes(d)}catch{return!1}}function c(d=n(o.seed)){return Pn(W(d,o.seed,"seed"),r.ORDER)}function f(d,y=!0){return e.BASE.multiply(r.fromBytes(d)).toBytes(y)}function u(d){let{secretKey:y,publicKey:B,publicKeyUncompressed:S}=o;if(!we(d)||"_lengths"in r&&r._lengths||y===B)return;let _=W(d,void 0,"key").length;return _===B||_===S}function h(d,y,B=!0){if(u(d)===!0)throw new Error("first arg must be private key");if(u(y)===!1)throw new Error("second arg must be public key");let S=r.fromBytes(d);return e.fromBytes(y).multiply(S).toBytes(B)}let g={isValidSecretKey:s,isValidPublicKey:i,randomSecretKey:c},b=To(c,f);return Object.freeze({getPublicKey:f,getSharedSecret:h,keygen:b,Point:e,utils:g,lengths:o})}function Mo(e,t,r={}){Kt(t),Ae(r,{},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"}),r=Object.assign({},r);let n=r.randomBytes||me,o=r.hmac||((l,a)=>Tt(t,l,a)),{Fp:s,Fn:i}=e,{ORDER:c,BITS:f}=i,{keygen:u,getPublicKey:h,getSharedSecret:g,utils:b,lengths:d}=si(e,r),y={prehash:!0,lowS:typeof r.lowS=="boolean"?r.lowS:!0,format:"compact",extraEntropy:!1},B=c*qo<s.ORDER;function S(l){let a=c>>ce;return l>a}function _(l,a){if(!i.isValidNot0(a))throw new Error(`invalid signature ${l}: out of range 1..Point.Fn.ORDER`);return a}function V(){if(B)throw new Error('"recovered" sig type is not supported for cofactor >2 curves')}function q(l,a){Jn(a);let x=d.signature,m=a==="compact"?x:a==="recovered"?x+1:void 0;return W(l,m)}class N{constructor(a,x,m){T(this,"r");T(this,"s");T(this,"recovery");if(this.r=_("r",a),this.s=_("s",x),m!=null){if(V(),![0,1,2,3].includes(m))throw new Error("invalid recovery id");this.recovery=m}Object.freeze(this)}static fromBytes(a,x=y.format){q(a,x);let m;if(x==="der"){let{r:E,s:I}=Nt.toSig(W(a));return new N(E,I)}x==="recovered"&&(m=a[0],x="compact",a=a.subarray(1));let v=d.signature/2,p=a.subarray(0,v),A=a.subarray(v,v*2);return new N(i.fromBytes(p),i.fromBytes(A),m)}static fromHex(a,x){return this.fromBytes(At(a),x)}assertRecovery(){let{recovery:a}=this;if(a==null)throw new Error("invalid recovery id: must be present");return a}addRecoveryBit(a){return new N(this.r,this.s,a)}recoverPublicKey(a){let{r:x,s:m}=this,v=this.assertRecovery(),p=v===2||v===3?x+c:x;if(!s.isValid(p))throw new Error("invalid recovery id: sig.r+curve.n != R.x");let A=s.toBytes(p),E=e.fromBytes(ft(Vo((v&1)===0),A)),I=i.inv(p),O=P(W(a,void 0,"msgHash")),D=i.create(-O*I),k=i.create(m*I),R=e.BASE.multiplyUnsafe(D).add(E.multiplyUnsafe(k));if(R.is0())throw new Error("invalid recovery: point at infinify");return R.assertValidity(),R}hasHighS(){return S(this.s)}toBytes(a=y.format){if(Jn(a),a==="der")return At(Nt.hexFromSig(this));let{r:x,s:m}=this,v=i.toBytes(x),p=i.toBytes(m);return a==="recovered"?(V(),ft(Uint8Array.of(this.assertRecovery()),v,p)):ft(v,p)}toHex(a){return $t(this.toBytes(a))}}let C=r.bits2int||function(a){if(a.length>8192)throw new Error("input is too large");let x=oe(a),m=a.length*8-f;return m>0?x>>BigInt(m):x},P=r.bits2int_modN||function(a){return i.create(C(a))},Y=ve(f);function z(l){return go("num < 2^"+f,l,It,Y),i.toBytes(l)}function U(l,a){return W(l,void 0,"message"),a?W(t(l),void 0,"prehashed message"):l}function K(l,a,x){let{lowS:m,prehash:v,extraEntropy:p}=Xn(x,y);l=U(l,v);let A=P(l),E=i.fromBytes(a);if(!i.isValidNot0(E))throw new Error("invalid private key");let I=[z(E),z(A)];if(p!=null&&p!==!1){let R=p===!0?n(d.secretKey):p;I.push(W(R,void 0,"extraEntropy"))}let O=ft(...I),D=A;function k(R){let Z=C(R);if(!i.isValidNot0(Z))return;let G=i.inv(Z),j=e.BASE.multiply(Z).toAffine(),$=i.create(j.x);if($===It)return;let J=i.create(G*i.create(D+$*E));if(J===It)return;let fe=(j.x===$?0:2)|Number(j.y&ce),ae=J;return m&&S(J)&&(ae=i.neg(J),fe^=1),new N($,ae,B?void 0:fe)}return{seed:O,k2sig:k}}function M(l,a,x={}){let{seed:m,k2sig:v}=K(l,a,x);return xo(t.outputLen,i.BYTES,o)(m,v).toBytes(x.format)}function L(l,a,x,m={}){let{lowS:v,prehash:p,format:A}=Xn(m,y);if(x=W(x,void 0,"publicKey"),a=U(a,p),!we(l)){let E=l instanceof N?", use sig.toBytes()":"";throw new Error("verify expects Uint8Array signature"+E)}q(l,A);try{let E=N.fromBytes(l,A),I=e.fromBytes(x);if(v&&E.hasHighS())return!1;let{r:O,s:D}=E,k=P(a),R=i.inv(D),Z=i.create(k*R),G=i.create(O*R),j=e.BASE.multiplyUnsafe(Z).add(I.multiplyUnsafe(G));return j.is0()?!1:i.create(j.x)===O}catch{return!1}}function w(l,a,x={}){let{prehash:m}=Xn(x,y);return a=U(a,m),N.fromBytes(l,"recovered").recoverPublicKey(a).toBytes()}return Object.freeze({keygen:u,getPublicKey:h,getSharedSecret:g,utils:b,lengths:d,Point:e,sign:M,verify:L,recoverPublicKey:w,Signature:N,hash:t})}var tr={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},ii={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]};var Zo=BigInt(2);function ci(e){let t=tr.p,r=BigInt(3),n=BigInt(6),o=BigInt(11),s=BigInt(22),i=BigInt(23),c=BigInt(44),f=BigInt(88),u=e*e*e%t,h=u*u*e%t,g=at(h,r,t)*h%t,b=at(g,r,t)*h%t,d=at(b,Zo,t)*u%t,y=at(d,o,t)*d%t,B=at(y,s,t)*y%t,S=at(B,c,t)*B%t,_=at(S,f,t)*S%t,V=at(_,c,t)*B%t,q=at(V,r,t)*h%t,N=at(q,i,t)*y%t,C=at(N,n,t)*u%t,P=at(C,Zo,t);if(!Fn.eql(Fn.sqr(P),e))throw new Error("Cannot find square root");return P}var Fn=se(tr.p,{sqrt:ci}),fi=Co(tr,{Fp:Fn,endo:ii}),jo=Mo(fi,re);function $o(e,t,r){return Kt(e),r===void 0&&(r=new Uint8Array(e.outputLen)),Tt(e,r,t)}var er=Uint8Array.of(0),Ko=Uint8Array.of();function Po(e,t,r,n=32){Kt(e),ut(n,"length");let o=e.outputLen;if(n>255*o)throw new Error("Length must be <= 255*HashLen");let s=Math.ceil(n/o);r===void 0?r=Ko:W(r,void 0,"info");let i=new Uint8Array(s*o),c=Tt.create(e,t),f=c._cloneInto(),u=new Uint8Array(c.outputLen);for(let h=0;h<s;h++)er[0]=h+1,f.update(h===0?Ko:u).update(r).update(er).digestInto(u),i.set(u,o*h),c._cloneInto(f);return c.destroy(),f.destroy(),vt(u,er),i.slice(0,n)}function zo(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function ai(e){if(!zo(e))throw new Error("Uint8Array expected")}function Yo(e,t){return Array.isArray(t)?t.length===0?!0:e?t.every(r=>typeof r=="string"):t.every(r=>Number.isSafeInteger(r)):!1}function Ie(e,t){if(typeof t!="string")throw new Error(`${e}: string expected`);return!0}function or(e){if(!Number.isSafeInteger(e))throw new Error(`invalid integer: ${e}`)}function rr(e){if(!Array.isArray(e))throw new Error("array expected")}function en(e,t){if(!Yo(!0,t))throw new Error(`${e}: array of strings expected`)}function ui(e,t){if(!Yo(!1,t))throw new Error(`${e}: array of numbers expected`)}function li(...e){let t=s=>s,r=(s,i)=>c=>s(i(c)),n=e.map(s=>s.encode).reduceRight(r,t),o=e.map(s=>s.decode).reduce(r,t);return{encode:n,decode:o}}function di(e){let t=typeof e=="string"?e.split(""):e,r=t.length;en("alphabet",t);let n=new Map(t.map((o,s)=>[o,s]));return{encode:o=>(rr(o),o.map(s=>{if(!Number.isSafeInteger(s)||s<0||s>=r)throw new Error(`alphabet.encode: digit index outside alphabet "${s}". Allowed: ${e}`);return t[s]})),decode:o=>(rr(o),o.map(s=>{Ie("alphabet.decode",s);let i=n.get(s);if(i===void 0)throw new Error(`Unknown letter: "${s}". Allowed: ${e}`);return i}))}}function hi(e=""){return Ie("join",e),{encode:t=>(en("join.decode",t),t.join(e)),decode:t=>(Ie("join.decode",t),t.split(e))}}function bi(e,t="="){return or(e),Ie("padding",t),{encode(r){for(en("padding.encode",r);r.length*e%8;)r.push(t);return r},decode(r){en("padding.decode",r);let n=r.length;if(n*e%8)throw new Error("padding: invalid, string should have whole number of bytes");for(;n>0&&r[n-1]===t;n--)if((n-1)*e%8===0)throw new Error("padding: invalid, string has too much padding");return r.slice(0,n)}}}var Wo=(e,t)=>t===0?e:Wo(t,e%t),nn=(e,t)=>e+(t-Wo(e,t)),nr=(()=>{let e=[];for(let t=0;t<40;t++)e.push(2**t);return e})();function Go(e,t,r,n){if(rr(e),t<=0||t>32)throw new Error(`convertRadix2: wrong from=${t}`);if(r<=0||r>32)throw new Error(`convertRadix2: wrong to=${r}`);if(nn(t,r)>32)throw new Error(`convertRadix2: carry overflow from=${t} to=${r} carryBits=${nn(t,r)}`);let o=0,s=0,i=nr[t],c=nr[r]-1,f=[];for(let u of e){if(or(u),u>=i)throw new Error(`convertRadix2: invalid data word=${u} from=${t}`);if(o=o<<t|u,s+t>32)throw new Error(`convertRadix2: carry overflow pos=${s} from=${t}`);for(s+=t;s>=r;s-=r)f.push((o>>s-r&c)>>>0);let h=nr[s];if(h===void 0)throw new Error("invalid carry");o&=h-1}if(o=o<<r-s&c,!n&&s>=t)throw new Error("Excess padding");if(!n&&o>0)throw new Error(`Non-zero padding: ${o}`);return n&&s>0&&f.push(o>>>0),f}function gi(e,t=!1){if(or(e),e<=0||e>32)throw new Error("radix2: bits should be in (0..32]");if(nn(8,e)>32||nn(e,8)>32)throw new Error("radix2: carry overflow");return{encode:r=>{if(!zo(r))throw new Error("radix2.encode input should be Uint8Array");return Go(Array.from(r),8,e,!t)},decode:r=>(ui("radix2.decode",r),Uint8Array.from(Go(r,e,8,t)))}}var xi=typeof Uint8Array.from([]).toBase64=="function"&&typeof Uint8Array.fromBase64=="function",pi=(e,t)=>{Ie("base64",e);let r=t?/^[A-Za-z0-9=_-]+$/:/^[A-Za-z0-9=+/]+$/,n=t?"base64url":"base64";if(e.length>0&&!r.test(e))throw new Error("invalid base64");return Uint8Array.fromBase64(e,{alphabet:n,lastChunkHandling:"strict"})},Xo=xi?{encode(e){return ai(e),e.toBase64()},decode(e){return pi(e,!1)}}:li(gi(6),di("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),bi(6),hi(""));var yi=new TextDecoder("utf-8"),wi=new TextEncoder,mi=1,Ei=4294967295,Bi=65536;function sr(e,t){let r=jo.getSharedSecret(e,At("02"+t)).subarray(1,33);return $o(re,r,wi.encode("nip44-v2"))}function vi(e,t){let r=Po(re,e,t,76);return{chacha_key:r.subarray(0,32),chacha_nonce:r.subarray(32,44),hmac_key:r.subarray(44,76)}}function Ai(e){if(!Number.isSafeInteger(e)||e<1)throw new Error("expected positive integer");if(e<=32)return 32;let t=2**(Math.floor(Math.log2(e-1))+1),r=t<=256?32:t/8;return r*(Math.floor((e-1)/r)+1)}function Si(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength),r=t.getUint16(0),n,o;if(r===0){if(n=t.getUint32(2),n<Bi)throw new Error("invalid padding");o=6}else n=r,o=2;let s=e.subarray(o,o+n);if(n<mi||n>Ei||s.length!==n||e.length!==o+Ai(n))throw new Error("invalid padding");return yi.decode(s)}function Ii(e,t,r){if(r.length!==32)throw new Error("AAD associated data must be 32 bytes");let n=ft(r,t);return Tt(re,e,n)}function Li(e){if(typeof e!="string")throw new Error("payload must be a valid string");let t=e.length;if(t<132)throw new Error("invalid payload length: "+t);if(e[0]==="#")throw new Error("unknown encryption version");let r;try{r=Xo.decode(e)}catch(s){throw new Error("invalid base64: "+s.message)}let n=r.length;if(n<99)throw new Error("invalid data length: "+n);let o=r[0];if(o!==2)throw new Error("unknown encryption version "+o);return{nonce:r.subarray(1,33),ciphertext:r.subarray(33,-32),mac:r.subarray(-32)}}function ir(e,t){let{nonce:r,ciphertext:n,mac:o}=Li(e),{chacha_key:s,chacha_nonce:i,hmac_key:c}=vi(t,r),f=Ii(c,n,r);if(!Pe(f,o))throw new Error("invalid MAC");let u=Hn(s,i,n);return Si(u)}function _i(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function Ui(){let e=new Uint8Array(16);return crypto.getRandomValues(e),Array.from(e,t=>t.toString(16).padStart(2,"0")).join("")}function rn(e,t){let r=e.find(n=>n[0]===t);return r?r[1]:void 0}function Lt(e){let t=new Uint8Array(e.length/2);for(let r=0;r<t.length;r++)t[r]=parseInt(e.substr(r*2,2),16);return t}async function Ri(e){if(!e.id||!e.pubkey||!e.sig||!e.tags||e.kind!==30470||!/^[0-9a-f]{128}$/i.test(e.sig)||!/^[0-9a-f]{64}$/i.test(e.pubkey)||!/^[0-9a-f]{64}$/i.test(e.id)||e.tags.length>100||e.content.length>65536||e.tags.some(i=>i.some(c=>c.length>1024)))return!1;let t=e.tags.map(i=>i[0]);if(!t.includes("tier")||!t.includes("age-range")||!t.includes("entity-type"))return!1;let r=JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content]),n=new TextEncoder,o=Sn(n.encode(r));if(st(o)!==e.id.toLowerCase())return!1;try{let i=Lt(e.sig),c=Lt(e.id),f=Lt(e.pubkey);return ye.verify(i,c,f)}catch{return!1}}function Oi(e,t){if(e===t||e==="18+"&&t==="18+")return!0;let r=["0-3","4-7","8-12","13-17","18+"],n=r.indexOf(e),o=r.indexOf(t);return n===-1||o===-1?!1:t==="18+"?e==="18+":e===t}async function Hi(e,t){if(t==null||!/^https:\/\//i.test(t)||!/^[0-9a-f]{64}$/i.test(e))return null;try{let r=await fetch(`${t}/status/${e}`,{signal:AbortSignal.timeout(5e3)});if(!r.ok)return null;let n=await r.json();if(typeof n!="object"||n===null)return null;let o=n;return{confirmed:o.confirmed===!0,method:["A","B","C","D"].includes(o.method)?o.method:null,profession:typeof o.profession=="string"?o.profession:void 0,jurisdiction:typeof o.jurisdiction=="string"?o.jurisdiction:void 0}}catch{return null}}async function Jo(e,t){if(!["0-3","4-7","8-12","13-17","18+"].includes(e))return{verified:!1,ageRange:null,tier:null,entityType:null,credentialId:null,verifierPubkey:null,verifierConfirmed:null,verifierMethod:null,issuedAt:null,expiresAt:null,error:"invalid-age-range"};let n={requiredAgeRange:e,relayUrl:t?.relayUrl||"wss://relay.damus.io",theme:t?.theme||"auto",timeout:t?.timeout||12e4,verifierCheckUrl:t?.verifierCheckUrl!==void 0?t.verifierCheckUrl:"https://verify.signet.forgesworn.dev",acceptUnconfirmed:t?.acceptUnconfirmed||!1,...t};n.timeout=Math.max(5e3,Math.min(n.timeout??12e4,6e5));let o=Ui(),s={type:"signet-verify-request",requestId:o,requiredAgeRange:n.requiredAgeRange,relayUrl:n.relayUrl,timestamp:Math.floor(Date.now()/1e3)},i=JSON.stringify(s),c=btoa(i);return new Promise(f=>{let u=document.createElement("style");u.textContent="#signet-verify-dialog::backdrop{background:rgba(0,0,0,0.7)}",document.head.appendChild(u);let h=n.theme==="dark"||n.theme==="auto"&&window.matchMedia("(prefers-color-scheme: dark)").matches,g=h?"#1a1a2e":"#ffffff",b=h?"#e0e0e0":"#1a1a2e",d=h?"#888":"#666",y=document.createElement("dialog");y.id="signet-verify-dialog",y.style.cssText=`border:none;border-radius:16px;padding:32px;max-width:380px;width:90%;text-align:center;box-shadow:0 20px 60px rgba(0,0,0,0.3);background:${g};color:${b};font-family:system-ui,-apple-system,sans-serif;`,y.innerHTML=`
1
+ "use strict";var Signet=(()=>{var xt=Object.defineProperty;var Jo=Object.getOwnPropertyDescriptor;var Qo=Object.getOwnPropertyNames;var es=Object.prototype.hasOwnProperty;var ts=(t,e,r)=>e in t?xt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var ns=(t,e)=>{for(var r in e)xt(t,r,{get:e[r],enumerable:!0})},rs=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Qo(e))!es.call(t,o)&&o!==r&&xt(t,o,{get:()=>e[o],enumerable:!(n=Jo(e,o))||n.enumerable});return t};var os=t=>rs(xt({},"__esModule",{value:!0}),t);var E=(t,e,r)=>ts(t,typeof e!="symbol"?e+"":e,r);var zi={};ns(zi,{verifyAge:()=>Wo,waitForAuthResponse:()=>Xo});var ss=t=>t/2**32|0,is=t=>t>>>0;function Qn(t,e,r,n){let o=ss(r),s=is(r);t.setUint32(e,n?s:o,n),t.setUint32(e+4,n?o:s,n)}function yt(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"&&"BYTES_PER_ELEMENT"in t&&t.BYTES_PER_ELEMENT===1}var Wt=t=>t?`"${t}" `:"";function ot(t,e=""){if(typeof t!="number")throw new TypeError(Wt(e)+"expected number, got "+typeof t);if(!Number.isSafeInteger(t)||t<0)throw new RangeError(Wt(e)+"expected integer >= 0, got "+t);return t}function Se(t,e,r=""){if(yt(t)&&(e===void 0||t.length===e))return t;e!==void 0&&ot(e,"length");let n=yt(t),o=e!==void 0?` of length ${e}`:"",s=n?`length=${t.length}`:`type=${typeof t}`,i=Wt(r)+"expected Uint8Array"+o+", got "+s;throw n?new RangeError(i):new TypeError(i)}var cs=(t,e)=>{if(t===null||typeof t!="object"||Array.isArray(t))throw new TypeError((e==="object"?"":`"${e}" `)+"expected object, got type="+typeof t)},er=(t,e)=>{cs(t,e);let r=Object.getPrototypeOf(t);if(r!==Object.prototype&&r!==null)throw new TypeError(`"${e}" expected plain object`);if(Object.hasOwn(t,"__proto__"))throw new TypeError(`"${e}.__proto__" is not allowed`)};function Xt(t,e=!0){if(t.destroyed)throw new Error("hash was destroyed");if(e&&t.finished)throw new Error("digest() was already called")}function nr(t,e){Se(t,void 0,"output");let r=e.outputLen;if(!(t.length>=r))throw new RangeError('"output" expected length >= '+r)}function Jt(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function wt(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function le(t,e){return t<<32-e|t>>>e}var rr=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",fs=Array.from({length:256},(t,e)=>e.toString(16).padStart(2,"0"));function Re(t){if(Se(t),rr)return t.toHex();let e="";for(let r=0;r<t.length;r++)e+=fs[t[r]];return e}function tr(t){return t>=48&&t<=57?t-48:t>=65&&t<=70?t-55:t>=97&&t<=102?t-87:void 0}function Qt(t){if(typeof t!="string")throw new TypeError("hex string expected, got "+typeof t);if(rr)try{return Uint8Array.fromHex(t)}catch(o){throw o instanceof SyntaxError?new RangeError(o.message):o}let e=t.length,r=e/2;if(e%2)throw new RangeError("hex string expected, got unpadded hex of length "+e);let n=new Uint8Array(r);for(let o=0,s=0;o<r;o++,s+=2){let i=tr(t.charCodeAt(s)),c=tr(t.charCodeAt(s+1));if(i===void 0||c===void 0){let f=t[s]+t[s+1];throw new RangeError('hex string expected, got non-hex character "'+f+'" at index '+s)}n[o]=i*16+c}return n}function or(...t){let e=0;for(let n=0;n<t.length;n++){let o=t[n];Se(o),e+=o.length}let r=new Uint8Array(e);for(let n=0,o=0;n<t.length;n++){let s=t[n];r.set(s,o),o+=s.length}return r}function as(t,e,r="opts"){return er(t,"defaults"),e!==void 0&&er(e,r),Object.assign(Object.create(null),t,e)}function sr(t,e={}){if(typeof t!="function")throw new TypeError('"hashCons" expected function, got type='+typeof t);e=as({},e,"info");let r=(o,s)=>t(s).update(o).digest(),n=t(void 0);return r.outputLen=n.outputLen,r.blockLen=n.blockLen,r.canXOF=n.canXOF,r.create=o=>t(o),Object.assign(r,e),Object.freeze(r)}function st(t=32){ot(t,"bytesLength");let e=typeof globalThis=="object"?globalThis.crypto:null;if(typeof e?.getRandomValues!="function")throw new Error("crypto.getRandomValues must be defined");if(t>65536)throw new RangeError(`"bytesLength" expected <= 65536, got ${t}`);return e.getRandomValues(new Uint8Array(t))}var ir=t=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,t])});function cr(t,e,r){return t&e^~t&r}function fr(t,e,r){return t&e^t&r^e&r}var mt=class{constructor(e,r,n,o){E(this,"blockLen");E(this,"outputLen");E(this,"canXOF",!1);E(this,"padOffset");E(this,"isLE");E(this,"buffer");E(this,"view");E(this,"finished",!1);E(this,"length",0);E(this,"pos",0);E(this,"destroyed",!1);this.blockLen=e,this.outputLen=r,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(e),this.view=wt(this.buffer)}update(e){Xt(this),Se(e);let{view:r,buffer:n,blockLen:o}=this,s=e.length,i=!1;for(let c=0;c<s;){let f=Math.min(o-this.pos,s-c);if(f===o){let a=wt(e);for(;o<=s-c;c+=o)this.process(a,c);i=!0;continue}n.set(c===0&&f===s?e:e.subarray(c,c+f),this.pos),this.pos+=f,c+=f,this.pos===o&&(this.process(r,0),this.pos=0,i=!0)}return this.length+=e.length,i&&this.roundClean(),this}digestInto(e){Xt(this),nr(e,this),this.finished=!0;let{buffer:r,view:n,blockLen:o,isLE:s}=this,{pos:i}=this;r[i++]=128,r.fill(0,i),this.padOffset>o-i&&(this.process(n,0),r.fill(0)),Qn(n,o-8,this.length*8,s),this.process(n,0),this.roundClean();let c=e===r?n:wt(e),f=this.outputLen,a=f/4,u=this.get();if(f%4||a>u.length)throw new Error("invalid outputLen");for(let d=0;d<a;d++)c.setUint32(4*d,u[d],s)}digest(){let{buffer:e,outputLen:r}=this;this.digestInto(e);let n=e.slice(0,r);return this.destroy(),n}_cloneIntoMeta(e){let{buffer:r,length:n,finished:o,destroyed:s,pos:i}=this;return e.destroyed=s,e.finished=o,e.length=n,e.pos=i,i&&e.buffer.set(r),e}clone(){return this._cloneInto()}},ar=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var us=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]),Oe=new Uint32Array(64),en=class extends mt{constructor(r,n){super(64,r,8,!1);E(this,"A",0);E(this,"B",0);E(this,"C",0);E(this,"D",0);E(this,"E",0);E(this,"F",0);E(this,"G",0);E(this,"H",0);this.A=n[0]|0,this.B=n[1]|0,this.C=n[2]|0,this.D=n[3]|0,this.E=n[4]|0,this.F=n[5]|0,this.G=n[6]|0,this.H=n[7]|0}get(){let{A:r,B:n,C:o,D:s,E:i,F:c,G:f,H:a}=this;return[r,n,o,s,i,c,f,a]}set(r,n,o,s,i,c,f,a){this.A=r|0,this.B=n|0,this.C=o|0,this.D=s|0,this.E=i|0,this.F=c|0,this.G=f|0,this.H=a|0}_cloneInto(r){return(r||(r=new this.constructor)).set(...this.get()),this._cloneIntoMeta(r)}process(r,n){for(let g=0;g<16;g++,n+=4)Oe[g]=r.getUint32(n,!1);for(let g=16;g<64;g++){let l=Oe[g-15],x=Oe[g-2],A=le(l,7)^le(l,18)^l>>>3,v=le(x,17)^le(x,19)^x>>>10;Oe[g]=v+Oe[g-7]+A+Oe[g-16]|0}let{A:o,B:s,C:i,D:c,E:f,F:a,G:u,H:d}=this;for(let g=0;g<64;g++){let l=le(f,6)^le(f,11)^le(f,25),x=d+l+cr(f,a,u)+us[g]+Oe[g]|0,v=(le(o,2)^le(o,13)^le(o,22))+fr(o,s,i)|0;d=u,u=a,a=f,f=c+x|0,c=i,i=s,s=o,o=x+v|0}o=o+this.A|0,s=s+this.B|0,i=i+this.C|0,c=c+this.D|0,f=f+this.E|0,a=a+this.F|0,u=u+this.G|0,d=d+this.H|0,this.set(o,s,i,c,f,a,u,d)}roundClean(){Jt(Oe)}destroy(){this.destroyed=!0,this.set(0,0,0,0,0,0,0,0),Jt(this.buffer)}},tn=class extends en{constructor(){super(32,ar)}};var $e=sr(()=>new tn,ir(1));function Bt(t,e,r=()=>{}){if(!Array.isArray(t))throw new TypeError(`"${e}" expected array, got type=${typeof t}`);for(let n=0;n<t.length;n++)r(t[n],`${e}[${n}]`);return t}var se=(t,e,r)=>Se(t,e,r),nn=ot;function Te(t,e="object"){if(t===null||typeof t!="object"||Array.isArray(t))throw new TypeError(e==="object"?"expected valid options object":`"${e}" expected object, got type=${typeof t}`);return t}function Ne(t,e){if(typeof t!="function")throw new TypeError(`"${e}" is invalid: expected function, got ${typeof t}`);return t}var dr=Re,Ze=(...t)=>or(...t),lr=t=>Qt(t),vt=yt,hr=t=>st(t),Et=BigInt(0),ur=BigInt(1),ds=t=>t?`"${t}" `:"";function Ie(t,e=""){if(typeof t!="boolean")throw new TypeError(ds(e)+"expected boolean, got type="+typeof t);return t}function ls(t){if(typeof t=="bigint"){if(!Le(t))throw new RangeError("positive bigint expected, got "+t)}else nn(t);return t}function At(t,e=""){if(typeof t!="number"){let r=e&&`"${e}" `;throw new TypeError(r+"expected number, got type="+typeof t)}if(!Number.isSafeInteger(t)){let r=e&&`"${e}" `;throw new RangeError(r+"expected safe integer, got "+t)}}function pr(t){if(typeof t!="string")throw new TypeError("hex string expected, got "+typeof t);return t===""?Et:BigInt("0x"+t)}function Ue(t){return pr(Re(t))}function rn(t){return pr(Re(sn(Se(t)).reverse()))}function St(t,e){if(ot(e),e===0)throw new Error("zero output length is invalid");t=ls(t);let r=e*2,n=t.toString(16);if(n.length>r)throw new RangeError("number is too large");return Qt(n.padStart(r,"0"))}function on(t,e){return St(t,e).reverse()}function sn(t){return Uint8Array.from(se(t))}function br(t){if(typeof t!="string")throw new TypeError("ascii string expected, got "+typeof t);return Uint8Array.from(t,(e,r)=>{let n=e.charCodeAt(0);if(e.length!==1||n>127)throw new RangeError(`string contains non-ASCII character "${t[r]}" with code ${n} at position ${r}`);return n})}function Le(t){return typeof t=="bigint"&&Et<=t}function cn(t,e,r){return Le(t)&&Le(e)&&Le(r)&&e<=t&&t<r}function gr(t,e,r,n){if(!cn(e,r,n))throw new RangeError("expected valid "+t+": "+r+" <= n < "+n+", got "+e)}function Pe(t){if(t<Et)throw new Error("expected non-negative bigint, got "+t);return t===Et?0:t.toString(2).length}var Rt=t=>(At(t,"n"),(ur<<BigInt(t))-ur);function Ot(t,e={},r={},n="object"){Te(t,n),Te(e,"fields"),Te(r,"optFields");function o(i,c,f){let a=n==="object"?`param "${String(i)}"`:`"${n}.${String(i)}"`,u=t[i];if(!Object.hasOwn(t,i)&&(f?u!==void 0:c!=="function"))throw new TypeError(`${a} is invalid: expected own property`);if(f&&u===void 0)return;let d=typeof u;if(d!==c||u===null)throw new TypeError(`${a} is invalid: expected ${c}, got ${d}`)}let s=(i,c)=>Object.entries(i).forEach(([f,a])=>o(f,a,c));s(e,!1),s(r,!0)}var Q=BigInt(0),X=BigInt(1),Ce=BigInt(2),mr=BigInt(3),fn=BigInt(4),Er=BigInt(5),hs=BigInt(7),Br=BigInt(8),ps=BigInt(9),bs=BigInt(15),vr=BigInt(16),gs=BigInt("0x10000000000000000");function he(t,e){if(e<=Q)throw new Error("mod: expected positive modulus, got "+e);let r=t%e;return r>=Q?r:e+r}function xs(t,e,r){if(r<=X)throw new Error("pow: expected modulus > 1, got "+r);if(typeof e!="bigint")throw new TypeError("invalid exponent: expected bigint, got "+typeof e);if(e<Q)throw new Error("invalid exponent, negatives unsupported");if(e===Q)return X;if(e===X)return t;let n=t%r;if(n<Q&&(n+=r),e<gs){let c=X;for(;e>Q;)e&X&&(c=c*n%r),n=n*n%r,e>>=X;return c}let o=[];for(;e>Q;)o.push(Number(e&bs)),e>>=fn;let s=new Array(16);s[0]=X,s[1]=n;for(let c=2;c<16;c++)s[c]=s[c-1]*n%r;let i=s[o[o.length-1]];for(let c=o.length-2;c>=0;c--){i=i*i%r,i=i*i%r,i=i*i%r,i=i*i%r;let f=o[c];f!==0&&(i=i*s[f]%r)}return i}function ce(t,e,r){if(r<=X)throw new Error("pow2: expected modulus > 1, got "+r);if(e<Q)throw new Error("pow2: expected non-negative exponent, got "+e);let n=t;for(;e-- >Q;)n*=n,n%=r;return n}function xr(t,e){if(t===Q)throw new Error("invert: expected non-zero number");if(e<=X)throw new Error("invert: expected modulus > 1, got "+e);let r=he(t,e),n=e,o=Q,s=X;for(;r!==Q;){let c=n/r,f=n-r*c,a=o-s*c;n=r,r=f,o=s,s=a}if(n!==X)throw new Error("invert: does not exist");return he(o,e)}function an(t,e,r){let n=t;if(!n.eql(n.sqr(e),r))throw new Error("Cannot find square root")}function un(t,e){if((t&X)===Q)throw new Error(e+": expected odd modulus, got "+t)}function Ar(t,e){let r=t,n=(r.ORDER+X)/fn,o=r.pow(e,n);return an(r,o,e),o}function ys(t,e){let r=t,n=(r.ORDER-Er)/Br,o=r.mul(e,Ce),s=r.pow(o,n),i=r.mul(e,s),c=r.mul(r.mul(i,Ce),s),f=r.mul(i,r.sub(c,r.ONE));return an(r,f,e),f}function ws(t){let e=Ke(t),r=Sr(t),n=r(e,e.neg(e.ONE)),o=r(e,n),s=r(e,e.neg(n)),i=(t+hs)/vr;return((c,f)=>{let a=c,u=a.pow(f,i),d=a.mul(u,n),g=a.mul(u,o),l=a.mul(u,s),x=a.eql(a.sqr(d),f),A=a.eql(a.sqr(g),f);u=a.cmov(u,d,x),d=a.cmov(l,g,A);let v=a.eql(a.sqr(d),f),L=a.cmov(u,d,v);return an(a,L,f),L})}function Sr(t){if(t<mr)throw new Error("sqrt is not defined for small field");un(t,"tonelliShanks");let e=t-X,r=0;for(;e%Ce===Q;)e/=Ce,r++;let n=Ce,o=Ke(t);for(;yr(o,n)===1;)if(n++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(r===1)return Ar;let s=o.pow(n,e),i=(e+X)/Ce;return function(f,a){let u=f;if(u.is0(a))return a;if(yr(u,a)!==1)throw new Error("Cannot find square root");let d=r,g=u.mul(u.ONE,s),l=u.pow(a,e),x=u.pow(a,i);for(;!u.eql(l,u.ONE);){if(u.is0(l))throw new Error("Cannot find square root: probably non-prime P");let A=1,v=u.sqr(l);for(;!u.eql(v,u.ONE);)if(A++,v=u.sqr(v),A===d)throw new Error("Cannot find square root");let L=X<<BigInt(d-A-1),q=u.pow(g,L);d=A,g=u.sqr(q),l=u.mul(l,g),x=u.mul(x,q)}return x}}function ms(t){return un(t,"Fp.sqrt"),t%fn===mr?Ar:t%Br===Er?ys:t%vr===ps?ws(t):Sr(t)}var Es=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Fe(t){if(Te(t,"field"),typeof t.ORDER!="bigint")throw new TypeError('param "ORDER" is invalid: expected bigint, got '+typeof t.ORDER);At(t.BYTES,"BYTES"),At(t.BITS,"BITS");for(let e of Es)Ne(t[e],"field."+e);if(t.BYTES<1||t.BITS<1)throw new Error("invalid field: expected BYTES/BITS > 0");if(t.ORDER<=X)throw new Error("invalid field: expected ORDER > 1, got "+t.ORDER);return t}function dn(t,e,r=!1){Fe(t),Bt(e,"nums"),Ie(r,"passZero");let n=t,o=new Array(e.length).fill(r?n.ZERO:void 0),s=e.reduce((c,f,a)=>n.is0(f)?c:(o[a]=c,n.mul(c,f)),n.ONE),i=n.inv(s);return e.reduceRight((c,f,a)=>n.is0(f)?c:(o[a]=n.mul(c,o[a]),n.mul(c,f)),i),o}function yr(t,e){Fe(t);let r=t;un(r.ORDER,"FpLegendre");let n=(r.ORDER-X)/Ce,o=r.pow(e,n),s=r.eql(o,r.ONE),i=r.eql(o,r.ZERO),c=r.eql(o,r.neg(r.ONE));if(!s&&!i&&!c)throw new Error("invalid Legendre symbol result");return s?1:i?0:-1}function Bs(t,e){if(e!==void 0&&nn(e),t<=Q)throw new Error("invalid n length: expected positive n, got "+t);if(e!==void 0&&e<1)throw new Error("invalid n length: expected positive bit length, got "+e);let r=Pe(t);if(e!==void 0&&e<r)throw new Error(`invalid n length: expected nBitLength (${e}) >= bitLen(n) (${r})`);let n=e!==void 0?e:r,o=Math.ceil(n/8);return{nBitLength:n,nByteLength:o}}var wr=new WeakMap,Tt=class{constructor(e,r={}){E(this,"ORDER");E(this,"BITS");E(this,"BYTES");E(this,"isLE");E(this,"ZERO",Q);E(this,"ONE",X);E(this,"_lengths");E(this,"_mod");if(e<=X)throw new Error("invalid field: expected ORDER > 1, got "+e);let n;this.isLE=!1,r!=null&&typeof r=="object"&&(typeof r.BITS=="number"&&(n=r.BITS),typeof r.sqrt=="function"&&Object.defineProperty(this,"sqrt",{value:r.sqrt,enumerable:!0}),typeof r.isLE=="boolean"&&(this.isLE=r.isLE),r.allowedLengths&&(this._lengths=Object.freeze(r.allowedLengths.slice())),typeof r.modFromBytes=="boolean"&&(this._mod=r.modFromBytes));let{nBitLength:o,nByteLength:s}=Bs(e,n);if(s>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");this.ORDER=e,this.BITS=o,this.BYTES=s,Object.freeze(this)}create(e){return he(e,this.ORDER)}isValid(e){if(typeof e!="bigint")throw new TypeError("invalid field element: expected bigint, got "+typeof e);return Q<=e&&e<this.ORDER}is0(e){return e===Q}isValidNot0(e){return!this.is0(e)&&this.isValid(e)}isOdd(e){return(e&X)===X}neg(e){return he(-e,this.ORDER)}eql(e,r){return e===r}sqr(e){return he(e*e,this.ORDER)}add(e,r){return he(e+r,this.ORDER)}sub(e,r){return he(e-r,this.ORDER)}mul(e,r){return he(e*r,this.ORDER)}pow(e,r){return xs(e,r,this.ORDER)}div(e,r){return he(e*xr(r,this.ORDER),this.ORDER)}sqrN(e){return e*e}addN(e,r){return e+r}subN(e,r){return e-r}mulN(e,r){return e*r}inv(e){return xr(e,this.ORDER)}sqrt(e){let r=wr.get(this);return r||wr.set(this,r=ms(this.ORDER)),r(this,e)}toBytes(e){return this.isLE?on(e,this.BYTES):St(e,this.BYTES)}fromBytes(e,r=!1){se(e);let{_lengths:n,BYTES:o,isLE:s,ORDER:i,_mod:c}=this;if(n){if(e.length<1||!n.includes(e.length)||e.length>o)throw new Error("Field.fromBytes: expected "+n+" bytes, got "+e.length);let a=new Uint8Array(o);a.set(e,s?0:a.length-e.length),e=a}if(e.length!==o)throw new Error("Field.fromBytes: expected "+o+" bytes, got "+e.length);let f=s?rn(e):Ue(e);if(c&&(f=he(f,i)),!r&&!this.isValid(f))throw new Error("invalid field element: outside of range 0..ORDER");return f}invertBatch(e){return dn(this,e,!0)}cmov(e,r,n){return Ie(n,"condition"),n?r:e}};function Ke(t,e={}){return Object.freeze(Tt.prototype),new Tt(t,e)}function Rr(t){if(typeof t!="bigint")throw new Error("field order must be bigint");if(t<=X)throw new Error("field order must be greater than 1");let e=Pe(t-X);return Math.ceil(e/8)}function vs(t){let e=Rr(t);return e+Math.ceil(e/2)}function Or(t,e,r=!1){se(t);let n=t.length,o=Rr(e),s=Math.max(vs(e),16);if(n<s||n>1024)throw new Error("expected "+s+"-1024 bytes of input, got "+n);let i=r?rn(t):Ue(t),c=he(i,e-X)+X;return r?on(c,o):St(c,o)}var pn=BigInt(0),it=BigInt(1),As=BigInt(4),ln=16,Tr=128,Ss=5,Lr=2**31;function It(t){let e=t;if(typeof e!="function")throw new TypeError('"Point" expected constructor, got type='+typeof t);Ne(e.fromAffine,"Point.fromAffine"),Ne(e.fromBytes,"Point.fromBytes"),Ne(e.fromHex,"Point.fromHex"),Te(e.BASE,"Point.BASE"),Te(e.ZERO,"Point.ZERO"),Fe(e.Fp),Fe(e.Fn)}function Ur(t,e){It(t),kr(e,t);let r=dn(t.Fp,e.map(n=>n.Z));return e.map((n,o)=>t.fromAffine(n.toAffine(r[o])))}function Rs(t,e,r=1){if(!Number.isSafeInteger(t)||t<r||t>e)throw new Error("invalid window size, expected ["+r+".."+e+"], got W="+t)}function Os(t,e){let r=t*(4*e+128);if(r>Lr)throw new Error("invalid window size: table would need ~"+Math.ceil(r/2**20)+" MiB, max "+Lr/2**20+" MiB")}function _r(t,e){if(t!==void 0){Ne(t,"randomBytes");try{let r=t(e);if(!vt(r)||r.length!==e)return}catch{return}return t}}function kr(t,e){Bt(t,"points"),t.forEach((r,n)=>{if(!(r instanceof e))throw new Error("invalid point at index "+n)})}function Ts(t,e,r){if(!Array.isArray(t))throw new Error("array of scalars expected");t.forEach((n,o)=>{if(!(r===void 0?e.isValid(n):Le(n)&&n<r))throw new Error("invalid scalar at index "+o)})}var Hr=new WeakMap;function hn(t){return Hr.get(t)||1}function Ls(t,e){let r=t.double(),n=[t];for(let o=1;o<e;o++)n.push(n[o-1].add(r));return n}function Is(t,e){let r=2**e,n=r/2,o=BigInt(r-1),s=[];for(;t>pn;){let i=0;t&it&&(i=Number(t&o),i>=n&&(i-=r),t-=BigInt(i)),s.push(i),t>>=it}return s}function Us(t,e,r){let n=2**e,o=n/2,s=BigInt(n-1),i=BigInt(e),c=[];for(let f=0;f<r;f++){let a=Number(t&s);t>>=i,a>o&&(a-=n,t+=it),c.push(a)}if(t!==pn)throw new Error("invalid wnaf");return c}function _s(t,e,r){let n=0;for(let s of r)n=Math.max(n,s.length);let o=t;for(let s=n-1;s>=0;s--){s!==n-1&&(o=o.double());for(let i=0;i<r.length;i++){let c=r[i][s];if(c){let f=e[i][Math.abs(c)-1>>1];o=o.add(c<0?f.negate():f)}}}return o}var Lt=class{constructor(e,r){E(this,"Point");E(this,"BASE");E(this,"ZERO");E(this,"randomBytes");E(this,"wnafPrecomputes",new WeakMap);E(this,"baseCanBeBlinded");E(this,"bits");It(e),this.randomBytes=_r(r,ln),this.Point=e,this.BASE=e.BASE,this.ZERO=e.ZERO,this.bits=e.Fn.BITS}buildWnafTable(e,r,n){let o=Math.ceil(n/r)+1,s=2**(r-1),i=[],c=e;for(let f=0;f<o;f++){let a=c;for(let u=0;u<s;u++)i.push(a),a=a.add(c);c=i[i.length-1].double()}return{W:r,bits:n,windows:o,comp:i}}wnafCachedCT(e,r){let{W:n,windows:o,comp:s}=e,i=2**(n-1),c=Us(r,n,o),f=this.ZERO,a=this.BASE;for(let u=0;u<o;u++){let d=c[u],g=u*i,l=Math.abs(d)-1,x=s[g];for(let v=1;v<i;v++)x=v===l?s[g+v]:x;let A=x.negate();d===0?a=a.add(s[g]):f=f.add(d<0?A:x)}return{p:f,f:a}}getWnafPrecomputes(e,r,n,o){let s=this.wnafPrecomputes.get(r),i=s?.find(c=>c.W===e&&c.bits===n);return i||(i=this.buildWnafTable(r,e,n),typeof o=="function"&&(i={...i,comp:o(i.comp)}),s||(s=[],this.wnafPrecomputes.set(r,s)),s.push(i)),i}assertPoint(e){if(!(e instanceof this.Point))throw new TypeError('"point" expected Point instance, got type='+typeof e)}validateMulInput(e,r){if(this.assertPoint(e),!cn(r,it,this.Point.Fn.ORDER))throw new Error("invalid scalar")}runCT(e,r,n,o){let s=hn(e);return s===1?this.fixedWindowCT(e,r,n):this.wnafCachedCT(this.getWnafPrecomputes(s,e,n,o),r)}mulCT(e,r,n){return this.validateMulInput(e,r),this.runCT(e,r,this.bits,n)}mulCTBlinded(e,r,n){if(this.validateMulInput(e,r),this.randomBytes===void 0)throw new Error("randomBytes is required for scalar blinding");let o=this.Point.Fn.BITS+Tr,s=this.randomBytes(ln);if(!vt(s)||s.length!==ln)throw new Error("randomBytes returned invalid byte array");s[0]=s[0]&63|128;let i=r+Ue(s)*this.Point.Fn.ORDER;return this.runCT(e,i,o,n)}fixedWindowCT(e,r,n){let o=Ss,s=1<<o,i=Rt(o),c=new Array(s);c[0]=this.ZERO;for(let u=1;u<s;u++)c[u]=c[u-1].add(e);let f=Math.ceil(n/o),a=this.ZERO;for(let u=f-1;u>=0;u--){if(u!==f-1)for(let l=0;l<o;l++)a=a.double();let d=Number(r>>BigInt(u*o)&i),g=c[0];for(let l=1;l<s;l++)g=l===d?c[l]:g;a=a.add(g)}return{p:a,f:a}}shouldBlind(e,r){return this.randomBytes===void 0?!1:r===it?!0:e!==this.BASE?!1:(this.baseCanBeBlinded===void 0&&(this.baseCanBeBlinded=this.mulUnsafe(this.BASE,this.Point.Fn.ORDER).is0()),this.baseCanBeBlinded)}mulSecret(e,r,n,o){return this.shouldBlind(e,n)?this.mulCTBlinded(e,r,o):this.mulCT(e,r,o)}mulUnsafe(e,r,n){if(this.assertPoint(e),!Le(r))throw new Error("invalid scalar");let o=hn(e);if(o===1||r>=this.Point.Fn.ORDER)return Ut(this.Point,[e],[r],!0);let s=this.getWnafPrecomputes(o,e,this.bits,n);return this.wnafCachedCT(s,r).p}setWindowSize(e,r){this.assertPoint(e),Rs(r,this.bits);let n=Math.ceil((this.bits+Tr)/r)+1;Os(n*2**(r-1),this.Point.Fp.BYTES),Hr.set(e,r),this.wnafPrecomputes.delete(e)}hasWindowSize(e){return hn(e)!==1}};function Ut(t,e,r,n=!1){if(It(t),kr(e,t),Ie(n,"allowOversized"),Ts(r,t.Fn,n?t.Fn.ORDER**As:void 0),e.length!==r.length)throw new Error("arrays of points and scalars must have equal length");let o=e.map(i=>Ls(i,4)),s=r.map(i=>Is(i,4));return _s(t.ZERO,o,s)}function Ir(t,e,r){if(e){if(e.ORDER!==t)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return Fe(e),e}else return Ke(t,{isLE:r})}function Nr(t,e,r={},n){if(t!=="weierstrass"&&t!=="edwards")throw new Error('expected curve type "weierstrass" or "edwards"');if(n===void 0&&(n=t==="edwards"),!e||typeof e!="object")throw new Error(`expected valid ${t} CURVE object`);Ot(r);for(let f of["p","n","h"]){let a=e[f];if(!(Le(a)&&a!==pn))throw new Error(`CURVE.${f} must be positive bigint`)}let o=Ir(e.p,r.Fp,n),s=Ir(e.n,r.Fn,n),c=["Gx","Gy","a",t==="weierstrass"?"b":"d"];for(let f of c)if(!o.isValid(e[f]))throw new Error(`CURVE.${f} must be valid field element of CURVE.Fp`);return e=Object.freeze(Object.assign({},e)),{CURVE:e,Fp:o,Fn:s}}function bn(t,e){return function(n){let o=t(n);return{secretKey:o,publicKey:e(o)}}}var Cr=(t,e)=>(t+(t>=0?e:-e)/Hs)/e;function ks(t,e,r){gr("scalar",t,ze,r);let[[n,o],[s,i]]=e,c=Cr(i*t,r),f=Cr(-o*t,r),a=t-c*n-f*s,u=-c*o-f*i,d=a<ze,g=u<ze;d&&(a=-a),g&&(u=-u);let l=Rt(Math.ceil(Pe(r)/2))+ct;if(a<ze||a>=l||u<ze||u>=l)throw new Error("splitScalar (endomorphism): failed for k");return{k1neg:d,k1:a,k2neg:g,k2:u}}var ze=BigInt(0),ct=BigInt(1),Hs=BigInt(2),gn=BigInt(3),Ns=BigInt(4);function qr(t,e={}){let r=Nr("weierstrass",t,e),n=r.Fp,o=r.Fn,s=r.CURVE,{h:i,n:c}=s;Ot(e,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object",randomBytes:"function"});let{endo:f,allowInfinityPoint:a,clearCofactor:u,isTorsionFree:d,fromBytes:g,toBytes:l}=e,x=e.randomBytes===void 0?hr:e.randomBytes;if(f&&(!n.is0(s.a)||typeof f.beta!="bigint"||!Array.isArray(f.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');let A=f?{beta:f.beta,basises:f.basises.map(m=>[...m])}:void 0,v=qs(n,o);function L(){if(!n.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function q(m,p,y){if(p.is0()){if(!a)throw new Error("bad point: ZERO");return Uint8Array.of(0)}let{x:U,y:k}=p.toAffine(),T=n.toBytes(U);if(Ie(y,"isCompressed"),y){L();let H=!n.isOdd(k);return Ze(Cs(H),T)}else return Ze(Uint8Array.of(4),T,n.toBytes(k))}function V(m){se(m,void 0,"Point");let{publicKey:p,publicKeyUncompressed:y}=v,U=m.length,k=m[0],T=m.subarray(1);if(a&&U===1&&k===0)return{x:n.ZERO,y:n.ZERO};if(U===p&&(k===2||k===3)){let H=n.fromBytes(T);if(!n.isValid(H))throw new Error("bad point: is not on curve, wrong x");let M=G(H),D;try{D=n.sqrt(M)}catch(J){let ne=J instanceof Error?": "+J.message:"";throw new Error("bad point: is not on curve, sqrt error"+ne)}L();let $=n.isOdd(D);return(k&1)===1!==$&&(D=n.neg(D)),{x:H,y:D}}else if(U===y&&k===4){let H=n.BYTES,M=n.fromBytes(T.subarray(0,H)),D=n.fromBytes(T.subarray(H,H*2));if(!j(M,D))throw new Error("bad point: is not on curve");return{x:M,y:D}}else throw new Error(`bad point: got length ${U}, expected compressed=${p} or uncompressed=${y}`)}let C=l===void 0?q:l,F=g===void 0?V:g,P=n.mul(s.b,gn),Y=n.is0(s.a)?m=>n.ZERO:m=>n.mul(s.a,m);function G(m){let p=n.sqr(m),y=n.mul(p,m);return n.add(n.add(y,n.mul(m,s.a)),s.b)}function j(m,p){let y=n.sqr(p),U=G(m);return n.eql(y,U)}if(!j(s.Gx,s.Gy))throw new Error("bad curve params: generator point");let z=n.mul(n.pow(s.a,gn),Ns),K=n.mul(n.sqr(s.b),BigInt(27));if(n.is0(n.add(z,K)))throw new Error("bad curve params: a or b");function _(m,p,y=!1){if(!n.isValid(p)||y&&n.is0(p))throw new Error(`bad point coordinate ${m}`);return typeof p=="object"&&p!==null?n.create(p):p}function O(m){if(!(m instanceof S))throw new Error("Weierstrass Point expected")}function b(m){if(!A||!A.basises)throw new Error("no endo");return ks(m,A.basises,o.ORDER)}function h(m,p,y,U){if(!o.isValid(U))throw new RangeError("invalid scalar: out of range");if(A){let{k1neg:k,k1:T,k2neg:H,k2:M}=b(U),D=new S(n.mul(y.X,A.beta),y.Y,y.Z);m.push(k?y.negate():y,H?D.negate():D),p.push(T,M)}else m.push(y),p.push(U)}let B=new WeakSet,R=class R{constructor(p,y,U){E(this,"X");E(this,"Y");E(this,"Z");this.X=_("x",p),this.Y=_("y",y,!0),this.Z=_("z",U),Object.freeze(this)}static CURVE(){return s}static fromAffine(p){let{x:y,y:U}=p||{};if(!p||!n.isValid(y)||!n.isValid(U))throw new Error("invalid affine point");if(p instanceof R)throw new Error("projective point not allowed");return n.is0(y)&&n.is0(U)?R.ZERO:new R(y,U,n.ONE)}static fromBytes(p){let y=R.fromAffine(F(se(p,void 0,"point")));return y.assertValidity(),y}static fromHex(p){return R.fromBytes(lr(p))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(p=6,y=!0){return w.setWindowSize(this,p),y||this.multiply(gn),this}assertValidity(){let p=this;if(p.is0()){if(a&&n.is0(p.X)&&n.eql(p.Y,n.ONE)&&n.is0(p.Z))return;throw new Error("bad point: ZERO")}if(B.has(p))return;let{x:y,y:U}=p.toAffine();if(!n.isValid(y)||!n.isValid(U))throw new Error("bad point: x or y not field elements");if(!j(y,U))throw new Error("bad point: equation left != right");if(!p.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");B.add(p)}hasEvenY(){let{y:p}=this.toAffine();if(!n.isOdd)throw new Error("Field doesn't support isOdd");return!n.isOdd(p)}equals(p){O(p);let{X:y,Y:U,Z:k}=this,{X:T,Y:H,Z:M}=p,D=n.eql(n.mul(y,M),n.mul(T,k)),$=n.eql(n.mul(U,M),n.mul(H,k));return D&&$}negate(){return new R(this.X,n.neg(this.Y),this.Z)}double(){let{X:p,Y:y,Z:U}=this,k=n.ZERO,T=n.ZERO,H=n.ZERO,M=n.mul(p,p),D=n.mul(y,y),$=n.mul(U,U),Z=n.mul(p,y);return Z=n.add(Z,Z),H=n.mul(p,U),H=n.add(H,H),k=Y(H),T=n.mul(P,$),T=n.add(k,T),k=n.sub(D,T),T=n.add(D,T),T=n.mul(k,T),k=n.mul(Z,k),H=n.mul(P,H),$=Y($),Z=n.sub(M,$),Z=Y(Z),Z=n.add(Z,H),H=n.add(M,M),M=n.add(H,M),M=n.add(M,$),M=n.mul(M,Z),T=n.add(T,M),$=n.mul(y,U),$=n.add($,$),M=n.mul($,Z),k=n.sub(k,M),H=n.mul($,D),H=n.add(H,H),H=n.add(H,H),new R(k,T,H)}add(p){O(p);let{X:y,Y:U,Z:k}=this,{X:T,Y:H,Z:M}=p,D=n.ZERO,$=n.ZERO,Z=n.ZERO,J=n.mul(y,T),ne=n.mul(U,H),de=n.mul(k,M),Ve=n.add(y,U),oe=n.add(T,H);Ve=n.mul(Ve,oe),oe=n.add(J,ne),Ve=n.sub(Ve,oe),oe=n.add(y,k);let ge=n.add(T,M);return oe=n.mul(oe,ge),ge=n.add(J,de),oe=n.sub(oe,ge),ge=n.add(U,k),D=n.add(H,M),ge=n.mul(ge,D),D=n.add(ne,de),ge=n.sub(ge,D),Z=Y(oe),D=n.mul(P,de),Z=n.add(D,Z),D=n.sub(ne,Z),Z=n.add(ne,Z),$=n.mul(D,Z),ne=n.add(J,J),ne=n.add(ne,J),de=Y(de),oe=n.mul(P,oe),ne=n.add(ne,de),de=n.sub(J,de),de=Y(de),oe=n.add(oe,de),J=n.mul(ne,oe),$=n.add($,J),J=n.mul(ge,oe),D=n.mul(Ve,D),D=n.sub(D,J),J=n.mul(Ve,ne),Z=n.mul(ge,Z),Z=n.add(Z,J),new R(D,$,Z)}subtract(p){return O(p),this.add(p.negate())}is0(){return this.equals(R.ZERO)}multiply(p){if(!o.isValidNot0(p))throw new RangeError("invalid scalar: out of range");let{p:y,f:U}=w.mulSecret(this,p,i,I);return I([y,U])[0]}multiplyUnsafe(p){let y=this,U=p;if(!o.isValid(U))throw new RangeError("invalid scalar: out of range");if(U===ze||y.is0())return R.ZERO;if(U===ct)return y;if(w.hasWindowSize(this))return w.mulUnsafe(y,U,I);let k=[],T=[];return h(k,T,y,U),Ut(R,k,T)}mulAddUnsafe(p,y,U){O(y);let k=[],T=[];return h(k,T,this,p),h(k,T,y,U),Ut(R,k,T)}toAffine(p){let y=this,U=p;if(U!=null&&!n.isValid(U))throw new RangeError('"invertedZ" expected valid field element');let{X:k,Y:T,Z:H}=y;if(n.eql(H,n.ONE))return{x:k,y:T};let M=y.is0();U==null&&(U=M?n.ONE:n.inv(H));let D=n.mul(k,U),$=n.mul(T,U),Z=n.mul(H,U);if(M)return{x:n.ZERO,y:n.ZERO};if(!n.eql(Z,n.ONE))throw new Error("invZ was invalid");return{x:D,y:$}}isTorsionFree(){return i===ct?!0:d?d(R,this):w.mulUnsafe(this,c).is0()}clearCofactor(){return i===ct?this:u?u(R,this):this.multiplyUnsafe(i)}isSmallOrder(){return i===ct?this.is0():this.clearCofactor().is0()}toBytes(p=!0){return Ie(p,"isCompressed"),this.assertValidity(),C(R,this,p)}toHex(p=!0){return dr(this.toBytes(p))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}};E(R,"BASE",new R(s.Gx,s.Gy,n.ONE)),E(R,"ZERO",new R(n.ZERO,n.ONE,n.ZERO)),E(R,"Fp",n),E(R,"Fn",o);let S=R,I=m=>Ur(S,m),w=new Lt(S,x);return w.bits>=6&&S.BASE.precompute(6),Object.freeze(S.prototype),Object.freeze(S),S}function Cs(t){return Uint8Array.of(t?2:3)}function qs(t,e){return{secretKey:e.BYTES,publicKey:1+t.BYTES,publicKeyUncompressed:1+2*t.BYTES,publicKeyHasPrefix:!0,signature:2*e.BYTES}}var kt={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},Ds={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]},Ms=BigInt(0),Dr=BigInt(2);function js(t){let e=kt.p,r=BigInt(3),n=BigInt(6),o=BigInt(11),s=BigInt(22),i=BigInt(23),c=BigInt(44),f=BigInt(88),a=t*t*t%e,u=a*a*t%e,d=ce(u,r,e)*u%e,g=ce(d,r,e)*u%e,l=ce(g,Dr,e)*a%e,x=ce(l,o,e)*l%e,A=ce(x,s,e)*x%e,v=ce(A,c,e)*A%e,L=ce(v,f,e)*v%e,q=ce(L,c,e)*A%e,V=ce(q,r,e)*u%e,C=ce(V,i,e)*x%e,F=ce(C,n,e)*a%e,P=ce(F,Dr,e);if(!Ge.eql(Ge.sqr(P),t))throw new Error("Cannot find square root");return P}var Ge=Ke(kt.p,{sqrt:js}),We=qr(kt,{Fp:Ge,endo:Ds});var Mr=Object.create(null);function _t(t,...e){let r=Mr[t];if(r===void 0){let n=$e(br(t));r=Ze(n,n),Mr[t]=r}return $e(Ze(r,...e))}var Vr=t=>t.toBytes(!0).slice(1),$r=({x:t})=>Ge.toBytes(t),Ht=t=>!Ge.isOdd(t);function Zr(t){let{Fn:e,BASE:r}=We,n=e.fromBytes(se(t,32,"secretKey")),s=r.multiply(n).toAffine();return{scalar:Ht(s.y)?n:e.neg(n),bytes:$r(s)}}function Pr(t){let e=Ge;if(!e.isValidNot0(t))throw new Error("invalid x: Fail if x \u2265 p");let r=e.sqr(t),n=e.add(e.mulN(r,t),BigInt(7)),o=e.sqrt(n);Ht(o)||(o=e.neg(o));let s=We.fromAffine({x:t,y:o});return s.assertValidity(),s}var Ye=Ue;function Fr(...t){return We.Fn.create(Ye(_t("BIP0340/challenge",...t)))}function jr(t){return Zr(t).bytes}function Vs(t,e,r=st(32)){let{Fn:n,BASE:o}=We,s=sn(se(t,void 0,"message")),{bytes:i,scalar:c}=Zr(e),f=se(r,32,"auxRand"),a=n.toBytes(c^Ye(_t("BIP0340/aux",f))),u=_t("BIP0340/nonce",a,i,s),d=n.create(Ye(u));if(d===Ms)throw new Error("sign failed: k is zero");let l=o.multiply(d).toAffine(),x=Ht(l.y)?d:n.neg(d),A=$r(l),v=Fr(A,i,s),L=new Uint8Array(64);if(L.set(A,0),L.set(n.toBytes(n.create(x+v*c)),32),!Kr(L,s,i))throw new Error("sign: Invalid signature produced");return L}function Kr(t,e,r){let{Fp:n,Fn:o,BASE:s}=We,i=se(t,64,"signature"),c=se(e,void 0,"message"),f=se(r,32,"publicKey");try{let a=Pr(Ye(f)),u=i.subarray(0,32),d=Ye(u);if(!n.isValidNot0(d))return!1;let g=Ye(i.subarray(32,64));if(!o.isValidNot0(g))return!1;let l=Fr(u,Vr(a),c),x=s.mulAddUnsafe(g,a,o.neg(l)),{x:A,y:v}=x.toAffine();return!(x.is0()||!Ht(v)||!n.eql(A,d))}catch{return!1}}var ft=(()=>{let r=n=>(n=n===void 0?st(48):n,Or(se(n,48,"seed"),kt.n));return Object.freeze({keygen:bn(r,jr),getPublicKey:jr,sign:Vs,verify:Kr,Point:We,utils:Object.freeze({randomSecretKey:r,taggedHash:_t,lift_x:Pr,pointToBytes:Vr}),lengths:Object.freeze({secretKey:32,publicKey:32,publicKeyHasPrefix:!1,signature:64,seed:48})})})();function $s(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function Nt(t){if(typeof t!="boolean")throw new Error(`boolean expected, not ${t}`)}function Ct(t){if(!Number.isSafeInteger(t)||t<0)throw new Error("positive integer expected, got "+t)}function ee(t,e,r=""){let n=$s(t),o=t?.length,s=e!==void 0;if(!n||s&&o!==e){let i=r&&`"${r}" `,c=s?` of length ${e}`:"",f=n?`length=${o}`:`type=${typeof t}`;throw new Error(i+"expected Uint8Array"+c+", got "+f)}return t}function xn(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 zr(t,e){ee(t,void 0,"output");let r=e.outputLen;if(t.length<r)throw new Error("digestInto() expects output buffer of length at least "+r)}function xe(t){return new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4))}function ye(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function Zs(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}var Ps=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function Yr(t,e){if(e==null||typeof e!="object")throw new Error("options must be defined");return Object.assign(t,e)}function qt(t,e){if(t.length!==e.length)return!1;let r=0;for(let n=0;n<t.length;n++)r|=t[n]^e[n];return r===0}var yn=(t,e)=>{function r(n,...o){if(ee(n,void 0,"key"),!Ps)throw new Error("Non little-endian hardware is not yet supported");if(t.nonceLength!==void 0){let u=o[0];ee(u,t.varSizeNonce?void 0:t.nonceLength,"nonce")}let s=t.tagLength;s&&o[1]!==void 0&&ee(o[1],void 0,"AAD");let i=e(n,...o),c=(u,d)=>{if(d!==void 0){if(u!==2)throw new Error("cipher output not supported");ee(d,void 0,"output")}},f=!1;return{encrypt(u,d){if(f)throw new Error("cannot encrypt() twice with same key + nonce");return f=!0,ee(u),c(i.encrypt.length,d),i.encrypt(u,d)},decrypt(u,d){if(ee(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(r,t),r};function wn(t,e,r=!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(r&&!Fs(e))throw new Error("invalid output, must be aligned");return e}function Gr(t,e,r){Nt(r);let n=new Uint8Array(16),o=Zs(n);return o.setBigUint64(0,BigInt(e),r),o.setBigUint64(8,BigInt(t),r),n}function Fs(t){return t.byteOffset%4===0}function Xe(t){return Uint8Array.from(t)}var Xr=t=>Uint8Array.from(t.split(""),e=>e.charCodeAt(0)),Ks=Xr("expand 16-byte k"),zs=Xr("expand 32-byte k"),Ys=xe(Ks),Gs=xe(zs);function N(t,e){return t<<e|t>>>32-e}function mn(t){return t.byteOffset%4===0}var Dt=64,Ws=16,Jr=2**32-1,Wr=Uint32Array.of();function Xs(t,e,r,n,o,s,i,c){let f=o.length,a=new Uint8Array(Dt),u=xe(a),d=mn(o)&&mn(s),g=d?xe(o):Wr,l=d?xe(s):Wr;for(let x=0;x<f;i++){if(t(e,r,n,u,i,c),i>=Jr)throw new Error("arx: counter overflow");let A=Math.min(Dt,f-x);if(d&&A===Dt){let v=x/4;if(x%4!==0)throw new Error("arx: invalid block position");for(let L=0,q;L<Ws;L++)q=v+L,l[q]=g[q]^u[L];x+=Dt;continue}for(let v=0,L;v<A;v++)L=x+v,s[L]=o[L]^a[v];x+=A}}function En(t,e){let{allowShortKeys:r,extendNonceFn:n,counterLength:o,counterRight:s,rounds:i}=Yr({allowShortKeys:!1,counterLength:8,counterRight:!1,rounds:20},e);if(typeof t!="function")throw new Error("core must be a function");return Ct(o),Ct(i),Nt(s),Nt(r),(c,f,a,u,d=0)=>{ee(c,void 0,"key"),ee(f,void 0,"nonce"),ee(a,void 0,"data");let g=a.length;if(u===void 0&&(u=new Uint8Array(g)),ee(u,void 0,"output"),Ct(d),d<0||d>=Jr)throw new Error("arx: counter overflow");if(u.length<g)throw new Error(`arx: output (${u.length}) is shorter than data (${g})`);let l=[],x=c.length,A,v;if(x===32)l.push(A=Xe(c)),v=Gs;else if(x===16&&r)A=new Uint8Array(32),A.set(c),A.set(c,16),v=Ys,l.push(A);else throw ee(c,32,"arx key"),new Error("invalid key size");mn(f)||l.push(f=Xe(f));let L=xe(A);if(n){if(f.length!==24)throw new Error("arx: extended nonce must be 24 bytes");n(v,L,xe(f.subarray(0,16)),L),f=f.subarray(16)}let q=16-o;if(q!==f.length)throw new Error(`arx: nonce must be ${q} or 16 bytes`);if(q!==12){let C=new Uint8Array(12);C.set(f,s?0:12-f.length),f=C,l.push(f)}let V=xe(f);return Xs(t,v,L,V,a,u,d,i),ye(...l),u}}function te(t,e){return t[e++]&255|(t[e++]&255)<<8}var Bn=class{constructor(e){E(this,"blockLen",16);E(this,"outputLen",16);E(this,"buffer",new Uint8Array(16));E(this,"r",new Uint16Array(10));E(this,"h",new Uint16Array(10));E(this,"pad",new Uint16Array(8));E(this,"pos",0);E(this,"finished",!1);e=Xe(ee(e,32,"key"));let r=te(e,0),n=te(e,2),o=te(e,4),s=te(e,6),i=te(e,8),c=te(e,10),f=te(e,12),a=te(e,14);this.r[0]=r&8191,this.r[1]=(r>>>13|n<<3)&8191,this.r[2]=(n>>>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|f<<5)&8065,this.r[8]=(f>>>8|a<<8)&8191,this.r[9]=a>>>5&127;for(let u=0;u<8;u++)this.pad[u]=te(e,16+2*u)}process(e,r,n=!1){let o=n?0:2048,{h:s,r:i}=this,c=i[0],f=i[1],a=i[2],u=i[3],d=i[4],g=i[5],l=i[6],x=i[7],A=i[8],v=i[9],L=te(e,r+0),q=te(e,r+2),V=te(e,r+4),C=te(e,r+6),F=te(e,r+8),P=te(e,r+10),Y=te(e,r+12),G=te(e,r+14),j=s[0]+(L&8191),z=s[1]+((L>>>13|q<<3)&8191),K=s[2]+((q>>>10|V<<6)&8191),_=s[3]+((V>>>7|C<<9)&8191),O=s[4]+((C>>>4|F<<12)&8191),b=s[5]+(F>>>1&8191),h=s[6]+((F>>>14|P<<2)&8191),B=s[7]+((P>>>11|Y<<5)&8191),S=s[8]+((Y>>>8|G<<8)&8191),I=s[9]+(G>>>5|o),w=0,R=w+j*c+z*(5*v)+K*(5*A)+_*(5*x)+O*(5*l);w=R>>>13,R&=8191,R+=b*(5*g)+h*(5*d)+B*(5*u)+S*(5*a)+I*(5*f),w+=R>>>13,R&=8191;let m=w+j*f+z*c+K*(5*v)+_*(5*A)+O*(5*x);w=m>>>13,m&=8191,m+=b*(5*l)+h*(5*g)+B*(5*d)+S*(5*u)+I*(5*a),w+=m>>>13,m&=8191;let p=w+j*a+z*f+K*c+_*(5*v)+O*(5*A);w=p>>>13,p&=8191,p+=b*(5*x)+h*(5*l)+B*(5*g)+S*(5*d)+I*(5*u),w+=p>>>13,p&=8191;let y=w+j*u+z*a+K*f+_*c+O*(5*v);w=y>>>13,y&=8191,y+=b*(5*A)+h*(5*x)+B*(5*l)+S*(5*g)+I*(5*d),w+=y>>>13,y&=8191;let U=w+j*d+z*u+K*a+_*f+O*c;w=U>>>13,U&=8191,U+=b*(5*v)+h*(5*A)+B*(5*x)+S*(5*l)+I*(5*g),w+=U>>>13,U&=8191;let k=w+j*g+z*d+K*u+_*a+O*f;w=k>>>13,k&=8191,k+=b*c+h*(5*v)+B*(5*A)+S*(5*x)+I*(5*l),w+=k>>>13,k&=8191;let T=w+j*l+z*g+K*d+_*u+O*a;w=T>>>13,T&=8191,T+=b*f+h*c+B*(5*v)+S*(5*A)+I*(5*x),w+=T>>>13,T&=8191;let H=w+j*x+z*l+K*g+_*d+O*u;w=H>>>13,H&=8191,H+=b*a+h*f+B*c+S*(5*v)+I*(5*A),w+=H>>>13,H&=8191;let M=w+j*A+z*x+K*l+_*g+O*d;w=M>>>13,M&=8191,M+=b*u+h*a+B*f+S*c+I*(5*v),w+=M>>>13,M&=8191;let D=w+j*v+z*A+K*x+_*l+O*g;w=D>>>13,D&=8191,D+=b*d+h*u+B*a+S*f+I*c,w+=D>>>13,D&=8191,w=(w<<2)+w|0,w=w+R|0,R=w&8191,w=w>>>13,m+=w,s[0]=R,s[1]=m,s[2]=p,s[3]=y,s[4]=U,s[5]=k,s[6]=T,s[7]=H,s[8]=M,s[9]=D}finalize(){let{h:e,pad:r}=this,n=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,n[0]=e[0]+5,o=n[0]>>>13,n[0]&=8191;for(let c=1;c<10;c++)n[c]=e[c]+o,o=n[c]>>>13,n[c]&=8191;n[9]-=8192;let s=(o^1)-1;for(let c=0;c<10;c++)n[c]&=s;s=~s;for(let c=0;c<10;c++)e[c]=e[c]&s|n[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]+r[0];e[0]=i&65535;for(let c=1;c<8;c++)i=(e[c]+r[c]|0)+(i>>>16)|0,e[c]=i&65535;ye(n)}update(e){xn(this),ee(e),e=Xe(e);let{buffer:r,blockLen:n}=this,o=e.length;for(let s=0;s<o;){let i=Math.min(n-this.pos,o-s);if(i===n){for(;n<=o-s;s+=n)this.process(e,s);continue}r.set(e.subarray(s,s+i),this.pos),this.pos+=i,s+=i,this.pos===n&&(this.process(r,0,!1),this.pos=0)}return this}destroy(){ye(this.h,this.r,this.buffer,this.pad)}digestInto(e){xn(this),zr(e,this),this.finished=!0;let{buffer:r,h:n}=this,{pos:o}=this;if(o){for(r[o++]=1;o<16;o++)r[o]=0;this.process(r,0,!0)}this.finalize();let s=0;for(let i=0;i<8;i++)e[s++]=n[i]>>>0,e[s++]=n[i]>>>8;return e}digest(){let{buffer:e,outputLen:r}=this;this.digestInto(e);let n=e.slice(0,r);return this.destroy(),n}};function Js(t){let e=(n,o)=>t(o).update(n).digest(),r=t(new Uint8Array(32));return e.outputLen=r.outputLen,e.blockLen=r.blockLen,e.create=n=>t(n),e}var Qr=Js(t=>new Bn(t));function no(t,e,r,n,o,s=20){let i=t[0],c=t[1],f=t[2],a=t[3],u=e[0],d=e[1],g=e[2],l=e[3],x=e[4],A=e[5],v=e[6],L=e[7],q=o,V=r[0],C=r[1],F=r[2],P=i,Y=c,G=f,j=a,z=u,K=d,_=g,O=l,b=x,h=A,B=v,S=L,I=q,w=V,R=C,m=F;for(let y=0;y<s;y+=2)P=P+z|0,I=N(I^P,16),b=b+I|0,z=N(z^b,12),P=P+z|0,I=N(I^P,8),b=b+I|0,z=N(z^b,7),Y=Y+K|0,w=N(w^Y,16),h=h+w|0,K=N(K^h,12),Y=Y+K|0,w=N(w^Y,8),h=h+w|0,K=N(K^h,7),G=G+_|0,R=N(R^G,16),B=B+R|0,_=N(_^B,12),G=G+_|0,R=N(R^G,8),B=B+R|0,_=N(_^B,7),j=j+O|0,m=N(m^j,16),S=S+m|0,O=N(O^S,12),j=j+O|0,m=N(m^j,8),S=S+m|0,O=N(O^S,7),P=P+K|0,m=N(m^P,16),B=B+m|0,K=N(K^B,12),P=P+K|0,m=N(m^P,8),B=B+m|0,K=N(K^B,7),Y=Y+_|0,I=N(I^Y,16),S=S+I|0,_=N(_^S,12),Y=Y+_|0,I=N(I^Y,8),S=S+I|0,_=N(_^S,7),G=G+O|0,w=N(w^G,16),b=b+w|0,O=N(O^b,12),G=G+O|0,w=N(w^G,8),b=b+w|0,O=N(O^b,7),j=j+z|0,R=N(R^j,16),h=h+R|0,z=N(z^h,12),j=j+z|0,R=N(R^j,8),h=h+R|0,z=N(z^h,7);let p=0;n[p++]=i+P|0,n[p++]=c+Y|0,n[p++]=f+G|0,n[p++]=a+j|0,n[p++]=u+z|0,n[p++]=d+K|0,n[p++]=g+_|0,n[p++]=l+O|0,n[p++]=x+b|0,n[p++]=A+h|0,n[p++]=v+B|0,n[p++]=L+S|0,n[p++]=q+I|0,n[p++]=V+w|0,n[p++]=C+R|0,n[p++]=F+m|0}function Qs(t,e,r,n){let o=t[0],s=t[1],i=t[2],c=t[3],f=e[0],a=e[1],u=e[2],d=e[3],g=e[4],l=e[5],x=e[6],A=e[7],v=r[0],L=r[1],q=r[2],V=r[3];for(let F=0;F<20;F+=2)o=o+f|0,v=N(v^o,16),g=g+v|0,f=N(f^g,12),o=o+f|0,v=N(v^o,8),g=g+v|0,f=N(f^g,7),s=s+a|0,L=N(L^s,16),l=l+L|0,a=N(a^l,12),s=s+a|0,L=N(L^s,8),l=l+L|0,a=N(a^l,7),i=i+u|0,q=N(q^i,16),x=x+q|0,u=N(u^x,12),i=i+u|0,q=N(q^i,8),x=x+q|0,u=N(u^x,7),c=c+d|0,V=N(V^c,16),A=A+V|0,d=N(d^A,12),c=c+d|0,V=N(V^c,8),A=A+V|0,d=N(d^A,7),o=o+a|0,V=N(V^o,16),x=x+V|0,a=N(a^x,12),o=o+a|0,V=N(V^o,8),x=x+V|0,a=N(a^x,7),s=s+u|0,v=N(v^s,16),A=A+v|0,u=N(u^A,12),s=s+u|0,v=N(v^s,8),A=A+v|0,u=N(u^A,7),i=i+d|0,L=N(L^i,16),g=g+L|0,d=N(d^g,12),i=i+d|0,L=N(L^i,8),g=g+L|0,d=N(d^g,7),c=c+f|0,q=N(q^c,16),l=l+q|0,f=N(f^l,12),c=c+f|0,q=N(q^c,8),l=l+q|0,f=N(f^l,7);let C=0;n[C++]=o,n[C++]=s,n[C++]=i,n[C++]=c,n[C++]=v,n[C++]=L,n[C++]=q,n[C++]=V}var vn=En(no,{counterRight:!1,counterLength:4,allowShortKeys:!1}),ei=En(no,{counterRight:!1,counterLength:8,extendNonceFn:Qs,allowShortKeys:!1});var ti=new Uint8Array(16),eo=(t,e)=>{t.update(e);let r=e.length%16;r&&t.update(ti.subarray(r))},ni=new Uint8Array(32);function to(t,e,r,n,o){o!==void 0&&ee(o,void 0,"AAD");let s=t(e,r,ni),i=Gr(n.length,o?o.length:0,!0),c=Qr.create(s);o&&eo(c,o),eo(c,n),c.update(i);let f=c.digest();return ye(s,i),f}var ro=t=>(e,r,n)=>({encrypt(s,i){let c=s.length;i=wn(c+16,i,!1),i.set(s);let f=i.subarray(0,-16);t(e,r,f,f,1);let a=to(t,e,r,f,n);return i.set(a,c),ye(a),i},decrypt(s,i){i=wn(s.length-16,i,!1);let c=s.subarray(0,-16),f=s.subarray(-16),a=to(t,e,r,c,n);if(!qt(f,a))throw new Error("invalid tag");return i.set(s.subarray(0,-16)),t(e,r,i,i,1),ye(a),i}}),Kc=yn({blockSize:64,nonceLength:12,tagLength:16},ro(vn)),zc=yn({blockSize:64,nonceLength:24,tagLength:16},ro(ei));function at(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function ue(t,e=""){if(!Number.isSafeInteger(t)||t<0){let r=e&&`"${e}" `;throw new Error(`${r}expected integer >= 0, got ${t}`)}}function W(t,e,r=""){let n=at(t),o=t?.length,s=e!==void 0;if(!n||s&&o!==e){let i=r&&`"${r}" `,c=s?` of length ${e}`:"",f=n?`length=${o}`:`type=${typeof t}`;throw new Error(i+"expected Uint8Array"+c+", got "+f)}return t}function qe(t){if(typeof t!="function"||typeof t.create!="function")throw new Error("Hash must wrapped by utils.createHasher");ue(t.outputLen),ue(t.blockLen)}function Je(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 so(t,e){W(t,void 0,"digestInto() output");let r=e.outputLen;if(t.length<r)throw new Error('"digestInto() output" expected to be of length >='+r)}function me(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function Mt(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function pe(t,e){return t<<32-e|t>>>e}var io=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",ri=Array.from({length:256},(t,e)=>e.toString(16).padStart(2,"0"));function De(t){if(W(t),io)return t.toHex();let e="";for(let r=0;r<t.length;r++)e+=ri[t[r]];return e}var we={_0:48,_9:57,A:65,F:70,a:97,f:102};function oo(t){if(t>=we._0&&t<=we._9)return t-we._0;if(t>=we.A&&t<=we.F)return t-(we.A-10);if(t>=we.a&&t<=we.f)return t-(we.a-10)}function Ee(t){if(typeof t!="string")throw new Error("hex string expected, got "+typeof t);if(io)return Uint8Array.fromHex(t);let e=t.length,r=e/2;if(e%2)throw new Error("hex string expected, got unpadded hex of length "+e);let n=new Uint8Array(r);for(let o=0,s=0;o<r;o++,s+=2){let i=oo(t.charCodeAt(s)),c=oo(t.charCodeAt(s+1));if(i===void 0||c===void 0){let f=t[s]+t[s+1];throw new Error('hex string expected, got non-hex character "'+f+'" at index '+s)}n[o]=i*16+c}return n}function fe(...t){let e=0;for(let n=0;n<t.length;n++){let o=t[n];W(o),e+=o.length}let r=new Uint8Array(e);for(let n=0,o=0;n<t.length;n++){let s=t[n];r.set(s,o),o+=s.length}return r}function co(t,e={}){let r=(o,s)=>t(s).update(o).digest(),n=t(void 0);return r.outputLen=n.outputLen,r.blockLen=n.blockLen,r.create=o=>t(o),Object.assign(r,e),Object.freeze(r)}function ut(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 fo=t=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,t])});function ao(t,e,r){return t&e^~t&r}function uo(t,e,r){return t&e^t&r^e&r}var jt=class{constructor(e,r,n,o){E(this,"blockLen");E(this,"outputLen");E(this,"padOffset");E(this,"isLE");E(this,"buffer");E(this,"view");E(this,"finished",!1);E(this,"length",0);E(this,"pos",0);E(this,"destroyed",!1);this.blockLen=e,this.outputLen=r,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(e),this.view=Mt(this.buffer)}update(e){Je(this),W(e);let{view:r,buffer:n,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 f=Mt(e);for(;o<=s-i;i+=o)this.process(f,i);continue}n.set(e.subarray(i,i+c),this.pos),this.pos+=c,i+=c,this.pos===o&&(this.process(r,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){Je(this),so(e,this),this.finished=!0;let{buffer:r,view:n,blockLen:o,isLE:s}=this,{pos:i}=this;r[i++]=128,me(this.buffer.subarray(i)),this.padOffset>o-i&&(this.process(n,0),i=0);for(let d=i;d<o;d++)r[d]=0;n.setBigUint64(o-8,BigInt(this.length*8),s),this.process(n,0);let c=Mt(e),f=this.outputLen;if(f%4)throw new Error("_sha2: outputLen must be aligned to 32bit");let a=f/4,u=this.get();if(a>u.length)throw new Error("_sha2: outputLen bigger than state");for(let d=0;d<a;d++)c.setUint32(4*d,u[d],s)}digest(){let{buffer:e,outputLen:r}=this;this.digestInto(e);let n=e.slice(0,r);return this.destroy(),n}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:r,buffer:n,length:o,finished:s,destroyed:i,pos:c}=this;return e.destroyed=i,e.finished=s,e.length=o,e.pos=c,o%r&&e.buffer.set(n),e}clone(){return this._cloneInto()}},Be=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var oi=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]),_e=new Uint32Array(64),An=class extends jt{constructor(e){super(64,e,8,!1)}get(){let{A:e,B:r,C:n,D:o,E:s,F:i,G:c,H:f}=this;return[e,r,n,o,s,i,c,f]}set(e,r,n,o,s,i,c,f){this.A=e|0,this.B=r|0,this.C=n|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=c|0,this.H=f|0}process(e,r){for(let d=0;d<16;d++,r+=4)_e[d]=e.getUint32(r,!1);for(let d=16;d<64;d++){let g=_e[d-15],l=_e[d-2],x=pe(g,7)^pe(g,18)^g>>>3,A=pe(l,17)^pe(l,19)^l>>>10;_e[d]=A+_e[d-7]+x+_e[d-16]|0}let{A:n,B:o,C:s,D:i,E:c,F:f,G:a,H:u}=this;for(let d=0;d<64;d++){let g=pe(c,6)^pe(c,11)^pe(c,25),l=u+g+ao(c,f,a)+oi[d]+_e[d]|0,A=(pe(n,2)^pe(n,13)^pe(n,22))+uo(n,o,s)|0;u=a,a=f,f=c,c=i+l|0,i=s,s=o,o=n,n=l+A|0}n=n+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,c=c+this.E|0,f=f+this.F|0,a=a+this.G|0,u=u+this.H|0,this.set(n,o,s,i,c,f,a,u)}roundClean(){me(_e)}destroy(){this.set(0,0,0,0,0,0,0,0),me(this.buffer)}},Sn=class extends An{constructor(){super(32);E(this,"A",Be[0]|0);E(this,"B",Be[1]|0);E(this,"C",Be[2]|0);E(this,"D",Be[3]|0);E(this,"E",Be[4]|0);E(this,"F",Be[5]|0);E(this,"G",Be[6]|0);E(this,"H",Be[7]|0)}};var Qe=co(()=>new Sn,fo(1));var On=BigInt(0),Rn=BigInt(1);function dt(t,e=""){if(typeof t!="boolean"){let r=e&&`"${e}" `;throw new Error(r+"expected boolean, got type="+typeof t)}return t}function lo(t){if(typeof t=="bigint"){if(!Vt(t))throw new Error("positive bigint expected, got "+t)}else ue(t);return t}function lt(t){let e=lo(t).toString(16);return e.length&1?"0"+e:e}function ho(t){if(typeof t!="string")throw new Error("hex string expected, got "+typeof t);return t===""?On:BigInt("0x"+t)}function et(t){return ho(De(t))}function Tn(t){return ho(De(si(W(t)).reverse()))}function $t(t,e){ue(e),t=lo(t);let r=Ee(t.toString(16).padStart(e*2,"0"));if(r.length!==e)throw new Error("number too large");return r}function Ln(t,e){return $t(t,e).reverse()}function si(t){return Uint8Array.from(t)}var Vt=t=>typeof t=="bigint"&&On<=t;function ii(t,e,r){return Vt(t)&&Vt(e)&&Vt(r)&&e<=t&&t<r}function po(t,e,r,n){if(!ii(e,r,n))throw new Error("expected valid "+t+": "+r+" <= n < "+n+", got "+e)}function In(t){let e;for(e=0;t>On;t>>=Rn,e+=1);return e}var ht=t=>(Rn<<BigInt(t))-Rn;function bo(t,e,r){if(ue(t,"hashLen"),ue(e,"qByteLen"),typeof r!="function")throw new Error("hmacFn must be a function");let n=v=>new Uint8Array(v),o=Uint8Array.of(),s=Uint8Array.of(0),i=Uint8Array.of(1),c=1e3,f=n(t),a=n(t),u=0,d=()=>{f.fill(1),a.fill(0),u=0},g=(...v)=>r(a,fe(f,...v)),l=(v=o)=>{a=g(s,v),f=g(),v.length!==0&&(a=g(i,v),f=g())},x=()=>{if(u++>=c)throw new Error("drbg: tried max amount of iterations");let v=0,L=[];for(;v<e;){f=g();let q=f.slice();L.push(q),v+=f.length}return fe(...L)};return(v,L)=>{d(),l(v);let q;for(;!(q=L(x()));)l();return d(),q}}function pt(t,e={},r={}){if(!t||typeof t!="object")throw new Error("expected valid options object");function n(s,i,c){let f=t[s];if(c&&f===void 0)return;let a=typeof f;if(a!==i||f===null)throw new Error(`param "${s}" is invalid: expected ${i}, got ${a}`)}let o=(s,i)=>Object.entries(s).forEach(([c,f])=>n(c,f,i));o(e,!1),o(r,!0)}function Un(t){let e=new WeakMap;return(r,...n)=>{let o=e.get(r);if(o!==void 0)return o;let s=t(r,...n);return e.set(r,s),s}}var ie=BigInt(0),re=BigInt(1),Me=BigInt(2),yo=BigInt(3),wo=BigInt(4),mo=BigInt(5),ci=BigInt(7),Eo=BigInt(8),fi=BigInt(9),Bo=BigInt(16);function be(t,e){let r=t%e;return r>=ie?r:e+r}function ae(t,e,r){let n=t;for(;e-- >ie;)n*=n,n%=r;return n}function go(t,e){if(t===ie)throw new Error("invert: expected non-zero number");if(e<=ie)throw new Error("invert: expected positive modulus, got "+e);let r=be(t,e),n=e,o=ie,s=re,i=re,c=ie;for(;r!==ie;){let a=n/r,u=n%r,d=o-i*a,g=s-c*a;n=r,r=u,o=i,s=c,i=d,c=g}if(n!==re)throw new Error("invert: does not exist");return be(o,e)}function kn(t,e,r){if(!t.eql(t.sqr(e),r))throw new Error("Cannot find square root")}function vo(t,e){let r=(t.ORDER+re)/wo,n=t.pow(e,r);return kn(t,n,e),n}function ai(t,e){let r=(t.ORDER-mo)/Eo,n=t.mul(e,Me),o=t.pow(n,r),s=t.mul(e,o),i=t.mul(t.mul(s,Me),o),c=t.mul(s,t.sub(i,t.ONE));return kn(t,c,e),c}function ui(t){let e=tt(t),r=Ao(t),n=r(e,e.neg(e.ONE)),o=r(e,n),s=r(e,e.neg(n)),i=(t+ci)/Bo;return(c,f)=>{let a=c.pow(f,i),u=c.mul(a,n),d=c.mul(a,o),g=c.mul(a,s),l=c.eql(c.sqr(u),f),x=c.eql(c.sqr(d),f);a=c.cmov(a,u,l),u=c.cmov(g,d,x);let A=c.eql(c.sqr(u),f),v=c.cmov(a,u,A);return kn(c,v,f),v}}function Ao(t){if(t<yo)throw new Error("sqrt is not defined for small field");let e=t-re,r=0;for(;e%Me===ie;)e/=Me,r++;let n=Me,o=tt(t);for(;xo(o,n)===1;)if(n++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(r===1)return vo;let s=o.pow(n,e),i=(e+re)/Me;return function(f,a){if(f.is0(a))return a;if(xo(f,a)!==1)throw new Error("Cannot find square root");let u=r,d=f.mul(f.ONE,s),g=f.pow(a,e),l=f.pow(a,i);for(;!f.eql(g,f.ONE);){if(f.is0(g))return f.ZERO;let x=1,A=f.sqr(g);for(;!f.eql(A,f.ONE);)if(x++,A=f.sqr(A),x===u)throw new Error("Cannot find square root");let v=re<<BigInt(u-x-1),L=f.pow(d,v);u=x,d=f.sqr(L),g=f.mul(g,d),l=f.mul(l,L)}return l}}function di(t){return t%wo===yo?vo:t%Eo===mo?ai:t%Bo===fi?ui(t):Ao(t)}var li=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Hn(t){let e={ORDER:"bigint",BYTES:"number",BITS:"number"},r=li.reduce((n,o)=>(n[o]="function",n),e);return pt(t,r),t}function hi(t,e,r){if(r<ie)throw new Error("invalid exponent, negatives unsupported");if(r===ie)return t.ONE;if(r===re)return e;let n=t.ONE,o=e;for(;r>ie;)r&re&&(n=t.mul(n,o)),o=t.sqr(o),r>>=re;return n}function Zt(t,e,r=!1){let n=new Array(e.length).fill(r?t.ZERO:void 0),o=e.reduce((i,c,f)=>t.is0(c)?i:(n[f]=i,t.mul(i,c)),t.ONE),s=t.inv(o);return e.reduceRight((i,c,f)=>t.is0(c)?i:(n[f]=t.mul(i,n[f]),t.mul(i,c)),s),n}function xo(t,e){let r=(t.ORDER-re)/Me,n=t.pow(e,r),o=t.eql(n,t.ONE),s=t.eql(n,t.ZERO),i=t.eql(n,t.neg(t.ONE));if(!o&&!s&&!i)throw new Error("invalid Legendre symbol result");return o?1:s?0:-1}function pi(t,e){e!==void 0&&ue(e);let r=e!==void 0?e:t.toString(2).length,n=Math.ceil(r/8);return{nBitLength:r,nByteLength:n}}var _n=class{constructor(e,r={}){E(this,"ORDER");E(this,"BITS");E(this,"BYTES");E(this,"isLE");E(this,"ZERO",ie);E(this,"ONE",re);E(this,"_lengths");E(this,"_sqrt");E(this,"_mod");if(e<=ie)throw new Error("invalid field: expected ORDER > 0, got "+e);let n;this.isLE=!1,r!=null&&typeof r=="object"&&(typeof r.BITS=="number"&&(n=r.BITS),typeof r.sqrt=="function"&&(this.sqrt=r.sqrt),typeof r.isLE=="boolean"&&(this.isLE=r.isLE),r.allowedLengths&&(this._lengths=r.allowedLengths?.slice()),typeof r.modFromBytes=="boolean"&&(this._mod=r.modFromBytes));let{nBitLength:o,nByteLength:s}=pi(e,n);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 be(e,this.ORDER)}isValid(e){if(typeof e!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof e);return ie<=e&&e<this.ORDER}is0(e){return e===ie}isValidNot0(e){return!this.is0(e)&&this.isValid(e)}isOdd(e){return(e&re)===re}neg(e){return be(-e,this.ORDER)}eql(e,r){return e===r}sqr(e){return be(e*e,this.ORDER)}add(e,r){return be(e+r,this.ORDER)}sub(e,r){return be(e-r,this.ORDER)}mul(e,r){return be(e*r,this.ORDER)}pow(e,r){return hi(this,e,r)}div(e,r){return be(e*go(r,this.ORDER),this.ORDER)}sqrN(e){return e*e}addN(e,r){return e+r}subN(e,r){return e-r}mulN(e,r){return e*r}inv(e){return go(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):$t(e,this.BYTES)}fromBytes(e,r=!1){W(e);let{_lengths:n,BYTES:o,isLE:s,ORDER:i,_mod:c}=this;if(n){if(!n.includes(e.length)||e.length>o)throw new Error("Field.fromBytes: expected "+n+" bytes, got "+e.length);let a=new Uint8Array(o);a.set(e,s?0:a.length-e.length),e=a}if(e.length!==o)throw new Error("Field.fromBytes: expected "+o+" bytes, got "+e.length);let f=s?Tn(e):et(e);if(c&&(f=be(f,i)),!r&&!this.isValid(f))throw new Error("invalid field element: outside of range 0..ORDER");return f}invertBatch(e){return Zt(this,e)}cmov(e,r,n){return n?r:e}};function tt(t,e={}){return new _n(t,e)}function So(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 Nn(t){let e=So(t);return e+Math.ceil(e/2)}function Cn(t,e,r=!1){W(t);let n=t.length,o=So(e),s=Nn(e);if(n<16||n<s||n>1024)throw new Error("expected "+s+"-1024 bytes of input, got "+n);let i=r?Tn(t):et(t),c=be(i,e-re)+re;return r?Ln(c,o):$t(c,o)}var nt=BigInt(0),je=BigInt(1);function bt(t,e){let r=e.negate();return t?r:e}function jn(t,e){let r=Zt(t.Fp,e.map(n=>n.Z));return e.map((n,o)=>t.fromAffine(n.toAffine(r[o])))}function Lo(t,e){if(!Number.isSafeInteger(t)||t<=0||t>e)throw new Error("invalid window size, expected [1.."+e+"], got W="+t)}function qn(t,e){Lo(t,e);let r=Math.ceil(e/t)+1,n=2**(t-1),o=2**t,s=ht(t),i=BigInt(t);return{windows:r,windowSize:n,mask:s,maxNumber:o,shiftBy:i}}function Ro(t,e,r){let{windowSize:n,mask:o,maxNumber:s,shiftBy:i}=r,c=Number(t&o),f=t>>i;c>n&&(c-=s,f+=je);let a=e*n,u=a+Math.abs(c)-1,d=c===0,g=c<0,l=e%2!==0;return{nextN:f,offset:u,isZero:d,isNeg:g,isNegF:l,offsetF:a}}var Dn=new WeakMap,Io=new WeakMap;function Mn(t){return Io.get(t)||1}function Oo(t){if(t!==nt)throw new Error("invalid wNAF")}var Pt=class{constructor(e,r){E(this,"BASE");E(this,"ZERO");E(this,"Fn");E(this,"bits");this.BASE=e.BASE,this.ZERO=e.ZERO,this.Fn=e.Fn,this.bits=r}_unsafeLadder(e,r,n=this.ZERO){let o=e;for(;r>nt;)r&je&&(n=n.add(o)),o=o.double(),r>>=je;return n}precomputeWindow(e,r){let{windows:n,windowSize:o}=qn(r,this.bits),s=[],i=e,c=i;for(let f=0;f<n;f++){c=i,s.push(c);for(let a=1;a<o;a++)c=c.add(i),s.push(c);i=c.double()}return s}wNAF(e,r,n){if(!this.Fn.isValid(n))throw new Error("invalid scalar");let o=this.ZERO,s=this.BASE,i=qn(e,this.bits);for(let c=0;c<i.windows;c++){let{nextN:f,offset:a,isZero:u,isNeg:d,isNegF:g,offsetF:l}=Ro(n,c,i);n=f,u?s=s.add(bt(g,r[l])):o=o.add(bt(d,r[a]))}return Oo(n),{p:o,f:s}}wNAFUnsafe(e,r,n,o=this.ZERO){let s=qn(e,this.bits);for(let i=0;i<s.windows&&n!==nt;i++){let{nextN:c,offset:f,isZero:a,isNeg:u}=Ro(n,i,s);if(n=c,!a){let d=r[f];o=o.add(u?d.negate():d)}}return Oo(n),o}getPrecomputes(e,r,n){let o=Dn.get(r);return o||(o=this.precomputeWindow(r,e),e!==1&&(typeof n=="function"&&(o=n(o)),Dn.set(r,o))),o}cached(e,r,n){let o=Mn(e);return this.wNAF(o,this.getPrecomputes(o,e,n),r)}unsafe(e,r,n,o){let s=Mn(e);return s===1?this._unsafeLadder(e,r,o):this.wNAFUnsafe(s,this.getPrecomputes(s,e,n),r,o)}createCache(e,r){Lo(r,this.bits),Io.set(e,r),Dn.delete(e)}hasCache(e){return Mn(e)!==1}};function Uo(t,e,r,n){let o=e,s=t.ZERO,i=t.ZERO;for(;r>nt||n>nt;)r&je&&(s=s.add(o)),n&je&&(i=i.add(o)),o=o.double(),r>>=je,n>>=je;return{p1:s,p2:i}}function To(t,e,r){if(e){if(e.ORDER!==t)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return Hn(e),e}else return tt(t,{isLE:r})}function _o(t,e,r={},n){if(n===void 0&&(n=t==="edwards"),!e||typeof e!="object")throw new Error(`expected valid ${t} CURVE object`);for(let f of["p","n","h"]){let a=e[f];if(!(typeof a=="bigint"&&a>nt))throw new Error(`CURVE.${f} must be positive bigint`)}let o=To(e.p,r.Fp,n),s=To(e.n,r.Fn,n),c=["Gx","Gy","a",t==="weierstrass"?"b":"d"];for(let f of c)if(!o.isValid(e[f]))throw new Error(`CURVE.${f} must be valid field element of CURVE.Fp`);return e=Object.freeze(Object.assign({},e)),{CURVE:e,Fp:o,Fn:s}}function ko(t,e){return function(n){let o=t(n);return{secretKey:o,publicKey:e(o)}}}var Ft=class{constructor(e,r){E(this,"oHash");E(this,"iHash");E(this,"blockLen");E(this,"outputLen");E(this,"finished",!1);E(this,"destroyed",!1);if(qe(e),W(r,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 n=this.blockLen,o=new Uint8Array(n);o.set(r.length>n?e.create().update(r).digest():r);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),me(o)}update(e){return Je(this),this.iHash.update(e),this}digestInto(e){Je(this),W(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:r,iHash:n,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=r._cloneInto(e.oHash),e.iHash=n._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},ke=(t,e,r)=>new Ft(t,e).update(r).digest();ke.create=(t,e)=>new Ft(t,e);var Ho=(t,e)=>(t+(t>=0?e:-e)/No)/e;function bi(t,e,r){let[[n,o],[s,i]]=e,c=Ho(i*t,r),f=Ho(-o*t,r),a=t-c*n-f*s,u=-c*o-f*i,d=a<ve,g=u<ve;d&&(a=-a),g&&(u=-u);let l=ht(Math.ceil(In(r)/2))+rt;if(a<ve||a>=l||u<ve||u>=l)throw new Error("splitScalar (endomorphism): failed, k="+t);return{k1neg:d,k1:a,k2neg:g,k2:u}}function $n(t){if(!["compact","recovered","der"].includes(t))throw new Error('Signature format must be "compact", "recovered", or "der"');return t}function Vn(t,e){let r={};for(let n of Object.keys(e))r[n]=t[n]===void 0?e[n]:t[n];return dt(r.lowS,"lowS"),dt(r.prehash,"prehash"),r.format!==void 0&&$n(r.format),r}var Zn=class extends Error{constructor(e=""){super(e)}},He={Err:Zn,_tlv:{encode:(t,e)=>{let{Err:r}=He;if(t<0||t>256)throw new r("tlv.encode: wrong tag");if(e.length&1)throw new r("tlv.encode: unpadded data");let n=e.length/2,o=lt(n);if(o.length/2&128)throw new r("tlv.encode: long form length too big");let s=n>127?lt(o.length/2|128):"";return lt(t)+s+o+e},decode(t,e){let{Err:r}=He,n=0;if(t<0||t>256)throw new r("tlv.encode: wrong tag");if(e.length<2||e[n++]!==t)throw new r("tlv.decode: wrong tlv");let o=e[n++],s=!!(o&128),i=0;if(!s)i=o;else{let f=o&127;if(!f)throw new r("tlv.decode(long): indefinite length not supported");if(f>4)throw new r("tlv.decode(long): byte length is too big");let a=e.subarray(n,n+f);if(a.length!==f)throw new r("tlv.decode: length bytes not complete");if(a[0]===0)throw new r("tlv.decode(long): zero leftmost byte");for(let u of a)i=i<<8|u;if(n+=f,i<128)throw new r("tlv.decode(long): not minimal encoding")}let c=e.subarray(n,n+i);if(c.length!==i)throw new r("tlv.decode: wrong value length");return{v:c,l:e.subarray(n+i)}}},_int:{encode(t){let{Err:e}=He;if(t<ve)throw new e("integer: negative integers are not allowed");let r=lt(t);if(Number.parseInt(r[0],16)&8&&(r="00"+r),r.length&1)throw new e("unexpected DER parsing assertion: unpadded hex");return r},decode(t){let{Err:e}=He;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 et(t)}},toSig(t){let{Err:e,_int:r,_tlv:n}=He,o=W(t,void 0,"signature"),{v:s,l:i}=n.decode(48,o);if(i.length)throw new e("invalid signature: left bytes after parsing");let{v:c,l:f}=n.decode(2,s),{v:a,l:u}=n.decode(2,f);if(u.length)throw new e("invalid signature: left bytes after parsing");return{r:r.decode(c),s:r.decode(a)}},hexFromSig(t){let{_tlv:e,_int:r}=He,n=e.encode(2,r.encode(t.r)),o=e.encode(2,r.encode(t.s)),s=n+o;return e.encode(48,s)}},ve=BigInt(0),rt=BigInt(1),No=BigInt(2),Kt=BigInt(3),gi=BigInt(4);function Co(t,e={}){let r=_o("weierstrass",t,e),{Fp:n,Fn:o}=r,s=r.CURVE,{h:i,n:c}=s;pt(e,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object"});let{endo:f}=e;if(f&&(!n.is0(s.a)||typeof f.beta!="bigint"||!Array.isArray(f.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');let a=Do(n,o);function u(){if(!n.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function d(O,b,h){let{x:B,y:S}=b.toAffine(),I=n.toBytes(B);if(dt(h,"isCompressed"),h){u();let w=!n.isOdd(S);return fe(qo(w),I)}else return fe(Uint8Array.of(4),I,n.toBytes(S))}function g(O){W(O,void 0,"Point");let{publicKey:b,publicKeyUncompressed:h}=a,B=O.length,S=O[0],I=O.subarray(1);if(B===b&&(S===2||S===3)){let w=n.fromBytes(I);if(!n.isValid(w))throw new Error("bad point: is not on curve, wrong x");let R=A(w),m;try{m=n.sqrt(R)}catch(U){let k=U instanceof Error?": "+U.message:"";throw new Error("bad point: is not on curve, sqrt error"+k)}u();let p=n.isOdd(m);return(S&1)===1!==p&&(m=n.neg(m)),{x:w,y:m}}else if(B===h&&S===4){let w=n.BYTES,R=n.fromBytes(I.subarray(0,w)),m=n.fromBytes(I.subarray(w,w*2));if(!v(R,m))throw new Error("bad point: is not on curve");return{x:R,y:m}}else throw new Error(`bad point: got length ${B}, expected compressed=${b} or uncompressed=${h}`)}let l=e.toBytes||d,x=e.fromBytes||g;function A(O){let b=n.sqr(O),h=n.mul(b,O);return n.add(n.add(h,n.mul(O,s.a)),s.b)}function v(O,b){let h=n.sqr(b),B=A(O);return n.eql(h,B)}if(!v(s.Gx,s.Gy))throw new Error("bad curve params: generator point");let L=n.mul(n.pow(s.a,Kt),gi),q=n.mul(n.sqr(s.b),BigInt(27));if(n.is0(n.add(L,q)))throw new Error("bad curve params: a or b");function V(O,b,h=!1){if(!n.isValid(b)||h&&n.is0(b))throw new Error(`bad point coordinate ${O}`);return b}function C(O){if(!(O instanceof j))throw new Error("Weierstrass Point expected")}function F(O){if(!f||!f.basises)throw new Error("no endo");return bi(O,f.basises,o.ORDER)}let P=Un((O,b)=>{let{X:h,Y:B,Z:S}=O;if(n.eql(S,n.ONE))return{x:h,y:B};let I=O.is0();b==null&&(b=I?n.ONE:n.inv(S));let w=n.mul(h,b),R=n.mul(B,b),m=n.mul(S,b);if(I)return{x:n.ZERO,y:n.ZERO};if(!n.eql(m,n.ONE))throw new Error("invZ was invalid");return{x:w,y:R}}),Y=Un(O=>{if(O.is0()){if(e.allowInfinityPoint&&!n.is0(O.Y))return;throw new Error("bad point: ZERO")}let{x:b,y:h}=O.toAffine();if(!n.isValid(b)||!n.isValid(h))throw new Error("bad point: x or y not field elements");if(!v(b,h))throw new Error("bad point: equation left != right");if(!O.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function G(O,b,h,B,S){return h=new j(n.mul(h.X,O),h.Y,h.Z),b=bt(B,b),h=bt(S,h),b.add(h)}let _=class _{constructor(b,h,B){E(this,"X");E(this,"Y");E(this,"Z");this.X=V("x",b),this.Y=V("y",h,!0),this.Z=V("z",B),Object.freeze(this)}static CURVE(){return s}static fromAffine(b){let{x:h,y:B}=b||{};if(!b||!n.isValid(h)||!n.isValid(B))throw new Error("invalid affine point");if(b instanceof _)throw new Error("projective point not allowed");return n.is0(h)&&n.is0(B)?_.ZERO:new _(h,B,n.ONE)}static fromBytes(b){let h=_.fromAffine(x(W(b,void 0,"point")));return h.assertValidity(),h}static fromHex(b){return _.fromBytes(Ee(b))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(b=8,h=!0){return K.createCache(this,b),h||this.multiply(Kt),this}assertValidity(){Y(this)}hasEvenY(){let{y:b}=this.toAffine();if(!n.isOdd)throw new Error("Field doesn't support isOdd");return!n.isOdd(b)}equals(b){C(b);let{X:h,Y:B,Z:S}=this,{X:I,Y:w,Z:R}=b,m=n.eql(n.mul(h,R),n.mul(I,S)),p=n.eql(n.mul(B,R),n.mul(w,S));return m&&p}negate(){return new _(this.X,n.neg(this.Y),this.Z)}double(){let{a:b,b:h}=s,B=n.mul(h,Kt),{X:S,Y:I,Z:w}=this,R=n.ZERO,m=n.ZERO,p=n.ZERO,y=n.mul(S,S),U=n.mul(I,I),k=n.mul(w,w),T=n.mul(S,I);return T=n.add(T,T),p=n.mul(S,w),p=n.add(p,p),R=n.mul(b,p),m=n.mul(B,k),m=n.add(R,m),R=n.sub(U,m),m=n.add(U,m),m=n.mul(R,m),R=n.mul(T,R),p=n.mul(B,p),k=n.mul(b,k),T=n.sub(y,k),T=n.mul(b,T),T=n.add(T,p),p=n.add(y,y),y=n.add(p,y),y=n.add(y,k),y=n.mul(y,T),m=n.add(m,y),k=n.mul(I,w),k=n.add(k,k),y=n.mul(k,T),R=n.sub(R,y),p=n.mul(k,U),p=n.add(p,p),p=n.add(p,p),new _(R,m,p)}add(b){C(b);let{X:h,Y:B,Z:S}=this,{X:I,Y:w,Z:R}=b,m=n.ZERO,p=n.ZERO,y=n.ZERO,U=s.a,k=n.mul(s.b,Kt),T=n.mul(h,I),H=n.mul(B,w),M=n.mul(S,R),D=n.add(h,B),$=n.add(I,w);D=n.mul(D,$),$=n.add(T,H),D=n.sub(D,$),$=n.add(h,S);let Z=n.add(I,R);return $=n.mul($,Z),Z=n.add(T,M),$=n.sub($,Z),Z=n.add(B,S),m=n.add(w,R),Z=n.mul(Z,m),m=n.add(H,M),Z=n.sub(Z,m),y=n.mul(U,$),m=n.mul(k,M),y=n.add(m,y),m=n.sub(H,y),y=n.add(H,y),p=n.mul(m,y),H=n.add(T,T),H=n.add(H,T),M=n.mul(U,M),$=n.mul(k,$),H=n.add(H,M),M=n.sub(T,M),M=n.mul(U,M),$=n.add($,M),T=n.mul(H,$),p=n.add(p,T),T=n.mul(Z,$),m=n.mul(D,m),m=n.sub(m,T),T=n.mul(D,H),y=n.mul(Z,y),y=n.add(y,T),new _(m,p,y)}subtract(b){return this.add(b.negate())}is0(){return this.equals(_.ZERO)}multiply(b){let{endo:h}=e;if(!o.isValidNot0(b))throw new Error("invalid scalar: out of range");let B,S,I=w=>K.cached(this,w,R=>jn(_,R));if(h){let{k1neg:w,k1:R,k2neg:m,k2:p}=F(b),{p:y,f:U}=I(R),{p:k,f:T}=I(p);S=U.add(T),B=G(h.beta,y,k,w,m)}else{let{p:w,f:R}=I(b);B=w,S=R}return jn(_,[B,S])[0]}multiplyUnsafe(b){let{endo:h}=e,B=this;if(!o.isValid(b))throw new Error("invalid scalar: out of range");if(b===ve||B.is0())return _.ZERO;if(b===rt)return B;if(K.hasCache(this))return this.multiply(b);if(h){let{k1neg:S,k1:I,k2neg:w,k2:R}=F(b),{p1:m,p2:p}=Uo(_,B,I,R);return G(h.beta,m,p,S,w)}else return K.unsafe(B,b)}toAffine(b){return P(this,b)}isTorsionFree(){let{isTorsionFree:b}=e;return i===rt?!0:b?b(_,this):K.unsafe(this,c).is0()}clearCofactor(){let{clearCofactor:b}=e;return i===rt?this:b?b(_,this):this.multiplyUnsafe(i)}isSmallOrder(){return this.multiplyUnsafe(i).is0()}toBytes(b=!0){return dt(b,"isCompressed"),this.assertValidity(),l(_,this,b)}toHex(b=!0){return De(this.toBytes(b))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}};E(_,"BASE",new _(s.Gx,s.Gy,n.ONE)),E(_,"ZERO",new _(n.ZERO,n.ONE,n.ZERO)),E(_,"Fp",n),E(_,"Fn",o);let j=_,z=o.BITS,K=new Pt(j,e.endo?Math.ceil(z/2):z);return j.BASE.precompute(8),j}function qo(t){return Uint8Array.of(t?2:3)}function Do(t,e){return{secretKey:e.BYTES,publicKey:1+t.BYTES,publicKeyUncompressed:1+2*t.BYTES,publicKeyHasPrefix:!0,signature:2*e.BYTES}}function xi(t,e={}){let{Fn:r}=t,n=e.randomBytes||ut,o=Object.assign(Do(t.Fp,r),{seed:Nn(r.ORDER)});function s(l){try{let x=r.fromBytes(l);return r.isValidNot0(x)}catch{return!1}}function i(l,x){let{publicKey:A,publicKeyUncompressed:v}=o;try{let L=l.length;return x===!0&&L!==A||x===!1&&L!==v?!1:!!t.fromBytes(l)}catch{return!1}}function c(l=n(o.seed)){return Cn(W(l,o.seed,"seed"),r.ORDER)}function f(l,x=!0){return t.BASE.multiply(r.fromBytes(l)).toBytes(x)}function a(l){let{secretKey:x,publicKey:A,publicKeyUncompressed:v}=o;if(!at(l)||"_lengths"in r&&r._lengths||x===A)return;let L=W(l,void 0,"key").length;return L===A||L===v}function u(l,x,A=!0){if(a(l)===!0)throw new Error("first arg must be private key");if(a(x)===!1)throw new Error("second arg must be public key");let v=r.fromBytes(l);return t.fromBytes(x).multiply(v).toBytes(A)}let d={isValidSecretKey:s,isValidPublicKey:i,randomSecretKey:c},g=ko(c,f);return Object.freeze({getPublicKey:f,getSharedSecret:u,keygen:g,Point:t,utils:d,lengths:o})}function Mo(t,e,r={}){qe(e),pt(r,{},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"}),r=Object.assign({},r);let n=r.randomBytes||ut,o=r.hmac||((b,h)=>ke(e,b,h)),{Fp:s,Fn:i}=t,{ORDER:c,BITS:f}=i,{keygen:a,getPublicKey:u,getSharedSecret:d,utils:g,lengths:l}=xi(t,r),x={prehash:!0,lowS:typeof r.lowS=="boolean"?r.lowS:!0,format:"compact",extraEntropy:!1},A=c*No<s.ORDER;function v(b){let h=c>>rt;return b>h}function L(b,h){if(!i.isValidNot0(h))throw new Error(`invalid signature ${b}: out of range 1..Point.Fn.ORDER`);return h}function q(){if(A)throw new Error('"recovered" sig type is not supported for cofactor >2 curves')}function V(b,h){$n(h);let B=l.signature,S=h==="compact"?B:h==="recovered"?B+1:void 0;return W(b,S)}class C{constructor(h,B,S){E(this,"r");E(this,"s");E(this,"recovery");if(this.r=L("r",h),this.s=L("s",B),S!=null){if(q(),![0,1,2,3].includes(S))throw new Error("invalid recovery id");this.recovery=S}Object.freeze(this)}static fromBytes(h,B=x.format){V(h,B);let S;if(B==="der"){let{r:m,s:p}=He.toSig(W(h));return new C(m,p)}B==="recovered"&&(S=h[0],B="compact",h=h.subarray(1));let I=l.signature/2,w=h.subarray(0,I),R=h.subarray(I,I*2);return new C(i.fromBytes(w),i.fromBytes(R),S)}static fromHex(h,B){return this.fromBytes(Ee(h),B)}assertRecovery(){let{recovery:h}=this;if(h==null)throw new Error("invalid recovery id: must be present");return h}addRecoveryBit(h){return new C(this.r,this.s,h)}recoverPublicKey(h){let{r:B,s:S}=this,I=this.assertRecovery(),w=I===2||I===3?B+c:B;if(!s.isValid(w))throw new Error("invalid recovery id: sig.r+curve.n != R.x");let R=s.toBytes(w),m=t.fromBytes(fe(qo((I&1)===0),R)),p=i.inv(w),y=P(W(h,void 0,"msgHash")),U=i.create(-y*p),k=i.create(S*p),T=t.BASE.multiplyUnsafe(U).add(m.multiplyUnsafe(k));if(T.is0())throw new Error("invalid recovery: point at infinify");return T.assertValidity(),T}hasHighS(){return v(this.s)}toBytes(h=x.format){if($n(h),h==="der")return Ee(He.hexFromSig(this));let{r:B,s:S}=this,I=i.toBytes(B),w=i.toBytes(S);return h==="recovered"?(q(),fe(Uint8Array.of(this.assertRecovery()),I,w)):fe(I,w)}toHex(h){return De(this.toBytes(h))}}let F=r.bits2int||function(h){if(h.length>8192)throw new Error("input is too large");let B=et(h),S=h.length*8-f;return S>0?B>>BigInt(S):B},P=r.bits2int_modN||function(h){return i.create(F(h))},Y=ht(f);function G(b){return po("num < 2^"+f,b,ve,Y),i.toBytes(b)}function j(b,h){return W(b,void 0,"message"),h?W(e(b),void 0,"prehashed message"):b}function z(b,h,B){let{lowS:S,prehash:I,extraEntropy:w}=Vn(B,x);b=j(b,I);let R=P(b),m=i.fromBytes(h);if(!i.isValidNot0(m))throw new Error("invalid private key");let p=[G(m),G(R)];if(w!=null&&w!==!1){let T=w===!0?n(l.secretKey):w;p.push(W(T,void 0,"extraEntropy"))}let y=fe(...p),U=R;function k(T){let H=F(T);if(!i.isValidNot0(H))return;let M=i.inv(H),D=t.BASE.multiply(H).toAffine(),$=i.create(D.x);if($===ve)return;let Z=i.create(M*i.create(U+$*m));if(Z===ve)return;let J=(D.x===$?0:2)|Number(D.y&rt),ne=Z;return S&&v(Z)&&(ne=i.neg(Z),J^=1),new C($,ne,A?void 0:J)}return{seed:y,k2sig:k}}function K(b,h,B={}){let{seed:S,k2sig:I}=z(b,h,B);return bo(e.outputLen,i.BYTES,o)(S,I).toBytes(B.format)}function _(b,h,B,S={}){let{lowS:I,prehash:w,format:R}=Vn(S,x);if(B=W(B,void 0,"publicKey"),h=j(h,w),!at(b)){let m=b instanceof C?", use sig.toBytes()":"";throw new Error("verify expects Uint8Array signature"+m)}V(b,R);try{let m=C.fromBytes(b,R),p=t.fromBytes(B);if(I&&m.hasHighS())return!1;let{r:y,s:U}=m,k=P(h),T=i.inv(U),H=i.create(k*T),M=i.create(y*T),D=t.BASE.multiplyUnsafe(H).add(p.multiplyUnsafe(M));return D.is0()?!1:i.create(D.x)===y}catch{return!1}}function O(b,h,B={}){let{prehash:S}=Vn(B,x);return h=j(h,S),C.fromBytes(b,"recovered").recoverPublicKey(h).toBytes()}return Object.freeze({keygen:a,getPublicKey:u,getSharedSecret:d,utils:g,lengths:l,Point:t,sign:K,verify:_,recoverPublicKey:O,Signature:C,hash:e})}var Fn={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},yi={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]};var jo=BigInt(2);function wi(t){let e=Fn.p,r=BigInt(3),n=BigInt(6),o=BigInt(11),s=BigInt(22),i=BigInt(23),c=BigInt(44),f=BigInt(88),a=t*t*t%e,u=a*a*t%e,d=ae(u,r,e)*u%e,g=ae(d,r,e)*u%e,l=ae(g,jo,e)*a%e,x=ae(l,o,e)*l%e,A=ae(x,s,e)*x%e,v=ae(A,c,e)*A%e,L=ae(v,f,e)*v%e,q=ae(L,c,e)*A%e,V=ae(q,r,e)*u%e,C=ae(V,i,e)*x%e,F=ae(C,n,e)*a%e,P=ae(F,jo,e);if(!Pn.eql(Pn.sqr(P),t))throw new Error("Cannot find square root");return P}var Pn=tt(Fn.p,{sqrt:wi}),mi=Co(Fn,{Fp:Pn,endo:yi}),Vo=Mo(mi,Qe);function Zo(t,e,r){return qe(t),r===void 0&&(r=new Uint8Array(t.outputLen)),ke(t,r,e)}var Kn=Uint8Array.of(0),$o=Uint8Array.of();function Po(t,e,r,n=32){qe(t),ue(n,"length");let o=t.outputLen;if(n>255*o)throw new Error("Length must be <= 255*HashLen");let s=Math.ceil(n/o);r===void 0?r=$o:W(r,void 0,"info");let i=new Uint8Array(s*o),c=ke.create(t,e),f=c._cloneInto(),a=new Uint8Array(c.outputLen);for(let u=0;u<s;u++)Kn[0]=u+1,f.update(u===0?$o:a).update(r).update(Kn).digestInto(a),i.set(a,o*u),c._cloneInto(f);return c.destroy(),f.destroy(),me(a,Kn),i.slice(0,n)}function Ko(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function Ei(t){if(!Ko(t))throw new Error("Uint8Array expected")}function zo(t,e){return Array.isArray(e)?e.length===0?!0:t?e.every(r=>typeof r=="string"):e.every(r=>Number.isSafeInteger(r)):!1}function gt(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 Yn(t){if(!Array.isArray(t))throw new Error("array expected")}function zt(t,e){if(!zo(!0,e))throw new Error(`${t}: array of strings expected`)}function Bi(t,e){if(!zo(!1,e))throw new Error(`${t}: array of numbers expected`)}function vi(...t){let e=s=>s,r=(s,i)=>c=>s(i(c)),n=t.map(s=>s.encode).reduceRight(r,e),o=t.map(s=>s.decode).reduce(r,e);return{encode:n,decode:o}}function Ai(t){let e=typeof t=="string"?t.split(""):t,r=e.length;zt("alphabet",e);let n=new Map(e.map((o,s)=>[o,s]));return{encode:o=>(Yn(o),o.map(s=>{if(!Number.isSafeInteger(s)||s<0||s>=r)throw new Error(`alphabet.encode: digit index outside alphabet "${s}". Allowed: ${t}`);return e[s]})),decode:o=>(Yn(o),o.map(s=>{gt("alphabet.decode",s);let i=n.get(s);if(i===void 0)throw new Error(`Unknown letter: "${s}". Allowed: ${t}`);return i}))}}function Si(t=""){return gt("join",t),{encode:e=>(zt("join.decode",e),e.join(t)),decode:e=>(gt("join.decode",e),e.split(t))}}function Ri(t,e="="){return Gn(t),gt("padding",e),{encode(r){for(zt("padding.encode",r);r.length*t%8;)r.push(e);return r},decode(r){zt("padding.decode",r);let n=r.length;if(n*t%8)throw new Error("padding: invalid, string should have whole number of bytes");for(;n>0&&r[n-1]===e;n--)if((n-1)*t%8===0)throw new Error("padding: invalid, string has too much padding");return r.slice(0,n)}}}var Yo=(t,e)=>e===0?t:Yo(e,t%e),Yt=(t,e)=>t+(e-Yo(t,e)),zn=(()=>{let t=[];for(let e=0;e<40;e++)t.push(2**e);return t})();function Fo(t,e,r,n){if(Yn(t),e<=0||e>32)throw new Error(`convertRadix2: wrong from=${e}`);if(r<=0||r>32)throw new Error(`convertRadix2: wrong to=${r}`);if(Yt(e,r)>32)throw new Error(`convertRadix2: carry overflow from=${e} to=${r} carryBits=${Yt(e,r)}`);let o=0,s=0,i=zn[e],c=zn[r]-1,f=[];for(let a of t){if(Gn(a),a>=i)throw new Error(`convertRadix2: invalid data word=${a} from=${e}`);if(o=o<<e|a,s+e>32)throw new Error(`convertRadix2: carry overflow pos=${s} from=${e}`);for(s+=e;s>=r;s-=r)f.push((o>>s-r&c)>>>0);let u=zn[s];if(u===void 0)throw new Error("invalid carry");o&=u-1}if(o=o<<r-s&c,!n&&s>=e)throw new Error("Excess padding");if(!n&&o>0)throw new Error(`Non-zero padding: ${o}`);return n&&s>0&&f.push(o>>>0),f}function Oi(t,e=!1){if(Gn(t),t<=0||t>32)throw new Error("radix2: bits should be in (0..32]");if(Yt(8,t)>32||Yt(t,8)>32)throw new Error("radix2: carry overflow");return{encode:r=>{if(!Ko(r))throw new Error("radix2.encode input should be Uint8Array");return Fo(Array.from(r),8,t,!e)},decode:r=>(Bi("radix2.decode",r),Uint8Array.from(Fo(r,t,8,e)))}}var Ti=typeof Uint8Array.from([]).toBase64=="function"&&typeof Uint8Array.fromBase64=="function",Li=(t,e)=>{gt("base64",t);let r=e?/^[A-Za-z0-9=_-]+$/:/^[A-Za-z0-9=+/]+$/,n=e?"base64url":"base64";if(t.length>0&&!r.test(t))throw new Error("invalid base64");return Uint8Array.fromBase64(t,{alphabet:n,lastChunkHandling:"strict"})},Go=Ti?{encode(t){return Ei(t),t.toBase64()},decode(t){return Li(t,!1)}}:vi(Oi(6),Ai("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),Ri(6),Si(""));var Ii=new TextDecoder("utf-8"),Ui=new TextEncoder,_i=1,ki=4294967295,Hi=65536;function Wn(t,e){let r=Vo.getSharedSecret(t,Ee("02"+e)).subarray(1,33);return Zo(Qe,r,Ui.encode("nip44-v2"))}function Ni(t,e){let r=Po(Qe,t,e,76);return{chacha_key:r.subarray(0,32),chacha_nonce:r.subarray(32,44),hmac_key:r.subarray(44,76)}}function Ci(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),r=e<=256?32:e/8;return r*(Math.floor((t-1)/r)+1)}function qi(t){let e=new DataView(t.buffer,t.byteOffset,t.byteLength),r=e.getUint16(0),n,o;if(r===0){if(n=e.getUint32(2),n<Hi)throw new Error("invalid padding");o=6}else n=r,o=2;let s=t.subarray(o,o+n);if(n<_i||n>ki||s.length!==n||t.length!==o+Ci(n))throw new Error("invalid padding");return Ii.decode(s)}function Di(t,e,r){if(r.length!==32)throw new Error("AAD associated data must be 32 bytes");let n=fe(r,e);return ke(Qe,t,n)}function Mi(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 r;try{r=Go.decode(t)}catch(s){throw new Error("invalid base64: "+s.message)}let n=r.length;if(n<99)throw new Error("invalid data length: "+n);let o=r[0];if(o!==2)throw new Error("unknown encryption version "+o);return{nonce:r.subarray(1,33),ciphertext:r.subarray(33,-32),mac:r.subarray(-32)}}function Xn(t,e){let{nonce:r,ciphertext:n,mac:o}=Mi(t),{chacha_key:s,chacha_nonce:i,hmac_key:c}=Ni(e,r),f=Di(c,n,r);if(!qt(f,o))throw new Error("invalid MAC");let a=vn(s,i,n);return qi(a)}function ji(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function Vi(){let t=new Uint8Array(16);return crypto.getRandomValues(t),Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")}function Gt(t,e){let r=t.find(n=>n[0]===e);return r?r[1]:void 0}function Ae(t){let e=new Uint8Array(t.length/2);for(let r=0;r<e.length;r++)e[r]=parseInt(t.substr(r*2,2),16);return e}async function $i(t){if(!t.id||!t.pubkey||!t.sig||!t.tags||t.kind!==30470||!/^[0-9a-f]{128}$/i.test(t.sig)||!/^[0-9a-f]{64}$/i.test(t.pubkey)||!/^[0-9a-f]{64}$/i.test(t.id)||t.tags.length>100||t.content.length>65536||t.tags.some(i=>i.some(c=>c.length>1024)))return!1;let e=t.tags.map(i=>i[0]);if(!e.includes("tier")||!e.includes("age-range")||!e.includes("entity-type"))return!1;let r=JSON.stringify([0,t.pubkey,t.created_at,t.kind,t.tags,t.content]),n=new TextEncoder,o=$e(n.encode(r));if(Re(o)!==t.id.toLowerCase())return!1;try{let i=Ae(t.sig),c=Ae(t.id),f=Ae(t.pubkey);return ft.verify(i,c,f)}catch{return!1}}function Zi(t,e){if(t===e||t==="18+"&&e==="18+")return!0;let r=["0-3","4-7","8-12","13-17","18+"],n=r.indexOf(t),o=r.indexOf(e);return n===-1||o===-1?!1:e==="18+"?t==="18+":t===e}async function Pi(t,e){if(e==null||!/^https:\/\//i.test(e)||!/^[0-9a-f]{64}$/i.test(t))return null;try{let r=await fetch(`${e}/status/${t}`,{signal:AbortSignal.timeout(5e3)});if(!r.ok)return null;let n=await r.json();if(typeof n!="object"||n===null)return null;let o=n;return{confirmed:o.confirmed===!0,method:["A","B","C","D"].includes(o.method)?o.method:null,profession:typeof o.profession=="string"?o.profession:void 0,jurisdiction:typeof o.jurisdiction=="string"?o.jurisdiction:void 0}}catch{return null}}async function Wo(t,e){if(!["0-3","4-7","8-12","13-17","18+"].includes(t))return{verified:!1,ageRange:null,tier:null,entityType:null,credentialId:null,verifierPubkey:null,verifierConfirmed:null,verifierMethod:null,issuedAt:null,expiresAt:null,error:"invalid-age-range"};let n={requiredAgeRange:t,relayUrl:e?.relayUrl||"wss://relay.damus.io",theme:e?.theme||"auto",timeout:e?.timeout||12e4,verifierCheckUrl:e?.verifierCheckUrl!==void 0?e.verifierCheckUrl:"https://verify.signet.forgesworn.dev",acceptUnconfirmed:e?.acceptUnconfirmed||!1,...e};n.timeout=Math.max(5e3,Math.min(n.timeout??12e4,6e5));let o=Vi(),s={type:"signet-verify-request",requestId:o,requiredAgeRange:n.requiredAgeRange,relayUrl:n.relayUrl,timestamp:Math.floor(Date.now()/1e3)},i=JSON.stringify(s),c=btoa(i);return new Promise(f=>{let a=document.createElement("style");a.textContent="#signet-verify-dialog::backdrop{background:rgba(0,0,0,0.7)}",document.head.appendChild(a);let u=n.theme==="dark"||n.theme==="auto"&&window.matchMedia("(prefers-color-scheme: dark)").matches,d=u?"#1a1a2e":"#ffffff",g=u?"#e0e0e0":"#1a1a2e",l=u?"#888":"#666",x=document.createElement("dialog");x.id="signet-verify-dialog",x.style.cssText=`border:none;border-radius:16px;padding:32px;max-width:380px;width:90%;text-align:center;box-shadow:0 20px 60px rgba(0,0,0,0.3);background:${d};color:${g};font-family:system-ui,-apple-system,sans-serif;`,x.innerHTML=`
2
2
  <h2 style="margin:0 0 8px;font-size:1.3rem;">Verify your age with Signet</h2>
3
- <p style="margin:0 0 24px;color:${d};font-size:0.9rem;">Scan this QR code with your Signet app to prove you are ${_i(e)}. No personal data is shared.</p>
3
+ <p style="margin:0 0 24px;color:${l};font-size:0.9rem;">Scan this QR code with your Signet app to prove you are ${ji(t)}. No personal data is shared.</p>
4
4
  <div id="signet-qr" style="display:flex;justify-content:center;margin-bottom:24px;"></div>
5
- <p style="margin:0 0 16px;color:${d};font-size:0.8rem;">Waiting for verification...</p>
6
- <button id="signet-cancel" style="background:none;border:1px solid ${d};color:${b};padding:10px 24px;border-radius:8px;cursor:pointer;font-size:0.9rem;">Cancel</button>
7
- `,document.body.appendChild(y),y.showModal();let B=y.querySelector("#signet-qr");if(B){let _=document.createElement("div");_.style.cssText=`width:200px;height:200px;background:${h?"#2a2a3e":"#f0f0f0"};border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:0.75rem;color:${d};word-break:break-all;padding:12px;`,_.textContent=`signet:verify:${c.slice(0,40)}...`,B.appendChild(_)}let S=new BroadcastChannel("signet-verify-"+o);y.querySelector("#signet-cancel")?.addEventListener("click",()=>{S.close(),y.close(),y.remove(),u.remove(),f({verified:!1,ageRange:null,tier:null,entityType:null,credentialId:null,verifierPubkey:null,verifierConfirmed:null,verifierMethod:null,issuedAt:null,expiresAt:null,error:"cancelled"})}),S.onmessage=async _=>{let V=_.data;if(typeof V!="object"||V===null)return;let q=V;if(q.type!=="signet-verify-response"||q.requestId!==o||!q.credential||typeof q.credential!="object"||!Array.isArray(q.credential.tags))return;let N=q.credential,C=await Ri(N),P=rn(N.tags,"age-range"),Y=rn(N.tags,"tier"),z=rn(N.tags,"entity-type"),U=rn(N.tags,"expires"),K=P?Oi(P,n.requiredAgeRange):!1,M=await Hi(N.pubkey,n.verifierCheckUrl),L=M?.confirmed??null,w=M?.method??null,l=n.acceptUnconfirmed||L===!0;y.close(),y.remove(),u.remove(),S.close();let a=Y?parseInt(Y,10):null,x=U?parseInt(U,10):null,m=Math.floor(Date.now()/1e3),v=x===null||!isNaN(x)&&x>m,p;C?v?K?l||(p=L===!1?"verifier-not-confirmed":"verifier-check-unavailable"):p="age-range-not-met":p="credential-expired":p="invalid-credential",f({verified:C&&v&&K&&l,ageRange:P||null,tier:a!==null&&!isNaN(a)?a:null,entityType:z||null,credentialId:N.id,verifierPubkey:N.pubkey,verifierConfirmed:L,verifierMethod:w,issuedAt:N.created_at,expiresAt:x!==null&&!isNaN(x)?x:null,error:p})},setTimeout(()=>{y.close(),y.remove(),u.remove(),S.close(),f({verified:!1,ageRange:null,tier:null,entityType:null,credentialId:null,verifierPubkey:null,verifierConfirmed:null,verifierMethod:null,issuedAt:null,expiresAt:null,error:"timeout"})},n.timeout)})}function cr(e){let t=JSON.stringify([0,e.pubkey,e.created_at,e.kind,e.tags,e.content]),r=Sn(new TextEncoder().encode(t));return st(r)}async function ki(e,t){try{let r=sr(t,e.pubkey),n=ir(e.content,r),o=JSON.parse(n);if(typeof o!="object"||o===null)return null;let s=o;if(s.kind!==13||typeof s.pubkey!="string"||!/^[0-9a-f]{64}$/i.test(s.pubkey)||typeof s.created_at!="number"||!Array.isArray(s.tags)||typeof s.content!="string"||typeof s.id!="string"||!/^[0-9a-f]{64}$/i.test(s.id)||typeof s.sig!="string"||!/^[0-9a-f]{128}$/i.test(s.sig)||cr({pubkey:s.pubkey,created_at:s.created_at,kind:13,tags:s.tags,content:s.content})!==s.id.toLowerCase())return null;let c=Lt(s.sig),f=Lt(s.id),u=Lt(s.pubkey);if(!ye.verify(c,f,u))return null;let h=sr(t,s.pubkey),g=ir(s.content,h),b=JSON.parse(g);if(typeof b!="object"||b===null)return null;let d=b;if(typeof d.pubkey!="string"||d.pubkey!==s.pubkey||typeof d.kind!="number"||typeof d.created_at!="number"||!Array.isArray(d.tags)||typeof d.content!="string")return null;let y=typeof d.id=="string"?d.id:cr({pubkey:d.pubkey,created_at:d.created_at,kind:d.kind,tags:d.tags,content:d.content});return{pubkey:d.pubkey,id:y,kind:d.kind,created_at:d.created_at,tags:d.tags,content:d.content}}catch{return null}}function Ti(e){if(typeof e!="string")return;let t=e.replace(/[\x00-\x1f\x7f-\x9f\u200b-\u200f\u2028-\u202e\u2066-\u2069]/g,"").trim().slice(0,64);return t.length>0?t:void 0}async function Qo(e){if(!/^[0-9a-f]{64}$/i.test(e.requestId))throw new Error("invalid-request-id");if(!(e.sessionPrivKey instanceof Uint8Array)||e.sessionPrivKey.length!==32)throw new Error("invalid-session-privkey");if(!/^wss:\/\//i.test(e.relayUrl)&&!/^ws:\/\/(localhost|127\.0\.0\.1)([:\/]|$)/i.test(e.relayUrl))throw new Error("invalid-relay-url");if(typeof e.expectedOrigin!="string"||e.expectedOrigin.length===0)throw new Error("invalid-expected-origin");let t=st(ye.getPublicKey(e.sessionPrivKey)),r=Math.max(5e3,Math.min(e.timeout??12e4,6e5)),n=e.requestId.toLowerCase(),o=e.expectedOrigin;return new Promise((s,i)=>{let c=`sa-${Math.random().toString(36).slice(2,12)}`,f=!1,u;try{u=new WebSocket(e.relayUrl)}catch{i(new Error("relay-error"));return}let h=b=>{if(!f){f=!0,clearTimeout(g);try{u.close()}catch{}b()}},g=setTimeout(()=>{h(()=>i(new Error("timeout")))},r);u.onopen=()=>{let b=Math.floor(Date.now()/1e3)-60;u.send(JSON.stringify(["REQ",c,{kinds:[1059],"#p":[t],since:b}]))},u.onmessage=async b=>{if(f)return;let d;try{d=JSON.parse(typeof b.data=="string"?b.data:"")}catch{return}if(!Array.isArray(d)||d[0]!=="EVENT"||d[1]!==c)return;let y=d[2];if(typeof y!="object"||y===null)return;let B=y;if(B.kind!==1059||typeof B.pubkey!="string"||typeof B.content!="string")return;let S=await ki({pubkey:B.pubkey,content:B.content},e.sessionPrivKey);if(!S||S.kind!==29999)return;let _=S.tags.find(a=>a[0]==="session");if(!_||_[1]!==n)return;let V=S.tags.find(a=>a[0]==="status");if(V?.[1]==="rejected"){h(()=>i(new Error("denied")));return}if(V?.[1]!=="approved"||Math.abs(Date.now()/1e3-S.created_at)>300)return;let N;try{let a=JSON.parse(S.content);if(typeof a!="object"||a===null)return;N=a}catch{return}if(N.type!=="signet-auth-response"||N.requestId!==n||typeof N.authEvent!="object"||N.authEvent===null)return;let C=N.authEvent;if(typeof C.id!="string"||!/^[0-9a-f]{64}$/i.test(C.id)||typeof C.pubkey!="string"||!/^[0-9a-f]{64}$/i.test(C.pubkey)||typeof C.sig!="string"||!/^[0-9a-f]{128}$/i.test(C.sig)||C.kind!==21236||typeof C.created_at!="number"||!Array.isArray(C.tags)||typeof C.content!="string"||C.pubkey.toLowerCase()!==S.pubkey.toLowerCase()||cr({pubkey:C.pubkey,created_at:C.created_at,kind:21236,tags:C.tags,content:C.content})!==C.id.toLowerCase())return;let Y=!1;try{let a=Lt(C.sig),x=Lt(C.id),m=Lt(C.pubkey);Y=ye.verify(a,x,m)}catch{Y=!1}if(!Y)return;let z=C.tags,U=z.find(a=>Array.isArray(a)&&a[0]==="challenge");if(!U||typeof U[1]!="string"||U[1].toLowerCase()!==n)return;let K=z.find(a=>Array.isArray(a)&&a[0]==="origin");if(!K||K[1]!==o||Math.abs(Date.now()/1e3-C.created_at)>300)return;let L={id:C.id.toLowerCase(),pubkey:C.pubkey.toLowerCase(),kind:21236,created_at:C.created_at,tags:z,content:C.content,sig:C.sig},w=Ti(N.displayName),l=typeof N.bunkerUri=="string"&&/^bunker:\/\//i.test(N.bunkerUri)?N.bunkerUri:void 0;h(()=>s({pubkey:L.pubkey,authEvent:L,credential:N.credential,...w!==void 0?{displayName:w}:{},...l!==void 0?{bunkerUri:l}:{},createdAt:L.created_at}))},u.onerror=()=>{h(()=>i(new Error("relay-error")))}})}typeof window<"u"&&(window.Signet={verifyAge:Jo,waitForAuthResponse:Qo});return ss(Ni);})();
5
+ <p style="margin:0 0 16px;color:${l};font-size:0.8rem;">Waiting for verification...</p>
6
+ <button id="signet-cancel" style="background:none;border:1px solid ${l};color:${g};padding:10px 24px;border-radius:8px;cursor:pointer;font-size:0.9rem;">Cancel</button>
7
+ `,document.body.appendChild(x),x.showModal();let A=x.querySelector("#signet-qr");if(A){let L=document.createElement("div");L.style.cssText=`width:200px;height:200px;background:${u?"#2a2a3e":"#f0f0f0"};border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:0.75rem;color:${l};word-break:break-all;padding:12px;`,L.textContent=`signet:verify:${c.slice(0,40)}...`,A.appendChild(L)}let v=new BroadcastChannel("signet-verify-"+o);x.querySelector("#signet-cancel")?.addEventListener("click",()=>{v.close(),x.close(),x.remove(),a.remove(),f({verified:!1,ageRange:null,tier:null,entityType:null,credentialId:null,verifierPubkey:null,verifierConfirmed:null,verifierMethod:null,issuedAt:null,expiresAt:null,error:"cancelled"})}),v.onmessage=async L=>{let q=L.data;if(typeof q!="object"||q===null)return;let V=q;if(V.type!=="signet-verify-response"||V.requestId!==o||!V.credential||typeof V.credential!="object"||!Array.isArray(V.credential.tags))return;let C=V.credential,F=await $i(C),P=Gt(C.tags,"age-range"),Y=Gt(C.tags,"tier"),G=Gt(C.tags,"entity-type"),j=Gt(C.tags,"expires"),z=P?Zi(P,n.requiredAgeRange):!1,K=await Pi(C.pubkey,n.verifierCheckUrl),_=K?.confirmed??null,O=K?.method??null,b=n.acceptUnconfirmed||_===!0;x.close(),x.remove(),a.remove(),v.close();let h=Y?parseInt(Y,10):null,B=j?parseInt(j,10):null,S=Math.floor(Date.now()/1e3),I=B===null||!isNaN(B)&&B>S,w;F?I?z?b||(w=_===!1?"verifier-not-confirmed":"verifier-check-unavailable"):w="age-range-not-met":w="credential-expired":w="invalid-credential",f({verified:F&&I&&z&&b,ageRange:P||null,tier:h!==null&&!isNaN(h)?h:null,entityType:G||null,credentialId:C.id,verifierPubkey:C.pubkey,verifierConfirmed:_,verifierMethod:O,issuedAt:C.created_at,expiresAt:B!==null&&!isNaN(B)?B:null,error:w})},setTimeout(()=>{x.close(),x.remove(),a.remove(),v.close(),f({verified:!1,ageRange:null,tier:null,entityType:null,credentialId:null,verifierPubkey:null,verifierConfirmed:null,verifierMethod:null,issuedAt:null,expiresAt:null,error:"timeout"})},n.timeout)})}function Jn(t){let e=JSON.stringify([0,t.pubkey,t.created_at,t.kind,t.tags,t.content]),r=$e(new TextEncoder().encode(e));return Re(r)}async function Fi(t,e){try{let r=Wn(e,t.pubkey),n=Xn(t.content,r),o=JSON.parse(n);if(typeof o!="object"||o===null)return null;let s=o;if(s.kind!==13||typeof s.pubkey!="string"||!/^[0-9a-f]{64}$/i.test(s.pubkey)||typeof s.created_at!="number"||!Array.isArray(s.tags)||typeof s.content!="string"||typeof s.id!="string"||!/^[0-9a-f]{64}$/i.test(s.id)||typeof s.sig!="string"||!/^[0-9a-f]{128}$/i.test(s.sig)||Jn({pubkey:s.pubkey,created_at:s.created_at,kind:13,tags:s.tags,content:s.content})!==s.id.toLowerCase())return null;let c=Ae(s.sig),f=Ae(s.id),a=Ae(s.pubkey);if(!ft.verify(c,f,a))return null;let u=Wn(e,s.pubkey),d=Xn(s.content,u),g=JSON.parse(d);if(typeof g!="object"||g===null)return null;let l=g;if(typeof l.pubkey!="string"||l.pubkey!==s.pubkey||typeof l.kind!="number"||typeof l.created_at!="number"||!Array.isArray(l.tags)||typeof l.content!="string")return null;let x=typeof l.id=="string"?l.id:Jn({pubkey:l.pubkey,created_at:l.created_at,kind:l.kind,tags:l.tags,content:l.content});return{pubkey:l.pubkey,id:x,kind:l.kind,created_at:l.created_at,tags:l.tags,content:l.content}}catch{return null}}function Ki(t){if(typeof t!="string")return;let e=t.replace(/[\x00-\x1f\x7f-\x9f\u200b-\u200f\u2028-\u202e\u2066-\u2069]/g,"").trim().slice(0,64);return e.length>0?e:void 0}async function Xo(t){if(!/^[0-9a-f]{64}$/i.test(t.requestId))throw new Error("invalid-request-id");if(!(t.sessionPrivKey instanceof Uint8Array)||t.sessionPrivKey.length!==32)throw new Error("invalid-session-privkey");if(!/^wss:\/\//i.test(t.relayUrl)&&!/^ws:\/\/(localhost|127\.0\.0\.1)([:\/]|$)/i.test(t.relayUrl))throw new Error("invalid-relay-url");if(typeof t.expectedOrigin!="string"||t.expectedOrigin.length===0)throw new Error("invalid-expected-origin");let e=Re(ft.getPublicKey(t.sessionPrivKey)),r=Math.max(5e3,Math.min(t.timeout??12e4,6e5)),n=t.requestId.toLowerCase(),o=t.expectedOrigin,s=Number.isFinite(t.since)?Math.floor(t.since):Math.floor(Date.now()/1e3)-60;return new Promise((i,c)=>{let f=`sa-${Math.random().toString(36).slice(2,12)}`,a=!1,u,d,g=Date.now()+r,l=C=>{if(!a){a=!0,clearTimeout(x),clearTimeout(d),typeof document<"u"&&document.removeEventListener("visibilitychange",v);try{u?.close()}catch{}C()}},x=setTimeout(()=>{l(()=>c(new Error("timeout")))},r),A=()=>{!a&&u?.readyState===1&&u.send(JSON.stringify(["REQ",f,{kinds:[1059],"#p":[e],since:s}]))},v=()=>{if(!(a||document.visibilityState!=="visible")){if(Date.now()>=g)return l(()=>c(new Error("timeout")));!u||u.readyState>=2?V():A()}};typeof document<"u"&&document.addEventListener("visibilitychange",v);let L=async C=>{if(a)return;let F;try{F=JSON.parse(typeof C.data=="string"?C.data:"")}catch{return}if(!Array.isArray(F)||F[0]!=="EVENT"||F[1]!==f)return;let P=F[2];if(typeof P!="object"||P===null)return;let Y=P;if(Y.kind!==1059||typeof Y.pubkey!="string"||typeof Y.content!="string")return;let G=await Fi({pubkey:Y.pubkey,content:Y.content},t.sessionPrivKey);if(!G||G.kind!==29999)return;let j=G.tags.find(y=>y[0]==="session");if(!j||j[1]!==n)return;let z=G.tags.find(y=>y[0]==="status");if(z?.[1]==="rejected"){l(()=>c(new Error("denied")));return}if(z?.[1]!=="approved"||Math.abs(Date.now()/1e3-G.created_at)>300)return;let _;try{let y=JSON.parse(G.content);if(typeof y!="object"||y===null)return;_=y}catch{return}if(_.type!=="signet-auth-response"||_.requestId!==n||typeof _.authEvent!="object"||_.authEvent===null)return;let O=_.authEvent;if(typeof O.id!="string"||!/^[0-9a-f]{64}$/i.test(O.id)||typeof O.pubkey!="string"||!/^[0-9a-f]{64}$/i.test(O.pubkey)||typeof O.sig!="string"||!/^[0-9a-f]{128}$/i.test(O.sig)||O.kind!==21236||typeof O.created_at!="number"||!Array.isArray(O.tags)||typeof O.content!="string"||O.pubkey.toLowerCase()!==G.pubkey.toLowerCase()||Jn({pubkey:O.pubkey,created_at:O.created_at,kind:21236,tags:O.tags,content:O.content})!==O.id.toLowerCase())return;let h=!1;try{let y=Ae(O.sig),U=Ae(O.id),k=Ae(O.pubkey);h=ft.verify(y,U,k)}catch{h=!1}if(!h)return;let B=O.tags,S=B.find(y=>Array.isArray(y)&&y[0]==="challenge");if(!S||typeof S[1]!="string"||S[1].toLowerCase()!==n)return;let I=B.find(y=>Array.isArray(y)&&y[0]==="origin");if(!I||I[1]!==o||Math.abs(Date.now()/1e3-O.created_at)>300)return;let R={id:O.id.toLowerCase(),pubkey:O.pubkey.toLowerCase(),kind:21236,created_at:O.created_at,tags:B,content:O.content,sig:O.sig},m=Ki(_.displayName),p=typeof _.bunkerUri=="string"&&/^bunker:\/\//i.test(_.bunkerUri)?_.bunkerUri:void 0;l(()=>i({pubkey:R.pubkey,authEvent:R,credential:_.credential,...m!==void 0?{displayName:m}:{},...p!==void 0?{bunkerUri:p}:{},createdAt:R.created_at}))},q=C=>{if(!a){if(typeof document>"u")return l(()=>c(new Error(C)));clearTimeout(d),document.visibilityState==="visible"&&(d=setTimeout(V,1e3))}};function V(){if(a)return;if(clearTimeout(d),Date.now()>=g)return l(()=>c(new Error("timeout")));let C=u;u=void 0;try{C?.close()}catch{}try{let F=new WebSocket(t.relayUrl);u=F,F.onopen=A,F.onmessage=L,F.onerror=()=>{u===F&&q("relay-error")},F.onclose=()=>{u===F&&q("relay-closed")}}catch{q("relay-error")}}V()})}typeof window<"u"&&(window.Signet={verifyAge:Wo,waitForAuthResponse:Xo});return os(zi);})();
8
8
  /*! Bundled license information:
9
9
 
10
- @noble/hashes/esm/utils.js:
11
- @noble/hashes/utils.js:
12
- (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
13
-
14
- @noble/curves/esm/utils.js:
15
- @noble/curves/esm/abstract/modular.js:
16
- @noble/curves/esm/abstract/curve.js:
17
- @noble/curves/esm/abstract/weierstrass.js:
18
- @noble/curves/esm/_shortw_utils.js:
19
- @noble/curves/esm/secp256k1.js:
10
+ @noble/curves/utils.js:
11
+ @noble/curves/abstract/modular.js:
12
+ @noble/curves/abstract/curve.js:
13
+ @noble/curves/abstract/weierstrass.js:
14
+ @noble/curves/secp256k1.js:
20
15
  @noble/curves/utils.js:
21
16
  @noble/curves/abstract/modular.js:
22
17
  @noble/curves/abstract/curve.js:
@@ -27,6 +22,9 @@
27
22
  @noble/ciphers/utils.js:
28
23
  (*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
29
24
 
25
+ @noble/hashes/utils.js:
26
+ (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
27
+
30
28
  @scure/base/index.js:
31
29
  (*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
32
30
  */
@@ -27,9 +27,9 @@ function getTagValue(tags, key) {
27
27
  return tag ? tag[1] : undefined;
28
28
  }
29
29
  // BIP-340 Schnorr signature verification via @noble/curves (bundled by esbuild)
30
- import { schnorr } from '@noble/curves/secp256k1';
31
- import { sha256 } from '@noble/hashes/sha256';
32
- import { bytesToHex } from '@noble/hashes/utils';
30
+ import { schnorr } from '@noble/curves/secp256k1.js';
31
+ import { sha256 } from '@noble/hashes/sha2.js';
32
+ import { bytesToHex } from '@noble/hashes/utils.js';
33
33
  // NIP-44 v2 decrypt for NIP-17 gift-wrap unwrapping (cross-device auth flow)
34
34
  import { decrypt as nip44Decrypt, getConversationKey } from 'nostr-tools/nip44';
35
35
  function hexToBytes(hex) {
@@ -418,24 +418,29 @@ export async function waitForAuthResponse(options) {
418
418
  const timeout = Math.max(5000, Math.min(options.timeout ?? 120000, 600000));
419
419
  const requestIdLower = options.requestId.toLowerCase();
420
420
  const expectedOrigin = options.expectedOrigin;
421
+ // Anchor the query before Android can suspend this page while opening its signer.
422
+ // A caller restarting a wait for a sign-in already issued must anchor this to
423
+ // when that sign-in started, or the fresh window skips past a response the
424
+ // relay is already holding — see the `since` option.
425
+ const since = Number.isFinite(options.since)
426
+ ? Math.floor(options.since)
427
+ : Math.floor(Date.now() / 1000) - 60;
421
428
  return new Promise((resolve, reject) => {
422
429
  const subId = `sa-${Math.random().toString(36).slice(2, 12)}`;
423
430
  let settled = false;
424
431
  let ws;
425
- try {
426
- ws = new WebSocket(options.relayUrl);
427
- }
428
- catch {
429
- reject(new Error('relay-error'));
430
- return;
431
- }
432
+ let retryTimer;
433
+ const deadline = Date.now() + timeout;
432
434
  const settle = (action) => {
433
435
  if (settled)
434
436
  return;
435
437
  settled = true;
436
438
  clearTimeout(timer);
439
+ clearTimeout(retryTimer);
440
+ if (typeof document !== 'undefined')
441
+ document.removeEventListener('visibilitychange', onVisible);
437
442
  try {
438
- ws.close();
443
+ ws?.close();
439
444
  }
440
445
  catch { /* ignore */ }
441
446
  action();
@@ -443,12 +448,28 @@ export async function waitForAuthResponse(options) {
443
448
  const timer = setTimeout(() => {
444
449
  settle(() => reject(new Error('timeout')));
445
450
  }, timeout);
446
- ws.onopen = () => {
447
- // Subscribe slightly in the past so a racing publish isn't missed.
448
- const since = Math.floor(Date.now() / 1000) - 60;
449
- ws.send(JSON.stringify(['REQ', subId, { kinds: [1059], '#p': [sessionPubkey], since }]));
451
+ const subscribe = () => {
452
+ // `since` comes from the enclosing scope so every (re)connection asks for
453
+ // the same window — see the `since` option.
454
+ if (!settled && ws?.readyState === 1) {
455
+ ws.send(JSON.stringify(['REQ', subId, { kinds: [1059], '#p': [sessionPubkey], since }]));
456
+ }
457
+ };
458
+ const onVisible = () => {
459
+ // Replay the stored response after switching back from a native signer.
460
+ // The original challenge, origin, signatures and freshness checks still apply.
461
+ if (settled || document.visibilityState !== 'visible')
462
+ return;
463
+ if (Date.now() >= deadline)
464
+ return settle(() => reject(new Error('timeout')));
465
+ if (!ws || ws.readyState >= 2)
466
+ connect();
467
+ else
468
+ subscribe();
450
469
  };
451
- ws.onmessage = async (msgEvent) => {
470
+ if (typeof document !== 'undefined')
471
+ document.addEventListener('visibilitychange', onVisible);
472
+ const onMessage = async (msgEvent) => {
452
473
  if (settled)
453
474
  return;
454
475
  let msg;
@@ -583,9 +604,44 @@ export async function waitForAuthResponse(options) {
583
604
  createdAt: verifiedAuthEvent.created_at,
584
605
  }));
585
606
  };
586
- ws.onerror = () => {
587
- settle(() => reject(new Error('relay-error')));
607
+ const retryOrFail = (reason) => {
608
+ if (settled)
609
+ return;
610
+ if (typeof document === 'undefined')
611
+ return settle(() => reject(new Error(reason)));
612
+ clearTimeout(retryTimer);
613
+ // Android may close a background tab's socket. Retry on return, using
614
+ // the same session and original filter, within the original timeout.
615
+ if (document.visibilityState === 'visible')
616
+ retryTimer = setTimeout(connect, 1000);
588
617
  };
618
+ function connect() {
619
+ if (settled)
620
+ return;
621
+ clearTimeout(retryTimer);
622
+ if (Date.now() >= deadline)
623
+ return settle(() => reject(new Error('timeout')));
624
+ const previous = ws;
625
+ ws = undefined;
626
+ try {
627
+ previous?.close();
628
+ }
629
+ catch { /* already closed */ }
630
+ try {
631
+ const socket = new WebSocket(options.relayUrl);
632
+ ws = socket;
633
+ socket.onopen = subscribe;
634
+ socket.onmessage = onMessage;
635
+ socket.onerror = () => { if (ws === socket)
636
+ retryOrFail('relay-error'); };
637
+ socket.onclose = () => { if (ws === socket)
638
+ retryOrFail('relay-closed'); };
639
+ }
640
+ catch {
641
+ retryOrFail('relay-error');
642
+ }
643
+ }
644
+ connect();
589
645
  });
590
646
  }
591
647
  // Auto-attach to window for script-tag usage
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signet-verify",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Signet SDK — website age verification and cross-device Sign-in-with-Signet",
5
5
  "type": "module",
6
6
  "main": "./dist/signet-verify.js",
@@ -27,15 +27,15 @@
27
27
  "test": "vitest run"
28
28
  },
29
29
  "dependencies": {
30
- "@noble/curves": "^1.8.1",
31
- "@noble/hashes": "^1.7.1",
30
+ "@noble/curves": "^2.4.0",
31
+ "@noble/hashes": "^2.4.0",
32
32
  "nostr-tools": "^2.23.3"
33
33
  },
34
34
  "devDependencies": {
35
35
  "esbuild": "^0.28.0",
36
36
  "signet-protocol": "^1.6.0",
37
37
  "typescript": "^5.7.0",
38
- "vitest": "^3.2.4"
38
+ "vitest": "^5.0.0"
39
39
  },
40
40
  "license": "MIT",
41
41
  "repository": {