@walletconnect/universal-provider
Version:
Universal Provider for WalletConnect Protocol
3 lines (2 loc) • 47.9 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var _e=require("@walletconnect/sign-client"),u=require("@walletconnect/utils"),re=require("@walletconnect/logger"),G=require("@walletconnect/jsonrpc-utils"),ne=require("@walletconnect/jsonrpc-http-connection"),P=require("@walletconnect/jsonrpc-provider"),Fe=require("events");function k(s){return s&&typeof s=="object"&&"default"in s?s:{default:s}}var Ue=k(_e),w=k(ne),xe=k(Fe);const ae="error",Le="wss://relay.walletconnect.org",Je="wc",Me="universal_provider",F=`${Je}@2:${Me}:`,ce="https://rpc.walletconnect.org/v1/",oe="generic",Be=`${ce}bundler`,O="call_status",Ge=86400,v={DEFAULT_CHAIN_CHANGED:"default_chain_changed"};function z(s){return s==null||typeof s!="object"&&typeof s!="function"}function he(s){return Object.getOwnPropertySymbols(s).filter(e=>Object.prototype.propertyIsEnumerable.call(s,e))}function pe(s){return s==null?s===void 0?"[object Undefined]":"[object Null]":Object.prototype.toString.call(s)}const ke="[object RegExp]",de="[object String]",ue="[object Number]",le="[object Boolean]",fe="[object Arguments]",ze="[object Symbol]",We="[object Date]",Ke="[object Map]",Ve="[object Set]",Ye="[object Array]",Xe="[object ArrayBuffer]",Qe="[object Object]",Ze="[object DataView]",Te="[object Uint8Array]",et="[object Uint8ClampedArray]",tt="[object Uint16Array]",st="[object Uint32Array]",it="[object Int8Array]",rt="[object Int16Array]",nt="[object Int32Array]",at="[object Float32Array]",ct="[object Float64Array]";function W(s){return ArrayBuffer.isView(s)&&!(s instanceof DataView)}function ot(s,e){return C(s,void 0,s,new Map,e)}function C(s,e,t,i=new Map,n=void 0){const a=n?.(s,e,t,i);if(a!=null)return a;if(z(s))return s;if(i.has(s))return i.get(s);if(Array.isArray(s)){const r=new Array(s.length);i.set(s,r);for(let c=0;c<s.length;c++)r[c]=C(s[c],c,t,i,n);return Object.hasOwn(s,"index")&&(r.index=s.index),Object.hasOwn(s,"input")&&(r.input=s.input),r}if(s instanceof Date)return new Date(s.getTime());if(s instanceof RegExp){const r=new RegExp(s.source,s.flags);return r.lastIndex=s.lastIndex,r}if(s instanceof Map){const r=new Map;i.set(s,r);for(const[c,o]of s)r.set(c,C(o,c,t,i,n));return r}if(s instanceof Set){const r=new Set;i.set(s,r);for(const c of s)r.add(C(c,void 0,t,i,n));return r}if(typeof Buffer<"u"&&Buffer.isBuffer(s))return s.subarray();if(W(s)){const r=new(Object.getPrototypeOf(s)).constructor(s.length);i.set(s,r);for(let c=0;c<s.length;c++)r[c]=C(s[c],c,t,i,n);return r}if(s instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&s instanceof SharedArrayBuffer)return s.slice(0);if(s instanceof DataView){const r=new DataView(s.buffer.slice(0),s.byteOffset,s.byteLength);return i.set(s,r),b(r,s,t,i,n),r}if(typeof File<"u"&&s instanceof File){const r=new File([s],s.name,{type:s.type});return i.set(s,r),b(r,s,t,i,n),r}if(s instanceof Blob){const r=new Blob([s],{type:s.type});return i.set(s,r),b(r,s,t,i,n),r}if(s instanceof Error){const r=new s.constructor;return i.set(s,r),r.message=s.message,r.name=s.name,r.stack=s.stack,r.cause=s.cause,b(r,s,t,i,n),r}if(typeof s=="object"&&ht(s)){const r=Object.create(Object.getPrototypeOf(s));return i.set(s,r),b(r,s,t,i,n),r}return s}function b(s,e,t=s,i,n){const a=[...Object.keys(e),...he(e)];for(let r=0;r<a.length;r++){const c=a[r],o=Object.getOwnPropertyDescriptor(s,c);(o==null||o.writable)&&(s[c]=C(e[c],c,t,i,n))}}function ht(s){switch(pe(s)){case fe:case Ye:case Xe:case Ze:case le:case We:case at:case ct:case it:case rt:case nt:case Ke:case ue:case Qe:case ke:case Ve:case de:case ze:case Te:case et:case tt:case st:return!0;default:return!1}}function pt(s,e){return ot(s,(t,i,n,a)=>{const r=e?.(t,i,n,a);if(r!=null)return r;if(typeof s=="object")switch(Object.prototype.toString.call(s)){case ue:case de:case le:{const c=new s.constructor(s?.valueOf());return b(c,s),c}case fe:{const c={};return b(c,s),c.length=s.length,c[Symbol.iterator]=s[Symbol.iterator],c}default:return}})}function me(s){return pt(s)}function ve(s){return s!==null&&typeof s=="object"&&pe(s)==="[object Arguments]"}function ge(s){return typeof s=="object"&&s!==null}function dt(){}function ut(s){return W(s)}function lt(s){if(typeof s!="object"||s==null)return!1;if(Object.getPrototypeOf(s)===null)return!0;if(Object.prototype.toString.call(s)!=="[object Object]"){const t=s[Symbol.toStringTag];return t==null||!Object.getOwnPropertyDescriptor(s,Symbol.toStringTag)?.writable?!1:s.toString()===`[object ${t}]`}let e=s;for(;Object.getPrototypeOf(e)!==null;)e=Object.getPrototypeOf(e);return Object.getPrototypeOf(s)===e}function ft(s){if(z(s))return s;if(Array.isArray(s)||W(s)||s instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&s instanceof SharedArrayBuffer)return s.slice(0);const e=Object.getPrototypeOf(s),t=e.constructor;if(s instanceof Date||s instanceof Map||s instanceof Set)return new t(s);if(s instanceof RegExp){const i=new t(s);return i.lastIndex=s.lastIndex,i}if(s instanceof DataView)return new t(s.buffer.slice(0));if(s instanceof Error){const i=new t(s.message);return i.stack=s.stack,i.name=s.name,i.cause=s.cause,i}if(typeof File<"u"&&s instanceof File)return new t([s],s.name,{type:s.type,lastModified:s.lastModified});if(typeof s=="object"){const i=Object.create(e);return Object.assign(i,s)}return s}function mt(s,...e){const t=e.slice(0,-1),i=e[e.length-1];let n=s;for(let a=0;a<t.length;a++){const r=t[a];n=U(n,r,i,new Map)}return n}function U(s,e,t,i){if(z(s)&&(s=Object(s)),e==null||typeof e!="object")return s;if(i.has(e))return ft(i.get(e));if(i.set(e,s),Array.isArray(e)){e=e.slice();for(let a=0;a<e.length;a++)e[a]=e[a]??void 0}const n=[...Object.keys(e),...he(e)];for(let a=0;a<n.length;a++){const r=n[a];let c=e[r],o=s[r];if(ve(c)&&(c={...c}),ve(o)&&(o={...o}),typeof Buffer<"u"&&Buffer.isBuffer(c)&&(c=me(c)),Array.isArray(c))if(typeof o=="object"&&o!=null){const l=[],p=Reflect.ownKeys(o);for(let y=0;y<p.length;y++){const f=p[y];l[f]=o[f]}o=l}else o=[];const d=t(o,c,r,s,e,i);d!=null?s[r]=d:Array.isArray(c)||ge(o)&&ge(c)?s[r]=U(o,c,t,i):o==null&<(c)?s[r]=U({},c,t,i):o==null&&ut(c)?s[r]=me(c):(o===void 0||c!==void 0)&&(s[r]=c)}return s}function vt(s,...e){return mt(s,...e,dt)}var gt=Object.defineProperty,Pt=Object.defineProperties,wt=Object.getOwnPropertyDescriptors,Pe=Object.getOwnPropertySymbols,yt=Object.prototype.hasOwnProperty,bt=Object.prototype.propertyIsEnumerable,we=(s,e,t)=>e in s?gt(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,x=(s,e)=>{for(var t in e||(e={}))yt.call(e,t)&&we(s,t,e[t]);if(Pe)for(var t of Pe(e))bt.call(e,t)&&we(s,t,e[t]);return s},It=(s,e)=>Pt(s,wt(e));function m(s,e,t){var i;const n=u.parseChainId(s);return((i=e.rpcMap)==null?void 0:i[n.reference])||`${ce}?chainId=${n.namespace}:${n.reference}&projectId=${t}`}function I(s){return s.includes(":")?s.split(":")[1]:s}function ye(s){return s.map(e=>`${e.split(":")[0]}:${e.split(":")[1]}`)}function $t(s,e){const t=Object.keys(e.namespaces).filter(n=>n.includes(s));if(!t.length)return[];const i=[];return t.forEach(n=>{const a=e.namespaces[n].accounts;i.push(...a)}),i}function be(s){return Object.fromEntries(Object.entries(s).filter(([e,t])=>{var i,n;return((i=t?.chains)==null?void 0:i.length)&&((n=t?.chains)==null?void 0:n.length)>0}))}function L(s={},e={}){const t=be(Ie(s)),i=be(Ie(e));return vt(t,i)}function Ie(s){var e,t,i,n,a;const r={};if(!u.isValidObject(s))return r;for(const[c,o]of Object.entries(s)){const d=u.isCaipNamespace(c)?[c]:o.chains,l=o.methods||[],p=o.events||[],y=o.rpcMap||{},f=u.parseNamespaceKey(c);r[f]=It(x(x({},r[f]),o),{chains:u.mergeArrays(d,(e=r[f])==null?void 0:e.chains),methods:u.mergeArrays(l,(t=r[f])==null?void 0:t.methods),events:u.mergeArrays(p,(i=r[f])==null?void 0:i.events)}),(u.isValidObject(y)||u.isValidObject(((n=r[f])==null?void 0:n.rpcMap)||{}))&&(r[f].rpcMap=x(x({},y),(a=r[f])==null?void 0:a.rpcMap))}return r}function $e(s){return s.includes(":")?s.split(":")[2]:s}function Oe(s){const e={};for(const[t,i]of Object.entries(s)){const n=i.methods||[],a=i.events||[],r=i.accounts||[],c=u.isCaipNamespace(t)?[t]:i.chains?i.chains:ye(i.accounts);e[t]={chains:c,methods:n,events:a,accounts:r}}return e}function K(s){return typeof s=="number"?s:s.includes("0x")?parseInt(s,16):(s=s.includes(":")?s.split(":")[1]:s,isNaN(Number(s))?s:Number(s))}function Ot(s){try{const e=JSON.parse(s);return typeof e=="object"&&e!==null&&!Array.isArray(e)}catch{return!1}}const Ce={},h=s=>Ce[s],V=(s,e)=>{Ce[s]=e};var Ct=Object.defineProperty,Ae=Object.getOwnPropertySymbols,At=Object.prototype.hasOwnProperty,Et=Object.prototype.propertyIsEnumerable,Ee=(s,e,t)=>e in s?Ct(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,He=(s,e)=>{for(var t in e||(e={}))At.call(e,t)&&Ee(s,t,e[t]);if(Ae)for(var t of Ae(e))Et.call(e,t)&&Ee(s,t,e[t]);return s};const Se="eip155",Ht=["atomic","flow-control","paymasterService","sessionKeys","auxiliaryFunds"],St=s=>s&&s.startsWith("0x")?BigInt(s).toString(10):s,Y=s=>s&&s.startsWith("0x")?s:`0x${BigInt(s).toString(16)}`,Ne=s=>Object.keys(s).filter(e=>Ht.includes(e)).reduce((e,t)=>(e[t]=Nt(s[t]),e),{}),Nt=s=>typeof s=="string"&&Ot(s)?JSON.parse(s):s,qt=(s,e,t)=>{const{sessionProperties:i={},scopedProperties:n={}}=s,a={};if(!u.isValidObject(n)&&!u.isValidObject(i))return;const r=Ne(i);for(const c of t){const o=St(c);if(!o)continue;a[Y(o)]=r;const d=n?.[`${Se}:${o}`];if(d){const l=d?.[`${Se}:${o}:${e}`];a[Y(o)]=He(He({},a[Y(o)]),Ne(l||d))}}for(const[c,o]of Object.entries(a))Object.keys(o).length===0&&delete a[c];return Object.keys(a).length>0?a:void 0};var jt=Object.defineProperty,Dt=(s,e,t)=>e in s?jt(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,Rt=(s,e,t)=>Dt(s,typeof e!="symbol"?e+"":e,t);let X;class se{constructor(e){Rt(this,"storage"),this.storage=e}async getItem(e){return await this.storage.getItem(e)}async setItem(e,t){return await this.storage.setItem(e,t)}async removeItem(e){return await this.storage.removeItem(e)}static getStorage(e){return X||(X=new se(e)),X}}var _t=Object.defineProperty,Ft=Object.defineProperties,Ut=Object.getOwnPropertyDescriptors,qe=Object.getOwnPropertySymbols,xt=Object.prototype.hasOwnProperty,Lt=Object.prototype.propertyIsEnumerable,je=(s,e,t)=>e in s?_t(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,Jt=(s,e)=>{for(var t in e||(e={}))xt.call(e,t)&&je(s,t,e[t]);if(qe)for(var t of qe(e))Lt.call(e,t)&&je(s,t,e[t]);return s},Mt=(s,e)=>Ft(s,Ut(e));async function Bt(s,e){const t=u.parseChainId(s.result.capabilities.caip345.caip2),i=s.result.capabilities.caip345.transactionHashes,n=await Promise.allSettled(i.map(p=>Gt(t.reference,p,e))),a=n.filter(p=>p.status==="fulfilled").map(p=>p.value).filter(p=>p);n.filter(p=>p.status==="rejected").forEach(p=>console.warn("Failed to fetch transaction receipt:",p.reason));const r=!a.length||a.some(p=>!p),c=a.every(p=>p?.status==="0x1"),o=a.every(p=>p?.status==="0x0"),d=a.some(p=>p?.status==="0x0");let l;return r?l=100:c?l=200:o?l=500:d&&(l=600),{id:s.result.id,version:s.request.version,atomic:s.request.atomicRequired,chainId:s.request.chainId,capabilities:s.result.capabilities,receipts:a,status:l}}async function Gt(s,e,t){return await t(parseInt(s)).request(G.formatJsonRpcRequest("eth_getTransactionReceipt",[e]))}async function kt({sendCalls:s,storage:e}){const t=await e.getItem(O);await e.setItem(O,Mt(Jt({},t),{[s.result.id]:{request:s.request,result:s.result,expiry:u.calcExpiry(Ge)}}))}async function zt({resultId:s,storage:e}){const t=await e.getItem(O);if(t){delete t[s],await e.setItem(O,t);for(const i in t)u.isExpired(t[i].expiry)&&delete t[i];await e.setItem(O,t)}}async function Wt({resultId:s,storage:e}){const t=await e.getItem(O),i=t?.[s];if(i&&!u.isExpired(i.expiry))return i;await zt({resultId:s,storage:e})}var Kt=Object.defineProperty,Vt=(s,e,t)=>e in s?Kt(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,A=(s,e,t)=>Vt(s,typeof e!="symbol"?e+"":e,t);class Yt{constructor(e){A(this,"name","polkadot"),A(this,"client"),A(this,"httpProviders"),A(this,"events"),A(this,"namespace"),A(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(e,t),this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${e}`)}getAccounts(){const e=this.namespace.accounts;return e?e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2])||[]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;const n=I(t);e[n]=this.createHttpProvider(n,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var Xt=Object.defineProperty,Qt=Object.defineProperties,Zt=Object.getOwnPropertyDescriptors,De=Object.getOwnPropertySymbols,Tt=Object.prototype.hasOwnProperty,es=Object.prototype.propertyIsEnumerable,Q=(s,e,t)=>e in s?Xt(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,Z=(s,e)=>{for(var t in e||(e={}))Tt.call(e,t)&&Q(s,t,e[t]);if(De)for(var t of De(e))es.call(e,t)&&Q(s,t,e[t]);return s},T=(s,e)=>Qt(s,Zt(e)),$=(s,e,t)=>Q(s,typeof e!="symbol"?e+"":e,t);class ts{constructor(e){$(this,"name","eip155"),$(this,"client"),$(this,"chainId"),$(this,"namespace"),$(this,"httpProviders"),$(this,"events"),$(this,"storage"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.httpProviders=this.createHttpProviders(),this.chainId=parseInt(this.getDefaultChain()),this.storage=se.getStorage(this.client.core.storage)}async request(e){switch(e.request.method){case"eth_requestAccounts":return this.getAccounts();case"eth_accounts":return this.getAccounts();case"wallet_switchEthereumChain":return await this.handleSwitchChain(e);case"eth_chainId":return parseInt(this.getDefaultChain());case"wallet_getCapabilities":return await this.getCapabilities(e);case"wallet_getCallsStatus":return await this.getCallStatus(e);case"wallet_sendCalls":return await this.sendCalls(e)}return this.namespace.methods.includes(e.request.method)?await this.client.request(e):this.getHttpProvider().request(e.request)}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(parseInt(e),t),this.chainId=parseInt(e),this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${e}`)}requestAccounts(){return this.getAccounts()}getDefaultChain(){if(this.chainId)return this.chainId.toString();if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}createHttpProvider(e,t){const i=t||m(`${this.name}:${e}`,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new ne.HttpConnection(i,h("disableProviderPing")))}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;const n=parseInt(I(t));e[n]=this.createHttpProvider(n,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}getHttpProvider(e){const t=e||this.chainId;return this.httpProviders[t]||(this.httpProviders=T(Z({},this.httpProviders),{[t]:this.createHttpProvider(t)}),this.httpProviders[t])}async handleSwitchChain(e){var t,i;let n=e.request.params?(t=e.request.params[0])==null?void 0:t.chainId:"0x0";n=n.startsWith("0x")?n:`0x${n}`;const a=parseInt(n,16);if(this.isChainApproved(a))this.setDefaultChain(`${a}`);else if(this.namespace.methods.includes("wallet_switchEthereumChain"))await this.client.request({topic:e.topic,request:{method:e.request.method,params:[{chainId:n}]},chainId:(i=this.namespace.chains)==null?void 0:i[0]}),this.setDefaultChain(`${a}`);else throw new Error(`Failed to switch to chain 'eip155:${a}'. The chain is not approved or the wallet does not support 'wallet_switchEthereumChain' method.`);return null}isChainApproved(e){return this.namespace.chains.includes(`${this.name}:${e}`)}async getCapabilities(e){var t,i,n,a,r;const c=(i=(t=e.request)==null?void 0:t.params)==null?void 0:i[0],o=((a=(n=e.request)==null?void 0:n.params)==null?void 0:a[1])||[];if(!c)throw new Error("Missing address parameter in `wallet_getCapabilities` request");const d=this.client.session.get(e.topic),l=((r=d?.sessionProperties)==null?void 0:r.capabilities)||{},p=`${c}${o.join(",")}`,y=l?.[p];if(y)return y;let f;try{f=qt(d,c,o)}catch(B){console.warn("Failed to extract capabilities from session",B)}if(f)return f;const ie=await this.client.request(e);try{await this.client.session.update(e.topic,{sessionProperties:T(Z({},d.sessionProperties||{}),{capabilities:T(Z({},l||{}),{[p]:ie})})})}catch(B){console.warn("Failed to update session with capabilities",B)}return ie}async getCallStatus(e){var t,i,n;const a=this.client.session.get(e.topic),r=(t=a.sessionProperties)==null?void 0:t.bundler_name;if(r){const d=this.getBundlerUrl(e.chainId,r);try{return await this.getUserOperationReceipt(d,e)}catch(l){console.warn("Failed to fetch call status from bundler",l,d)}}const c=(i=a.sessionProperties)==null?void 0:i.bundler_url;if(c)try{return await this.getUserOperationReceipt(c,e)}catch(d){console.warn("Failed to fetch call status from custom bundler",d,c)}const o=await Wt({resultId:(n=e.request.params)==null?void 0:n[0],storage:this.storage});if(o)try{return await Bt(o,this.getHttpProvider.bind(this))}catch(d){console.warn("Failed to fetch call status from stored send calls",d,o)}if(this.namespace.methods.includes(e.request.method))return await this.client.request(e);throw new Error("Fetching call status not approved by the wallet.")}async getUserOperationReceipt(e,t){var i;const n=new URL(e),a=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(G.formatJsonRpcRequest("eth_getUserOperationReceipt",[(i=t.request.params)==null?void 0:i[0]]))});if(!a.ok)throw new Error(`Failed to fetch user operation receipt - ${a.status}`);return await a.json()}getBundlerUrl(e,t){return`${Be}?projectId=${this.client.core.projectId}&chainId=${e}&bundler=${t}`}async sendCalls(e){var t,i,n;const a=await this.client.request(e),r=(t=e.request.params)==null?void 0:t[0],c=a?.id,o=a?.capabilities||{},d=(i=o?.caip345)==null?void 0:i.caip2,l=(n=o?.caip345)==null?void 0:n.transactionHashes;return!c||!d||!(l!=null&&l.length)||await kt({sendCalls:{request:r,result:a},storage:this.storage}),a}}var ss=Object.defineProperty,is=(s,e,t)=>e in s?ss(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,E=(s,e,t)=>is(s,typeof e!="symbol"?e+"":e,t);class rs{constructor(e){E(this,"name","solana"),E(this,"client"),E(this,"httpProviders"),E(this,"events"),E(this,"namespace"),E(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(e,t),this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${e}`)}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;const n=I(t);e[n]=this.createHttpProvider(n,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var ns=Object.defineProperty,as=(s,e,t)=>e in s?ns(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,H=(s,e,t)=>as(s,typeof e!="symbol"?e+"":e,t);class cs{constructor(e){H(this,"name","cosmos"),H(this,"client"),H(this,"httpProviders"),H(this,"events"),H(this,"namespace"),H(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(e,t),this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${this.chainId}`)}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;const n=I(t);e[n]=this.createHttpProvider(n,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var os=Object.defineProperty,hs=(s,e,t)=>e in s?os(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,S=(s,e,t)=>hs(s,typeof e!="symbol"?e+"":e,t);class ps{constructor(e){S(this,"name","algorand"),S(this,"client"),S(this,"httpProviders"),S(this,"events"),S(this,"namespace"),S(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){if(!this.httpProviders[e]){const i=t||m(`${this.name}:${e}`,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);this.setHttpProvider(e,i)}this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${this.chainId}`)}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;e[t]=this.createHttpProvider(t,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace,this.client.core.projectId);return typeof i>"u"?void 0:new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var ds=Object.defineProperty,us=(s,e,t)=>e in s?ds(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,N=(s,e,t)=>us(s,typeof e!="symbol"?e+"":e,t);class ls{constructor(e){N(this,"name","cip34"),N(this,"client"),N(this,"httpProviders"),N(this,"events"),N(this,"namespace"),N(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(e,t),this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${this.chainId}`)}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{const i=this.getCardanoRPCUrl(t),n=I(t);e[n]=this.createHttpProvider(n,i)}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}getCardanoRPCUrl(e){const t=this.namespace.rpcMap;if(t)return t[e]}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||this.getCardanoRPCUrl(e);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var fs=Object.defineProperty,ms=(s,e,t)=>e in s?fs(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,q=(s,e,t)=>ms(s,typeof e!="symbol"?e+"":e,t);class vs{constructor(e){q(this,"name","elrond"),q(this,"client"),q(this,"httpProviders"),q(this,"events"),q(this,"namespace"),q(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(e,t),this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${e}`)}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;const n=I(t);e[n]=this.createHttpProvider(n,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var gs=Object.defineProperty,Ps=(s,e,t)=>e in s?gs(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,j=(s,e,t)=>Ps(s,typeof e!="symbol"?e+"":e,t);class ws{constructor(e){j(this,"name","multiversx"),j(this,"client"),j(this,"httpProviders"),j(this,"events"),j(this,"namespace"),j(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(e,t),this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${e}`)}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;const n=I(t);e[n]=this.createHttpProvider(n,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var ys=Object.defineProperty,bs=(s,e,t)=>e in s?ys(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,D=(s,e,t)=>bs(s,typeof e!="symbol"?e+"":e,t);class Is{constructor(e){D(this,"name","near"),D(this,"client"),D(this,"httpProviders"),D(this,"events"),D(this,"namespace"),D(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){if(this.chainId=e,!this.httpProviders[e]){const i=t||m(`${this.name}:${e}`,this.namespace);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);this.setHttpProvider(e,i)}this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${this.chainId}`)}getAccounts(){const e=this.namespace.accounts;return e?e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2])||[]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{var i;e[t]=this.createHttpProvider(t,(i=this.namespace.rpcMap)==null?void 0:i[t])}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace);return typeof i>"u"?void 0:new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var $s=Object.defineProperty,Os=(s,e,t)=>e in s?$s(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,R=(s,e,t)=>Os(s,typeof e!="symbol"?e+"":e,t);class Cs{constructor(e){R(this,"name","tezos"),R(this,"client"),R(this,"httpProviders"),R(this,"events"),R(this,"namespace"),R(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace=Object.assign(this.namespace,e)}requestAccounts(){return this.getAccounts()}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider().request(e.request)}setDefaultChain(e,t){if(this.chainId=e,!this.httpProviders[e]){const i=t||m(`${this.name}:${e}`,this.namespace);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);this.setHttpProvider(e,i)}this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${this.chainId}`)}getAccounts(){const e=this.namespace.accounts;return e?e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2])||[]:[]}createHttpProviders(){const e={};return this.namespace.chains.forEach(t=>{e[t]=this.createHttpProvider(t)}),e}getHttpProvider(){const e=`${this.name}:${this.chainId}`,t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace);return typeof i>"u"?void 0:new P.JsonRpcProvider(new w.default(i))}}var As=Object.defineProperty,Es=(s,e,t)=>e in s?As(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,_=(s,e,t)=>Es(s,typeof e!="symbol"?e+"":e,t);class Hs{constructor(e){_(this,"name",oe),_(this,"client"),_(this,"httpProviders"),_(this,"events"),_(this,"namespace"),_(this,"chainId"),this.namespace=e.namespace,this.events=h("events"),this.client=h("client"),this.chainId=this.getDefaultChain(),this.name=this.getNamespaceName(),this.httpProviders=this.createHttpProviders()}updateNamespace(e){this.namespace.chains=[...new Set((this.namespace.chains||[]).concat(e.chains||[]))],this.namespace.accounts=[...new Set((this.namespace.accounts||[]).concat(e.accounts||[]))],this.namespace.methods=[...new Set((this.namespace.methods||[]).concat(e.methods||[]))],this.namespace.events=[...new Set((this.namespace.events||[]).concat(e.events||[]))],this.httpProviders=this.createHttpProviders()}requestAccounts(){return this.getAccounts()}request(e){return this.namespace.methods.includes(e.request.method)?this.client.request(e):this.getHttpProvider(e.chainId).request(e.request)}setDefaultChain(e,t){this.httpProviders[e]||this.setHttpProvider(e,t),this.chainId=e,this.events.emit(v.DEFAULT_CHAIN_CHANGED,`${this.name}:${e}`)}getDefaultChain(){if(this.chainId)return this.chainId;if(this.namespace.defaultChain)return this.namespace.defaultChain;const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return e.split(":")[1]}getNamespaceName(){const e=this.namespace.chains[0];if(!e)throw new Error("ChainId not found");return u.parseChainId(e).namespace}getAccounts(){const e=this.namespace.accounts;return e?[...new Set(e.filter(t=>t.split(":")[1]===this.chainId.toString()).map(t=>t.split(":")[2]))]:[]}createHttpProviders(){var e,t;const i={};return(t=(e=this.namespace)==null?void 0:e.accounts)==null||t.forEach(n=>{const a=u.parseChainId(n);i[a.reference]=this.createHttpProvider(n)}),i}getHttpProvider(e){const t=this.httpProviders[e];if(typeof t>"u")throw new Error(`JSON-RPC provider for ${e} not found`);return t}setHttpProvider(e,t){const i=this.createHttpProvider(e,t);i&&(this.httpProviders[e]=i)}createHttpProvider(e,t){const i=t||m(e,this.namespace,this.client.core.projectId);if(!i)throw new Error(`No RPC url provided for chainId: ${e}`);return new P.JsonRpcProvider(new w.default(i,h("disableProviderPing")))}}var Ss=Object.defineProperty,Ns=Object.defineProperties,qs=Object.getOwnPropertyDescriptors,Re=Object.getOwnPropertySymbols,js=Object.prototype.hasOwnProperty,Ds=Object.prototype.propertyIsEnumerable,ee=(s,e,t)=>e in s?Ss(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,J=(s,e)=>{for(var t in e||(e={}))js.call(e,t)&&ee(s,t,e[t]);if(Re)for(var t of Re(e))Ds.call(e,t)&&ee(s,t,e[t]);return s},te=(s,e)=>Ns(s,qs(e)),g=(s,e,t)=>ee(s,typeof e!="symbol"?e+"":e,t);class M{constructor(e){g(this,"client"),g(this,"namespaces"),g(this,"optionalNamespaces"),g(this,"sessionProperties"),g(this,"scopedProperties"),g(this,"events",new xe.default),g(this,"rpcProviders",{}),g(this,"session"),g(this,"providerOpts"),g(this,"logger"),g(this,"uri"),g(this,"disableProviderPing",!1),this.providerOpts=e,this.logger=typeof e?.logger<"u"&&typeof e?.logger!="string"?e.logger:re.pino(re.getDefaultLoggerOptions({level:e?.logger||ae})),this.disableProviderPing=e?.disableProviderPing||!1}static async init(e){const t=new M(e);return await t.initialize(),t}async request(e,t,i){const[n,a]=this.validateChain(t);if(!this.session)throw new Error("Please call connect() before request()");return await this.getProvider(n).request({request:J({},e),chainId:`${n}:${a}`,topic:this.session.topic,expiry:i})}sendAsync(e,t,i,n){const a=new Date().getTime();this.request(e,i,n).then(r=>t(null,G.formatJsonRpcResult(a,r))).catch(r=>t(r,void 0))}async enable(){if(!this.client)throw new Error("Sign Client not initialized");return this.session||await this.connect({namespaces:this.namespaces,optionalNamespaces:this.optionalNamespaces,sessionProperties:this.sessionProperties,scopedProperties:this.scopedProperties}),await this.requestAccounts()}async disconnect(){var e;if(!this.session)throw new Error("Please call connect() before enable()");await this.client.disconnect({topic:(e=this.session)==null?void 0:e.topic,reason:u.getSdkError("USER_DISCONNECTED")}),await this.cleanup()}async connect(e){if(!this.client)throw new Error("Sign Client not initialized");if(this.setNamespaces(e),this.cleanupPendingPairings(),!e.skipPairing)return await this.pair(e.pairingTopic)}async authenticate(e,t){if(!this.client)throw new Error("Sign Client not initialized");this.setNamespaces(e),await this.cleanupPendingPairings();const{uri:i,response:n}=await this.client.authenticate(e,t);i&&(this.uri=i,this.events.emit("display_uri",i));const a=await n();if(this.session=a.session,this.session){const r=Oe(this.session.namespaces);this.namespaces=L(this.namespaces,r),await this.persist("namespaces",this.namespaces),this.onConnect()}return a}on(e,t){this.events.on(e,t)}once(e,t){this.events.once(e,t)}removeListener(e,t){this.events.removeListener(e,t)}off(e,t){this.events.off(e,t)}get isWalletConnect(){return!0}async pair(e){const{uri:t,approval:i}=await this.client.connect({pairingTopic:e,requiredNamespaces:this.namespaces,optionalNamespaces:this.optionalNamespaces,sessionProperties:this.sessionProperties,scopedProperties:this.scopedProperties});t&&(this.uri=t,this.events.emit("display_uri",t));const n=await i();this.session=n;const a=Oe(n.namespaces);return this.namespaces=L(this.namespaces,a),await this.persist("namespaces",this.namespaces),await this.persist("optionalNamespaces",this.optionalNamespaces),this.onConnect(),this.session}setDefaultChain(e,t){try{if(!this.session)return;const[i,n]=this.validateChain(e);this.getProvider(i).setDefaultChain(n,t)}catch(i){if(!/Please call connect/.test(i.message))throw i}}async cleanupPendingPairings(e={}){try{this.logger.info("Cleaning up inactive pairings...");const t=this.client.pairing.getAll();if(!u.isValidArray(t))return;for(const i of t)e.deletePairings?this.client.core.expirer.set(i.topic,0):await this.client.core.relayer.subscriber.unsubscribe(i.topic);this.logger.info(`Inactive pairings cleared: ${t.length}`)}catch(t){this.logger.warn("Failed to cleanup pending pairings",t)}}abortPairingAttempt(){this.logger.warn("abortPairingAttempt is deprecated. This is now a no-op.")}async checkStorage(){this.namespaces=await this.getFromStore("namespaces")||{},this.optionalNamespaces=await this.getFromStore("optionalNamespaces")||{},this.session&&this.createProviders()}async initialize(){this.logger.trace("Initialized"),await this.createClient(),await this.checkStorage(),this.registerEventListeners()}async createClient(){var e,t;if(this.client=this.providerOpts.client||await Ue.default.init({core:this.providerOpts.core,logger:this.providerOpts.logger||ae,relayUrl:this.providerOpts.relayUrl||Le,projectId:this.providerOpts.projectId,metadata:this.providerOpts.metadata,storageOptions:this.providerOpts.storageOptions,storage:this.providerOpts.storage,name:this.providerOpts.name,customStoragePrefix:this.providerOpts.customStoragePrefix,telemetryEnabled:this.providerOpts.telemetryEnabled}),this.providerOpts.session)try{this.session=this.client.session.get(this.providerOpts.session.topic)}catch(i){throw this.logger.error("Failed to get session",i),new Error(`The provided session: ${(t=(e=this.providerOpts)==null?void 0:e.session)==null?void 0:t.topic} doesn't exist in the Sign client`)}else{const i=this.client.session.getAll();this.session=i[0]}this.logger.trace("SignClient Initialized")}createProviders(){if(!this.client)throw new Error("Sign Client not initialized");if(!this.session)throw new Error("Session not initialized. Please call connect() before enable()");const e=[...new Set(Object.keys(this.session.namespaces).map(t=>u.parseNamespaceKey(t)))];V("client",this.client),V("events",this.events),V("disableProviderPing",this.disableProviderPing),e.forEach(t=>{if(!this.session)return;const i=$t(t,this.session);if(i?.length===0)return;const n=ye(i),a=L(this.namespaces,this.optionalNamespaces),r=te(J({},a[t]),{accounts:i,chains:n});switch(t){case"eip155":this.rpcProviders[t]=new ts({namespace:r});break;case"algorand":this.rpcProviders[t]=new ps({namespace:r});break;case"solana":this.rpcProviders[t]=new rs({namespace:r});break;case"cosmos":this.rpcProviders[t]=new cs({namespace:r});break;case"polkadot":this.rpcProviders[t]=new Yt({namespace:r});break;case"cip34":this.rpcProviders[t]=new ls({namespace:r});break;case"elrond":this.rpcProviders[t]=new vs({namespace:r});break;case"multiversx":this.rpcProviders[t]=new ws({namespace:r});break;case"near":this.rpcProviders[t]=new Is({namespace:r});break;case"tezos":this.rpcProviders[t]=new Cs({namespace:r});break;default:this.rpcProviders[t]=new Hs({namespace:r})}})}registerEventListeners(){if(typeof this.client>"u")throw new Error("Sign Client is not initialized");this.client.on("session_ping",e=>{var t;const{topic:i}=e;i===((t=this.session)==null?void 0:t.topic)&&this.events.emit("session_ping",e)}),this.client.on("session_event",e=>{var t;const{params:i,topic:n}=e;if(n!==((t=this.session)==null?void 0:t.topic))return;const{event:a}=i;if(a.name==="accountsChanged"){const r=a.data;r&&u.isValidArray(r)&&this.events.emit("accountsChanged",r.map($e))}else if(a.name==="chainChanged"){const r=i.chainId,c=i.event.data,o=u.parseNamespaceKey(r),d=K(r)!==K(c)?`${o}:${K(c)}`:r;this.onChainChanged(d)}else this.events.emit(a.name,a.data);this.events.emit("session_event",e)}),this.client.on("session_update",({topic:e,params:t})=>{var i,n;if(e!==((i=this.session)==null?void 0:i.topic))return;const{namespaces:a}=t,r=(n=this.client)==null?void 0:n.session.get(e);this.session=te(J({},r),{namespaces:a}),this.onSessionUpdate(),this.events.emit("session_update",{topic:e,params:t})}),this.client.on("session_delete",async e=>{var t;e.topic===((t=this.session)==null?void 0:t.topic)&&(await this.cleanup(),this.events.emit("session_delete",e),this.events.emit("disconnect",te(J({},u.getSdkError("USER_DISCONNECTED")),{data:e.topic})))}),this.on(v.DEFAULT_CHAIN_CHANGED,e=>{this.onChainChanged(e,!0)})}getProvider(e){return this.rpcProviders[e]||this.rpcProviders[oe]}onSessionUpdate(){Object.keys(this.rpcProviders).forEach(e=>{var t;this.getProvider(e).updateNamespace((t=this.session)==null?void 0:t.namespaces[e])})}setNamespaces(e){const{namespaces:t={},optionalNamespaces:i={},sessionProperties:n,scopedProperties:a}=e;this.optionalNamespaces=L(t,i),this.sessionProperties=n,this.scopedProperties=a}validateChain(e){const[t,i]=e?.split(":")||["",""];if(!this.namespaces||!Object.keys(this.namespaces).length)return[t,i];if(t&&!Object.keys(this.namespaces||{}).map(r=>u.parseNamespaceKey(r)).includes(t))throw new Error(`Namespace '${t}' is not configured. Please call connect() first with namespace config.`);if(t&&i)return[t,i];const n=u.parseNamespaceKey(Object.keys(this.namespaces)[0]),a=this.rpcProviders[n].getDefaultChain();return[n,a]}async requestAccounts(){const[e]=this.validateChain();return await this.getProvider(e).requestAccounts()}async onChainChanged(e,t=!1){if(!this.namespaces)return;const[i,n]=this.validateChain(e);if(!n)return;this.updateNamespaceChain(i,n);const a=this.getProvider(i).getDefaultChain();t?(this.events.emit("chainChanged",n),this.emitAccountsChangedOnChainChange({namespace:i,previousChainId:a,newChainId:e})):this.getProvider(i).setDefaultChain(n),await this.persist("namespaces",this.namespaces)}emitAccountsChangedOnChainChange({namespace:e,previousChainId:t,newChainId:i}){var n,a;try{if(t===i)return;const r=(a=(n=this.session)==null?void 0:n.namespaces[e])==null?void 0:a.accounts;if(!r)return;const c=r.filter(o=>o.includes(`${i}:`)).map($e);if(!u.isValidArray(c))return;this.events.emit("accountsChanged",c)}catch(r){this.logger.warn("Failed to emit accountsChanged on chain change",r)}}updateNamespaceChain(e,t){if(!this.namespaces)return;const i=this.namespaces[e]?e:`${e}:${t}`,n={chains:[],methods:[],events:[],defaultChain:t};this.namespaces[i]?this.namespaces[i]&&(this.namespaces[i].defaultChain=t):this.namespaces[i]=n}onConnect(){this.createProviders(),this.events.emit("connect",{session:this.session})}async cleanup(){this.namespaces=void 0,this.optionalNamespaces=void 0,this.sessionProperties=void 0,await this.deleteFromStore("namespaces"),await this.deleteFromStore("optionalNamespaces"),await this.deleteFromStore("sessionProperties"),this.session=void 0,this.cleanupPendingPairings({deletePairings:!0}),await this.cleanupStorage()}async persist(e,t){var i;const n=((i=this.session)==null?void 0:i.topic)||"";await this.client.core.storage.setItem(`${F}/${e}${n}`,t)}async getFromStore(e){var t;const i=((t=this.session)==null?void 0:t.topic)||"";return await this.client.core.storage.getItem(`${F}/${e}${i}`)}async deleteFromStore(e){var t;const i=((t=this.session)==null?void 0:t.topic)||"";await this.client.core.storage.removeItem(`${F}/${e}${i}`)}async cleanupStorage(){var e;try{if(((e=this.client)==null?void 0:e.session.length)>0)return;const t=await this.client.core.storage.getKeys();for(const i of t)i.startsWith(F)&&await this.client.core.storage.removeItem(i)}catch(t){this.logger.warn("Failed to cleanup storage",t)}}}const Rs=M;exports.UniversalProvider=Rs,exports.default=M;
//# sourceMappingURL=index.cjs.js.map