noble-curves-extended
Version:
This project extends @noble/curves to allow randomBytes to be specified externally
62 lines • 79.4 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Y=require("@noble/curves/abstract/utils"),ut=require("@noble/hashes/utils"),d=require("@noble/curves/abstract/modular"),Ue=require("@noble/hashes/sha512"),Fn=require("@noble/curves/abstract/edwards"),On=require("@noble/hashes/hmac"),Pn=require("@noble/curves/abstract/weierstrass"),Ve=require("@noble/hashes/sha256"),g=require("u8a-utils"),$n=BigInt(0),be=s=>typeof s=="bigint"&&$n<=s;function ht(s,e,t){return be(s)&&be(e)&&be(t)&&e<=s&&s<t}function me(s,e,t,r){if(!ht(e,t,r))throw new Error("expected valid "+s+": "+t+" <= n < "+r+", got "+e)}function dt(s,e,t={}){if(!s||typeof s!="object")throw new Error("expected valid options object");function r(n,i,o){const c=s[n];if(o&&c===void 0)return;const f=typeof c;if(f!==i||c===null)throw new Error(`param "${n}" is invalid: expected ${i}, got ${f}`)}Object.entries(e).forEach(([n,i])=>r(n,i,!1)),Object.entries(t).forEach(([n,i])=>r(n,i,!0))}const Q=BigInt(0),J=BigInt(1),ae=BigInt(2);function Dn(s){return dt(s,{adjustScalarBytes:"function",powPminus2:"function"}),Object.freeze({...s})}function gt(s){const e=Dn(s),{P:t,type:r,adjustScalarBytes:n,powPminus2:i,randomBytes:o}=e,c=r==="x25519";if(!c&&r!=="x448")throw new Error("invalid type");const f=o||ut.randomBytes,u=c?255:448,h=c?32:56,a=BigInt(c?9:5),w=BigInt(c?121665:39081),b=c?ae**BigInt(254):ae**BigInt(447),S=c?BigInt(8)*ae**BigInt(251)-J:BigInt(4)*ae**BigInt(445)-J,M=b+S+J,m=k=>d.mod(k,t),X=oe(a);function oe(k){return Y.numberToBytesLE(m(k),h)}function he(k){const x=Y.ensureBytes("u coordinate",k,h);return c&&(x[31]&=127),m(Y.bytesToNumberLE(x))}function de(k){return Y.bytesToNumberLE(n(Y.ensureBytes("scalar",k,h)))}function Z(k,x){const V=_n(he(x),de(k));if(V===Q)throw new Error("invalid private or public key received");return oe(V)}function z(k){return Z(k,X)}function ce(k,x,V){const fe=m(k*(x-V));return x=m(x-fe),V=m(V+fe),{x_2:x,x_3:V}}function _n(k,x){me("u",k,Q,t),me("scalar",x,b,M);const V=x,fe=k;let T=J,_=Q,L=k,F=J,D=Q;for(let ge=BigInt(u-1);ge>=Q;ge--){const Qe=V>>ge&J;D^=Qe,{x_2:T,x_3:L}=ce(D,T,L),{x_2:_,x_3:F}=ce(D,_,F),D=Qe;const we=T+_,ve=m(we*we),ye=T-_,je=m(ye*ye),et=ve-je,Mn=L+F,Ln=L-F,tt=m(Ln*we),rt=m(Mn*ye),nt=tt+rt,st=tt-rt;L=m(nt*nt),F=m(fe*m(st*st)),T=m(ve*je),_=m(et*(ve+m(w*et)))}({x_2:T,x_3:L}=ce(D,T,L)),{x_2:_,x_3:F}=ce(D,_,F);const Hn=i(_);return m(T*Hn)}return{CURVE:e,scalarMult:Z,scalarMultBase:z,getSharedSecret:(k,x)=>Z(k,x),getPublicKey:k=>z(k),utils:{randomPrivateKey:()=>f(h)},GuBytes:X.slice()}}const ke=d.Field(BigInt("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")),Jn=s=>({utils:{randomPrivateKey:()=>{const t=d.getMinHashLength(ke.ORDER);return d.mapHashToField(s(t),ke.ORDER)}}}),Kn=BigInt(1),it=BigInt(2),Gn=BigInt(5),Wn=BigInt(8),G={p:BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"),n:BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),h:Wn,a:BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),d:BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),Gx:BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),Gy:BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")};function Ce(s){const e=BigInt(10),t=BigInt(20),r=BigInt(40),n=BigInt(80),i=G.p,c=s*s%i*s%i,f=d.pow2(c,it,i)*c%i,u=d.pow2(f,Kn,i)*s%i,h=d.pow2(u,Gn,i)*u%i,a=d.pow2(h,e,i)*h%i,w=d.pow2(a,t,i)*a%i,b=d.pow2(w,r,i)*w%i,S=d.pow2(b,n,i)*b%i,M=d.pow2(S,n,i)*b%i,m=d.pow2(M,e,i)*h%i;return{pow_p_5_8:d.pow2(m,it,i)*s%i,b2:c}}function Re(s){return s[0]&=248,s[31]&=127,s[31]|=64,s}const ot=BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");function qn(s,e){const t=G.p,r=d.mod(e*e*e,t),n=d.mod(r*r*e,t),i=Ce(s*n).pow_p_5_8;let o=d.mod(s*r*i,t);const c=d.mod(e*o*o,t),f=o,u=d.mod(o*ot,t),h=c===s,a=c===d.mod(-s,t),w=c===d.mod(-s*ot,t);return h&&(o=f),(a||w)&&(o=u),d.isNegativeLE(o,t)&&(o=d.mod(-o,t)),{isValid:h||a,value:o}}const Xn=d.Field(G.p,void 0,!0),Zn={...G,Fp:Xn,hash:Ue.sha512,adjustScalarBytes:Re,uvRatio:qn},wt=s=>Fn.twistedEdwards({...Zn,randomBytes:s}),zn=["Ed25519"],Yn=BigInt(3),vt=s=>(()=>{const e=G.p;return gt({P:e,type:"x25519",powPminus2:t=>{const{pow_p_5_8:r,b2:n}=Ce(t);return d.mod(d.pow2(r,Yn,e)*n,e)},adjustScalarBytes:Re,randomBytes:s})})(),Qn=["X25519"],yt=s=>(e,...t)=>On.hmac(s,e,ut.concatBytes(...t));function se(s,e,t){return Pn.weierstrass({...s,hash:e,randomBytes:t,hmac:yt(e)})}const Te=(s,e)=>{if(e.length==s.CURVE.nByteLength*2)return s.Signature.fromCompact(e);if(e.length==s.CURVE.nByteLength*2+1){const t=e[e.length-1],r=e.slice(0,-1);return s.Signature.fromCompact(r).addRecoveryBit(t)}throw new Error("Invalid raw signature")},_e={p:BigInt("0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"),n:BigInt("0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"),h:BigInt(1),a:BigInt("0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc"),b:BigInt("0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),Gx:BigInt("0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"),Gy:BigInt("0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5")},jn=d.Field(_e.p),bt=s=>se({..._e,Fp:jn,lowS:!1},Ve.sha256,s),He={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff"),n:BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973"),h:BigInt(1),a:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc"),b:BigInt("0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef"),Gx:BigInt("0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7"),Gy:BigInt("0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f")},es=d.Field(He.p),Bt=s=>se({...He,Fp:es,lowS:!1},Ue.sha384,s),Me={p:BigInt("0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),n:BigInt("0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409"),h:BigInt(1),a:BigInt("0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc"),b:BigInt("0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00"),Gx:BigInt("0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66"),Gy:BigInt("0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650")},ts=d.Field(Me.p),mt=s=>se({...Me,Fp:ts,lowS:!1,allowedPrivateKeyLengths:[130,131,132]},Ue.sha512,s),te={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},rs=BigInt(1),Ee=BigInt(2),ct=(s,e)=>(s+e/Ee)/e;function ns(s){const e=te.p,t=BigInt(3),r=BigInt(6),n=BigInt(11),i=BigInt(22),o=BigInt(23),c=BigInt(44),f=BigInt(88),u=s*s*s%e,h=u*u*s%e,a=d.pow2(h,t,e)*h%e,w=d.pow2(a,t,e)*h%e,b=d.pow2(w,Ee,e)*u%e,S=d.pow2(b,n,e)*b%e,M=d.pow2(S,i,e)*S%e,m=d.pow2(M,c,e)*M%e,X=d.pow2(m,f,e)*m%e,oe=d.pow2(X,c,e)*M%e,he=d.pow2(oe,t,e)*h%e,de=d.pow2(he,o,e)*S%e,Z=d.pow2(de,r,e)*u%e,z=d.pow2(Z,Ee,e);if(!xe.eql(xe.sqr(z),s))throw new Error("Cannot find square root");return z}const xe=d.Field(te.p,void 0,void 0,{sqrt:ns}),kt=s=>se({...te,Fp:xe,lowS:!0,endo:{beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),splitScalar:e=>{const t=te.n,r=BigInt("0x3086d221a7d46bcde86c90e49284eb15"),n=-rs*BigInt("0xe4437ed6010e88286f547fa90abfe4c3"),i=BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),o=r,c=BigInt("0x100000000000000000000000000000000"),f=ct(o*e,t),u=ct(-n*e,t);let h=d.mod(e-f*r-u*i,t),a=d.mod(-f*n-u*o,t);const w=h>c,b=a>c;if(w&&(h=t-h),b&&(a=t-a),h>c||a>c)throw new Error("splitScalar: Endomorphism failed, k="+e);return{k1neg:w,k1:h,k2neg:b,k2:a}}}},Ve.sha256,s),Et=s=>{if(!s||s.recovery===void 0||s.recovery===null)throw new Error("Invalid signature");const e=s.toCompactRawBytes(),t=new Uint8Array(e.length+1);return t.set(e),t[t.length-1]=s.recovery,t},ss=["P-256","P-384","P-521","secp256k1"],is=["ES256","ES384","ES512","ES256K"],y=s=>s instanceof Error?s.message:String(s),xt=s=>{try{return s.utils.randomPrivateKey()}catch(e){throw console.log(y(e)),new Error("Failed to generate random private key")}},pt=(s,e,t,r=!0)=>{if(!r)throw new Error("Uncompressed public key is not supported");try{if(t.length===e*2){const n=t.slice(0,e),i=t.slice(e),o=s.getPublicKey(n);if(g.compareUint8Arrays(o,i))return o;throw new Error("Embedded public key is invalid")}return s.getPublicKey(t)}catch(n){throw console.log(y(n)),new Error("Failed to get public key")}},St=(s,e,{message:t,privateKey:r,recovered:n=!1})=>{if(n)throw new Error("Recovered signature is not supported");try{const i=g.isUint8Array(t)?g.ensureUint8Array(t):t,o=r.length===e*2?r.slice(0,e):r;return s.sign(i,o)}catch(i){throw console.log(y(i)),new Error("Failed to sign message")}},At=(s,{signature:e,message:t,publicKey:r})=>{try{const n=g.isUint8Array(t)?g.ensureUint8Array(t):t;return s.verify(e,n,r)}catch(n){return console.log(y(n)),!1}},Le=(s,e,t)=>{try{if(t.byteLength!==e)throw new Error(`Invalid public key byte length: ${t.byteLength}, expected ${e}`);return{kty:"OKP",crv:"Ed25519",alg:"EdDSA",x:g.encodeBase64Url(t)}}catch(r){throw console.log(y(r)),new Error("Failed to convert public key to JWK")}},Nt=(s,e,t)=>{try{const r=s.getPublicKey(t);return{...Le(s,e,r),d:g.encodeBase64Url(t)}}catch(r){throw console.log(y(r)),new Error("Failed to convert private key to JWK")}},It=(s,e,t,r)=>{try{return Fe(s,e,t,r)}catch(n){throw console.log(y(n)),new Error("Failed to convert JWK to raw public key")}},Fe=(s,e,t,r)=>{if(r.kty==null)throw new Error("Missing required parameter for kty");if(r.kty!=="OKP")throw new Error(`Invalid key type: ${r.kty}, expected OKP`);if(r.crv==null)throw new Error("Missing required parameter for crv");if(r.crv!==t)throw new Error(`Invalid curve: ${r.crv}, expected ${t}`);if(r.x==null)throw new Error("Missing required parameter for x");if(typeof r.x!="string")throw new Error("Invalid parameter type for x");if(r.alg!==void 0&&r.alg!==null&&r.alg!=="EdDSA")throw new Error(`Invalid algorithm: ${r.alg}, expected EdDSA`);let n;try{n=g.decodeBase64Url(r.x)}catch{throw new Error("Malformed encoding for x")}if(n.length!==e)throw new Error(`Invalid the length of the key data for x: ${n.length}, expected ${e}`);return n},Ut=(s,e,t,r)=>{try{return Vt(s,e,t,r)}catch(n){throw console.log(y(n)),new Error("Failed to convert JWK to raw private key")}},Vt=(s,e,t,r)=>{const n=Fe(s,e,t,r);if(r.d==null)throw new Error("Missing required parameter for d");if(typeof r.d!="string")throw new Error("Invalid parameter type for d");let i;try{i=g.decodeBase64Url(r.d)}catch{throw new Error("Malformed encoding for d")}if(i.length!==e)throw new Error(`Invalid the length of the key data for d: ${i.length}, expected ${e}`);if(!g.compareUint8Arrays(s.getPublicKey(i),n))throw new Error("The public key derived from the private key does not match the public key in the JWK");return i};class Ct{constructor({curve:e,randomBytes:t,curveName:r,signatureAlgorithmName:n,keyByteLength:i}){this.randomPrivateKey=()=>xt(this.curve),this.getPublicKey=(o,c=!0)=>pt(this.curve,this.keyByteLength,o,c),this.sign=({message:o,privateKey:c,recovered:f=!1})=>St(this.curve,this.keyByteLength,{message:o,privateKey:c,recovered:f}),this.verify=({signature:o,message:c,publicKey:f})=>At(this.curve,{signature:o,message:c,publicKey:f}),this.recoverPublicKey=o=>{throw new Error("Public key recovery is not supported")},this.toJwkPrivateKey=o=>Nt(this.curve,this.keyByteLength,o),this.toJwkPublicKey=o=>Le(this.curve,this.keyByteLength,o),this.toRawPrivateKey=o=>Ut(this.curve,this.keyByteLength,this.curveName,o),this.toRawPublicKey=o=>It(this.curve,this.keyByteLength,this.curveName,o),this.curve=e,this.randomBytes=t,this.curveName=r,this.keyByteLength=i,this.signatureAlgorithmName=n}getCurve(){return this.curve}}const Rt=s=>{try{return s.utils.randomPrivateKey()}catch(e){throw console.log(y(e)),new Error("Failed to generate random private key")}},Tt=(s,e,t=!0)=>{try{return s.getPublicKey(e,t)}catch(r){throw console.log(y(r)),new Error("Failed to get public key")}},_t=(s,{message:e,privateKey:t,recovered:r=!1})=>{try{const n=g.isUint8Array(e)?g.ensureUint8Array(e):e,i=s.sign(n,t,{prehash:!0});return r?Et(i):i.toCompactRawBytes()}catch(n){throw console.log(y(n)),new Error("Failed to sign message")}},Ht=(s,{signature:e,message:t,publicKey:r})=>{try{const n=g.isUint8Array(t)?g.ensureUint8Array(t):t,i=Te(s,e);return s.verify(i,n,r,{prehash:!0})}catch(n){return console.log(y(n)),!1}},Mt=(s,{privateKey:e,publicKey:t})=>{try{const r=s.getSharedSecret(e,t).slice(1);if(r.some(n=>n!==0))return r;throw new Error("Shared secret is zero")}catch(r){throw console.log(y(r)),new Error("Failed to compute shared secret")}},Oe=(s,e,t,r,n)=>{try{const o=s.ProjectivePoint.fromHex(n).toRawBytes(!1);if(o.length!==e*2+1)throw new Error(`Invalid uncompressed public key length: ${o.length}, expected ${e*2+1}`);const c=o.slice(1,1+e),f=o.slice(1+e);return{kty:"EC",crv:t,alg:r,x:g.encodeBase64Url(c),y:g.encodeBase64Url(f)}}catch(i){throw console.log(y(i)),new Error("Failed to convert public key to JWK")}},Lt=(s,e,t,r,n)=>{try{const i=s.getPublicKey(n);return{...Oe(s,e,t,r,i),d:g.encodeBase64Url(n)}}catch(i){throw console.log(y(i)),new Error("Failed to convert private key to JWK")}},Ft=(s,e,t,r,n)=>{try{return Ot(s,e,t,r,n)}catch(i){throw console.log(y(i)),new Error("Failed to convert JWK to raw public key")}},Ot=(s,e,t,r,n)=>{if(n.kty===void 0||n.kty===null)throw new Error("Missing required parameter for kty");if(n.kty!=="EC")throw new Error(`Invalid key type: ${n.kty}, expected EC`);if(n.crv==null)throw new Error("Missing required parameter for crv");if(n.crv!==t)throw new Error(`Invalid curve: ${n.crv}, expected ${t}`);if(n.x==null)throw new Error("Missing required parameter for x");if(typeof n.x!="string")throw new Error("Invalid parameter type for x");if(n.y==null)throw new Error("Missing required parameter for y");if(typeof n.y!="string")throw new Error("Invalid parameter type for y");if(n.alg!=null&&n.alg!==r)throw new Error(`Invalid algorithm: ${n.alg}, expected ${r}`);let i;try{i=g.decodeBase64Url(n.x)}catch{throw new Error("Malformed encoding for x")}if(i.length!==e)throw new Error(`Invalid the length of the key data for x: ${i.length}, expected ${e}`);let o;try{o=g.decodeBase64Url(n.y)}catch{throw new Error("Malformed encoding for y")}if(o.length!==e)throw new Error(`Invalid the length of the key data for y: ${o.length}, expected ${e}`);return new Uint8Array([4,...i,...o])},Pt=(s,e,t,r,n)=>{try{return os(s,e,t,r,n)}catch(i){throw console.log(y(i)),new Error("Failed to convert JWK to raw private key")}},os=(s,e,t,r,n)=>{const i=Ot(s,e,t,r,n);if(n.d===void 0||n.d===null)throw new Error("Missing required parameter for d");if(typeof n.d!="string")throw new Error("Invalid parameter type for d");let o;try{o=g.decodeBase64Url(n.d)}catch{throw new Error("Malformed encoding for d")}if(o.length!==e)throw new Error(`Invalid the length of the key data for d: ${o.length}, expected ${e}`);if(!g.compareUint8Arrays(s.getPublicKey(o,!1),i))throw new Error("The public key derived from the private key does not match the public key in the JWK");return o},$t=(s,{signature:e,message:t,compressed:r=!0})=>{try{const n=g.isUint8Array(t)?g.ensureUint8Array(t):t,i=s.CURVE.hash(n);return Te(s,e).recoverPublicKey(i).toRawBytes(r)}catch(n){throw console.log(y(n)),new Error("Failed to recover public key")}};class ie{constructor({curve:e,randomBytes:t,curveName:r,signatureAlgorithmName:n,keyByteLength:i}){this.randomPrivateKey=()=>Rt(this.curve),this.getPublicKey=(o,c=!0)=>Tt(this.curve,o,c),this.sign=({message:o,privateKey:c,recovered:f=!1})=>_t(this.curve,{message:o,privateKey:c,recovered:f}),this.verify=({signature:o,message:c,publicKey:f})=>Ht(this.curve,{signature:o,message:c,publicKey:f}),this.recoverPublicKey=({signature:o,message:c,compressed:f=!0})=>$t(this.curve,{signature:o,message:c,compressed:f}),this.getSharedSecret=({privateKey:o,publicKey:c})=>Mt(this.curve,{privateKey:o,publicKey:c}),this.toJwkPrivateKey=o=>Lt(this.curve,this.keyByteLength,this.curveName,this.signatureAlgorithmName,o),this.toJwkPublicKey=o=>Oe(this.curve,this.keyByteLength,this.curveName,this.signatureAlgorithmName,o),this.toRawPrivateKey=o=>Pt(this.curve,this.keyByteLength,this.curveName,this.signatureAlgorithmName,o),this.toRawPublicKey=o=>Ft(this.curve,this.keyByteLength,this.curveName,this.signatureAlgorithmName,o),this.curve=e,this.randomBytes=t,this.keyByteLength=i,this.curveName=r,this.signatureAlgorithmName=n}getCurve(){return this.curve}}class Dt extends ie{constructor(e){super({curve:bt(e),randomBytes:e,curveName:"P-256",signatureAlgorithmName:"ES256",keyByteLength:32})}}class Jt extends ie{constructor(e){super({curve:mt(e),randomBytes:e,curveName:"P-521",signatureAlgorithmName:"ES512",keyByteLength:66})}}class Kt extends ie{constructor(e){super({curve:Bt(e),randomBytes:e,curveName:"P-384",signatureAlgorithmName:"ES384",keyByteLength:48})}}class Gt extends ie{constructor(e){super({curve:kt(e),randomBytes:e,curveName:"secp256k1",signatureAlgorithmName:"ES256K",keyByteLength:32})}}const Wt=s=>{try{return s.utils.randomPrivateKey()}catch(e){throw console.log(y(e)),new Error("Failed to generate random private key")}},qt=(s,e,t=!0)=>{if(!t)throw new Error("Uncompressed public key is not supported");try{return s.getPublicKey(e)}catch(r){throw console.log(y(r)),new Error("Failed to get public key")}},Pe=(s,e,t,r)=>{try{if(r.length!==e)throw new Error(`Invalid public key length: ${r.length}, expected ${e}`);return{kty:"OKP",crv:t,x:g.encodeBase64Url(r)}}catch(n){throw console.log(y(n)),new Error("Failed to convert public key to JWK")}},Xt=(s,e,t,r)=>{try{const n=s.getPublicKey(r);return{...Pe(s,e,t,n),d:g.encodeBase64Url(r)}}catch(n){throw console.log(y(n)),new Error("Failed to convert private key to JWK")}},Zt=(s,e,t,r)=>{try{return $e(s,e,t,r)}catch(n){throw console.log(y(n)),new Error("Failed to convert JWK to raw public key")}},$e=(s,e,t,r)=>{if(r.kty===void 0||r.kty===null)throw new Error("Missing required parameter for kty");if(r.kty!=="OKP")throw new Error(`Invalid key type: ${r.kty}, expected OKP`);if(r.crv===void 0||r.crv===null)throw new Error("Missing required parameter for crv");if(r.crv!==t)throw new Error(`Invalid curve: ${r.crv}, expected ${t}`);if(r.x==null)throw new Error("Missing required parameter for x");if(typeof r.x!="string")throw new Error("Invalid parameter type for x");let n;try{n=g.decodeBase64Url(r.x)}catch{throw new Error("Malformed encoding for x")}if(n.length!==e)throw new Error(`Invalid the length of the key data for x: ${n.length}, expected ${e}`);return n},zt=(s,e,t,r)=>{try{return Yt(s,e,t,r)}catch(n){throw console.log(y(n)),new Error("Failed to convert JWK to raw private key")}},Yt=(s,e,t,r)=>{const n=$e(s,e,t,r);if(r.d===void 0||r.d===null)throw new Error("Missing required parameter for d");if(typeof r.d!="string")throw new Error("Invalid parameter type for d");let i;try{i=g.decodeBase64Url(r.d)}catch{throw new Error("Malformed encoding for d")}if(i.length!==e)throw new Error(`Invalid the length of the key data for d: ${i.length}, expected ${e}`);if(!g.compareUint8Arrays(s.getPublicKey(i),n))throw new Error("The public key derived from the private key does not match the public key in the JWK");return i},Qt=["0000000000000000000000000000000000000000000000000000000000000000","0100000000000000000000000000000000000000000000000000000000000000","e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800","5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157","7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec","7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed","7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffee"].map(s=>g.decodeHex(s)),jt=s=>Qt.some(e=>g.compareUint8Arrays(s,e)),er=(s,e,{privateKey:t,publicKey:r})=>{try{if(e==="X25519"&&jt(r))throw new Error("Public key is a small order point");const n=s.getSharedSecret(t,r);if(n.some(i=>i!==0))return n;throw new Error("Shared secret is zero")}catch(n){throw console.log(y(n)),new Error("Failed to compute shared secret")}};class tr{constructor({curve:e,randomBytes:t,curveName:r,keyByteLength:n}){this.curve=e,this.randomBytes=t,this.curveName=r,this.keyByteLength=n}getCurve(){return this.curve}randomPrivateKey(){return Wt(this.curve)}getPublicKey(e,t=!0){return qt(this.curve,e,t)}getSharedSecret({privateKey:e,publicKey:t}){return er(this.curve,this.curveName,{privateKey:e,publicKey:t})}toJwkPrivateKey(e){return Xt(this.curve,this.keyByteLength,this.curveName,e)}toJwkPublicKey(e){return Pe(this.curve,this.keyByteLength,this.curveName,e)}toRawPrivateKey(e){return zt(this.curve,this.keyByteLength,this.curveName,e)}toRawPublicKey(e){return Zt(this.curve,this.keyByteLength,this.curveName,e)}}class cs extends tr{constructor(e){super({curve:vt(e),randomBytes:e,curveName:"X25519",keyByteLength:32})}}const fs=(s,e)=>{switch(s){case"P-256":return new Dt(e);case"P-384":return new Kt(e);case"P-521":return new Jt(e);case"secp256k1":return new Gt(e);case"X25519":return new cs(e);default:throw new Error(`Unsupported signature curve: ${s}`)}};class as extends Ct{constructor(e){super({curve:wt(e),randomBytes:e,curveName:"Ed25519",signatureAlgorithmName:"EdDSA",keyByteLength:32})}}const rr=(s,e)=>{switch(s){case"P-256":return new Dt(e);case"P-384":return new Kt(e);case"P-521":return new Jt(e);case"secp256k1":return new Gt(e);case"Ed25519":return new as(e);default:throw new Error(`Unsupported signature curve: ${s}`)}},ls=s=>{throw new Error("RNG usage is disallowed for this curve")},us=s=>rr(s,ls),hs=new TextEncoder,nr=s=>{const{crv:e,kty:t,x:r,y:n}=s;if(t!=="EC")throw new Error(`Invalid key type: ${t}, expected EC`);if(e==null)throw new Error("Missing required parameter for crv");if(r==null)throw new Error("Missing required parameter for x");if(n==null)throw new Error("Missing required parameter for y");return JSON.stringify({crv:e,kty:t,x:r,y:n},["crv","kty","x","y"])},sr=s=>{const{crv:e,kty:t,x:r}=s;if(t!=="OKP")throw new Error(`Invalid key type: ${t}, expected OKP`);if(e==null)throw new Error("Missing required parameter for crv");if(r==null)throw new Error("Missing required parameter for x");return JSON.stringify({crv:e,kty:t,x:r},["crv","kty","x"])},ir=s=>{const{kty:e}=s;if(e==="EC")return nr(s);if(e==="OKP")return sr(s);throw new Error(`Invalid key type: ${e}, expected EC or OKP`)},ds=s=>{const e=ir(s),t=hs.encode(e);return Ve.sha256(t)};/*!
* MIT License
*
* Copyright (c) 2017-2024 Peculiar Ventures, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/const gs="[object ArrayBuffer]";class v{static isArrayBuffer(e){return Object.prototype.toString.call(e)===gs}static toArrayBuffer(e){return this.isArrayBuffer(e)?e:e.byteLength===e.buffer.byteLength||e.byteOffset===0&&e.byteLength===e.buffer.byteLength?e.buffer:this.toUint8Array(e.buffer).slice(e.byteOffset,e.byteOffset+e.byteLength).buffer}static toUint8Array(e){return this.toView(e,Uint8Array)}static toView(e,t){if(e.constructor===t)return e;if(this.isArrayBuffer(e))return new t(e);if(this.isArrayBufferView(e))return new t(e.buffer,e.byteOffset,e.byteLength);throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'")}static isBufferSource(e){return this.isArrayBufferView(e)||this.isArrayBuffer(e)}static isArrayBufferView(e){return ArrayBuffer.isView(e)||e&&this.isArrayBuffer(e.buffer)}static isEqual(e,t){const r=v.toUint8Array(e),n=v.toUint8Array(t);if(r.length!==n.byteLength)return!1;for(let i=0;i<r.length;i++)if(r[i]!==n[i])return!1;return!0}static concat(...e){let t;Array.isArray(e[0])&&!(e[1]instanceof Function)||Array.isArray(e[0])&&e[1]instanceof Function?t=e[0]:e[e.length-1]instanceof Function?t=e.slice(0,e.length-1):t=e;let r=0;for(const o of t)r+=o.byteLength;const n=new Uint8Array(r);let i=0;for(const o of t){const c=this.toUint8Array(o);n.set(c,i),i+=c.length}return e[e.length-1]instanceof Function?this.toView(n,e[e.length-1]):n.buffer}}const Be="string",ws=/^[0-9a-f\s]+$/i,vs=/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,ys=/^[a-zA-Z0-9-_]+$/;class ft{static fromString(e){const t=unescape(encodeURIComponent(e)),r=new Uint8Array(t.length);for(let n=0;n<t.length;n++)r[n]=t.charCodeAt(n);return r.buffer}static toString(e){const t=v.toUint8Array(e);let r="";for(let i=0;i<t.length;i++)r+=String.fromCharCode(t[i]);return decodeURIComponent(escape(r))}}class U{static toString(e,t=!1){const r=v.toArrayBuffer(e),n=new DataView(r);let i="";for(let o=0;o<r.byteLength;o+=2){const c=n.getUint16(o,t);i+=String.fromCharCode(c)}return i}static fromString(e,t=!1){const r=new ArrayBuffer(e.length*2),n=new DataView(r);for(let i=0;i<e.length;i++)n.setUint16(i*2,e.charCodeAt(i),t);return r}}class B{static isHex(e){return typeof e===Be&&ws.test(e)}static isBase64(e){return typeof e===Be&&vs.test(e)}static isBase64Url(e){return typeof e===Be&&ys.test(e)}static ToString(e,t="utf8"){const r=v.toUint8Array(e);switch(t.toLowerCase()){case"utf8":return this.ToUtf8String(r);case"binary":return this.ToBinary(r);case"hex":return this.ToHex(r);case"base64":return this.ToBase64(r);case"base64url":return this.ToBase64Url(r);case"utf16le":return U.toString(r,!0);case"utf16":case"utf16be":return U.toString(r);default:throw new Error(`Unknown type of encoding '${t}'`)}}static FromString(e,t="utf8"){if(!e)return new ArrayBuffer(0);switch(t.toLowerCase()){case"utf8":return this.FromUtf8String(e);case"binary":return this.FromBinary(e);case"hex":return this.FromHex(e);case"base64":return this.FromBase64(e);case"base64url":return this.FromBase64Url(e);case"utf16le":return U.fromString(e,!0);case"utf16":case"utf16be":return U.fromString(e);default:throw new Error(`Unknown type of encoding '${t}'`)}}static ToBase64(e){const t=v.toUint8Array(e);if(typeof btoa<"u"){const r=this.ToString(t,"binary");return btoa(r)}else return Buffer.from(t).toString("base64")}static FromBase64(e){const t=this.formatString(e);if(!t)return new ArrayBuffer(0);if(!B.isBase64(t))throw new TypeError("Argument 'base64Text' is not Base64 encoded");return typeof atob<"u"?this.FromBinary(atob(t)):new Uint8Array(Buffer.from(t,"base64")).buffer}static FromBase64Url(e){const t=this.formatString(e);if(!t)return new ArrayBuffer(0);if(!B.isBase64Url(t))throw new TypeError("Argument 'base64url' is not Base64Url encoded");return this.FromBase64(this.Base64Padding(t.replace(/\-/g,"+").replace(/\_/g,"/")))}static ToBase64Url(e){return this.ToBase64(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/\=/g,"")}static FromUtf8String(e,t=B.DEFAULT_UTF8_ENCODING){switch(t){case"ascii":return this.FromBinary(e);case"utf8":return ft.fromString(e);case"utf16":case"utf16be":return U.fromString(e);case"utf16le":case"usc2":return U.fromString(e,!0);default:throw new Error(`Unknown type of encoding '${t}'`)}}static ToUtf8String(e,t=B.DEFAULT_UTF8_ENCODING){switch(t){case"ascii":return this.ToBinary(e);case"utf8":return ft.toString(e);case"utf16":case"utf16be":return U.toString(e);case"utf16le":case"usc2":return U.toString(e,!0);default:throw new Error(`Unknown type of encoding '${t}'`)}}static FromBinary(e){const t=e.length,r=new Uint8Array(t);for(let n=0;n<t;n++)r[n]=e.charCodeAt(n);return r.buffer}static ToBinary(e){const t=v.toUint8Array(e);let r="";for(let n=0;n<t.length;n++)r+=String.fromCharCode(t[n]);return r}static ToHex(e){const t=v.toUint8Array(e);let r="";const n=t.length;for(let i=0;i<n;i++){const o=t[i];o<16&&(r+="0"),r+=o.toString(16)}return r}static FromHex(e){let t=this.formatString(e);if(!t)return new ArrayBuffer(0);if(!B.isHex(t))throw new TypeError("Argument 'hexString' is not HEX encoded");t.length%2&&(t=`0${t}`);const r=new Uint8Array(t.length/2);for(let n=0;n<t.length;n=n+2){const i=t.slice(n,n+2);r[n/2]=parseInt(i,16)}return r.buffer}static ToUtf16String(e,t=!1){return U.toString(e,t)}static FromUtf16String(e,t=!1){return U.fromString(e,t)}static Base64Padding(e){const t=4-e.length%4;if(t<4)for(let r=0;r<t;r++)e+="=";return e}static formatString(e){return(e==null?void 0:e.replace(/[\n\r\t ]/g,""))||""}}B.DEFAULT_UTF8_ENCODING="utf8";/*!
Copyright (c) Peculiar Ventures, LLC
*/function K(s,e){let t=0;if(s.length===1)return s[0];for(let r=s.length-1;r>=0;r--)t+=s[s.length-1-r]*Math.pow(2,e*r);return t}function O(s,e,t=-1){const r=t;let n=s,i=0,o=Math.pow(2,e);for(let c=1;c<8;c++){if(s<o){let f;if(r<0)f=new ArrayBuffer(c),i=c;else{if(r<c)return new ArrayBuffer(0);f=new ArrayBuffer(r),i=r}const u=new Uint8Array(f);for(let h=c-1;h>=0;h--){const a=Math.pow(2,h*e);u[i-h-1]=Math.floor(n/a),n-=u[i-h-1]*a}return f}o*=Math.pow(2,e)}return new ArrayBuffer(0)}function pe(...s){let e=0,t=0;for(const i of s)e+=i.length;const r=new ArrayBuffer(e),n=new Uint8Array(r);for(const i of s)n.set(i,t),t+=i.length;return n}function or(){const s=new Uint8Array(this.valueHex);if(this.valueHex.byteLength>=2){const c=s[0]===255&&s[1]&128,f=s[0]===0&&(s[1]&128)===0;(c||f)&&this.warnings.push("Needlessly long format")}const e=new ArrayBuffer(this.valueHex.byteLength),t=new Uint8Array(e);for(let c=0;c<this.valueHex.byteLength;c++)t[c]=0;t[0]=s[0]&128;const r=K(t,8),n=new ArrayBuffer(this.valueHex.byteLength),i=new Uint8Array(n);for(let c=0;c<this.valueHex.byteLength;c++)i[c]=s[c];return i[0]&=127,K(i,8)-r}function bs(s){const e=s<0?s*-1:s;let t=128;for(let r=1;r<8;r++){if(e<=t){if(s<0){const o=t-e,c=O(o,8,r),f=new Uint8Array(c);return f[0]|=128,c}let n=O(e,8,r),i=new Uint8Array(n);if(i[0]&128){const o=n.slice(0),c=new Uint8Array(o);n=new ArrayBuffer(n.byteLength+1),i=new Uint8Array(n);for(let f=0;f<o.byteLength;f++)i[f+1]=c[f];i[0]=0}return n}t*=Math.pow(2,8)}return new ArrayBuffer(0)}function Bs(s,e){if(s.byteLength!==e.byteLength)return!1;const t=new Uint8Array(s),r=new Uint8Array(e);for(let n=0;n<t.length;n++)if(t[n]!==r[n])return!1;return!0}function A(s,e){const t=s.toString(10);if(e<t.length)return"";const r=e-t.length,n=new Array(r);for(let o=0;o<r;o++)n[o]="0";return n.join("").concat(t)}/*!
* Copyright (c) 2014, GMO GlobalSign
* Copyright (c) 2015-2022, Peculiar Ventures
* All rights reserved.
*
* Author 2014-2019, Yury Strozhevsky
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/function le(){if(typeof BigInt>"u")throw new Error("BigInt is not defined. Your environment doesn't implement BigInt.")}function De(s){let e=0,t=0;for(let n=0;n<s.length;n++){const i=s[n];e+=i.byteLength}const r=new Uint8Array(e);for(let n=0;n<s.length;n++){const i=s[n];r.set(new Uint8Array(i),t),t+=i.byteLength}return r.buffer}function C(s,e,t,r){return e instanceof Uint8Array?e.byteLength?t<0?(s.error="Wrong parameter: inputOffset less than zero",!1):r<0?(s.error="Wrong parameter: inputLength less than zero",!1):e.byteLength-t-r<0?(s.error="End of input reached before message was fully decoded (inconsistent offset and length values)",!1):!0:(s.error="Wrong parameter: inputBuffer has zero length",!1):(s.error="Wrong parameter: inputBuffer must be 'Uint8Array'",!1)}class Je{constructor(){this.items=[]}write(e){this.items.push(e)}final(){return De(this.items)}}const j=[new Uint8Array([1])],at="0123456789",W="",I=new ArrayBuffer(0),Ke=new Uint8Array(0),re="EndOfContent",cr="OCTET STRING",fr="BIT STRING";function R(s){var e;return e=class extends s{get valueHex(){return this.valueHexView.slice().buffer}set valueHex(r){this.valueHexView=new Uint8Array(r)}constructor(...r){var n;super(...r);const i=r[0]||{};this.isHexOnly=(n=i.isHexOnly)!==null&&n!==void 0?n:!1,this.valueHexView=i.valueHex?v.toUint8Array(i.valueHex):Ke}fromBER(r,n,i){const o=r instanceof ArrayBuffer?new Uint8Array(r):r;if(!C(this,o,n,i))return-1;const c=n+i;return this.valueHexView=o.subarray(n,c),this.valueHexView.length?(this.blockLength=i,c):(this.warnings.push("Zero buffer length"),n)}toBER(r=!1){return this.isHexOnly?r?new ArrayBuffer(this.valueHexView.byteLength):this.valueHexView.byteLength===this.valueHexView.buffer.byteLength?this.valueHexView.buffer:this.valueHexView.slice().buffer:(this.error="Flag 'isHexOnly' is not set, abort",I)}toJSON(){return{...super.toJSON(),isHexOnly:this.isHexOnly,valueHex:B.ToHex(this.valueHexView)}}},e.NAME="hexBlock",e}class P{static blockName(){return this.NAME}get valueBeforeDecode(){return this.valueBeforeDecodeView.slice().buffer}set valueBeforeDecode(e){this.valueBeforeDecodeView=new Uint8Array(e)}constructor({blockLength:e=0,error:t=W,warnings:r=[],valueBeforeDecode:n=Ke}={}){this.blockLength=e,this.error=t,this.warnings=r,this.valueBeforeDecodeView=v.toUint8Array(n)}toJSON(){return{blockName:this.constructor.NAME,blockLength:this.blockLength,error:this.error,warnings:this.warnings,valueBeforeDecode:B.ToHex(this.valueBeforeDecodeView)}}}P.NAME="baseBlock";class p extends P{fromBER(e,t,r){throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'")}toBER(e,t){throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'")}}p.NAME="valueBlock";class ar extends R(P){constructor({idBlock:e={}}={}){var t,r,n,i;super(),e?(this.isHexOnly=(t=e.isHexOnly)!==null&&t!==void 0?t:!1,this.valueHexView=e.valueHex?v.toUint8Array(e.valueHex):Ke,this.tagClass=(r=e.tagClass)!==null&&r!==void 0?r:-1,this.tagNumber=(n=e.tagNumber)!==null&&n!==void 0?n:-1,this.isConstructed=(i=e.isConstructed)!==null&&i!==void 0?i:!1):(this.tagClass=-1,this.tagNumber=-1,this.isConstructed=!1)}toBER(e=!1){let t=0;switch(this.tagClass){case 1:t|=0;break;case 2:t|=64;break;case 3:t|=128;break;case 4:t|=192;break;default:return this.error="Unknown tag class",I}if(this.isConstructed&&(t|=32),this.tagNumber<31&&!this.isHexOnly){const n=new Uint8Array(1);if(!e){let i=this.tagNumber;i&=31,t|=i,n[0]=t}return n.buffer}if(!this.isHexOnly){const n=O(this.tagNumber,7),i=new Uint8Array(n),o=n.byteLength,c=new Uint8Array(o+1);if(c[0]=t|31,!e){for(let f=0;f<o-1;f++)c[f+1]=i[f]|128;c[o]=i[o-1]}return c.buffer}const r=new Uint8Array(this.valueHexView.byteLength+1);if(r[0]=t|31,!e){const n=this.valueHexView;for(let i=0;i<n.length-1;i++)r[i+1]=n[i]|128;r[this.valueHexView.byteLength]=n[n.length-1]}return r.buffer}fromBER(e,t,r){const n=v.toUint8Array(e);if(!C(this,n,t,r))return-1;const i=n.subarray(t,t+r);if(i.length===0)return this.error="Zero buffer length",-1;switch(i[0]&192){case 0:this.tagClass=1;break;case 64:this.tagClass=2;break;case 128:this.tagClass=3;break;case 192:this.tagClass=4;break;default:return this.error="Unknown tag class",-1}this.isConstructed=(i[0]&32)===32,this.isHexOnly=!1;const c=i[0]&31;if(c!==31)this.tagNumber=c,this.blockLength=1;else{let f=1,u=this.valueHexView=new Uint8Array(255),h=255;for(;i[f]&128;){if(u[f-1]=i[f]&127,f++,f>=i.length)return this.error="End of input reached before message was fully decoded",-1;if(f===h){h+=255;const w=new Uint8Array(h);for(let b=0;b<u.length;b++)w[b]=u[b];u=this.valueHexView=new Uint8Array(h)}}this.blockLength=f+1,u[f-1]=i[f]&127;const a=new Uint8Array(f);for(let w=0;w<f;w++)a[w]=u[w];u=this.valueHexView=new Uint8Array(f),u.set(a),this.blockLength<=9?this.tagNumber=K(u,7):(this.isHexOnly=!0,this.warnings.push("Tag too long, represented as hex-coded"))}if(this.tagClass===1&&this.isConstructed)switch(this.tagNumber){case 1:case 2:case 5:case 6:case 9:case 13:case 14:case 23:case 24:case 31:case 32:case 33:case 34:return this.error="Constructed encoding used for primitive type",-1}return t+this.blockLength}toJSON(){return{...super.toJSON(),tagClass:this.tagClass,tagNumber:this.tagNumber,isConstructed:this.isConstructed}}}ar.NAME="identificationBlock";class lr extends P{constructor({lenBlock:e={}}={}){var t,r,n;super(),this.isIndefiniteForm=(t=e.isIndefiniteForm)!==null&&t!==void 0?t:!1,this.longFormUsed=(r=e.longFormUsed)!==null&&r!==void 0?r:!1,this.length=(n=e.length)!==null&&n!==void 0?n:0}fromBER(e,t,r){const n=v.toUint8Array(e);if(!C(this,n,t,r))return-1;const i=n.subarray(t,t+r);if(i.length===0)return this.error="Zero buffer length",-1;if(i[0]===255)return this.error="Length block 0xFF is reserved by standard",-1;if(this.isIndefiniteForm=i[0]===128,this.isIndefiniteForm)return this.blockLength=1,t+this.blockLength;if(this.longFormUsed=!!(i[0]&128),this.longFormUsed===!1)return this.length=i[0],this.blockLength=1,t+this.blockLength;const o=i[0]&127;if(o>8)return this.error="Too big integer",-1;if(o+1>i.length)return this.error="End of input reached before message was fully decoded",-1;const c=t+1,f=n.subarray(c,c+o);return f[o-1]===0&&this.warnings.push("Needlessly long encoded length"),this.length=K(f,8),this.longFormUsed&&this.length<=127&&this.warnings.push("Unnecessary usage of long length form"),this.blockLength=o+1,t+this.blockLength}toBER(e=!1){let t,r;if(this.length>127&&(this.longFormUsed=!0),this.isIndefiniteForm)return t=new ArrayBuffer(1),e===!1&&(r=new Uint8Array(t),r[0]=128),t;if(this.longFormUsed){const n=O(this.length,8);if(n.byteLength>127)return this.error="Too big length",I;if(t=new ArrayBuffer(n.byteLength+1),e)return t;const i=new Uint8Array(n);r=new Uint8Array(t),r[0]=n.byteLength|128;for(let o=0;o<n.byteLength;o++)r[o+1]=i[o];return t}return t=new ArrayBuffer(1),e===!1&&(r=new Uint8Array(t),r[0]=this.length),t}toJSON(){return{...super.toJSON(),isIndefiniteForm:this.isIndefiniteForm,longFormUsed:this.longFormUsed,length:this.length}}}lr.NAME="lengthBlock";const l={};class E extends P{constructor({name:e=W,optional:t=!1,primitiveSchema:r,...n}={},i){super(n),this.name=e,this.optional=t,r&&(this.primitiveSchema=r),this.idBlock=new ar(n),this.lenBlock=new lr(n),this.valueBlock=i?new i(n):new p(n)}fromBER(e,t,r){const n=this.valueBlock.fromBER(e,t,this.lenBlock.isIndefiniteForm?r:this.lenBlock.length);return n===-1?(this.error=this.valueBlock.error,n):(this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.valueBlock.error.length||(this.blockLength+=this.valueBlock.blockLength),n)}toBER(e,t){const r=t||new Je;t||ur(this);const n=this.idBlock.toBER(e);if(r.write(n),this.lenBlock.isIndefiniteForm)r.write(new Uint8Array([128]).buffer),this.valueBlock.toBER(e,r),r.write(new ArrayBuffer(2));else{const i=this.valueBlock.toBER(e);this.lenBlock.length=i.byteLength;const o=this.lenBlock.toBER(e);r.write(o),r.write(i)}return t?I:r.final()}toJSON(){const e={...super.toJSON(),idBlock:this.idBlock.toJSON(),lenBlock:this.lenBlock.toJSON(),valueBlock:this.valueBlock.toJSON(),name:this.name,optional:this.optional};return this.primitiveSchema&&(e.primitiveSchema=this.primitiveSchema.toJSON()),e}toString(e="ascii"){return e==="ascii"?this.onAsciiEncoding():B.ToHex(this.toBER())}onAsciiEncoding(){const e=this.constructor.NAME,t=B.ToHex(this.valueBlock.valueBeforeDecodeView);return`${e} : ${t}`}isEqual(e){if(this===e)return!0;if(!(e instanceof this.constructor))return!1;const t=this.toBER(),r=e.toBER();return Bs(t,r)}}E.NAME="BaseBlock";function ur(s){var e;if(s instanceof l.Constructed)for(const t of s.valueBlock.value)ur(t)&&(s.lenBlock.isIndefiniteForm=!0);return!!(!((e=s.lenBlock)===null||e===void 0)&&e.isIndefiniteForm)}class hr extends E{getValue(){return this.valueBlock.value}setValue(e){this.valueBlock.value=e}constructor({value:e=W,...t}={},r){super(t,r),e&&this.fromString(e)}fromBER(e,t,r){const n=this.valueBlock.fromBER(e,t,this.lenBlock.isIndefiniteForm?r:this.lenBlock.length);return n===-1?(this.error=this.valueBlock.error,n):(this.fromBuffer(this.valueBlock.valueHexView),this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.valueBlock.error.length||(this.blockLength+=this.valueBlock.blockLength),n)}onAsciiEncoding(){return`${this.constructor.NAME} : '${this.valueBlock.value}'`}}hr.NAME="BaseStringBlock";class dr extends R(p){constructor({isHexOnly:e=!0,...t}={}){super(t),this.isHexOnly=e}}dr.NAME="PrimitiveValueBlock";var gr;class wr extends E{constructor(e={}){super(e,dr),this.idBlock.isConstructed=!1}}gr=wr;l.Primitive=gr;wr.NAME="PRIMITIVE";function ms(s,e){if(s instanceof e)return s;const t=new e;return t.idBlock=s.idBlock,t.lenBlock=s.lenBlock,t.warnings=s.warnings,t.valueBeforeDecodeView=s.valueBeforeDecodeView,t}function ue(s,e=0,t=s.length){const r=e;let n=new E({},p);const i=new P;if(!C(i,s,e,t))return n.error=i.error,{offset:-1,result:n};if(!s.subarray(e,e+t).length)return n.error="Zero buffer length",{offset:-1,result:n};let c=n.idBlock.fromBER(s,e,t);if(n.idBlock.warnings.length&&n.warnings.concat(n.idBlock.warnings),c===-1)return n.error=n.idBlock.error,{offset:-1,result:n};if(e=c,t-=n.idBlock.blockLength,c=n.lenBlock.fromBER(s,e,t),n.lenBlock.warnings.length&&n.warnings.concat(n.lenBlock.warnings),c===-1)return n.error=n.lenBlock.error,{offset:-1,result:n};if(e=c,t-=n.lenBlock.blockLength,!n.idBlock.isConstructed&&n.lenBlock.isIndefiniteForm)return n.error="Indefinite length form used for primitive encoding form",{offset:-1,result:n};let f=E;switch(n.idBlock.tagClass){case 1:if(n.idBlock.tagNumber>=37&&n.idBlock.isHexOnly===!1)return n.error="UNIVERSAL 37 and upper tags are reserved by ASN.1 standard",{offset:-1,result:n};switch(n.idBlock.tagNumber){case 0:if(n.idBlock.isConstructed&&n.lenBlock.length>0)return n.error="Type [UNIVERSAL 0] is reserved",{offset:-1,result:n};f=l.EndOfContent;break;case 1:f=l.Boolean;break;case 2:f=l.Integer;break;case 3:f=l.BitString;break;case 4:f=l.OctetString;break;case 5:f=l.Null;break;case 6:f=l.ObjectIdentifier;break;case 10:f=l.Enumerated;break;case 12:f=l.Utf8String;break;case 13:f=l.RelativeObjectIdentifier;break;case 14:f=l.TIME;break;case 15:return n.error="[UNIVERSAL 15] is reserved by ASN.1 standard",{offset:-1,result:n};case 16:f=l.Sequence;break;case 17:f=l.Set;break;case 18:f=l.NumericString;break;case 19:f=l.PrintableString;break;case 20:f=l.TeletexString;break;case 21:f=l.VideotexString;break;case 22:f=l.IA5String;break;case 23:f=l.UTCTime;break;case 24:f=l.GeneralizedTime;break;case 25:f=l.GraphicString;break;case 26:f=l.VisibleString;break;case 27:f=l.GeneralString;break;case 28:f=l.UniversalString;break;case 29:f=l.CharacterString;break;case 30:f=l.BmpString;break;case 31:f=l.DATE;break;case 32:f=l.TimeOfDay;break;case 33:f=l.DateTime;break;case 34:f=l.Duration;break;default:{const u=n.idBlock.isConstructed?new l.Constructed:new l.Primitive;u.idBlock=n.idBlock,u.lenBlock=n.lenBlock,u.warnings=n.warnings,n=u}}break;case 2:case 3:case 4:default:f=n.idBlock.isConstructed?l.Constructed:l.Primitive}return n=ms(n,f),c=n.fromBER(s,e,n.lenBlock.isIndefiniteForm?t:n.lenBlock.length),n.valueBeforeDecodeView=s.subarray(r,r+n.blockLength),{offset:c,result:n}}function vr(s){if(!s.byteLength){const e=new E({},p);return e.error="Input buffer has zero length",{offset:-1,result:e}}return ue(v.toUint8Array(s).slice(),0,s.byteLength)}function ks(s,e){return s?1:e}class H extends p{constructor({value:e=[],isIndefiniteForm:t=!1,...r}={}){super(r),this.value=e,this.isIndefiniteForm=t}fromBER(e,t,r){const n=v.toUint8Array(e);if(!C(this,n,t,r))return-1;if(this.valueBeforeDecodeView=n.subarray(t,t+r),this.valueBeforeDecodeView.length===0)return this.warnings.push("Zero buffer length"),t;let i=t;for(;ks(this.isIndefiniteForm,r)>0;){const o=ue(n,i,r);if(o.offset===-1)return this.error=o.result.error,this.warnings.concat(o.result.warnings),-1;if(i=o.offset,this.blockLength+=o.result.blockLength,r-=o.result.blockLength,this.value.push(o.result),this.isIndefiniteForm&&o.result.constructor.NAME===re)break}return this.isIndefiniteForm&&(this.value[this.value.length-1].constructor.NAME===re?this.value.pop():this.warnings.push("No EndOfContent block encoded")),i}toBER(e,t){const r=t||new Je;for(let n=0;n<this.value.length;n++)this.value[n].toBER(e,r);return t?I:r.final()}toJSON(){const e={...super.toJSON(),isIndefiniteForm:this.isIndefiniteForm,value:[]};for(const t of this.value)e.value.push(t.toJSON());return e}}H.NAME="ConstructedValueBlock";var yr;class q extends E{constructor(e={}){super(e,H),this.idBlock.isConstructed=!0}fromBER(e,t,r){this.valueBlock.isIndefiniteForm=this.lenBlock.isIndefiniteForm;const n=this.valueBlock.fromBER(e,t,this.lenBlock.isIndefiniteForm?r:this.lenBlock.length);return n===-1?(this.error=this.valueBlock.error,n):(this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.valueBlock.error.length||(this.blockLength+=this.valueBlock.blockLength),n)}onAsciiEncoding(){const e=[];for(const r of this.valueBlock.value)e.push(r.toString("ascii").split(`
`).map(n=>` ${n}`).join(`
`));const t=this.idBlock.tagClass===3?`[${this.idBlock.tagNumber}]`:this.constructor.NAME;return e.length?`${t} :
${e.join(`
`)}`:`${t} :`}}yr=q;l.Constructed=yr;q.NAME="CONSTRUCTED";class br extends p{fromBER(e,t,r){return t}toBER(e){return I}}br.override="EndOfContentValueBlock";var Br;class mr extends E{constructor(e={}){super(e,br),this.idBlock.tagClass=1,this.idBlock.tagNumber=0}}Br=mr;l.EndOfContent=Br;mr.NAME=re;var kr;class Er extends E{constructor(e={}){super(e,p),this.idBlock.tagClass=1,this.idBlock.tagNumber=5}fromBER(e,t,r){return this.lenBlock.length>0&&this.warnings.push("Non-zero length of value block for Null type"),this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.blockLength+=r,t+r>e.byteLength?(this.error="End of input reached before message was fully decoded (inconsistent offset and length values)",-1):t+r}toBER(e,t){const r=new ArrayBuffer(2);if(!e){const n=new Uint8Array(r);n[0]=5,n[1]=0}return t&&t.write(r),r}onAsciiEncoding(){return`${this.constructor.NAME}`}}kr=Er;l.Null=kr;Er.NAME="NULL";class xr extends R(p){get value(){for(const e of this.valueHexView)if(e>0)return!0;return!1}set value(e){this.valueHexView[0]=e?255:0}constructor({value:e,...t}={}){super(t),t.valueHex?this.valueHexView=v.toUint8Array(t.valueHex):this.valueHexView=new Uint8Array(1),e&&(this.value=e)}fromBER(e,t,r){const n=v.toUint8Array(e);return C(this,n,t,r)?(this.valueHexView=n.subarray(t,t+r),r>1&&this.warnings.push("Boolean value encoded in more then 1 octet"),this.isHexOnly=!0,or.call(this),this.blockLength=r,t+r):-1}toBER(){return this.valueHexView.slice()}toJSON(){return{...super.toJSON(),value:this.value}}}xr.NAME="BooleanValueBlock";var pr;class Sr extends E{getValue(){return this.valueBlock.value}setValue(e){this.valueBlock.value=e}constructor(e={}){super(e,xr),this.idBlock.tagClass=1,this.idBlock.tagNumber=1}onAsciiEncoding(){return`${this.constructor.NAME} : ${t