UNPKG

@walletconnect/utils

Version:

Utilities for WalletConnect Protocol

4 lines • 87.7 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var To=require("detect-browser"),Te=require("@walletconnect/time"),ve=require("@walletconnect/window-getters"),Uo=require("@walletconnect/window-metadata"),_o=require("viem"),Co=require("bs58"),Lo=require("@walletconnect/relay-auth"),M=require("uint8arrays"),Po=require("@walletconnect/relay-api");function $o(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var jo=$o(Co);const st=":";function Nt(e){const[t,n]=e.split(st);return{namespace:t,reference:n}}function En(e){const{namespace:t,reference:n}=e;return[t,n].join(st)}function Ot(e){const[t,n,r]=e.split(st);return{namespace:t,reference:n,address:r}}function vn(e){const{namespace:t,reference:n,address:r}=e;return[t,n,r].join(st)}function It(e,t){const n=[];return e.forEach(r=>{const o=t(r);n.includes(o)||n.push(o)}),n}function xn(e){const{address:t}=Ot(e);return t}function Sn(e){const{namespace:t,reference:n}=Ot(e);return En({namespace:t,reference:n})}function ko(e,t){const{namespace:n,reference:r}=Nt(t);return vn({namespace:n,reference:r,address:e})}function Vo(e){return It(e,xn)}function An(e){return It(e,Sn)}function Mo(e,t=[]){const n=[];return Object.keys(e).forEach(r=>{if(t.length&&!t.includes(r))return;const o=e[r];n.push(...o.accounts)}),n}function Do(e,t=[]){const n=[];return Object.keys(e).forEach(r=>{if(t.length&&!t.includes(r))return;const o=e[r];n.push(...An(o.accounts))}),n}function qo(e,t=[]){const n=[];return Object.keys(e).forEach(r=>{if(t.length&&!t.includes(r))return;const o=e[r];n.push(...ct(r,o))}),n}function ct(e,t){return e.includes(":")?[e]:t.chains||[]}var Ho=Object.defineProperty,Fo=Object.defineProperties,Ko=Object.getOwnPropertyDescriptors,Nn=Object.getOwnPropertySymbols,zo=Object.prototype.hasOwnProperty,Zo=Object.prototype.propertyIsEnumerable,On=(e,t,n)=>t in e?Ho(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,In=(e,t)=>{for(var n in t||(t={}))zo.call(t,n)&&On(e,n,t[n]);if(Nn)for(var n of Nn(t))Zo.call(t,n)&&On(e,n,t[n]);return e},Yo=(e,t)=>Fo(e,Ko(t));const Bn="ReactNative",G={reactNative:"react-native",node:"node",browser:"browser",unknown:"unknown"},at=" ",Go=":",Rn="/",Bt=2,Wo=1e3,Tn="js";function Rt(){return typeof process<"u"&&typeof process.versions<"u"&&typeof process.versions.node<"u"}function he(){return!ve.getDocument()&&!!ve.getNavigator()&&navigator.product===Bn}function Jo(){return he()&&typeof global<"u"&&typeof(global==null?void 0:global.Platform)<"u"&&(global==null?void 0:global.Platform.OS)==="android"}function Xo(){return he()&&typeof global<"u"&&typeof(global==null?void 0:global.Platform)<"u"&&(global==null?void 0:global.Platform.OS)==="ios"}function Ye(){return!Rt()&&!!ve.getNavigator()&&!!ve.getDocument()}function xe(){return he()?G.reactNative:Rt()?G.node:Ye()?G.browser:G.unknown}function Qo(){var e;try{return he()&&typeof global<"u"&&typeof(global==null?void 0:global.Application)<"u"?(e=global.Application)==null?void 0:e.applicationId:void 0}catch{return}}function Un(e,t){const n=new URLSearchParams(e);for(const r of Object.keys(t).sort())if(t.hasOwnProperty(r)){const o=t[r];o!==void 0&&n.set(r,o)}return n.toString()}function ei(e){var t,n;const r=_n();try{return e!=null&&e.url&&r.url&&e.url!==r.url&&(console.warn(`The configured WalletConnect 'metadata.url':${e.url} differs from the actual page url:${r.url}. This is probably unintended and can lead to issues.`),e.url=r.url),(t=e?.icons)!=null&&t.length&&e.icons.length>0&&(e.icons=e.icons.filter(o=>o!=="")),Yo(In(In({},r),e),{url:e?.url||r.url,name:e?.name||r.name,description:e?.description||r.description,icons:(n=e?.icons)!=null&&n.length&&e.icons.length>0?e.icons:r.icons})}catch(o){return console.warn("Error populating app metadata",o),e||r}}function _n(){return Uo.getWindowMetadata()||{name:"",description:"",url:"",icons:[""]}}function ti(e,t){var n;const r=xe(),o={protocol:e,version:t,env:r};return r==="browser"&&(o.host=((n=ve.getLocation())==null?void 0:n.host)||"unknown"),o}function Cn(){if(xe()===G.reactNative&&typeof global<"u"&&typeof(global==null?void 0:global.Platform)<"u"){const{OS:n,Version:r}=global.Platform;return[n,r].join("-")}const e=To.detect();if(e===null)return"unknown";const t=e.os?e.os.replace(" ","").toLowerCase():"unknown";return e.type==="browser"?[t,e.name,e.version].join("-"):[t,e.version].join("-")}function Ln(){var e;const t=xe();return t===G.browser?[t,((e=ve.getLocation())==null?void 0:e.host)||"unknown"].join(":"):t}function Pn(e,t,n){const r=Cn(),o=Ln();return[[e,t].join("-"),[Tn,n].join("-"),r,o].join("/")}function ni({protocol:e,version:t,relayUrl:n,sdkVersion:r,auth:o,projectId:i,useOnCloseEvent:s,bundleId:c,packageName:a}){const u=n.split("?"),l=Pn(e,t,r),f={auth:o,ua:l,projectId:i,useOnCloseEvent:s||void 0,packageName:a||void 0,bundleId:c||void 0},h=Un(u[1]||"",f);return u[0]+"?"+h}function ri(e){let t=(e.match(/^[^:]+(?=:\/\/)/gi)||[])[0];const n=typeof t<"u"?e.split("://")[1]:e;return t=t==="wss"?"https":"http",[t,n].join("://")}function oi(e,t,n){if(!e[t]||typeof e[t]!==n)throw new Error(`Missing or invalid "${t}" param`)}function $n(e,t=Bt){return jn(e.split(Rn),t)}function ii(e){return $n(e).join(at)}function pe(e,t){return e.filter(n=>t.includes(n)).length===e.length}function jn(e,t=Bt){return e.slice(Math.max(e.length-t,0))}function si(e){return Object.fromEntries(e.entries())}function ci(e){return new Map(Object.entries(e))}function ai(e,t){const n={};return Object.keys(e).forEach(r=>{n[r]=t(e[r])}),n}const ui=e=>e;function kn(e){return e.trim().replace(/^\w/,t=>t.toUpperCase())}function fi(e){return e.split(at).map(t=>kn(t)).join(at)}function li(e=Te.FIVE_MINUTES,t){const n=Te.toMiliseconds(e||Te.FIVE_MINUTES);let r,o,i,s;return{resolve:c=>{i&&r&&(clearTimeout(i),r(c),s=Promise.resolve(c))},reject:c=>{i&&o&&(clearTimeout(i),o(c))},done:()=>new Promise((c,a)=>{if(s)return c(s);i=setTimeout(()=>{const u=new Error(t);s=Promise.reject(u),a(u)},n),r=c,o=a})}}function di(e,t,n){return new Promise(async(r,o)=>{const i=setTimeout(()=>o(new Error(n)),t);try{const s=await e;r(s)}catch(s){o(s)}clearTimeout(i)})}function Tt(e,t){if(typeof t=="string"&&t.startsWith(`${e}:`))return t;if(e.toLowerCase()==="topic"){if(typeof t!="string")throw new Error('Value must be "string" for expirer target type: topic');return`topic:${t}`}else if(e.toLowerCase()==="id"){if(typeof t!="number")throw new Error('Value must be "number" for expirer target type: id');return`id:${t}`}throw new Error(`Unknown expirer target type: ${e}`)}function hi(e){return Tt("topic",e)}function pi(e){return Tt("id",e)}function gi(e){const[t,n]=e.split(":"),r={id:void 0,topic:void 0};if(t==="topic"&&typeof n=="string")r.topic=n;else if(t==="id"&&Number.isInteger(Number(n)))r.id=Number(n);else throw new Error(`Invalid target, expected id:number or topic:string, got ${t}:${n}`);return r}function yi(e,t){return Te.fromMiliseconds((t||Date.now())+Te.toMiliseconds(e))}function mi(e){return Date.now()>=Te.toMiliseconds(e)}function wi(e,t){return`${e}${t?`:${t}`:""}`}function ce(e=[],t=[]){return[...new Set([...e,...t])]}async function bi({id:e,topic:t,wcDeepLink:n}){var r;try{if(!n)return;const o=typeof n=="string"?JSON.parse(n):n,i=o?.href;if(typeof i!="string")return;const s=Vn(i,e,t),c=xe();if(c===G.browser){if(!((r=ve.getDocument())!=null&&r.hasFocus())){console.warn("Document does not have focus, skipping deeplink.");return}Mn(s)}else c===G.reactNative&&typeof(global==null?void 0:global.Linking)<"u"&&await global.Linking.openURL(s)}catch(o){console.error(o)}}function Vn(e,t,n){const r=`requestId=${t}&sessionTopic=${n}`;e.endsWith("/")&&(e=e.slice(0,-1));let o=`${e}`;if(e.startsWith("https://t.me")){const i=e.includes("?")?"&startapp=":"?startapp=";o=`${o}${i}${Hn(r,!0)}`}else o=`${o}/wc?${r}`;return o}function Mn(e){let t="_self";qn()?t="_top":(Dn()||e.startsWith("https://")||e.startsWith("http://"))&&(t="_blank"),window.open(e,t,"noreferrer noopener")}async function Ei(e,t){let n="";try{if(Ye()&&(n=localStorage.getItem(t),n))return n;n=await e.getItem(t)}catch(r){console.error(r)}return n}function Ut(e,t){return e.filter(n=>t.includes(n))}function vi(e,t){if(!e.includes(t))return null;const n=e.split(/([&,?,=])/),r=n.indexOf(t);return n[r+2]}function xi(){return typeof crypto<"u"&&crypto!=null&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/gu,e=>{const t=Math.random()*16|0;return(e==="x"?t:t&3|8).toString(16)})}function Si(){return typeof process<"u"&&process.env.IS_VITEST==="true"}function Dn(){return typeof window<"u"&&(!!window.TelegramWebviewProxy||!!window.Telegram||!!window.TelegramWebviewProxyProto)}function qn(){try{return window.self!==window.top}catch{return!1}}function Hn(e,t=!1){const n=Buffer.from(e).toString("base64");return t?n.replace(/[=]/g,""):n}function _t(e){return Buffer.from(e,"base64").toString("utf-8")}function Ai(e){return new Promise(t=>setTimeout(t,e))}function Ge(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function Ni(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function We(e,...t){if(!Ni(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error("Uint8Array expected of length "+t+", got length="+e.length)}function Ct(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");Ge(e.outputLen),Ge(e.blockLen)}function Ue(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 Fn(e,t){We(e);const n=t.outputLen;if(e.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}const ut=BigInt(2**32-1),Kn=BigInt(32);function Oi(e,t=!1){return t?{h:Number(e&ut),l:Number(e>>Kn&ut)}:{h:Number(e>>Kn&ut)|0,l:Number(e&ut)|0}}function Ii(e,t=!1){let n=new Uint32Array(e.length),r=new Uint32Array(e.length);for(let o=0;o<e.length;o++){const{h:i,l:s}=Oi(e[o],t);[n[o],r[o]]=[i,s]}return[n,r]}const Bi=(e,t,n)=>e<<n|t>>>32-n,Ri=(e,t,n)=>t<<n|e>>>32-n,Ti=(e,t,n)=>t<<n-32|e>>>64-n,Ui=(e,t,n)=>e<<n-32|t>>>64-n,_e=typeof globalThis=="object"&&"crypto"in globalThis?globalThis.crypto:void 0;function _i(e){return new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4))}function Lt(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function ie(e,t){return e<<32-t|e>>>t}const zn=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function Ci(e){return e<<24&4278190080|e<<8&16711680|e>>>8&65280|e>>>24&255}function Zn(e){for(let t=0;t<e.length;t++)e[t]=Ci(e[t])}function Li(e){if(typeof e!="string")throw new Error("utf8ToBytes expected string, got "+typeof e);return new Uint8Array(new TextEncoder().encode(e))}function Ce(e){return typeof e=="string"&&(e=Li(e)),We(e),e}function Pi(...e){let t=0;for(let r=0;r<e.length;r++){const o=e[r];We(o),t+=o.length}const n=new Uint8Array(t);for(let r=0,o=0;r<e.length;r++){const i=e[r];n.set(i,o),o+=i.length}return n}class Pt{clone(){return this._cloneInto()}}function Yn(e){const t=r=>e().update(Ce(r)).digest(),n=e();return t.outputLen=n.outputLen,t.blockLen=n.blockLen,t.create=()=>e(),t}function Le(e=32){if(_e&&typeof _e.getRandomValues=="function")return _e.getRandomValues(new Uint8Array(e));if(_e&&typeof _e.randomBytes=="function")return _e.randomBytes(e);throw new Error("crypto.getRandomValues must be defined")}const Gn=[],Wn=[],Jn=[],$i=BigInt(0),Je=BigInt(1),ji=BigInt(2),ki=BigInt(7),Vi=BigInt(256),Mi=BigInt(113);for(let e=0,t=Je,n=1,r=0;e<24;e++){[n,r]=[r,(2*n+3*r)%5],Gn.push(2*(5*r+n)),Wn.push((e+1)*(e+2)/2%64);let o=$i;for(let i=0;i<7;i++)t=(t<<Je^(t>>ki)*Mi)%Vi,t&ji&&(o^=Je<<(Je<<BigInt(i))-Je);Jn.push(o)}const[Di,qi]=Ii(Jn,!0),Xn=(e,t,n)=>n>32?Ti(e,t,n):Bi(e,t,n),Qn=(e,t,n)=>n>32?Ui(e,t,n):Ri(e,t,n);function Hi(e,t=24){const n=new Uint32Array(10);for(let r=24-t;r<24;r++){for(let s=0;s<10;s++)n[s]=e[s]^e[s+10]^e[s+20]^e[s+30]^e[s+40];for(let s=0;s<10;s+=2){const c=(s+8)%10,a=(s+2)%10,u=n[a],l=n[a+1],f=Xn(u,l,1)^n[c],h=Qn(u,l,1)^n[c+1];for(let y=0;y<50;y+=10)e[s+y]^=f,e[s+y+1]^=h}let o=e[2],i=e[3];for(let s=0;s<24;s++){const c=Wn[s],a=Xn(o,i,c),u=Qn(o,i,c),l=Gn[s];o=e[l],i=e[l+1],e[l]=a,e[l+1]=u}for(let s=0;s<50;s+=10){for(let c=0;c<10;c++)n[c]=e[s+c];for(let c=0;c<10;c++)e[s+c]^=~n[(c+2)%10]&n[(c+4)%10]}e[0]^=Di[r],e[1]^=qi[r]}n.fill(0)}class wn extends Pt{constructor(t,n,r,o=!1,i=24){if(super(),this.blockLen=t,this.suffix=n,this.outputLen=r,this.enableXOF=o,this.rounds=i,this.pos=0,this.posOut=0,this.finished=!1,this.destroyed=!1,Ge(r),0>=this.blockLen||this.blockLen>=200)throw new Error("Sha3 supports only keccak-f1600 function");this.state=new Uint8Array(200),this.state32=_i(this.state)}keccak(){zn||Zn(this.state32),Hi(this.state32,this.rounds),zn||Zn(this.state32),this.posOut=0,this.pos=0}update(t){Ue(this);const{blockLen:n,state:r}=this;t=Ce(t);const o=t.length;for(let i=0;i<o;){const s=Math.min(n-this.pos,o-i);for(let c=0;c<s;c++)r[this.pos++]^=t[i++];this.pos===n&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:n,pos:r,blockLen:o}=this;t[r]^=n,(n&128)!==0&&r===o-1&&this.keccak(),t[o-1]^=128,this.keccak()}writeInto(t){Ue(this,!1),We(t),this.finish();const n=this.state,{blockLen:r}=this;for(let o=0,i=t.length;o<i;){this.posOut>=r&&this.keccak();const s=Math.min(r-this.posOut,i-o);t.set(n.subarray(this.posOut,this.posOut+s),o),this.posOut+=s,o+=s}return t}xofInto(t){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(t)}xof(t){return Ge(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(Fn(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){const{blockLen:n,suffix:r,outputLen:o,rounds:i,enableXOF:s}=this;return t||(t=new wn(n,r,o,s,i)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=i,t.suffix=r,t.outputLen=o,t.enableXOF=s,t.destroyed=this.destroyed,t}}const Fi=(e,t,n)=>Yn(()=>new wn(t,e,n)),Ki=Fi(1,136,256/8),zi="https://rpc.walletconnect.org/v1";function $t(e){const t=`Ethereum Signed Message: ${e.length}`,n=new TextEncoder().encode(t+e);return"0x"+Buffer.from(Ki(n)).toString("hex")}async function er(e,t,n,r,o,i){switch(n.t){case"eip191":return await tr(e,t,n.s);case"eip1271":return await nr(e,t,n.s,r,o,i);default:throw new Error(`verifySignature failed: Attempted to verify CacaoSignature with unknown type: ${n.t}`)}}async function tr(e,t,n){return(await _o.recoverAddress({hash:$t(t),signature:n})).toLowerCase()===e.toLowerCase()}async function nr(e,t,n,r,o,i){const s=Nt(r);if(!s.namespace||!s.reference)throw new Error(`isValidEip1271Signature failed: chainId must be in CAIP-2 format, received: ${r}`);try{const c="0x1626ba7e",a="0000000000000000000000000000000000000000000000000000000000000040",u="0000000000000000000000000000000000000000000000000000000000000041",l=n.substring(2),f=$t(t).substring(2),h=c+f+a+u+l,y=await fetch(`${i||zi}/?chainId=${r}&projectId=${o}`,{method:"POST",body:JSON.stringify({id:Zi(),jsonrpc:"2.0",method:"eth_call",params:[{to:e,data:h},"latest"]})}),{result:E}=await y.json();return E?E.slice(0,c.length).toLowerCase()===c.toLowerCase():!1}catch(c){return console.error("isValidEip1271Signature: ",c),!1}}function Zi(){return Date.now()+Math.floor(Math.random()*1e3)}function Yi(e){const t=atob(e),n=new Uint8Array(t.length);for(let s=0;s<t.length;s++)n[s]=t.charCodeAt(s);const r=n[0];if(r===0)throw new Error("No signatures found");const o=1+r*64;if(n.length<o)throw new Error("Transaction data too short for claimed signature count");if(n.length<100)throw new Error("Transaction too short");const i=Buffer.from(e,"base64").slice(1,65);return jo.default.encode(i)}var Gi=Object.defineProperty,Wi=Object.defineProperties,Ji=Object.getOwnPropertyDescriptors,rr=Object.getOwnPropertySymbols,Xi=Object.prototype.hasOwnProperty,Qi=Object.prototype.propertyIsEnumerable,or=(e,t,n)=>t in e?Gi(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,jt=(e,t)=>{for(var n in t||(t={}))Xi.call(t,n)&&or(e,n,t[n]);if(rr)for(var n of rr(t))Qi.call(t,n)&&or(e,n,t[n]);return e},ir=(e,t)=>Wi(e,Ji(t));const es="did:pkh:",ft=e=>e?.split(":"),sr=e=>{const t=e&&ft(e);if(t)return e.includes(es)?t[3]:t[1]},cr=e=>{const t=e&&ft(e);if(t)return t[2]+":"+t[3]},kt=e=>{const t=e&&ft(e);if(t)return t.pop()};async function ts(e){const{cacao:t,projectId:n}=e,{s:r,p:o}=t,i=ar(o,o.iss),s=kt(o.iss);return await er(s,i,r,cr(o.iss),n)}const ar=(e,t)=>{const n=`${e.domain} wants you to sign in with your Ethereum account:`,r=kt(t);if(!e.aud&&!e.uri)throw new Error("Either `aud` or `uri` is required to construct the message");let o=e.statement||void 0;const i=`URI: ${e.aud||e.uri}`,s=`Version: ${e.version}`,c=`Chain ID: ${sr(t)}`,a=`Nonce: ${e.nonce}`,u=`Issued At: ${e.iat}`,l=e.exp?`Expiration Time: ${e.exp}`:void 0,f=e.nbf?`Not Before: ${e.nbf}`:void 0,h=e.requestId?`Request ID: ${e.requestId}`:void 0,y=e.resources?`Resources:${e.resources.map(p=>` - ${p}`).join("")}`:void 0,E=dt(e.resources);if(E){const p=ge(E);o=Dt(o,p)}return[n,r,"",o,"",i,s,c,a,u,l,f,h,y].filter(p=>p!=null).join(` `)};function ns(e,t,n){return n.includes("did:pkh:")||(n=`did:pkh:${n}`),{h:{t:"caip122"},p:{iss:n,domain:e.domain,aud:e.aud,version:e.version,nonce:e.nonce,iat:e.iat,statement:e.statement,requestId:e.requestId,resources:e.resources,nbf:e.nbf,exp:e.exp},s:t}}function rs(e){var t;const{authPayload:n,chains:r,methods:o}=e,i=n.statement||"";if(!(r!=null&&r.length))return n;const s=n.chains,c=Ut(s,r);if(!(c!=null&&c.length))throw new Error("No supported chains");const a=ur(n.resources);if(!a)return n;se(a);const u=fr(a,"eip155");let l=n?.resources||[];if(u!=null&&u.length){const f=lr(u),h=Ut(f,o);if(!(h!=null&&h.length))throw new Error(`Supported methods don't satisfy the requested: ${JSON.stringify(f)}, supported: ${JSON.stringify(o)}`);const y=Vt("request",h,{chains:c}),E=gr(a,"eip155",y);l=((t=n?.resources)==null?void 0:t.slice(0,-1))||[],l.push(lt(E))}return ir(jt({},n),{statement:mr(i,dt(l)),chains:c,resources:n!=null&&n.resources||l.length>0?l:void 0})}function ur(e){const t=dt(e);if(t&&Mt(t))return ge(t)}function os(e,t){var n;return(n=e?.att)==null?void 0:n.hasOwnProperty(t)}function fr(e,t){var n,r;return(n=e?.att)!=null&&n[t]?Object.keys((r=e?.att)==null?void 0:r[t]):[]}function is(e){return e?.map(t=>Object.keys(t))||[]}function lr(e){return e?.map(t=>{var n;return(n=t.split("/"))==null?void 0:n[1]})||[]}function dr(e){return Buffer.from(JSON.stringify(e)).toString("base64")}function hr(e){return JSON.parse(Buffer.from(e,"base64").toString("utf-8"))}function se(e){if(!e)throw new Error("No recap provided, value is undefined");if(!e.att)throw new Error("No `att` property found");const t=Object.keys(e.att);if(!(t!=null&&t.length))throw new Error("No resources found in `att` property");t.forEach(n=>{const r=e.att[n];if(Array.isArray(r))throw new Error(`Resource must be an object: ${n}`);if(typeof r!="object")throw new Error(`Resource must be an object: ${n}`);if(!Object.keys(r).length)throw new Error(`Resource object is empty: ${n}`);Object.keys(r).forEach(o=>{const i=r[o];if(!Array.isArray(i))throw new Error(`Ability limits ${o} must be an array of objects, found: ${i}`);if(!i.length)throw new Error(`Value of ${o} is empty array, must be an array with objects`);i.forEach(s=>{if(typeof s!="object")throw new Error(`Ability limits (${o}) must be an array of objects, found: ${s}`)})})})}function pr(e,t,n,r={}){return n?.sort((o,i)=>o.localeCompare(i)),{att:{[e]:Vt(t,n,r)}}}function gr(e,t,n){var r;e.att[t]=jt({},n);const o=(r=Object.keys(e.att))==null?void 0:r.sort((s,c)=>s.localeCompare(c)),i={att:{}};return o.reduce((s,c)=>(s.att[c]=e.att[c],s),i)}function Vt(e,t,n={}){t=t?.sort((o,i)=>o.localeCompare(i));const r=t.map(o=>({[`${e}/${o}`]:[n]}));return Object.assign({},...r)}function lt(e){return se(e),`urn:recap:${dr(e).replace(/=/g,"")}`}function ge(e){const t=hr(e.replace("urn:recap:",""));return se(t),t}function ss(e,t,n){const r=pr(e,t,n);return lt(r)}function Mt(e){return e&&e.includes("urn:recap:")}function cs(e,t){const n=ge(e),r=ge(t),o=yr(n,r);return lt(o)}function yr(e,t){se(e),se(t);const n=Object.keys(e.att).concat(Object.keys(t.att)).sort((o,i)=>o.localeCompare(i)),r={att:{}};return n.forEach(o=>{var i,s;Object.keys(((i=e.att)==null?void 0:i[o])||{}).concat(Object.keys(((s=t.att)==null?void 0:s[o])||{})).sort((c,a)=>c.localeCompare(a)).forEach(c=>{var a,u;r.att[o]=ir(jt({},r.att[o]),{[c]:((a=e.att[o])==null?void 0:a[c])||((u=t.att[o])==null?void 0:u[c])})})}),r}function Dt(e="",t){se(t);const n="I further authorize the stated URI to perform the following actions on my behalf: ";if(e.includes(n))return e;const r=[];let o=0;Object.keys(t.att).forEach(c=>{const a=Object.keys(t.att[c]).map(f=>({ability:f.split("/")[0],action:f.split("/")[1]}));a.sort((f,h)=>f.action.localeCompare(h.action));const u={};a.forEach(f=>{u[f.ability]||(u[f.ability]=[]),u[f.ability].push(f.action)});const l=Object.keys(u).map(f=>(o++,`(${o}) '${f}': '${u[f].join("', '")}' for '${c}'.`));r.push(l.join(", ").replace(".,","."))});const i=r.join(" "),s=`${n}${i}`;return`${e?e+" ":""}${s}`}function as(e){var t;const n=ge(e);se(n);const r=(t=n.att)==null?void 0:t.eip155;return r?Object.keys(r).map(o=>o.split("/")[1]):[]}function us(e){const t=ge(e);se(t);const n=[];return Object.values(t.att).forEach(r=>{Object.values(r).forEach(o=>{var i;(i=o?.[0])!=null&&i.chains&&n.push(o[0].chains)})}),[...new Set(n.flat())]}function mr(e,t){if(!t)return e;const n=ge(t);return se(n),Dt(e,n)}function dt(e){if(!e)return;const t=e?.[e.length-1];return Mt(t)?t:void 0}function qt(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function wr(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function ee(e,...t){if(!wr(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error("Uint8Array expected of length "+t+", got length="+e.length)}function br(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 fs(e,t){ee(e);const n=t.outputLen;if(e.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}function Er(e){if(typeof e!="boolean")throw new Error(`boolean expected, not ${e}`)}const ye=e=>new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4)),ls=e=>new DataView(e.buffer,e.byteOffset,e.byteLength),ds=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!ds)throw new Error("Non little-endian hardware is not supported");function hs(e){if(typeof e!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(e))}function Ht(e){if(typeof e=="string")e=hs(e);else if(wr(e))e=Ft(e);else throw new Error("Uint8Array expected, got "+typeof e);return e}function ps(e,t){if(t==null||typeof t!="object")throw new Error("options must be defined");return Object.assign(e,t)}function gs(e,t){if(e.length!==t.length)return!1;let n=0;for(let r=0;r<e.length;r++)n|=e[r]^t[r];return n===0}const ys=(e,t)=>{function n(r,...o){if(ee(r),e.nonceLength!==void 0){const l=o[0];if(!l)throw new Error("nonce / iv required");e.varSizeNonce?ee(l):ee(l,e.nonceLength)}const i=e.tagLength;i&&o[1]!==void 0&&ee(o[1]);const s=t(r,...o),c=(l,f)=>{if(f!==void 0){if(l!==2)throw new Error("cipher output not supported");ee(f)}};let a=!1;return{encrypt(l,f){if(a)throw new Error("cannot encrypt() twice with same key + nonce");return a=!0,ee(l),c(s.encrypt.length,f),s.encrypt(l,f)},decrypt(l,f){if(ee(l),i&&l.length<i)throw new Error("invalid ciphertext length: smaller than tagLength="+i);return c(s.decrypt.length,f),s.decrypt(l,f)}}}return Object.assign(n,e),n};function vr(e,t,n=!0){if(t===void 0)return new Uint8Array(e);if(t.length!==e)throw new Error("invalid output length, expected "+e+", got: "+t.length);if(n&&!ms(t))throw new Error("invalid output, must be aligned");return t}function xr(e,t,n,r){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,n,r);const o=BigInt(32),i=BigInt(4294967295),s=Number(n>>o&i),c=Number(n&i),a=r?4:0,u=r?0:4;e.setUint32(t+a,s,r),e.setUint32(t+u,c,r)}function ms(e){return e.byteOffset%4===0}function Ft(e){return Uint8Array.from(e)}function Pe(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}const Sr=e=>Uint8Array.from(e.split("").map(t=>t.charCodeAt(0))),ws=Sr("expand 16-byte k"),bs=Sr("expand 32-byte k"),Es=ye(ws),vs=ye(bs);function V(e,t){return e<<t|e>>>32-t}function Kt(e){return e.byteOffset%4===0}const ht=64,xs=16,Ar=2**32-1,Nr=new Uint32Array;function Ss(e,t,n,r,o,i,s,c){const a=o.length,u=new Uint8Array(ht),l=ye(u),f=Kt(o)&&Kt(i),h=f?ye(o):Nr,y=f?ye(i):Nr;for(let E=0;E<a;s++){if(e(t,n,r,l,s,c),s>=Ar)throw new Error("arx: counter overflow");const p=Math.min(ht,a-E);if(f&&p===ht){const d=E/4;if(E%4!==0)throw new Error("arx: invalid block position");for(let v=0,m;v<xs;v++)m=d+v,y[m]=h[m]^l[v];E+=ht;continue}for(let d=0,v;d<p;d++)v=E+d,i[v]=o[v]^u[d];E+=p}}function As(e,t){const{allowShortKeys:n,extendNonceFn:r,counterLength:o,counterRight:i,rounds:s}=ps({allowShortKeys:!1,counterLength:8,counterRight:!1,rounds:20},t);if(typeof e!="function")throw new Error("core must be a function");return qt(o),qt(s),Er(i),Er(n),(c,a,u,l,f=0)=>{ee(c),ee(a),ee(u);const h=u.length;if(l===void 0&&(l=new Uint8Array(h)),ee(l),qt(f),f<0||f>=Ar)throw new Error("arx: counter overflow");if(l.length<h)throw new Error(`arx: output (${l.length}) is shorter than data (${h})`);const y=[];let E=c.length,p,d;if(E===32)y.push(p=Ft(c)),d=vs;else if(E===16&&n)p=new Uint8Array(32),p.set(c),p.set(c,16),d=Es,y.push(p);else throw new Error(`arx: invalid 32-byte key, got length=${E}`);Kt(a)||y.push(a=Ft(a));const v=ye(p);if(r){if(a.length!==24)throw new Error("arx: extended nonce must be 24 bytes");r(d,v,ye(a.subarray(0,16)),v),a=a.subarray(16)}const m=16-o;if(m!==a.length)throw new Error(`arx: nonce must be ${m} or 16 bytes`);if(m!==12){const B=new Uint8Array(12);B.set(a,i?0:12-a.length),a=B,y.push(a)}const A=ye(a);return Ss(e,d,v,A,u,l,f,s),Pe(...y),l}}const z=(e,t)=>e[t++]&255|(e[t++]&255)<<8;class Ns{constructor(t){this.blockLen=16,this.outputLen=16,this.buffer=new Uint8Array(16),this.r=new Uint16Array(10),this.h=new Uint16Array(10),this.pad=new Uint16Array(8),this.pos=0,this.finished=!1,t=Ht(t),ee(t,32);const n=z(t,0),r=z(t,2),o=z(t,4),i=z(t,6),s=z(t,8),c=z(t,10),a=z(t,12),u=z(t,14);this.r[0]=n&8191,this.r[1]=(n>>>13|r<<3)&8191,this.r[2]=(r>>>10|o<<6)&7939,this.r[3]=(o>>>7|i<<9)&8191,this.r[4]=(i>>>4|s<<12)&255,this.r[5]=s>>>1&8190,this.r[6]=(s>>>14|c<<2)&8191,this.r[7]=(c>>>11|a<<5)&8065,this.r[8]=(a>>>8|u<<8)&8191,this.r[9]=u>>>5&127;for(let l=0;l<8;l++)this.pad[l]=z(t,16+2*l)}process(t,n,r=!1){const o=r?0:2048,{h:i,r:s}=this,c=s[0],a=s[1],u=s[2],l=s[3],f=s[4],h=s[5],y=s[6],E=s[7],p=s[8],d=s[9],v=z(t,n+0),m=z(t,n+2),A=z(t,n+4),B=z(t,n+6),C=z(t,n+8),O=z(t,n+10),N=z(t,n+12),T=z(t,n+14);let S=i[0]+(v&8191),L=i[1]+((v>>>13|m<<3)&8191),R=i[2]+((m>>>10|A<<6)&8191),_=i[3]+((A>>>7|B<<9)&8191),P=i[4]+((B>>>4|C<<12)&8191),g=i[5]+(C>>>1&8191),w=i[6]+((C>>>14|O<<2)&8191),b=i[7]+((O>>>11|N<<5)&8191),I=i[8]+((N>>>8|T<<8)&8191),U=i[9]+(T>>>5|o),x=0,$=x+S*c+L*(5*d)+R*(5*p)+_*(5*E)+P*(5*y);x=$>>>13,$&=8191,$+=g*(5*h)+w*(5*f)+b*(5*l)+I*(5*u)+U*(5*a),x+=$>>>13,$&=8191;let j=x+S*a+L*c+R*(5*d)+_*(5*p)+P*(5*E);x=j>>>13,j&=8191,j+=g*(5*y)+w*(5*h)+b*(5*f)+I*(5*l)+U*(5*u),x+=j>>>13,j&=8191;let k=x+S*u+L*a+R*c+_*(5*d)+P*(5*p);x=k>>>13,k&=8191,k+=g*(5*E)+w*(5*y)+b*(5*h)+I*(5*f)+U*(5*l),x+=k>>>13,k&=8191;let D=x+S*l+L*u+R*a+_*c+P*(5*d);x=D>>>13,D&=8191,D+=g*(5*p)+w*(5*E)+b*(5*y)+I*(5*h)+U*(5*f),x+=D>>>13,D&=8191;let q=x+S*f+L*l+R*u+_*a+P*c;x=q>>>13,q&=8191,q+=g*(5*d)+w*(5*p)+b*(5*E)+I*(5*y)+U*(5*h),x+=q>>>13,q&=8191;let Z=x+S*h+L*f+R*l+_*u+P*a;x=Z>>>13,Z&=8191,Z+=g*c+w*(5*d)+b*(5*p)+I*(5*E)+U*(5*y),x+=Z>>>13,Z&=8191;let Y=x+S*y+L*h+R*f+_*l+P*u;x=Y>>>13,Y&=8191,Y+=g*a+w*c+b*(5*d)+I*(5*p)+U*(5*E),x+=Y>>>13,Y&=8191;let oe=x+S*E+L*y+R*h+_*f+P*l;x=oe>>>13,oe&=8191,oe+=g*u+w*a+b*c+I*(5*d)+U*(5*p),x+=oe>>>13,oe&=8191;let J=x+S*p+L*E+R*y+_*h+P*f;x=J>>>13,J&=8191,J+=g*l+w*u+b*a+I*c+U*(5*d),x+=J>>>13,J&=8191;let Q=x+S*d+L*p+R*E+_*y+P*h;x=Q>>>13,Q&=8191,Q+=g*f+w*l+b*u+I*a+U*c,x+=Q>>>13,Q&=8191,x=(x<<2)+x|0,x=x+$|0,$=x&8191,x=x>>>13,j+=x,i[0]=$,i[1]=j,i[2]=k,i[3]=D,i[4]=q,i[5]=Z,i[6]=Y,i[7]=oe,i[8]=J,i[9]=Q}finalize(){const{h:t,pad:n}=this,r=new Uint16Array(10);let o=t[1]>>>13;t[1]&=8191;for(let c=2;c<10;c++)t[c]+=o,o=t[c]>>>13,t[c]&=8191;t[0]+=o*5,o=t[0]>>>13,t[0]&=8191,t[1]+=o,o=t[1]>>>13,t[1]&=8191,t[2]+=o,r[0]=t[0]+5,o=r[0]>>>13,r[0]&=8191;for(let c=1;c<10;c++)r[c]=t[c]+o,o=r[c]>>>13,r[c]&=8191;r[9]-=8192;let i=(o^1)-1;for(let c=0;c<10;c++)r[c]&=i;i=~i;for(let c=0;c<10;c++)t[c]=t[c]&i|r[c];t[0]=(t[0]|t[1]<<13)&65535,t[1]=(t[1]>>>3|t[2]<<10)&65535,t[2]=(t[2]>>>6|t[3]<<7)&65535,t[3]=(t[3]>>>9|t[4]<<4)&65535,t[4]=(t[4]>>>12|t[5]<<1|t[6]<<14)&65535,t[5]=(t[6]>>>2|t[7]<<11)&65535,t[6]=(t[7]>>>5|t[8]<<8)&65535,t[7]=(t[8]>>>8|t[9]<<5)&65535;let s=t[0]+n[0];t[0]=s&65535;for(let c=1;c<8;c++)s=(t[c]+n[c]|0)+(s>>>16)|0,t[c]=s&65535;Pe(r)}update(t){br(this);const{buffer:n,blockLen:r}=this;t=Ht(t);const o=t.length;for(let i=0;i<o;){const s=Math.min(r-this.pos,o-i);if(s===r){for(;r<=o-i;i+=r)this.process(t,i);continue}n.set(t.subarray(i,i+s),this.pos),this.pos+=s,i+=s,this.pos===r&&(this.process(n,0,!1),this.pos=0)}return this}destroy(){Pe(this.h,this.r,this.buffer,this.pad)}digestInto(t){br(this),fs(t,this),this.finished=!0;const{buffer:n,h:r}=this;let{pos:o}=this;if(o){for(n[o++]=1;o<16;o++)n[o]=0;this.process(n,0,!0)}this.finalize();let i=0;for(let s=0;s<8;s++)t[i++]=r[s]>>>0,t[i++]=r[s]>>>8;return t}digest(){const{buffer:t,outputLen:n}=this;this.digestInto(t);const r=t.slice(0,n);return this.destroy(),r}}function Os(e){const t=(r,o)=>e(o).update(Ht(r)).digest(),n=e(new Uint8Array(32));return t.outputLen=n.outputLen,t.blockLen=n.blockLen,t.create=r=>e(r),t}const Is=Os(e=>new Ns(e));function Bs(e,t,n,r,o,i=20){let s=e[0],c=e[1],a=e[2],u=e[3],l=t[0],f=t[1],h=t[2],y=t[3],E=t[4],p=t[5],d=t[6],v=t[7],m=o,A=n[0],B=n[1],C=n[2],O=s,N=c,T=a,S=u,L=l,R=f,_=h,P=y,g=E,w=p,b=d,I=v,U=m,x=A,$=B,j=C;for(let D=0;D<i;D+=2)O=O+L|0,U=V(U^O,16),g=g+U|0,L=V(L^g,12),O=O+L|0,U=V(U^O,8),g=g+U|0,L=V(L^g,7),N=N+R|0,x=V(x^N,16),w=w+x|0,R=V(R^w,12),N=N+R|0,x=V(x^N,8),w=w+x|0,R=V(R^w,7),T=T+_|0,$=V($^T,16),b=b+$|0,_=V(_^b,12),T=T+_|0,$=V($^T,8),b=b+$|0,_=V(_^b,7),S=S+P|0,j=V(j^S,16),I=I+j|0,P=V(P^I,12),S=S+P|0,j=V(j^S,8),I=I+j|0,P=V(P^I,7),O=O+R|0,j=V(j^O,16),b=b+j|0,R=V(R^b,12),O=O+R|0,j=V(j^O,8),b=b+j|0,R=V(R^b,7),N=N+_|0,U=V(U^N,16),I=I+U|0,_=V(_^I,12),N=N+_|0,U=V(U^N,8),I=I+U|0,_=V(_^I,7),T=T+P|0,x=V(x^T,16),g=g+x|0,P=V(P^g,12),T=T+P|0,x=V(x^T,8),g=g+x|0,P=V(P^g,7),S=S+L|0,$=V($^S,16),w=w+$|0,L=V(L^w,12),S=S+L|0,$=V($^S,8),w=w+$|0,L=V(L^w,7);let k=0;r[k++]=s+O|0,r[k++]=c+N|0,r[k++]=a+T|0,r[k++]=u+S|0,r[k++]=l+L|0,r[k++]=f+R|0,r[k++]=h+_|0,r[k++]=y+P|0,r[k++]=E+g|0,r[k++]=p+w|0,r[k++]=d+b|0,r[k++]=v+I|0,r[k++]=m+U|0,r[k++]=A+x|0,r[k++]=B+$|0,r[k++]=C+j|0}const Rs=As(Bs,{counterRight:!1,counterLength:4,allowShortKeys:!1}),Ts=new Uint8Array(16),Or=(e,t)=>{e.update(t);const n=t.length%16;n&&e.update(Ts.subarray(n))},Us=new Uint8Array(32);function Ir(e,t,n,r,o){const i=e(t,n,Us),s=Is.create(i);o&&Or(s,o),Or(s,r);const c=new Uint8Array(16),a=ls(c);xr(a,0,BigInt(o?o.length:0),!0),xr(a,8,BigInt(r.length),!0),s.update(c);const u=s.digest();return Pe(i,c),u}const _s=e=>(t,n,r)=>({encrypt(i,s){const c=i.length;s=vr(c+16,s,!1),s.set(i);const a=s.subarray(0,-16);e(t,n,a,a,1);const u=Ir(e,t,n,a,r);return s.set(u,c),Pe(u),s},decrypt(i,s){s=vr(i.length-16,s,!1);const c=i.subarray(0,-16),a=i.subarray(-16),u=Ir(e,t,n,c,r);if(!gs(a,u))throw new Error("invalid tag");return s.set(i.subarray(0,-16)),e(t,n,s,s,1),Pe(u),s}}),Br=ys({blockSize:64,nonceLength:12,tagLength:16},_s(Rs));class Rr extends Pt{constructor(t,n){super(),this.finished=!1,this.destroyed=!1,Ct(t);const r=Ce(n);if(this.iHash=t.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const o=this.blockLen,i=new Uint8Array(o);i.set(r.length>o?t.create().update(r).digest():r);for(let s=0;s<i.length;s++)i[s]^=54;this.iHash.update(i),this.oHash=t.create();for(let s=0;s<i.length;s++)i[s]^=106;this.oHash.update(i),i.fill(0)}update(t){return Ue(this),this.iHash.update(t),this}digestInto(t){Ue(this),We(t,this.outputLen),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));const{oHash:n,iHash:r,finished:o,destroyed:i,blockLen:s,outputLen:c}=this;return t=t,t.finished=o,t.destroyed=i,t.blockLen=s,t.outputLen=c,t.oHash=n._cloneInto(t.oHash),t.iHash=r._cloneInto(t.iHash),t}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const pt=(e,t,n)=>new Rr(e,t).update(n).digest();pt.create=(e,t)=>new Rr(e,t);function Cs(e,t,n){return Ct(e),n===void 0&&(n=new Uint8Array(e.outputLen)),pt(e,Ce(n),Ce(t))}const zt=new Uint8Array([0]),Tr=new Uint8Array;function Ls(e,t,n,r=32){if(Ct(e),Ge(r),r>255*e.outputLen)throw new Error("Length should be <= 255*HashLen");const o=Math.ceil(r/e.outputLen);n===void 0&&(n=Tr);const i=new Uint8Array(o*e.outputLen),s=pt.create(e,t),c=s._cloneInto(),a=new Uint8Array(s.outputLen);for(let u=0;u<o;u++)zt[0]=u+1,c.update(u===0?Tr:a).update(n).update(zt).digestInto(a),i.set(a,e.outputLen*u),s._cloneInto(c);return s.destroy(),c.destroy(),a.fill(0),zt.fill(0),i.slice(0,r)}const Ps=(e,t,n,r,o)=>Ls(e,Cs(e,t,n),r,o);function $s(e,t,n,r){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,n,r);const o=BigInt(32),i=BigInt(4294967295),s=Number(n>>o&i),c=Number(n&i),a=r?4:0,u=r?0:4;e.setUint32(t+a,s,r),e.setUint32(t+u,c,r)}function js(e,t,n){return e&t^~e&n}function ks(e,t,n){return e&t^e&n^t&n}class Vs extends Pt{constructor(t,n,r,o){super(),this.blockLen=t,this.outputLen=n,this.padOffset=r,this.isLE=o,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=Lt(this.buffer)}update(t){Ue(this);const{view:n,buffer:r,blockLen:o}=this;t=Ce(t);const i=t.length;for(let s=0;s<i;){const c=Math.min(o-this.pos,i-s);if(c===o){const a=Lt(t);for(;o<=i-s;s+=o)this.process(a,s);continue}r.set(t.subarray(s,s+c),this.pos),this.pos+=c,s+=c,this.pos===o&&(this.process(n,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){Ue(this),Fn(t,this),this.finished=!0;const{buffer:n,view:r,blockLen:o,isLE:i}=this;let{pos:s}=this;n[s++]=128,this.buffer.subarray(s).fill(0),this.padOffset>o-s&&(this.process(r,0),s=0);for(let f=s;f<o;f++)n[f]=0;$s(r,o-8,BigInt(this.length*8),i),this.process(r,0);const c=Lt(t),a=this.outputLen;if(a%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const u=a/4,l=this.get();if(u>l.length)throw new Error("_sha2: outputLen bigger than state");for(let f=0;f<u;f++)c.setUint32(4*f,l[f],i)}digest(){const{buffer:t,outputLen:n}=this;this.digestInto(t);const r=t.slice(0,n);return this.destroy(),r}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:n,buffer:r,length:o,finished:i,destroyed:s,pos:c}=this;return t.length=o,t.pos=c,t.finished=i,t.destroyed=s,o%n&&t.buffer.set(r),t}}const Ms=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]),me=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),we=new Uint32Array(64);class Ds extends Vs{constructor(){super(64,32,8,!1),this.A=me[0]|0,this.B=me[1]|0,this.C=me[2]|0,this.D=me[3]|0,this.E=me[4]|0,this.F=me[5]|0,this.G=me[6]|0,this.H=me[7]|0}get(){const{A:t,B:n,C:r,D:o,E:i,F:s,G:c,H:a}=this;return[t,n,r,o,i,s,c,a]}set(t,n,r,o,i,s,c,a){this.A=t|0,this.B=n|0,this.C=r|0,this.D=o|0,this.E=i|0,this.F=s|0,this.G=c|0,this.H=a|0}process(t,n){for(let f=0;f<16;f++,n+=4)we[f]=t.getUint32(n,!1);for(let f=16;f<64;f++){const h=we[f-15],y=we[f-2],E=ie(h,7)^ie(h,18)^h>>>3,p=ie(y,17)^ie(y,19)^y>>>10;we[f]=p+we[f-7]+E+we[f-16]|0}let{A:r,B:o,C:i,D:s,E:c,F:a,G:u,H:l}=this;for(let f=0;f<64;f++){const h=ie(c,6)^ie(c,11)^ie(c,25),y=l+h+js(c,a,u)+Ms[f]+we[f]|0,p=(ie(r,2)^ie(r,13)^ie(r,22))+ks(r,o,i)|0;l=u,u=a,a=c,c=s+y|0,s=i,i=o,o=r,r=y+p|0}r=r+this.A|0,o=o+this.B|0,i=i+this.C|0,s=s+this.D|0,c=c+this.E|0,a=a+this.F|0,u=u+this.G|0,l=l+this.H|0,this.set(r,o,i,s,c,a,u,l)}roundClean(){we.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}}const Xe=Yn(()=>new Ds);/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const gt=BigInt(0),yt=BigInt(1),qs=BigInt(2);function Se(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function Qe(e){if(!Se(e))throw new Error("Uint8Array expected")}function $e(e,t){if(typeof t!="boolean")throw new Error(e+" boolean expected, got "+t)}const Hs=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function je(e){Qe(e);let t="";for(let n=0;n<e.length;n++)t+=Hs[e[n]];return t}function ke(e){const t=e.toString(16);return t.length&1?"0"+t:t}function Zt(e){if(typeof e!="string")throw new Error("hex string expected, got "+typeof e);return e===""?gt:BigInt("0x"+e)}const ae={_0:48,_9:57,A:65,F:70,a:97,f:102};function Ur(e){if(e>=ae._0&&e<=ae._9)return e-ae._0;if(e>=ae.A&&e<=ae.F)return e-(ae.A-10);if(e>=ae.a&&e<=ae.f)return e-(ae.a-10)}function Ve(e){if(typeof e!="string")throw new Error("hex string expected, got "+typeof e);const t=e.length,n=t/2;if(t%2)throw new Error("hex string expected, got unpadded hex of length "+t);const r=new Uint8Array(n);for(let o=0,i=0;o<n;o++,i+=2){const s=Ur(e.charCodeAt(i)),c=Ur(e.charCodeAt(i+1));if(s===void 0||c===void 0){const a=e[i]+e[i+1];throw new Error('hex string expected, got non-hex character "'+a+'" at index '+i)}r[o]=s*16+c}return r}function Ae(e){return Zt(je(e))}function et(e){return Qe(e),Zt(je(Uint8Array.from(e).reverse()))}function Me(e,t){return Ve(e.toString(16).padStart(t*2,"0"))}function mt(e,t){return Me(e,t).reverse()}function Fs(e){return Ve(ke(e))}function te(e,t,n){let r;if(typeof t=="string")try{r=Ve(t)}catch(i){throw new Error(e+" must be hex string or Uint8Array, cause: "+i)}else if(Se(t))r=Uint8Array.from(t);else throw new Error(e+" must be hex string or Uint8Array");const o=r.length;if(typeof n=="number"&&o!==n)throw new Error(e+" of length "+n+" expected, got "+o);return r}function tt(...e){let t=0;for(let r=0;r<e.length;r++){const o=e[r];Qe(o),t+=o.length}const n=new Uint8Array(t);for(let r=0,o=0;r<e.length;r++){const i=e[r];n.set(i,o),o+=i.length}return n}function Ks(e,t){if(e.length!==t.length)return!1;let n=0;for(let r=0;r<e.length;r++)n|=e[r]^t[r];return n===0}function zs(e){if(typeof e!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(e))}const Yt=e=>typeof e=="bigint"&&gt<=e;function wt(e,t,n){return Yt(e)&&Yt(t)&&Yt(n)&&t<=e&&e<n}function ue(e,t,n,r){if(!wt(t,n,r))throw new Error("expected valid "+e+": "+n+" <= n < "+r+", got "+t)}function _r(e){let t;for(t=0;e>gt;e>>=yt,t+=1);return t}function Zs(e,t){return e>>BigInt(t)&yt}function Ys(e,t,n){return e|(n?yt:gt)<<BigInt(t)}const Gt=e=>(qs<<BigInt(e-1))-yt,Wt=e=>new Uint8Array(e),Cr=e=>Uint8Array.from(e);function Lr(e,t,n){if(typeof e!="number"||e<2)throw new Error("hashLen must be a number");if(typeof t!="number"||t<2)throw new Error("qByteLen must be a number");if(typeof n!="function")throw new Error("hmacFn must be a function");let r=Wt(e),o=Wt(e),i=0;const s=()=>{r.fill(1),o.fill(0),i=0},c=(...f)=>n(o,r,...f),a=(f=Wt())=>{o=c(Cr([0]),f),r=c(),f.length!==0&&(o=c(Cr([1]),f),r=c())},u=()=>{if(i++>=1e3)throw new Error("drbg: tried 1000 values");let f=0;const h=[];for(;f<t;){r=c();const y=r.slice();h.push(y),f+=r.length}return tt(...h)};return(f,h)=>{s(),a(f);let y;for(;!(y=h(u()));)a();return s(),y}}const Gs={bigint:e=>typeof e=="bigint",function:e=>typeof e=="function",boolean:e=>typeof e=="boolean",string:e=>typeof e=="string",stringOrUint8Array:e=>typeof e=="string"||Se(e),isSafeInteger:e=>Number.isSafeInteger(e),array:e=>Array.isArray(e),field:(e,t)=>t.Fp.isValid(e),hash:e=>typeof e=="function"&&Number.isSafeInteger(e.outputLen)};function De(e,t,n={}){const r=(o,i,s)=>{const c=Gs[i];if(typeof c!="function")throw new Error("invalid validator function");const a=e[o];if(!(s&&a===void 0)&&!c(a,e))throw new Error("param "+String(o)+" is invalid. Expected "+i+", got "+a)};for(const[o,i]of Object.entries(t))r(o,i,!1);for(const[o,i]of Object.entries(n))r(o,i,!0);return e}const Ws=()=>{throw new Error("not implemented")};function Jt(e){const t=new WeakMap;return(n,...r)=>{const o=t.get(n);if(o!==void 0)return o;const i=e(n,...r);return t.set(n,i),i}}var Js=Object.freeze({__proto__:null,isBytes:Se,abytes:Qe,abool:$e,bytesToHex:je,numberToHexUnpadded:ke,hexToNumber:Zt,hexToBytes:Ve,bytesToNumberBE:Ae,bytesToNumberLE:et,numberToBytesBE:Me,numberToBytesLE:mt,numberToVarBytesBE:Fs,ensureBytes:te,concatBytes:tt,equalBytes:Ks,utf8ToBytes:zs,inRange:wt,aInRange:ue,bitLen:_r,bitGet:Zs,bitSet:Ys,bitMask:Gt,createHmacDrbg:Lr,validateObject:De,notImplemented:Ws,memoized:Jt});const F=BigInt(0),H=BigInt(1),Ne=BigInt(2),Xs=BigInt(3),Xt=BigInt(4),Pr=BigInt(5),$r=BigInt(8);function X(e,t){const n=e%t;return n>=F?n:t+n}function jr(e,t,n){if(t<F)throw new Error("invalid exponent, negatives unsupported");if(n<=F)throw new Error("invalid modulus");if(n===H)return F;let r=H;for(;t>F;)t&H&&(r=r*e%n),e=e*e%n,t>>=H;return r}function re(e,t,n){let r=e;for(;t-- >F;)r*=r,r%=n;return r}function Qt(e,t){if(e===F)throw new Error("invert: expected non-zero number");if(t<=F)throw new Error("invert: expected positive modulus, got "+t);let n=X(e,t),r=t,o=F,i=H;for(;n!==F;){const c=r/n,a=r%n,u=o-i*c;r=n,n=a,o=i,i=u}if(r!==H)throw new Error("invert: does not exist");return X(o,t)}function Qs(e){const t=(e-H)/Ne;let n,r,o;for(n=e-H,r=0;n%Ne===F;n/=Ne,r++);for(o=Ne;o<e&&jr(o,t,e)!==e-H;o++)if(o>1e3)throw new Error("Cannot find square root: likely non-prime P");if(r===1){const s=(e+H)/Xt;return function(a,u){const l=a.pow(u,s);if(!a.eql(a.sqr(l),u))throw new Error("Cannot find square root");return l}}const i=(n+H)/Ne;return function(c,a){if(c.pow(a,t)===c.neg(c.ONE))throw new Error("Cannot find square root");let u=r,l=c.pow(c.mul(c.ONE,o),n),f=c.pow(a,i),h=c.pow(a,n);for(;!c.eql(h,c.ONE);){if(c.eql(h,c.ZERO))return c.ZERO;let y=1;for(let p=c.sqr(h);y<u&&!c.eql(p,c.ONE);y++)p=c.sqr(p);const E=c.pow(l,H<<BigInt(u-y-1));l=c.sqr(E),f=c.mul(f,E),h=c.mul(h,l),u=y}return f}}function ec(e){if(e%Xt===Xs){const t=(e+H)/Xt;return function(r,o){const i=r.pow(o,t);if(!r.eql(r.sqr(i),o))throw new Error("Cannot find square root");return i}}if(e%$r===Pr){const t=(e-Pr)/$r;return function(r,o){const i=r.mul(o,Ne),s=r.pow(i,t),c=r.mul(o,s),a=r.mul(r.mul(c,Ne),s),u=r.mul(c,r.sub(a,r.ONE));if(!r.eql(r.sqr(u),o))throw new Error("Cannot find square root");return u}}return Qs(e)}const tc=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function nc(e){const t={ORDER:"bigint",MASK:"bigint",BYTES:"isSafeInteger",BITS:"isSafeInteger"},n=tc.reduce((r,o)=>(r[o]="function",r),t);return De(e,n)}function rc(e,t,n){if(n<F)throw new Error("invalid exponent, negatives unsupported");if(n===F)return e.ONE;if(n===H)return t;let r=e.ONE,o=t;for(;n>F;)n&H&&(r=e.mul(r,o)),o=e.sqr(o),n>>=H;return r}function oc(e,t){const n=new Array(t.length),r=t.reduce((i,s,c)=>e.is0(s)?i:(n[c]=i,e.mul(i,s)),e.ONE),o=e.inv(r);return t.reduceRight((i,s,c)=>e.is0(s)?i:(n[c]=e.mul(i,n[c]),e.mul(i,s)),o),n}function kr(e,t){const n=t!==void 0?t:e.toString(2).length,r=Math.ceil(n/8);return{nBitLength:n,nByteLength:r}}function Vr(e,t,n=!1,r={}){if(e<=F)throw new Error("invalid field: expected ORDER > 0, got "+e);const{nBitLength:o,nByteLength:i}=kr(e,t);if(i>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");let s;const c=Object.freeze({ORDER:e,isLE:n,BITS:o,BYTES:i,MASK:Gt(o),ZERO:F,ONE:H,create:a=>X(a,e),isValid:a=>{if(typeof a!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof a);return F<=a&&a<e},is0:a=>a===F,isOdd:a=>(a&H)===H,neg:a=>X(-a,e),eql:(a,u)=>a===u,sqr:a=>X(a*a,e),add:(a,u)=>X(a+u,e),sub:(a,u)=>X(a-u,e),mul:(a,u)=>X(a*u,e),pow:(a,u)=>rc(c,a,u),div:(a,u)=>X(a*Qt(u,e),e),sqrN:a=>a*a,addN:(a,u)=>a+u,subN:(a,u)=>a-u,mulN:(a,u)=>a*u,inv:a=>Qt(a,e),sqrt:r.sqrt||(a=>(s||(s=ec(e)),s(c,a))),invertBatch:a=>oc(c,a),cmov:(a,u,l)=>l?u:a,toBytes:a=>n?mt(a,i):Me(a,i),fromBytes:a=>{if(a.length!==i)throw new Error("Field.fromBytes: expected "+i+" bytes, got "+a.length);return n?et(a):Ae(a)}});return Object.freeze(c)}function Mr(e){if(typeof e!="bigint")throw new Error("field order must be bigint");const t=e.toString(2).length;return Math.ceil(t/8)}function Dr(e){const t=Mr(e);return t+Math.ceil(t/2)}function ic(e,t,n=!1){const r=e.length,o=Mr(t),i=Dr(t);if(r<16||r<i||r>1024)throw new Error("expected "+i+"-1024 bytes of input, got "+r);const s=n?et(e):Ae(e),c=X(s,t-H)+H;return n?mt(c,o):Me(c,o)}const qr=BigInt(0),bt=BigInt(1);function en(e,t){const n=t.negate();return e?n:t}function Hr(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+e)}function tn(e,t){Hr(e,t);const n=Math.ceil(t/e)+1,r=2**(e-1);return{windows:n,windowSize:r}}function sc(e,t){if(!Array.isArray(e))throw new Error("array expected");e.forEach((n,r)=>{if(!(n instanceof t))throw new Error("invalid point at index "+r)})}function cc(e,t){if(!Array.isArray(e))throw new Error("array of scalars expected");e.forEach((n,r)=>{if(!t.isValid(n))throw new Error("invalid scalar at index "+r)})}const nn=new WeakMap,Fr=new WeakMap;function rn(e){return Fr.get(e)||1}function ac(e,t){return{constTimeNegate:en,hasPrecomputes(n){return rn(n)!==1},unsafeLadder(n,r,o=e.ZERO){let i=n;for(;r>qr;)r&bt&&(o=o.add(i)),i=i.double(),r>>=bt;return o},precomputeWindow(n,r){const{windows:o,windowSize:i}=tn(r,t),s=[];let c=n,a=c;for(let u=0;u<o;u++){a=c,s.push(a);for(let l=1;l<i;l++)a=a.add(c),s.push(a);c=a.double()}return s},wNAF(n,r,o){const{windows:i,windowSize:s}=tn(n,t);let c=e.ZERO,a=e.BASE;const u=BigInt(2**n-1),l=2**n,f=BigInt(n);for(let h=0;h<i;h++){const y=h*s;let E=Number(o&u);o>>=f,E>s&&(E-=l,o+=bt);const p=y,d=y+Math.abs(E)-1,v=h%2!==0,m=E<0;E===0?a=a.add(en(v,r[p])):c=c.add(en(m,r[d]))}return{p:c,f:a}},wNAFUnsafe(n,r,o,i=e.ZERO){const{windows:s,windowSize:c}=tn(n,t),a=BigInt(2**n-1),u=2**n,l=BigInt(n);for(let f=0;f<s;f++){const h=f*c;if(o===qr)break;let y=Number(o&a);if(o>>=l,y>c&&(y-=u,o+=bt),y===0)continue;let E=r[h+Math.abs(y)-1];y<0&&(E=E.negate()),i=i.add(E)}return i},getPrecomputes(n,r,o){let i=nn.get(r);return i||(i=this.precomputeWindow(r,n),n!==1&&nn.set(r,o(i))),i},wNAFCached(n,r,o){const i=rn(n);return this.wNAF(i,this.getPrecomputes(i,n,o),r)},wNAFCachedUnsafe(n,r,o,i){const s=rn(n);return s===1?this.unsafeLadder(n,r,i):this.wNAFUnsafe(s,this.getPrecomputes(s,n,o),r,i)},setWindowSize(n,r){Hr(r,t),Fr.set(n,r),nn.delete(n)}}}function uc(e,t,n,r){if(sc(n,e),cc(r,t),n.length!==r.length)throw new Error("arrays of points and scalars must have equal length");const o=e.ZERO,i=_r(BigInt(n.length)),s=i>12?i-3:i>4?i-2:i?2:1,c=(1<<s)-1,a=new Array(c+1).fill(o),u=Math.floor((t.BITS-1)/s)*s;let l=o;for(let f=u;f>=0;f-=s){a.fill(o);for(let y=0;y<r.length;y++){const E=r[y],p=Number(E>>BigInt(f)&BigInt(c));a[p]=a[p].add(n[y])}let h=o;for(let y=a.length-1,E=o;y>0;y--)E=E.add(a[y]),h=h.add(E);if(l=l.add(h),f!==0)for(let y=0;y<s;y++)l=l.double()}return l}function Kr(e){return nc(e.Fp),De(e,{n:"bigint",h:"bigint",Gx:"field",Gy:"field"},{nBitLength:"isSafeInteger",nByteLength:"isSafeInteger"}),Object.freeze({...kr(e.n,e.nBitLength),...e,p:e.Fp.ORDER})}BigInt(0),BigInt(1),BigInt(2),BigInt(8);const qe=BigInt(0),on=BigInt(1);function fc(e){return De(e,{a:"bigint"},{montgomeryBits:"isSafeInteger",nByteLength:"isSafeInteger",adjustScalarBytes:"function",domain:"function",powPminus2:"function",Gu:"bigint"}),Object.freeze({...e})}function lc(e){const t=fc(e),{P:n}=t,r=m=>X(m,n),o=t.montgomeryBits,i=Math.ceil(o/8),s=t.nByteLength,c=t.adjustScalarBytes||(m=>m),a=t.powPminus2||(m=>jr(m,n-BigInt(2),n));function u(m,A,B){const C=r(m*(A-B));return A=r(A-C),B=r(B+C),[A,B]}const l=(t.a-BigInt(2))/BigInt(4);function f(m,A){ue("u",m,qe,n),ue("scalar",A,qe,n);const B=A,C=m;let O=on,N=qe,T=m,S=on,L=qe,R;for(let P=BigInt(o-1);P>=qe;P--){const g=B>>P&on;L^=g,R=u(L,O,T),O=R[0],T=R[1],R=u(L,N,S),N=R[0],S=R[1],L=g;const w=O+N,b=r(w*w),I=O-N,U=r(I*I),x=b-U,$=T+S,j=T-S,k=r(j*w),D=r($*I),q=k+D,Z=k-D;T=r(q*q),S=r(C*r(Z*Z)),O=r(b*U),N=r(x*(b+r(l*x)))}R=u(L,O,T),O=R[0],T=R[1],R=u(L,N,S),N=R[0],S=R[1];const _=a(N);return r(O*_)}function h(m){return mt(r(m),i)}function y(m){const A=te("u coordinate",m,i);return s===32&&(A[31]&=127),et(A)}function E(m){const A=te("scalar",m),B=A.length;if(B!==i&&B!==s){l