1 |
- "use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[2084],{62084:(t,e,s)=>{s.d(e,{EthereumProvider:()=>E});var i=s(31670),n=s(96374),a=s(27258);let r=["eth_sendTransaction","personal_sign"],h=["eth_accounts","eth_requestAccounts","eth_sendRawTransaction","eth_sign","eth_signTransaction","eth_signTypedData","eth_signTypedData_v3","eth_signTypedData_v4","eth_sendTransaction","personal_sign","wallet_switchEthereumChain","wallet_addEthereumChain","wallet_getPermissions","wallet_requestPermissions","wallet_registerOnboarding","wallet_watchAsset","wallet_scanQRCode","wallet_sendCalls","wallet_getCapabilities","wallet_getCallsStatus","wallet_showCallsStatus"],c=["chainChanged","accountsChanged"],o=["chainChanged","accountsChanged","message","disconnect","connect"];var l=Object.defineProperty,d=Object.defineProperties,p=Object.getOwnPropertyDescriptors,g=Object.getOwnPropertySymbols,u=Object.prototype.hasOwnProperty,m=Object.prototype.propertyIsEnumerable,C=(t,e,s)=>e in t?l(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,I=(t,e)=>{for(var s in e||(e={}))u.call(e,s)&&C(t,s,e[s]);if(g)for(var s of g(e))m.call(e,s)&&C(t,s,e[s]);return t},w=(t,e)=>d(t,p(e));function v(t){return Number(t[0].split(":")[1])}function f(t){return`0x${t.toString(16)}`}class _{constructor(){this.events=new i.EventEmitter,this.namespace="eip155",this.accounts=[],this.chainId=1,this.STORAGE_KEY="wc@2:ethereum_provider:",this.on=(t,e)=>(this.events.on(t,e),this),this.once=(t,e)=>(this.events.once(t,e),this),this.removeListener=(t,e)=>(this.events.removeListener(t,e),this),this.off=(t,e)=>(this.events.off(t,e),this),this.parseAccount=t=>this.isCompatibleChainId(t)?this.parseAccountId(t).address:t,this.signer={},this.rpc={}}static async init(t){let e=new _;return await e.initialize(t),e}async request(t,e){return await this.signer.request(t,this.formatChainId(this.chainId),e)}sendAsync(t,e,s){this.signer.sendAsync(t,e,this.formatChainId(this.chainId),s)}get connected(){return!!this.signer.client&&this.signer.client.core.relayer.connected}get connecting(){return!!this.signer.client&&this.signer.client.core.relayer.connecting}async enable(){return this.session||await this.connect(),await this.request({method:"eth_requestAccounts"})}async connect(t){if(!this.signer.client)throw Error("Provider not initialized. Call init() first");this.loadConnectOpts(t);let{required:e,optional:s}=function(t){let{chains:e,optionalChains:s,methods:i,optionalMethods:a,events:l,optionalEvents:d,rpcMap:p}=t;if(!(0,n.OP1)(e))throw Error("Invalid chains");let g={chains:e,methods:i||r,events:l||c,rpcMap:I({},e.length?{[v(e)]:p[v(e)]}:{})},u=l?.filter(t=>!c.includes(t)),m=i?.filter(t=>!r.includes(t));if(!s&&!d&&!a&&!(null!=u&&u.length)&&!(null!=m&&m.length))return{required:e.length?g:void 0};let C={chains:[...new Set(u?.length&&m?.length||!s?g.chains.concat(s||[]):s)],methods:[...new Set(g.methods.concat(null!=a&&a.length?a:h))],events:[...new Set(g.events.concat(null!=d&&d.length?d:o))],rpcMap:p};return{required:e.length?g:void 0,optional:s.length?C:void 0}}(this.rpc);try{let i=await new Promise(async(i,n)=>{var a;this.rpc.showQrModal&&(null==(a=this.modal)||a.subscribeModal(t=>{t.open||this.signer.session||(this.signer.abortPairingAttempt(),n(Error("Connection request reset. Please try again.")))})),await this.signer.connect(w(I({namespaces:I({},e&&{[this.namespace]:e})},s&&{optionalNamespaces:{[this.namespace]:s}}),{pairingTopic:t?.pairingTopic})).then(t=>{i(t)}).catch(t=>{n(Error(t.message))})});if(!i)return;let a=(0,n.Zz7)(i.namespaces,[this.namespace]);this.setChainIds(this.rpc.chains.length?this.rpc.chains:a),this.setAccounts(a),this.events.emit("connect",{chainId:f(this.chainId)})}catch(t){throw this.signer.logger.error(t),t}finally{this.modal&&this.modal.closeModal()}}async authenticate(t,e){if(!this.signer.client)throw Error("Provider not initialized. Call init() first");this.loadConnectOpts({chains:t?.chains});try{let s=await new Promise(async(s,i)=>{var n;this.rpc.showQrModal&&(null==(n=this.modal)||n.subscribeModal(t=>{t.open||this.signer.session||(this.signer.abortPairingAttempt(),i(Error("Connection request reset. Please try again.")))})),await this.signer.authenticate(w(I({},t),{chains:this.rpc.chains}),e).then(t=>{s(t)}).catch(t=>{i(Error(t.message))})}),i=s.session;if(i){let t=(0,n.Zz7)(i.namespaces,[this.namespace]);this.setChainIds(this.rpc.chains.length?this.rpc.chains:t),this.setAccounts(t),this.events.emit("connect",{chainId:f(this.chainId)})}return s}catch(t){throw this.signer.logger.error(t),t}finally{this.modal&&this.modal.closeModal()}}async disconnect(){this.session&&await this.signer.disconnect(),this.reset()}get isWalletConnect(){return!0}get session(){return this.signer.session}registerEventListeners(){this.signer.on("session_event",t=>{let{params:e}=t,{event:s}=e;"accountsChanged"===s.name?(this.accounts=this.parseAccounts(s.data),this.events.emit("accountsChanged",this.accounts)):"chainChanged"===s.name?this.setChainId(this.formatChainId(s.data)):this.events.emit(s.name,s.data),this.events.emit("session_event",t)}),this.signer.on("chainChanged",t=>{let e=parseInt(t);this.chainId=e,this.events.emit("chainChanged",f(this.chainId)),this.persist()}),this.signer.on("session_update",t=>{this.events.emit("session_update",t)}),this.signer.on("session_delete",t=>{this.reset(),this.events.emit("session_delete",t),this.events.emit("disconnect",w(I({},(0,n.Hjj)("USER_DISCONNECTED")),{data:t.topic,name:"USER_DISCONNECTED"}))}),this.signer.on("display_uri",t=>{var e,s;this.rpc.showQrModal&&(null==(e=this.modal)||e.closeModal(),null==(s=this.modal)||s.openModal({uri:t})),this.events.emit("display_uri",t)})}switchEthereumChain(t){this.request({method:"wallet_switchEthereumChain",params:[{chainId:t.toString(16)}]})}isCompatibleChainId(t){return"string"==typeof t&&t.startsWith(`${this.namespace}:`)}formatChainId(t){return`${this.namespace}:${t}`}parseChainId(t){return Number(t.split(":")[1])}setChainIds(t){let e=t.filter(t=>this.isCompatibleChainId(t)).map(t=>this.parseChainId(t));e.length&&(this.chainId=e[0],this.events.emit("chainChanged",f(this.chainId)),this.persist())}setChainId(t){if(this.isCompatibleChainId(t)){let e=this.parseChainId(t);this.chainId=e,this.switchEthereumChain(e)}}parseAccountId(t){let[e,s,i]=t.split(":");return{chainId:`${e}:${s}`,address:i}}setAccounts(t){this.accounts=t.filter(t=>this.parseChainId(this.parseAccountId(t).chainId)===this.chainId).map(t=>this.parseAccountId(t).address),this.events.emit("accountsChanged",this.accounts)}getRpcConfig(t){var e,s;let i=null!=(e=t?.chains)?e:[],n=null!=(s=t?.optionalChains)?s:[],a=i.concat(n);if(!a.length)throw Error("No chains specified in either `chains` or `optionalChains`");let h=i.length?t?.methods||r:[],o=i.length?t?.events||c:[],l=t?.optionalMethods||[],d=t?.optionalEvents||[],p=t?.rpcMap||this.buildRpcMap(a,t.projectId),g=t?.qrModalOptions||void 0;return{chains:i?.map(t=>this.formatChainId(t)),optionalChains:n.map(t=>this.formatChainId(t)),methods:h,events:o,optionalMethods:l,optionalEvents:d,rpcMap:p,showQrModal:!!(null!=t&&t.showQrModal),qrModalOptions:g,projectId:t.projectId,metadata:t.metadata}}buildRpcMap(t,e){let s={};return t.forEach(t=>{s[t]=this.getRpcUrl(t,e)}),s}async initialize(t){if(this.rpc=this.getRpcConfig(t),this.chainId=this.rpc.chains.length?v(this.rpc.chains):v(this.rpc.optionalChains),this.signer=await a.l.init({projectId:this.rpc.projectId,metadata:this.rpc.metadata,disableProviderPing:t.disableProviderPing,relayUrl:t.relayUrl,storageOptions:t.storageOptions,customStoragePrefix:t.customStoragePrefix,telemetryEnabled:t.telemetryEnabled}),this.registerEventListeners(),await this.loadPersistedSession(),this.rpc.showQrModal){let t;try{let{WalletConnectModal:e}=await s.e(1732).then(s.bind(s,91732));t=e}catch{throw Error("To use QR modal, please install @walletconnect/modal package")}if(t)try{this.modal=new t(I({projectId:this.rpc.projectId},this.rpc.qrModalOptions))}catch(t){throw this.signer.logger.error(t),Error("Could not generate WalletConnectModal Instance")}}}loadConnectOpts(t){if(!t)return;let{chains:e,optionalChains:s,rpcMap:i}=t;e&&(0,n.OP1)(e)&&(this.rpc.chains=e.map(t=>this.formatChainId(t)),e.forEach(t=>{this.rpc.rpcMap[t]=i?.[t]||this.getRpcUrl(t)})),s&&(0,n.OP1)(s)&&(this.rpc.optionalChains=[],this.rpc.optionalChains=s?.map(t=>this.formatChainId(t)),s.forEach(t=>{this.rpc.rpcMap[t]=i?.[t]||this.getRpcUrl(t)}))}getRpcUrl(t,e){var s;return(null==(s=this.rpc.rpcMap)?void 0:s[t])||`https://rpc.walletconnect.org/v1/?chainId=eip155:${t}&projectId=${e||this.rpc.projectId}`}async loadPersistedSession(){if(this.session)try{let t=await this.signer.client.core.storage.getItem(`${this.STORAGE_KEY}/chainId`),e=this.session.namespaces[`${this.namespace}:${t}`]?this.session.namespaces[`${this.namespace}:${t}`]:this.session.namespaces[this.namespace];this.setChainIds(t?[this.formatChainId(t)]:e?.accounts),this.setAccounts(e?.accounts)}catch(t){this.signer.logger.error("Failed to load persisted session, clearing state..."),this.signer.logger.error(t),await this.disconnect().catch(t=>this.signer.logger.warn(t))}}reset(){this.chainId=1,this.accounts=[]}persist(){this.session&&this.signer.client.core.storage.setItem(`${this.STORAGE_KEY}/chainId`,this.chainId)}parseAccounts(t){return"string"==typeof t||t instanceof String?[this.parseAccount(t)]:t.map(t=>this.parseAccount(t))}}let E=_}}]);
|