Skip to content

Commit 11b4ea1

Browse files
committed
sync chain from commonProvider to SDK
1 parent a4a87b0 commit 11b4ea1

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

packages/modal/src/modalManager.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
type BaseConnectorConfig,
33
cloneDeep,
4-
CommonJRPCProvider,
54
CONNECTOR_CATEGORY,
65
CONNECTOR_EVENTS,
76
CONNECTOR_NAMES,
@@ -81,15 +80,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
8180
await this.loginModal.initModal();
8281

8382
// setup common JRPC provider
84-
this.commonJRPCProvider = await CommonJRPCProvider.getProviderInstance({
85-
chain: this.currentChain,
86-
chains: this.coreOptions.chains,
87-
});
88-
const { key_export_enabled: keyExportEnabled } = projectConfig;
89-
if (typeof keyExportEnabled === "boolean") {
90-
// dont know if we need to do this.
91-
this.commonJRPCProvider.setKeyExportFlag(keyExportEnabled);
92-
}
83+
await this.setupCommonJRPCProvider(projectConfig);
9384

9485
// initialize connectors
9586
this.on(CONNECTOR_EVENTS.CONNECTORS_UPDATED, ({ connectors }) => this.initConnectors({ connectors, projectConfig, modalConfig: params }));

packages/no-modal/src/base/core/IWeb3Auth.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ export interface IWeb3AuthCoreOptions {
121121
export interface IWeb3AuthCore extends SafeEventEmitter {
122122
readonly coreOptions: IWeb3AuthCoreOptions;
123123
connectedConnectorName: string | null;
124+
currentChain: CustomChainConfig;
124125
status: CONNECTOR_STATUS_TYPE;
125126
provider: IProvider | null;
126127
init(): Promise<void>;
127-
getCurrentChain(): CustomChainConfig;
128128
getConnector(connectorName: WALLET_CONNECTOR_TYPE): IConnector<unknown> | null;
129129
getPlugin(pluginName: string): IPlugin | null;
130130
logout(options?: { cleanup: boolean }): Promise<void>;
@@ -136,9 +136,6 @@ export interface IWeb3AuthCore extends SafeEventEmitter {
136136
export interface IWeb3Auth extends IWeb3AuthCore {
137137
connected: boolean;
138138
cachedConnector: string | null;
139-
getCurrentChain(): CustomChainConfig;
140-
getChain(chainId: string): CustomChainConfig | undefined;
141-
getChains(): CustomChainConfig[];
142139
getConnector(connectorName: WALLET_CONNECTOR_TYPE): IConnector<unknown> | null;
143140
/**
144141
* Connect to a specific wallet connector

packages/no-modal/src/noModal.ts

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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 }) {

packages/no-modal/src/plugins/wallet-services-plugin/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class WalletServicesPlugin extends SafeEventEmitter implements IPlugin {
4949
if (this.isInitialized) return;
5050
if (!web3auth) throw WalletServicesPluginError.web3authRequired();
5151
if (web3auth.provider && !this.SUPPORTED_CONNECTORS.includes(web3auth.connectedConnectorName)) throw WalletServicesPluginError.notInitialized();
52-
const currentChainConfig = web3auth.getCurrentChain();
52+
const currentChainConfig = web3auth.currentChain;
5353
if (!([CHAIN_NAMESPACES.EIP155, CHAIN_NAMESPACES.SOLANA] as ChainNamespaceType[]).includes(currentChainConfig.chainNamespace))
5454
throw WalletServicesPluginError.unsupportedChainNamespace();
5555

0 commit comments

Comments
 (0)