@muisit/cryptokey
Advanced tools
+2
-2
@@ -1,2 +0,2 @@ | ||
| function Pt(r){return globalThis.Buffer!=null?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):r}function Xt(r=0){return globalThis.Buffer!=null&&globalThis.Buffer.allocUnsafe!=null?Pt(globalThis.Buffer.allocUnsafe(r)):new Uint8Array(r)}import{bases as Qe}from"multiformats/basics";function tr(r,t,e,n){return{name:r,prefix:t,encoder:{name:r,prefix:t,encode:e},decoder:{decode:n}}}var $e=tr("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),me=tr("ascii","a",r=>{let t="a";for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return t},r=>{r=r.substring(1);let t=Xt(r.length);for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}),Yr={utf8:$e,"utf-8":$e,hex:Qe.base16,latin1:me,ascii:me,binary:me,...Qe},Qt=Yr;function Bt(r,t="utf8"){let e=Qt[t];if(!e)throw new Error(`Unsupported encoding "${t}"`);return(t==="utf8"||t==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?Pt(globalThis.Buffer.from(r,"utf-8")):e.decoder.decode(`${e.prefix}${r}`)}function Y(r,t="utf8"){let e=Qt[t];if(!e)throw new Error(`Unsupported encoding "${t}"`);return(t==="utf8"||t==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?globalThis.Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("utf8"):e.encoder.encode(r).substring(1)}var O=class r{keyType;privateKeyBytes;publicKeyBytes;constructor(){this.keyType="none",this.privateKeyBytes=null,this.publicKeyBytes=null}async sign(t,e,n="raw"){let o=await this.signBytes(t,e);switch(n){default:case"raw":break;case"base16":case"hex":return Y(o,"base16");case"base58btc":return Y(o,"base58btc");case"base64":return Y(o,"base64");case"base64url":return Y(o,"base64url")}throw new Error("unable to encode resulting signature using "+n)}async initialisePrivateKey(t){this.privateKeyBytes=t}static bytesToHex(t){return Y(t,"base16")}static hexToBytes(t){let e=t.startsWith("0x")?t.substring(2):t;return e.length%2!==0&&(e=`0${e}`),Bt(e,"base16")}static bytesToBase64(t,e=!1){let n=Y(t,"base64");if(e){let o=(4-n.length%4)%4;return n+"=".repeat(o)}return n}static base64ToBytes(t){return Bt(t,"base64")}static bytesToBase64Url(t){return Y(t,"base64url")}static base64UrlToBytes(t){return Bt(t,"base64url")}hasPublicKey(){return this.publicKeyBytes!==null&&this.publicKeyBytes.length>0}publicKey(){return this.publicKeyBytes??new Uint8Array}exportPublicKey(){return r.bytesToHex(this.publicKey())}setPublicKey(t){this.publicKeyBytes=r.hexToBytes(t)}hasPrivateKey(){return this.privateKeyBytes!==null&&this.privateKeyBytes.length>0}privateKey(){return this.privateKeyBytes??new Uint8Array}exportPrivateKey(){return r.bytesToHex(this.privateKey())}setPrivateKey(t){this.privateKeyBytes=r.hexToBytes(t)}};function er(r){if(!Number.isSafeInteger(r)||r<0)throw new Error("positive integer expected, got "+r)}function jr(r){return r instanceof Uint8Array||ArrayBuffer.isView(r)&&r.constructor.name==="Uint8Array"}function _t(r,...t){if(!jr(r))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(r.length))throw new Error("Uint8Array expected of length "+t+", got length="+r.length)}function rr(r){if(typeof r!="function"||typeof r.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");er(r.outputLen),er(r.blockLen)}function Lt(r,t=!0){if(r.destroyed)throw new Error("Hash instance has been destroyed");if(t&&r.finished)throw new Error("Hash#digest() has already been called")}function nr(r,t){_t(r);let e=t.outputLen;if(r.length<e)throw new Error("digestInto() expects output buffer of length at least "+e)}import*as mt from"node:crypto";var Ut=mt&&typeof mt=="object"&&"webcrypto"in mt?mt.webcrypto:mt&&typeof mt=="object"&&"randomBytes"in mt?mt:void 0;function $t(r){return new DataView(r.buffer,r.byteOffset,r.byteLength)}function ft(r,t){return r<<32-t|r>>>t}var Zo=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function";function or(r){if(typeof r!="string")throw new Error("utf8ToBytes expected string, got "+typeof r);return new Uint8Array(new TextEncoder().encode(r))}function Ft(r){return typeof r=="string"&&(r=or(r)),_t(r),r}function be(...r){let t=0;for(let n=0;n<r.length;n++){let o=r[n];_t(o),t+=o.length}let e=new Uint8Array(t);for(let n=0,o=0;n<r.length;n++){let s=r[n];e.set(s,o),o+=s.length}return e}var Ot=class{clone(){return this._cloneInto()}};function te(r){let t=n=>r().update(Ft(n)).digest(),e=r();return t.outputLen=e.outputLen,t.blockLen=e.blockLen,t.create=()=>r(),t}function Jt(r=32){if(Ut&&typeof Ut.getRandomValues=="function")return Ut.getRandomValues(new Uint8Array(r));if(Ut&&typeof Ut.randomBytes=="function")return Uint8Array.from(Ut.randomBytes(r));throw new Error("crypto.getRandomValues must be defined")}function Xr(r,t,e,n){if(typeof r.setBigUint64=="function")return r.setBigUint64(t,e,n);let o=BigInt(32),s=BigInt(4294967295),i=Number(e>>o&s),a=Number(e&s),c=n?4:0,h=n?0:4;r.setUint32(t+c,i,n),r.setUint32(t+h,a,n)}function sr(r,t,e){return r&t^~r&e}function ir(r,t,e){return r&t^r&e^t&e}var Nt=class extends Ot{constructor(t,e,n,o){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=t,this.outputLen=e,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(t),this.view=$t(this.buffer)}update(t){Lt(this);let{view:e,buffer:n,blockLen:o}=this;t=Ft(t);let s=t.length;for(let i=0;i<s;){let a=Math.min(o-this.pos,s-i);if(a===o){let c=$t(t);for(;o<=s-i;i+=o)this.process(c,i);continue}n.set(t.subarray(i,i+a),this.pos),this.pos+=a,i+=a,this.pos===o&&(this.process(e,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){Lt(this),nr(t,this),this.finished=!0;let{buffer:e,view:n,blockLen:o,isLE:s}=this,{pos:i}=this;e[i++]=128,this.buffer.subarray(i).fill(0),this.padOffset>o-i&&(this.process(n,0),i=0);for(let p=i;p<o;p++)e[p]=0;Xr(n,o-8,BigInt(this.length*8),s),this.process(n,0);let a=$t(t),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let h=c/4,f=this.get();if(h>f.length)throw new Error("_sha2: outputLen bigger than state");for(let p=0;p<h;p++)a.setUint32(4*p,f[p],s)}digest(){let{buffer:t,outputLen:e}=this;this.digestInto(t);let n=t.slice(0,e);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:e,buffer:n,length:o,finished:s,destroyed:i,pos:a}=this;return t.length=o,t.pos=a,t.finished=s,t.destroyed=i,o%e&&t.buffer.set(n),t}};var Qr=new Uint32Array([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]),Et=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),vt=new Uint32Array(64),xe=class extends Nt{constructor(t=32){super(64,t,8,!1),this.A=Et[0]|0,this.B=Et[1]|0,this.C=Et[2]|0,this.D=Et[3]|0,this.E=Et[4]|0,this.F=Et[5]|0,this.G=Et[6]|0,this.H=Et[7]|0}get(){let{A:t,B:e,C:n,D:o,E:s,F:i,G:a,H:c}=this;return[t,e,n,o,s,i,a,c]}set(t,e,n,o,s,i,a,c){this.A=t|0,this.B=e|0,this.C=n|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=a|0,this.H=c|0}process(t,e){for(let p=0;p<16;p++,e+=4)vt[p]=t.getUint32(e,!1);for(let p=16;p<64;p++){let y=vt[p-15],A=vt[p-2],k=ft(y,7)^ft(y,18)^y>>>3,g=ft(A,17)^ft(A,19)^A>>>10;vt[p]=g+vt[p-7]+k+vt[p-16]|0}let{A:n,B:o,C:s,D:i,E:a,F:c,G:h,H:f}=this;for(let p=0;p<64;p++){let y=ft(a,6)^ft(a,11)^ft(a,25),A=f+y+sr(a,c,h)+Qr[p]+vt[p]|0,g=(ft(n,2)^ft(n,13)^ft(n,22))+ir(n,o,s)|0;f=h,h=c,c=a,a=i+A|0,i=s,s=o,o=n,n=A+g|0}n=n+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,a=a+this.E|0,c=c+this.F|0,h=h+this.G|0,f=f+this.H|0,this.set(n,o,s,i,a,c,h,f)}roundClean(){vt.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}};var ut=te(()=>new xe);var ee=BigInt(4294967295),ge=BigInt(32);function cr(r,t=!1){return t?{h:Number(r&ee),l:Number(r>>ge&ee)}:{h:Number(r>>ge&ee)|0,l:Number(r&ee)|0}}function $r(r,t=!1){let e=new Uint32Array(r.length),n=new Uint32Array(r.length);for(let o=0;o<r.length;o++){let{h:s,l:i}=cr(r[o],t);[e[o],n[o]]=[s,i]}return[e,n]}var tn=(r,t)=>BigInt(r>>>0)<<ge|BigInt(t>>>0),en=(r,t,e)=>r>>>e,rn=(r,t,e)=>r<<32-e|t>>>e,nn=(r,t,e)=>r>>>e|t<<32-e,on=(r,t,e)=>r<<32-e|t>>>e,sn=(r,t,e)=>r<<64-e|t>>>e-32,cn=(r,t,e)=>r>>>e-32|t<<64-e,an=(r,t)=>t,fn=(r,t)=>r,un=(r,t,e)=>r<<e|t>>>32-e,ln=(r,t,e)=>t<<e|r>>>32-e,dn=(r,t,e)=>t<<e-32|r>>>64-e,hn=(r,t,e)=>r<<e-32|t>>>64-e;function yn(r,t,e,n){let o=(t>>>0)+(n>>>0);return{h:r+e+(o/2**32|0)|0,l:o|0}}var pn=(r,t,e)=>(r>>>0)+(t>>>0)+(e>>>0),mn=(r,t,e,n)=>t+e+n+(r/2**32|0)|0,bn=(r,t,e,n)=>(r>>>0)+(t>>>0)+(e>>>0)+(n>>>0),xn=(r,t,e,n,o)=>t+e+n+o+(r/2**32|0)|0,gn=(r,t,e,n,o)=>(r>>>0)+(t>>>0)+(e>>>0)+(n>>>0)+(o>>>0),wn=(r,t,e,n,o,s)=>t+e+n+o+s+(r/2**32|0)|0;var Bn={fromBig:cr,split:$r,toBig:tn,shrSH:en,shrSL:rn,rotrSH:nn,rotrSL:on,rotrBH:sn,rotrBL:cn,rotr32H:an,rotr32L:fn,rotlSH:un,rotlSL:ln,rotlBH:dn,rotlBL:hn,add:yn,add3L:pn,add3H:mn,add4L:bn,add4H:xn,add5H:wn,add5L:gn},_=Bn;var[En,vn]=_.split(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(r=>BigInt(r))),Kt=new Uint32Array(80),St=new Uint32Array(80),we=class extends Nt{constructor(t=64){super(128,t,16,!1),this.Ah=1779033703,this.Al=-205731576,this.Bh=-1150833019,this.Bl=-2067093701,this.Ch=1013904242,this.Cl=-23791573,this.Dh=-1521486534,this.Dl=1595750129,this.Eh=1359893119,this.El=-1377402159,this.Fh=-1694144372,this.Fl=725511199,this.Gh=528734635,this.Gl=-79577749,this.Hh=1541459225,this.Hl=327033209}get(){let{Ah:t,Al:e,Bh:n,Bl:o,Ch:s,Cl:i,Dh:a,Dl:c,Eh:h,El:f,Fh:p,Fl:y,Gh:A,Gl:k,Hh:g,Hl:d}=this;return[t,e,n,o,s,i,a,c,h,f,p,y,A,k,g,d]}set(t,e,n,o,s,i,a,c,h,f,p,y,A,k,g,d){this.Ah=t|0,this.Al=e|0,this.Bh=n|0,this.Bl=o|0,this.Ch=s|0,this.Cl=i|0,this.Dh=a|0,this.Dl=c|0,this.Eh=h|0,this.El=f|0,this.Fh=p|0,this.Fl=y|0,this.Gh=A|0,this.Gl=k|0,this.Hh=g|0,this.Hl=d|0}process(t,e){for(let l=0;l<16;l++,e+=4)Kt[l]=t.getUint32(e),St[l]=t.getUint32(e+=4);for(let l=16;l<80;l++){let K=Kt[l-15]|0,H=St[l-15]|0,C=_.rotrSH(K,H,1)^_.rotrSH(K,H,8)^_.shrSH(K,H,7),I=_.rotrSL(K,H,1)^_.rotrSL(K,H,8)^_.shrSL(K,H,7),U=Kt[l-2]|0,S=St[l-2]|0,M=_.rotrSH(U,S,19)^_.rotrBH(U,S,61)^_.shrSH(U,S,6),D=_.rotrSL(U,S,19)^_.rotrBL(U,S,61)^_.shrSL(U,S,6),L=_.add4L(I,D,St[l-7],St[l-16]),G=_.add4H(L,C,M,Kt[l-7],Kt[l-16]);Kt[l]=G|0,St[l]=L|0}let{Ah:n,Al:o,Bh:s,Bl:i,Ch:a,Cl:c,Dh:h,Dl:f,Eh:p,El:y,Fh:A,Fl:k,Gh:g,Gl:d,Hh:B,Hl:x}=this;for(let l=0;l<80;l++){let K=_.rotrSH(p,y,14)^_.rotrSH(p,y,18)^_.rotrBH(p,y,41),H=_.rotrSL(p,y,14)^_.rotrSL(p,y,18)^_.rotrBL(p,y,41),C=p&A^~p&g,I=y&k^~y&d,U=_.add5L(x,H,I,vn[l],St[l]),S=_.add5H(U,B,K,C,En[l],Kt[l]),M=U|0,D=_.rotrSH(n,o,28)^_.rotrBH(n,o,34)^_.rotrBH(n,o,39),L=_.rotrSL(n,o,28)^_.rotrBL(n,o,34)^_.rotrBL(n,o,39),G=n&s^n&a^s&a,w=o&i^o&c^i&c;B=g|0,x=d|0,g=A|0,d=k|0,A=p|0,k=y|0,{h:p,l:y}=_.add(h|0,f|0,S|0,M|0),h=a|0,f=c|0,a=s|0,c=i|0,s=n|0,i=o|0;let E=_.add3L(M,L,w);n=_.add3H(E,S,D,G),o=E|0}({h:n,l:o}=_.add(this.Ah|0,this.Al|0,n|0,o|0)),{h:s,l:i}=_.add(this.Bh|0,this.Bl|0,s|0,i|0),{h:a,l:c}=_.add(this.Ch|0,this.Cl|0,a|0,c|0),{h,l:f}=_.add(this.Dh|0,this.Dl|0,h|0,f|0),{h:p,l:y}=_.add(this.Eh|0,this.El|0,p|0,y|0),{h:A,l:k}=_.add(this.Fh|0,this.Fl|0,A|0,k|0),{h:g,l:d}=_.add(this.Gh|0,this.Gl|0,g|0,d|0),{h:B,l:x}=_.add(this.Hh|0,this.Hl|0,B|0,x|0),this.set(n,o,s,i,a,c,h,f,p,y,A,k,g,d,B,x)}roundClean(){Kt.fill(0),St.fill(0)}destroy(){this.buffer.fill(0),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}};var Be=te(()=>new we);var Se=BigInt(0),Ke=BigInt(1);function Ct(r){return r instanceof Uint8Array||ArrayBuffer.isView(r)&&r.constructor.name==="Uint8Array"}function Ae(r){if(!Ct(r))throw new Error("Uint8Array expected")}function dt(r,t){if(typeof t!="boolean")throw new Error(r+" boolean expected, got "+t)}function zt(r){let t=r.toString(16);return t.length&1?"0"+t:t}function ur(r){if(typeof r!="string")throw new Error("hex string expected, got "+typeof r);return r===""?Se:BigInt("0x"+r)}var lr=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",Kn=Array.from({length:256},(r,t)=>t.toString(16).padStart(2,"0"));function At(r){if(Ae(r),lr)return r.toHex();let t="";for(let e=0;e<r.length;e++)t+=Kn[r[e]];return t}var bt={_0:48,_9:57,A:65,F:70,a:97,f:102};function ar(r){if(r>=bt._0&&r<=bt._9)return r-bt._0;if(r>=bt.A&&r<=bt.F)return r-(bt.A-10);if(r>=bt.a&&r<=bt.f)return r-(bt.a-10)}function Gt(r){if(typeof r!="string")throw new Error("hex string expected, got "+typeof r);if(lr)return Uint8Array.fromHex(r);let t=r.length,e=t/2;if(t%2)throw new Error("hex string expected, got unpadded hex of length "+t);let n=new Uint8Array(e);for(let o=0,s=0;o<e;o++,s+=2){let i=ar(r.charCodeAt(s)),a=ar(r.charCodeAt(s+1));if(i===void 0||a===void 0){let c=r[s]+r[s+1];throw new Error('hex string expected, got non-hex character "'+c+'" at index '+s)}n[o]=i*16+a}return n}function xt(r){return ur(At(r))}function ht(r){return Ae(r),ur(At(Uint8Array.from(r).reverse()))}function Rt(r,t){return Gt(r.toString(16).padStart(t*2,"0"))}function Tt(r,t){return Rt(r,t).reverse()}function F(r,t,e){let n;if(typeof t=="string")try{n=Gt(t)}catch(s){throw new Error(r+" must be hex string or Uint8Array, cause: "+s)}else if(Ct(t))n=Uint8Array.from(t);else throw new Error(r+" must be hex string or Uint8Array");let o=n.length;if(typeof e=="number"&&o!==e)throw new Error(r+" of length "+e+" expected, got "+o);return n}function It(...r){let t=0;for(let n=0;n<r.length;n++){let o=r[n];Ae(o),t+=o.length}let e=new Uint8Array(t);for(let n=0,o=0;n<r.length;n++){let s=r[n];e.set(s,o),o+=s.length}return e}var Ee=r=>typeof r=="bigint"&&Se<=r;function re(r,t,e){return Ee(r)&&Ee(t)&&Ee(e)&&t<=r&&r<e}function ot(r,t,e,n){if(!re(t,e,n))throw new Error("expected valid "+r+": "+e+" <= n < "+n+", got "+t)}function dr(r){let t;for(t=0;r>Se;r>>=Ke,t+=1);return t}var Ht=r=>(Ke<<BigInt(r))-Ke,ve=r=>new Uint8Array(r),fr=r=>Uint8Array.from(r);function hr(r,t,e){if(typeof r!="number"||r<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 e!="function")throw new Error("hmacFn must be a function");let n=ve(r),o=ve(r),s=0,i=()=>{n.fill(1),o.fill(0),s=0},a=(...p)=>e(o,n,...p),c=(p=ve(0))=>{o=a(fr([0]),p),n=a(),p.length!==0&&(o=a(fr([1]),p),n=a())},h=()=>{if(s++>=1e3)throw new Error("drbg: tried 1000 values");let p=0,y=[];for(;p<t;){n=a();let A=n.slice();y.push(A),p+=n.length}return It(...y)};return(p,y)=>{i(),c(p);let A;for(;!(A=y(h()));)c();return i(),A}}var Sn={bigint:r=>typeof r=="bigint",function:r=>typeof r=="function",boolean:r=>typeof r=="boolean",string:r=>typeof r=="string",stringOrUint8Array:r=>typeof r=="string"||Ct(r),isSafeInteger:r=>Number.isSafeInteger(r),array:r=>Array.isArray(r),field:(r,t)=>t.Fp.isValid(r),hash:r=>typeof r=="function"&&Number.isSafeInteger(r.outputLen)};function lt(r,t,e={}){let n=(o,s,i)=>{let a=Sn[s];if(typeof a!="function")throw new Error("invalid validator function");let c=r[o];if(!(i&&c===void 0)&&!a(c,r))throw new Error("param "+String(o)+" is invalid. Expected "+s+", got "+c)};for(let[o,s]of Object.entries(t))n(o,s,!1);for(let[o,s]of Object.entries(e))n(o,s,!0);return r}function Wt(r){let t=new WeakMap;return(e,...n)=>{let o=t.get(e);if(o!==void 0)return o;let s=r(e,...n);return t.set(e,s),s}}var $=BigInt(0),z=BigInt(1),kt=BigInt(2),An=BigInt(3),Te=BigInt(4),yr=BigInt(5),pr=BigInt(8),Tn=BigInt(9),In=BigInt(16);function V(r,t){let e=r%t;return e>=$?e:t+e}function Ie(r,t,e){if(t<$)throw new Error("invalid exponent, negatives unsupported");if(e<=$)throw new Error("invalid modulus");if(e===z)return $;let n=z;for(;t>$;)t&z&&(n=n*r%e),r=r*r%e,t>>=z;return n}function J(r,t,e){let n=r;for(;t-- >$;)n*=n,n%=e;return n}function ne(r,t){if(r===$)throw new Error("invert: expected non-zero number");if(t<=$)throw new Error("invert: expected positive modulus, got "+t);let e=V(r,t),n=t,o=$,s=z,i=z,a=$;for(;e!==$;){let h=n/e,f=n%e,p=o-i*h,y=s-a*h;n=e,e=f,o=i,s=a,i=p,a=y}if(n!==z)throw new Error("invert: does not exist");return V(o,t)}function Un(r){let t=(r-z)/kt,e,n,o;for(e=r-z,n=0;e%kt===$;e/=kt,n++);for(o=kt;o<r&&Ie(o,t,r)!==r-z;o++)if(o>1e3)throw new Error("Cannot find square root: likely non-prime P");if(n===1){let i=(r+z)/Te;return function(c,h){let f=c.pow(h,i);if(!c.eql(c.sqr(f),h))throw new Error("Cannot find square root");return f}}let s=(e+z)/kt;return function(a,c){if(a.pow(c,t)===a.neg(a.ONE))throw new Error("Cannot find square root");let h=n,f=a.pow(a.mul(a.ONE,o),e),p=a.pow(c,s),y=a.pow(c,e);for(;!a.eql(y,a.ONE);){if(a.eql(y,a.ZERO))return a.ZERO;let A=1;for(let g=a.sqr(y);A<h&&!a.eql(g,a.ONE);A++)g=a.sqr(g);let k=a.pow(f,z<<BigInt(h-A-1));f=a.sqr(k),p=a.mul(p,k),y=a.mul(y,f),h=A}return p}}function Cn(r){if(r%Te===An){let t=(r+z)/Te;return function(n,o){let s=n.pow(o,t);if(!n.eql(n.sqr(s),o))throw new Error("Cannot find square root");return s}}if(r%pr===yr){let t=(r-yr)/pr;return function(n,o){let s=n.mul(o,kt),i=n.pow(s,t),a=n.mul(o,i),c=n.mul(n.mul(a,kt),i),h=n.mul(a,n.sub(c,n.ONE));if(!n.eql(n.sqr(h),o))throw new Error("Cannot find square root");return h}}return r%In,Un(r)}var mr=(r,t)=>(V(r,t)&z)===z,Rn=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Ue(r){let t={ORDER:"bigint",MASK:"bigint",BYTES:"isSafeInteger",BITS:"isSafeInteger"},e=Rn.reduce((n,o)=>(n[o]="function",n),t);return lt(r,e)}function Hn(r,t,e){if(e<$)throw new Error("invalid exponent, negatives unsupported");if(e===$)return r.ONE;if(e===z)return t;let n=r.ONE,o=t;for(;e>$;)e&z&&(n=r.mul(n,o)),o=r.sqr(o),e>>=z;return n}function kn(r,t){let e=new Array(t.length),n=t.reduce((s,i,a)=>r.is0(i)?s:(e[a]=s,r.mul(s,i)),r.ONE),o=r.inv(n);return t.reduceRight((s,i,a)=>r.is0(i)?s:(e[a]=r.mul(s,e[a]),r.mul(s,i)),o),e}function Ce(r,t){let e=t!==void 0?t:r.toString(2).length,n=Math.ceil(e/8);return{nBitLength:e,nByteLength:n}}function yt(r,t,e=!1,n={}){if(r<=$)throw new Error("invalid field: expected ORDER > 0, got "+r);let{nBitLength:o,nByteLength:s}=Ce(r,t);if(s>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");let i,a=Object.freeze({ORDER:r,isLE:e,BITS:o,BYTES:s,MASK:Ht(o),ZERO:$,ONE:z,create:c=>V(c,r),isValid:c=>{if(typeof c!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof c);return $<=c&&c<r},is0:c=>c===$,isOdd:c=>(c&z)===z,neg:c=>V(-c,r),eql:(c,h)=>c===h,sqr:c=>V(c*c,r),add:(c,h)=>V(c+h,r),sub:(c,h)=>V(c-h,r),mul:(c,h)=>V(c*h,r),pow:(c,h)=>Hn(a,c,h),div:(c,h)=>V(c*ne(h,r),r),sqrN:c=>c*c,addN:(c,h)=>c+h,subN:(c,h)=>c-h,mulN:(c,h)=>c*h,inv:c=>ne(c,r),sqrt:n.sqrt||(c=>(i||(i=Cn(r)),i(a,c))),invertBatch:c=>kn(a,c),cmov:(c,h,f)=>f?h:c,toBytes:c=>e?Tt(c,s):Rt(c,s),fromBytes:c=>{if(c.length!==s)throw new Error("Field.fromBytes: expected "+s+" bytes, got "+c.length);return e?ht(c):xt(c)}});return Object.freeze(a)}function br(r){if(typeof r!="bigint")throw new Error("field order must be bigint");let t=r.toString(2).length;return Math.ceil(t/8)}function Re(r){let t=br(r);return t+Math.ceil(t/2)}function xr(r,t,e=!1){let n=r.length,o=br(t),s=Re(t);if(n<16||n<s||n>1024)throw new Error("expected "+s+"-1024 bytes of input, got "+n);let i=e?ht(r):xt(r),a=V(i,t-z)+z;return e?Tt(a,o):Rt(a,o)}var gr=BigInt(0),_e=BigInt(1);function He(r,t){let e=t.negate();return r?e:t}function Br(r,t){if(!Number.isSafeInteger(r)||r<=0||r>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+r)}function ke(r,t){Br(r,t);let e=Math.ceil(t/r)+1,n=2**(r-1),o=2**r,s=Ht(r),i=BigInt(r);return{windows:e,windowSize:n,mask:s,maxNumber:o,shiftBy:i}}function wr(r,t,e){let{windowSize:n,mask:o,maxNumber:s,shiftBy:i}=e,a=Number(r&o),c=r>>i;a>n&&(a-=s,c+=_e);let h=t*n,f=h+Math.abs(a)-1,p=a===0,y=a<0,A=t%2!==0;return{nextN:c,offset:f,isZero:p,isNeg:y,isNegF:A,offsetF:h}}function Dn(r,t){if(!Array.isArray(r))throw new Error("array expected");r.forEach((e,n)=>{if(!(e instanceof t))throw new Error("invalid point at index "+n)})}function Pn(r,t){if(!Array.isArray(r))throw new Error("array of scalars expected");r.forEach((e,n)=>{if(!t.isValid(e))throw new Error("invalid scalar at index "+n)})}var De=new WeakMap,Er=new WeakMap;function Pe(r){return Er.get(r)||1}function oe(r,t){return{constTimeNegate:He,hasPrecomputes(e){return Pe(e)!==1},unsafeLadder(e,n,o=r.ZERO){let s=e;for(;n>gr;)n&_e&&(o=o.add(s)),s=s.double(),n>>=_e;return o},precomputeWindow(e,n){let{windows:o,windowSize:s}=ke(n,t),i=[],a=e,c=a;for(let h=0;h<o;h++){c=a,i.push(c);for(let f=1;f<s;f++)c=c.add(a),i.push(c);a=c.double()}return i},wNAF(e,n,o){let s=r.ZERO,i=r.BASE,a=ke(e,t);for(let c=0;c<a.windows;c++){let{nextN:h,offset:f,isZero:p,isNeg:y,isNegF:A,offsetF:k}=wr(o,c,a);o=h,p?i=i.add(He(A,n[k])):s=s.add(He(y,n[f]))}return{p:s,f:i}},wNAFUnsafe(e,n,o,s=r.ZERO){let i=ke(e,t);for(let a=0;a<i.windows&&o!==gr;a++){let{nextN:c,offset:h,isZero:f,isNeg:p}=wr(o,a,i);if(o=c,!f){let y=n[h];s=s.add(p?y.negate():y)}}return s},getPrecomputes(e,n,o){let s=De.get(n);return s||(s=this.precomputeWindow(n,e),e!==1&&De.set(n,o(s))),s},wNAFCached(e,n,o){let s=Pe(e);return this.wNAF(s,this.getPrecomputes(s,e,o),n)},wNAFCachedUnsafe(e,n,o,s){let i=Pe(e);return i===1?this.unsafeLadder(e,n,s):this.wNAFUnsafe(i,this.getPrecomputes(i,e,o),n,s)},setWindowSize(e,n){Br(n,t),Er.set(e,n),De.delete(e)}}}function se(r,t,e,n){if(Dn(e,r),Pn(n,t),e.length!==n.length)throw new Error("arrays of points and scalars must have equal length");let o=r.ZERO,s=dr(BigInt(e.length)),i=s>12?s-3:s>4?s-2:s?2:1,a=Ht(i),c=new Array(Number(a)+1).fill(o),h=Math.floor((t.BITS-1)/i)*i,f=o;for(let p=h;p>=0;p-=i){c.fill(o);for(let A=0;A<n.length;A++){let k=n[A],g=Number(k>>BigInt(p)&a);c[g]=c[g].add(e[A])}let y=o;for(let A=c.length-1,k=o;A>0;A--)k=k.add(c[A]),y=y.add(k);if(f=f.add(y),p!==0)for(let A=0;A<i;A++)f=f.double()}return f}function Yt(r){return Ue(r.Fp),lt(r,{n:"bigint",h:"bigint",Gx:"field",Gy:"field"},{nBitLength:"isSafeInteger",nByteLength:"isSafeInteger"}),Object.freeze({...Ce(r.n,r.nBitLength),...r,p:r.Fp.ORDER})}var pt=BigInt(0),st=BigInt(1),vr=BigInt(2),_n=BigInt(8),Ln={zip215:!0};function On(r){let t=Yt(r);return lt(r,{hash:"function",a:"bigint",d:"bigint",randomBytes:"function"},{adjustScalarBytes:"function",domain:"function",uvRatio:"function",mapToCurve:"function"}),Object.freeze({...t})}function Kr(r){let t=On(r),{Fp:e,n,prehash:o,hash:s,randomBytes:i,nByteLength:a,h:c}=t,h=vr<<BigInt(a*8)-st,f=e.create,p=yt(t.n,t.nBitLength),y=t.uvRatio||((m,u)=>{try{return{isValid:!0,value:e.sqrt(m*e.inv(u))}}catch{return{isValid:!1,value:pt}}}),A=t.adjustScalarBytes||(m=>m),k=t.domain||((m,u,b)=>{if(dt("phflag",b),u.length||b)throw new Error("Contexts/pre-hash are not supported");return m});function g(m,u,b=!1){let v=b?st:pt;ot("coordinate "+m,u,v,h)}function d(m){if(!(m instanceof l))throw new Error("ExtendedPoint expected")}let B=Wt((m,u)=>{let{ex:b,ey:v,ez:R}=m,P=m.is0();u==null&&(u=P?_n:e.inv(R));let N=f(b*u),W=f(v*u),q=f(R*u);if(P)return{x:pt,y:st};if(q!==st)throw new Error("invZ was invalid");return{x:N,y:W}}),x=Wt(m=>{let{a:u,d:b}=t;if(m.is0())throw new Error("bad point: ZERO");let{ex:v,ey:R,ez:P,et:N}=m,W=f(v*v),q=f(R*R),Z=f(P*P),X=f(Z*Z),j=f(W*u),rt=f(Z*f(j+q)),it=f(X+f(b*f(W*q)));if(rt!==it)throw new Error("bad point: equation left != right (1)");let tt=f(v*R),nt=f(P*N);if(tt!==nt)throw new Error("bad point: equation left != right (2)");return!0});class l{constructor(u,b,v,R){g("x",u),g("y",b),g("z",v,!0),g("t",R),this.ex=u,this.ey=b,this.ez=v,this.et=R,Object.freeze(this)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}static fromAffine(u){if(u instanceof l)throw new Error("extended point not allowed");let{x:b,y:v}=u||{};return g("x",b),g("y",v),new l(b,v,st,f(b*v))}static normalizeZ(u){let b=e.invertBatch(u.map(v=>v.ez));return u.map((v,R)=>v.toAffine(b[R])).map(l.fromAffine)}static msm(u,b){return se(l,p,u,b)}_setWindowSize(u){C.setWindowSize(this,u)}assertValidity(){x(this)}equals(u){d(u);let{ex:b,ey:v,ez:R}=this,{ex:P,ey:N,ez:W}=u,q=f(b*W),Z=f(P*R),X=f(v*W),j=f(N*R);return q===Z&&X===j}is0(){return this.equals(l.ZERO)}negate(){return new l(f(-this.ex),this.ey,this.ez,f(-this.et))}double(){let{a:u}=t,{ex:b,ey:v,ez:R}=this,P=f(b*b),N=f(v*v),W=f(vr*f(R*R)),q=f(u*P),Z=b+v,X=f(f(Z*Z)-P-N),j=q+N,rt=j-W,it=q-N,tt=f(X*rt),nt=f(j*it),ct=f(X*it),at=f(rt*j);return new l(tt,nt,at,ct)}add(u){d(u);let{a:b,d:v}=t,{ex:R,ey:P,ez:N,et:W}=this,{ex:q,ey:Z,ez:X,et:j}=u,rt=f(R*q),it=f(P*Z),tt=f(W*v*j),nt=f(N*X),ct=f((R+P)*(q+Z)-rt-it),at=nt-tt,Zt=nt+tt,Xe=f(it-b*rt),Fr=f(ct*at),Jr=f(Zt*Xe),zr=f(ct*Xe),Gr=f(at*Zt);return new l(Fr,Jr,Gr,zr)}subtract(u){return this.add(u.negate())}wNAF(u){return C.wNAFCached(this,u,l.normalizeZ)}multiply(u){let b=u;ot("scalar",b,st,n);let{p:v,f:R}=this.wNAF(b);return l.normalizeZ([v,R])[0]}multiplyUnsafe(u,b=l.ZERO){let v=u;return ot("scalar",v,pt,n),v===pt?H:this.is0()||v===st?this:C.wNAFCachedUnsafe(this,v,l.normalizeZ,b)}isSmallOrder(){return this.multiplyUnsafe(c).is0()}isTorsionFree(){return C.unsafeLadder(this,n).is0()}toAffine(u){return B(this,u)}clearCofactor(){let{h:u}=t;return u===st?this:this.multiplyUnsafe(u)}static fromHex(u,b=!1){let{d:v,a:R}=t,P=e.BYTES;u=F("pointHex",u,P),dt("zip215",b);let N=u.slice(),W=u[P-1];N[P-1]=W&-129;let q=ht(N),Z=b?h:e.ORDER;ot("pointHex.y",q,pt,Z);let X=f(q*q),j=f(X-st),rt=f(v*X-R),{isValid:it,value:tt}=y(j,rt);if(!it)throw new Error("Point.fromHex: invalid y coordinate");let nt=(tt&st)===st,ct=(W&128)!==0;if(!b&&tt===pt&&ct)throw new Error("Point.fromHex: x=0 and x_0=1");return ct!==nt&&(tt=f(-tt)),l.fromAffine({x:tt,y:q})}static fromPrivateKey(u){let{scalar:b}=S(u);return K.multiply(b)}toRawBytes(){let{x:u,y:b}=this.toAffine(),v=Tt(b,e.BYTES);return v[v.length-1]|=u&st?128:0,v}toHex(){return At(this.toRawBytes())}}l.BASE=new l(t.Gx,t.Gy,st,f(t.Gx*t.Gy)),l.ZERO=new l(pt,st,st,pt);let{BASE:K,ZERO:H}=l,C=oe(l,a*8);function I(m){return V(m,n)}function U(m){return I(ht(m))}function S(m){let u=e.BYTES;m=F("private key",m,u);let b=F("hashed private key",s(m),2*u),v=A(b.slice(0,u)),R=b.slice(u,2*u),P=U(v);return{head:v,prefix:R,scalar:P}}function M(m){let{head:u,prefix:b,scalar:v}=S(m),R=K.multiply(v),P=R.toRawBytes();return{head:u,prefix:b,scalar:v,point:R,pointBytes:P}}function D(m){return M(m).pointBytes}function L(m=new Uint8Array,...u){let b=It(...u);return U(s(k(b,F("context",m),!!o)))}function G(m,u,b={}){m=F("message",m),o&&(m=o(m));let{prefix:v,scalar:R,pointBytes:P}=M(u),N=L(b.context,v,m),W=K.multiply(N).toRawBytes(),q=L(b.context,W,P,m),Z=I(N+q*R);ot("signature.s",Z,pt,n);let X=It(W,Tt(Z,e.BYTES));return F("result",X,e.BYTES*2)}let w=Ln;function E(m,u,b,v=w){let{context:R,zip215:P}=v,N=e.BYTES;m=F("signature",m,2*N),u=F("message",u),b=F("publicKey",b,N),P!==void 0&&dt("zip215",P),o&&(u=o(u));let W=ht(m.slice(N,2*N)),q,Z,X;try{q=l.fromHex(b,P),Z=l.fromHex(m.slice(0,N),P),X=K.multiplyUnsafe(W)}catch{return!1}if(!P&&q.isSmallOrder())return!1;let j=L(R,Z.toRawBytes(),q.toRawBytes(),u);return Z.add(q.multiplyUnsafe(j)).subtract(X).clearCofactor().equals(l.ZERO)}return K._setWindowSize(8),{CURVE:t,getPublicKey:D,sign:G,verify:E,ExtendedPoint:l,utils:{getExtendedPublicKey:M,randomPrivateKey:()=>i(e.BYTES),precompute(m=8,u=l.BASE){return u._setWindowSize(m),u.multiply(BigInt(3)),u}}}}var qt=BigInt(0),Le=BigInt(1);function Nn(r){return lt(r,{a:"bigint"},{montgomeryBits:"isSafeInteger",nByteLength:"isSafeInteger",adjustScalarBytes:"function",domain:"function",powPminus2:"function",Gu:"bigint"}),Object.freeze({...r})}function Sr(r){let t=Nn(r),{P:e}=t,n=x=>V(x,e),o=t.montgomeryBits,s=Math.ceil(o/8),i=t.nByteLength,a=t.adjustScalarBytes||(x=>x),c=t.powPminus2||(x=>Ie(x,e-BigInt(2),e));function h(x,l,K){let H=n(x*(l-K));return l=n(l-H),K=n(K+H),[l,K]}let f=(t.a-BigInt(2))/BigInt(4);function p(x,l){ot("u",x,qt,e),ot("scalar",l,qt,e);let K=l,H=x,C=Le,I=qt,U=x,S=Le,M=qt,D;for(let G=BigInt(o-1);G>=qt;G--){let w=K>>G&Le;M^=w,D=h(M,C,U),C=D[0],U=D[1],D=h(M,I,S),I=D[0],S=D[1],M=w;let E=C+I,T=n(E*E),m=C-I,u=n(m*m),b=T-u,v=U+S,R=U-S,P=n(R*E),N=n(v*m),W=P+N,q=P-N;U=n(W*W),S=n(H*n(q*q)),C=n(T*u),I=n(b*(T+n(f*b)))}D=h(M,C,U),C=D[0],U=D[1],D=h(M,I,S),I=D[0],S=D[1];let L=c(I);return n(C*L)}function y(x){return Tt(n(x),s)}function A(x){let l=F("u coordinate",x,s);return i===32&&(l[31]&=127),ht(l)}function k(x){let l=F("scalar",x),K=l.length;if(K!==s&&K!==i){let H=""+s+" or "+i;throw new Error("invalid scalar, expected "+H+" bytes, got "+K)}return ht(a(l))}function g(x,l){let K=A(l),H=k(x),C=p(K,H);if(C===qt)throw new Error("invalid private or public key received");return y(C)}let d=y(t.Gu);function B(x){return g(x,d)}return{scalarMult:g,scalarMultBase:B,getSharedSecret:(x,l)=>g(x,l),getPublicKey:x=>B(x),utils:{randomPrivateKey:()=>t.randomBytes(t.nByteLength)},GuBytes:d}}var jt=BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819949"),Ar=BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752"),Us=BigInt(0),Wn=BigInt(1),Tr=BigInt(2),qn=BigInt(3),Mn=BigInt(5),Vn=BigInt(8);function Ur(r){let t=BigInt(10),e=BigInt(20),n=BigInt(40),o=BigInt(80),s=jt,a=r*r%s*r%s,c=J(a,Tr,s)*a%s,h=J(c,Wn,s)*r%s,f=J(h,Mn,s)*h%s,p=J(f,t,s)*f%s,y=J(p,e,s)*p%s,A=J(y,n,s)*y%s,k=J(A,o,s)*A%s,g=J(k,o,s)*A%s,d=J(g,t,s)*f%s;return{pow_p_5_8:J(d,Tr,s)*r%s,b2:a}}function Cr(r){return r[0]&=248,r[31]&=127,r[31]|=64,r}function Zn(r,t){let e=jt,n=V(t*t*t,e),o=V(n*n*t,e),s=Ur(r*o).pow_p_5_8,i=V(r*n*s,e),a=V(t*i*i,e),c=i,h=V(i*Ar,e),f=a===r,p=a===V(-r,e),y=a===V(-r*Ar,e);return f&&(i=c),(p||y)&&(i=h),mr(i,e)&&(i=V(-i,e)),{isValid:f||p,value:i}}var Ir=yt(jt,void 0,!0),Fn={a:Ir.create(BigInt(-1)),d:BigInt("37095705934669439343138083508754565189542113879843219016388785533085940283555"),Fp:Ir,n:BigInt("7237005577332262213973186563042994240857116359379907606001950938285454250989"),h:Vn,Gx:BigInt("15112221349535400772501151409588531511454012693041857206046113283949847762202"),Gy:BigInt("46316835694926478169428394003475163141307993866256225615783033603165251855960"),hash:Be,randomBytes:Jt,adjustScalarBytes:Cr,uvRatio:Zn},Mt=Kr(Fn);var Oe=Sr({P:jt,a:BigInt(486662),montgomeryBits:255,nByteLength:32,Gu:BigInt(9),powPminus2:r=>{let t=jt,{pow_p_5_8:e,b2:n}=Ur(r);return V(J(e,qn,t)*n,t)},adjustScalarBytes:Cr,randomBytes:Jt});var ie=class extends O{constructor(){super(),this.keyType="Ed25519"}async createPrivateKey(){await this.initialisePrivateKey(Mt.utils.randomPrivateKey())}async initialisePrivateKey(t){this.privateKeyBytes=t??Mt.utils.randomPrivateKey(),this.privateKeyBytes.length>32&&(this.privateKeyBytes=this.privateKeyBytes.slice(0,32)),this.publicKeyBytes=Mt.getPublicKey(this.privateKeyBytes)}async toJWK(t){return{kty:"OKP",crv:"Ed25519",kid:O.bytesToHex(this.publicKey()),use:"sig",key_ops:["verify"],alg:t||"EdDSA",x:Y(this.publicKey(),"base64url")}}async importFromJWK(t){t.kty=="OKP"&&t.crv=="Ed25519"&&t.x&&(this.publicKeyBytes=O.base64UrlToBytes(t.x))}algorithms(){return["EdDSA","Ed25519"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);return Mt.sign(e,this.privateKey())}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);return!!Mt.verify(e,n,this.publicKey())}};var ce=class extends O{constructor(){super(),this.keyType="X25519"}async createPrivateKey(){await this.initialisePrivateKey(Oe.utils.randomPrivateKey())}async initialisePrivateKey(t){this.privateKeyBytes=t,this.privateKeyBytes.length>32&&(this.privateKeyBytes=this.privateKeyBytes.slice(0,32)),this.publicKeyBytes=Oe.getPublicKey(this.privateKeyBytes)}async toJWK(t){return{kty:"OKP",crv:"X25519",kid:O.bytesToHex(this.publicKey()),use:"enc",key_ops:["encrypt"],x:Y(this.publicKeyBytes,"base64url")}}async importFromJWK(t){t.kty=="OKP"&&t.crv=="X25519"&&t.x&&(this.publicKeyBytes=O.base64UrlToBytes(t.x))}algorithms(){return[]}async signBytes(t,e){throw new Error("Algorithm "+t+" not supported on key type "+this.keyType)}async verify(t,e,n){throw new Error("Algorithm "+t+" not supported on key type "+this.keyType)}};import*as qe from"node:crypto";var ae=class extends Ot{constructor(t,e){super(),this.finished=!1,this.destroyed=!1,rr(t);let n=Ft(e);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),s.fill(0)}update(t){return Lt(this),this.iHash.update(t),this}digestInto(t){Lt(this),_t(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:e,iHash:n,finished:o,destroyed:s,blockLen:i,outputLen:a}=this;return t=t,t.finished=o,t.destroyed=s,t.blockLen=i,t.outputLen=a,t.oHash=e._cloneInto(t.oHash),t.iHash=n._cloneInto(t.iHash),t}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},Ne=(r,t,e)=>new ae(r,t).update(e).digest();Ne.create=(r,t)=>new ae(r,t);function Rr(r){r.lowS!==void 0&&dt("lowS",r.lowS),r.prehash!==void 0&&dt("prehash",r.prehash)}function Jn(r){let t=Yt(r);lt(t,{a:"field",b:"field"},{allowedPrivateKeyLengths:"array",wrapPrivateKey:"boolean",isTorsionFree:"function",clearCofactor:"function",allowInfinityPoint:"boolean",fromBytes:"function",toBytes:"function"});let{endo:e,Fp:n,a:o}=t;if(e){if(!n.eql(o,n.ZERO))throw new Error("invalid endomorphism, can only be defined for Koblitz curves that have a=0");if(typeof e!="object"||typeof e.beta!="bigint"||typeof e.splitScalar!="function")throw new Error("invalid endomorphism, expected beta: bigint and splitScalar: function")}return Object.freeze({...t})}var We=class extends Error{constructor(t=""){super(t)}},gt={Err:We,_tlv:{encode:(r,t)=>{let{Err:e}=gt;if(r<0||r>256)throw new e("tlv.encode: wrong tag");if(t.length&1)throw new e("tlv.encode: unpadded data");let n=t.length/2,o=zt(n);if(o.length/2&128)throw new e("tlv.encode: long form length too big");let s=n>127?zt(o.length/2|128):"";return zt(r)+s+o+t},decode(r,t){let{Err:e}=gt,n=0;if(r<0||r>256)throw new e("tlv.encode: wrong tag");if(t.length<2||t[n++]!==r)throw new e("tlv.decode: wrong tlv");let o=t[n++],s=!!(o&128),i=0;if(!s)i=o;else{let c=o&127;if(!c)throw new e("tlv.decode(long): indefinite length not supported");if(c>4)throw new e("tlv.decode(long): byte length is too big");let h=t.subarray(n,n+c);if(h.length!==c)throw new e("tlv.decode: length bytes not complete");if(h[0]===0)throw new e("tlv.decode(long): zero leftmost byte");for(let f of h)i=i<<8|f;if(n+=c,i<128)throw new e("tlv.decode(long): not minimal encoding")}let a=t.subarray(n,n+i);if(a.length!==i)throw new e("tlv.decode: wrong value length");return{v:a,l:t.subarray(n+i)}}},_int:{encode(r){let{Err:t}=gt;if(r<wt)throw new t("integer: negative integers are not allowed");let e=zt(r);if(Number.parseInt(e[0],16)&8&&(e="00"+e),e.length&1)throw new t("unexpected DER parsing assertion: unpadded hex");return e},decode(r){let{Err:t}=gt;if(r[0]&128)throw new t("invalid signature integer: negative");if(r[0]===0&&!(r[1]&128))throw new t("invalid signature integer: unnecessary leading zero");return xt(r)}},toSig(r){let{Err:t,_int:e,_tlv:n}=gt,o=F("signature",r),{v:s,l:i}=n.decode(48,o);if(i.length)throw new t("invalid signature: left bytes after parsing");let{v:a,l:c}=n.decode(2,s),{v:h,l:f}=n.decode(2,c);if(f.length)throw new t("invalid signature: left bytes after parsing");return{r:e.decode(a),s:e.decode(h)}},hexFromSig(r){let{_tlv:t,_int:e}=gt,n=t.encode(2,e.encode(r.r)),o=t.encode(2,e.encode(r.s)),s=n+o;return t.encode(48,s)}},wt=BigInt(0),et=BigInt(1),Fs=BigInt(2),Hr=BigInt(3),Js=BigInt(4);function zn(r){let t=Jn(r),{Fp:e}=t,n=yt(t.n,t.nBitLength),o=t.toBytes||((g,d,B)=>{let x=d.toAffine();return It(Uint8Array.from([4]),e.toBytes(x.x),e.toBytes(x.y))}),s=t.fromBytes||(g=>{let d=g.subarray(1),B=e.fromBytes(d.subarray(0,e.BYTES)),x=e.fromBytes(d.subarray(e.BYTES,2*e.BYTES));return{x:B,y:x}});function i(g){let{a:d,b:B}=t,x=e.sqr(g),l=e.mul(x,g);return e.add(e.add(l,e.mul(g,d)),B)}if(!e.eql(e.sqr(t.Gy),i(t.Gx)))throw new Error("bad generator point: equation left != right");function a(g){return re(g,et,t.n)}function c(g){let{allowedPrivateKeyLengths:d,nByteLength:B,wrapPrivateKey:x,n:l}=t;if(d&&typeof g!="bigint"){if(Ct(g)&&(g=At(g)),typeof g!="string"||!d.includes(g.length))throw new Error("invalid private key");g=g.padStart(B*2,"0")}let K;try{K=typeof g=="bigint"?g:xt(F("private key",g,B))}catch{throw new Error("invalid private key, expected hex or "+B+" bytes, got "+typeof g)}return x&&(K=V(K,l)),ot("private key",K,et,l),K}function h(g){if(!(g instanceof y))throw new Error("ProjectivePoint expected")}let f=Wt((g,d)=>{let{px:B,py:x,pz:l}=g;if(e.eql(l,e.ONE))return{x:B,y:x};let K=g.is0();d==null&&(d=K?e.ONE:e.inv(l));let H=e.mul(B,d),C=e.mul(x,d),I=e.mul(l,d);if(K)return{x:e.ZERO,y:e.ZERO};if(!e.eql(I,e.ONE))throw new Error("invZ was invalid");return{x:H,y:C}}),p=Wt(g=>{if(g.is0()){if(t.allowInfinityPoint&&!e.is0(g.py))return;throw new Error("bad point: ZERO")}let{x:d,y:B}=g.toAffine();if(!e.isValid(d)||!e.isValid(B))throw new Error("bad point: x or y not FE");let x=e.sqr(B),l=i(d);if(!e.eql(x,l))throw new Error("bad point: equation left != right");if(!g.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});class y{constructor(d,B,x){if(d==null||!e.isValid(d))throw new Error("x required");if(B==null||!e.isValid(B))throw new Error("y required");if(x==null||!e.isValid(x))throw new Error("z required");this.px=d,this.py=B,this.pz=x,Object.freeze(this)}static fromAffine(d){let{x:B,y:x}=d||{};if(!d||!e.isValid(B)||!e.isValid(x))throw new Error("invalid affine point");if(d instanceof y)throw new Error("projective point not allowed");let l=K=>e.eql(K,e.ZERO);return l(B)&&l(x)?y.ZERO:new y(B,x,e.ONE)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}static normalizeZ(d){let B=e.invertBatch(d.map(x=>x.pz));return d.map((x,l)=>x.toAffine(B[l])).map(y.fromAffine)}static fromHex(d){let B=y.fromAffine(s(F("pointHex",d)));return B.assertValidity(),B}static fromPrivateKey(d){return y.BASE.multiply(c(d))}static msm(d,B){return se(y,n,d,B)}_setWindowSize(d){k.setWindowSize(this,d)}assertValidity(){p(this)}hasEvenY(){let{y:d}=this.toAffine();if(e.isOdd)return!e.isOdd(d);throw new Error("Field doesn't support isOdd")}equals(d){h(d);let{px:B,py:x,pz:l}=this,{px:K,py:H,pz:C}=d,I=e.eql(e.mul(B,C),e.mul(K,l)),U=e.eql(e.mul(x,C),e.mul(H,l));return I&&U}negate(){return new y(this.px,e.neg(this.py),this.pz)}double(){let{a:d,b:B}=t,x=e.mul(B,Hr),{px:l,py:K,pz:H}=this,C=e.ZERO,I=e.ZERO,U=e.ZERO,S=e.mul(l,l),M=e.mul(K,K),D=e.mul(H,H),L=e.mul(l,K);return L=e.add(L,L),U=e.mul(l,H),U=e.add(U,U),C=e.mul(d,U),I=e.mul(x,D),I=e.add(C,I),C=e.sub(M,I),I=e.add(M,I),I=e.mul(C,I),C=e.mul(L,C),U=e.mul(x,U),D=e.mul(d,D),L=e.sub(S,D),L=e.mul(d,L),L=e.add(L,U),U=e.add(S,S),S=e.add(U,S),S=e.add(S,D),S=e.mul(S,L),I=e.add(I,S),D=e.mul(K,H),D=e.add(D,D),S=e.mul(D,L),C=e.sub(C,S),U=e.mul(D,M),U=e.add(U,U),U=e.add(U,U),new y(C,I,U)}add(d){h(d);let{px:B,py:x,pz:l}=this,{px:K,py:H,pz:C}=d,I=e.ZERO,U=e.ZERO,S=e.ZERO,M=t.a,D=e.mul(t.b,Hr),L=e.mul(B,K),G=e.mul(x,H),w=e.mul(l,C),E=e.add(B,x),T=e.add(K,H);E=e.mul(E,T),T=e.add(L,G),E=e.sub(E,T),T=e.add(B,l);let m=e.add(K,C);return T=e.mul(T,m),m=e.add(L,w),T=e.sub(T,m),m=e.add(x,l),I=e.add(H,C),m=e.mul(m,I),I=e.add(G,w),m=e.sub(m,I),S=e.mul(M,T),I=e.mul(D,w),S=e.add(I,S),I=e.sub(G,S),S=e.add(G,S),U=e.mul(I,S),G=e.add(L,L),G=e.add(G,L),w=e.mul(M,w),T=e.mul(D,T),G=e.add(G,w),w=e.sub(L,w),w=e.mul(M,w),T=e.add(T,w),L=e.mul(G,T),U=e.add(U,L),L=e.mul(m,T),I=e.mul(E,I),I=e.sub(I,L),L=e.mul(E,G),S=e.mul(m,S),S=e.add(S,L),new y(I,U,S)}subtract(d){return this.add(d.negate())}is0(){return this.equals(y.ZERO)}wNAF(d){return k.wNAFCached(this,d,y.normalizeZ)}multiplyUnsafe(d){let{endo:B,n:x}=t;ot("scalar",d,wt,x);let l=y.ZERO;if(d===wt)return l;if(this.is0()||d===et)return this;if(!B||k.hasPrecomputes(this))return k.wNAFCachedUnsafe(this,d,y.normalizeZ);let{k1neg:K,k1:H,k2neg:C,k2:I}=B.splitScalar(d),U=l,S=l,M=this;for(;H>wt||I>wt;)H&et&&(U=U.add(M)),I&et&&(S=S.add(M)),M=M.double(),H>>=et,I>>=et;return K&&(U=U.negate()),C&&(S=S.negate()),S=new y(e.mul(S.px,B.beta),S.py,S.pz),U.add(S)}multiply(d){let{endo:B,n:x}=t;ot("scalar",d,et,x);let l,K;if(B){let{k1neg:H,k1:C,k2neg:I,k2:U}=B.splitScalar(d),{p:S,f:M}=this.wNAF(C),{p:D,f:L}=this.wNAF(U);S=k.constTimeNegate(H,S),D=k.constTimeNegate(I,D),D=new y(e.mul(D.px,B.beta),D.py,D.pz),l=S.add(D),K=M.add(L)}else{let{p:H,f:C}=this.wNAF(d);l=H,K=C}return y.normalizeZ([l,K])[0]}multiplyAndAddUnsafe(d,B,x){let l=y.BASE,K=(C,I)=>I===wt||I===et||!C.equals(l)?C.multiplyUnsafe(I):C.multiply(I),H=K(this,B).add(K(d,x));return H.is0()?void 0:H}toAffine(d){return f(this,d)}isTorsionFree(){let{h:d,isTorsionFree:B}=t;if(d===et)return!0;if(B)return B(y,this);throw new Error("isTorsionFree() has not been declared for the elliptic curve")}clearCofactor(){let{h:d,clearCofactor:B}=t;return d===et?this:B?B(y,this):this.multiplyUnsafe(t.h)}toRawBytes(d=!0){return dt("isCompressed",d),this.assertValidity(),o(y,this,d)}toHex(d=!0){return dt("isCompressed",d),At(this.toRawBytes(d))}}y.BASE=new y(t.Gx,t.Gy,e.ONE),y.ZERO=new y(e.ZERO,e.ONE,e.ZERO);let A=t.nBitLength,k=oe(y,t.endo?Math.ceil(A/2):A);return{CURVE:t,ProjectivePoint:y,normPrivateKeyToScalar:c,weierstrassEquation:i,isWithinCurveOrder:a}}function Gn(r){let t=Yt(r);return lt(t,{hash:"hash",hmac:"function",randomBytes:"function"},{bits2int:"function",bits2int_modN:"function",lowS:"boolean"}),Object.freeze({lowS:!0,...t})}function kr(r){let t=Gn(r),{Fp:e,n}=t,o=e.BYTES+1,s=2*e.BYTES+1;function i(w){return V(w,n)}function a(w){return ne(w,n)}let{ProjectivePoint:c,normPrivateKeyToScalar:h,weierstrassEquation:f,isWithinCurveOrder:p}=zn({...t,toBytes(w,E,T){let m=E.toAffine(),u=e.toBytes(m.x),b=It;return dt("isCompressed",T),T?b(Uint8Array.from([E.hasEvenY()?2:3]),u):b(Uint8Array.from([4]),u,e.toBytes(m.y))},fromBytes(w){let E=w.length,T=w[0],m=w.subarray(1);if(E===o&&(T===2||T===3)){let u=xt(m);if(!re(u,et,e.ORDER))throw new Error("Point is not on curve");let b=f(u),v;try{v=e.sqrt(b)}catch(N){let W=N instanceof Error?": "+N.message:"";throw new Error("Point is not on curve"+W)}let R=(v&et)===et;return(T&1)===1!==R&&(v=e.neg(v)),{x:u,y:v}}else if(E===s&&T===4){let u=e.fromBytes(m.subarray(0,e.BYTES)),b=e.fromBytes(m.subarray(e.BYTES,2*e.BYTES));return{x:u,y:b}}else{let u=o,b=s;throw new Error("invalid Point, expected length of "+u+", or uncompressed "+b+", got "+E)}}}),y=w=>At(Rt(w,t.nByteLength));function A(w){let E=n>>et;return w>E}function k(w){return A(w)?i(-w):w}let g=(w,E,T)=>xt(w.slice(E,T));class d{constructor(E,T,m){ot("r",E,et,n),ot("s",T,et,n),this.r=E,this.s=T,m!=null&&(this.recovery=m),Object.freeze(this)}static fromCompact(E){let T=t.nByteLength;return E=F("compactSignature",E,T*2),new d(g(E,0,T),g(E,T,2*T))}static fromDER(E){let{r:T,s:m}=gt.toSig(F("DER",E));return new d(T,m)}assertValidity(){}addRecoveryBit(E){return new d(this.r,this.s,E)}recoverPublicKey(E){let{r:T,s:m,recovery:u}=this,b=C(F("msgHash",E));if(u==null||![0,1,2,3].includes(u))throw new Error("recovery id invalid");let v=u===2||u===3?T+t.n:T;if(v>=e.ORDER)throw new Error("recovery id 2 or 3 invalid");let R=(u&1)===0?"02":"03",P=c.fromHex(R+y(v)),N=a(v),W=i(-b*N),q=i(m*N),Z=c.BASE.multiplyAndAddUnsafe(P,W,q);if(!Z)throw new Error("point at infinify");return Z.assertValidity(),Z}hasHighS(){return A(this.s)}normalizeS(){return this.hasHighS()?new d(this.r,i(-this.s),this.recovery):this}toDERRawBytes(){return Gt(this.toDERHex())}toDERHex(){return gt.hexFromSig({r:this.r,s:this.s})}toCompactRawBytes(){return Gt(this.toCompactHex())}toCompactHex(){return y(this.r)+y(this.s)}}let B={isValidPrivateKey(w){try{return h(w),!0}catch{return!1}},normPrivateKeyToScalar:h,randomPrivateKey:()=>{let w=Re(t.n);return xr(t.randomBytes(w),t.n)},precompute(w=8,E=c.BASE){return E._setWindowSize(w),E.multiply(BigInt(3)),E}};function x(w,E=!0){return c.fromPrivateKey(w).toRawBytes(E)}function l(w){let E=Ct(w),T=typeof w=="string",m=(E||T)&&w.length;return E?m===o||m===s:T?m===2*o||m===2*s:w instanceof c}function K(w,E,T=!0){if(l(w))throw new Error("first arg must be private key");if(!l(E))throw new Error("second arg must be public key");return c.fromHex(E).multiply(h(w)).toRawBytes(T)}let H=t.bits2int||function(w){if(w.length>8192)throw new Error("input is too large");let E=xt(w),T=w.length*8-t.nBitLength;return T>0?E>>BigInt(T):E},C=t.bits2int_modN||function(w){return i(H(w))},I=Ht(t.nBitLength);function U(w){return ot("num < 2^"+t.nBitLength,w,wt,I),Rt(w,t.nByteLength)}function S(w,E,T=M){if(["recovered","canonical"].some(j=>j in T))throw new Error("sign() legacy options not supported");let{hash:m,randomBytes:u}=t,{lowS:b,prehash:v,extraEntropy:R}=T;b==null&&(b=!0),w=F("msgHash",w),Rr(T),v&&(w=F("prehashed msgHash",m(w)));let P=C(w),N=h(E),W=[U(N),U(P)];if(R!=null&&R!==!1){let j=R===!0?u(e.BYTES):R;W.push(F("extraEntropy",j))}let q=It(...W),Z=P;function X(j){let rt=H(j);if(!p(rt))return;let it=a(rt),tt=c.BASE.multiply(rt).toAffine(),nt=i(tt.x);if(nt===wt)return;let ct=i(it*i(Z+nt*N));if(ct===wt)return;let at=(tt.x===nt?0:2)|Number(tt.y&et),Zt=ct;return b&&A(ct)&&(Zt=k(ct),at^=1),new d(nt,Zt,at)}return{seed:q,k2sig:X}}let M={lowS:t.lowS,prehash:!1},D={lowS:t.lowS,prehash:!1};function L(w,E,T=M){let{seed:m,k2sig:u}=S(w,E,T),b=t;return hr(b.hash.outputLen,b.nByteLength,b.hmac)(m,u)}c.BASE._setWindowSize(8);function G(w,E,T,m=D){let u=w;E=F("msgHash",E),T=F("publicKey",T);let{lowS:b,prehash:v,format:R}=m;if(Rr(m),"strict"in m)throw new Error("options.strict was renamed to lowS");if(R!==void 0&&R!=="compact"&&R!=="der")throw new Error("format must be compact or der");let P=typeof u=="string"||Ct(u),N=!P&&!R&&typeof u=="object"&&u!==null&&typeof u.r=="bigint"&&typeof u.s=="bigint";if(!P&&!N)throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");let W,q;try{if(N&&(W=new d(u.r,u.s)),P){try{R!=="compact"&&(W=d.fromDER(u))}catch(at){if(!(at instanceof gt.Err))throw at}!W&&R!=="der"&&(W=d.fromCompact(u))}q=c.fromHex(T)}catch{return!1}if(!W||b&&W.hasHighS())return!1;v&&(E=t.hash(E));let{r:Z,s:X}=W,j=C(E),rt=a(X),it=i(j*rt),tt=i(Z*rt),nt=c.BASE.multiplyAndAddUnsafe(q,it,tt)?.toAffine();return nt?i(nt.x)===Z:!1}return{CURVE:t,getPublicKey:x,getSharedSecret:K,sign:L,verify:G,ProjectivePoint:c,Signature:d,utils:B}}function Yn(r){return{hash:r,hmac:(t,...e)=>Ne(r,t,be(...e)),randomBytes:Jt}}function fe(r,t){let e=n=>kr({...r,...Yn(n)});return{...e(t),create:e}}var Dr=yt(BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff")),jn=Dr.create(BigInt("-3")),Xn=BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),Dt=fe({a:jn,b:Xn,Fp:Dr,n:BigInt("0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),Gx:BigInt("0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"),Gy:BigInt("0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"),h:BigInt(1),lowS:!1},ut);var ue=class extends O{constructor(){super(),this.keyType="Secp256r1"}async createPrivateKey(){let t=qe.createECDH("prime256v1");t.generateKeys(),await this.initialisePrivateKey(O.hexToBytes(t.getPrivateKey("hex")))}async initialisePrivateKey(t){let e=qe.createECDH("prime256v1");e.setPrivateKey(t),this.privateKeyBytes=t,this.publicKeyBytes=O.hexToBytes(e.getPublicKey("hex","compressed"))}compressedToUncompressed(t){let n=Dt.ProjectivePoint.fromHex(O.bytesToHex(t)).toHex(!1);return O.hexToBytes(n)}async toJWK(t){let e=this.compressedToUncompressed(this.publicKey());return{kty:"EC",crv:"P-256",kid:O.bytesToHex(this.publicKey()),use:"sig",key_ops:["verify"],alg:t||"ES256",x:Y(e.slice(1,33),"base64url"),y:Y(e.slice(33),"base64url")}}async importFromJWK(t){if(t.kty=="EC"&&t.crv=="P-256"&&t.x&&t.y){let e=new Uint8Array(65);e.set([4],0),e.set(O.base64UrlToBytes(t.x),1),e.set(O.base64UrlToBytes(t.y),33),this.publicKeyBytes=Dt.ProjectivePoint.fromHex(e).toRawBytes(!0)}}algorithms(){return["ES256"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let n=ut(e),o=Dt.sign(n,this.privateKey()),s=Dt.CURVE.Fp.toBytes(o.r),i=Dt.CURVE.Fp.toBytes(o.s);return new Uint8Array([...s,...i])}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let o=ut(n);return!!Dt.verify(e,o,this.publicKey())}};var Lr=BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),Pr=BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),Qn=BigInt(1),Me=BigInt(2),_r=(r,t)=>(r+t/Me)/t;function $n(r){let t=Lr,e=BigInt(3),n=BigInt(6),o=BigInt(11),s=BigInt(22),i=BigInt(23),a=BigInt(44),c=BigInt(88),h=r*r*r%t,f=h*h*r%t,p=J(f,e,t)*f%t,y=J(p,e,t)*f%t,A=J(y,Me,t)*h%t,k=J(A,o,t)*A%t,g=J(k,s,t)*k%t,d=J(g,a,t)*g%t,B=J(d,c,t)*d%t,x=J(B,a,t)*g%t,l=J(x,e,t)*f%t,K=J(l,i,t)*k%t,H=J(K,n,t)*h%t,C=J(H,Me,t);if(!Ve.eql(Ve.sqr(C),r))throw new Error("Cannot find square root");return C}var Ve=yt(Lr,void 0,void 0,{sqrt:$n}),Vt=fe({a:BigInt(0),b:BigInt(7),Fp:Ve,n:Pr,Gx:BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),Gy:BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),h:BigInt(1),lowS:!0,endo:{beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),splitScalar:r=>{let t=Pr,e=BigInt("0x3086d221a7d46bcde86c90e49284eb15"),n=-Qn*BigInt("0xe4437ed6010e88286f547fa90abfe4c3"),o=BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),s=e,i=BigInt("0x100000000000000000000000000000000"),a=_r(s*r,t),c=_r(-n*r,t),h=V(r-a*e-c*o,t),f=V(-a*n-c*s,t),p=h>i,y=f>i;if(p&&(h=t-h),y&&(f=t-f),h>i||f>i)throw new Error("splitScalar: Endomorphism failed, k="+r);return{k1neg:p,k1:h,k2neg:y,k2:f}}}},ut),ui=BigInt(0);var li=Vt.ProjectivePoint;import*as Ze from"node:crypto";var le=class extends O{constructor(){super(),this.keyType="Secp256k1"}async createPrivateKey(){let t=Ze.createECDH("secp256k1");t.generateKeys(),await this.initialisePrivateKey(O.hexToBytes(t.getPrivateKey("hex")))}async initialisePrivateKey(t){let e=Ze.createECDH("secp256k1");e.setPrivateKey(t),this.privateKeyBytes=t,this.publicKeyBytes=O.hexToBytes(e.getPublicKey("hex","compressed"))}compressedToUncompressed(t){let n=Vt.ProjectivePoint.fromHex(O.bytesToHex(t)).toHex(!1);return O.hexToBytes(n)}async toJWK(t){let e=this.compressedToUncompressed(this.publicKey());return{kty:"EC",crv:"secp256k1",kid:O.bytesToHex(this.publicKey()),use:"sig",key_ops:["verify"],alg:t||"ES256K",x:Y(e.slice(1,33),"base64url"),y:Y(e.slice(33),"base64url")}}async importFromJWK(t){if(t.kty=="EC"&&t.crv=="secp256k1"&&t.x&&t.y){let e=new Uint8Array(65);e.set([4],0),e.set(O.base64UrlToBytes(t.x),1),e.set(O.base64UrlToBytes(t.y),33),this.publicKeyBytes=Vt.ProjectivePoint.fromHex(e).toRawBytes(!0)}}algorithms(){return["ES256K","ES256K-R"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let n=ut(e),o=Vt.sign(n,this.privateKey());if(t=="ES256K-R"){let s=new Uint8Array(65);return s.set(o.toCompactRawBytes(),0),s[64]=o.recovery,s}return o.toCompactRawBytes()}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let o=ut(n),s=!1;return t=="ES256K-R"&&(e=e.slice(0,64)),s=Vt.verify(e,o,this.publicKey()),!!s}};import{createPrivateKey as to,createPublicKey as Or}from"crypto";import{generateKeyPair as eo,exportSPKI as Fe,importSPKI as ro,exportPKCS8 as no,importPKCS8 as oo,exportJWK as so,importJWK as io}from"jose";import*as Je from"node:crypto";var de=class extends O{constructor(){super(),this.keyType="RSA"}async createPrivateKey(){let{privateKey:t}=await eo("RS256",{modulusLength:2048,extractable:!0});await this.createPrivateKeyFromPEM(await no(t))}async initialisePrivateKey(t){if(!t)await this.createPrivateKey();else{this.privateKeyBytes=t;let e=await this.createCryptoKeyFromPrivateKey(),n=Or(e);await this.createPublicKeyFromPEM(await Fe(n))}}async createPublicKeyFromPEM(t){let e=t.replace(/-----BEGIN PUBLIC KEY-----/,"").replace(/-----END PUBLIC KEY-----/,"").replace(/\s+/g,"");this.publicKeyBytes=O.base64ToBytes(e)}async createPrivateKeyFromPEM(t){let e=t.replace(/-----BEGIN PRIVATE KEY-----/,"").replace(/-----END PRIVATE KEY-----/,"").replace(/\s+/g,"");this.privateKeyBytes=O.base64ToBytes(e);let n=to(t),o=Or(n);await this.createPublicKeyFromPEM(await Fe(o))}async createCryptoKeyFromPrivateKey(t="RS256"){let o=`-----BEGIN PRIVATE KEY----- | ||
| function Pt(r){return globalThis.Buffer!=null?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):r}function Xt(r=0){return globalThis.Buffer!=null&&globalThis.Buffer.allocUnsafe!=null?Pt(globalThis.Buffer.allocUnsafe(r)):new Uint8Array(r)}import{bases as Qe}from"multiformats/basics";function tr(r,t,e,n){return{name:r,prefix:t,encoder:{name:r,prefix:t,encode:e},decoder:{decode:n}}}var $e=tr("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),me=tr("ascii","a",r=>{let t="a";for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return t},r=>{r=r.substring(1);let t=Xt(r.length);for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}),jr={utf8:$e,"utf-8":$e,hex:Qe.base16,latin1:me,ascii:me,binary:me,...Qe},Qt=jr;function Bt(r,t="utf8"){let e=Qt[t];if(!e)throw new Error(`Unsupported encoding "${t}"`);return(t==="utf8"||t==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?Pt(globalThis.Buffer.from(r,"utf-8")):e.decoder.decode(`${e.prefix}${r}`)}function j(r,t="utf8"){let e=Qt[t];if(!e)throw new Error(`Unsupported encoding "${t}"`);return(t==="utf8"||t==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?globalThis.Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("utf8"):e.encoder.encode(r).substring(1)}var O=class r{keyType;privateKeyBytes;publicKeyBytes;constructor(){this.keyType="none",this.privateKeyBytes=null,this.publicKeyBytes=null}async sign(t,e,n="raw"){let o=await this.signBytes(t,e);switch(n){default:case"raw":break;case"base16":case"hex":return j(o,"base16");case"base58btc":return j(o,"base58btc");case"base64":return j(o,"base64");case"base64url":return j(o,"base64url")}throw new Error("unable to encode resulting signature using "+n)}async initialisePrivateKey(t){this.privateKeyBytes=t}static bytesToHex(t){return j(t,"base16")}static hexToBytes(t){let e=t.startsWith("0x")?t.substring(2):t;return e.length%2!==0&&(e=`0${e}`),Bt(e,"base16")}static bytesToBase64(t,e=!1){let n=j(t,"base64");if(e){let o=(4-n.length%4)%4;return n+"=".repeat(o)}return n}static base64ToBytes(t){return Bt(t,"base64")}static bytesToBase64Url(t){return j(t,"base64url")}static base64UrlToBytes(t){return Bt(t,"base64url")}hasPublicKey(){return this.publicKeyBytes!==null&&this.publicKeyBytes.length>0}publicKey(){return this.publicKeyBytes??new Uint8Array}exportPublicKey(){return r.bytesToHex(this.publicKey())}setPublicKey(t){this.publicKeyBytes=r.hexToBytes(t)}hasPrivateKey(){return this.privateKeyBytes!==null&&this.privateKeyBytes.length>0}privateKey(){return this.privateKeyBytes??new Uint8Array}exportPrivateKey(){return r.bytesToHex(this.privateKey())}setPrivateKey(t){this.privateKeyBytes=r.hexToBytes(t)}};function er(r){if(!Number.isSafeInteger(r)||r<0)throw new Error("positive integer expected, got "+r)}function Yr(r){return r instanceof Uint8Array||ArrayBuffer.isView(r)&&r.constructor.name==="Uint8Array"}function _t(r,...t){if(!Yr(r))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(r.length))throw new Error("Uint8Array expected of length "+t+", got length="+r.length)}function rr(r){if(typeof r!="function"||typeof r.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");er(r.outputLen),er(r.blockLen)}function Lt(r,t=!0){if(r.destroyed)throw new Error("Hash instance has been destroyed");if(t&&r.finished)throw new Error("Hash#digest() has already been called")}function nr(r,t){_t(r);let e=t.outputLen;if(r.length<e)throw new Error("digestInto() expects output buffer of length at least "+e)}import*as mt from"node:crypto";var Ut=mt&&typeof mt=="object"&&"webcrypto"in mt?mt.webcrypto:mt&&typeof mt=="object"&&"randomBytes"in mt?mt:void 0;function $t(r){return new DataView(r.buffer,r.byteOffset,r.byteLength)}function ft(r,t){return r<<32-t|r>>>t}var Zo=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function";function or(r){if(typeof r!="string")throw new Error("utf8ToBytes expected string, got "+typeof r);return new Uint8Array(new TextEncoder().encode(r))}function Ft(r){return typeof r=="string"&&(r=or(r)),_t(r),r}function be(...r){let t=0;for(let n=0;n<r.length;n++){let o=r[n];_t(o),t+=o.length}let e=new Uint8Array(t);for(let n=0,o=0;n<r.length;n++){let s=r[n];e.set(s,o),o+=s.length}return e}var Ot=class{clone(){return this._cloneInto()}};function te(r){let t=n=>r().update(Ft(n)).digest(),e=r();return t.outputLen=e.outputLen,t.blockLen=e.blockLen,t.create=()=>r(),t}function Jt(r=32){if(Ut&&typeof Ut.getRandomValues=="function")return Ut.getRandomValues(new Uint8Array(r));if(Ut&&typeof Ut.randomBytes=="function")return Uint8Array.from(Ut.randomBytes(r));throw new Error("crypto.getRandomValues must be defined")}function Xr(r,t,e,n){if(typeof r.setBigUint64=="function")return r.setBigUint64(t,e,n);let o=BigInt(32),s=BigInt(4294967295),i=Number(e>>o&s),a=Number(e&s),c=n?4:0,h=n?0:4;r.setUint32(t+c,i,n),r.setUint32(t+h,a,n)}function sr(r,t,e){return r&t^~r&e}function ir(r,t,e){return r&t^r&e^t&e}var Nt=class extends Ot{constructor(t,e,n,o){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=t,this.outputLen=e,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(t),this.view=$t(this.buffer)}update(t){Lt(this);let{view:e,buffer:n,blockLen:o}=this;t=Ft(t);let s=t.length;for(let i=0;i<s;){let a=Math.min(o-this.pos,s-i);if(a===o){let c=$t(t);for(;o<=s-i;i+=o)this.process(c,i);continue}n.set(t.subarray(i,i+a),this.pos),this.pos+=a,i+=a,this.pos===o&&(this.process(e,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){Lt(this),nr(t,this),this.finished=!0;let{buffer:e,view:n,blockLen:o,isLE:s}=this,{pos:i}=this;e[i++]=128,this.buffer.subarray(i).fill(0),this.padOffset>o-i&&(this.process(n,0),i=0);for(let p=i;p<o;p++)e[p]=0;Xr(n,o-8,BigInt(this.length*8),s),this.process(n,0);let a=$t(t),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let h=c/4,f=this.get();if(h>f.length)throw new Error("_sha2: outputLen bigger than state");for(let p=0;p<h;p++)a.setUint32(4*p,f[p],s)}digest(){let{buffer:t,outputLen:e}=this;this.digestInto(t);let n=t.slice(0,e);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:e,buffer:n,length:o,finished:s,destroyed:i,pos:a}=this;return t.length=o,t.pos=a,t.finished=s,t.destroyed=i,o%e&&t.buffer.set(n),t}};var Qr=new Uint32Array([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]),Et=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),vt=new Uint32Array(64),xe=class extends Nt{constructor(t=32){super(64,t,8,!1),this.A=Et[0]|0,this.B=Et[1]|0,this.C=Et[2]|0,this.D=Et[3]|0,this.E=Et[4]|0,this.F=Et[5]|0,this.G=Et[6]|0,this.H=Et[7]|0}get(){let{A:t,B:e,C:n,D:o,E:s,F:i,G:a,H:c}=this;return[t,e,n,o,s,i,a,c]}set(t,e,n,o,s,i,a,c){this.A=t|0,this.B=e|0,this.C=n|0,this.D=o|0,this.E=s|0,this.F=i|0,this.G=a|0,this.H=c|0}process(t,e){for(let p=0;p<16;p++,e+=4)vt[p]=t.getUint32(e,!1);for(let p=16;p<64;p++){let y=vt[p-15],S=vt[p-2],k=ft(y,7)^ft(y,18)^y>>>3,g=ft(S,17)^ft(S,19)^S>>>10;vt[p]=g+vt[p-7]+k+vt[p-16]|0}let{A:n,B:o,C:s,D:i,E:a,F:c,G:h,H:f}=this;for(let p=0;p<64;p++){let y=ft(a,6)^ft(a,11)^ft(a,25),S=f+y+sr(a,c,h)+Qr[p]+vt[p]|0,g=(ft(n,2)^ft(n,13)^ft(n,22))+ir(n,o,s)|0;f=h,h=c,c=a,a=i+S|0,i=s,s=o,o=n,n=S+g|0}n=n+this.A|0,o=o+this.B|0,s=s+this.C|0,i=i+this.D|0,a=a+this.E|0,c=c+this.F|0,h=h+this.G|0,f=f+this.H|0,this.set(n,o,s,i,a,c,h,f)}roundClean(){vt.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}};var ut=te(()=>new xe);var ee=BigInt(4294967295),ge=BigInt(32);function cr(r,t=!1){return t?{h:Number(r&ee),l:Number(r>>ge&ee)}:{h:Number(r>>ge&ee)|0,l:Number(r&ee)|0}}function $r(r,t=!1){let e=new Uint32Array(r.length),n=new Uint32Array(r.length);for(let o=0;o<r.length;o++){let{h:s,l:i}=cr(r[o],t);[e[o],n[o]]=[s,i]}return[e,n]}var tn=(r,t)=>BigInt(r>>>0)<<ge|BigInt(t>>>0),en=(r,t,e)=>r>>>e,rn=(r,t,e)=>r<<32-e|t>>>e,nn=(r,t,e)=>r>>>e|t<<32-e,on=(r,t,e)=>r<<32-e|t>>>e,sn=(r,t,e)=>r<<64-e|t>>>e-32,cn=(r,t,e)=>r>>>e-32|t<<64-e,an=(r,t)=>t,fn=(r,t)=>r,un=(r,t,e)=>r<<e|t>>>32-e,ln=(r,t,e)=>t<<e|r>>>32-e,dn=(r,t,e)=>t<<e-32|r>>>64-e,hn=(r,t,e)=>r<<e-32|t>>>64-e;function yn(r,t,e,n){let o=(t>>>0)+(n>>>0);return{h:r+e+(o/2**32|0)|0,l:o|0}}var pn=(r,t,e)=>(r>>>0)+(t>>>0)+(e>>>0),mn=(r,t,e,n)=>t+e+n+(r/2**32|0)|0,bn=(r,t,e,n)=>(r>>>0)+(t>>>0)+(e>>>0)+(n>>>0),xn=(r,t,e,n,o)=>t+e+n+o+(r/2**32|0)|0,gn=(r,t,e,n,o)=>(r>>>0)+(t>>>0)+(e>>>0)+(n>>>0)+(o>>>0),wn=(r,t,e,n,o,s)=>t+e+n+o+s+(r/2**32|0)|0;var Bn={fromBig:cr,split:$r,toBig:tn,shrSH:en,shrSL:rn,rotrSH:nn,rotrSL:on,rotrBH:sn,rotrBL:cn,rotr32H:an,rotr32L:fn,rotlSH:un,rotlSL:ln,rotlBH:dn,rotlBL:hn,add:yn,add3L:pn,add3H:mn,add4L:bn,add4H:xn,add5H:wn,add5L:gn},_=Bn;var[En,vn]=_.split(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(r=>BigInt(r))),Kt=new Uint32Array(80),At=new Uint32Array(80),we=class extends Nt{constructor(t=64){super(128,t,16,!1),this.Ah=1779033703,this.Al=-205731576,this.Bh=-1150833019,this.Bl=-2067093701,this.Ch=1013904242,this.Cl=-23791573,this.Dh=-1521486534,this.Dl=1595750129,this.Eh=1359893119,this.El=-1377402159,this.Fh=-1694144372,this.Fl=725511199,this.Gh=528734635,this.Gl=-79577749,this.Hh=1541459225,this.Hl=327033209}get(){let{Ah:t,Al:e,Bh:n,Bl:o,Ch:s,Cl:i,Dh:a,Dl:c,Eh:h,El:f,Fh:p,Fl:y,Gh:S,Gl:k,Hh:g,Hl:d}=this;return[t,e,n,o,s,i,a,c,h,f,p,y,S,k,g,d]}set(t,e,n,o,s,i,a,c,h,f,p,y,S,k,g,d){this.Ah=t|0,this.Al=e|0,this.Bh=n|0,this.Bl=o|0,this.Ch=s|0,this.Cl=i|0,this.Dh=a|0,this.Dl=c|0,this.Eh=h|0,this.El=f|0,this.Fh=p|0,this.Fl=y|0,this.Gh=S|0,this.Gl=k|0,this.Hh=g|0,this.Hl=d|0}process(t,e){for(let l=0;l<16;l++,e+=4)Kt[l]=t.getUint32(e),At[l]=t.getUint32(e+=4);for(let l=16;l<80;l++){let K=Kt[l-15]|0,H=At[l-15]|0,C=_.rotrSH(K,H,1)^_.rotrSH(K,H,8)^_.shrSH(K,H,7),I=_.rotrSL(K,H,1)^_.rotrSL(K,H,8)^_.shrSL(K,H,7),U=Kt[l-2]|0,A=At[l-2]|0,M=_.rotrSH(U,A,19)^_.rotrBH(U,A,61)^_.shrSH(U,A,6),D=_.rotrSL(U,A,19)^_.rotrBL(U,A,61)^_.shrSL(U,A,6),L=_.add4L(I,D,At[l-7],At[l-16]),G=_.add4H(L,C,M,Kt[l-7],Kt[l-16]);Kt[l]=G|0,At[l]=L|0}let{Ah:n,Al:o,Bh:s,Bl:i,Ch:a,Cl:c,Dh:h,Dl:f,Eh:p,El:y,Fh:S,Fl:k,Gh:g,Gl:d,Hh:B,Hl:x}=this;for(let l=0;l<80;l++){let K=_.rotrSH(p,y,14)^_.rotrSH(p,y,18)^_.rotrBH(p,y,41),H=_.rotrSL(p,y,14)^_.rotrSL(p,y,18)^_.rotrBL(p,y,41),C=p&S^~p&g,I=y&k^~y&d,U=_.add5L(x,H,I,vn[l],At[l]),A=_.add5H(U,B,K,C,En[l],Kt[l]),M=U|0,D=_.rotrSH(n,o,28)^_.rotrBH(n,o,34)^_.rotrBH(n,o,39),L=_.rotrSL(n,o,28)^_.rotrBL(n,o,34)^_.rotrBL(n,o,39),G=n&s^n&a^s&a,w=o&i^o&c^i&c;B=g|0,x=d|0,g=S|0,d=k|0,S=p|0,k=y|0,{h:p,l:y}=_.add(h|0,f|0,A|0,M|0),h=a|0,f=c|0,a=s|0,c=i|0,s=n|0,i=o|0;let E=_.add3L(M,L,w);n=_.add3H(E,A,D,G),o=E|0}({h:n,l:o}=_.add(this.Ah|0,this.Al|0,n|0,o|0)),{h:s,l:i}=_.add(this.Bh|0,this.Bl|0,s|0,i|0),{h:a,l:c}=_.add(this.Ch|0,this.Cl|0,a|0,c|0),{h,l:f}=_.add(this.Dh|0,this.Dl|0,h|0,f|0),{h:p,l:y}=_.add(this.Eh|0,this.El|0,p|0,y|0),{h:S,l:k}=_.add(this.Fh|0,this.Fl|0,S|0,k|0),{h:g,l:d}=_.add(this.Gh|0,this.Gl|0,g|0,d|0),{h:B,l:x}=_.add(this.Hh|0,this.Hl|0,B|0,x|0),this.set(n,o,s,i,a,c,h,f,p,y,S,k,g,d,B,x)}roundClean(){Kt.fill(0),At.fill(0)}destroy(){this.buffer.fill(0),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}};var Be=te(()=>new we);var Ae=BigInt(0),Ke=BigInt(1);function Ct(r){return r instanceof Uint8Array||ArrayBuffer.isView(r)&&r.constructor.name==="Uint8Array"}function Se(r){if(!Ct(r))throw new Error("Uint8Array expected")}function dt(r,t){if(typeof t!="boolean")throw new Error(r+" boolean expected, got "+t)}function zt(r){let t=r.toString(16);return t.length&1?"0"+t:t}function ur(r){if(typeof r!="string")throw new Error("hex string expected, got "+typeof r);return r===""?Ae:BigInt("0x"+r)}var lr=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",Kn=Array.from({length:256},(r,t)=>t.toString(16).padStart(2,"0"));function St(r){if(Se(r),lr)return r.toHex();let t="";for(let e=0;e<r.length;e++)t+=Kn[r[e]];return t}var bt={_0:48,_9:57,A:65,F:70,a:97,f:102};function ar(r){if(r>=bt._0&&r<=bt._9)return r-bt._0;if(r>=bt.A&&r<=bt.F)return r-(bt.A-10);if(r>=bt.a&&r<=bt.f)return r-(bt.a-10)}function Gt(r){if(typeof r!="string")throw new Error("hex string expected, got "+typeof r);if(lr)return Uint8Array.fromHex(r);let t=r.length,e=t/2;if(t%2)throw new Error("hex string expected, got unpadded hex of length "+t);let n=new Uint8Array(e);for(let o=0,s=0;o<e;o++,s+=2){let i=ar(r.charCodeAt(s)),a=ar(r.charCodeAt(s+1));if(i===void 0||a===void 0){let c=r[s]+r[s+1];throw new Error('hex string expected, got non-hex character "'+c+'" at index '+s)}n[o]=i*16+a}return n}function xt(r){return ur(St(r))}function ht(r){return Se(r),ur(St(Uint8Array.from(r).reverse()))}function Rt(r,t){return Gt(r.toString(16).padStart(t*2,"0"))}function Tt(r,t){return Rt(r,t).reverse()}function F(r,t,e){let n;if(typeof t=="string")try{n=Gt(t)}catch(s){throw new Error(r+" must be hex string or Uint8Array, cause: "+s)}else if(Ct(t))n=Uint8Array.from(t);else throw new Error(r+" must be hex string or Uint8Array");let o=n.length;if(typeof e=="number"&&o!==e)throw new Error(r+" of length "+e+" expected, got "+o);return n}function It(...r){let t=0;for(let n=0;n<r.length;n++){let o=r[n];Se(o),t+=o.length}let e=new Uint8Array(t);for(let n=0,o=0;n<r.length;n++){let s=r[n];e.set(s,o),o+=s.length}return e}var Ee=r=>typeof r=="bigint"&&Ae<=r;function re(r,t,e){return Ee(r)&&Ee(t)&&Ee(e)&&t<=r&&r<e}function ot(r,t,e,n){if(!re(t,e,n))throw new Error("expected valid "+r+": "+e+" <= n < "+n+", got "+t)}function dr(r){let t;for(t=0;r>Ae;r>>=Ke,t+=1);return t}var Ht=r=>(Ke<<BigInt(r))-Ke,ve=r=>new Uint8Array(r),fr=r=>Uint8Array.from(r);function hr(r,t,e){if(typeof r!="number"||r<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 e!="function")throw new Error("hmacFn must be a function");let n=ve(r),o=ve(r),s=0,i=()=>{n.fill(1),o.fill(0),s=0},a=(...p)=>e(o,n,...p),c=(p=ve(0))=>{o=a(fr([0]),p),n=a(),p.length!==0&&(o=a(fr([1]),p),n=a())},h=()=>{if(s++>=1e3)throw new Error("drbg: tried 1000 values");let p=0,y=[];for(;p<t;){n=a();let S=n.slice();y.push(S),p+=n.length}return It(...y)};return(p,y)=>{i(),c(p);let S;for(;!(S=y(h()));)c();return i(),S}}var An={bigint:r=>typeof r=="bigint",function:r=>typeof r=="function",boolean:r=>typeof r=="boolean",string:r=>typeof r=="string",stringOrUint8Array:r=>typeof r=="string"||Ct(r),isSafeInteger:r=>Number.isSafeInteger(r),array:r=>Array.isArray(r),field:(r,t)=>t.Fp.isValid(r),hash:r=>typeof r=="function"&&Number.isSafeInteger(r.outputLen)};function lt(r,t,e={}){let n=(o,s,i)=>{let a=An[s];if(typeof a!="function")throw new Error("invalid validator function");let c=r[o];if(!(i&&c===void 0)&&!a(c,r))throw new Error("param "+String(o)+" is invalid. Expected "+s+", got "+c)};for(let[o,s]of Object.entries(t))n(o,s,!1);for(let[o,s]of Object.entries(e))n(o,s,!0);return r}function Wt(r){let t=new WeakMap;return(e,...n)=>{let o=t.get(e);if(o!==void 0)return o;let s=r(e,...n);return t.set(e,s),s}}var $=BigInt(0),z=BigInt(1),kt=BigInt(2),Sn=BigInt(3),Te=BigInt(4),yr=BigInt(5),pr=BigInt(8),Tn=BigInt(9),In=BigInt(16);function V(r,t){let e=r%t;return e>=$?e:t+e}function Ie(r,t,e){if(t<$)throw new Error("invalid exponent, negatives unsupported");if(e<=$)throw new Error("invalid modulus");if(e===z)return $;let n=z;for(;t>$;)t&z&&(n=n*r%e),r=r*r%e,t>>=z;return n}function J(r,t,e){let n=r;for(;t-- >$;)n*=n,n%=e;return n}function ne(r,t){if(r===$)throw new Error("invert: expected non-zero number");if(t<=$)throw new Error("invert: expected positive modulus, got "+t);let e=V(r,t),n=t,o=$,s=z,i=z,a=$;for(;e!==$;){let h=n/e,f=n%e,p=o-i*h,y=s-a*h;n=e,e=f,o=i,s=a,i=p,a=y}if(n!==z)throw new Error("invert: does not exist");return V(o,t)}function Un(r){let t=(r-z)/kt,e,n,o;for(e=r-z,n=0;e%kt===$;e/=kt,n++);for(o=kt;o<r&&Ie(o,t,r)!==r-z;o++)if(o>1e3)throw new Error("Cannot find square root: likely non-prime P");if(n===1){let i=(r+z)/Te;return function(c,h){let f=c.pow(h,i);if(!c.eql(c.sqr(f),h))throw new Error("Cannot find square root");return f}}let s=(e+z)/kt;return function(a,c){if(a.pow(c,t)===a.neg(a.ONE))throw new Error("Cannot find square root");let h=n,f=a.pow(a.mul(a.ONE,o),e),p=a.pow(c,s),y=a.pow(c,e);for(;!a.eql(y,a.ONE);){if(a.eql(y,a.ZERO))return a.ZERO;let S=1;for(let g=a.sqr(y);S<h&&!a.eql(g,a.ONE);S++)g=a.sqr(g);let k=a.pow(f,z<<BigInt(h-S-1));f=a.sqr(k),p=a.mul(p,k),y=a.mul(y,f),h=S}return p}}function Cn(r){if(r%Te===Sn){let t=(r+z)/Te;return function(n,o){let s=n.pow(o,t);if(!n.eql(n.sqr(s),o))throw new Error("Cannot find square root");return s}}if(r%pr===yr){let t=(r-yr)/pr;return function(n,o){let s=n.mul(o,kt),i=n.pow(s,t),a=n.mul(o,i),c=n.mul(n.mul(a,kt),i),h=n.mul(a,n.sub(c,n.ONE));if(!n.eql(n.sqr(h),o))throw new Error("Cannot find square root");return h}}return r%In,Un(r)}var mr=(r,t)=>(V(r,t)&z)===z,Rn=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Ue(r){let t={ORDER:"bigint",MASK:"bigint",BYTES:"isSafeInteger",BITS:"isSafeInteger"},e=Rn.reduce((n,o)=>(n[o]="function",n),t);return lt(r,e)}function Hn(r,t,e){if(e<$)throw new Error("invalid exponent, negatives unsupported");if(e===$)return r.ONE;if(e===z)return t;let n=r.ONE,o=t;for(;e>$;)e&z&&(n=r.mul(n,o)),o=r.sqr(o),e>>=z;return n}function kn(r,t){let e=new Array(t.length),n=t.reduce((s,i,a)=>r.is0(i)?s:(e[a]=s,r.mul(s,i)),r.ONE),o=r.inv(n);return t.reduceRight((s,i,a)=>r.is0(i)?s:(e[a]=r.mul(s,e[a]),r.mul(s,i)),o),e}function Ce(r,t){let e=t!==void 0?t:r.toString(2).length,n=Math.ceil(e/8);return{nBitLength:e,nByteLength:n}}function yt(r,t,e=!1,n={}){if(r<=$)throw new Error("invalid field: expected ORDER > 0, got "+r);let{nBitLength:o,nByteLength:s}=Ce(r,t);if(s>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");let i,a=Object.freeze({ORDER:r,isLE:e,BITS:o,BYTES:s,MASK:Ht(o),ZERO:$,ONE:z,create:c=>V(c,r),isValid:c=>{if(typeof c!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof c);return $<=c&&c<r},is0:c=>c===$,isOdd:c=>(c&z)===z,neg:c=>V(-c,r),eql:(c,h)=>c===h,sqr:c=>V(c*c,r),add:(c,h)=>V(c+h,r),sub:(c,h)=>V(c-h,r),mul:(c,h)=>V(c*h,r),pow:(c,h)=>Hn(a,c,h),div:(c,h)=>V(c*ne(h,r),r),sqrN:c=>c*c,addN:(c,h)=>c+h,subN:(c,h)=>c-h,mulN:(c,h)=>c*h,inv:c=>ne(c,r),sqrt:n.sqrt||(c=>(i||(i=Cn(r)),i(a,c))),invertBatch:c=>kn(a,c),cmov:(c,h,f)=>f?h:c,toBytes:c=>e?Tt(c,s):Rt(c,s),fromBytes:c=>{if(c.length!==s)throw new Error("Field.fromBytes: expected "+s+" bytes, got "+c.length);return e?ht(c):xt(c)}});return Object.freeze(a)}function br(r){if(typeof r!="bigint")throw new Error("field order must be bigint");let t=r.toString(2).length;return Math.ceil(t/8)}function Re(r){let t=br(r);return t+Math.ceil(t/2)}function xr(r,t,e=!1){let n=r.length,o=br(t),s=Re(t);if(n<16||n<s||n>1024)throw new Error("expected "+s+"-1024 bytes of input, got "+n);let i=e?ht(r):xt(r),a=V(i,t-z)+z;return e?Tt(a,o):Rt(a,o)}var gr=BigInt(0),_e=BigInt(1);function He(r,t){let e=t.negate();return r?e:t}function Br(r,t){if(!Number.isSafeInteger(r)||r<=0||r>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+r)}function ke(r,t){Br(r,t);let e=Math.ceil(t/r)+1,n=2**(r-1),o=2**r,s=Ht(r),i=BigInt(r);return{windows:e,windowSize:n,mask:s,maxNumber:o,shiftBy:i}}function wr(r,t,e){let{windowSize:n,mask:o,maxNumber:s,shiftBy:i}=e,a=Number(r&o),c=r>>i;a>n&&(a-=s,c+=_e);let h=t*n,f=h+Math.abs(a)-1,p=a===0,y=a<0,S=t%2!==0;return{nextN:c,offset:f,isZero:p,isNeg:y,isNegF:S,offsetF:h}}function Dn(r,t){if(!Array.isArray(r))throw new Error("array expected");r.forEach((e,n)=>{if(!(e instanceof t))throw new Error("invalid point at index "+n)})}function Pn(r,t){if(!Array.isArray(r))throw new Error("array of scalars expected");r.forEach((e,n)=>{if(!t.isValid(e))throw new Error("invalid scalar at index "+n)})}var De=new WeakMap,Er=new WeakMap;function Pe(r){return Er.get(r)||1}function oe(r,t){return{constTimeNegate:He,hasPrecomputes(e){return Pe(e)!==1},unsafeLadder(e,n,o=r.ZERO){let s=e;for(;n>gr;)n&_e&&(o=o.add(s)),s=s.double(),n>>=_e;return o},precomputeWindow(e,n){let{windows:o,windowSize:s}=ke(n,t),i=[],a=e,c=a;for(let h=0;h<o;h++){c=a,i.push(c);for(let f=1;f<s;f++)c=c.add(a),i.push(c);a=c.double()}return i},wNAF(e,n,o){let s=r.ZERO,i=r.BASE,a=ke(e,t);for(let c=0;c<a.windows;c++){let{nextN:h,offset:f,isZero:p,isNeg:y,isNegF:S,offsetF:k}=wr(o,c,a);o=h,p?i=i.add(He(S,n[k])):s=s.add(He(y,n[f]))}return{p:s,f:i}},wNAFUnsafe(e,n,o,s=r.ZERO){let i=ke(e,t);for(let a=0;a<i.windows&&o!==gr;a++){let{nextN:c,offset:h,isZero:f,isNeg:p}=wr(o,a,i);if(o=c,!f){let y=n[h];s=s.add(p?y.negate():y)}}return s},getPrecomputes(e,n,o){let s=De.get(n);return s||(s=this.precomputeWindow(n,e),e!==1&&De.set(n,o(s))),s},wNAFCached(e,n,o){let s=Pe(e);return this.wNAF(s,this.getPrecomputes(s,e,o),n)},wNAFCachedUnsafe(e,n,o,s){let i=Pe(e);return i===1?this.unsafeLadder(e,n,s):this.wNAFUnsafe(i,this.getPrecomputes(i,e,o),n,s)},setWindowSize(e,n){Br(n,t),Er.set(e,n),De.delete(e)}}}function se(r,t,e,n){if(Dn(e,r),Pn(n,t),e.length!==n.length)throw new Error("arrays of points and scalars must have equal length");let o=r.ZERO,s=dr(BigInt(e.length)),i=s>12?s-3:s>4?s-2:s?2:1,a=Ht(i),c=new Array(Number(a)+1).fill(o),h=Math.floor((t.BITS-1)/i)*i,f=o;for(let p=h;p>=0;p-=i){c.fill(o);for(let S=0;S<n.length;S++){let k=n[S],g=Number(k>>BigInt(p)&a);c[g]=c[g].add(e[S])}let y=o;for(let S=c.length-1,k=o;S>0;S--)k=k.add(c[S]),y=y.add(k);if(f=f.add(y),p!==0)for(let S=0;S<i;S++)f=f.double()}return f}function jt(r){return Ue(r.Fp),lt(r,{n:"bigint",h:"bigint",Gx:"field",Gy:"field"},{nBitLength:"isSafeInteger",nByteLength:"isSafeInteger"}),Object.freeze({...Ce(r.n,r.nBitLength),...r,p:r.Fp.ORDER})}var pt=BigInt(0),st=BigInt(1),vr=BigInt(2),_n=BigInt(8),Ln={zip215:!0};function On(r){let t=jt(r);return lt(r,{hash:"function",a:"bigint",d:"bigint",randomBytes:"function"},{adjustScalarBytes:"function",domain:"function",uvRatio:"function",mapToCurve:"function"}),Object.freeze({...t})}function Kr(r){let t=On(r),{Fp:e,n,prehash:o,hash:s,randomBytes:i,nByteLength:a,h:c}=t,h=vr<<BigInt(a*8)-st,f=e.create,p=yt(t.n,t.nBitLength),y=t.uvRatio||((m,u)=>{try{return{isValid:!0,value:e.sqrt(m*e.inv(u))}}catch{return{isValid:!1,value:pt}}}),S=t.adjustScalarBytes||(m=>m),k=t.domain||((m,u,b)=>{if(dt("phflag",b),u.length||b)throw new Error("Contexts/pre-hash are not supported");return m});function g(m,u,b=!1){let v=b?st:pt;ot("coordinate "+m,u,v,h)}function d(m){if(!(m instanceof l))throw new Error("ExtendedPoint expected")}let B=Wt((m,u)=>{let{ex:b,ey:v,ez:R}=m,P=m.is0();u==null&&(u=P?_n:e.inv(R));let N=f(b*u),W=f(v*u),q=f(R*u);if(P)return{x:pt,y:st};if(q!==st)throw new Error("invZ was invalid");return{x:N,y:W}}),x=Wt(m=>{let{a:u,d:b}=t;if(m.is0())throw new Error("bad point: ZERO");let{ex:v,ey:R,ez:P,et:N}=m,W=f(v*v),q=f(R*R),Z=f(P*P),X=f(Z*Z),Y=f(W*u),rt=f(Z*f(Y+q)),it=f(X+f(b*f(W*q)));if(rt!==it)throw new Error("bad point: equation left != right (1)");let tt=f(v*R),nt=f(P*N);if(tt!==nt)throw new Error("bad point: equation left != right (2)");return!0});class l{constructor(u,b,v,R){g("x",u),g("y",b),g("z",v,!0),g("t",R),this.ex=u,this.ey=b,this.ez=v,this.et=R,Object.freeze(this)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}static fromAffine(u){if(u instanceof l)throw new Error("extended point not allowed");let{x:b,y:v}=u||{};return g("x",b),g("y",v),new l(b,v,st,f(b*v))}static normalizeZ(u){let b=e.invertBatch(u.map(v=>v.ez));return u.map((v,R)=>v.toAffine(b[R])).map(l.fromAffine)}static msm(u,b){return se(l,p,u,b)}_setWindowSize(u){C.setWindowSize(this,u)}assertValidity(){x(this)}equals(u){d(u);let{ex:b,ey:v,ez:R}=this,{ex:P,ey:N,ez:W}=u,q=f(b*W),Z=f(P*R),X=f(v*W),Y=f(N*R);return q===Z&&X===Y}is0(){return this.equals(l.ZERO)}negate(){return new l(f(-this.ex),this.ey,this.ez,f(-this.et))}double(){let{a:u}=t,{ex:b,ey:v,ez:R}=this,P=f(b*b),N=f(v*v),W=f(vr*f(R*R)),q=f(u*P),Z=b+v,X=f(f(Z*Z)-P-N),Y=q+N,rt=Y-W,it=q-N,tt=f(X*rt),nt=f(Y*it),ct=f(X*it),at=f(rt*Y);return new l(tt,nt,at,ct)}add(u){d(u);let{a:b,d:v}=t,{ex:R,ey:P,ez:N,et:W}=this,{ex:q,ey:Z,ez:X,et:Y}=u,rt=f(R*q),it=f(P*Z),tt=f(W*v*Y),nt=f(N*X),ct=f((R+P)*(q+Z)-rt-it),at=nt-tt,Zt=nt+tt,Xe=f(it-b*rt),Fr=f(ct*at),Jr=f(Zt*Xe),zr=f(ct*Xe),Gr=f(at*Zt);return new l(Fr,Jr,Gr,zr)}subtract(u){return this.add(u.negate())}wNAF(u){return C.wNAFCached(this,u,l.normalizeZ)}multiply(u){let b=u;ot("scalar",b,st,n);let{p:v,f:R}=this.wNAF(b);return l.normalizeZ([v,R])[0]}multiplyUnsafe(u,b=l.ZERO){let v=u;return ot("scalar",v,pt,n),v===pt?H:this.is0()||v===st?this:C.wNAFCachedUnsafe(this,v,l.normalizeZ,b)}isSmallOrder(){return this.multiplyUnsafe(c).is0()}isTorsionFree(){return C.unsafeLadder(this,n).is0()}toAffine(u){return B(this,u)}clearCofactor(){let{h:u}=t;return u===st?this:this.multiplyUnsafe(u)}static fromHex(u,b=!1){let{d:v,a:R}=t,P=e.BYTES;u=F("pointHex",u,P),dt("zip215",b);let N=u.slice(),W=u[P-1];N[P-1]=W&-129;let q=ht(N),Z=b?h:e.ORDER;ot("pointHex.y",q,pt,Z);let X=f(q*q),Y=f(X-st),rt=f(v*X-R),{isValid:it,value:tt}=y(Y,rt);if(!it)throw new Error("Point.fromHex: invalid y coordinate");let nt=(tt&st)===st,ct=(W&128)!==0;if(!b&&tt===pt&&ct)throw new Error("Point.fromHex: x=0 and x_0=1");return ct!==nt&&(tt=f(-tt)),l.fromAffine({x:tt,y:q})}static fromPrivateKey(u){let{scalar:b}=A(u);return K.multiply(b)}toRawBytes(){let{x:u,y:b}=this.toAffine(),v=Tt(b,e.BYTES);return v[v.length-1]|=u&st?128:0,v}toHex(){return St(this.toRawBytes())}}l.BASE=new l(t.Gx,t.Gy,st,f(t.Gx*t.Gy)),l.ZERO=new l(pt,st,st,pt);let{BASE:K,ZERO:H}=l,C=oe(l,a*8);function I(m){return V(m,n)}function U(m){return I(ht(m))}function A(m){let u=e.BYTES;m=F("private key",m,u);let b=F("hashed private key",s(m),2*u),v=S(b.slice(0,u)),R=b.slice(u,2*u),P=U(v);return{head:v,prefix:R,scalar:P}}function M(m){let{head:u,prefix:b,scalar:v}=A(m),R=K.multiply(v),P=R.toRawBytes();return{head:u,prefix:b,scalar:v,point:R,pointBytes:P}}function D(m){return M(m).pointBytes}function L(m=new Uint8Array,...u){let b=It(...u);return U(s(k(b,F("context",m),!!o)))}function G(m,u,b={}){m=F("message",m),o&&(m=o(m));let{prefix:v,scalar:R,pointBytes:P}=M(u),N=L(b.context,v,m),W=K.multiply(N).toRawBytes(),q=L(b.context,W,P,m),Z=I(N+q*R);ot("signature.s",Z,pt,n);let X=It(W,Tt(Z,e.BYTES));return F("result",X,e.BYTES*2)}let w=Ln;function E(m,u,b,v=w){let{context:R,zip215:P}=v,N=e.BYTES;m=F("signature",m,2*N),u=F("message",u),b=F("publicKey",b,N),P!==void 0&&dt("zip215",P),o&&(u=o(u));let W=ht(m.slice(N,2*N)),q,Z,X;try{q=l.fromHex(b,P),Z=l.fromHex(m.slice(0,N),P),X=K.multiplyUnsafe(W)}catch{return!1}if(!P&&q.isSmallOrder())return!1;let Y=L(R,Z.toRawBytes(),q.toRawBytes(),u);return Z.add(q.multiplyUnsafe(Y)).subtract(X).clearCofactor().equals(l.ZERO)}return K._setWindowSize(8),{CURVE:t,getPublicKey:D,sign:G,verify:E,ExtendedPoint:l,utils:{getExtendedPublicKey:M,randomPrivateKey:()=>i(e.BYTES),precompute(m=8,u=l.BASE){return u._setWindowSize(m),u.multiply(BigInt(3)),u}}}}var qt=BigInt(0),Le=BigInt(1);function Nn(r){return lt(r,{a:"bigint"},{montgomeryBits:"isSafeInteger",nByteLength:"isSafeInteger",adjustScalarBytes:"function",domain:"function",powPminus2:"function",Gu:"bigint"}),Object.freeze({...r})}function Ar(r){let t=Nn(r),{P:e}=t,n=x=>V(x,e),o=t.montgomeryBits,s=Math.ceil(o/8),i=t.nByteLength,a=t.adjustScalarBytes||(x=>x),c=t.powPminus2||(x=>Ie(x,e-BigInt(2),e));function h(x,l,K){let H=n(x*(l-K));return l=n(l-H),K=n(K+H),[l,K]}let f=(t.a-BigInt(2))/BigInt(4);function p(x,l){ot("u",x,qt,e),ot("scalar",l,qt,e);let K=l,H=x,C=Le,I=qt,U=x,A=Le,M=qt,D;for(let G=BigInt(o-1);G>=qt;G--){let w=K>>G&Le;M^=w,D=h(M,C,U),C=D[0],U=D[1],D=h(M,I,A),I=D[0],A=D[1],M=w;let E=C+I,T=n(E*E),m=C-I,u=n(m*m),b=T-u,v=U+A,R=U-A,P=n(R*E),N=n(v*m),W=P+N,q=P-N;U=n(W*W),A=n(H*n(q*q)),C=n(T*u),I=n(b*(T+n(f*b)))}D=h(M,C,U),C=D[0],U=D[1],D=h(M,I,A),I=D[0],A=D[1];let L=c(I);return n(C*L)}function y(x){return Tt(n(x),s)}function S(x){let l=F("u coordinate",x,s);return i===32&&(l[31]&=127),ht(l)}function k(x){let l=F("scalar",x),K=l.length;if(K!==s&&K!==i){let H=""+s+" or "+i;throw new Error("invalid scalar, expected "+H+" bytes, got "+K)}return ht(a(l))}function g(x,l){let K=S(l),H=k(x),C=p(K,H);if(C===qt)throw new Error("invalid private or public key received");return y(C)}let d=y(t.Gu);function B(x){return g(x,d)}return{scalarMult:g,scalarMultBase:B,getSharedSecret:(x,l)=>g(x,l),getPublicKey:x=>B(x),utils:{randomPrivateKey:()=>t.randomBytes(t.nByteLength)},GuBytes:d}}var Yt=BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819949"),Sr=BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752"),Us=BigInt(0),Wn=BigInt(1),Tr=BigInt(2),qn=BigInt(3),Mn=BigInt(5),Vn=BigInt(8);function Ur(r){let t=BigInt(10),e=BigInt(20),n=BigInt(40),o=BigInt(80),s=Yt,a=r*r%s*r%s,c=J(a,Tr,s)*a%s,h=J(c,Wn,s)*r%s,f=J(h,Mn,s)*h%s,p=J(f,t,s)*f%s,y=J(p,e,s)*p%s,S=J(y,n,s)*y%s,k=J(S,o,s)*S%s,g=J(k,o,s)*S%s,d=J(g,t,s)*f%s;return{pow_p_5_8:J(d,Tr,s)*r%s,b2:a}}function Cr(r){return r[0]&=248,r[31]&=127,r[31]|=64,r}function Zn(r,t){let e=Yt,n=V(t*t*t,e),o=V(n*n*t,e),s=Ur(r*o).pow_p_5_8,i=V(r*n*s,e),a=V(t*i*i,e),c=i,h=V(i*Sr,e),f=a===r,p=a===V(-r,e),y=a===V(-r*Sr,e);return f&&(i=c),(p||y)&&(i=h),mr(i,e)&&(i=V(-i,e)),{isValid:f||p,value:i}}var Ir=yt(Yt,void 0,!0),Fn={a:Ir.create(BigInt(-1)),d:BigInt("37095705934669439343138083508754565189542113879843219016388785533085940283555"),Fp:Ir,n:BigInt("7237005577332262213973186563042994240857116359379907606001950938285454250989"),h:Vn,Gx:BigInt("15112221349535400772501151409588531511454012693041857206046113283949847762202"),Gy:BigInt("46316835694926478169428394003475163141307993866256225615783033603165251855960"),hash:Be,randomBytes:Jt,adjustScalarBytes:Cr,uvRatio:Zn},Mt=Kr(Fn);var Oe=Ar({P:Yt,a:BigInt(486662),montgomeryBits:255,nByteLength:32,Gu:BigInt(9),powPminus2:r=>{let t=Yt,{pow_p_5_8:e,b2:n}=Ur(r);return V(J(e,qn,t)*n,t)},adjustScalarBytes:Cr,randomBytes:Jt});var ie=class extends O{constructor(){super(),this.keyType="Ed25519"}async createPrivateKey(){await this.initialisePrivateKey(Mt.utils.randomPrivateKey())}async initialisePrivateKey(t){this.privateKeyBytes=t??Mt.utils.randomPrivateKey(),this.privateKeyBytes.length>32&&(this.privateKeyBytes=this.privateKeyBytes.slice(0,32)),this.publicKeyBytes=Mt.getPublicKey(this.privateKeyBytes)}async toJWK(t){return{kty:"OKP",crv:"Ed25519",kid:O.bytesToHex(this.publicKey()),use:"sig",key_ops:["verify"],alg:t||"EdDSA",x:j(this.publicKey(),"base64url")}}async importFromJWK(t){t.kty=="OKP"&&t.crv=="Ed25519"&&t.x&&(this.publicKeyBytes=O.base64UrlToBytes(t.x))}algorithms(){return["EdDSA","Ed25519"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);return Mt.sign(e,this.privateKey())}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);return!!Mt.verify(e,n,this.publicKey())}};var ce=class extends O{constructor(){super(),this.keyType="X25519"}async createPrivateKey(){await this.initialisePrivateKey(Oe.utils.randomPrivateKey())}async initialisePrivateKey(t){this.privateKeyBytes=t,this.privateKeyBytes.length>32&&(this.privateKeyBytes=this.privateKeyBytes.slice(0,32)),this.publicKeyBytes=Oe.getPublicKey(this.privateKeyBytes)}async toJWK(t){return{kty:"OKP",crv:"X25519",kid:O.bytesToHex(this.publicKey()),use:"enc",key_ops:["encrypt"],x:j(this.publicKeyBytes,"base64url")}}async importFromJWK(t){t.kty=="OKP"&&t.crv=="X25519"&&t.x&&(this.publicKeyBytes=O.base64UrlToBytes(t.x))}algorithms(){return[]}async signBytes(t,e){throw new Error("Algorithm "+t+" not supported on key type "+this.keyType)}async verify(t,e,n){throw new Error("Algorithm "+t+" not supported on key type "+this.keyType)}};import*as qe from"node:crypto";var ae=class extends Ot{constructor(t,e){super(),this.finished=!1,this.destroyed=!1,rr(t);let n=Ft(e);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),s.fill(0)}update(t){return Lt(this),this.iHash.update(t),this}digestInto(t){Lt(this),_t(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:e,iHash:n,finished:o,destroyed:s,blockLen:i,outputLen:a}=this;return t=t,t.finished=o,t.destroyed=s,t.blockLen=i,t.outputLen=a,t.oHash=e._cloneInto(t.oHash),t.iHash=n._cloneInto(t.iHash),t}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},Ne=(r,t,e)=>new ae(r,t).update(e).digest();Ne.create=(r,t)=>new ae(r,t);function Rr(r){r.lowS!==void 0&&dt("lowS",r.lowS),r.prehash!==void 0&&dt("prehash",r.prehash)}function Jn(r){let t=jt(r);lt(t,{a:"field",b:"field"},{allowedPrivateKeyLengths:"array",wrapPrivateKey:"boolean",isTorsionFree:"function",clearCofactor:"function",allowInfinityPoint:"boolean",fromBytes:"function",toBytes:"function"});let{endo:e,Fp:n,a:o}=t;if(e){if(!n.eql(o,n.ZERO))throw new Error("invalid endomorphism, can only be defined for Koblitz curves that have a=0");if(typeof e!="object"||typeof e.beta!="bigint"||typeof e.splitScalar!="function")throw new Error("invalid endomorphism, expected beta: bigint and splitScalar: function")}return Object.freeze({...t})}var We=class extends Error{constructor(t=""){super(t)}},gt={Err:We,_tlv:{encode:(r,t)=>{let{Err:e}=gt;if(r<0||r>256)throw new e("tlv.encode: wrong tag");if(t.length&1)throw new e("tlv.encode: unpadded data");let n=t.length/2,o=zt(n);if(o.length/2&128)throw new e("tlv.encode: long form length too big");let s=n>127?zt(o.length/2|128):"";return zt(r)+s+o+t},decode(r,t){let{Err:e}=gt,n=0;if(r<0||r>256)throw new e("tlv.encode: wrong tag");if(t.length<2||t[n++]!==r)throw new e("tlv.decode: wrong tlv");let o=t[n++],s=!!(o&128),i=0;if(!s)i=o;else{let c=o&127;if(!c)throw new e("tlv.decode(long): indefinite length not supported");if(c>4)throw new e("tlv.decode(long): byte length is too big");let h=t.subarray(n,n+c);if(h.length!==c)throw new e("tlv.decode: length bytes not complete");if(h[0]===0)throw new e("tlv.decode(long): zero leftmost byte");for(let f of h)i=i<<8|f;if(n+=c,i<128)throw new e("tlv.decode(long): not minimal encoding")}let a=t.subarray(n,n+i);if(a.length!==i)throw new e("tlv.decode: wrong value length");return{v:a,l:t.subarray(n+i)}}},_int:{encode(r){let{Err:t}=gt;if(r<wt)throw new t("integer: negative integers are not allowed");let e=zt(r);if(Number.parseInt(e[0],16)&8&&(e="00"+e),e.length&1)throw new t("unexpected DER parsing assertion: unpadded hex");return e},decode(r){let{Err:t}=gt;if(r[0]&128)throw new t("invalid signature integer: negative");if(r[0]===0&&!(r[1]&128))throw new t("invalid signature integer: unnecessary leading zero");return xt(r)}},toSig(r){let{Err:t,_int:e,_tlv:n}=gt,o=F("signature",r),{v:s,l:i}=n.decode(48,o);if(i.length)throw new t("invalid signature: left bytes after parsing");let{v:a,l:c}=n.decode(2,s),{v:h,l:f}=n.decode(2,c);if(f.length)throw new t("invalid signature: left bytes after parsing");return{r:e.decode(a),s:e.decode(h)}},hexFromSig(r){let{_tlv:t,_int:e}=gt,n=t.encode(2,e.encode(r.r)),o=t.encode(2,e.encode(r.s)),s=n+o;return t.encode(48,s)}},wt=BigInt(0),et=BigInt(1),Fs=BigInt(2),Hr=BigInt(3),Js=BigInt(4);function zn(r){let t=Jn(r),{Fp:e}=t,n=yt(t.n,t.nBitLength),o=t.toBytes||((g,d,B)=>{let x=d.toAffine();return It(Uint8Array.from([4]),e.toBytes(x.x),e.toBytes(x.y))}),s=t.fromBytes||(g=>{let d=g.subarray(1),B=e.fromBytes(d.subarray(0,e.BYTES)),x=e.fromBytes(d.subarray(e.BYTES,2*e.BYTES));return{x:B,y:x}});function i(g){let{a:d,b:B}=t,x=e.sqr(g),l=e.mul(x,g);return e.add(e.add(l,e.mul(g,d)),B)}if(!e.eql(e.sqr(t.Gy),i(t.Gx)))throw new Error("bad generator point: equation left != right");function a(g){return re(g,et,t.n)}function c(g){let{allowedPrivateKeyLengths:d,nByteLength:B,wrapPrivateKey:x,n:l}=t;if(d&&typeof g!="bigint"){if(Ct(g)&&(g=St(g)),typeof g!="string"||!d.includes(g.length))throw new Error("invalid private key");g=g.padStart(B*2,"0")}let K;try{K=typeof g=="bigint"?g:xt(F("private key",g,B))}catch{throw new Error("invalid private key, expected hex or "+B+" bytes, got "+typeof g)}return x&&(K=V(K,l)),ot("private key",K,et,l),K}function h(g){if(!(g instanceof y))throw new Error("ProjectivePoint expected")}let f=Wt((g,d)=>{let{px:B,py:x,pz:l}=g;if(e.eql(l,e.ONE))return{x:B,y:x};let K=g.is0();d==null&&(d=K?e.ONE:e.inv(l));let H=e.mul(B,d),C=e.mul(x,d),I=e.mul(l,d);if(K)return{x:e.ZERO,y:e.ZERO};if(!e.eql(I,e.ONE))throw new Error("invZ was invalid");return{x:H,y:C}}),p=Wt(g=>{if(g.is0()){if(t.allowInfinityPoint&&!e.is0(g.py))return;throw new Error("bad point: ZERO")}let{x:d,y:B}=g.toAffine();if(!e.isValid(d)||!e.isValid(B))throw new Error("bad point: x or y not FE");let x=e.sqr(B),l=i(d);if(!e.eql(x,l))throw new Error("bad point: equation left != right");if(!g.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});class y{constructor(d,B,x){if(d==null||!e.isValid(d))throw new Error("x required");if(B==null||!e.isValid(B))throw new Error("y required");if(x==null||!e.isValid(x))throw new Error("z required");this.px=d,this.py=B,this.pz=x,Object.freeze(this)}static fromAffine(d){let{x:B,y:x}=d||{};if(!d||!e.isValid(B)||!e.isValid(x))throw new Error("invalid affine point");if(d instanceof y)throw new Error("projective point not allowed");let l=K=>e.eql(K,e.ZERO);return l(B)&&l(x)?y.ZERO:new y(B,x,e.ONE)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}static normalizeZ(d){let B=e.invertBatch(d.map(x=>x.pz));return d.map((x,l)=>x.toAffine(B[l])).map(y.fromAffine)}static fromHex(d){let B=y.fromAffine(s(F("pointHex",d)));return B.assertValidity(),B}static fromPrivateKey(d){return y.BASE.multiply(c(d))}static msm(d,B){return se(y,n,d,B)}_setWindowSize(d){k.setWindowSize(this,d)}assertValidity(){p(this)}hasEvenY(){let{y:d}=this.toAffine();if(e.isOdd)return!e.isOdd(d);throw new Error("Field doesn't support isOdd")}equals(d){h(d);let{px:B,py:x,pz:l}=this,{px:K,py:H,pz:C}=d,I=e.eql(e.mul(B,C),e.mul(K,l)),U=e.eql(e.mul(x,C),e.mul(H,l));return I&&U}negate(){return new y(this.px,e.neg(this.py),this.pz)}double(){let{a:d,b:B}=t,x=e.mul(B,Hr),{px:l,py:K,pz:H}=this,C=e.ZERO,I=e.ZERO,U=e.ZERO,A=e.mul(l,l),M=e.mul(K,K),D=e.mul(H,H),L=e.mul(l,K);return L=e.add(L,L),U=e.mul(l,H),U=e.add(U,U),C=e.mul(d,U),I=e.mul(x,D),I=e.add(C,I),C=e.sub(M,I),I=e.add(M,I),I=e.mul(C,I),C=e.mul(L,C),U=e.mul(x,U),D=e.mul(d,D),L=e.sub(A,D),L=e.mul(d,L),L=e.add(L,U),U=e.add(A,A),A=e.add(U,A),A=e.add(A,D),A=e.mul(A,L),I=e.add(I,A),D=e.mul(K,H),D=e.add(D,D),A=e.mul(D,L),C=e.sub(C,A),U=e.mul(D,M),U=e.add(U,U),U=e.add(U,U),new y(C,I,U)}add(d){h(d);let{px:B,py:x,pz:l}=this,{px:K,py:H,pz:C}=d,I=e.ZERO,U=e.ZERO,A=e.ZERO,M=t.a,D=e.mul(t.b,Hr),L=e.mul(B,K),G=e.mul(x,H),w=e.mul(l,C),E=e.add(B,x),T=e.add(K,H);E=e.mul(E,T),T=e.add(L,G),E=e.sub(E,T),T=e.add(B,l);let m=e.add(K,C);return T=e.mul(T,m),m=e.add(L,w),T=e.sub(T,m),m=e.add(x,l),I=e.add(H,C),m=e.mul(m,I),I=e.add(G,w),m=e.sub(m,I),A=e.mul(M,T),I=e.mul(D,w),A=e.add(I,A),I=e.sub(G,A),A=e.add(G,A),U=e.mul(I,A),G=e.add(L,L),G=e.add(G,L),w=e.mul(M,w),T=e.mul(D,T),G=e.add(G,w),w=e.sub(L,w),w=e.mul(M,w),T=e.add(T,w),L=e.mul(G,T),U=e.add(U,L),L=e.mul(m,T),I=e.mul(E,I),I=e.sub(I,L),L=e.mul(E,G),A=e.mul(m,A),A=e.add(A,L),new y(I,U,A)}subtract(d){return this.add(d.negate())}is0(){return this.equals(y.ZERO)}wNAF(d){return k.wNAFCached(this,d,y.normalizeZ)}multiplyUnsafe(d){let{endo:B,n:x}=t;ot("scalar",d,wt,x);let l=y.ZERO;if(d===wt)return l;if(this.is0()||d===et)return this;if(!B||k.hasPrecomputes(this))return k.wNAFCachedUnsafe(this,d,y.normalizeZ);let{k1neg:K,k1:H,k2neg:C,k2:I}=B.splitScalar(d),U=l,A=l,M=this;for(;H>wt||I>wt;)H&et&&(U=U.add(M)),I&et&&(A=A.add(M)),M=M.double(),H>>=et,I>>=et;return K&&(U=U.negate()),C&&(A=A.negate()),A=new y(e.mul(A.px,B.beta),A.py,A.pz),U.add(A)}multiply(d){let{endo:B,n:x}=t;ot("scalar",d,et,x);let l,K;if(B){let{k1neg:H,k1:C,k2neg:I,k2:U}=B.splitScalar(d),{p:A,f:M}=this.wNAF(C),{p:D,f:L}=this.wNAF(U);A=k.constTimeNegate(H,A),D=k.constTimeNegate(I,D),D=new y(e.mul(D.px,B.beta),D.py,D.pz),l=A.add(D),K=M.add(L)}else{let{p:H,f:C}=this.wNAF(d);l=H,K=C}return y.normalizeZ([l,K])[0]}multiplyAndAddUnsafe(d,B,x){let l=y.BASE,K=(C,I)=>I===wt||I===et||!C.equals(l)?C.multiplyUnsafe(I):C.multiply(I),H=K(this,B).add(K(d,x));return H.is0()?void 0:H}toAffine(d){return f(this,d)}isTorsionFree(){let{h:d,isTorsionFree:B}=t;if(d===et)return!0;if(B)return B(y,this);throw new Error("isTorsionFree() has not been declared for the elliptic curve")}clearCofactor(){let{h:d,clearCofactor:B}=t;return d===et?this:B?B(y,this):this.multiplyUnsafe(t.h)}toRawBytes(d=!0){return dt("isCompressed",d),this.assertValidity(),o(y,this,d)}toHex(d=!0){return dt("isCompressed",d),St(this.toRawBytes(d))}}y.BASE=new y(t.Gx,t.Gy,e.ONE),y.ZERO=new y(e.ZERO,e.ONE,e.ZERO);let S=t.nBitLength,k=oe(y,t.endo?Math.ceil(S/2):S);return{CURVE:t,ProjectivePoint:y,normPrivateKeyToScalar:c,weierstrassEquation:i,isWithinCurveOrder:a}}function Gn(r){let t=jt(r);return lt(t,{hash:"hash",hmac:"function",randomBytes:"function"},{bits2int:"function",bits2int_modN:"function",lowS:"boolean"}),Object.freeze({lowS:!0,...t})}function kr(r){let t=Gn(r),{Fp:e,n}=t,o=e.BYTES+1,s=2*e.BYTES+1;function i(w){return V(w,n)}function a(w){return ne(w,n)}let{ProjectivePoint:c,normPrivateKeyToScalar:h,weierstrassEquation:f,isWithinCurveOrder:p}=zn({...t,toBytes(w,E,T){let m=E.toAffine(),u=e.toBytes(m.x),b=It;return dt("isCompressed",T),T?b(Uint8Array.from([E.hasEvenY()?2:3]),u):b(Uint8Array.from([4]),u,e.toBytes(m.y))},fromBytes(w){let E=w.length,T=w[0],m=w.subarray(1);if(E===o&&(T===2||T===3)){let u=xt(m);if(!re(u,et,e.ORDER))throw new Error("Point is not on curve");let b=f(u),v;try{v=e.sqrt(b)}catch(N){let W=N instanceof Error?": "+N.message:"";throw new Error("Point is not on curve"+W)}let R=(v&et)===et;return(T&1)===1!==R&&(v=e.neg(v)),{x:u,y:v}}else if(E===s&&T===4){let u=e.fromBytes(m.subarray(0,e.BYTES)),b=e.fromBytes(m.subarray(e.BYTES,2*e.BYTES));return{x:u,y:b}}else{let u=o,b=s;throw new Error("invalid Point, expected length of "+u+", or uncompressed "+b+", got "+E)}}}),y=w=>St(Rt(w,t.nByteLength));function S(w){let E=n>>et;return w>E}function k(w){return S(w)?i(-w):w}let g=(w,E,T)=>xt(w.slice(E,T));class d{constructor(E,T,m){ot("r",E,et,n),ot("s",T,et,n),this.r=E,this.s=T,m!=null&&(this.recovery=m),Object.freeze(this)}static fromCompact(E){let T=t.nByteLength;return E=F("compactSignature",E,T*2),new d(g(E,0,T),g(E,T,2*T))}static fromDER(E){let{r:T,s:m}=gt.toSig(F("DER",E));return new d(T,m)}assertValidity(){}addRecoveryBit(E){return new d(this.r,this.s,E)}recoverPublicKey(E){let{r:T,s:m,recovery:u}=this,b=C(F("msgHash",E));if(u==null||![0,1,2,3].includes(u))throw new Error("recovery id invalid");let v=u===2||u===3?T+t.n:T;if(v>=e.ORDER)throw new Error("recovery id 2 or 3 invalid");let R=(u&1)===0?"02":"03",P=c.fromHex(R+y(v)),N=a(v),W=i(-b*N),q=i(m*N),Z=c.BASE.multiplyAndAddUnsafe(P,W,q);if(!Z)throw new Error("point at infinify");return Z.assertValidity(),Z}hasHighS(){return S(this.s)}normalizeS(){return this.hasHighS()?new d(this.r,i(-this.s),this.recovery):this}toDERRawBytes(){return Gt(this.toDERHex())}toDERHex(){return gt.hexFromSig({r:this.r,s:this.s})}toCompactRawBytes(){return Gt(this.toCompactHex())}toCompactHex(){return y(this.r)+y(this.s)}}let B={isValidPrivateKey(w){try{return h(w),!0}catch{return!1}},normPrivateKeyToScalar:h,randomPrivateKey:()=>{let w=Re(t.n);return xr(t.randomBytes(w),t.n)},precompute(w=8,E=c.BASE){return E._setWindowSize(w),E.multiply(BigInt(3)),E}};function x(w,E=!0){return c.fromPrivateKey(w).toRawBytes(E)}function l(w){let E=Ct(w),T=typeof w=="string",m=(E||T)&&w.length;return E?m===o||m===s:T?m===2*o||m===2*s:w instanceof c}function K(w,E,T=!0){if(l(w))throw new Error("first arg must be private key");if(!l(E))throw new Error("second arg must be public key");return c.fromHex(E).multiply(h(w)).toRawBytes(T)}let H=t.bits2int||function(w){if(w.length>8192)throw new Error("input is too large");let E=xt(w),T=w.length*8-t.nBitLength;return T>0?E>>BigInt(T):E},C=t.bits2int_modN||function(w){return i(H(w))},I=Ht(t.nBitLength);function U(w){return ot("num < 2^"+t.nBitLength,w,wt,I),Rt(w,t.nByteLength)}function A(w,E,T=M){if(["recovered","canonical"].some(Y=>Y in T))throw new Error("sign() legacy options not supported");let{hash:m,randomBytes:u}=t,{lowS:b,prehash:v,extraEntropy:R}=T;b==null&&(b=!0),w=F("msgHash",w),Rr(T),v&&(w=F("prehashed msgHash",m(w)));let P=C(w),N=h(E),W=[U(N),U(P)];if(R!=null&&R!==!1){let Y=R===!0?u(e.BYTES):R;W.push(F("extraEntropy",Y))}let q=It(...W),Z=P;function X(Y){let rt=H(Y);if(!p(rt))return;let it=a(rt),tt=c.BASE.multiply(rt).toAffine(),nt=i(tt.x);if(nt===wt)return;let ct=i(it*i(Z+nt*N));if(ct===wt)return;let at=(tt.x===nt?0:2)|Number(tt.y&et),Zt=ct;return b&&S(ct)&&(Zt=k(ct),at^=1),new d(nt,Zt,at)}return{seed:q,k2sig:X}}let M={lowS:t.lowS,prehash:!1},D={lowS:t.lowS,prehash:!1};function L(w,E,T=M){let{seed:m,k2sig:u}=A(w,E,T),b=t;return hr(b.hash.outputLen,b.nByteLength,b.hmac)(m,u)}c.BASE._setWindowSize(8);function G(w,E,T,m=D){let u=w;E=F("msgHash",E),T=F("publicKey",T);let{lowS:b,prehash:v,format:R}=m;if(Rr(m),"strict"in m)throw new Error("options.strict was renamed to lowS");if(R!==void 0&&R!=="compact"&&R!=="der")throw new Error("format must be compact or der");let P=typeof u=="string"||Ct(u),N=!P&&!R&&typeof u=="object"&&u!==null&&typeof u.r=="bigint"&&typeof u.s=="bigint";if(!P&&!N)throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");let W,q;try{if(N&&(W=new d(u.r,u.s)),P){try{R!=="compact"&&(W=d.fromDER(u))}catch(at){if(!(at instanceof gt.Err))throw at}!W&&R!=="der"&&(W=d.fromCompact(u))}q=c.fromHex(T)}catch{return!1}if(!W||b&&W.hasHighS())return!1;v&&(E=t.hash(E));let{r:Z,s:X}=W,Y=C(E),rt=a(X),it=i(Y*rt),tt=i(Z*rt),nt=c.BASE.multiplyAndAddUnsafe(q,it,tt)?.toAffine();return nt?i(nt.x)===Z:!1}return{CURVE:t,getPublicKey:x,getSharedSecret:K,sign:L,verify:G,ProjectivePoint:c,Signature:d,utils:B}}function jn(r){return{hash:r,hmac:(t,...e)=>Ne(r,t,be(...e)),randomBytes:Jt}}function fe(r,t){let e=n=>kr({...r,...jn(n)});return{...e(t),create:e}}var Dr=yt(BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff")),Yn=Dr.create(BigInt("-3")),Xn=BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),Dt=fe({a:Yn,b:Xn,Fp:Dr,n:BigInt("0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),Gx:BigInt("0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"),Gy:BigInt("0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"),h:BigInt(1),lowS:!1},ut);var ue=class extends O{constructor(){super(),this.keyType="Secp256r1"}async createPrivateKey(){let t=qe.createECDH("prime256v1");t.generateKeys(),await this.initialisePrivateKey(O.hexToBytes(t.getPrivateKey("hex")))}async initialisePrivateKey(t){let e=qe.createECDH("prime256v1");e.setPrivateKey(t),this.privateKeyBytes=t,this.publicKeyBytes=O.hexToBytes(e.getPublicKey("hex","compressed"))}compressedToUncompressed(t){let n=Dt.ProjectivePoint.fromHex(O.bytesToHex(t)).toHex(!1);return O.hexToBytes(n)}async toJWK(t){let e=this.compressedToUncompressed(this.publicKey());return{kty:"EC",crv:"P-256",kid:O.bytesToHex(this.publicKey()),use:"sig",key_ops:["verify"],alg:t||"ES256",x:j(e.slice(1,33),"base64url"),y:j(e.slice(33),"base64url")}}async importFromJWK(t){if(t.kty=="EC"&&t.crv=="P-256"&&t.x&&t.y){let e=new Uint8Array(65);e.set([4],0),e.set(O.base64UrlToBytes(t.x),1),e.set(O.base64UrlToBytes(t.y),33),this.publicKeyBytes=Dt.ProjectivePoint.fromHex(e).toRawBytes(!0)}}algorithms(){return["ES256"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let n=ut(e),o=Dt.sign(n,this.privateKey()),s=Dt.CURVE.Fp.toBytes(o.r),i=Dt.CURVE.Fp.toBytes(o.s);return new Uint8Array([...s,...i])}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let o=ut(n);return!!Dt.verify(e,o,this.publicKey())}};var Lr=BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),Pr=BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),Qn=BigInt(1),Me=BigInt(2),_r=(r,t)=>(r+t/Me)/t;function $n(r){let t=Lr,e=BigInt(3),n=BigInt(6),o=BigInt(11),s=BigInt(22),i=BigInt(23),a=BigInt(44),c=BigInt(88),h=r*r*r%t,f=h*h*r%t,p=J(f,e,t)*f%t,y=J(p,e,t)*f%t,S=J(y,Me,t)*h%t,k=J(S,o,t)*S%t,g=J(k,s,t)*k%t,d=J(g,a,t)*g%t,B=J(d,c,t)*d%t,x=J(B,a,t)*g%t,l=J(x,e,t)*f%t,K=J(l,i,t)*k%t,H=J(K,n,t)*h%t,C=J(H,Me,t);if(!Ve.eql(Ve.sqr(C),r))throw new Error("Cannot find square root");return C}var Ve=yt(Lr,void 0,void 0,{sqrt:$n}),Vt=fe({a:BigInt(0),b:BigInt(7),Fp:Ve,n:Pr,Gx:BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),Gy:BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),h:BigInt(1),lowS:!0,endo:{beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),splitScalar:r=>{let t=Pr,e=BigInt("0x3086d221a7d46bcde86c90e49284eb15"),n=-Qn*BigInt("0xe4437ed6010e88286f547fa90abfe4c3"),o=BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),s=e,i=BigInt("0x100000000000000000000000000000000"),a=_r(s*r,t),c=_r(-n*r,t),h=V(r-a*e-c*o,t),f=V(-a*n-c*s,t),p=h>i,y=f>i;if(p&&(h=t-h),y&&(f=t-f),h>i||f>i)throw new Error("splitScalar: Endomorphism failed, k="+r);return{k1neg:p,k1:h,k2neg:y,k2:f}}}},ut),ui=BigInt(0);var li=Vt.ProjectivePoint;import*as Ze from"node:crypto";var le=class extends O{constructor(){super(),this.keyType="Secp256k1"}async createPrivateKey(){let t=Ze.createECDH("secp256k1");t.generateKeys(),await this.initialisePrivateKey(O.hexToBytes(t.getPrivateKey("hex")))}async initialisePrivateKey(t){let e=Ze.createECDH("secp256k1");e.setPrivateKey(t),this.privateKeyBytes=t,this.publicKeyBytes=O.hexToBytes(e.getPublicKey("hex","compressed"))}compressedToUncompressed(t){let n=Vt.ProjectivePoint.fromHex(O.bytesToHex(t)).toHex(!1);return O.hexToBytes(n)}async toJWK(t){let e=this.compressedToUncompressed(this.publicKey());return{kty:"EC",crv:"secp256k1",kid:O.bytesToHex(this.publicKey()),use:"sig",key_ops:["verify"],alg:t||"ES256K",x:j(e.slice(1,33),"base64url"),y:j(e.slice(33),"base64url")}}async importFromJWK(t){if(t.kty=="EC"&&t.crv=="secp256k1"&&t.x&&t.y){let e=new Uint8Array(65);e.set([4],0),e.set(O.base64UrlToBytes(t.x),1),e.set(O.base64UrlToBytes(t.y),33),this.publicKeyBytes=Vt.ProjectivePoint.fromHex(e).toRawBytes(!0)}}algorithms(){return["ES256K","ES256K-R"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let n=ut(e),o=Vt.sign(n,this.privateKey());if(t=="ES256K-R"){let s=new Uint8Array(65);return s.set(o.toCompactRawBytes(),0),s[64]=o.recovery,s}return o.toCompactRawBytes()}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let o=ut(n),s=!1;return t=="ES256K-R"&&(e=e.slice(0,64)),s=Vt.verify(e,o,this.publicKey()),!!s}};import{createPrivateKey as to,createPublicKey as Or}from"crypto";import{generateKeyPair as eo,exportSPKI as Fe,importSPKI as ro,exportPKCS8 as no,importPKCS8 as oo,exportJWK as so,importJWK as io}from"jose";import*as Je from"node:crypto";var de=class extends O{constructor(){super(),this.keyType="RSA"}async createPrivateKey(){let{privateKey:t}=await eo("RS256",{modulusLength:2048,extractable:!0});await this.createPrivateKeyFromPEM(await no(t))}async initialisePrivateKey(t){if(!t)await this.createPrivateKey();else{this.privateKeyBytes=t;let e=await this.createCryptoKeyFromPrivateKey(),n=Or(e);await this.createPublicKeyFromPEM(await Fe(n))}}async createPublicKeyFromPEM(t){let e=t.replace(/-----BEGIN PUBLIC KEY-----/,"").replace(/-----END PUBLIC KEY-----/,"").replace(/\s+/g,"");this.publicKeyBytes=O.base64ToBytes(e)}async createPrivateKeyFromPEM(t){let e=t.replace(/-----BEGIN PRIVATE KEY-----/,"").replace(/-----END PRIVATE KEY-----/,"").replace(/\s+/g,"");this.privateKeyBytes=O.base64ToBytes(e);let n=to(t),o=Or(n);await this.createPublicKeyFromPEM(await Fe(o))}async createCryptoKeyFromPrivateKey(t="RS256"){let o=`-----BEGIN PRIVATE KEY----- | ||
| `+O.bytesToBase64(this.privateKey(),!0).match(/.{1,64}/g).join(` | ||
@@ -9,3 +9,3 @@ `)+` | ||
| -----END PUBLIC KEY----- | ||
| `;return await ro(o,t,{extractable:!0})}async toJWK(t){let e=await this.createCryptoKeyFromPublicKey(),n=await so(e);return n.alg=t||"RS256",n.use="sig",n.key_ops=["verify"],n}async importFromJWK(t){if(t.kty=="RSA"&&t.n&&t.e){let e=await io(t,t.alg||"RS256"),n=await Fe(e);await this.createPublicKeyFromPEM(n)}}algorithms(){return["RS256","RS512"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let n=await this.createCryptoKeyFromPrivateKey(t),o=this.algorithmToSubtleAlgorithm(t);return new Uint8Array(await Je.subtle.sign(o,n,e))}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let o=await this.createCryptoKeyFromPublicKey(t);return!!await Je.subtle.verify(this.algorithmToSubtleAlgorithm(t),o,e,n)}algorithmToSubtleAlgorithm(t){return t=="RS512"?{name:"RSASSA-PKCS1-v1_5",hash:{name:"SHA-512"}}:{name:"RSASSA-PKCS1-v1_5",hash:{name:"SHA-256"}}}};import{bases as Wr}from"multiformats/basics";import{varint as Ge}from"multiformats";async function Nr(r){if(!r.startsWith("did:jwk:"))throw new Error("Invalid did:jwk");let t=r.slice(8);return await ze(Bt(t,"base64url"))}async function ze(r){let t=Y(r,"utf-8"),e=JSON.parse(t);if(e&&Object.keys(e)&&e.kty&&e.crv)return await Q.createFromJWK(e);throw new Error("Unable to decode jwk")}async function he(r){let t=await r.toJWK();return t.kid&&delete t.kid,t.key_ops&&delete t.key_ops,t.d&&delete t.d,"did:jwk:"+Y(Bt(JSON.stringify(t),"utf-8"),"base64url")}async function qr(r){if(!r.startsWith("did:key:"))throw new Error("Unable to decode did:key "+r);let t=r.split(":")[2].split("#")[0],e=Wr.base58btc.decode(t),n=Ge.decode(e),o=e.slice(n[1]),s=null;switch(n[0]){case 236:s=await Q.createFromType("x25519");break;case 237:s=await Q.createFromType("Ed25519");break;case 231:s=await Q.createFromType("Secp256k1");break;case 4608:s=await Q.createFromType("Secp256r1");break;case 60241:return await ze(o);case 4613:s=await Q.createFromType("RSA");break;case 160:case 161:case 162:case 163:case 164:case 234:case 235:case 238:case 239:case 4609:case 4610:case 4611:case 4612:case 4614:case 4619:case 4620:case 4621:case 4666:case 4876:case 4877:case 6676:case 6677:case 6678:case 40960:throw new Error("not implemented");default:throw new Error("not a proper public key")}if(s!==null)return s.setPublicKey(O.bytesToHex(o)),s;throw new Error("Could not decode did:key")}function Ye(r){let t=0,e;switch(r.keyType){case"Ed25519":t=237,e=r.publicKey();break;case"X25519":t=236,e=r.publicKey();break;case"Secp256r1":t=4608,e=r.publicKey();break;case"Secp256k1":t=231,e=r.publicKey();break;case"RSA":t=4613,e=r.publicKey();break;default:throw new Error("Unable to convert key type "+r.keyType+" to a did:key")}let n=Ge.encodingLength(t),o=new Uint8Array(n+e.length);return Ge.encodeTo(t,o),o.set(e,n),Wr.base58btc.encode(o)}async function ye(r){return"did:key:"+Ye(r)}async function Mr(r){let t=null;switch(r.kty){case"OKP":switch(r.crv){case"Ed25519":t=await Q.createFromType("Ed25519");break;case"X25519":t=await Q.createFromType("X25519");break}break;case"EC":switch(r.crv){case"P-256":t=await Q.createFromType("Secp256r1");break;case"secp256k1":t=await Q.createFromType("Secp256k1");break}break;case"RSA":t=await Q.createFromType("RSA")}if(!t)throw new Error("JWK type "+r.kty+"/"+r.crv+" not supported");return await t.importFromJWK(r),t}var je={JsonWebKey2020:["https://w3id.org/security/suites/jws-2020/v1"],Multikey:["https://w3id.org/security/multikey/v1"],EcdsaSecp256k1VerificationKey2020:["https://w3id.org/security/suites/secp256k1-2020/v1"],EcdsaSecp256k1VerificationKey2019:["https://w3id.org/security/suites/secp256k1-2019/v1"],Ed25519VerificationKey2020:["https://w3id.org/security/suites/ed25519-2020/v1"],Ed25519VerificationKey2018:["https://w3id.org/security/suites/ed25519-2018/v1"],X25519KeyAgreementKey2020:["https://w3id.org/security/suites/x25519-2020/v1"],X25519KeyAgreementKey2019:["https://w3id.org/security/suites/x25519-2019/v1"],EcdsaSecp256r1VerificationKey2019:["https://w3id.org/security/suites/ecdsa-2019/v1"]};async function Vr(r,t=0,e,n,o){let s,i="#0";switch(t){case 0:n=n||await he(r),s={id:n+i,type:e||"JsonWebKey",publicKeyJwk:await r.toJWK(),controller:n};break;case 1:n=n||await ye(r),s={id:n+i,type:e||"Multikey",publicKeyMultibase:Ye(r),controller:n};break;default:throw new Error(`invalidPublicKeyType: Unsupported public key format ${t}`)}let a=["https://www.w3.org/ns/did/v1"];e&&je[e]&&(a=a.concat(je[e]));let c={"@context":a,id:n,verificationMethod:[s],authentication:[i],assertionMethod:[i],capabilityDelegation:[i],capabilityInvocation:[i]};return r.keyType=="X25519"&&(c={id:n,verificationMethod:[s],keyAgreement:[i]}),o&&o.length&&(c.service=o),c}async function pe(r){if(r.verificationMethod)for(let t of r.verificationMethod){if(t.publicKeyJwk)return await Q.createFromJWK(t.publicKeyJwk);if(t.publicKeyMultibase)return await Q.createFromDIDKey("did:key:"+t.publicKeyMultibase)}throw new Error("No key found in DID document")}async function Zr(r){if(!r.startsWith("did:web:"))throw new Error("Unable to decode did:web "+r);let n="https://"+r.substring(8).split("#")[0].replaceAll(":","/")+"/.well-known/did.json",o=await fetch(n).then(s=>s.json());return await pe(o)}var Q=class r{static async createFromType(t,e){let n;switch(t.toLocaleLowerCase()){case"ed25519":n=new ie;break;case"x25519":n=new ce;break;case"secp256r1":n=new ue;break;case"secp256k1":n=new le;break;case"rsa":n=new de;break;default:throw new Error("key type "+t+" notsupported")}return e&&await n.initialisePrivateKey(O.hexToBytes(e)),n}static async resolve(t){if(t.startsWith("did:key:"))return r.createFromDIDKey(t);if(t.startsWith("did:jwk:"))return r.createFromDIDJWK(t);if(t.startsWith("did:web:"))return await r.createFromDIDWeb(t);throw new Error("Cannot resolve "+t)}static getKeyReference(t){if(t.startsWith("did:key:"))return t.substring(8);if(t.startsWith("did:jwk:"))return"0";if(t.startsWith("did:web:"))return"0";throw new Error("Cannot resolve "+t)}static async createFromDIDWeb(t){return await Zr(t)}static async createFromDIDKey(t){return await qr(t)}static async toDIDKey(t){return await ye(t)}static async createFromDIDJWK(t){return await Nr(t)}static async toDIDJWK(t){return await he(t)}static async createFromJWK(t){return await Mr(t)}static async createFromDIDDocument(t){return await pe(t)}static async toDIDDocument(t,e,n,o="JsonWebKey"){return await Vr(t,0,o,e,n)}static async toJWK(t){return await t.toJWK()}};export{O as CryptoKey,Q as Factory}; | ||
| `;return await ro(o,t,{extractable:!0})}async toJWK(t){let e=await this.createCryptoKeyFromPublicKey(),n=await so(e);return n.alg=t||"RS256",n.use="sig",n.key_ops=["verify"],n}async importFromJWK(t){if(t.kty=="RSA"&&t.n&&t.e){let e=await io(t,t.alg||"RS256"),n=await Fe(e);await this.createPublicKeyFromPEM(n)}}algorithms(){return["RS256","RS512"]}async signBytes(t,e){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let n=await this.createCryptoKeyFromPrivateKey(t),o=this.algorithmToSubtleAlgorithm(t);return new Uint8Array(await Je.subtle.sign(o,n,e))}async verify(t,e,n){if(!this.algorithms().includes(t))throw new Error("Algorithm "+t+" not supported on key type "+this.keyType);let o=await this.createCryptoKeyFromPublicKey(t);return!!await Je.subtle.verify(this.algorithmToSubtleAlgorithm(t),o,e,n)}algorithmToSubtleAlgorithm(t){return t=="RS512"?{name:"RSASSA-PKCS1-v1_5",hash:{name:"SHA-512"}}:{name:"RSASSA-PKCS1-v1_5",hash:{name:"SHA-256"}}}};import{bases as Wr}from"multiformats/basics";import{varint as Ge}from"multiformats";async function Nr(r){if(!r.startsWith("did:jwk:"))throw new Error("Invalid did:jwk");let t=r.slice(8);return await ze(Bt(t,"base64url"))}async function ze(r){let t=j(r,"utf-8"),e=JSON.parse(t);if(e&&Object.keys(e)&&e.kty&&e.crv)return await Q.createFromJWK(e);throw new Error("Unable to decode jwk")}async function he(r){let t=await r.toJWK();return t.kid&&delete t.kid,t.key_ops&&delete t.key_ops,t.d&&delete t.d,"did:jwk:"+j(Bt(JSON.stringify(t),"utf-8"),"base64url")}async function qr(r){if(!r.startsWith("did:key:"))throw new Error("Unable to decode did:key "+r);let t=r.split(":")[2].split("#")[0],e=Wr.base58btc.decode(t),n=Ge.decode(e),o=e.slice(n[1]),s=null;switch(n[0]){case 236:s=await Q.createFromType("x25519");break;case 237:s=await Q.createFromType("Ed25519");break;case 231:s=await Q.createFromType("Secp256k1");break;case 4608:s=await Q.createFromType("Secp256r1");break;case 60241:return await ze(o);case 4613:s=await Q.createFromType("RSA");break;case 160:case 161:case 162:case 163:case 164:case 234:case 235:case 238:case 239:case 4609:case 4610:case 4611:case 4612:case 4614:case 4619:case 4620:case 4621:case 4666:case 4876:case 4877:case 6676:case 6677:case 6678:case 40960:throw new Error("not implemented");default:throw new Error("not a proper public key")}if(s!==null)return s.setPublicKey(O.bytesToHex(o)),s;throw new Error("Could not decode did:key")}function je(r){let t=0,e;switch(r.keyType){case"Ed25519":t=237,e=r.publicKey();break;case"X25519":t=236,e=r.publicKey();break;case"Secp256r1":t=4608,e=r.publicKey();break;case"Secp256k1":t=231,e=r.publicKey();break;case"RSA":t=4613,e=r.publicKey();break;default:throw new Error("Unable to convert key type "+r.keyType+" to a did:key")}let n=Ge.encodingLength(t),o=new Uint8Array(n+e.length);return Ge.encodeTo(t,o),o.set(e,n),Wr.base58btc.encode(o)}async function ye(r){return"did:key:"+je(r)}async function Mr(r){let t=null;switch(r.kty){case"OKP":switch(r.crv){case"Ed25519":t=await Q.createFromType("Ed25519");break;case"X25519":t=await Q.createFromType("X25519");break}break;case"EC":switch(r.crv){case"P-256":t=await Q.createFromType("Secp256r1");break;case"secp256k1":t=await Q.createFromType("Secp256k1");break}break;case"RSA":t=await Q.createFromType("RSA")}if(!t)throw new Error("JWK type "+r.kty+"/"+r.crv+" not supported");return await t.importFromJWK(r),t}var Ye={JsonWebKey2020:["https://w3id.org/security/suites/jws-2020/v1"],Multikey:["https://w3id.org/security/multikey/v1"],EcdsaSecp256k1VerificationKey2020:["https://w3id.org/security/suites/secp256k1-2020/v1"],EcdsaSecp256k1VerificationKey2019:["https://w3id.org/security/suites/secp256k1-2019/v1"],Ed25519VerificationKey2020:["https://w3id.org/security/suites/ed25519-2020/v1"],Ed25519VerificationKey2018:["https://w3id.org/security/suites/ed25519-2018/v1"],X25519KeyAgreementKey2020:["https://w3id.org/security/suites/x25519-2020/v1"],X25519KeyAgreementKey2019:["https://w3id.org/security/suites/x25519-2019/v1"],EcdsaSecp256r1VerificationKey2019:["https://w3id.org/security/suites/ecdsa-2019/v1"]};async function Vr(r,t=0,e,n,o){let s,i="#0";switch(t){case 0:n=n||await he(r),s={id:n+i,type:e||"JsonWebKey",publicKeyJwk:await r.toJWK(),controller:n};break;case 1:n=n||await ye(r),s={id:n+i,type:e||"Multikey",publicKeyMultibase:je(r),controller:n};break;default:throw new Error(`invalidPublicKeyType: Unsupported public key format ${t}`)}let a=["https://www.w3.org/ns/did/v1"];e&&Ye[e]&&(a=a.concat(Ye[e]));let c={"@context":a,id:n,verificationMethod:[s],authentication:[i],assertionMethod:[i],capabilityDelegation:[i],capabilityInvocation:[i]};return r.keyType=="X25519"&&(c={id:n,verificationMethod:[s],keyAgreement:[i]}),o&&o.length&&(c.service=o),c}async function pe(r){if(r.verificationMethod)for(let t of r.verificationMethod){if(t.publicKeyJwk)return await Q.createFromJWK(t.publicKeyJwk);if(t.publicKeyMultibase)return await Q.createFromDIDKey("did:key:"+t.publicKeyMultibase)}throw new Error("No key found in DID document")}async function Zr(r){if(!r.startsWith("did:web:"))throw new Error("Unable to decode did:web "+r);let e=r.substring(8).split("#")[0].replaceAll(":","/").replaceAll("%3A",":"),n="https://"+e+"/.well-known/did.json";e.indexOf("/")>=0&&(n="https://"+e+"/did.json");let o=await fetch(n).then(s=>s.json());return await pe(o)}var Q=class r{static async createFromType(t,e){let n;switch(t.toLocaleLowerCase()){case"ed25519":n=new ie;break;case"x25519":n=new ce;break;case"secp256r1":n=new ue;break;case"secp256k1":n=new le;break;case"rsa":n=new de;break;default:throw new Error("key type "+t+" notsupported")}return e&&await n.initialisePrivateKey(O.hexToBytes(e)),n}static async resolve(t){if(t.startsWith("did:key:"))return r.createFromDIDKey(t);if(t.startsWith("did:jwk:"))return r.createFromDIDJWK(t);if(t.startsWith("did:web:"))return await r.createFromDIDWeb(t);throw new Error("Cannot resolve "+t)}static getKeyReference(t){if(t.startsWith("did:key:"))return t.substring(8);if(t.startsWith("did:jwk:"))return"0";if(t.startsWith("did:web:"))return"0";throw new Error("Cannot resolve "+t)}static async createFromDIDWeb(t){return await Zr(t)}static async createFromDIDKey(t){return await qr(t)}static async toDIDKey(t){return await ye(t)}static async createFromDIDJWK(t){return await Nr(t)}static async toDIDJWK(t){return await he(t)}static async createFromJWK(t){return await Mr(t)}static async createFromDIDDocument(t){return await pe(t)}static async toDIDDocument(t,e,n,o="JsonWebKey"){return await Vr(t,0,o,e,n)}static async toJWK(t){return await t.toJWK()}};export{O as CryptoKey,Q as Factory}; | ||
| /*! Bundled license information: | ||
@@ -12,0 +12,0 @@ |
+1
-1
| { | ||
| "name": "@muisit/cryptokey", | ||
| "version": "1.2.2", | ||
| "version": "1.2.3", | ||
| "description": "Cryptography for assymmetric keys in Node", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs", |
@@ -10,4 +10,10 @@ import { convertFromDIDDocument } from "./convertDIDDocument"; | ||
| // replace any : with a / | ||
| const path = keycode.replaceAll(":", "/"); | ||
| const url = "https://" + path + "/.well-known/did.json"; | ||
| const path = keycode.replaceAll(":", "/").replaceAll('%3A', ':'); | ||
| let url = "https://" + path + "/.well-known/did.json"; | ||
| if (path.indexOf('/') >= 0) { | ||
| // if the url contains a subpath, do not append .well-known | ||
| // url cannot end with a slash (colon), so any slash means there is a subpath | ||
| // (but a slash at index 0 means there is no domain...) | ||
| url = "https://" + path + "/did.json"; | ||
| } | ||
@@ -14,0 +20,0 @@ // fetch the document |
Sorry, the diff of this file is too big to display
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
182520
0.25%1525
0.46%