Skip to content

Commit a0d263f

Browse files
committed
change format of project config's chains
1 parent f84da39 commit a0d263f

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

packages/modal/src/modalManager.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
6969
if (!this.options.uiConfig.mode) this.options.uiConfig.mode = "light";
7070

7171
// init config
72-
super.initChainsConfig(projectConfig);
7372
super.initAccountAbstractionConfig(projectConfig);
73+
super.initChainsConfig(projectConfig);
7474
super.initCachedConnectorAndChainId();
7575

7676
// init login modal
@@ -129,20 +129,23 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
129129
// TODO: we're using mock project config to test, remove this before production
130130
// projectConfig = {
131131
// ...projectConfig,
132-
// chains: {
133-
// "0x1": {
132+
// chains: [
133+
// {
134+
// chainId: "0x1",
134135
// enabled: true,
135136
// config: getChainConfig("eip155", "0x1", this.options.clientId),
136137
// },
137-
// "0x65": {
138+
// {
139+
// chainId: "0x65",
138140
// enabled: true,
139141
// config: getChainConfig("solana", "0x65", this.options.clientId),
140142
// },
141-
// "0x67": {
143+
// {
144+
// chainId: "0x67",
142145
// enabled: false,
143146
// config: getChainConfig("solana", "0x67", this.options.clientId),
144147
// },
145-
// },
148+
// ],
146149
// walletUi: {
147150
// confirmationModalEnabled: true,
148151
// portfolioWidgetEnabled: true,
@@ -178,6 +181,13 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
178181
// ],
179182
// },
180183
// },
184+
// externalWalletLogin: {
185+
// enabled: true,
186+
// config: {
187+
// phantom: { enabled: false },
188+
// trust: { enabled: false },
189+
// },
190+
// },
181191
// };
182192
} catch (e) {
183193
log.error("Failed to fetch project configurations", e);

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type BUTTON_POSITION_TYPE } from "@toruslabs/base-controllers";
22
import { type SmartAccountType } from "@toruslabs/ethereum-controllers";
3-
import { type LoginConfig, type WhiteLabelData } from "@web3auth/auth";
3+
import { type WhiteLabelData } from "@web3auth/auth";
44

55
import { type ChainNamespaceType, type CustomChainConfig } from "./chain/IChainInterface";
66

@@ -9,21 +9,17 @@ export interface WhitelistResponse {
99
signed_urls: Record<string, string>;
1010
}
1111

12-
export type ChainConfigItem = {
12+
export type ChainsConfig = {
13+
chainId: string;
1314
enabled: boolean;
1415
config: CustomChainConfig;
15-
};
16+
}[];
1617

1718
export interface ExternalWalletsConfig {
1819
enabled: boolean;
1920
config: Record<string, { enabled: boolean }>;
2021
}
2122

22-
export type LoginConfigItem = {
23-
enabled: boolean;
24-
config?: LoginConfig[keyof LoginConfig];
25-
};
26-
2723
export const SMART_ACCOUNT_WALLET_SCOPE = {
2824
EMBEDDED_ONLY: "embeddedOnly",
2925
ALL: "all",
@@ -76,11 +72,11 @@ export interface ProjectConfig {
7672
// Project settings
7773
userDataIncludedInToken?: boolean; // TODO: implement this
7874
sessionTime?: number;
79-
keyExportEnabled?: boolean; // TODO: implement this in WS
75+
keyExportEnabled?: boolean;
8076
whitelist?: WhitelistResponse; // remain unchanged
8177
whitelabel?: WhiteLabelData; // remain unchanged
8278
// Chains
83-
chains?: Record<string, ChainConfigItem>;
79+
chains?: ChainsConfig;
8480
// Login config
8581
externalWalletLogin?: ExternalWalletsConfig;
8682
// Smart accounts

packages/no-modal/src/noModal.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
107107
}
108108

109109
// init config
110-
this.initChainsConfig(projectConfig);
111110
this.initAccountAbstractionConfig(projectConfig);
111+
this.initChainsConfig(projectConfig);
112112
this.initCachedConnectorAndChainId();
113113

114114
// setup common JRPC provider
@@ -226,25 +226,41 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
226226
}
227227

228228
protected initChainsConfig(projectConfig: ProjectConfig) {
229-
const projectConfigChains = projectConfig.chains;
230229
// merge chains from project config with core options, core options chains will take precedence over project config chains
231-
const mergedChains = { ...projectConfigChains };
232-
(this.coreOptions.chains || []).forEach((chain) => {
233-
const projectConfigChain = projectConfigChains?.[chain.chainId];
234-
if (!projectConfigChain) mergedChains[chain.chainId] = { enabled: true, config: chain };
235-
else {
236-
mergedChains[chain.chainId] = { ...projectConfigChain, ...{ ...chain, enabled: true } };
237-
}
238-
});
239-
this.coreOptions.chains = Object.values(mergedChains)
240-
.filter((chain) => chain.enabled)
241-
.map((chain) => chain.config);
230+
const chainMap = new Map<string, CustomChainConfig>();
231+
const enabledProjectConfigChains = projectConfig.chains?.filter((chain) => chain.enabled).map((chain) => chain.config) || [];
232+
const allChains = [...enabledProjectConfigChains, ...(this.coreOptions.chains || [])];
233+
for (const chain of allChains) {
234+
const existingChain = chainMap.get(chain.chainId);
235+
if (!existingChain) chainMap.set(chain.chainId, chain);
236+
else chainMap.set(chain.chainId, { ...existingChain, ...chain });
237+
}
238+
this.coreOptions.chains = Array.from(chainMap.values());
242239

243240
// validate chains and namespaces
244-
if (!this.coreOptions.chains?.length) throw WalletInitializationError.invalidParams("Please provide chains");
241+
if (this.coreOptions.chains.length === 0) {
242+
log.error("Please provide chains");
243+
throw WalletInitializationError.invalidParams("Please provide chains");
244+
}
245245
for (const chain of this.coreOptions.chains) {
246-
if (!chain.chainNamespace || !Object.values(CHAIN_NAMESPACES).includes(chain.chainNamespace))
247-
throw WalletInitializationError.invalidParams("Please provide a valid chainNamespace in chains");
246+
if (!chain.chainNamespace || !Object.values(CHAIN_NAMESPACES).includes(chain.chainNamespace)) {
247+
log.error(`Please provide a valid chainNamespace in chains for chain ${chain.chainId}`);
248+
throw WalletInitializationError.invalidParams(`Please provide a valid chainNamespace in chains for chain ${chain.chainId}`);
249+
}
250+
}
251+
252+
// if AA is enabled, filter out chains that are not AA-supported
253+
if (this.coreOptions.accountAbstractionConfig) {
254+
const aaSupportedChainIds = new Set(
255+
this.coreOptions.accountAbstractionConfig?.chains
256+
?.filter((chain) => chain.chainId && chain.bundlerConfig?.url)
257+
.map((chain) => chain.chainId) || []
258+
);
259+
this.coreOptions.chains = this.coreOptions.chains.filter((chain) => aaSupportedChainIds.has(chain.chainId));
260+
if (this.coreOptions.chains.length === 0) {
261+
log.error("Account Abstraction is enabled but no supported chains found");
262+
throw WalletInitializationError.invalidParams("Account Abstraction is enabled but no supported chains found");
263+
}
248264
}
249265
}
250266

0 commit comments

Comments
 (0)