@@ -45,9 +45,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
4545
4646 public cachedConnector : string | null = null ;
4747
48- public cachedCurrentChainId : string | null = null ;
49-
50- public currentChain : CustomChainConfig ;
48+ protected currentChainId : string ;
5149
5250 protected connectors : IConnector < unknown > [ ] = [ ] ;
5351
@@ -84,11 +82,14 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
8482 } ) ) ,
8583 } ;
8684
87- // handle cached current chain
88- this . cachedCurrentChainId = storageAvailable ( this . storage ) ? window [ this . storage ] . getItem ( CURRENT_CHAIN_CACHE_KEY ) : null ;
89- // use corrected chains from coreOptions
90- const cachedChain = this . cachedCurrentChainId ? this . coreOptions . chains . find ( ( chain ) => chain . chainId === this . cachedCurrentChainId ) : null ;
91- this . currentChain = cachedChain || this . coreOptions . chains [ 0 ] ; // use first chain in list as default chain config
85+ // init chainId using cached chainId if it exists and is valid, otherwise use the first chain
86+ const cachedChainId = storageAvailable ( this . storage ) ? window [ this . storage ] . getItem ( CURRENT_CHAIN_CACHE_KEY ) : null ;
87+ const isCachedChainIdValid = cachedChainId && this . coreOptions . chains . some ( ( chain ) => chain . chainId === cachedChainId ) ;
88+ this . currentChainId = isCachedChainIdValid ? cachedChainId : this . coreOptions . chains [ 0 ] . chainId ;
89+ }
90+
91+ get currentChain ( ) : CustomChainConfig {
92+ return this . coreOptions . chains . find ( ( chain ) => chain . chainId === this . currentChainId ) ;
9293 }
9394
9495 get connected ( ) : boolean {
@@ -111,12 +112,6 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
111112 }
112113
113114 public async init ( ) : Promise < void > {
114- // setup common JRPC provider
115- this . commonJRPCProvider = await CommonJRPCProvider . getProviderInstance ( {
116- chain : this . getCurrentChain ( ) ,
117- chains : this . getChains ( ) ,
118- } ) ;
119-
120115 // get project config
121116 let projectConfig : PROJECT_CONFIG_RESPONSE ;
122117 try {
@@ -130,6 +125,9 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
130125 throw WalletInitializationError . notReady ( "failed to fetch project configurations" , e ) ;
131126 }
132127
128+ // setup common JRPC provider
129+ await this . setupCommonJRPCProvider ( projectConfig ) ;
130+
133131 // initialize connectors
134132 this . on ( CONNECTOR_EVENTS . CONNECTORS_UPDATED , async ( { connectors } ) => {
135133 await Promise . all ( connectors . map ( this . setupConnector ) ) ;
@@ -223,20 +221,24 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
223221 return this . connectedConnector . authenticateUser ( ) ;
224222 }
225223
226- public getCurrentChain ( ) : CustomChainConfig {
227- return this . currentChain ;
224+ public getPlugin ( name : string ) : IPlugin | null {
225+ return this . plugins [ name ] || null ;
228226 }
229227
230- public getChain ( chainId : string ) : CustomChainConfig | undefined {
231- return this . coreOptions . chains . find ( ( chain ) => chain . chainId === chainId ) ;
232- }
228+ protected async setupCommonJRPCProvider ( projectConfig : PROJECT_CONFIG_RESPONSE ) {
229+ this . commonJRPCProvider = await CommonJRPCProvider . getProviderInstance ( {
230+ chain : this . currentChain ,
231+ chains : this . coreOptions . chains ,
232+ } ) ;
233233
234- public getChains ( ) : CustomChainConfig [ ] {
235- return this . coreOptions . chains ;
236- }
234+ // sync chainId
235+ this . commonJRPCProvider . on ( "chainChanged" , ( chainId ) => this . setCurrentChain ( chainId ) ) ;
237236
238- public getPlugin ( name : string ) : IPlugin | null {
239- return this . plugins [ name ] || null ;
237+ const { key_export_enabled : keyExportEnabled } = projectConfig ;
238+ if ( typeof keyExportEnabled === "boolean" ) {
239+ // dont know if we need to do this.
240+ this . commonJRPCProvider . setKeyExportFlag ( keyExportEnabled ) ;
241+ }
240242 }
241243
242244 protected async setupConnector ( connector : IConnector < unknown > ) : Promise < void > {
@@ -351,8 +353,8 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
351353 const aaProvider = await accountAbstractionProvider ( {
352354 accountAbstractionConfig,
353355 provider,
354- chain : this . getCurrentChain ( ) ,
355- chains : this . getChains ( ) ,
356+ chain : this . currentChain ,
357+ chains : this . coreOptions . chains ,
356358 } ) ;
357359 finalProvider = aaProvider ;
358360 // TODO: when switching chains to Solana or other chains, we need to switch to the non-AA provider
@@ -433,17 +435,16 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
433435 }
434436
435437 private setCurrentChain ( chainId : string ) {
436- if ( chainId === this . currentChain . chainId ) return ;
438+ if ( chainId === this . currentChainId ) return ;
437439 const newChain = this . coreOptions . chains . find ( ( chain ) => chain . chainId === chainId ) ;
438- if ( ! newChain ) throw WalletInitializationError . invalidParams ( " Invalid chainId" ) ;
439- this . currentChain = newChain ;
440+ if ( ! newChain ) throw WalletInitializationError . invalidParams ( ` Invalid chainId: ${ chainId } ` ) ;
441+ this . currentChainId = chainId ;
440442 this . cacheCurrentChain ( chainId ) ;
441443 }
442444
443445 private cacheCurrentChain ( chainId : string ) {
444446 if ( ! storageAvailable ( this . storage ) ) return ;
445447 window [ this . storage ] . setItem ( CURRENT_CHAIN_CACHE_KEY , chainId ) ;
446- this . cachedCurrentChainId = chainId ;
447448 }
448449
449450 private connectToPlugins ( data : { connector : WALLET_CONNECTOR_TYPE } ) {
0 commit comments