From: Ardis Lu Date: Mon, 28 Aug 2023 04:51:05 +0000 (-0700) Subject: Replace ethers with lower-level noble-crypto libs X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=c79ee839f9d584dba81b7d8fff5ec810c64afa61;p=alu.bz Replace ethers with lower-level noble-crypto libs --- diff --git a/functions/api.js b/functions/api.js index 2eab950..8d403da 100644 --- a/functions/api.js +++ b/functions/api.js @@ -1,5 +1,5 @@ -// From "ethers/hash" v6.1.0 -import { verifyMessage } from './verifyMessage'; +import { Signature } from './secp256k1.js'; // From noble-secp256k1 v2.0.0 +import { keccak256 } from './keccak256.js'; // From noble-hashes v1.3.2 // Same function used on the frontend, ran again to verify. async function getNftCount(address) { @@ -72,7 +72,12 @@ export async function onRequestPost({ request, env }) { return new Response('The short URL is too long (maximum length is 80 characters).', { status: 400, headers: { 'Content-Type': 'text/plain; charset=UTF-8' } }); } const message = `Sign this message to confirm you want to create https://alu.bz/${shortUrl} to redirect to ${fullUrl}.`; // Must match frontend exactly - const signingAddress = verifyMessage(message, signature); + const personalMessage = `\x19Ethereum Signed Message:\n${message.length}${message}`; // ERC-191 + const personalMessageHash = keccak256(new TextEncoder().encode(personalMessage)); + const signatureBytes = Uint8Array.from(signature.substring(2).match(/.{2}/g), v => parseInt(v, 16)); + const signatureObj = Signature.fromCompact(signatureBytes.slice(0, 64)).addRecoveryBit(signatureBytes[64] === 27 ? 0 : 1); + const signingPublicKey = signatureObj.recoverPublicKey(personalMessageHash); + const signingAddress = `0x${[...keccak256(signingPublicKey.toRawBytes(false).slice(1)).slice(-20)].map(v => v.toString(16).padStart(2, '0')).join('')}`; const nftCount = await getNftCount(signingAddress); if (!(nftCount > 0)) { return new Response('Invalid signature.', { status: 403, headers: { 'Content-Type': 'text/plain; charset=UTF-8' } }); diff --git a/functions/keccak256.js b/functions/keccak256.js new file mode 100644 index 0000000..94a2632 --- /dev/null +++ b/functions/keccak256.js @@ -0,0 +1,6 @@ +function g(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`Wrong positive integer: ${t}`)}function b(t,...e){if(!(t instanceof Uint8Array))throw new Error("Expected Uint8Array");if(e.length>0&&!e.includes(t.length))throw new Error(`Expected Uint8Array of length ${e}, not of length=${t.length}`)}function k(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function L(t,e){b(t);let n=e.outputLen;if(t.length>m&d)}:{h:Number(t>>m&d)|0,l:Number(t&d)|0}}function A(t,e=!1){let n=new Uint32Array(t.length),r=new Uint32Array(t.length);for(let s=0;st<>>32-n,B=(t,e,n)=>e<>>32-n,_=(t,e,n)=>e<>>64-n,I=(t,e,n)=>t<>>64-n;var M=t=>t instanceof Uint8Array;var E=t=>new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4));var W=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!W)throw new Error("Non little-endian hardware is not supported");function C(t){if(typeof t!="string")throw new Error(`utf8ToBytes expected string, got ${typeof t}`);return new Uint8Array(new TextEncoder().encode(t))}function w(t){if(typeof t=="string"&&(t=C(t)),!M(t))throw new Error(`expected Uint8Array, got ${typeof t}`);return t}var x=class{clone(){return this._cloneInto()}},tt={}.toString;function H(t){let e=r=>t().update(w(r)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}function U(t){let e=(r,s)=>t(s).update(w(r)).digest(),n=t({});return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=r=>t(r),e}var[N,F,X]=[[],[],[]],D=BigInt(0),h=BigInt(1),P=BigInt(2),v=BigInt(7),q=BigInt(256),z=BigInt(113);for(let t=0,e=h,n=1,r=0;t<24;t++){[n,r]=[r,(2*n+3*r)%5],N.push(2*(5*r+n)),F.push((t+1)*(t+2)/2%64);let s=D;for(let i=0;i<7;i++)e=(e<>v)*z)%q,e&P&&(s^=h<<(h<n>32?_(t,e,n):O(t,e,n),T=(t,e,n)=>n>32?I(t,e,n):B(t,e,n);function K(t,e=24){let n=new Uint32Array(10);for(let r=24-e;r<24;r++){for(let o=0;o<10;o++)n[o]=t[o]^t[o+10]^t[o+20]^t[o+30]^t[o+40];for(let o=0;o<10;o+=2){let c=(o+8)%10,p=(o+2)%10,l=n[p],u=n[p+1],j=S(l,u,1)^n[c],R=T(l,u,1)^n[c+1];for(let a=0;a<50;a+=10)t[o+a]^=j,t[o+a+1]^=R}let s=t[2],i=t[3];for(let o=0;o<24;o++){let c=F[o],p=S(s,i,c),l=T(s,i,c),u=N[o];s=t[u],i=t[u+1],t[u]=p,t[u+1]=l}for(let o=0;o<50;o+=10){for(let c=0;c<10;c++)n[c]=t[o+c];for(let c=0;c<10;c++)t[o+c]^=~n[(c+2)%10]&n[(c+4)%10]}t[0]^=G[r],t[1]^=J[r]}n.fill(0)}var y=class t extends x{constructor(e,n,r,s=!1,i=24){if(super(),this.blockLen=e,this.suffix=n,this.outputLen=r,this.enableXOF=s,this.rounds=i,this.pos=0,this.posOut=0,this.finished=!1,this.destroyed=!1,g(r),0>=this.blockLen||this.blockLen>=200)throw new Error("Sha3 supports only keccak-f1600 function");this.state=new Uint8Array(200),this.state32=E(this.state)}keccak(){K(this.state32,this.rounds),this.posOut=0,this.pos=0}update(e){k(this);let{blockLen:n,state:r}=this;e=w(e);let s=e.length;for(let i=0;i=r&&this.keccak();let o=Math.min(r-this.posOut,i-s);e.set(n.subarray(this.posOut,this.posOut+o),s),this.posOut+=o,s+=o}return e}xofInto(e){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(e)}xof(e){return g(e),this.xofInto(new Uint8Array(e))}digestInto(e){if(L(e,this),this.finished)throw new Error("digest() was already called");return this.writeInto(e),this.destroy(),e}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,this.state.fill(0)}_cloneInto(e){let{blockLen:n,suffix:r,outputLen:s,rounds:i,enableXOF:o}=this;return e||(e=new t(n,r,s,o,i)),e.state32.set(this.state32),e.pos=this.pos,e.posOut=this.posOut,e.finished=this.finished,e.rounds=i,e.suffix=r,e.outputLen=s,e.enableXOF=o,e.destroyed=this.destroyed,e}},f=(t,e,n)=>H(()=>new y(e,t,n)),st=f(6,144,224/8),it=f(6,136,256/8),ct=f(6,104,384/8),ft=f(6,72,512/8),ut=f(1,144,224/8),Q=f(1,136,256/8),ht=f(1,104,384/8),pt=f(1,72,512/8),$=(t,e,n)=>U((r={})=>new y(e,t,r.dkLen===void 0?n:r.dkLen,!0)),lt=$(31,168,128/8),at=$(31,136,256/8);export{Q as keccak256}; +/*! Bundled license information: + +@noble/hashes/esm/utils.js: + (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *) +*/ diff --git a/functions/secp256k1.js b/functions/secp256k1.js new file mode 100644 index 0000000..b60b5b6 --- /dev/null +++ b/functions/secp256k1.js @@ -0,0 +1,6 @@ +var C=2n**256n,B=C-0x1000003d1n,x=C-0x14551231950b75fc4402da1732fc9bebfn,D=0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,W=0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n,J={p:B,n:x,a:0n,b:7n,Gx:D,Gy:W},v=32,G=t=>s(s(t*t)*t+J.b),u=(t="")=>{throw new Error(t)},U=t=>typeof t=="bigint",$=t=>typeof t=="string",Y=t=>U(t)&&0nU(t)&&0n!(t instanceof Uint8Array)||typeof n=="number"&&n>0&&t.length!==n?u("Uint8Array expected"):t,N=t=>new Uint8Array(t),R=(t,n)=>_($(t)?E(t):N(t),n),s=(t,n=B)=>{let e=t%n;return e>=0n?e:n+e},M=t=>t instanceof S?t:u("Point expected"),Q,S=class t{constructor(n,e,r){this.px=n,this.py=e,this.pz=r}static fromAffine(n){return new t(n.x,n.y,1n)}static fromHex(n){n=R(n);let e,r=n[0],o=n.subarray(1),i=q(o,0,v),a=n.length;if(a===33&&[2,3].includes(r)){Y(i)||u("Point hex invalid: x not FE");let l=nt(G(i)),y=(l&1n)===1n;(r&1)===1!==y&&(l=s(-l)),e=new t(i,l,1n)}return a===65&&r===4&&(e=new t(i,q(o,v,2*v),1n)),e?e.ok():u("Point is not on curve")}static fromPrivateKey(n){return H.mul(et(n))}get x(){return this.aff().x}get y(){return this.aff().y}equals(n){let{px:e,py:r,pz:o}=this,{px:i,py:a,pz:l}=M(n),y=s(e*l),g=s(i*o),c=s(r*l),d=s(a*o);return y===g&&c===d}negate(){return new t(this.px,s(-this.py),this.pz)}double(){return this.add(this)}add(n){let{px:e,py:r,pz:o}=this,{px:i,py:a,pz:l}=M(n),{a:y,b:g}=J,c=0n,d=0n,f=0n,z=s(g*3n),p=s(e*i),m=s(r*a),b=s(o*l),P=s(e+r),h=s(i+a);P=s(P*h),h=s(p+m),P=s(P-h),h=s(e+o);let w=s(i+l);return h=s(h*w),w=s(p+b),h=s(h-w),w=s(r+o),c=s(a+l),w=s(w*c),c=s(m+b),w=s(w-c),f=s(y*h),c=s(z*b),f=s(c+f),c=s(m-f),f=s(m+f),d=s(c*f),m=s(p+p),m=s(m+p),b=s(y*b),h=s(z*h),m=s(m+b),b=s(p-b),b=s(y*b),h=s(h+b),p=s(m*h),d=s(d+p),p=s(w*h),c=s(P*c),c=s(c-p),p=s(P*m),f=s(w*f),f=s(f+p),new t(c,d,f)}mul(n,e=!0){if(!e&&n===0n)return T;if(K(n)||u("invalid scalar"),this.equals(H))return ut(n).p;let r=T,o=H;for(let i=this;n>0n;i=i.double(),n>>=1n)n&1n?r=r.add(i):e&&(o=o.add(i));return r}mulAddQUns(n,e,r){return this.mul(e,!1).add(n.mul(r,!1)).ok()}toAffine(){let{px:n,py:e,pz:r}=this;if(this.equals(T))return{x:0n,y:0n};if(r===1n)return{x:n,y:e};let o=O(r);return s(r*o)!==1n&&u("invalid inverse"),{x:s(n*o),y:s(e*o)}}assertValidity(){let{x:n,y:e}=this.aff();return(!Y(n)||!Y(e))&&u("Point invalid: x or y"),s(e*e)===G(n)?this:u("Point invalid: not on curve")}multiply(n){return this.mul(n)}aff(){return this.toAffine()}ok(){return this.assertValidity()}toHex(n=!0){let{x:e,y:r}=this.aff();return(n?(r&1n)===0n?"02":"03":"04")+k(e)+(n?"":k(r))}toRawBytes(n=!0){return E(this.toHex(n))}};S.BASE=new S(D,W,1n);S.ZERO=new S(0n,1n,0n);var{BASE:H,ZERO:T}=S,tt=(t,n)=>t.toString(16).padStart(n,"0"),X=t=>Array.from(t).map(n=>tt(n,2)).join(""),E=t=>{let n=t.length;(!$(t)||n%2)&&u("hex invalid 1");let e=N(n/2);for(let r=0;rBigInt("0x"+(X(t)||"0")),q=(t,n,e)=>Z(t.slice(n,e)),I=t=>U(t)&&t>=0n&&tX(I(t)),F=(...t)=>{let n=N(t.reduce((r,o)=>r+_(o).length,0)),e=0;return t.forEach(r=>{n.set(r,e),e+=r.length}),n},O=(t,n=B)=>{(t===0n||n<=0n)&&u("no inverse n="+t+" mod="+n);let e=s(t,n),r=n,o=0n,i=1n,a=1n,l=0n;for(;e!==0n;){let y=r/e,g=r%e,c=o-a*y,d=i-l*y;r=e,e=g,o=a,i=l,a=c,l=d}return r===1n?s(o,n):u("no inverse")},nt=t=>{let n=1n;for(let e=t,r=(B+1n)/4n;r>0n;r>>=1n)r&1n&&(n=n*e%B),e=e*e%B;return s(n*n)===t?n:u("sqrt invalid")},et=t=>(U(t)||(t=Z(R(t,v))),K(t)?t:u("private key out of range")),rt=t=>t>x>>1n;var j=class t{constructor(n,e,r){this.r=n,this.s=e,this.recovery=r,this.assertValidity()}static fromCompact(n){return n=R(n,64),new t(q(n,0,v),q(n,v,2*v))}assertValidity(){return K(this.r)&&K(this.s)?this:u()}addRecoveryBit(n){return new t(this.r,this.s,n)}hasHighS(){return rt(this.s)}recoverPublicKey(n){let{r:e,s:r,recovery:o}=this;[0,1,2,3].includes(o)||u("recovery id invalid");let i=ot(R(n,32)),a=o===2||o===3?e+x:e;a>=B&&u("q.x invalid");let l=o&1?"03":"02",y=S.fromHex(l+k(a)),g=O(a,x),c=s(-i*g,x),d=s(r*g,x);return H.mulAddQUns(y,c,d)}toCompactRawBytes(){return E(this.toCompactHex())}toCompactHex(){return k(this.r)+k(this.s)}},st=t=>{let n=t.length*8-256,e=Z(t);return n>0?e>>BigInt(n):e},ot=t=>s(st(t),x);var L=()=>typeof globalThis=="object"&&"crypto"in globalThis?globalThis.crypto:void 0,V;function it(t){t=R(t);let n=v+8;(t.length1024)&&u("expected proper params");let e=s(Z(t),x-1n)+1n;return I(e)}var ct={hexToBytes:E,bytesToHex:X,concatBytes:F,bytesToNumberBE:Z,numberToBytesBE:I,mod:s,invert:O,hmacSha256Async:async(t,...n)=>{let e=L();if(!e)return u("etc.hmacSha256Async not set");let r=e.subtle,o=await r.importKey("raw",t,{name:"HMAC",hash:{name:"SHA-256"}},!1,["sign"]);return N(await r.sign("HMAC",o,F(...n)))},hmacSha256Sync:V,hashToPrivateKey:it,randomBytes:t=>{let n=L();return n||u("crypto.getRandomValues must be defined"),n.getRandomValues(N(t))}};Object.defineProperties(ct,{hmacSha256Sync:{configurable:!1,get(){return V},set(t){V||(V=t)}}});var A=8,at=()=>{let t=[],n=256/A+1,e=H,r=e;for(let o=0;o{let n=Q||(Q=at()),e=(c,d)=>{let f=d.negate();return c?f:d},r=T,o=H,i=1+256/A,a=2**(A-1),l=BigInt(2**A-1),y=2**A,g=BigInt(A);for(let c=0;c>=g,f>a&&(f-=y,t+=1n);let z=d,p=d+Math.abs(f)-1,m=c%2!==0,b=f<0;f===0?o=o.add(e(m,n[z])):r=r.add(e(b,n[p]))}return{p:r,f:o}};export{j as Signature}; +/*! Bundled license information: + +@noble/secp256k1/index.js: + (*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) *) +*/ diff --git a/functions/verifyMessage.js b/functions/verifyMessage.js deleted file mode 100644 index 9e3234e..0000000 --- a/functions/verifyMessage.js +++ /dev/null @@ -1,10 +0,0 @@ -var br=Object.create;var xe=Object.defineProperty;var mr=Object.getOwnPropertyDescriptor;var wr=Object.getOwnPropertyNames;var Er=Object.getPrototypeOf,Ar=Object.prototype.hasOwnProperty;var Br=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var Sr=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of wr(t))!Ar.call(e,s)&&s!==r&&xe(e,s,{get:()=>t[s],enumerable:!(n=mr(t,s))||n.enumerable});return e};var kr=(e,t,r)=>(r=e!=null?br(Er(e)):{},Sr(t||!e||!e.__esModule?xe(r,"default",{value:e,enumerable:!0}):r,e));var Ge=Br(()=>{});function qt(e){if(!Number.isSafeInteger(e)||e<0)throw new Error(`Wrong positive integer: ${e}`)}function Ir(e){if(typeof e!="boolean")throw new Error(`Expected boolean, not ${e}`)}function ye(e,...t){if(!(e instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(t.length>0&&!t.includes(e.length))throw new TypeError(`Expected Uint8Array of length ${t}, not of length=${e.length}`)}function Ur(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");qt(e.outputLen),qt(e.blockLen)}function vr(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function Hr(e,t){ye(e);let r=t.outputLen;if(e.lengthnew Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4)),gt=e=>new DataView(e.buffer,e.byteOffset,e.byteLength),_=(e,t)=>e<<32-t|e>>>t,Cr=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!Cr)throw new Error("Non little-endian hardware is not supported");var Wn=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function Rr(e){if(typeof e!="string")throw new TypeError(`utf8ToBytes expected string, got ${typeof e}`);return new TextEncoder().encode(e)}function j(e){if(typeof e=="string"&&(e=Rr(e)),!(e instanceof Uint8Array))throw new TypeError(`Expected input type is Uint8Array (got ${typeof e})`);return e}var G=class{clone(){return this._cloneInto()}};function W(e){let t=n=>e().update(j(n)).digest(),r=e();return t.outputLen=r.outputLen,t.blockLen=r.blockLen,t.create=()=>e(),t}function be(e){let t=(n,s)=>e(s).update(j(n)).digest(),r=e({});return t.outputLen=r.outputLen,t.blockLen=r.blockLen,t.create=n=>e(n),t}var Rt=class extends G{constructor(t,r){super(),this.finished=!1,this.destroyed=!1,v.hash(t);let n=j(r);if(this.iHash=t.create(),!(this.iHash instanceof G))throw new TypeError("Expected instance of class which extends utils.Hash");let s=this.blockLen=this.iHash.blockLen;this.outputLen=this.iHash.outputLen;let o=new Uint8Array(s);o.set(n.length>this.iHash.blockLen?t.create().update(n).digest():n);for(let i=0;inew Rt(e,t).update(r).digest();Tt.create=(e,t)=>new Rt(e,t);function Tr(e,t,r,n){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,r,n);let s=BigInt(32),o=BigInt(4294967295),i=Number(r>>s&o),c=Number(r&o),a=n?4:0,f=n?0:4;e.setUint32(t+a,i,n),e.setUint32(t+f,c,n)}var ct=class extends G{constructor(t,r,n,s){super(),this.blockLen=t,this.outputLen=r,this.padOffset=n,this.isLE=s,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=gt(this.buffer)}update(t){v.exists(this);let{view:r,buffer:n,blockLen:s}=this;t=j(t);let o=t.length;for(let i=0;is-i&&(this.process(n,0),i=0);for(let a=i;ac.setUint32(4*f,a,o))}digest(){let{buffer:t,outputLen:r}=this;this.digestInto(t);let n=t.slice(0,r);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:r,buffer:n,length:s,finished:o,destroyed:i,pos:c}=this;return t.length=s,t.pos=c,t.finished=o,t.destroyed=i,s%r&&t.buffer.set(n),t}};var Pr=(e,t,r)=>e&t^~e&r,Or=(e,t,r)=>e&t^e&r^t&r,Fr=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]),X=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),q=new Uint32Array(64),Yt=class extends ct{constructor(){super(64,32,8,!1),this.A=X[0]|0,this.B=X[1]|0,this.C=X[2]|0,this.D=X[3]|0,this.E=X[4]|0,this.F=X[5]|0,this.G=X[6]|0,this.H=X[7]|0}get(){let{A:t,B:r,C:n,D:s,E:o,F:i,G:c,H:a}=this;return[t,r,n,s,o,i,c,a]}set(t,r,n,s,o,i,c,a){this.A=t|0,this.B=r|0,this.C=n|0,this.D=s|0,this.E=o|0,this.F=i|0,this.G=c|0,this.H=a|0}process(t,r){for(let h=0;h<16;h++,r+=4)q[h]=t.getUint32(r,!1);for(let h=16;h<64;h++){let l=q[h-15],x=q[h-2],b=_(l,7)^_(l,18)^l>>>3,B=_(x,17)^_(x,19)^x>>>10;q[h]=B+q[h-7]+b+q[h-16]|0}let{A:n,B:s,C:o,D:i,E:c,F:a,G:f,H:u}=this;for(let h=0;h<64;h++){let l=_(c,6)^_(c,11)^_(c,25),x=u+l+Pr(c,a,f)+Fr[h]+q[h]|0,B=(_(n,2)^_(n,13)^_(n,22))+Or(n,s,o)|0;u=f,f=a,a=c,c=i+x|0,i=o,o=s,s=n,n=x+B|0}n=n+this.A|0,s=s+this.B|0,o=o+this.C|0,i=i+this.D|0,c=c+this.E|0,a=a+this.F|0,f=f+this.G|0,u=u+this.H|0,this.set(n,s,o,i,c,a,f,u)}roundClean(){q.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}},me=W(()=>new Yt);var Pt=BigInt(4294967295),Qt=BigInt(32);function we(e,t=!1){return t?{h:Number(e&Pt),l:Number(e>>Qt&Pt)}:{h:Number(e>>Qt&Pt)|0,l:Number(e&Pt)|0}}function _r(e,t=!1){let r=new Uint32Array(e.length),n=new Uint32Array(e.length);for(let s=0;sBigInt(e>>>0)<>>0),Dr=(e,t,r)=>e>>>r,Vr=(e,t,r)=>e<<32-r|t>>>r,$r=(e,t,r)=>e>>>r|t<<32-r,Mr=(e,t,r)=>e<<32-r|t>>>r,Gr=(e,t,r)=>e<<64-r|t>>>r-32,jr=(e,t,r)=>e>>>r-32|t<<64-r,Kr=(e,t)=>t,Zr=(e,t)=>e,Wr=(e,t,r)=>e<>>32-r,Xr=(e,t,r)=>t<>>32-r,qr=(e,t,r)=>t<>>64-r,Yr=(e,t,r)=>e<>>64-r;function Qr(e,t,r,n){let s=(t>>>0)+(n>>>0);return{h:e+r+(s/2**32|0)|0,l:s|0}}var Jr=(e,t,r)=>(e>>>0)+(t>>>0)+(r>>>0),tn=(e,t,r,n)=>t+r+n+(e/2**32|0)|0,en=(e,t,r,n)=>(e>>>0)+(t>>>0)+(r>>>0)+(n>>>0),rn=(e,t,r,n,s)=>t+r+n+s+(e/2**32|0)|0,nn=(e,t,r,n,s)=>(e>>>0)+(t>>>0)+(r>>>0)+(n>>>0)+(s>>>0),sn=(e,t,r,n,s,o)=>t+r+n+s+o+(e/2**32|0)|0,on={fromBig:we,split:_r,toBig:zr,shrSH:Dr,shrSL:Vr,rotrSH:$r,rotrSL:Mr,rotrBH:Gr,rotrBL:jr,rotr32H:Kr,rotr32L:Zr,rotlSH:Wr,rotlSL:Xr,rotlBH:qr,rotlBL:Yr,add:Qr,add3L:Jr,add3H:tn,add4L:en,add4H:rn,add5H:sn,add5L:nn},p=on;var[cn,fn]=p.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(e=>BigInt(e))),Y=new Uint32Array(80),Q=new Uint32Array(80),bt=class extends ct{constructor(){super(128,64,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:r,Bh:n,Bl:s,Ch:o,Cl:i,Dh:c,Dl:a,Eh:f,El:u,Fh:h,Fl:l,Gh:x,Gl:b,Hh:B,Hl:U}=this;return[t,r,n,s,o,i,c,a,f,u,h,l,x,b,B,U]}set(t,r,n,s,o,i,c,a,f,u,h,l,x,b,B,U){this.Ah=t|0,this.Al=r|0,this.Bh=n|0,this.Bl=s|0,this.Ch=o|0,this.Cl=i|0,this.Dh=c|0,this.Dl=a|0,this.Eh=f|0,this.El=u|0,this.Fh=h|0,this.Fl=l|0,this.Gh=x|0,this.Gl=b|0,this.Hh=B|0,this.Hl=U|0}process(t,r){for(let E=0;E<16;E++,r+=4)Y[E]=t.getUint32(r),Q[E]=t.getUint32(r+=4);for(let E=16;E<80;E++){let P=Y[E-15]|0,L=Q[E-15]|0,Lt=p.rotrSH(P,L,1)^p.rotrSH(P,L,8)^p.shrSH(P,L,7),Zt=p.rotrSL(P,L,1)^p.rotrSL(P,L,8)^p.shrSL(P,L,7),$=Y[E-2]|0,M=Q[E-2]|0,Nt=p.rotrSH($,M,19)^p.rotrBH($,M,61)^p.shrSH($,M,6),Wt=p.rotrSL($,M,19)^p.rotrBL($,M,61)^p.shrSL($,M,6),Ct=p.add4L(Zt,Wt,Q[E-7],Q[E-16]),Xt=p.add4H(Ct,Lt,Nt,Y[E-7],Y[E-16]);Y[E]=Xt|0,Q[E]=Ct|0}let{Ah:n,Al:s,Bh:o,Bl:i,Ch:c,Cl:a,Dh:f,Dl:u,Eh:h,El:l,Fh:x,Fl:b,Gh:B,Gl:U,Hh:R,Hl:T}=this;for(let E=0;E<80;E++){let P=p.rotrSH(h,l,14)^p.rotrSH(h,l,18)^p.rotrBH(h,l,41),L=p.rotrSL(h,l,14)^p.rotrSL(h,l,18)^p.rotrBL(h,l,41),Lt=h&x^~h&B,Zt=l&b^~l&U,$=p.add5L(T,L,Zt,fn[E],Q[E]),M=p.add5H($,R,P,Lt,cn[E],Y[E]),Nt=$|0,Wt=p.rotrSH(n,s,28)^p.rotrBH(n,s,34)^p.rotrBH(n,s,39),Ct=p.rotrSL(n,s,28)^p.rotrBL(n,s,34)^p.rotrBL(n,s,39),Xt=n&o^n&c^o&c,gr=s&i^s&a^i&a;R=B|0,T=U|0,B=x|0,U=b|0,x=h|0,b=l|0,{h,l}=p.add(f|0,u|0,M|0,Nt|0),f=c|0,u=a|0,c=o|0,a=i|0,o=n|0,i=s|0;let pe=p.add3L(Nt,Ct,gr);n=p.add3H(pe,M,Wt,Xt),s=pe|0}({h:n,l:s}=p.add(this.Ah|0,this.Al|0,n|0,s|0)),{h:o,l:i}=p.add(this.Bh|0,this.Bl|0,o|0,i|0),{h:c,l:a}=p.add(this.Ch|0,this.Cl|0,c|0,a|0),{h:f,l:u}=p.add(this.Dh|0,this.Dl|0,f|0,u|0),{h,l}=p.add(this.Eh|0,this.El|0,h|0,l|0),{h:x,l:b}=p.add(this.Fh|0,this.Fl|0,x|0,b|0),{h:B,l:U}=p.add(this.Gh|0,this.Gl|0,B|0,U|0),{h:R,l:T}=p.add(this.Hh|0,this.Hl|0,R|0,T|0),this.set(n,s,o,i,c,a,f,u,h,l,x,b,B,U,R,T)}roundClean(){Y.fill(0),Q.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)}},Jt=class extends bt{constructor(){super(),this.Ah=573645204,this.Al=-64227540,this.Bh=-1621794909,this.Bl=-934517566,this.Ch=596883563,this.Cl=1867755857,this.Dh=-1774684391,this.Dl=1497426621,this.Eh=-1775747358,this.El=-1467023389,this.Fh=-1101128155,this.Fl=1401305490,this.Gh=721525244,this.Gl=746961066,this.Hh=246885852,this.Hl=-2117784414,this.outputLen=32}},te=class extends bt{constructor(){super(),this.Ah=-876896931,this.Al=-1056596264,this.Bh=1654270250,this.Bl=914150663,this.Ch=-1856437926,this.Cl=812702999,this.Dh=355462360,this.Dl=-150054599,this.Eh=1731405415,this.El=-4191439,this.Fh=-1900787065,this.Fl=1750603025,this.Gh=-619958771,this.Gl=1694076839,this.Hh=1203062813,this.Hl=-1090891868,this.outputLen=48}},Ee=W(()=>new bt),xs=W(()=>new Jt),ys=W(()=>new te);var Ae="6.1.0";function an(e,t,r){let n=t.split("|").map(o=>o.trim());for(let o=0;o>4],r+=t[e[n]&15];return r}if(typeof e=="object"&&typeof e.toJSON=="function")return ft(e.toJSON());switch(typeof e){case"boolean":case"symbol":return e.toString();case"bigint":return BigInt(e).toString();case"number":return e.toString();case"string":return JSON.stringify(e);case"object":{let t=Object.keys(e);return t.sort(),"{ "+t.map(r=>`${ft(r)}: ${ft(e[r])}`).join(", ")+" }"}}return"[ COULD NOT SERIALIZE ]"}function Se(e,t,r){{let s=[];if(r){if("message"in r||"code"in r||"name"in r)throw new Error(`value will overwrite populated values: ${ft(r)}`);for(let o in r){let i=r[o];s.push(o+"="+ft(i))}}s.push(`code=${t}`),s.push(`version=${Ae}`),s.length&&(e+=" ("+s.join(", ")+")")}let n;switch(t){case"INVALID_ARGUMENT":n=new TypeError(e);break;case"NUMERIC_FAULT":case"BUFFER_OVERRUN":n=new RangeError(e);break;default:n=new Error(e)}return Be(n,{code:t}),r&&Object.assign(n,r),n}function V(e,t,r,n){if(!e)throw Se(t,r,n)}function y(e,t,r,n){V(e,t,"INVALID_ARGUMENT",{argument:r,value:n})}var hn=["NFD","NFC","NFKD","NFKC"].reduce((e,t)=>{try{if("test".normalize(t)!=="test")throw new Error("bad");if(t==="NFD"){let r=String.fromCharCode(233).normalize("NFD"),n=String.fromCharCode(101,769);if(r!==n)throw new Error("broken")}e.push(t)}catch{}return e},[]);function ee(e){V(hn.indexOf(e)>=0,"platform missing String.prototype.normalize","UNSUPPORTED_OPERATION",{operation:"String.prototype.normalize",info:{form:e}})}function re(e,t,r){if(r==null&&(r=""),e!==t){let n=r,s="new";r&&(n+=".",s+=" "+r),V(!1,`private constructor; use ${n}from* methods`,"UNSUPPORTED_OPERATION",{operation:s})}}function Ie(e,t,r){if(e instanceof Uint8Array)return r?new Uint8Array(e):e;if(typeof e=="string"&&e.match(/^0x([0-9a-f][0-9a-f])*$/i)){let n=new Uint8Array((e.length-2)/2),s=2;for(let o=0;o>4]+ke[s&15]}return r}function K(e){return"0x"+e.map(t=>S(t).substring(2)).join("")}function J(e){return mt(e,!0)?(e.length-2)/2:k(e).length}function un(e,t,r){let n=k(e);V(t>=n.length,"padding exceeds data length","BUFFER_OVERRUN",{buffer:new Uint8Array(n),length:t,offset:t+1});let s=new Uint8Array(t);return s.fill(0),r?s.set(n,t-n.length):s.set(n,0),S(s)}function ne(e,t){return un(e,t,!0)}var Ue=BigInt(0),Is=BigInt(1),at=9007199254740991;function ot(e,t){switch(typeof e){case"bigint":return e;case"number":return y(Number.isInteger(e),"underflow",t||"value",e),y(e>=-at&&e<=at,"overflow",t||"value",e),BigInt(e);case"string":try{if(e==="")throw new Error("empty string");return e[0]==="-"&&e[1]!=="-"?-BigInt(e.substring(1)):BigInt(e)}catch(r){y(!1,`invalid BigNumberish string: ${r.message}`,t||"value",e)}}y(!1,"invalid BigNumberish value",t||"value",e)}function se(e,t){let r=ot(e,t);return V(r>=Ue,"unsigned value cannot be negative","NUMERIC_FAULT",{fault:"overflow",operation:"getUint",value:e}),r}function wt(e,t){switch(typeof e){case"bigint":return y(e>=-at&&e<=at,"overflow",t||"value",e),Number(e);case"number":return y(Number.isInteger(e),"underflow",t||"value",e),y(e>=-at&&e<=at,"overflow",t||"value",e),e;case"string":try{if(e==="")throw new Error("empty string");return wt(BigInt(e),t)}catch(r){y(!1,`invalid numeric string: ${r.message}`,t||"value",e)}}y(!1,"invalid numeric value",t||"value",e)}function Ot(e,t){let n=se(e,"value").toString(16);if(t==null)n.length%2&&(n="0"+n);else{let s=wt(t,"width");for(V(s*2>=n.length,`value exceeds width (${s} bits)`,"NUMERIC_FAULT",{operation:"toBeHex",fault:"overflow",value:e});n.length>6===2;i++)o++;return o}return e==="OVERRUN"?r.length-t-1:0}function dn(e,t,r,n,s){return e==="OVERLONG"?(y(typeof s=="number","invalid bad code point for replacement","badCodepoint",s),n.push(s),0):(n.push(65533),ve(e,t,r,n,s))}var pn=Object.freeze({error:ln,ignore:ve,replace:dn});function Et(e,t){t!=null&&(ee(t),e=e.normalize(t));let r=[];for(let n=0;n>6|192),r.push(s&63|128);else if((s&64512)==55296){n++;let o=e.charCodeAt(n);y(n>18|240),r.push(i>>12&63|128),r.push(i>>6&63|128),r.push(i&63|128)}else r.push(s>>12|224),r.push(s>>6&63|128),r.push(s&63|128)}return new Uint8Array(r)}function xn(){if(typeof self<"u")return self;if(typeof window<"u")return window;if(typeof global<"u")return global;throw new Error("unable to locate global object")}var He=xn(),ro=He.crypto||He.msCrypto;function Le(e,t){let r={sha256:me,sha512:Ee}[e];return y(r!=null,"invalid hmac algorithm","algorithm",e),Tt.create(r,t)}var Ne=!1,Ce=function(e,t,r){return Le(e,t).update(r).digest()},Re=Ce;function ht(e,t,r){let n=k(t,"key"),s=k(r,"data");return S(Re(e,n,s))}ht._=Ce;ht.lock=function(){Ne=!0};ht.register=function(e){if(Ne)throw new Error("computeHmac is locked");Re=e};Object.freeze(ht);var[Oe,Fe,_e]=[[],[],[]],yn=BigInt(0),At=BigInt(1),gn=BigInt(2),bn=BigInt(7),mn=BigInt(256),wn=BigInt(113);for(let e=0,t=At,r=1,n=0;e<24;e++){[r,n]=[n,(2*r+3*n)%5],Oe.push(2*(5*n+r)),Fe.push((e+1)*(e+2)/2%64);let s=yn;for(let o=0;o<7;o++)t=(t<>bn)*wn)%mn,t&gn&&(s^=At<<(At<r>32?p.rotlBH(e,t,r):p.rotlSH(e,t,r),Pe=(e,t,r)=>r>32?p.rotlBL(e,t,r):p.rotlSL(e,t,r);function Bn(e,t=24){let r=new Uint32Array(10);for(let n=24-t;n<24;n++){for(let i=0;i<10;i++)r[i]=e[i]^e[i+10]^e[i+20]^e[i+30]^e[i+40];for(let i=0;i<10;i+=2){let c=(i+8)%10,a=(i+2)%10,f=r[a],u=r[a+1],h=Te(f,u,1)^r[c],l=Pe(f,u,1)^r[c+1];for(let x=0;x<50;x+=10)e[i+x]^=h,e[i+x+1]^=l}let s=e[2],o=e[3];for(let i=0;i<24;i++){let c=Fe[i],a=Te(s,o,c),f=Pe(s,o,c),u=Oe[i];s=e[u],o=e[u+1],e[u]=a,e[u+1]=f}for(let i=0;i<50;i+=10){for(let c=0;c<10;c++)r[c]=e[i+c];for(let c=0;c<10;c++)e[i+c]^=~r[(c+2)%10]&r[(c+4)%10]}e[0]^=En[n],e[1]^=An[n]}r.fill(0)}var ut=class extends G{constructor(t,r,n,s=!1,o=24){if(super(),this.blockLen=t,this.suffix=r,this.outputLen=n,this.enableXOF=s,this.rounds=o,this.pos=0,this.posOut=0,this.finished=!1,this.destroyed=!1,v.number(n),0>=this.blockLen||this.blockLen>=200)throw new Error("Sha3 supports only keccak-f1600 function");this.state=new Uint8Array(200),this.state32=ge(this.state)}keccak(){Bn(this.state32,this.rounds),this.posOut=0,this.pos=0}update(t){v.exists(this);let{blockLen:r,state:n}=this;t=j(t);let s=t.length;for(let o=0;o=n&&this.keccak();let i=Math.min(n-this.posOut,o-s);t.set(r.subarray(this.posOut,this.posOut+i),s),this.posOut+=i,s+=i}return t}xofInto(t){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(t)}xof(t){return v.number(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(v.output(t,this),this.finished)throw new Error("digest() was already called");return this.writeInto(t),this.destroy(),t}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,this.state.fill(0)}_cloneInto(t){let{blockLen:r,suffix:n,outputLen:s,rounds:o,enableXOF:i}=this;return t||(t=new ut(r,n,s,i,o)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=o,t.suffix=n,t.outputLen=s,t.enableXOF=i,t.destroyed=this.destroyed,t}},tt=(e,t,r)=>W(()=>new ut(t,e,r)),ho=tt(6,144,224/8),uo=tt(6,136,256/8),lo=tt(6,104,384/8),po=tt(6,72,512/8),xo=tt(1,144,224/8),ze=tt(1,136,256/8),yo=tt(1,104,384/8),go=tt(1,72,512/8),De=(e,t,r)=>be((n={})=>new ut(t,e,n.dkLen===void 0?r:n.dkLen,!0)),bo=De(31,168,128/8),mo=De(31,136,256/8);var Ve=!1,$e=function(e){return ze(e)},Me=$e;function z(e){let t=k(e,"data");return S(Me(t))}z._=$e;z.lock=function(){Ve=!0};z.register=function(e){if(Ve)throw new TypeError("keccak256 is locked");Me=e};Object.freeze(z);var Sn=kr(Ge(),1);var g=BigInt(0),A=BigInt(1),rt=BigInt(2),St=BigInt(3),je=BigInt(8),I=Object.freeze({a:g,b:BigInt(7),P:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:A,Gx:BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),Gy:BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee")}),Ke=(e,t)=>(e+t/rt)/t,Ft={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),splitScalar(e){let{n:t}=I,r=BigInt("0x3086d221a7d46bcde86c90e49284eb15"),n=-A*BigInt("0xe4437ed6010e88286f547fa90abfe4c3"),s=BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),o=r,i=BigInt("0x100000000000000000000000000000000"),c=Ke(o*e,t),a=Ke(-n*e,t),f=d(e-c*r-a*s,t),u=d(-c*n-a*o,t),h=f>i,l=u>i;if(h&&(f=t-f),l&&(u=t-u),f>i||u>i)throw new Error("splitScalarEndo: Endomorphism failed, k="+e);return{k1neg:h,k1:f,k2neg:l,k2:u}}},D=32,pt=32,kn=32,Vt=D+1,$t=2*D+1;function Ze(e){let{a:t,b:r}=I,n=d(e*e),s=d(n*e);return d(s+t*e+r)}var _t=I.a===g,Mt=class extends Error{constructor(t){super(t)}};function We(e){if(!(e instanceof m))throw new TypeError("JacobianPoint expected")}var m=class{constructor(t,r,n){this.x=t,this.y=r,this.z=n}static fromAffine(t){if(!(t instanceof w))throw new TypeError("JacobianPoint#fromAffine: expected Point");return t.equals(w.ZERO)?m.ZERO:new m(t.x,t.y,A)}static toAffineBatch(t){let r=Ln(t.map(n=>n.z));return t.map((n,s)=>n.toAffine(r[s]))}static normalizeZ(t){return m.toAffineBatch(t).map(m.fromAffine)}equals(t){We(t);let{x:r,y:n,z:s}=this,{x:o,y:i,z:c}=t,a=d(s*s),f=d(c*c),u=d(r*f),h=d(o*a),l=d(d(n*c)*f),x=d(d(i*s)*a);return u===h&&l===x}negate(){return new m(this.x,d(-this.y),this.z)}double(){let{x:t,y:r,z:n}=this,s=d(t*t),o=d(r*r),i=d(o*o),c=t+o,a=d(rt*(d(c*c)-s-i)),f=d(St*s),u=d(f*f),h=d(u-rt*a),l=d(f*(a-h)-je*i),x=d(rt*r*n);return new m(h,l,x)}add(t){We(t);let{x:r,y:n,z:s}=this,{x:o,y:i,z:c}=t;if(o===g||i===g)return this;if(r===g||n===g)return t;let a=d(s*s),f=d(c*c),u=d(r*f),h=d(o*a),l=d(d(n*c)*f),x=d(d(i*s)*a),b=d(h-u),B=d(x-l);if(b===g)return B===g?this.double():m.ZERO;let U=d(b*b),R=d(b*U),T=d(u*U),E=d(B*B-R-rt*T),P=d(B*(T-E)-l*R),L=d(s*c*b);return new m(E,P,L)}subtract(t){return this.add(t.negate())}multiplyUnsafe(t){let r=m.ZERO;if(typeof t=="bigint"&&t===g)return r;let n=Ye(t);if(n===A)return this;if(!_t){let h=r,l=this;for(;n>g;)n&A&&(h=h.add(l)),l=l.double(),n>>=A;return h}let{k1neg:s,k1:o,k2neg:i,k2:c}=Ft.splitScalar(n),a=r,f=r,u=this;for(;o>g||c>g;)o&A&&(a=a.add(u)),c&A&&(f=f.add(u)),u=u.double(),o>>=A,c>>=A;return s&&(a=a.negate()),i&&(f=f.negate()),f=new m(d(f.x*Ft.beta),f.y,f.z),a.add(f)}precomputeWindow(t){let r=_t?128/t+1:256/t+1,n=[],s=this,o=s;for(let i=0;i>=h,b>a&&(b-=u,t+=A);let B=x,U=x+Math.abs(b)-1,R=l%2!==0,T=b<0;b===0?i=i.add(zt(R,s[B])):o=o.add(zt(T,s[U]))}return{p:o,f:i}}multiply(t,r){let n=Ye(t),s,o;if(_t){let{k1neg:i,k1:c,k2neg:a,k2:f}=Ft.splitScalar(n),{p:u,f:h}=this.wNAF(c,r),{p:l,f:x}=this.wNAF(f,r);u=zt(i,u),l=zt(a,l),l=new m(d(l.x*Ft.beta),l.y,l.z),s=u.add(l),o=h.add(x)}else{let{p:i,f:c}=this.wNAF(n,r);s=i,o=c}return m.normalizeZ([s,o])[0]}toAffine(t){let{x:r,y:n,z:s}=this,o=this.equals(m.ZERO);t==null&&(t=o?je:Ht(s));let i=t,c=d(i*i),a=d(c*i),f=d(r*c),u=d(n*a),h=d(s*i);if(o)return w.ZERO;if(h!==A)throw new Error("invZ was invalid");return new w(f,u)}};m.BASE=new m(I.Gx,I.Gy,A);m.ZERO=new m(g,A,g);function zt(e,t){let r=t.negate();return e?r:t}var ce=new WeakMap,w=class{constructor(t,r){this.x=t,this.y=r}_setWindowSize(t){this._WINDOW_SIZE=t,ce.delete(this)}hasEvenY(){return this.y%rt===g}static fromCompressedHex(t){let r=t.length===32,n=nt(r?t:t.subarray(1));if(!ie(n))throw new Error("Point is not on curve");let s=Ze(n),o=Hn(s),i=(o&A)===A;r?i&&(o=d(-o)):(t[0]&1)===1!==i&&(o=d(-o));let c=new w(n,o);return c.assertValidity(),c}static fromUncompressedHex(t){let r=nt(t.subarray(1,D+1)),n=nt(t.subarray(D+1,D*2+1)),s=new w(r,n);return s.assertValidity(),s}static fromHex(t){let r=It(t),n=r.length,s=r[0];if(n===D)return this.fromCompressedHex(r);if(n===Vt&&(s===2||s===3))return this.fromCompressedHex(r);if(n===$t&&s===4)return this.fromUncompressedHex(r);throw new Error(`Point.fromHex: received invalid point. Expected 32-${Vt} compressed bytes or ${$t} uncompressed bytes, not ${n}`)}static fromPrivateKey(t){return w.BASE.multiply(vt(t))}static fromSignature(t,r,n){let{r:s,s:o}=Tn(r);if(![0,1,2,3].includes(n))throw new Error("Cannot recover: invalid recovery bit");let i=Je(It(t)),{n:c}=I,a=n===2||n===3?s+c:s,f=Ht(a,c),u=d(-i*f,c),h=d(o*f,c),l=n&1?"03":"02",x=w.fromHex(l+lt(a)),b=w.BASE.multiplyAndAddUnsafe(x,u,h);if(!b)throw new Error("Cannot recover signature: point at infinify");return b.assertValidity(),b}toRawBytes(t=!1){return it(this.toHex(t))}toHex(t=!1){let r=lt(this.x);return t?`${this.hasEvenY()?"02":"03"}${r}`:`04${r}${lt(this.y)}`}toHexX(){return this.toHex(!0).slice(2)}toRawX(){return this.toRawBytes(!0).slice(1)}assertValidity(){let t="Point is not on elliptic curve",{x:r,y:n}=this;if(!ie(r)||!ie(n))throw new Error(t);let s=d(n*n),o=Ze(r);if(d(s-o)!==g)throw new Error(t)}equals(t){return this.x===t.x&&this.y===t.y}negate(){return new w(this.x,d(-this.y))}double(){return m.fromAffine(this).double().toAffine()}add(t){return m.fromAffine(this).add(m.fromAffine(t)).toAffine()}subtract(t){return this.add(t.negate())}multiply(t){return m.fromAffine(this).multiply(t,this).toAffine()}multiplyAndAddUnsafe(t,r,n){let s=m.fromAffine(this),o=r===g||r===A||this!==w.BASE?s.multiplyUnsafe(r):s.multiply(r),i=m.fromAffine(t).multiplyUnsafe(n),c=o.add(i);return c.equals(m.ZERO)?void 0:c.toAffine()}};w.BASE=new w(I.Gx,I.Gy);w.ZERO=new w(g,g);function Xe(e){return Number.parseInt(e[0],16)>=8?"00"+e:e}function qe(e){if(e.length<2||e[0]!==2)throw new Error(`Invalid signature integer tag: ${xt(e)}`);let t=e[1],r=e.subarray(2,t+2);if(!t||r.length!==t)throw new Error("Invalid signature integer: wrong length");if(r[0]===0&&r[1]<=127)throw new Error("Invalid signature integer: trailing length");return{data:nt(r),left:e.subarray(t+2)}}function In(e){if(e.length<2||e[0]!=48)throw new Error(`Invalid signature tag: ${xt(e)}`);if(e[1]!==e.length-2)throw new Error("Invalid signature: incorrect length");let{data:t,left:r}=qe(e.subarray(2)),{data:n,left:s}=qe(r);if(s.length)throw new Error(`Invalid signature: left bytes after parsing: ${xt(s)}`);return{r:t,s:n}}var C=class{constructor(t,r){this.r=t,this.s=r,this.assertValidity()}static fromCompact(t){let r=t instanceof Uint8Array,n="Signature.fromCompact";if(typeof t!="string"&&!r)throw new TypeError(`${n}: Expected string or Uint8Array`);let s=r?xt(t):t;if(s.length!==128)throw new Error(`${n}: Expected 64-byte hex`);return new C(Gt(s.slice(0,64)),Gt(s.slice(64,128)))}static fromDER(t){let r=t instanceof Uint8Array;if(typeof t!="string"&&!r)throw new TypeError("Signature.fromDER: Expected string or Uint8Array");let{r:n,s}=In(r?t:it(t));return new C(n,s)}static fromHex(t){return this.fromDER(t)}assertValidity(){let{r:t,s:r}=this;if(!Ut(t))throw new Error("Invalid Signature: r must be 0 < r < n");if(!Ut(r))throw new Error("Invalid Signature: s must be 0 < s < n")}hasHighS(){let t=I.n>>A;return this.s>t}normalizeS(){return this.hasHighS()?new C(this.r,d(-this.s,I.n)):this}toDERRawBytes(){return it(this.toDERHex())}toDERHex(){let t=Xe(Bt(this.s)),r=Xe(Bt(this.r)),n=t.length/2,s=r.length/2,o=Bt(n),i=Bt(s);return`30${Bt(s+n+4)}02${i}${r}02${o}${t}`}toRawBytes(){return this.toDERRawBytes()}toHex(){return this.toDERHex()}toCompactRawBytes(){return it(this.toCompactHex())}toCompactHex(){return lt(this.r)+lt(this.s)}};function et(...e){if(!e.every(n=>n instanceof Uint8Array))throw new Error("Uint8Array list expected");if(e.length===1)return e[0];let t=e.reduce((n,s)=>n+s.length,0),r=new Uint8Array(t);for(let n=0,s=0;nt.toString(16).padStart(2,"0"));function xt(e){if(!(e instanceof Uint8Array))throw new Error("Expected Uint8Array");let t="";for(let r=0;r0)return BigInt(e);if(typeof e=="bigint"&&Ut(e))return e;throw new TypeError("Expected valid private scalar: 0 < scalar < curve.n")}function d(e,t=I.P){let r=e%t;return r>=g?r:t+r}function O(e,t){let{P:r}=I,n=e;for(;t-- >g;)n*=n,n%=r;return n}function Hn(e){let{P:t}=I,r=BigInt(6),n=BigInt(11),s=BigInt(22),o=BigInt(23),i=BigInt(44),c=BigInt(88),a=e*e*e%t,f=a*a*e%t,u=O(f,St)*f%t,h=O(u,St)*f%t,l=O(h,rt)*a%t,x=O(l,n)*l%t,b=O(x,s)*x%t,B=O(b,i)*b%t,U=O(B,c)*B%t,R=O(U,i)*b%t,T=O(R,St)*f%t,E=O(T,o)*x%t,P=O(E,r)*a%t,L=O(P,rt);if(L*L%t!==e)throw new Error("Cannot find square root");return L}function Ht(e,t=I.P){if(e===g||t<=g)throw new Error(`invert: expected positive integers, got n=${e} mod=${t}`);let r=d(e,t),n=t,s=g,o=A,i=A,c=g;for(;r!==g;){let f=n/r,u=n%r,h=s-i*f,l=o-c*f;n=r,r=u,s=i,o=c,i=h,c=l}if(n!==A)throw new Error("invert: does not exist");return d(s,t)}function Ln(e,t=I.P){let r=new Array(e.length),n=e.reduce((o,i,c)=>i===g?o:(r[c]=o,d(o*i,t)),A),s=Ht(n,t);return e.reduceRight((o,i,c)=>i===g?o:(r[c]=d(o*r[c],t),d(o*i,t)),s),r}function Nn(e){let t=e.length*8-pt*8,r=nt(e);return t>0?r>>BigInt(t):r}function Je(e,t=!1){let r=Nn(e);if(t)return r;let{n}=I;return r>=n?r-n:r}var dt,kt,ae=class{constructor(t,r){if(this.hashLen=t,this.qByteLen=r,typeof t!="number"||t<2)throw new Error("hashLen must be a number");if(typeof r!="number"||r<2)throw new Error("qByteLen must be a number");this.v=new Uint8Array(t).fill(1),this.k=new Uint8Array(t).fill(0),this.counter=0}hmac(...t){return Z.hmacSha256(this.k,...t)}hmacSync(...t){return kt(this.k,...t)}checkSync(){if(typeof kt!="function")throw new Mt("hmacSha256Sync needs to be set")}incr(){if(this.counter>=1e3)throw new Error("Tried 1,000 k values for sign(), all were invalid");this.counter+=1}async reseed(t=new Uint8Array){this.k=await this.hmac(this.v,Uint8Array.from([0]),t),this.v=await this.hmac(this.v),t.length!==0&&(this.k=await this.hmac(this.v,Uint8Array.from([1]),t),this.v=await this.hmac(this.v))}reseedSync(t=new Uint8Array){this.checkSync(),this.k=this.hmacSync(this.v,Uint8Array.from([0]),t),this.v=this.hmacSync(this.v),t.length!==0&&(this.k=this.hmacSync(this.v,Uint8Array.from([1]),t),this.v=this.hmacSync(this.v))}async generate(){this.incr();let t=0,r=[];for(;t0)t=BigInt(e);else if(typeof e=="string"){if(e.length!==2*pt)throw new Error("Expected 32 bytes of private key");t=Gt(e)}else if(e instanceof Uint8Array){if(e.length!==pt)throw new Error("Expected 32 bytes of private key");t=nt(e)}else throw new TypeError("Expected valid private key");if(!Ut(t))throw new Error("Expected private key: 0 < key < n");return t}function Rn(e){return e instanceof w?(e.assertValidity(),e):w.fromHex(e)}function Tn(e){if(e instanceof C)return e.assertValidity(),e;try{return C.fromDER(e)}catch{return C.fromCompact(e)}}function tr(e,t=!1){return w.fromPrivateKey(e).toRawBytes(t)}function er(e,t,r,n=!1){return w.fromSignature(e,t,r).toRawBytes(n)}function Qe(e){let t=e instanceof Uint8Array,r=typeof e=="string",n=(t||r)&&e.length;return t?n===Vt||n===$t:r?n===Vt*2||n===$t*2:e instanceof w}function rr(e,t,r=!1){if(Qe(e))throw new TypeError("getSharedSecret: first arg must be private key");if(!Qe(t))throw new TypeError("getSharedSecret: second arg must be public key");let n=Rn(t);return n.assertValidity(),n.multiply(vt(e)).toRawBytes(r)}function nr(e){let t=e.length>D?e.slice(0,D):e;return nt(t)}function Pn(e){let t=nr(e),r=d(t,I.n);return sr(r{e=It(e);let t=pt+8;if(e.length1024)throw new Error("Expected valid bytes of private key as per FIPS 186");let r=d(nt(e),I.n-A)+A;return fe(r)},randomBytes:(e=32)=>{if(N.web)return N.web.getRandomValues(new Uint8Array(e));if(N.node){let{randomBytes:t}=N.node;return Uint8Array.from(t(e))}else throw new Error("The environment doesn't have randomBytes function")},randomPrivateKey:()=>Z.hashToPrivateKey(Z.randomBytes(pt+8)),precompute(e=8,t=w.BASE){let r=t===w.BASE?t:new w(t.x,t.y);return r._setWindowSize(e),r.multiply(St),r},sha256:async(...e)=>{if(N.web){let t=await N.web.subtle.digest("SHA-256",et(...e));return new Uint8Array(t)}else if(N.node){let{createHash:t}=N.node,r=t("sha256");return e.forEach(n=>r.update(n)),Uint8Array.from(r.digest())}else throw new Error("The environment doesn't have sha256 function")},hmacSha256:async(e,...t)=>{if(N.web){let r=await N.web.subtle.importKey("raw",e,{name:"HMAC",hash:{name:"SHA-256"}},!1,["sign"]),n=et(...t),s=await N.web.subtle.sign("HMAC",r,n);return new Uint8Array(s)}else if(N.node){let{createHmac:r}=N.node,n=r("sha256",e);return t.forEach(s=>n.update(s)),Uint8Array.from(n.digest())}else throw new Error("The environment doesn't have hmac-sha256 function")},sha256Sync:void 0,hmacSha256Sync:void 0,taggedHash:async(e,...t)=>{let r=Dt[e];if(r===void 0){let n=await Z.sha256(Uint8Array.from(e,s=>s.charCodeAt(0)));r=et(n,n),Dt[e]=r}return Z.sha256(r,...t)},taggedHashSync:(e,...t)=>{if(typeof dt!="function")throw new Mt("sha256Sync is undefined, you need to set it");let r=Dt[e];if(r===void 0){let n=dt(Uint8Array.from(e,s=>s.charCodeAt(0)));r=et(n,n),Dt[e]=r}return dt(r,...t)},_JacobianPoint:m};Object.defineProperties(Z,{sha256Sync:{configurable:!1,get(){return dt},set(e){dt||(dt=e)}},hmacSha256Sync:{configurable:!1,get(){return kt},set(e){kt||(kt=e)}}});var jt="0x0000000000000000000000000000000000000000000000000000000000000000";var he=`Ethereum Signed Message: -`;var ir=BigInt(0),cr=BigInt(1),fr=BigInt(2),ar=BigInt(27),hr=BigInt(28),Kt=BigInt(35),yt={};function ur(e){return ne(oe(e),32)}var H=class{#t;#r;#n;#e;get r(){return this.#t}set r(t){y(J(t)===32,"invalid r","value",t),this.#t=S(t)}get s(){return this.#r}set s(t){y(J(t)===32,"invalid r","value",t);let r=S(t);y(parseInt(r.substring(0,3))<8,"non-canonical s","value",r),this.#r=r}get v(){return this.#n}set v(t){let r=wt(t,"value");y(r===27||r===28,"invalid v","v",t),this.#n=r}get networkV(){return this.#e}get legacyChainId(){let t=this.networkV;return t==null?null:H.getChainId(t)}get yParity(){return this.v===27?0:1}get yParityAndS(){let t=k(this.s);return this.yParity&&(t[0]|=128),S(t)}get compactSerialized(){return K([this.r,this.yParityAndS])}get serialized(){return K([this.r,this.s,this.yParity?"0x1c":"0x1b"])}constructor(t,r,n,s){re(t,yt,"Signature"),this.#t=r,this.#r=n,this.#n=s,this.#e=null}[Symbol.for("nodejs.util.inspect.custom")](){return`Signature { r: "${this.r}", s: "${this.s}", yParity: ${this.yParity}, networkV: ${this.networkV} }`}clone(){let t=new H(yt,this.r,this.s,this.v);return this.networkV&&(t.#e=this.networkV),t}toJSON(){let t=this.networkV;return{_type:"signature",networkV:t!=null?t.toString():null,r:this.r,s:this.s,v:this.v}}static getChainId(t){let r=ot(t,"v");return r==ar||r==hr?ir:(y(r>=Kt,"invalid EIP-155 v","v",t),(r-Kt)/fr)}static getChainIdV(t,r){return ot(t)*fr+BigInt(35+r-27)}static getNormalizedV(t){let r=ot(t);return r===ir||r===ar?27:r===cr||r===hr?28:(y(r>=Kt,"invalid v","v",t),r&cr?27:28)}static from(t){function r(f,u){y(f,u,"signature",t)}if(t==null)return new H(yt,jt,jt,27);if(typeof t=="string"){let f=k(t,"signature");if(f.length===64){let u=S(f.slice(0,32)),h=f.slice(32,64),l=h[0]&128?28:27;return h[0]&=127,new H(yt,u,S(h),l)}if(f.length===65){let u=S(f.slice(0,32)),h=f.slice(32,64);r((h[0]&128)===0,"non-canonical s");let l=H.getNormalizedV(f[64]);return new H(yt,u,S(h),l)}r(!1,"invalid raw signature length")}if(t instanceof H)return t.clone();let n=t.r;r(n!=null,"missing r");let s=ur(n),o=function(f,u){if(f!=null)return ur(f);if(u!=null){r(mt(u,32),"invalid yParityAndS");let h=k(u);return h[0]&=127,S(h)}r(!1,"missing s")}(t.s,t.yParityAndS);r((k(o)[0]&128)==0,"non-canonical s");let{networkV:i,v:c}=function(f,u,h){if(f!=null){let l=ot(f);return{networkV:l>=Kt?l:void 0,v:H.getNormalizedV(l)}}if(u!=null)return r(mt(u,32),"invalid yParityAndS"),{v:k(u)[0]&128?28:27};if(h!=null){switch(h){case 0:return{v:27};case 1:return{v:28}}r(!1,"invalid yParity")}r(!1,"missing v")}(t.v,t.yParityAndS,t.yParity),a=new H(yt,s,o,c);return i&&(a.#e=i),r(!("yParity"in t&&t.yParity!==a.yParity),"yParity mismatch"),r(!("yParityAndS"in t&&t.yParityAndS!==a.yParityAndS),"yParityAndS mismatch"),a}};Z.hmacSha256Sync=function(e,...t){return k(ht("sha256",e,K(t)))};var F=class{#t;constructor(t){y(J(t)===32,"invalid private key","privateKey","[REDACTED]"),this.#t=S(t)}get privateKey(){return this.#t}get publicKey(){return F.computePublicKey(this.#t)}get compressedPublicKey(){return F.computePublicKey(this.#t,!0)}sign(t){y(J(t)===32,"invalid digest length","digest",t);let[r,n]=or(st(t),st(this.#t),{recovered:!0,canonical:!0}),s=C.fromHex(r);return H.from({r:Ot("0x"+s.r.toString(16),32),s:Ot("0x"+s.s.toString(16),32),v:n?28:27})}computeSharedSecret(t){let r=F.computePublicKey(t);return console.log(r),S(rr(st(this.#t),k(r)))}static computePublicKey(t,r){let n=k(t,"key");if(n.length===32){let o=tr(n,!!r);return S(o)}if(n.length===64){let o=new Uint8Array(65);o[0]=4,o.set(n,1),n=o}let s=w.fromHex(n);return S(s.toRawBytes(r))}static recoverPublicKey(t,r){y(J(t)===32,"invalid digest length","digest",t);let n=H.from(r),s=C.fromCompact(st(K([n.r,n.s]))).toDERRawBytes(),o=er(st(t),s,n.yParity);if(o!=null)return S(o);y(!1,"invalid signautre for digest","signature",r)}static addPoints(t,r,n){let s=w.fromHex(F.computePublicKey(t).substring(2)),o=w.fromHex(F.computePublicKey(r).substring(2));return"0x"+s.add(o).toHex(!!n)}};var zn=BigInt(0),Dn=BigInt(36);function lr(e){e=e.toLowerCase();let t=e.substring(2).split(""),r=new Uint8Array(40);for(let s=0;s<40;s++)r[s]=t[s].charCodeAt(0);let n=k(z(r));for(let s=0;s<40;s+=2)n[s>>1]>>4>=8&&(t[s]=t[s].toUpperCase()),(n[s>>1]&15)>=8&&(t[s+1]=t[s+1].toUpperCase());return"0x"+t.join("")}var ue={};for(let e=0;e<10;e++)ue[String(e)]=String(e);for(let e=0;e<26;e++)ue[String.fromCharCode(65+e)]=String(10+e);var dr=15;function Vn(e){e=e.toUpperCase(),e=e.substring(4)+e.substring(0,2)+"00";let t=e.split("").map(n=>ue[n]).join("");for(;t.length>=dr;){let n=t.substring(0,dr);t=parseInt(n,10)%97+t.substring(n.length)}let r=String(98-parseInt(t,10)%97);for(;r.length<2;)r="0"+r;return r}var $n=function(){let e={};for(let t=0;t<36;t++){let r="0123456789abcdefghijklmnopqrstuvwxyz"[t];e[r]=BigInt(t)}return e}();function Mn(e){e=e.toLowerCase();let t=zn;for(let r=0;r