3 lines
64 KiB
JavaScript
3 lines
64 KiB
JavaScript
import Xt,{EventEmitter as U}from"events";import Wt from"@walletconnect/keyvaluestorage";import{HEARTBEAT_EVENTS as G,HeartBeat as Qt}from"@walletconnect/heartbeat";import{generateChildLogger as b,getLoggerContext as E,pino as ye,getDefaultLoggerOptions as me}from"@walletconnect/logger";import{IMessageTracker as Zt,IPublisher as ei,ISubscriber as ti,IRelayer as ii,IStore as si,IJsonRpcHistory as ri,IExpirer as ni,IVerify as oi,IEchoClient as ai,ICore as hi}from"@walletconnect/types";import{safeJsonStringify as ci,safeJsonParse as ui}from"@walletconnect/safe-json";import*as Y from"@walletconnect/relay-auth";import{getInternalError as c,mapToObj as be,objToMap as fe,generateKeyPair as li,generateRandomBytes32 as ie,deriveSymKey as di,hashKey as gi,validateEncoding as pi,isTypeOneEnvelope as Ee,encrypt as Di,validateDecoding as yi,decrypt as mi,deserialize as we,decodeTypeByte as bi,BASE16 as fi,hashMessage as se,getRelayProtocolName as re,createExpiringPromise as k,getRelayProtocolApi as H,isUndefined as ne,getSdkError as K,isValidArray as Ei,getBundleId as wi,isOnline as ve,formatRelayRpcUrl as vi,subscribeToNetworkChange as Ii,isProposalStruct as Ci,isSessionStruct as Ri,TYPE_1 as _i,calcExpiry as J,formatUri as Ti,parseUri as Ie,createDelayedPromise as Si,engineEvent as oe,isExpired as Ce,isValidParams as ae,isValidUrl as Pi,isValidString as xi,parseExpirerTarget as Oi,formatTopicTarget as Ai,formatIdTarget as zi,isReactNative as Ni,isBrowser as Ui,isNode as Li}from"@walletconnect/utils";import{toString as $i}from"uint8arrays";import{ONE_DAY as L,SIX_HOURS as Fi,ONE_SECOND as Re,THIRTY_DAYS as X,FIVE_SECONDS as _e,THIRTY_SECONDS as Te,toMiliseconds as N,TEN_SECONDS as Mi,Watch as ki,FIVE_MINUTES as Se}from"@walletconnect/time";import{JsonRpcProvider as Ki}from"@walletconnect/jsonrpc-provider";import{getBigIntRpcId as Bi,isJsonRpcRequest as Pe,isJsonRpcResponse as xe,formatJsonRpcResult as Oe,formatJsonRpcRequest as Ae,formatJsonRpcError as Vi,isJsonRpcResult as qi,isJsonRpcError as ze}from"@walletconnect/jsonrpc-utils";import ji from"@walletconnect/jsonrpc-ws-connection";import Gi from"lodash.isequal";import Yi from"isomorphic-unfetch";function Hi(n,e){if(n.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),i=0;i<t.length;i++)t[i]=255;for(var s=0;s<n.length;s++){var r=n.charAt(s),o=r.charCodeAt(0);if(t[o]!==255)throw new TypeError(r+" is ambiguous");t[o]=s}var a=n.length,h=n.charAt(0),l=Math.log(a)/Math.log(256),d=Math.log(256)/Math.log(a);function p(u){if(u instanceof Uint8Array||(ArrayBuffer.isView(u)?u=new Uint8Array(u.buffer,u.byteOffset,u.byteLength):Array.isArray(u)&&(u=Uint8Array.from(u))),!(u instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(u.length===0)return"";for(var m=0,z=0,I=0,_=u.length;I!==_&&u[I]===0;)I++,m++;for(var T=(_-I)*d+1>>>0,f=new Uint8Array(T);I!==_;){for(var S=u[I],A=0,C=T-1;(S!==0||A<z)&&C!==-1;C--,A++)S+=256*f[C]>>>0,f[C]=S%a>>>0,S=S/a>>>0;if(S!==0)throw new Error("Non-zero carry");z=A,I++}for(var x=T-z;x!==T&&f[x]===0;)x++;for(var j=h.repeat(m);x<T;++x)j+=n.charAt(f[x]);return j}function y(u){if(typeof u!="string")throw new TypeError("Expected String");if(u.length===0)return new Uint8Array;var m=0;if(u[m]!==" "){for(var z=0,I=0;u[m]===h;)z++,m++;for(var _=(u.length-m)*l+1>>>0,T=new Uint8Array(_);u[m];){var f=t[u.charCodeAt(m)];if(f===255)return;for(var S=0,A=_-1;(f!==0||S<I)&&A!==-1;A--,S++)f+=a*T[A]>>>0,T[A]=f%256>>>0,f=f/256>>>0;if(f!==0)throw new Error("Non-zero carry");I=S,m++}if(u[m]!==" "){for(var C=_-I;C!==_&&T[C]===0;)C++;for(var x=new Uint8Array(z+(_-C)),j=z;C!==_;)x[j++]=T[C++];return x}}}function M(u){var m=y(u);if(m)return m;throw new Error(`Non-${e} character`)}return{encode:p,decodeUnsafe:y,decode:M}}var Ji=Hi,Xi=Ji;const Ne=n=>{if(n instanceof Uint8Array&&n.constructor.name==="Uint8Array")return n;if(n instanceof ArrayBuffer)return new Uint8Array(n);if(ArrayBuffer.isView(n))return new Uint8Array(n.buffer,n.byteOffset,n.byteLength);throw new Error("Unknown type, must be binary type")},Wi=n=>new TextEncoder().encode(n),Qi=n=>new TextDecoder().decode(n);class Zi{constructor(e,t,i){this.name=e,this.prefix=t,this.baseEncode=i}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}}class es{constructor(e,t,i){if(this.name=e,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.codePointAt(0),this.baseDecode=i}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return Ue(this,e)}}class ts{constructor(e){this.decoders=e}or(e){return Ue(this,e)}decode(e){const t=e[0],i=this.decoders[t];if(i)return i.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}}const Ue=(n,e)=>new ts({...n.decoders||{[n.prefix]:n},...e.decoders||{[e.prefix]:e}});class is{constructor(e,t,i,s){this.name=e,this.prefix=t,this.baseEncode=i,this.baseDecode=s,this.encoder=new Zi(e,t,i),this.decoder=new es(e,t,s)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}}const W=({name:n,prefix:e,encode:t,decode:i})=>new is(n,e,t,i),B=({prefix:n,name:e,alphabet:t})=>{const{encode:i,decode:s}=Xi(t,e);return W({prefix:n,name:e,encode:i,decode:r=>Ne(s(r))})},ss=(n,e,t,i)=>{const s={};for(let d=0;d<e.length;++d)s[e[d]]=d;let r=n.length;for(;n[r-1]==="=";)--r;const o=new Uint8Array(r*t/8|0);let a=0,h=0,l=0;for(let d=0;d<r;++d){const p=s[n[d]];if(p===void 0)throw new SyntaxError(`Non-${i} character`);h=h<<t|p,a+=t,a>=8&&(a-=8,o[l++]=255&h>>a)}if(a>=t||255&h<<8-a)throw new SyntaxError("Unexpected end of data");return o},rs=(n,e,t)=>{const i=e[e.length-1]==="=",s=(1<<t)-1;let r="",o=0,a=0;for(let h=0;h<n.length;++h)for(a=a<<8|n[h],o+=8;o>t;)o-=t,r+=e[s&a>>o];if(o&&(r+=e[s&a<<t-o]),i)for(;r.length*t&7;)r+="=";return r},g=({name:n,prefix:e,bitsPerChar:t,alphabet:i})=>W({prefix:e,name:n,encode(s){return rs(s,i,t)},decode(s){return ss(s,i,t,n)}}),ns=W({prefix:"\0",name:"identity",encode:n=>Qi(n),decode:n=>Wi(n)});var os=Object.freeze({__proto__:null,identity:ns});const as=g({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var hs=Object.freeze({__proto__:null,base2:as});const cs=g({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var us=Object.freeze({__proto__:null,base8:cs});const ls=B({prefix:"9",name:"base10",alphabet:"0123456789"});var ds=Object.freeze({__proto__:null,base10:ls});const gs=g({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),ps=g({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Ds=Object.freeze({__proto__:null,base16:gs,base16upper:ps});const ys=g({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),ms=g({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),bs=g({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),fs=g({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Es=g({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),ws=g({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),vs=g({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Is=g({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Cs=g({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Rs=Object.freeze({__proto__:null,base32:ys,base32upper:ms,base32pad:bs,base32padupper:fs,base32hex:Es,base32hexupper:ws,base32hexpad:vs,base32hexpadupper:Is,base32z:Cs});const _s=B({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Ts=B({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var Ss=Object.freeze({__proto__:null,base36:_s,base36upper:Ts});const Ps=B({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),xs=B({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Os=Object.freeze({__proto__:null,base58btc:Ps,base58flickr:xs});const As=g({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),zs=g({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Ns=g({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Us=g({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Ls=Object.freeze({__proto__:null,base64:As,base64pad:zs,base64url:Ns,base64urlpad:Us});const Le=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),$s=Le.reduce((n,e,t)=>(n[t]=e,n),[]),Fs=Le.reduce((n,e,t)=>(n[e.codePointAt(0)]=t,n),[]);function Ms(n){return n.reduce((e,t)=>(e+=$s[t],e),"")}function ks(n){const e=[];for(const t of n){const i=Fs[t.codePointAt(0)];if(i===void 0)throw new Error(`Non-base256emoji character: ${t}`);e.push(i)}return new Uint8Array(e)}const Ks=W({prefix:"\u{1F680}",name:"base256emoji",encode:Ms,decode:ks});var Bs=Object.freeze({__proto__:null,base256emoji:Ks}),Vs=Fe,$e=128,qs=127,js=~qs,Gs=Math.pow(2,31);function Fe(n,e,t){e=e||[],t=t||0;for(var i=t;n>=Gs;)e[t++]=n&255|$e,n/=128;for(;n&js;)e[t++]=n&255|$e,n>>>=7;return e[t]=n|0,Fe.bytes=t-i+1,e}var Ys=he,Hs=128,Me=127;function he(n,i){var t=0,i=i||0,s=0,r=i,o,a=n.length;do{if(r>=a)throw he.bytes=0,new RangeError("Could not decode varint");o=n[r++],t+=s<28?(o&Me)<<s:(o&Me)*Math.pow(2,s),s+=7}while(o>=Hs);return he.bytes=r-i,t}var Js=Math.pow(2,7),Xs=Math.pow(2,14),Ws=Math.pow(2,21),Qs=Math.pow(2,28),Zs=Math.pow(2,35),er=Math.pow(2,42),tr=Math.pow(2,49),ir=Math.pow(2,56),sr=Math.pow(2,63),rr=function(n){return n<Js?1:n<Xs?2:n<Ws?3:n<Qs?4:n<Zs?5:n<er?6:n<tr?7:n<ir?8:n<sr?9:10},nr={encode:Vs,decode:Ys,encodingLength:rr},ke=nr;const Ke=(n,e,t=0)=>(ke.encode(n,e,t),e),Be=n=>ke.encodingLength(n),ce=(n,e)=>{const t=e.byteLength,i=Be(n),s=i+Be(t),r=new Uint8Array(s+t);return Ke(n,r,0),Ke(t,r,i),r.set(e,s),new or(n,t,e,r)};class or{constructor(e,t,i,s){this.code=e,this.size=t,this.digest=i,this.bytes=s}}const Ve=({name:n,code:e,encode:t})=>new ar(n,e,t);class ar{constructor(e,t,i){this.name=e,this.code=t,this.encode=i}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ce(this.code,t):t.then(i=>ce(this.code,i))}else throw Error("Unknown type, must be binary type")}}const qe=n=>async e=>new Uint8Array(await crypto.subtle.digest(n,e)),hr=Ve({name:"sha2-256",code:18,encode:qe("SHA-256")}),cr=Ve({name:"sha2-512",code:19,encode:qe("SHA-512")});var ur=Object.freeze({__proto__:null,sha256:hr,sha512:cr});const je=0,lr="identity",Ge=Ne,dr=n=>ce(je,Ge(n)),gr={code:je,name:lr,encode:Ge,digest:dr};var pr=Object.freeze({__proto__:null,identity:gr});new TextEncoder,new TextDecoder;const Ye={...os,...hs,...us,...ds,...Ds,...Rs,...Ss,...Os,...Ls,...Bs};({...ur,...pr});function He(n){return globalThis.Buffer!=null?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):n}function Dr(n=0){return globalThis.Buffer!=null&&globalThis.Buffer.allocUnsafe!=null?He(globalThis.Buffer.allocUnsafe(n)):new Uint8Array(n)}function Je(n,e,t,i){return{name:n,prefix:e,encoder:{name:n,prefix:e,encode:t},decoder:{decode:i}}}const Xe=Je("utf8","u",n=>"u"+new TextDecoder("utf8").decode(n),n=>new TextEncoder().encode(n.substring(1))),ue=Je("ascii","a",n=>{let e="a";for(let t=0;t<n.length;t++)e+=String.fromCharCode(n[t]);return e},n=>{n=n.substring(1);const e=Dr(n.length);for(let t=0;t<n.length;t++)e[t]=n.charCodeAt(t);return e}),yr={utf8:Xe,"utf-8":Xe,hex:Ye.base16,latin1:ue,ascii:ue,binary:ue,...Ye};function mr(n,e="utf8"){const t=yr[e];if(!t)throw new Error(`Unsupported encoding "${e}"`);return(e==="utf8"||e==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?He(globalThis.Buffer.from(n,"utf-8")):t.decoder.decode(`${t.prefix}${n}`)}const le="wc",We=2,Q="core",O=`${le}@2:${Q}:`,Qe={name:Q,logger:"error"},Ze={database:":memory:"},et="crypto",de="client_ed25519_seed",tt=L,it="keychain",st="0.3",rt="messages",nt="0.3",ot=Fi,at="publisher",ht="irn",ct="error",ge="wss://relay.walletconnect.com",pe="wss://relay.walletconnect.org",ut="relayer",D={message:"relayer_message",message_ack:"relayer_message_ack",connect:"relayer_connect",disconnect:"relayer_disconnect",error:"relayer_error",connection_stalled:"relayer_connection_stalled",transport_closed:"relayer_transport_closed",publish:"relayer_publish"},lt="_subscription",P={payload:"payload",connect:"connect",disconnect:"disconnect",error:"error"},dt=Re,br={database:":memory:"},gt="2.11.2",pt=1e4,Dt="0.3",yt="WALLETCONNECT_CLIENT_ID",w={created:"subscription_created",deleted:"subscription_deleted",expired:"subscription_expired",disabled:"subscription_disabled",sync:"subscription_sync",resubscribed:"subscription_resubscribed"},fr=X,mt="subscription",bt="0.3",ft=_e*1e3,Et="pairing",wt="0.3",Er=X,$={wc_pairingDelete:{req:{ttl:L,prompt:!1,tag:1e3},res:{ttl:L,prompt:!1,tag:1001}},wc_pairingPing:{req:{ttl:Te,prompt:!1,tag:1002},res:{ttl:Te,prompt:!1,tag:1003}},unregistered_method:{req:{ttl:L,prompt:!1,tag:0},res:{ttl:L,prompt:!1,tag:0}}},V={create:"pairing_create",expire:"pairing_expire",delete:"pairing_delete",ping:"pairing_ping"},R={created:"history_created",updated:"history_updated",deleted:"history_deleted",sync:"history_sync"},vt="history",It="0.3",Ct="expirer",v={created:"expirer_created",deleted:"expirer_deleted",expired:"expirer_expired",sync:"expirer_sync"},Rt="0.3",wr=L,Z="verify-api",F="https://verify.walletconnect.com",ee="https://verify.walletconnect.org",_t=[F,ee],Tt="echo",St="https://echo.walletconnect.com";class Pt{constructor(e,t){this.core=e,this.logger=t,this.keychain=new Map,this.name=it,this.version=st,this.initialized=!1,this.storagePrefix=O,this.init=async()=>{if(!this.initialized){const i=await this.getKeyChain();typeof i<"u"&&(this.keychain=i),this.initialized=!0}},this.has=i=>(this.isInitialized(),this.keychain.has(i)),this.set=async(i,s)=>{this.isInitialized(),this.keychain.set(i,s),await this.persist()},this.get=i=>{this.isInitialized();const s=this.keychain.get(i);if(typeof s>"u"){const{message:r}=c("NO_MATCHING_KEY",`${this.name}: ${i}`);throw new Error(r)}return s},this.del=async i=>{this.isInitialized(),this.keychain.delete(i),await this.persist()},this.core=e,this.logger=b(t,this.name)}get context(){return E(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name}async setKeyChain(e){await this.core.storage.setItem(this.storageKey,be(e))}async getKeyChain(){const e=await this.core.storage.getItem(this.storageKey);return typeof e<"u"?fe(e):void 0}async persist(){await this.setKeyChain(this.keychain)}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}}class xt{constructor(e,t,i){this.core=e,this.logger=t,this.name=et,this.initialized=!1,this.init=async()=>{this.initialized||(await this.keychain.init(),this.initialized=!0)},this.hasKeys=s=>(this.isInitialized(),this.keychain.has(s)),this.getClientId=async()=>{this.isInitialized();const s=await this.getClientSeed(),r=Y.generateKeyPair(s);return Y.encodeIss(r.publicKey)},this.generateKeyPair=()=>{this.isInitialized();const s=li();return this.setPrivateKey(s.publicKey,s.privateKey)},this.signJWT=async s=>{this.isInitialized();const r=await this.getClientSeed(),o=Y.generateKeyPair(r),a=ie(),h=tt;return await Y.signJWT(a,s,h,o)},this.generateSharedKey=(s,r,o)=>{this.isInitialized();const a=this.getPrivateKey(s),h=di(a,r);return this.setSymKey(h,o)},this.setSymKey=async(s,r)=>{this.isInitialized();const o=r||gi(s);return await this.keychain.set(o,s),o},this.deleteKeyPair=async s=>{this.isInitialized(),await this.keychain.del(s)},this.deleteSymKey=async s=>{this.isInitialized(),await this.keychain.del(s)},this.encode=async(s,r,o)=>{this.isInitialized();const a=pi(o),h=ci(r);if(Ee(a)){const y=a.senderPublicKey,M=a.receiverPublicKey;s=await this.generateSharedKey(y,M)}const l=this.getSymKey(s),{type:d,senderPublicKey:p}=a;return Di({type:d,symKey:l,message:h,senderPublicKey:p})},this.decode=async(s,r,o)=>{this.isInitialized();const a=yi(r,o);if(Ee(a)){const h=a.receiverPublicKey,l=a.senderPublicKey;s=await this.generateSharedKey(h,l)}try{const h=this.getSymKey(s),l=mi({symKey:h,encoded:r});return ui(l)}catch(h){this.logger.error(`Failed to decode message from topic: '${s}', clientId: '${await this.getClientId()}'`),this.logger.error(h)}},this.getPayloadType=s=>{const r=we(s);return bi(r.type)},this.getPayloadSenderPublicKey=s=>{const r=we(s);return r.senderPublicKey?$i(r.senderPublicKey,fi):void 0},this.core=e,this.logger=b(t,this.name),this.keychain=i||new Pt(this.core,this.logger)}get context(){return E(this.logger)}async setPrivateKey(e,t){return await this.keychain.set(e,t),e}getPrivateKey(e){return this.keychain.get(e)}async getClientSeed(){let e="";try{e=this.keychain.get(de)}catch{e=ie(),await this.keychain.set(de,e)}return mr(e,"base16")}getSymKey(e){return this.keychain.get(e)}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}}class Ot extends Zt{constructor(e,t){super(e,t),this.logger=e,this.core=t,this.messages=new Map,this.name=rt,this.version=nt,this.initialized=!1,this.storagePrefix=O,this.init=async()=>{if(!this.initialized){this.logger.trace("Initialized");try{const i=await this.getRelayerMessages();typeof i<"u"&&(this.messages=i),this.logger.debug(`Successfully Restored records for ${this.name}`),this.logger.trace({type:"method",method:"restore",size:this.messages.size})}catch(i){this.logger.debug(`Failed to Restore records for ${this.name}`),this.logger.error(i)}finally{this.initialized=!0}}},this.set=async(i,s)=>{this.isInitialized();const r=se(s);let o=this.messages.get(i);return typeof o>"u"&&(o={}),typeof o[r]<"u"||(o[r]=s,this.messages.set(i,o),await this.persist()),r},this.get=i=>{this.isInitialized();let s=this.messages.get(i);return typeof s>"u"&&(s={}),s},this.has=(i,s)=>{this.isInitialized();const r=this.get(i),o=se(s);return typeof r[o]<"u"},this.del=async i=>{this.isInitialized(),this.messages.delete(i),await this.persist()},this.logger=b(e,this.name),this.core=t}get context(){return E(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name}async setRelayerMessages(e){await this.core.storage.setItem(this.storageKey,be(e))}async getRelayerMessages(){const e=await this.core.storage.getItem(this.storageKey);return typeof e<"u"?fe(e):void 0}async persist(){await this.setRelayerMessages(this.messages)}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}}class vr extends ei{constructor(e,t){super(e,t),this.relayer=e,this.logger=t,this.events=new U,this.name=at,this.queue=new Map,this.publishTimeout=N(Mi*2),this.needsTransportRestart=!1,this.publish=async(i,s,r)=>{var o;this.logger.debug("Publishing Payload"),this.logger.trace({type:"method",method:"publish",params:{topic:i,message:s,opts:r}});try{const a=r?.ttl||ot,h=re(r),l=r?.prompt||!1,d=r?.tag||0,p=r?.id||Bi().toString(),y={topic:i,message:s,opts:{ttl:a,relay:h,prompt:l,tag:d,id:p}},M=setTimeout(()=>this.queue.set(p,y),this.publishTimeout);try{await await k(this.rpcPublish(i,s,a,h,l,d,p),this.publishTimeout,`Failed to publish payload, please try again. id:${p} tag:${d}`),this.removeRequestFromQueue(p),this.relayer.events.emit(D.publish,y)}catch(u){if(this.logger.debug("Publishing Payload stalled"),this.needsTransportRestart=!0,(o=r?.internal)!=null&&o.throwOnFailedPublish)throw this.removeRequestFromQueue(p),u;return}finally{clearTimeout(M)}this.logger.debug("Successfully Published Payload"),this.logger.trace({type:"method",method:"publish",params:{topic:i,message:s,opts:r}})}catch(a){throw this.logger.debug("Failed to Publish Payload"),this.logger.error(a),a}},this.on=(i,s)=>{this.events.on(i,s)},this.once=(i,s)=>{this.events.once(i,s)},this.off=(i,s)=>{this.events.off(i,s)},this.removeListener=(i,s)=>{this.events.removeListener(i,s)},this.relayer=e,this.logger=b(t,this.name),this.registerEventListeners()}get context(){return E(this.logger)}rpcPublish(e,t,i,s,r,o,a){var h,l,d,p;const y={method:H(s.protocol).publish,params:{topic:e,message:t,ttl:i,prompt:r,tag:o},id:a};return ne((h=y.params)==null?void 0:h.prompt)&&((l=y.params)==null||delete l.prompt),ne((d=y.params)==null?void 0:d.tag)&&((p=y.params)==null||delete p.tag),this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"message",direction:"outgoing",request:y}),this.relayer.request(y)}removeRequestFromQueue(e){this.queue.delete(e)}checkQueue(){this.queue.forEach(async e=>{const{topic:t,message:i,opts:s}=e;await this.publish(t,i,s)})}registerEventListeners(){this.relayer.core.heartbeat.on(G.pulse,()=>{if(this.needsTransportRestart){this.needsTransportRestart=!1,this.relayer.events.emit(D.connection_stalled);return}this.checkQueue()}),this.relayer.on(D.message_ack,e=>{this.removeRequestFromQueue(e.id.toString())})}}class Ir{constructor(){this.map=new Map,this.set=(e,t)=>{const i=this.get(e);this.exists(e,t)||this.map.set(e,[...i,t])},this.get=e=>this.map.get(e)||[],this.exists=(e,t)=>this.get(e).includes(t),this.delete=(e,t)=>{if(typeof t>"u"){this.map.delete(e);return}if(!this.map.has(e))return;const i=this.get(e);if(!this.exists(e,t))return;const s=i.filter(r=>r!==t);if(!s.length){this.map.delete(e);return}this.map.set(e,s)},this.clear=()=>{this.map.clear()}}get topics(){return Array.from(this.map.keys())}}var Cr=Object.defineProperty,Rr=Object.defineProperties,_r=Object.getOwnPropertyDescriptors,At=Object.getOwnPropertySymbols,Tr=Object.prototype.hasOwnProperty,Sr=Object.prototype.propertyIsEnumerable,zt=(n,e,t)=>e in n?Cr(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,q=(n,e)=>{for(var t in e||(e={}))Tr.call(e,t)&&zt(n,t,e[t]);if(At)for(var t of At(e))Sr.call(e,t)&&zt(n,t,e[t]);return n},De=(n,e)=>Rr(n,_r(e));class Nt extends ti{constructor(e,t){super(e,t),this.relayer=e,this.logger=t,this.subscriptions=new Map,this.topicMap=new Ir,this.events=new U,this.name=mt,this.version=bt,this.pending=new Map,this.cached=[],this.initialized=!1,this.pendingSubscriptionWatchLabel="pending_sub_watch_label",this.pollingInterval=20,this.storagePrefix=O,this.subscribeTimeout=1e4,this.restartInProgress=!1,this.batchSubscribeTopicsLimit=500,this.init=async()=>{this.initialized||(this.logger.trace("Initialized"),this.registerEventListeners(),this.clientId=await this.relayer.core.crypto.getClientId())},this.subscribe=async(i,s)=>{await this.restartToComplete(),this.isInitialized(),this.logger.debug("Subscribing Topic"),this.logger.trace({type:"method",method:"subscribe",params:{topic:i,opts:s}});try{const r=re(s),o={topic:i,relay:r};this.pending.set(i,o);const a=await this.rpcSubscribe(i,r);return this.onSubscribe(a,o),this.logger.debug("Successfully Subscribed Topic"),this.logger.trace({type:"method",method:"subscribe",params:{topic:i,opts:s}}),a}catch(r){throw this.logger.debug("Failed to Subscribe Topic"),this.logger.error(r),r}},this.unsubscribe=async(i,s)=>{await this.restartToComplete(),this.isInitialized(),typeof s?.id<"u"?await this.unsubscribeById(i,s.id,s):await this.unsubscribeByTopic(i,s)},this.isSubscribed=async i=>{if(this.topics.includes(i))return!0;const s=`${this.pendingSubscriptionWatchLabel}_${i}`;return await new Promise((r,o)=>{const a=new ki;a.start(s);const h=setInterval(()=>{!this.pending.has(i)&&this.topics.includes(i)&&(clearInterval(h),a.stop(s),r(!0)),a.elapsed(s)>=ft&&(clearInterval(h),a.stop(s),o(new Error("Subscription resolution timeout")))},this.pollingInterval)}).catch(()=>!1)},this.on=(i,s)=>{this.events.on(i,s)},this.once=(i,s)=>{this.events.once(i,s)},this.off=(i,s)=>{this.events.off(i,s)},this.removeListener=(i,s)=>{this.events.removeListener(i,s)},this.restart=async()=>{this.restartInProgress=!0,await this.restore(),await this.reset(),this.restartInProgress=!1},this.relayer=e,this.logger=b(t,this.name),this.clientId=""}get context(){return E(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.relayer.core.customStoragePrefix+"//"+this.name}get length(){return this.subscriptions.size}get ids(){return Array.from(this.subscriptions.keys())}get values(){return Array.from(this.subscriptions.values())}get topics(){return this.topicMap.topics}hasSubscription(e,t){let i=!1;try{i=this.getSubscription(e).topic===t}catch{}return i}onEnable(){this.cached=[],this.initialized=!0}onDisable(){this.cached=this.values,this.subscriptions.clear(),this.topicMap.clear()}async unsubscribeByTopic(e,t){const i=this.topicMap.get(e);await Promise.all(i.map(async s=>await this.unsubscribeById(e,s,t)))}async unsubscribeById(e,t,i){this.logger.debug("Unsubscribing Topic"),this.logger.trace({type:"method",method:"unsubscribe",params:{topic:e,id:t,opts:i}});try{const s=re(i);await this.rpcUnsubscribe(e,t,s);const r=K("USER_DISCONNECTED",`${this.name}, ${e}`);await this.onUnsubscribe(e,t,r),this.logger.debug("Successfully Unsubscribed Topic"),this.logger.trace({type:"method",method:"unsubscribe",params:{topic:e,id:t,opts:i}})}catch(s){throw this.logger.debug("Failed to Unsubscribe Topic"),this.logger.error(s),s}}async rpcSubscribe(e,t){const i={method:H(t.protocol).subscribe,params:{topic:e}};this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"payload",direction:"outgoing",request:i});try{await await k(this.relayer.request(i),this.subscribeTimeout)}catch{this.logger.debug("Outgoing Relay Subscribe Payload stalled"),this.relayer.events.emit(D.connection_stalled)}return se(e+this.clientId)}async rpcBatchSubscribe(e){if(!e.length)return;const t=e[0].relay,i={method:H(t.protocol).batchSubscribe,params:{topics:e.map(s=>s.topic)}};this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"payload",direction:"outgoing",request:i});try{return await await k(this.relayer.request(i),this.subscribeTimeout)}catch{this.logger.debug("Outgoing Relay Payload stalled"),this.relayer.events.emit(D.connection_stalled)}}rpcUnsubscribe(e,t,i){const s={method:H(i.protocol).unsubscribe,params:{topic:e,id:t}};return this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"payload",direction:"outgoing",request:s}),this.relayer.request(s)}onSubscribe(e,t){this.setSubscription(e,De(q({},t),{id:e})),this.pending.delete(t.topic)}onBatchSubscribe(e){e.length&&e.forEach(t=>{this.setSubscription(t.id,q({},t)),this.pending.delete(t.topic)})}async onUnsubscribe(e,t,i){this.events.removeAllListeners(t),this.hasSubscription(t,e)&&this.deleteSubscription(t,i),await this.relayer.messages.del(e)}async setRelayerSubscriptions(e){await this.relayer.core.storage.setItem(this.storageKey,e)}async getRelayerSubscriptions(){return await this.relayer.core.storage.getItem(this.storageKey)}setSubscription(e,t){this.subscriptions.has(e)||(this.logger.debug("Setting subscription"),this.logger.trace({type:"method",method:"setSubscription",id:e,subscription:t}),this.addSubscription(e,t))}addSubscription(e,t){this.subscriptions.set(e,q({},t)),this.topicMap.set(t.topic,e),this.events.emit(w.created,t)}getSubscription(e){this.logger.debug("Getting subscription"),this.logger.trace({type:"method",method:"getSubscription",id:e});const t=this.subscriptions.get(e);if(!t){const{message:i}=c("NO_MATCHING_KEY",`${this.name}: ${e}`);throw new Error(i)}return t}deleteSubscription(e,t){this.logger.debug("Deleting subscription"),this.logger.trace({type:"method",method:"deleteSubscription",id:e,reason:t});const i=this.getSubscription(e);this.subscriptions.delete(e),this.topicMap.delete(i.topic,e),this.events.emit(w.deleted,De(q({},i),{reason:t}))}async persist(){await this.setRelayerSubscriptions(this.values),this.events.emit(w.sync)}async reset(){if(this.cached.length){const e=Math.ceil(this.cached.length/this.batchSubscribeTopicsLimit);for(let t=0;t<e;t++){const i=this.cached.splice(0,this.batchSubscribeTopicsLimit);await this.batchSubscribe(i)}}this.events.emit(w.resubscribed)}async restore(){try{const e=await this.getRelayerSubscriptions();if(typeof e>"u"||!e.length)return;if(this.subscriptions.size){const{message:t}=c("RESTORE_WILL_OVERRIDE",this.name);throw this.logger.error(t),this.logger.error(`${this.name}: ${JSON.stringify(this.values)}`),new Error(t)}this.cached=e,this.logger.debug(`Successfully Restored subscriptions for ${this.name}`),this.logger.trace({type:"method",method:"restore",subscriptions:this.values})}catch(e){this.logger.debug(`Failed to Restore subscriptions for ${this.name}`),this.logger.error(e)}}async batchSubscribe(e){if(!e.length)return;const t=await this.rpcBatchSubscribe(e);Ei(t)&&this.onBatchSubscribe(t.map((i,s)=>De(q({},e[s]),{id:i})))}async onConnect(){this.restartInProgress||(await this.restart(),this.onEnable())}onDisconnect(){this.onDisable()}async checkPending(){if(!this.initialized||this.relayer.transportExplicitlyClosed)return;const e=[];this.pending.forEach(t=>{e.push(t)}),await this.batchSubscribe(e)}registerEventListeners(){this.relayer.core.heartbeat.on(G.pulse,async()=>{await this.checkPending()}),this.relayer.on(D.connect,async()=>{await this.onConnect()}),this.relayer.on(D.disconnect,()=>{this.onDisconnect()}),this.events.on(w.created,async e=>{const t=w.created;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,data:e}),await this.persist()}),this.events.on(w.deleted,async e=>{const t=w.deleted;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,data:e}),await this.persist()})}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}async restartToComplete(){this.restartInProgress&&await new Promise(e=>{const t=setInterval(()=>{this.restartInProgress||(clearInterval(t),e())},this.pollingInterval)})}}var Pr=Object.defineProperty,Ut=Object.getOwnPropertySymbols,xr=Object.prototype.hasOwnProperty,Or=Object.prototype.propertyIsEnumerable,Lt=(n,e,t)=>e in n?Pr(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,Ar=(n,e)=>{for(var t in e||(e={}))xr.call(e,t)&&Lt(n,t,e[t]);if(Ut)for(var t of Ut(e))Or.call(e,t)&&Lt(n,t,e[t]);return n};class $t extends ii{constructor(e){super(e),this.protocol="wc",this.version=2,this.events=new U,this.name=ut,this.transportExplicitlyClosed=!1,this.initialized=!1,this.connectionAttemptInProgress=!1,this.connectionStatusPollingInterval=20,this.staleConnectionErrors=["socket hang up","socket stalled"],this.hasExperiencedNetworkDisruption=!1,this.requestsInFlight=new Map,this.request=async t=>{this.logger.debug("Publishing Request Payload");const i=t.id;try{await this.toEstablishConnection();const s=this.provider.request(t);return this.requestsInFlight.set(i,{promise:s,request:t}),await s}catch(s){throw this.logger.debug("Failed to Publish Request"),this.logger.error(s),s}finally{this.requestsInFlight.delete(i)}},this.onPayloadHandler=t=>{this.onProviderPayload(t)},this.onConnectHandler=()=>{this.events.emit(D.connect)},this.onDisconnectHandler=()=>{this.onProviderDisconnect()},this.onProviderErrorHandler=t=>{this.logger.error(t),this.events.emit(D.error,t),this.logger.info("Fatal socket error received, closing transport"),this.transportClose()},this.registerProviderListeners=()=>{this.provider.on(P.payload,this.onPayloadHandler),this.provider.on(P.connect,this.onConnectHandler),this.provider.on(P.disconnect,this.onDisconnectHandler),this.provider.on(P.error,this.onProviderErrorHandler)},this.core=e.core,this.logger=typeof e.logger<"u"&&typeof e.logger!="string"?b(e.logger,this.name):ye(me({level:e.logger||ct})),this.messages=new Ot(this.logger,e.core),this.subscriber=new Nt(this,this.logger),this.publisher=new vr(this,this.logger),this.relayUrl=e?.relayUrl||ge,this.projectId=e.projectId,this.bundleId=wi(),this.provider={}}async init(){this.logger.trace("Initialized"),this.registerEventListeners(),await this.createProvider(),await Promise.all([this.messages.init(),this.subscriber.init()]);try{await this.transportOpen()}catch{this.logger.warn(`Connection via ${this.relayUrl} failed, attempting to connect via failover domain ${pe}...`),await this.restartTransport(pe)}this.initialized=!0,setTimeout(async()=>{this.subscriber.topics.length===0&&(this.logger.info("No topics subscribed to after init, closing transport"),await this.transportClose(),this.transportExplicitlyClosed=!1)},pt)}get context(){return E(this.logger)}get connected(){return this.provider.connection.connected}get connecting(){return this.provider.connection.connecting}async publish(e,t,i){this.isInitialized(),await this.publisher.publish(e,t,i),await this.recordMessageEvent({topic:e,message:t,publishedAt:Date.now()})}async subscribe(e,t){var i;this.isInitialized();let s=((i=this.subscriber.topicMap.get(e))==null?void 0:i[0])||"";if(s)return s;let r;const o=a=>{a.topic===e&&(this.subscriber.off(w.created,o),r())};return await Promise.all([new Promise(a=>{r=a,this.subscriber.on(w.created,o)}),new Promise(async a=>{s=await this.subscriber.subscribe(e,t),a()})]),s}async unsubscribe(e,t){this.isInitialized(),await this.subscriber.unsubscribe(e,t)}on(e,t){this.events.on(e,t)}once(e,t){this.events.once(e,t)}off(e,t){this.events.off(e,t)}removeListener(e,t){this.events.removeListener(e,t)}async transportClose(){this.requestsInFlight.size>0&&(this.logger.debug("Waiting for all in-flight requests to finish before closing transport..."),this.requestsInFlight.forEach(async e=>{await e.promise})),this.transportExplicitlyClosed=!0,this.hasExperiencedNetworkDisruption&&this.connected?await k(this.provider.disconnect(),1e3,"provider.disconnect()").catch(()=>this.onProviderDisconnect()):this.connected&&await this.provider.disconnect()}async transportOpen(e){if(this.transportExplicitlyClosed=!1,await this.confirmOnlineStateOrThrow(),!this.connectionAttemptInProgress){e&&e!==this.relayUrl&&(this.relayUrl=e,await this.transportClose(),await this.createProvider()),this.connectionAttemptInProgress=!0;try{await Promise.all([new Promise(t=>{if(!this.initialized)return t();this.subscriber.once(w.resubscribed,()=>{t()})}),new Promise(async(t,i)=>{try{await k(this.provider.connect(),1e4,`Socket stalled when trying to connect to ${this.relayUrl}`)}catch(s){i(s);return}t()})])}catch(t){this.logger.error(t);const i=t;if(!this.isConnectionStalled(i.message))throw t;this.provider.events.emit(P.disconnect)}finally{this.connectionAttemptInProgress=!1,this.hasExperiencedNetworkDisruption=!1}}}async restartTransport(e){await this.confirmOnlineStateOrThrow(),!this.connectionAttemptInProgress&&(this.relayUrl=e||this.relayUrl,await this.transportClose(),await this.createProvider(),await this.transportOpen())}async confirmOnlineStateOrThrow(){if(!await ve())throw new Error("No internet connection detected. Please restart your network and try again.")}isConnectionStalled(e){return this.staleConnectionErrors.some(t=>e.includes(t))}async createProvider(){this.provider.connection&&this.unregisterProviderListeners();const e=await this.core.crypto.signJWT(this.relayUrl);this.provider=new Ki(new ji(vi({sdkVersion:gt,protocol:this.protocol,version:this.version,relayUrl:this.relayUrl,projectId:this.projectId,auth:e,useOnCloseEvent:!0,bundleId:this.bundleId}))),this.registerProviderListeners()}async recordMessageEvent(e){const{topic:t,message:i}=e;await this.messages.set(t,i)}async shouldIgnoreMessageEvent(e){const{topic:t,message:i}=e;if(!i||i.length===0)return this.logger.debug(`Ignoring invalid/empty message: ${i}`),!0;if(!await this.subscriber.isSubscribed(t))return this.logger.debug(`Ignoring message for non-subscribed topic ${t}`),!0;const s=this.messages.has(t,i);return s&&this.logger.debug(`Ignoring duplicate message: ${i}`),s}async onProviderPayload(e){if(this.logger.debug("Incoming Relay Payload"),this.logger.trace({type:"payload",direction:"incoming",payload:e}),Pe(e)){if(!e.method.endsWith(lt))return;const t=e.params,{topic:i,message:s,publishedAt:r}=t.data,o={topic:i,message:s,publishedAt:r};this.logger.debug("Emitting Relayer Payload"),this.logger.trace(Ar({type:"event",event:t.id},o)),this.events.emit(t.id,o),await this.acknowledgePayload(e),await this.onMessageEvent(o)}else xe(e)&&this.events.emit(D.message_ack,e)}async onMessageEvent(e){await this.shouldIgnoreMessageEvent(e)||(this.events.emit(D.message,e),await this.recordMessageEvent(e))}async acknowledgePayload(e){const t=Oe(e.id,!0);await this.provider.connection.send(t)}unregisterProviderListeners(){this.provider.off(P.payload,this.onPayloadHandler),this.provider.off(P.connect,this.onConnectHandler),this.provider.off(P.disconnect,this.onDisconnectHandler),this.provider.off(P.error,this.onProviderErrorHandler)}async registerEventListeners(){this.events.on(D.connection_stalled,()=>{this.restartTransport().catch(t=>this.logger.error(t))});let e=await ve();Ii(async t=>{this.initialized&&e!==t&&(e=t,t?await this.restartTransport().catch(i=>this.logger.error(i)):(this.hasExperiencedNetworkDisruption=!0,await this.transportClose().catch(i=>this.logger.error(i))))})}onProviderDisconnect(){this.events.emit(D.disconnect),this.attemptToReconnect()}attemptToReconnect(){this.transportExplicitlyClosed||(this.logger.info("attemptToReconnect called. Connecting..."),setTimeout(async()=>{await this.restartTransport().catch(e=>this.logger.error(e))},N(dt)))}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}async toEstablishConnection(){if(await this.confirmOnlineStateOrThrow(),!this.connected){if(this.connectionAttemptInProgress)return await new Promise(e=>{const t=setInterval(()=>{this.connected&&(clearInterval(t),e())},this.connectionStatusPollingInterval)});await this.restartTransport()}}}var zr=Object.defineProperty,Ft=Object.getOwnPropertySymbols,Nr=Object.prototype.hasOwnProperty,Ur=Object.prototype.propertyIsEnumerable,Mt=(n,e,t)=>e in n?zr(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,kt=(n,e)=>{for(var t in e||(e={}))Nr.call(e,t)&&Mt(n,t,e[t]);if(Ft)for(var t of Ft(e))Ur.call(e,t)&&Mt(n,t,e[t]);return n};class Kt extends si{constructor(e,t,i,s=O,r=void 0){super(e,t,i,s),this.core=e,this.logger=t,this.name=i,this.map=new Map,this.version=Dt,this.cached=[],this.initialized=!1,this.storagePrefix=O,this.init=async()=>{this.initialized||(this.logger.trace("Initialized"),await this.restore(),this.cached.forEach(o=>{this.getKey&&o!==null&&!ne(o)?this.map.set(this.getKey(o),o):Ci(o)?this.map.set(o.id,o):Ri(o)&&this.map.set(o.topic,o)}),this.cached=[],this.initialized=!0)},this.set=async(o,a)=>{this.isInitialized(),this.map.has(o)?await this.update(o,a):(this.logger.debug("Setting value"),this.logger.trace({type:"method",method:"set",key:o,value:a}),this.map.set(o,a),await this.persist())},this.get=o=>(this.isInitialized(),this.logger.debug("Getting value"),this.logger.trace({type:"method",method:"get",key:o}),this.getData(o)),this.getAll=o=>(this.isInitialized(),o?this.values.filter(a=>Object.keys(o).every(h=>Gi(a[h],o[h]))):this.values),this.update=async(o,a)=>{this.isInitialized(),this.logger.debug("Updating value"),this.logger.trace({type:"method",method:"update",key:o,update:a});const h=kt(kt({},this.getData(o)),a);this.map.set(o,h),await this.persist()},this.delete=async(o,a)=>{this.isInitialized(),this.map.has(o)&&(this.logger.debug("Deleting value"),this.logger.trace({type:"method",method:"delete",key:o,reason:a}),this.map.delete(o),await this.persist())},this.logger=b(t,this.name),this.storagePrefix=s,this.getKey=r}get context(){return E(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name}get length(){return this.map.size}get keys(){return Array.from(this.map.keys())}get values(){return Array.from(this.map.values())}async setDataStore(e){await this.core.storage.setItem(this.storageKey,e)}async getDataStore(){return await this.core.storage.getItem(this.storageKey)}getData(e){const t=this.map.get(e);if(!t){const{message:i}=c("NO_MATCHING_KEY",`${this.name}: ${e}`);throw this.logger.error(i),new Error(i)}return t}async persist(){await this.setDataStore(this.values)}async restore(){try{const e=await this.getDataStore();if(typeof e>"u"||!e.length)return;if(this.map.size){const{message:t}=c("RESTORE_WILL_OVERRIDE",this.name);throw this.logger.error(t),new Error(t)}this.cached=e,this.logger.debug(`Successfully Restored value for ${this.name}`),this.logger.trace({type:"method",method:"restore",value:this.values})}catch(e){this.logger.debug(`Failed to Restore value for ${this.name}`),this.logger.error(e)}}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}}class Bt{constructor(e,t){this.core=e,this.logger=t,this.name=Et,this.version=wt,this.events=new Xt,this.initialized=!1,this.storagePrefix=O,this.ignoredPayloadTypes=[_i],this.registeredMethods=[],this.init=async()=>{this.initialized||(await this.pairings.init(),await this.cleanup(),this.registerRelayerEvents(),this.registerExpirerEvents(),this.initialized=!0,this.logger.trace("Initialized"))},this.register=({methods:i})=>{this.isInitialized(),this.registeredMethods=[...new Set([...this.registeredMethods,...i])]},this.create=async()=>{this.isInitialized();const i=ie(),s=await this.core.crypto.setSymKey(i),r=J(Se),o={protocol:ht},a={topic:s,expiry:r,relay:o,active:!1},h=Ti({protocol:this.core.protocol,version:this.core.version,topic:s,symKey:i,relay:o,expiryTimestamp:r});return await this.pairings.set(s,a),await this.core.relayer.subscribe(s),this.core.expirer.set(s,r),{topic:s,uri:h}},this.pair=async i=>{this.isInitialized(),this.isValidPair(i);const{topic:s,symKey:r,relay:o,expiryTimestamp:a}=Ie(i.uri);let h;if(this.pairings.keys.includes(s)&&(h=this.pairings.get(s),h.active))throw new Error(`Pairing already exists: ${s}. Please try again with a new connection URI.`);const l=a||J(Se),d={topic:s,relay:o,expiry:l,active:!1};return await this.pairings.set(s,d),this.core.expirer.set(s,l),i.activatePairing&&await this.activate({topic:s}),this.events.emit(V.create,d),this.core.crypto.keychain.has(s)||(await this.core.crypto.setSymKey(r,s),await this.core.relayer.subscribe(s,{relay:o})),d},this.activate=async({topic:i})=>{this.isInitialized();const s=J(X);await this.pairings.update(i,{active:!0,expiry:s}),this.core.expirer.set(i,s)},this.ping=async i=>{this.isInitialized(),await this.isValidPing(i);const{topic:s}=i;if(this.pairings.keys.includes(s)){const r=await this.sendRequest(s,"wc_pairingPing",{}),{done:o,resolve:a,reject:h}=Si();this.events.once(oe("pairing_ping",r),({error:l})=>{l?h(l):a()}),await o()}},this.updateExpiry=async({topic:i,expiry:s})=>{this.isInitialized(),await this.pairings.update(i,{expiry:s})},this.updateMetadata=async({topic:i,metadata:s})=>{this.isInitialized(),await this.pairings.update(i,{peerMetadata:s})},this.getPairings=()=>(this.isInitialized(),this.pairings.values),this.disconnect=async i=>{this.isInitialized(),await this.isValidDisconnect(i);const{topic:s}=i;this.pairings.keys.includes(s)&&(await this.sendRequest(s,"wc_pairingDelete",K("USER_DISCONNECTED")),await this.deletePairing(s))},this.sendRequest=async(i,s,r)=>{const o=Ae(s,r),a=await this.core.crypto.encode(i,o),h=$[s].req;return this.core.history.set(i,o),this.core.relayer.publish(i,a,h),o.id},this.sendResult=async(i,s,r)=>{const o=Oe(i,r),a=await this.core.crypto.encode(s,o),h=await this.core.history.get(s,i),l=$[h.request.method].res;await this.core.relayer.publish(s,a,l),await this.core.history.resolve(o)},this.sendError=async(i,s,r)=>{const o=Vi(i,r),a=await this.core.crypto.encode(s,o),h=await this.core.history.get(s,i),l=$[h.request.method]?$[h.request.method].res:$.unregistered_method.res;await this.core.relayer.publish(s,a,l),await this.core.history.resolve(o)},this.deletePairing=async(i,s)=>{await this.core.relayer.unsubscribe(i),await Promise.all([this.pairings.delete(i,K("USER_DISCONNECTED")),this.core.crypto.deleteSymKey(i),s?Promise.resolve():this.core.expirer.del(i)])},this.cleanup=async()=>{const i=this.pairings.getAll().filter(s=>Ce(s.expiry));await Promise.all(i.map(s=>this.deletePairing(s.topic)))},this.onRelayEventRequest=i=>{const{topic:s,payload:r}=i;switch(r.method){case"wc_pairingPing":return this.onPairingPingRequest(s,r);case"wc_pairingDelete":return this.onPairingDeleteRequest(s,r);default:return this.onUnknownRpcMethodRequest(s,r)}},this.onRelayEventResponse=async i=>{const{topic:s,payload:r}=i,o=(await this.core.history.get(s,r.id)).request.method;switch(o){case"wc_pairingPing":return this.onPairingPingResponse(s,r);default:return this.onUnknownRpcMethodResponse(o)}},this.onPairingPingRequest=async(i,s)=>{const{id:r}=s;try{this.isValidPing({topic:i}),await this.sendResult(r,i,!0),this.events.emit(V.ping,{id:r,topic:i})}catch(o){await this.sendError(r,i,o),this.logger.error(o)}},this.onPairingPingResponse=(i,s)=>{const{id:r}=s;setTimeout(()=>{qi(s)?this.events.emit(oe("pairing_ping",r),{}):ze(s)&&this.events.emit(oe("pairing_ping",r),{error:s.error})},500)},this.onPairingDeleteRequest=async(i,s)=>{const{id:r}=s;try{this.isValidDisconnect({topic:i}),await this.deletePairing(i),this.events.emit(V.delete,{id:r,topic:i})}catch(o){await this.sendError(r,i,o),this.logger.error(o)}},this.onUnknownRpcMethodRequest=async(i,s)=>{const{id:r,method:o}=s;try{if(this.registeredMethods.includes(o))return;const a=K("WC_METHOD_UNSUPPORTED",o);await this.sendError(r,i,a),this.logger.error(a)}catch(a){await this.sendError(r,i,a),this.logger.error(a)}},this.onUnknownRpcMethodResponse=i=>{this.registeredMethods.includes(i)||this.logger.error(K("WC_METHOD_UNSUPPORTED",i))},this.isValidPair=i=>{var s;if(!ae(i)){const{message:o}=c("MISSING_OR_INVALID",`pair() params: ${i}`);throw new Error(o)}if(!Pi(i.uri)){const{message:o}=c("MISSING_OR_INVALID",`pair() uri: ${i.uri}`);throw new Error(o)}const r=Ie(i.uri);if(!((s=r?.relay)!=null&&s.protocol)){const{message:o}=c("MISSING_OR_INVALID","pair() uri#relay-protocol");throw new Error(o)}if(!(r!=null&&r.symKey)){const{message:o}=c("MISSING_OR_INVALID","pair() uri#symKey");throw new Error(o)}if(r!=null&&r.expiryTimestamp&&N(r?.expiryTimestamp)<Date.now()){const{message:o}=c("EXPIRED","pair() URI has expired. Please try again with a new connection URI.");throw new Error(o)}},this.isValidPing=async i=>{if(!ae(i)){const{message:r}=c("MISSING_OR_INVALID",`ping() params: ${i}`);throw new Error(r)}const{topic:s}=i;await this.isValidPairingTopic(s)},this.isValidDisconnect=async i=>{if(!ae(i)){const{message:r}=c("MISSING_OR_INVALID",`disconnect() params: ${i}`);throw new Error(r)}const{topic:s}=i;await this.isValidPairingTopic(s)},this.isValidPairingTopic=async i=>{if(!xi(i,!1)){const{message:s}=c("MISSING_OR_INVALID",`pairing topic should be a string: ${i}`);throw new Error(s)}if(!this.pairings.keys.includes(i)){const{message:s}=c("NO_MATCHING_KEY",`pairing topic doesn't exist: ${i}`);throw new Error(s)}if(Ce(this.pairings.get(i).expiry)){await this.deletePairing(i);const{message:s}=c("EXPIRED",`pairing topic: ${i}`);throw new Error(s)}},this.core=e,this.logger=b(t,this.name),this.pairings=new Kt(this.core,this.logger,this.name,this.storagePrefix)}get context(){return E(this.logger)}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}registerRelayerEvents(){this.core.relayer.on(D.message,async e=>{const{topic:t,message:i}=e;if(!this.pairings.keys.includes(t)||this.ignoredPayloadTypes.includes(this.core.crypto.getPayloadType(i)))return;const s=await this.core.crypto.decode(t,i);try{Pe(s)?(this.core.history.set(t,s),this.onRelayEventRequest({topic:t,payload:s})):xe(s)&&(await this.core.history.resolve(s),await this.onRelayEventResponse({topic:t,payload:s}),this.core.history.delete(t,s.id))}catch(r){this.logger.error(r)}})}registerExpirerEvents(){this.core.expirer.on(v.expired,async e=>{const{topic:t}=Oi(e.target);t&&this.pairings.keys.includes(t)&&(await this.deletePairing(t,!0),this.events.emit(V.expire,{topic:t}))})}}class Vt extends ri{constructor(e,t){super(e,t),this.core=e,this.logger=t,this.records=new Map,this.events=new U,this.name=vt,this.version=It,this.cached=[],this.initialized=!1,this.storagePrefix=O,this.init=async()=>{this.initialized||(this.logger.trace("Initialized"),await this.restore(),this.cached.forEach(i=>this.records.set(i.id,i)),this.cached=[],this.registerEventListeners(),this.initialized=!0)},this.set=(i,s,r)=>{if(this.isInitialized(),this.logger.debug("Setting JSON-RPC request history record"),this.logger.trace({type:"method",method:"set",topic:i,request:s,chainId:r}),this.records.has(s.id))return;const o={id:s.id,topic:i,request:{method:s.method,params:s.params||null},chainId:r,expiry:J(X)};this.records.set(o.id,o),this.events.emit(R.created,o)},this.resolve=async i=>{if(this.isInitialized(),this.logger.debug("Updating JSON-RPC response history record"),this.logger.trace({type:"method",method:"update",response:i}),!this.records.has(i.id))return;const s=await this.getRecord(i.id);typeof s.response>"u"&&(s.response=ze(i)?{error:i.error}:{result:i.result},this.records.set(s.id,s),this.events.emit(R.updated,s))},this.get=async(i,s)=>(this.isInitialized(),this.logger.debug("Getting record"),this.logger.trace({type:"method",method:"get",topic:i,id:s}),await this.getRecord(s)),this.delete=(i,s)=>{this.isInitialized(),this.logger.debug("Deleting record"),this.logger.trace({type:"method",method:"delete",id:s}),this.values.forEach(r=>{if(r.topic===i){if(typeof s<"u"&&r.id!==s)return;this.records.delete(r.id),this.events.emit(R.deleted,r)}})},this.exists=async(i,s)=>(this.isInitialized(),this.records.has(s)?(await this.getRecord(s)).topic===i:!1),this.on=(i,s)=>{this.events.on(i,s)},this.once=(i,s)=>{this.events.once(i,s)},this.off=(i,s)=>{this.events.off(i,s)},this.removeListener=(i,s)=>{this.events.removeListener(i,s)},this.logger=b(t,this.name)}get context(){return E(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name}get size(){return this.records.size}get keys(){return Array.from(this.records.keys())}get values(){return Array.from(this.records.values())}get pending(){const e=[];return this.values.forEach(t=>{if(typeof t.response<"u")return;const i={topic:t.topic,request:Ae(t.request.method,t.request.params,t.id),chainId:t.chainId};return e.push(i)}),e}async setJsonRpcRecords(e){await this.core.storage.setItem(this.storageKey,e)}async getJsonRpcRecords(){return await this.core.storage.getItem(this.storageKey)}getRecord(e){this.isInitialized();const t=this.records.get(e);if(!t){const{message:i}=c("NO_MATCHING_KEY",`${this.name}: ${e}`);throw new Error(i)}return t}async persist(){await this.setJsonRpcRecords(this.values),this.events.emit(R.sync)}async restore(){try{const e=await this.getJsonRpcRecords();if(typeof e>"u"||!e.length)return;if(this.records.size){const{message:t}=c("RESTORE_WILL_OVERRIDE",this.name);throw this.logger.error(t),new Error(t)}this.cached=e,this.logger.debug(`Successfully Restored records for ${this.name}`),this.logger.trace({type:"method",method:"restore",records:this.values})}catch(e){this.logger.debug(`Failed to Restore records for ${this.name}`),this.logger.error(e)}}registerEventListeners(){this.events.on(R.created,e=>{const t=R.created;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,record:e}),this.persist()}),this.events.on(R.updated,e=>{const t=R.updated;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,record:e}),this.persist()}),this.events.on(R.deleted,e=>{const t=R.deleted;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,record:e}),this.persist()}),this.core.heartbeat.on(G.pulse,()=>{this.cleanup()})}cleanup(){try{this.records.forEach(e=>{N(e.expiry||0)-Date.now()<=0&&(this.logger.info(`Deleting expired history log: ${e.id}`),this.delete(e.topic,e.id))})}catch(e){this.logger.warn(e)}}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}}class qt extends ni{constructor(e,t){super(e,t),this.core=e,this.logger=t,this.expirations=new Map,this.events=new U,this.name=Ct,this.version=Rt,this.cached=[],this.initialized=!1,this.storagePrefix=O,this.init=async()=>{this.initialized||(this.logger.trace("Initialized"),await this.restore(),this.cached.forEach(i=>this.expirations.set(i.target,i)),this.cached=[],this.registerEventListeners(),this.initialized=!0)},this.has=i=>{try{const s=this.formatTarget(i);return typeof this.getExpiration(s)<"u"}catch{return!1}},this.set=(i,s)=>{this.isInitialized();const r=this.formatTarget(i),o={target:r,expiry:s};this.expirations.set(r,o),this.checkExpiry(r,o),this.events.emit(v.created,{target:r,expiration:o})},this.get=i=>{this.isInitialized();const s=this.formatTarget(i);return this.getExpiration(s)},this.del=i=>{if(this.isInitialized(),this.has(i)){const s=this.formatTarget(i),r=this.getExpiration(s);this.expirations.delete(s),this.events.emit(v.deleted,{target:s,expiration:r})}},this.on=(i,s)=>{this.events.on(i,s)},this.once=(i,s)=>{this.events.once(i,s)},this.off=(i,s)=>{this.events.off(i,s)},this.removeListener=(i,s)=>{this.events.removeListener(i,s)},this.logger=b(t,this.name)}get context(){return E(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name}get length(){return this.expirations.size}get keys(){return Array.from(this.expirations.keys())}get values(){return Array.from(this.expirations.values())}formatTarget(e){if(typeof e=="string")return Ai(e);if(typeof e=="number")return zi(e);const{message:t}=c("UNKNOWN_TYPE",`Target type: ${typeof e}`);throw new Error(t)}async setExpirations(e){await this.core.storage.setItem(this.storageKey,e)}async getExpirations(){return await this.core.storage.getItem(this.storageKey)}async persist(){await this.setExpirations(this.values),this.events.emit(v.sync)}async restore(){try{const e=await this.getExpirations();if(typeof e>"u"||!e.length)return;if(this.expirations.size){const{message:t}=c("RESTORE_WILL_OVERRIDE",this.name);throw this.logger.error(t),new Error(t)}this.cached=e,this.logger.debug(`Successfully Restored expirations for ${this.name}`),this.logger.trace({type:"method",method:"restore",expirations:this.values})}catch(e){this.logger.debug(`Failed to Restore expirations for ${this.name}`),this.logger.error(e)}}getExpiration(e){const t=this.expirations.get(e);if(!t){const{message:i}=c("NO_MATCHING_KEY",`${this.name}: ${e}`);throw this.logger.error(i),new Error(i)}return t}checkExpiry(e,t){const{expiry:i}=t;N(i)-Date.now()<=0&&this.expire(e,t)}expire(e,t){this.expirations.delete(e),this.events.emit(v.expired,{target:e,expiration:t})}checkExpirations(){this.core.relayer.connected&&this.expirations.forEach((e,t)=>this.checkExpiry(t,e))}registerEventListeners(){this.core.heartbeat.on(G.pulse,()=>this.checkExpirations()),this.events.on(v.created,e=>{const t=v.created;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,data:e}),this.persist()}),this.events.on(v.expired,e=>{const t=v.expired;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,data:e}),this.persist()}),this.events.on(v.deleted,e=>{const t=v.deleted;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,data:e}),this.persist()})}isInitialized(){if(!this.initialized){const{message:e}=c("NOT_INITIALIZED",this.name);throw new Error(e)}}}class jt extends oi{constructor(e,t){super(e,t),this.projectId=e,this.logger=t,this.name=Z,this.initialized=!1,this.queue=[],this.verifyDisabled=!1,this.init=async i=>{if(this.verifyDisabled||Ni()||!Ui())return;const s=this.getVerifyUrl(i?.verifyUrl);this.verifyUrl!==s&&this.removeIframe(),this.verifyUrl=s;try{await this.createIframe()}catch(r){this.logger.info(`Verify iframe failed to load: ${this.verifyUrl}`),this.logger.info(r)}if(!this.initialized){this.removeIframe(),this.verifyUrl=ee;try{await this.createIframe()}catch(r){this.logger.info(`Verify iframe failed to load: ${this.verifyUrl}`),this.logger.info(r),this.verifyDisabled=!0}}},this.register=async i=>{this.initialized?this.sendPost(i.attestationId):(this.addToQueue(i.attestationId),await this.init())},this.resolve=async i=>{if(this.isDevEnv)return"";const s=this.getVerifyUrl(i?.verifyUrl);let r;try{r=await this.fetchAttestation(i.attestationId,s)}catch(o){this.logger.info(`failed to resolve attestation: ${i.attestationId} from url: ${s}`),this.logger.info(o),r=await this.fetchAttestation(i.attestationId,ee)}return r},this.fetchAttestation=async(i,s)=>{this.logger.info(`resolving attestation: ${i} from url: ${s}`);const r=this.startAbortTimer(Re*2),o=await fetch(`${s}/attestation/${i}`,{signal:this.abortController.signal});return clearTimeout(r),o.status===200?await o.json():void 0},this.addToQueue=i=>{this.queue.push(i)},this.processQueue=()=>{this.queue.length!==0&&(this.queue.forEach(i=>this.sendPost(i)),this.queue=[])},this.sendPost=i=>{var s;try{if(!this.iframe)return;(s=this.iframe.contentWindow)==null||s.postMessage(i,"*"),this.logger.info(`postMessage sent: ${i} ${this.verifyUrl}`)}catch{}},this.createIframe=async()=>{let i;const s=r=>{r.data==="verify_ready"&&(this.initialized=!0,this.processQueue(),window.removeEventListener("message",s),i())};await Promise.race([new Promise(r=>{if(document.getElementById(Z))return r();window.addEventListener("message",s);const o=document.createElement("iframe");o.id=Z,o.src=`${this.verifyUrl}/${this.projectId}`,o.style.display="none",document.body.append(o),this.iframe=o,i=r}),new Promise((r,o)=>setTimeout(()=>{window.removeEventListener("message",s),o("verify iframe load timeout")},N(_e)))])},this.removeIframe=()=>{this.iframe&&(this.iframe.remove(),this.iframe=void 0,this.initialized=!1)},this.getVerifyUrl=i=>{let s=i||F;return _t.includes(s)||(this.logger.info(`verify url: ${s}, not included in trusted list, assigning default: ${F}`),s=F),s},this.logger=b(t,this.name),this.verifyUrl=F,this.abortController=new AbortController,this.isDevEnv=Li()&&process.env.IS_VITEST}get context(){return E(this.logger)}startAbortTimer(e){return this.abortController=new AbortController,setTimeout(()=>this.abortController.abort(),N(e))}}class Gt extends ai{constructor(e,t){super(e,t),this.projectId=e,this.logger=t,this.context=Tt,this.registerDeviceToken=async i=>{const{clientId:s,token:r,notificationType:o,enableEncrypted:a=!1}=i,h=`${St}/${this.projectId}/clients`;await Yi(h,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({client_id:s,type:o,token:r,always_raw:a})})},this.logger=b(t,this.context)}}var Lr=Object.defineProperty,Yt=Object.getOwnPropertySymbols,$r=Object.prototype.hasOwnProperty,Fr=Object.prototype.propertyIsEnumerable,Ht=(n,e,t)=>e in n?Lr(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,Jt=(n,e)=>{for(var t in e||(e={}))$r.call(e,t)&&Ht(n,t,e[t]);if(Yt)for(var t of Yt(e))Fr.call(e,t)&&Ht(n,t,e[t]);return n};class te extends hi{constructor(e){super(e),this.protocol=le,this.version=We,this.name=Q,this.events=new U,this.initialized=!1,this.on=(i,s)=>this.events.on(i,s),this.once=(i,s)=>this.events.once(i,s),this.off=(i,s)=>this.events.off(i,s),this.removeListener=(i,s)=>this.events.removeListener(i,s),this.projectId=e?.projectId,this.relayUrl=e?.relayUrl||ge,this.customStoragePrefix=e!=null&&e.customStoragePrefix?`:${e.customStoragePrefix}`:"";const t=typeof e?.logger<"u"&&typeof e?.logger!="string"?e.logger:ye(me({level:e?.logger||Qe.logger}));this.logger=b(t,this.name),this.heartbeat=new Qt,this.crypto=new xt(this,this.logger,e?.keychain),this.history=new Vt(this,this.logger),this.expirer=new qt(this,this.logger),this.storage=e!=null&&e.storage?e.storage:new Wt(Jt(Jt({},Ze),e?.storageOptions)),this.relayer=new $t({core:this,logger:this.logger,relayUrl:this.relayUrl,projectId:this.projectId}),this.pairing=new Bt(this,this.logger),this.verify=new jt(this.projectId||"",this.logger),this.echoClient=new Gt(this.projectId||"",this.logger)}static async init(e){const t=new te(e);await t.initialize();const i=await t.crypto.getClientId();return await t.storage.setItem(yt,i),t}get context(){return E(this.logger)}async start(){this.initialized||await this.initialize()}async initialize(){this.logger.trace("Initialized");try{await this.crypto.init(),await this.history.init(),await this.expirer.init(),await this.relayer.init(),await this.heartbeat.init(),await this.pairing.init(),this.initialized=!0,this.logger.info("Core Initialization Success")}catch(e){throw this.logger.warn(`Core Initialization Failure at epoch ${Date.now()}`,e),this.logger.error(e.message),e}}}const Mr=te;export{Q as CORE_CONTEXT,Qe as CORE_DEFAULT,le as CORE_PROTOCOL,Ze as CORE_STORAGE_OPTIONS,O as CORE_STORAGE_PREFIX,We as CORE_VERSION,de as CRYPTO_CLIENT_SEED,et as CRYPTO_CONTEXT,tt as CRYPTO_JWT_TTL,Mr as Core,xt as Crypto,Tt as ECHO_CONTEXT,St as ECHO_URL,Ct as EXPIRER_CONTEXT,wr as EXPIRER_DEFAULT_TTL,v as EXPIRER_EVENTS,Rt as EXPIRER_STORAGE_VERSION,Gt as EchoClient,qt as Expirer,vt as HISTORY_CONTEXT,R as HISTORY_EVENTS,It as HISTORY_STORAGE_VERSION,Vt as JsonRpcHistory,it as KEYCHAIN_CONTEXT,st as KEYCHAIN_STORAGE_VERSION,Pt as KeyChain,rt as MESSAGES_CONTEXT,nt as MESSAGES_STORAGE_VERSION,Ot as MessageTracker,Et as PAIRING_CONTEXT,Er as PAIRING_DEFAULT_TTL,V as PAIRING_EVENTS,$ as PAIRING_RPC_OPTS,wt as PAIRING_STORAGE_VERSION,ft as PENDING_SUB_RESOLUTION_TIMEOUT,at as PUBLISHER_CONTEXT,ot as PUBLISHER_DEFAULT_TTL,Bt as Pairing,ut as RELAYER_CONTEXT,ct as RELAYER_DEFAULT_LOGGER,ht as RELAYER_DEFAULT_PROTOCOL,ge as RELAYER_DEFAULT_RELAY_URL,D as RELAYER_EVENTS,pe as RELAYER_FAILOVER_RELAY_URL,P as RELAYER_PROVIDER_EVENTS,dt as RELAYER_RECONNECT_TIMEOUT,gt as RELAYER_SDK_VERSION,br as RELAYER_STORAGE_OPTIONS,lt as RELAYER_SUBSCRIBER_SUFFIX,pt as RELAYER_TRANSPORT_CUTOFF,$t as Relayer,Dt as STORE_STORAGE_VERSION,mt as SUBSCRIBER_CONTEXT,fr as SUBSCRIBER_DEFAULT_TTL,w as SUBSCRIBER_EVENTS,bt as SUBSCRIBER_STORAGE_VERSION,Kt as Store,Nt as Subscriber,_t as TRUSTED_VERIFY_URLS,Z as VERIFY_CONTEXT,ee as VERIFY_FALLBACK_SERVER,F as VERIFY_SERVER,jt as Verify,yt as WALLETCONNECT_CLIENT_ID,te as default};
|
|
//# sourceMappingURL=index.es.js.map
|