@@ -81,9 +81,6 @@ class WalletConnect2Session {
8181 private timeoutRef : NodeJS . Timeout | null = null ;
8282 private requestsToRedirect : { [ request : string ] : boolean } = { } ;
8383 private topicByRequestId : { [ requestId : string ] : string } = { } ;
84- private lastChainId : Hex ;
85- private isHandlingChainChange = false ;
86- private storeUnsubscribe : ( ( ) => void ) | null = null ;
8784
8885 private _isHandlingRequest = false ;
8986
@@ -209,8 +206,6 @@ class WalletConnect2Session {
209206 } ) ;
210207
211208 this . checkPendingRequests ( ) ;
212- this . lastChainId = this . getCurrentChainId ( ) ;
213- this . storeUnsubscribe = store . subscribe ( this . onStoreChange . bind ( this ) ) ;
214209 }
215210
216211 /**
@@ -235,19 +230,6 @@ class WalletConnect2Session {
235230 return getHostname ( this . selfReportedUrl ) ;
236231 }
237232
238- private onStoreChange ( ) {
239- const newChainId = this . getCurrentChainId ( ) ;
240- if ( newChainId !== this . lastChainId && ! this . isHandlingChainChange ) {
241- this . lastChainId = newChainId ;
242- const decimalChainId = Number . parseInt ( newChainId , 16 ) ;
243- this . handleChainChange ( decimalChainId ) . catch ( ( error ) => {
244- DevLogger . log (
245- 'WC2::store.subscribe Error handling chain change:' ,
246- error ,
247- ) ;
248- } ) ;
249- }
250- }
251233 public getCurrentChainId ( ) {
252234 const perOriginChainId = selectPerOriginChainId (
253235 store . getState ( ) ,
@@ -377,72 +359,6 @@ class WalletConnect2Session {
377359 }
378360 }
379361
380- /** Handle chain change by updating session namespaces and emitting event */
381- private async handleChainChange ( chainIdDecimal : number ) {
382- if ( this . isHandlingChainChange ) return ;
383- this . isHandlingChainChange = true ;
384-
385- try {
386- // Update session namespaces
387- const currentNamespaces = this . normalizeSessionNamespaces (
388- this . session . namespaces ,
389- ) ;
390- const newChainId = `eip155:${ chainIdDecimal } ` ;
391- const updatedChains = [
392- ...new Set ( [ ...( currentNamespaces ?. eip155 ?. chains || [ ] ) , newChainId ] ) ,
393- ] ;
394-
395- const accounts = [
396- ...new Set (
397- ( currentNamespaces ?. eip155 ?. accounts || [ ] ) . map (
398- ( acc ) => acc . split ( ':' ) [ 2 ] ,
399- ) ,
400- ) ,
401- ] . map ( ( account ) => `${ newChainId } :${ account } ` ) ;
402-
403- const updatedAccounts = [
404- ...new Set ( [
405- ...( currentNamespaces ?. eip155 ?. accounts || [ ] ) ,
406- ...accounts ,
407- ] ) ,
408- ] ;
409-
410- const updatedNamespaces = {
411- ...currentNamespaces ,
412- eip155 : {
413- ...( currentNamespaces ?. eip155 || { } ) ,
414- chains : updatedChains ,
415- methods : currentNamespaces ?. eip155 ?. methods || [ ] ,
416- events : currentNamespaces ?. eip155 ?. events || [ ] ,
417- accounts : updatedAccounts ,
418- } ,
419- } ;
420-
421- DevLogger . log (
422- `WC2::handleChainChange updating session with namespaces` ,
423- updatedNamespaces ,
424- ) ;
425-
426- await this . web3Wallet . updateSession ( {
427- topic : this . session . topic ,
428- namespaces : updatedNamespaces ,
429- } ) ;
430- // await acknowledged();
431-
432- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
433-
434- // Emit chainChanged event
435- await this . emitEvent ( 'chainChanged' , chainIdDecimal ) ;
436- } catch ( error ) {
437- DevLogger . log (
438- `WC2::handleChainChange error while updating session` ,
439- error ,
440- ) ;
441- throw error ;
442- } finally {
443- this . isHandlingChainChange = false ;
444- }
445- }
446362 approveRequest = async ( { id, result } : { id : string ; result : unknown } ) => {
447363 const topic = this . topicByRequestId [ id ] ;
448364
@@ -706,8 +622,8 @@ class WalletConnect2Session {
706622 await hasPermissionsToSwitchChainRequest ( caip2ChainId , channelId ) ;
707623
708624 if ( ! allowed && ! allowSwitchingToNewChain ) {
709- throw rpcErrors . invalidParams ( {
710- message : `Invalid parameters: active chainId is different than the one provided .` ,
625+ throw providerErrors . unauthorized ( {
626+ message : `Requested chain is not permitted for this WalletConnect session. Reconnect and approve ${ caip2ChainId } to continue .` ,
711627 } ) ;
712628 }
713629
@@ -926,8 +842,8 @@ class WalletConnect2Session {
926842
927843 if ( ! isAllowedChainId ) {
928844 DevLogger . log ( `WC::checkWCPermissions chainId is not permitted` ) ;
929- throw rpcErrors . invalidParams ( {
930- message : `Invalid parameters: active chainId is different than the one provided .` ,
845+ throw providerErrors . unauthorized ( {
846+ message : `Requested chain is not permitted for this WalletConnect session. Reconnect and approve ${ caip2ChainId } to continue .` ,
931847 } ) ;
932848 }
933849
@@ -1024,8 +940,6 @@ class WalletConnect2Session {
1024940 } ;
1025941
1026942 removeListeners = async ( ) => {
1027- this . storeUnsubscribe ?.( ) ;
1028- this . storeUnsubscribe = null ;
1029943 this . backgroundBridge . onDisconnect ( ) ;
1030944 } ;
1031945
0 commit comments