@walletconnect/core
Version:
Core for WalletConnect Protocol
1 lines • 76.8 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var D=require("events"),k=require("@walletconnect/heartbeat"),je=require("@walletconnect/keyvaluestorage"),d=require("@walletconnect/logger"),S=require("@walletconnect/types"),c=require("@walletconnect/time"),G=require("@walletconnect/safe-json"),Y=require("@walletconnect/relay-auth"),Je=require("uint8arrays/from-string"),n=require("@walletconnect/utils"),Ze=require("uint8arrays"),Qe=require("@walletconnect/jsonrpc-provider"),b=require("@walletconnect/jsonrpc-utils"),et=require("@walletconnect/jsonrpc-ws-connection"),tt=require("es-toolkit/compat"),it=require("@walletconnect/window-getters");function H(g){return g&&g.__esModule?g:{default:g}}function st(g){if(g&&g.__esModule)return g;var e=Object.create(null);return g&&Object.keys(g).forEach(function(s){if(s!=="default"){var i=Object.getOwnPropertyDescriptor(g,s);Object.defineProperty(e,s,i.get?i:{enumerable:!0,get:function(){return g[s]}})}}),e.default=g,Object.freeze(e)}var rt=H(D),ot=H(je),q=st(Y),nt=H(et);const W="wc",X=2,U="core",P=`${W}@2:${U}:`,Q={name:U,logger:"error"},ee={database:":memory:"},te="crypto",j="client_ed25519_seed",ie=c.ONE_DAY,se="keychain",re="0.3",oe="messages",ne="0.3",ae=c.SIX_HOURS,he="publisher",ce="irn",le="error",J="wss://relay.walletconnect.org",ge="relayer",y={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"},de="_subscription",T={payload:"payload",connect:"connect",disconnect:"disconnect",error:"error"},pe=.1,at={database:":memory:"},B="2.23.9",ht=1e4,M={link_mode:"link_mode",relay:"relay"},K={inbound:"inbound",outbound:"outbound"},ue="0.3",ye="WALLETCONNECT_CLIENT_ID",Z="WALLETCONNECT_LINK_MODE_APPS",w={created:"subscription_created",deleted:"subscription_deleted",expired:"subscription_expired",disabled:"subscription_disabled",sync:"subscription_sync",resubscribed:"subscription_resubscribed"},ct=c.THIRTY_DAYS,_e="subscription",me="0.3",lt=c.FIVE_SECONDS*1e3,Ee="pairing",be="0.3",gt=c.THIRTY_DAYS,z={wc_pairingDelete:{req:{ttl:c.ONE_DAY,prompt:!1,tag:1e3},res:{ttl:c.ONE_DAY,prompt:!1,tag:1001}},wc_pairingPing:{req:{ttl:c.THIRTY_SECONDS,prompt:!1,tag:1002},res:{ttl:c.THIRTY_SECONDS,prompt:!1,tag:1003}},unregistered_method:{req:{ttl:c.ONE_DAY,prompt:!1,tag:0},res:{ttl:c.ONE_DAY,prompt:!1,tag:0}}},V={create:"pairing_create",expire:"pairing_expire",delete:"pairing_delete",ping:"pairing_ping"},v={created:"history_created",updated:"history_updated",deleted:"history_deleted",sync:"history_sync"},fe="history",we="0.3",Te="expirer",R={created:"expirer_created",deleted:"expirer_deleted",expired:"expirer_expired",sync:"expirer_sync"},Ie="0.3",dt=c.ONE_DAY,ve="verify-api",pt="https://verify.walletconnect.com",Re="https://verify.walletconnect.org",$=Re,Se=`${$}/v3`,Pe=[pt,Re],Ce="echo",Oe="https://echo.walletconnect.com",ut="event-client",O={pairing_started:"pairing_started",pairing_uri_validation_success:"pairing_uri_validation_success",pairing_uri_not_expired:"pairing_uri_not_expired",store_new_pairing:"store_new_pairing",subscribing_pairing_topic:"subscribing_pairing_topic",subscribe_pairing_topic_success:"subscribe_pairing_topic_success",existing_pairing:"existing_pairing",pairing_not_expired:"pairing_not_expired",emit_inactive_pairing:"emit_inactive_pairing",emit_session_proposal:"emit_session_proposal",subscribing_to_pairing_topic:"subscribing_to_pairing_topic"},N={no_wss_connection:"no_wss_connection",no_internet_connection:"no_internet_connection",malformed_pairing_uri:"malformed_pairing_uri",active_pairing_already_exists:"active_pairing_already_exists",subscribe_pairing_topic_failure:"subscribe_pairing_topic_failure",pairing_expired:"pairing_expired",proposal_expired:"proposal_expired",proposal_listener_not_found:"proposal_listener_not_found"},yt={session_approve_started:"session_approve_started",proposal_not_expired:"proposal_not_expired",session_namespaces_validation_success:"session_namespaces_validation_success",create_session_topic:"create_session_topic",subscribing_session_topic:"subscribing_session_topic",subscribe_session_topic_success:"subscribe_session_topic_success",publishing_session_approve:"publishing_session_approve",session_approve_publish_success:"session_approve_publish_success",store_session:"store_session",publishing_session_settle:"publishing_session_settle",session_settle_publish_success:"session_settle_publish_success",session_request_response_started:"session_request_response_started",session_request_response_validation_success:"session_request_response_validation_success",session_request_response_publish_started:"session_request_response_publish_started"},_t={no_internet_connection:"no_internet_connection",no_wss_connection:"no_wss_connection",proposal_expired:"proposal_expired",subscribe_session_topic_failure:"subscribe_session_topic_failure",session_approve_publish_failure:"session_approve_publish_failure",session_settle_publish_failure:"session_settle_publish_failure",session_approve_namespace_validation_failure:"session_approve_namespace_validation_failure",proposal_not_found:"proposal_not_found",session_request_response_validation_failure:"session_request_response_validation_failure",session_request_response_publish_failure:"session_request_response_publish_failure"},mt={authenticated_session_approve_started:"authenticated_session_approve_started",authenticated_session_not_expired:"authenticated_session_not_expired",chains_caip2_compliant:"chains_caip2_compliant",chains_evm_compliant:"chains_evm_compliant",create_authenticated_session_topic:"create_authenticated_session_topic",cacaos_verified:"cacaos_verified",store_authenticated_session:"store_authenticated_session",subscribing_authenticated_session_topic:"subscribing_authenticated_session_topic",subscribe_authenticated_session_topic_success:"subscribe_authenticated_session_topic_success",publishing_authenticated_session_approve:"publishing_authenticated_session_approve",authenticated_session_approve_publish_success:"authenticated_session_approve_publish_success"},Et={no_internet_connection:"no_internet_connection",no_wss_connection:"no_wss_connection",missing_session_authenticate_request:"missing_session_authenticate_request",session_authenticate_request_expired:"session_authenticate_request_expired",chains_caip2_compliant_failure:"chains_caip2_compliant_failure",chains_evm_compliant_failure:"chains_evm_compliant_failure",invalid_cacao:"invalid_cacao",subscribe_authenticated_session_topic_failure:"subscribe_authenticated_session_topic_failure",authenticated_session_approve_publish_failure:"authenticated_session_approve_publish_failure",authenticated_session_pending_request_not_found:"authenticated_session_pending_request_not_found"},Ne=.1,Ae="event-client",xe=86400,Le="https://pulse.walletconnect.org/batch";class De{constructor(e,s){this.core=e,this.logger=s,this.keychain=new Map,this.name=se,this.version=re,this.initialized=!1,this.storagePrefix=P,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,t)=>{this.isInitialized(),this.keychain.set(i,t),await this.persist()},this.get=i=>{this.isInitialized();const t=this.keychain.get(i);if(typeof t>"u"){const{message:r}=n.getInternalError("NO_MATCHING_KEY",`${this.name}: ${i}`);throw new Error(r)}return t},this.del=async i=>{this.isInitialized(),this.keychain.delete(i),await this.persist()},this.core=e,this.logger=d.generateChildLogger(s,this.name)}get context(){return d.getLoggerContext(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name}async setKeyChain(e){await this.core.storage.setItem(this.storageKey,n.mapToObj(e))}async getKeyChain(){const e=await this.core.storage.getItem(this.storageKey);return typeof e<"u"?n.objToMap(e):void 0}async persist(){await this.setKeyChain(this.keychain)}isInitialized(){if(!this.initialized){const{message:e}=n.getInternalError("NOT_INITIALIZED",this.name);throw new Error(e)}}}class ke{constructor(e,s,i){this.core=e,this.logger=s,this.name=te,this.randomSessionIdentifier=n.generateRandomBytes32(),this.initialized=!1,this.init=async()=>{this.initialized||(await this.keychain.init(),this.initialized=!0)},this.hasKeys=t=>(this.isInitialized(),this.keychain.has(t)),this.getClientId=async()=>{if(this.isInitialized(),this.clientId)return this.clientId;const t=await this.getClientSeed(),r=q.generateKeyPair(t),o=q.encodeIss(r.publicKey);return this.clientId=o,o},this.generateKeyPair=()=>{this.isInitialized();const t=n.generateKeyPair();return this.setPrivateKey(t.publicKey,t.privateKey)},this.signJWT=async t=>{this.isInitialized();const r=await this.getClientSeed(),o=q.generateKeyPair(r),a=this.randomSessionIdentifier,h=ie;return await q.signJWT(a,t,h,o)},this.generateSharedKey=(t,r,o)=>{this.isInitialized();const a=this.getPrivateKey(t),h=n.deriveSymKey(a,r);return this.setSymKey(h,o)},this.setSymKey=async(t,r)=>{this.isInitialized();const o=r||n.hashKey(t);return await this.keychain.set(o,t),o},this.deleteKeyPair=async t=>{this.isInitialized(),await this.keychain.del(t)},this.deleteSymKey=async t=>{this.isInitialized(),await this.keychain.del(t)},this.encode=async(t,r,o)=>{this.isInitialized();const a=n.validateEncoding(o),h=G.safeJsonStringify(r);if(n.isTypeTwoEnvelope(a))return n.encodeTypeTwoEnvelope(h,o?.encoding);if(n.isTypeOneEnvelope(a)){const u=a.senderPublicKey,f=a.receiverPublicKey;t=await this.generateSharedKey(u,f)}const l=this.getSymKey(t),{type:_,senderPublicKey:p}=a;return n.encrypt({type:_,symKey:l,message:h,senderPublicKey:p,encoding:o?.encoding})},this.decode=async(t,r,o)=>{this.isInitialized();const a=n.validateDecoding(r,o);if(n.isTypeTwoEnvelope(a)){const h=n.decodeTypeTwoEnvelope(r,o?.encoding);return G.safeJsonParse(h)}if(n.isTypeOneEnvelope(a)){const h=a.receiverPublicKey,l=a.senderPublicKey;t=await this.generateSharedKey(h,l)}try{const h=this.getSymKey(t),l=n.decrypt({symKey:h,encoded:r,encoding:o?.encoding});return G.safeJsonParse(l)}catch(h){this.logger.error(`Failed to decode message from topic: '${t}', clientId: '${await this.getClientId()}'`),this.logger.error(h)}},this.getPayloadType=(t,r=n.BASE64)=>{const o=n.deserialize({encoded:t,encoding:r});return n.decodeTypeByte(o.type)},this.getPayloadSenderPublicKey=(t,r=n.BASE64)=>{const o=n.deserialize({encoded:t,encoding:r});return o.senderPublicKey?Ze.toString(o.senderPublicKey,n.BASE16):void 0},this.core=e,this.logger=d.generateChildLogger(s,this.name),this.keychain=i||new De(this.core,this.logger)}get context(){return d.getLoggerContext(this.logger)}async setPrivateKey(e,s){return await this.keychain.set(e,s),e}getPrivateKey(e){return this.keychain.get(e)}async getClientSeed(){let e="";try{e=this.keychain.get(j)}catch{e=n.generateRandomBytes32(),await this.keychain.set(j,e)}return Je.fromString(e,"base16")}getSymKey(e){return this.keychain.get(e)}isInitialized(){if(!this.initialized){const{message:e}=n.getInternalError("NOT_INITIALIZED",this.name);throw new Error(e)}}}class Me extends S.IMessageTracker{constructor(e,s){super(e,s),this.logger=e,this.core=s,this.messages=new Map,this.messagesWithoutClientAck=new Map,this.name=oe,this.version=ne,this.initialized=!1,this.storagePrefix=P,this.init=async()=>{if(!this.initialized){this.logger.trace("Initialized");try{const i=await this.getRelayerMessages();typeof i<"u"&&(this.messages=i);const t=await this.getRelayerMessagesWithoutClientAck();typeof t<"u"&&(this.messagesWithoutClientAck=t),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,t,r)=>{this.isInitialized();const o=n.hashMessage(t);let a=this.messages.get(i);if(typeof a>"u"&&(a={}),typeof a[o]<"u")return o;if(a[o]=t,this.messages.set(i,a),r===K.inbound){const h=this.messagesWithoutClientAck.get(i)||{};this.messagesWithoutClientAck.set(i,{...h,[o]:t})}return await this.persist(),o},this.get=i=>{this.isInitialized();let t=this.messages.get(i);return typeof t>"u"&&(t={}),t},this.getWithoutAck=i=>{this.isInitialized();const t={};for(const r of i){const o=this.messagesWithoutClientAck.get(r)||{};t[r]=Object.values(o)}return t},this.has=(i,t)=>{this.isInitialized();const r=this.get(i),o=n.hashMessage(t);return typeof r[o]<"u"},this.ack=async(i,t)=>{this.isInitialized();const r=this.messagesWithoutClientAck.get(i);if(typeof r>"u")return;const o=n.hashMessage(t);delete r[o],Object.keys(r).length===0?this.messagesWithoutClientAck.delete(i):this.messagesWithoutClientAck.set(i,r),await this.persist()},this.del=async i=>{this.isInitialized(),this.messages.delete(i),this.messagesWithoutClientAck.delete(i),await this.persist()},this.logger=d.generateChildLogger(e,this.name),this.core=s}get context(){return d.getLoggerContext(this.logger)}get storageKey(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name}get storageKeyWithoutClientAck(){return this.storagePrefix+this.version+this.core.customStoragePrefix+"//"+this.name+"_withoutClientAck"}async setRelayerMessages(e){await this.core.storage.setItem(this.storageKey,n.mapToObj(e))}async setRelayerMessagesWithoutClientAck(e){await this.core.storage.setItem(this.storageKeyWithoutClientAck,n.mapToObj(e))}async getRelayerMessages(){const e=await this.core.storage.getItem(this.storageKey);return typeof e<"u"?n.objToMap(e):void 0}async getRelayerMessagesWithoutClientAck(){const e=await this.core.storage.getItem(this.storageKeyWithoutClientAck);return typeof e<"u"?n.objToMap(e):void 0}async persist(){await this.setRelayerMessages(this.messages),await this.setRelayerMessagesWithoutClientAck(this.messagesWithoutClientAck)}isInitialized(){if(!this.initialized){const{message:e}=n.getInternalError("NOT_INITIALIZED",this.name);throw new Error(e)}}}class bt extends S.IPublisher{constructor(e,s){super(e,s),this.relayer=e,this.logger=s,this.events=new D.EventEmitter,this.name=he,this.queue=new Map,this.publishTimeout=c.toMiliseconds(c.ONE_MINUTE),this.initialPublishTimeout=c.toMiliseconds(c.ONE_SECOND*15),this.needsTransportRestart=!1,this.publish=async(i,t,r)=>{this.logger.debug("Publishing Payload"),this.logger.trace({type:"method",method:"publish",params:{topic:i,message:t,opts:r}});const o=r?.ttl||ae,a=r?.prompt||!1,h=r?.tag||0,l=r?.id||b.getBigIntRpcId().toString(),_=n.getRelayProtocolApi(n.getRelayProtocolName().protocol),p={id:l,method:r?.publishMethod||_.publish,params:{topic:i,message:t,ttl:o,prompt:a,tag:h,attestation:r?.attestation,...r?.tvf}},u=`Failed to publish payload, please try again. id:${l} tag:${h}`;try{n.isUndefined(p.params?.prompt)&&delete p.params?.prompt,n.isUndefined(p.params?.tag)&&delete p.params?.tag;const f=new Promise(async I=>{const m=({id:E})=>{p.id?.toString()===E.toString()&&(this.removeRequestFromQueue(E),this.relayer.events.removeListener(y.publish,m),I())};this.relayer.events.on(y.publish,m);const A=n.createExpiringPromise(new Promise((E,C)=>{this.rpcPublish(p,r).then(E).catch(x=>{this.logger.warn(x,x?.message),C(x)})}),this.initialPublishTimeout,`Failed initial publish, retrying.... id:${l} tag:${h}`);try{await A,this.events.removeListener(y.publish,m)}catch(E){this.queue.set(l,{request:p,opts:r,attempt:1}),this.logger.warn(E,E?.message)}});this.logger.trace({type:"method",method:"publish",params:{id:l,topic:i,message:t,opts:r}}),await n.createExpiringPromise(f,this.publishTimeout,u)}catch(f){if(this.logger.debug("Failed to Publish Payload"),this.logger.error(f),r?.internal?.throwOnFailedPublish)throw f}finally{this.queue.delete(l)}},this.publishCustom=async i=>{this.logger.debug("Publishing custom payload"),this.logger.trace({type:"method",method:"publishCustom",params:i});const{payload:t,opts:r={}}=i,{attestation:o,tvf:a,publishMethod:h,prompt:l,tag:_,ttl:p=c.FIVE_MINUTES}=r,u=r.id||b.getBigIntRpcId().toString(),f=n.getRelayProtocolApi(n.getRelayProtocolName().protocol),I=h||f.publish,m={id:u,method:I,params:{...t,ttl:p,prompt:l,tag:_,attestation:o,...a}},A=`Failed to publish custom payload, please try again. id:${u} tag:${_}`;try{n.isUndefined(m.params?.prompt)&&delete m.params?.prompt,n.isUndefined(m.params?.tag)&&delete m.params?.tag;const E=new Promise(async C=>{const x=({id:L})=>{m.id?.toString()===L.toString()&&(this.removeRequestFromQueue(L),this.relayer.events.removeListener(y.publish,x),C())};this.relayer.events.on(y.publish,x);const We=n.createExpiringPromise(new Promise((L,Xe)=>{this.rpcPublish(m,r).then(L).catch(F=>{this.logger.warn(F,F?.message),Xe(F)})}),this.initialPublishTimeout,`Failed initial custom payload publish, retrying.... method:${I} id:${u} tag:${_}`);try{await We,this.events.removeListener(y.publish,x)}catch(L){this.queue.set(u,{request:m,opts:r,attempt:1}),this.logger.warn(L,L?.message)}});this.logger.trace({type:"method",method:"publish",params:{id:u,payload:t,opts:r}}),await n.createExpiringPromise(E,this.publishTimeout,A)}catch(E){if(this.logger.debug("Failed to Publish Payload"),this.logger.error(E),r?.internal?.throwOnFailedPublish)throw E}finally{this.queue.delete(u)}},this.on=(i,t)=>{this.events.on(i,t)},this.once=(i,t)=>{this.events.once(i,t)},this.off=(i,t)=>{this.events.off(i,t)},this.removeListener=(i,t)=>{this.events.removeListener(i,t)},this.relayer=e,this.logger=d.generateChildLogger(s,this.name),this.registerEventListeners()}get context(){return d.getLoggerContext(this.logger)}async rpcPublish(e,s){this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"message",direction:"outgoing",request:e});const i=await this.relayer.request(e);return this.relayer.events.emit(y.publish,{...e,...s}),this.logger.debug("Successfully Published Payload"),i}removeRequestFromQueue(e){this.queue.delete(e)}checkQueue(){this.queue.forEach(async(e,s)=>{const i=e.attempt+1;this.queue.set(s,{...e,attempt:i}),this.logger.warn({},`Publisher: queue->publishing: ${e.request.id}, tag: ${e.request.params?.tag}, attempt: ${i}`),await this.rpcPublish(e.request,e.opts),this.logger.warn({},`Publisher: queue->published: ${e.request.id}`)})}registerEventListeners(){this.relayer.core.heartbeat.on(k.HEARTBEAT_EVENTS.pulse,()=>{if(this.needsTransportRestart){this.needsTransportRestart=!1,this.relayer.events.emit(y.connection_stalled);return}this.checkQueue()}),this.relayer.on(y.message_ack,e=>{this.removeRequestFromQueue(e.id.toString())})}}class ft{constructor(){this.map=new Map,this.set=(e,s)=>{const i=this.get(e);this.exists(e,s)||this.map.set(e,[...i,s])},this.get=e=>this.map.get(e)||[],this.exists=(e,s)=>this.get(e).includes(s),this.delete=(e,s)=>{if(typeof s>"u"){this.map.delete(e);return}if(!this.map.has(e))return;const i=this.get(e);if(!this.exists(e,s))return;const t=i.filter(r=>r!==s);if(!t.length){this.map.delete(e);return}this.map.set(e,t)},this.clear=()=>{this.map.clear()}}get topics(){return Array.from(this.map.keys())}}class ze extends S.ISubscriber{constructor(e,s){super(e,s),this.relayer=e,this.logger=s,this.subscriptions=new Map,this.topicMap=new ft,this.events=new D.EventEmitter,this.name=_e,this.version=me,this.pending=new Map,this.cached=[],this.initialized=!1,this.storagePrefix=P,this.subscribeTimeout=c.toMiliseconds(c.ONE_MINUTE),this.initialSubscribeTimeout=c.toMiliseconds(c.ONE_SECOND*15),this.batchSubscribeTopicsLimit=500,this.init=async()=>{this.initialized||(this.logger.trace("Initialized"),this.registerEventListeners(),await this.restore()),this.initialized=!0},this.subscribe=async(i,t)=>{this.isInitialized(),this.logger.debug("Subscribing Topic"),this.logger.trace({type:"method",method:"subscribe",params:{topic:i,opts:t}});try{const r=n.getRelayProtocolName(t),o={topic:i,relay:r,transportType:t?.transportType};t?.internal?.skipSubscribe||this.pending.set(i,o);const a=await this.rpcSubscribe(i,r,t);return typeof a=="string"&&(this.onSubscribe(a,o),this.logger.debug("Successfully Subscribed Topic"),this.logger.trace({type:"method",method:"subscribe",params:{topic:i,opts:t}})),a}catch(r){throw this.logger.debug("Failed to Subscribe Topic"),this.logger.error(r),r}},this.unsubscribe=async(i,t)=>{this.isInitialized(),typeof t?.id<"u"?await this.unsubscribeById(i,t.id,t):await this.unsubscribeByTopic(i,t)},this.isSubscribed=i=>new Promise(t=>{t(this.topicMap.topics.includes(i))}),this.isKnownTopic=i=>new Promise(t=>{t(this.topicMap.topics.includes(i)||this.pending.has(i)||this.cached.some(r=>r.topic===i))}),this.on=(i,t)=>{this.events.on(i,t)},this.once=(i,t)=>{this.events.once(i,t)},this.off=(i,t)=>{this.events.off(i,t)},this.removeListener=(i,t)=>{this.events.removeListener(i,t)},this.start=async()=>{await this.onConnect()},this.stop=async()=>{await this.onDisconnect()},this.restart=async()=>{await this.restore(),await this.onRestart()},this.checkPending=async()=>{if(this.pending.size===0&&(!this.initialized||!this.relayer.connected))return;const i=[];this.pending.forEach(t=>{i.push(t)}),await this.batchSubscribe(i)},this.registerEventListeners=()=>{this.relayer.core.heartbeat.on(k.HEARTBEAT_EVENTS.pulse,async()=>{await this.checkPending()}),this.events.on(w.created,async i=>{const t=w.created;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,data:i}),await this.persist()}),this.events.on(w.deleted,async i=>{const t=w.deleted;this.logger.info(`Emitting ${t}`),this.logger.debug({type:"event",event:t,data:i}),await this.persist()})},this.relayer=e,this.logger=d.generateChildLogger(s,this.name),this.clientId=""}get context(){return d.getLoggerContext(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}get hasAnyTopics(){return this.topicMap.topics.length>0||this.pending.size>0||this.cached.length>0||this.subscriptions.size>0}hasSubscription(e,s){let i=!1;try{i=this.getSubscription(e).topic===s}catch{}return i}reset(){this.cached=[],this.initialized=!0}onDisable(){this.values.length>0&&(this.cached=this.values),this.subscriptions.clear(),this.topicMap.clear()}async unsubscribeByTopic(e,s){const i=this.topicMap.get(e);await Promise.all(i.map(async t=>await this.unsubscribeById(e,t,s)))}async unsubscribeById(e,s,i){this.logger.debug("Unsubscribing Topic"),this.logger.trace({type:"method",method:"unsubscribe",params:{topic:e,id:s,opts:i}});try{const t=n.getSdkError("USER_DISCONNECTED",`${this.name}, ${e}`);await this.onUnsubscribe(e,s,t);const r=n.getRelayProtocolName(i);await this.restartToComplete({topic:e,id:s,relay:r}),await this.rpcUnsubscribe(e,s,r),this.logger.debug("Successfully Unsubscribed Topic"),this.logger.trace({type:"method",method:"unsubscribe",params:{topic:e,id:s,opts:i}})}catch(t){throw this.logger.debug("Failed to Unsubscribe Topic"),this.logger.error(t),t}}async rpcSubscribe(e,s,i){const t=await this.getSubscriptionId(e);if(i?.internal?.skipSubscribe)return t;(!i||i?.transportType===M.relay)&&await this.restartToComplete({topic:e,id:e,relay:s});const r={method:n.getRelayProtocolApi(s.protocol).subscribe,params:{topic:e}};this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"payload",direction:"outgoing",request:r});const o=i?.internal?.throwOnFailedPublish;try{if(i?.transportType===M.link_mode)return setTimeout(()=>{(this.relayer.connected||this.relayer.connecting)&&this.relayer.request(r).catch(l=>this.logger.warn(l))},c.toMiliseconds(c.ONE_SECOND)),t;const a=new Promise(async l=>{const _=p=>{p.topic===e&&(this.events.removeListener(w.created,_),l(p.id))};this.events.on(w.created,_);try{const p=await n.createExpiringPromise(new Promise((u,f)=>{this.relayer.request(r).catch(I=>{this.logger.warn(I,I?.message),f(I)}).then(u)}),this.initialSubscribeTimeout,`Subscribing to ${e} failed, please try again`);this.events.removeListener(w.created,_),l(p)}catch{}}),h=await n.createExpiringPromise(a,this.subscribeTimeout,`Subscribing to ${e} failed, please try again`);if(!h&&o)throw new Error(`Subscribing to ${e} failed, please try again`);return h?t:null}catch(a){if(this.logger.debug("Outgoing Relay Subscribe Payload stalled"),this.relayer.events.emit(y.connection_stalled),o)throw a}return null}async rpcBatchSubscribe(e){if(!e.length)return!0;const s=e[0].relay,i={method:n.getRelayProtocolApi(s.protocol).batchSubscribe,params:{topics:e.map(t=>t.topic)}};this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"payload",direction:"outgoing",request:i});try{return await n.createExpiringPromise(new Promise((t,r)=>{this.relayer.request(i).then(t).catch(o=>{this.logger.warn(o),r(o)})}),this.subscribeTimeout,"rpcBatchSubscribe failed, please try again"),!0}catch{return this.relayer.events.emit(y.connection_stalled),!1}}async rpcBatchFetchMessages(e){if(!e.length)return;const s=e[0].relay,i={method:n.getRelayProtocolApi(s.protocol).batchFetchMessages,params:{topics:e.map(r=>r.topic)}};this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"payload",direction:"outgoing",request:i});let t;try{t=await await n.createExpiringPromise(new Promise((r,o)=>{this.relayer.request(i).catch(a=>{this.logger.warn(a),o(a)}).then(r)}),this.subscribeTimeout,"rpcBatchFetchMessages failed, please try again")}catch{this.relayer.events.emit(y.connection_stalled)}return t}rpcUnsubscribe(e,s,i){const t={method:n.getRelayProtocolApi(i.protocol).unsubscribe,params:{topic:e,id:s}};return this.logger.debug("Outgoing Relay Payload"),this.logger.trace({type:"payload",direction:"outgoing",request:t}),this.relayer.request(t)}onSubscribe(e,s){this.setSubscription(e,{...s,id:e}),this.pending.delete(s.topic)}onBatchSubscribe(e){e.length&&e.forEach(s=>{this.setSubscription(s.id,{...s}),this.pending.delete(s.topic)})}async onUnsubscribe(e,s,i){this.events.removeAllListeners(s),this.hasSubscription(s,e)&&this.deleteSubscription(s,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,s){this.logger.debug("Setting subscription"),this.logger.trace({type:"method",method:"setSubscription",id:e,subscription:s}),this.addSubscription(e,s)}addSubscription(e,s){this.subscriptions.set(e,{...s}),this.topicMap.set(s.topic,e),this.events.emit(w.created,s)}getSubscription(e){this.logger.debug("Getting subscription"),this.logger.trace({type:"method",method:"getSubscription",id:e});const s=this.subscriptions.get(e);if(!s){const{message:i}=n.getInternalError("NO_MATCHING_KEY",`${this.name}: ${e}`);throw new Error(i)}return s}deleteSubscription(e,s){this.logger.debug("Deleting subscription"),this.logger.trace({type:"method",method:"deleteSubscription",id:e,reason:s});const i=this.getSubscription(e);this.subscriptions.delete(e),this.topicMap.delete(i.topic,e),this.events.emit(w.deleted,{...i,reason:s})}async persist(){await this.setRelayerSubscriptions(this.values),this.events.emit(w.sync)}async onRestart(){if(this.cached.length){const e=[...this.cached],s=Math.ceil(this.cached.length/this.batchSubscribeTopicsLimit);for(let i=0;i<s;i++){const t=e.splice(0,this.batchSubscribeTopicsLimit);await this.batchSubscribe(t)}}this.events.emit(w.resubscribed)}async restore(){try{const e=await this.getRelayerSubscriptions();if(typeof e>"u"||!e.length)return;if(this.subscriptions.size&&!e.every(s=>s.topic===this.subscriptions.get(s.id)?.topic)){const{message:s}=n.getInternalError("RESTORE_WILL_OVERRIDE",this.name);throw this.logger.error(s),this.logger.error(`${this.name}: ${JSON.stringify(this.values)}`),new Error(s)}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){if(!await this.rpcBatchSubscribe(e)){this.logger.warn(`Batch subscribe failed for ${e.length} topics, adding to pending for retry`),e.forEach(s=>{this.pending.set(s.topic,s)});return}this.onBatchSubscribe(await Promise.all(e.map(async s=>({...s,id:await this.getSubscriptionId(s.topic)}))))}}async batchFetchMessages(e){if(!e.length)return;this.logger.trace(`Fetching batch messages for ${e.length} subscriptions`);const s=await this.rpcBatchFetchMessages(e);s&&s.messages&&(await n.sleep(c.toMiliseconds(c.ONE_SECOND)),await this.relayer.handleBatchMessageEvents(s.messages))}async onConnect(){await this.restart(),this.reset()}onDisconnect(){this.onDisable()}isInitialized(){if(!this.initialized){const{message:e}=n.getInternalError("NOT_INITIALIZED",this.name);throw new Error(e)}}async restartToComplete(e){!this.relayer.connected&&!this.relayer.connecting&&(this.cached.push(e),await this.relayer.transportOpen())}async getClientId(){return this.clientId||(this.clientId=await this.relayer.core.crypto.getClientId()),this.clientId}async getSubscriptionId(e){return n.hashMessage(e+await this.getClientId())}}class Ve extends S.IRelayer{constructor(e){super(e),this.protocol="wc",this.version=2,this.events=new D.EventEmitter,this.name=ge,this.transportExplicitlyClosed=!1,this.initialized=!1,this.connectionAttemptInProgress=!1,this.hasExperiencedNetworkDisruption=!1,this.heartBeatTimeout=c.toMiliseconds(c.THIRTY_SECONDS+c.FIVE_SECONDS),this.reconnectInProgress=!1,this.requestsInFlight=[],this.connectTimeout=c.toMiliseconds(c.ONE_SECOND*15),this.stalledRestartInProgress=!1,this.stalledRestartBackoff=0,this.stalledRestartBaseInterval=c.toMiliseconds(c.ONE_SECOND*2),this.stalledRestartMaxInterval=c.toMiliseconds(c.THIRTY_SECONDS),this.request=async s=>{this.logger.debug("Publishing Request Payload");const i=s.id||b.getBigIntRpcId().toString();await this.toEstablishConnection();try{this.logger.trace({id:i,method:s.method,topic:s.params?.topic},"relayer.request - publishing...");const t=`${i}:${s.params?.tag||""}`;this.requestsInFlight.push(t);const r=await this.provider.request(s);return this.requestsInFlight=this.requestsInFlight.filter(o=>o!==t),r}catch(t){throw this.logger.debug(`Failed to Publish Request: ${i}`),t}},this.resetPingTimeout=()=>{n.isNode()&&(clearTimeout(this.pingTimeout),this.pingTimeout=setTimeout(()=>{try{this.logger.debug({},"pingTimeout: Connection stalled, terminating..."),this.provider?.connection?.socket?.terminate?.()}catch(s){this.logger.warn(s,s?.message)}},this.heartBeatTimeout))},this.onPayloadHandler=s=>{this.onProviderPayload(s),this.resetPingTimeout()},this.onConnectHandler=()=>{this.logger.warn({},"Relayer connected \u{1F6DC}"),this.startPingTimeout(),this.stalledRestartBackoff=0,this.events.emit(y.connect)},this.onDisconnectHandler=()=>{this.logger.warn({},"Relayer disconnected \u{1F6D1}"),this.requestsInFlight=[],this.onProviderDisconnect()},this.onProviderErrorHandler=s=>{this.logger.fatal(`Fatal socket error: ${s.message}`),this.events.emit(y.error,s),this.logger.fatal("Fatal socket error received, closing transport"),this.transportExplicitlyClosed=!0,clearTimeout(this.reconnectTimeout),this.reconnectTimeout=void 0,this.reconnectInProgress=!1,this.transportClose().catch(i=>this.logger.warn(i))},this.registerProviderListeners=()=>{this.provider.on(T.payload,this.onPayloadHandler),this.provider.on(T.connect,this.onConnectHandler),this.provider.on(T.disconnect,this.onDisconnectHandler),this.provider.on(T.error,this.onProviderErrorHandler)},this.core=e.core,this.logger=n.createLogger({logger:e.logger??le,name:this.name}),this.messages=new Me(this.logger,e.core),this.subscriber=new ze(this,this.logger),this.publisher=new bt(this,this.logger),this.projectId=e.projectId,this.relayUrl=e.relayUrl||J,n.isAndroid()?this.packageName=n.getAppId():n.isIos()&&(this.bundleId=n.getAppId()),this.provider={}}async init(){this.logger.trace("Initialized"),this.registerEventListeners(),await Promise.all([this.messages.init(),this.subscriber.init()]),this.initialized=!0,this.transportOpen().catch(e=>this.logger.warn(e,e?.message))}get context(){return d.getLoggerContext(this.logger)}get connected(){return this.provider?.connection?.socket?.readyState===1}get connecting(){return this.provider?.connection?.socket?.readyState===0||this.connectPromise!==void 0}async publish(e,s,i){this.isInitialized(),await this.publisher.publish(e,s,i),await this.recordMessageEvent({topic:e,message:s,publishedAt:Date.now(),transportType:M.relay},K.outbound)}async publishCustom(e){this.isInitialized(),await this.publisher.publishCustom(e)}async subscribe(e,s){this.isInitialized(),(!s?.transportType||s?.transportType==="relay")&&await this.toEstablishConnection();const i=s?.internal?.throwOnFailedPublish??!0;let t=this.subscriber.topicMap.get(e)?.[0]||"",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((a,h)=>{this.subscriber.subscribe(e,{internal:{throwOnFailedPublish:i},...s}).then(l=>{t=l||t,a()}).catch(l=>{i?h(l):a()})})]),t}async unsubscribe(e,s){this.isInitialized(),await this.subscriber.unsubscribe(e,s)}on(e,s){this.events.on(e,s)}once(e,s){this.events.once(e,s)}off(e,s){this.events.off(e,s)}removeListener(e,s){this.events.removeListener(e,s)}async transportDisconnect(){this.provider.disconnect&&(this.hasExperiencedNetworkDisruption||this.connected)?await n.createExpiringPromise(this.provider.disconnect(),2e3,"provider.disconnect()").catch(()=>this.onProviderDisconnect()):this.onProviderDisconnect()}async transportClose(){this.transportExplicitlyClosed=!0,clearTimeout(this.stalledRestartTimeout),this.stalledRestartInProgress=!1,this.stalledRestartBackoff=0,await this.resetTransport()}async transportOpen(e){if(!this.subscriber.hasAnyTopics){this.logger.info("Starting WS connection skipped because the client has no topics to work with.");return}if(this.connectPromise?(this.logger.debug({},"Waiting for existing connection attempt to resolve..."),await this.connectPromise,this.logger.debug({},"Existing connection attempt resolved")):(this.connectPromise=this.connect(e).finally(()=>{this.connectPromise=void 0}),await this.connectPromise),!this.connected)throw new Error(`Couldn't establish socket connection to the relay server: ${this.relayUrl}`)}async restartTransport(e){this.logger.debug({},"Restarting transport..."),!this.connectionAttemptInProgress&&(this.relayUrl=e||this.relayUrl,await this.confirmOnlineStateOrThrow(),await this.resetTransport(),await this.transportOpen())}async resetTransport(){this.reconnectInProgress=!0,clearTimeout(this.reconnectTimeout),this.reconnectTimeout=void 0,await this.transportDisconnect(),await this.subscriber.stop(),this.reconnectInProgress=!1}async confirmOnlineStateOrThrow(){if(!await n.isOnline())throw new Error("No internet connection detected. Please restart your network and try again.")}async handleBatchMessageEvents(e){if(e?.length===0){this.logger.trace("Batch message events is empty. Ignoring...");return}const s=e.sort((i,t)=>i.publishedAt-t.publishedAt);this.logger.debug(`Batch of ${s.length} message events sorted`);for(const i of s)try{await this.onMessageEvent(i)}catch(t){this.logger.warn(t,"Error while processing batch message event: "+t?.message)}this.logger.trace(`Batch of ${s.length} message events processed`)}async onLinkMessageEvent(e,s){const{topic:i}=e;if(!s.sessionExists){const t=n.calcExpiry(c.FIVE_MINUTES),r={topic:i,expiry:t,relay:{protocol:"irn"},active:!1};await this.core.pairing.pairings.set(i,r)}this.events.emit(y.message,e),await this.recordMessageEvent(e,K.inbound)}async connect(e){await this.confirmOnlineStateOrThrow(),e&&e!==this.relayUrl&&(this.relayUrl=e,await this.transportDisconnect()),this.transportExplicitlyClosed=!1;let s=1;try{for(;s<6;){this.connectionAttemptInProgress=!0;try{if(this.transportExplicitlyClosed)break;this.logger.debug({},`Connecting to ${this.relayUrl}, attempt: ${s}...`),await this.createProvider(),await new Promise((i,t)=>{const r=()=>{t(new Error("Connection interrupted while trying to connect"))};this.provider.once(T.disconnect,r),n.createExpiringPromise(this.provider.connect(),this.connectTimeout,`Socket stalled when trying to connect to ${this.relayUrl}`).then(()=>i()).catch(t).finally(()=>{this.provider.off(T.disconnect,r),clearTimeout(this.reconnectTimeout)})}),await new Promise((i,t)=>{const r=()=>{t(new Error("Connection interrupted while trying to subscribe"))};this.provider.once(T.disconnect,r),this.subscriber.start().then(i).catch(t).finally(()=>{this.provider.off(T.disconnect,r)})}),this.hasExperiencedNetworkDisruption=!1}catch(i){await this.subscriber.stop();const t=i;this.logger.warn({},t.message),this.hasExperiencedNetworkDisruption=!0}if(this.connected){this.logger.debug({},`Connected to ${this.relayUrl} successfully on attempt: ${s}`);break}await new Promise(i=>setTimeout(i,c.toMiliseconds(s*1))),s++}}finally{this.connectionAttemptInProgress=!1,clearTimeout(this.reconnectTimeout),this.reconnectTimeout=void 0,this.reconnectInProgress=!1}}startPingTimeout(){if(n.isNode())try{this.provider?.connection?.socket?.on("ping",()=>{this.resetPingTimeout()}),this.resetPingTimeout()}catch(e){this.logger.warn(e,e?.message)}}async createProvider(){if(this.provider.connection&&(this.unregisterProviderListeners(),this.connected))try{await n.createExpiringPromise(this.provider.disconnect(),1e3,"Closing previous provider")}catch{}const e=await this.core.crypto.signJWT(this.relayUrl);this.provider=new Qe.JsonRpcProvider(new nt.default(n.formatRelayRpcUrl({sdkVersion:B,protocol:this.protocol,version:this.version,relayUrl:this.relayUrl,projectId:this.projectId,auth:e,useOnCloseEvent:!0,bundleId:this.bundleId,packageName:this.packageName}))),this.registerProviderListeners()}async recordMessageEvent(e,s){const{topic:i,message:t}=e;await this.messages.set(i,t,s)}async shouldIgnoreMessageEvent(e){const{topic:s,message:i}=e;if(!i||i.length===0)return this.logger.warn(`Ignoring invalid/empty message: ${i}`),!0;if(!await this.subscriber.isKnownTopic(s))return this.logger.warn(`Ignoring message for unknown topic ${s}`),!0;const t=this.messages.has(s,i);return t&&this.logger.warn(`Ignoring duplicate message: ${i}`),t}async onProviderPayload(e){if(this.logger.debug("Incoming Relay Payload"),this.logger.trace({type:"payload",direction:"incoming",payload:e}),b.isJsonRpcRequest(e)){if(!e.method.endsWith(de))return;const s=e.params,{topic:i,message:t,publishedAt:r,attestation:o}=s.data,a={topic:i,message:t,publishedAt:r,transportType:M.relay,attestation:o};this.logger.debug("Emitting Relayer Payload"),this.logger.trace({type:"event",event:s.id,...a}),this.events.emit(s.id,a),await this.acknowledgePayload(e),await this.onMessageEvent(a)}else b.isJsonRpcResponse(e)&&this.events.emit(y.message_ack,e)}async onMessageEvent(e){await this.shouldIgnoreMessageEvent(e)||(await this.recordMessageEvent(e,K.inbound),this.events.emit(y.message,e))}async acknowledgePayload(e){const s=b.formatJsonRpcResult(e.id,!0);await this.provider.connection.send(s)}unregisterProviderListeners(){this.provider.off(T.payload,this.onPayloadHandler),this.provider.off(T.connect,this.onConnectHandler),this.provider.off(T.disconnect,this.onDisconnectHandler),this.provider.off(T.error,this.onProviderErrorHandler),clearTimeout(this.pingTimeout)}async registerEventListeners(){let e=await n.isOnline();n.subscribeToNetworkChange(async s=>{e!==s&&(e=s,s?await this.transportOpen().catch(i=>this.logger.error(i,i?.message)):(this.hasExperiencedNetworkDisruption=!0,await this.transportDisconnect(),this.transportExplicitlyClosed=!1))}),this.core.heartbeat.on(k.HEARTBEAT_EVENTS.pulse,async()=>{if(!this.transportExplicitlyClosed&&!this.connected&&n.isAppVisible())try{await this.confirmOnlineStateOrThrow(),await this.transportOpen()}catch(s){this.logger.warn(s,s?.message)}}),this.events.on(y.connection_stalled,()=>{if(this.transportExplicitlyClosed||this.stalledRestartInProgress)return;this.stalledRestartInProgress=!0;const s=this.stalledRestartBackoff===0?0:Math.min(Math.pow(2,this.stalledRestartBackoff-1)*this.stalledRestartBaseInterval,this.stalledRestartMaxInterval);this.stalledRestartBackoff++,this.logger.warn(`Connection stalled, restarting transport${s?` in ${s}ms`:""}...`),this.stalledRestartTimeout=setTimeout(async()=>{try{if(this.transportExplicitlyClosed)return;await this.restartTransport()}catch(i){this.logger.error(i,i?.message)}finally{this.stalledRestartInProgress=!1}},s)})}async onProviderDisconnect(){if(clearTimeout(this.pingTimeout),this.events.emit(y.disconnect),!this.reconnectInProgress){this.reconnectInProgress=!0;try{await this.subscriber.stop()}catch(e){this.logger.warn(e,"subscriber.stop() failed during disconnect")}if(!this.subscriber.hasAnyTopics||this.transportExplicitlyClosed){this.reconnectInProgress=!1;return}this.reconnectTimeout=setTimeout(async()=>{await this.transportOpen().catch(e=>this.logger.error(e,e?.message)),this.reconnectTimeout=void 0,this.reconnectInProgress=!1},c.toMiliseconds(pe))}}isInitialized(){if(!this.initialized){const{message:e}=n.getInternalError("NOT_INITIALIZED",this.name);throw new Error(e)}}async toEstablishConnection(){if(await this.confirmOnlineStateOrThrow(),!this.connected){if(this.connectPromise){await this.connectPromise;return}this.connectPromise=this.connect().finally(()=>{this.connectPromise=void 0}),await this.connectPromise}}}class Ue extends S.IStore{constructor(e,s,i,t=P,r=void 0){super(e,s,i,t),this.core=e,this.logger=s,this.name=i,this.map=new Map,this.version=ue,this.cached=[],this.initialized=!1,this.storagePrefix=P,this.recentlyDeleted=[],this.recentlyDeletedLimit=200,this.init=async()=>{this.initialized||(this.logger.trace("Initialized"),await this.restore(),this.cached.forEach(o=>{this.getKey&&o!==null&&!n.isUndefined(o)?this.map.set(this.getKey(o),o):n.isProposalStruct(o)?this.map.set(o.id,o):n.isSessionStruct(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=>tt.isEqual(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={...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),this.addToRecentlyDeleted(o),await this.persist())},this.logger=d.generateChildLogger(s,this.name),this.storagePrefix=t,this.getKey=r}get context(){return d.getLoggerContext(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())}addToRecentlyDeleted(e){this.recentlyDeleted.push(e),this.recentlyDeleted.length>=this.recentlyDeletedLimit&&this.recentlyDeleted.splice(0,this.recentlyDeletedLimit/2)}async setDataStore(e){await this.core.storage.setItem(this.storageKey,e)}async getDataStore(){return await this.core.storage.getItem(this.storageKey)}getData(e){const s=this.map.get(e);if(!s){if(this.recentlyDeleted.includes(e)){const{message:t}=n.getInternalError("MISSING_OR_INVALID",`Record was recently deleted - ${this.name}: ${e}`);throw this.logger.error(t),new Error(t)}const{message:i}=n.getInternalError("NO_MATCHING_KEY",`${this.name}: ${e}`);throw this.logger.error(i),new Error(i)}return s}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:s}=n.getInternalError("RESTORE_WILL_OVERRIDE",this.name);throw this.logger.error(s),new Error(s)}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}=n.getInternalError("NOT_INITIALIZED",this.name);throw new Error(e)}}}class Ke{constructor(e,s){this.core=e,this.logger=s,this.name=Ee,this.version=be,this.events=new rt.default,this.initialized=!1,this.storagePrefix=P,this.ignoredPayloadTypes=[n.TYPE_1],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 i=>{this.isInitialized();const t=n.generateRandomBytes32(),r=await this.core.crypto.setSymKey(t),o=n.calcExpiry(c.FIVE_MINUTES),a={protocol:ce},h={topic:r,expiry:o,relay:a,active:!1,methods:i?.methods},l=n.formatUri({protocol:this.core.protocol,version:this.core.version,topic:r,symKey:t,relay:a,expiryTimestamp:o,methods:i?.methods});return this.events.emit(V.create,h),this.core.expirer.set(r,o),await this.pairings.set(r,h),await this.core.relayer.subscribe(r,{transportType:i?.transportType,internal:i?.internal}),{topic:r,uri:l}},this.pair=async i=>{this.isInitialized();const t=this.core.eventClient.createEvent({properties:{topic:i?.uri,trace:[O.pairing_started]}});this.isValidPair(i,t);const{topic:r,symKey:o,relay:a,expiryTimestamp:h,methods:l}=n.parseUri(i.uri);t.props.properties.topic=r,t.addTrace(O.pairing_uri_validation_success),t.addTrace(O.pairing_uri_not_expired);let _;if(this.pairings.keys.includes(r)){if(_=this.pairings.get(r),t.addTrace(O.existing_pairing),_.active)throw t.setError(N.active_pairing_already_exists),new Error(`Pairing already exists: ${r}. Please try again with a new connection URI.`);t.addTrace(O.pairing_not_expired)}const p=h||n.calcExpiry(c.FIVE_MINUTES),u={topic:r,relay:a,expiry:p,active:!1,methods:l};this.core.expirer.set(r,p),await this.pairings.set(r,u),t.addTrace(O.store_new_pairing),i.activatePairing&&await this.activate({topic:r}),this.events.emit(V.create,u),t.addTrace(O.emit_inactive_pairing),this.core.crypto.keychain.has(r)||await this.core.crypto.setSymKey(o,r),t.addTrace(O.subscribing_pairing_topic);try{await this.core.relayer.confirmOnlineStateOrThrow()}catch{t.setError(N.no_internet_connection)}try{await this.core.relayer.subscribe(r,{relay:a})}catch(f){throw t.setError(N.subscribe_pairing_topic_failure),f}return t.addTrace(O.subscribe_pairing_topic_success),u},this.activate=async({topic:i})=>{this.isInitialized();const t=n.calcExpiry(c.FIVE_MINUTES);this.core.expirer.set(i,t),await this.pairings.update(i,{active:!0,expiry:t})},this.ping=async i=>{this.isInitialized(),await this.isValidPing(i),this.logger.warn("ping() is deprecated and will be removed in the next major release.");const{topic:t}=i;if(this.pairings.keys.includes(t)){const r=await this.sendRequest(t,"wc_pairingPing",{}),{done:o,resolve:a,reject:h}=n.createDelayedPromise();this.events.once(n.engineEvent("pairing_ping",r),({error:l})=>{l?h(l):a()}),await o()}},this.updateExpiry=async({topic:i,expiry:t})=>{this.isInitialized(),await this.pairings.update(i,{expiry:t})},this.updateMetadata=async({topic:i,metadata:t})=>{this.isInitialized(),await this.pairings.update(i,{peerMetadata:t})},this.getPairings=()=>(this.isInitialized(),this.pairings.values),this.disconnect=async i=>{this.isInitialized(),await this.isValidDisconnect(i);const{topic:t}=i;this.pairings.keys.includes(t)&&(await this.sendRequest(t,"wc_pairingDelete",n.getSdkError("USER_DISCONNECTED")),await this.deletePairing(t))},this.formatUriFromPairing=i=>{this.isInitialized();const{topic:t,relay:r,expiry:o,methods:a}=i,h=this.core.crypto.keychain.get(t);return n.formatUri({protocol:this.core.protocol,version:this.core.version,topic:t,symKey:h,relay:r,expiryTimestamp:o,methods:a})},this.sendRequest=async(i,t,r)=>{const o=b.formatJsonRpcRequest(t,r),a=await this.core.crypto.encode(i,o),h=z[t].req;return this.core.history.set(i,o),this.core.relayer.publish(i,a,h),o.id},this.sendResult=async(i,t,r)=>{const o=b.formatJsonRpcResult(i,r),a=await this.core.crypto.encode(t,o),h=(await this.core.history.get(t,i)).request.method,l=z[h].res;await this.core.relayer.publish(t,a,l),await this.core.history.resolve(o)},this.sendError=async(i,t,r)=>{const o=b.formatJsonRpcError(i,r),a=await this.core.crypto.encode(t,o),h=(await this.core.history.get(t,i)).request.method,l=z[h]?z[h].res:z.unregistered_method.res;await this.core.relayer.publish(t,a,l),await this.core.history.resolve(o)},this.deletePairing=async(i,t)=>{await this.core.relayer.unsubscribe(i),await Promise.all([this.pairings.delete(i,n.getSdkError("USER_DISCONNECTED")),this.core.crypto.deleteSymKey(i),t?Promise.resolve():this.core.expirer.del(i)])},this.cleanup=async()=>{const i=this.pairings.getAll().filter(t=>n.isExpired(t.expiry));await Promise.all(i.map(t=>this.deletePairing(t.topic)))},this.onRelayEventRequest=async i=>{const{topic:t,payload:r}=i;switch(r.method){case"wc_pairingPing":return await this.onPairingPingRequest(t,r);case"wc_pairingDelete":