Skip to content

Commit 196e30a

Browse files
author
Nguyen Anh Tu
committed
correct current chain state for provider
1 parent 8944bf2 commit 196e30a

File tree

21 files changed

+179
-158
lines changed

21 files changed

+179
-158
lines changed

packages/modal/src/modalManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
8181

8282
// setup common JRPC provider
8383
this.commonJRPCProvider = await CommonJRPCProvider.getProviderInstance({
84-
getCurrentChain: this.getCurrentChain.bind(this),
85-
getChain: this.getChain.bind(this),
84+
chain: this.currentChain,
85+
chains: this.coreOptions.chains,
8686
});
8787
const { key_export_enabled: keyExportEnabled } = projectConfig;
8888
if (typeof keyExportEnabled === "boolean") {

packages/no-modal/src/base/connector/interfaces.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "@web3auth/auth";
1414

1515
import { ConnectorNamespaceType, CustomChainConfig } from "../chain/IChainInterface";
16-
import { IWeb3Auth, IWeb3AuthCoreOptions } from "../core/IWeb3Auth";
16+
import { IWeb3AuthCoreOptions } from "../core/IWeb3Auth";
1717
import { Web3AuthError } from "../errors";
1818
import { PROJECT_CONFIG_RESPONSE } from "../interfaces";
1919
import { ProviderEvents, SafeEventEmitterProvider } from "../provider/IProvider";
@@ -42,8 +42,6 @@ export type UserAuthInfo = { idToken: string };
4242

4343
export interface BaseConnectorSettings {
4444
coreOptions: IWeb3AuthCoreOptions;
45-
getCurrentChain: IWeb3Auth["getCurrentChain"];
46-
getChain: IWeb3Auth["getChain"];
4745
}
4846

4947
export interface IProvider extends SafeEventEmitter<ProviderEvents> {
@@ -85,8 +83,6 @@ export interface IConnector<T> extends SafeEventEmitter {
8583
export type ConnectorParams = {
8684
projectConfig?: PROJECT_CONFIG_RESPONSE;
8785
coreOptions: IWeb3AuthCoreOptions;
88-
getCurrentChain: IWeb3Auth["getCurrentChain"];
89-
getChain: IWeb3Auth["getChain"];
9086
};
9187

9288
export type ConnectorFn = (params: ConnectorParams) => IConnector<unknown>;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export interface IWeb3Auth extends IWeb3AuthCore {
134134
cachedConnector: string | null;
135135
getCurrentChain(): CustomChainConfig;
136136
getChain(chainId: string): CustomChainConfig | undefined;
137+
getChains(): CustomChainConfig[];
137138
getConnector(connectorName: WALLET_CONNECTOR_TYPE): IConnector<unknown> | null;
138139
/**
139140
* Connect to a specific wallet connector

packages/no-modal/src/connectors/auth-connector/authConnector.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,11 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
5656

5757
private wsEmbedInstance: WsEmbed | null = null;
5858

59-
private getCurrentChain: AuthConnectorOptions["getCurrentChain"];
60-
61-
private getChain: AuthConnectorOptions["getChain"];
62-
6359
constructor(params: AuthConnectorOptions) {
6460
super(params);
6561

6662
// set auth options
67-
const defaultOptions = getAuthDefaultOptions({
68-
getCurrentChain: params.getCurrentChain,
69-
getChain: params.getChain,
70-
});
63+
const defaultOptions = getAuthDefaultOptions();
7164
log.info("setting connector settings", params.connectorSettings);
7265
this.authOptions = deepmerge.all([
7366
defaultOptions.connectorSettings,
@@ -77,8 +70,6 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
7770

7871
this.loginSettings = params.loginSettings || { loginProvider: "" };
7972
this.wsSettings = params.walletServicesSettings || {};
80-
this.getCurrentChain = params.getCurrentChain;
81-
this.getChain = params.getChain;
8273
}
8374

8475
get provider(): IProvider | null {
@@ -140,14 +131,17 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
140131
case CHAIN_NAMESPACES.XRPL: {
141132
const { XrplPrivateKeyProvider } = await import("@/core/xrpl-provider");
142133
this.privateKeyProvider = new XrplPrivateKeyProvider({
143-
config: { getCurrentChain: this.getCurrentChain, getChain: this.getChain },
134+
config: { chain: chainConfig, chains: this.coreOptions.chains },
144135
});
145136
break;
146137
}
147138
default: {
148139
const { CommonPrivateKeyProvider } = await import("@/core/base-provider");
149140
this.privateKeyProvider = new CommonPrivateKeyProvider({
150-
config: {},
141+
config: {
142+
chain: chainConfig,
143+
chains: this.coreOptions.chains,
144+
},
151145
});
152146
}
153147
}
@@ -273,6 +267,10 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
273267
}
274268
}
275269

270+
private getChain(chainId: string) {
271+
return this.coreOptions.chains.find((x) => x.chainId === chainId);
272+
}
273+
276274
private _getFinalPrivKey() {
277275
if (!this.authInstance) return "";
278276
let finalPrivKey = this.authInstance.privKey;
@@ -353,7 +351,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
353351
}
354352

355353
export const authConnector = (params?: { uxMode?: UX_MODE_TYPE }): ConnectorFn => {
356-
return ({ projectConfig, coreOptions, getCurrentChain, getChain }: ConnectorParams) => {
354+
return ({ projectConfig, coreOptions }: ConnectorParams) => {
357355
const connectorSettings: AuthConnectorOptions["connectorSettings"] = {
358356
network: coreOptions.web3AuthNetwork || WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
359357
clientId: coreOptions.clientId,
@@ -395,8 +393,6 @@ export const authConnector = (params?: { uxMode?: UX_MODE_TYPE }): ConnectorFn =
395393
connectorSettings,
396394
walletServicesSettings: finalWsSettings,
397395
coreOptions,
398-
getCurrentChain,
399-
getChain,
400396
};
401397
return new AuthConnector(connectorOptions);
402398
};
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { UX_MODE, WEB3AUTH_NETWORK } from "@web3auth/auth";
22

3-
import { IWeb3Auth } from "@/core/base";
4-
53
import { AuthConnectorOptions } from "./interface";
64

7-
export const getAuthDefaultOptions = ({
8-
getCurrentChain,
9-
getChain,
10-
}: {
11-
getCurrentChain: IWeb3Auth["getCurrentChain"];
12-
getChain: IWeb3Auth["getChain"];
13-
}): AuthConnectorOptions => {
5+
export const getAuthDefaultOptions = (): AuthConnectorOptions => {
146
return {
157
connectorSettings: {
168
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
@@ -22,7 +14,5 @@ export const getAuthDefaultOptions = ({
2214
chains: [],
2315
clientId: "",
2416
},
25-
getCurrentChain,
26-
getChain,
2717
};
2818
};

packages/no-modal/src/connectors/coinbase-connector/coinbaseConnector.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,10 @@ class CoinbaseConnector extends BaseEvmConnector<void> {
167167
}
168168

169169
export const coinbaseConnector = (params?: CoinbaseWalletSDKOptions): ConnectorFn => {
170-
return ({ coreOptions, getCurrentChain, getChain }: ConnectorParams) => {
170+
return ({ coreOptions }: ConnectorParams) => {
171171
return new CoinbaseConnector({
172172
connectorSettings: params,
173173
coreOptions,
174-
getCurrentChain,
175-
getChain,
176174
});
177175
};
178176
};

packages/no-modal/src/connectors/default-evm-connector/injectedEvmConnector.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,11 @@ class InjectedEvmConnector extends BaseEvmConnector<void> {
173173
}
174174

175175
export const injectedEvmConnector = (providerDetail: EIP6963ProviderDetail): ConnectorFn => {
176-
return ({ coreOptions, getCurrentChain, getChain }: ConnectorParams) => {
176+
return ({ coreOptions }: ConnectorParams) => {
177177
return new InjectedEvmConnector({
178178
name: normalizeWalletName(providerDetail.info.name),
179179
provider: providerDetail.provider as IProvider,
180180
coreOptions,
181-
getCurrentChain,
182-
getChain,
183181
});
184182
};
185183
};

packages/no-modal/src/connectors/default-solana-connector/walletStandardConnector.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,9 @@ export class WalletStandardConnector extends BaseSolanaConnector<void> {
4646

4747
private injectedProvider: WalletStandardProvider | null = null;
4848

49-
private getCurrentChain: BaseConnectorSettings["getCurrentChain"];
50-
51-
private getChain: BaseConnectorSettings["getChain"];
52-
5349
constructor(options: BaseConnectorSettings & { name: string; wallet: Wallet }) {
5450
super(options);
5551
this.name = options.name;
56-
this.getCurrentChain = options.getCurrentChain;
57-
this.getChain = options.getChain;
5852
// in VueJS, for some wallets e.g. Gate, Solflare, when connecting it throws error "attempted to get private field on non-instance"
5953
// it seems that Vue create a Proxy object for the wallet object which causes the issue
6054
// ref: https://stackoverflow.com/questions/64917686/vue-array-converted-to-proxy-object
@@ -77,7 +71,7 @@ export class WalletStandardConnector extends BaseSolanaConnector<void> {
7771
const chainConfig = this.coreOptions.chains.find((x) => x.chainId === options.chainId);
7872
super.checkInitializationRequirements({ chainConfig });
7973

80-
this.injectedProvider = new WalletStandardProvider({ config: { getCurrentChain: this.getCurrentChain, getChain: this.getChain } });
74+
this.injectedProvider = new WalletStandardProvider({ config: { chain: chainConfig, chains: this.coreOptions.chains } });
8175
const providerHandler = new WalletStandardProviderHandler({
8276
wallet: this.wallet,
8377
getCurrentChain: () => getSolanaChainByChainConfig(chainConfig),
@@ -165,13 +159,11 @@ export class WalletStandardConnector extends BaseSolanaConnector<void> {
165159
}
166160

167161
export const walletStandardConnector = (wallet: Wallet): ConnectorFn => {
168-
return ({ coreOptions, getCurrentChain, getChain }: ConnectorParams) => {
162+
return ({ coreOptions }: ConnectorParams) => {
169163
return new WalletStandardConnector({
170164
name: normalizeWalletName(wallet.name),
171165
wallet,
172166
coreOptions,
173-
getCurrentChain,
174-
getChain,
175167
});
176168
};
177169
};

packages/no-modal/src/connectors/wallet-connect-v2-connector/WalletConnectV2Provider.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ISignClient, SignClientTypes } from "@walletconnect/types";
22
import { getAccountsFromNamespaces, parseAccountId } from "@walletconnect/utils";
33
import { JRPCEngine, JRPCMiddleware, providerErrors, providerFromEngine } from "@web3auth/auth";
44

5-
import { CHAIN_NAMESPACES, log, WalletLoginError } from "@/core/base";
5+
import { CHAIN_NAMESPACES, CustomChainConfig, log, WalletLoginError } from "@/core/base";
66
import { BaseProvider, BaseProviderConfig, BaseProviderState } from "@/core/base-provider";
77
import { createEthChainSwitchMiddleware, createEthJsonRpcClient, createEthMiddleware, IEthChainSwitchHandlers } from "@/core/ethereum-provider";
88
import { createSolanaJsonRpcClient as createSolJsonRpcClient, createSolanaMiddleware } from "@/core/solana-provider";
@@ -20,20 +20,20 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
2020

2121
constructor({ config, state, connector }: { config: WalletConnectV2ProviderConfig; state?: BaseProviderState; connector?: ISignClient }) {
2222
super({
23-
config: { getCurrentChain: config.getCurrentChain, getChain: config.getChain, skipLookupNetwork: !!config.skipLookupNetwork },
24-
state: { ...(state || {}), accounts: [] },
23+
config: { chain: config.chain, chains: config.chains, skipLookupNetwork: !!config.skipLookupNetwork },
24+
state: { ...(state || {}), chainId: "loading", accounts: [] },
2525
});
2626
this.connector = connector || null;
2727
}
2828

2929
public static getProviderInstance = async (params: {
3030
connector: ISignClient;
3131
skipLookupNetwork: boolean;
32-
getCurrentChain: BaseProviderConfig["getCurrentChain"];
33-
getChain: BaseProviderConfig["getChain"];
32+
chain: CustomChainConfig;
33+
chains: CustomChainConfig[];
3434
}): Promise<WalletConnectV2Provider> => {
3535
const providerFactory = new WalletConnectV2Provider({
36-
config: { getCurrentChain: params.getCurrentChain, getChain: params.getChain, skipLookupNetwork: params.skipLookupNetwork },
36+
config: { chain: params.chain, chains: params.chains, skipLookupNetwork: params.skipLookupNetwork },
3737
});
3838
await providerFactory.setupProvider(params.connector);
3939
return providerFactory;
@@ -48,13 +48,13 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
4848

4949
public async setupProvider(connector: ISignClient): Promise<void> {
5050
this.onConnectorStateUpdate(connector);
51-
await this.setupEngine(connector, this.config.getCurrentChain().chainId);
51+
await this.setupEngine(connector, this.config.chain.chainId);
5252
}
5353

5454
public async switchChain({ chainId }: { chainId: string }): Promise<void> {
5555
if (!this.connector)
5656
throw providerErrors.custom({ message: "Connector is not initialized, pass wallet connect connector in constructor", code: 4902 });
57-
const currentChainConfig = this.config.getChain(chainId);
57+
const currentChainConfig = this.getChain(chainId);
5858

5959
const { chainId: currentChainId } = currentChainConfig;
6060
const currentNumChainId = parseInt(currentChainId, 16);
@@ -63,6 +63,8 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
6363

6464
await this.setupEngine(this.connector, chainId);
6565
this.lookupNetwork(this.connector, chainId);
66+
67+
this.update({ chainId });
6668
}
6769

6870
// no need to implement this method in wallet connect v2.
@@ -71,7 +73,7 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
7173
}
7274

7375
private async setupEngine(connector: ISignClient, chainId: string): Promise<void> {
74-
const chain = this.config.getChain(chainId);
76+
const chain = this.getChain(chainId);
7577
if (chain.chainNamespace === CHAIN_NAMESPACES.EIP155) {
7678
await this.setupEthEngine(connector, chainId);
7779
} else if (chain.chainNamespace === CHAIN_NAMESPACES.SOLANA) {
@@ -85,7 +87,7 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
8587
}
8688

8789
private async setupEthEngine(connector: ISignClient, chainId: string): Promise<void> {
88-
const chain = this.config.getChain(chainId);
90+
const chain = this.getChain(chainId);
8991
const numChainId = parseInt(chainId, 16);
9092
const providerHandlers = getEthProviderHandlers({ connector, chainId: numChainId });
9193
const jrpcRes = await getAccounts(connector);
@@ -105,7 +107,7 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
105107
}
106108

107109
private async setupSolEngine(connector: ISignClient, chainId: string): Promise<void> {
108-
const chain = this.config.getChain(chainId);
110+
const chain = this.getChain(chainId);
109111
const providerHandlers = getSolProviderHandlers({ connector, chainId });
110112
const jrpcRes = await getAccounts(connector);
111113

@@ -177,7 +179,7 @@ export class WalletConnectV2Provider extends BaseProvider<BaseProviderConfig, Wa
177179
const connectedHexChainId = `0x${connectedChainId.toString(16)}`;
178180

179181
// Check if chainId changed and trigger event
180-
const currentChain = this.config.getCurrentChain();
182+
const { currentChain } = this;
181183
if (connectedHexChainId && currentChain.chainId !== connectedHexChainId) {
182184
// Handle rpcUrl update
183185
await this.setupEngine(connector, connectedHexChainId);

0 commit comments

Comments
 (0)