@web3auth/modal 8.0.1 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/config.ts DELETED
@@ -1,93 +0,0 @@
1
- import { CHAIN_NAMESPACES, EVM_ADAPTERS, SOLANA_ADAPTERS } from "@web3auth/base";
2
-
3
- import { AdaptersModalConfig } from "./interface";
4
-
5
- export const defaultSolanaDappModalConfig: AdaptersModalConfig = {
6
- chainNamespace: CHAIN_NAMESPACES.SOLANA,
7
- adapters: {
8
- [SOLANA_ADAPTERS.TORUS_SOLANA]: {
9
- label: "Torus Wallet",
10
- showOnModal: true,
11
- showOnMobile: true,
12
- showOnDesktop: true,
13
- },
14
- [SOLANA_ADAPTERS.OPENLOGIN]: {
15
- label: "OpenLogin",
16
- showOnModal: true,
17
- showOnMobile: true,
18
- showOnDesktop: true,
19
- },
20
- [SOLANA_ADAPTERS.PHANTOM]: {
21
- label: "Phantom",
22
- showOnModal: true,
23
- showOnMobile: true,
24
- showOnDesktop: true,
25
- },
26
- },
27
- };
28
-
29
- export const defaultEvmDappModalConfig: AdaptersModalConfig = {
30
- chainNamespace: CHAIN_NAMESPACES.EIP155,
31
- adapters: {
32
- [EVM_ADAPTERS.TORUS_EVM]: {
33
- label: "Torus Wallet",
34
- showOnModal: true,
35
- showOnMobile: true,
36
- showOnDesktop: true,
37
- },
38
- [EVM_ADAPTERS.METAMASK]: {
39
- label: "MetaMask",
40
- showOnModal: true,
41
- showOnMobile: true,
42
- showOnDesktop: true,
43
- },
44
- [EVM_ADAPTERS.OPENLOGIN]: {
45
- label: "OpenLogin",
46
- showOnModal: true,
47
- showOnMobile: true,
48
- showOnDesktop: true,
49
- },
50
- [EVM_ADAPTERS.WALLET_CONNECT_V2]: {
51
- label: "Wallet Connect",
52
- showOnModal: true,
53
- showOnMobile: true,
54
- showOnDesktop: true,
55
- },
56
- },
57
- };
58
-
59
- export const defaultSolanaWalletModalConfig: AdaptersModalConfig = {
60
- chainNamespace: CHAIN_NAMESPACES.SOLANA,
61
- adapters: {
62
- [SOLANA_ADAPTERS.OPENLOGIN]: {
63
- label: "OpenLogin",
64
- showOnModal: true,
65
- showOnMobile: true,
66
- showOnDesktop: true,
67
- },
68
- },
69
- };
70
-
71
- export const defaultEvmWalletModalConfig: AdaptersModalConfig = {
72
- chainNamespace: CHAIN_NAMESPACES.EIP155,
73
- adapters: {
74
- [EVM_ADAPTERS.OPENLOGIN]: {
75
- label: "OpenLogin",
76
- showOnModal: true,
77
- showOnMobile: true,
78
- showOnDesktop: true,
79
- },
80
- },
81
- };
82
-
83
- export const defaultOtherModalConfig: AdaptersModalConfig = {
84
- chainNamespace: CHAIN_NAMESPACES.OTHER,
85
- adapters: {
86
- [EVM_ADAPTERS.OPENLOGIN]: {
87
- label: "OpenLogin",
88
- showOnModal: true,
89
- showOnMobile: true,
90
- showOnDesktop: true,
91
- },
92
- },
93
- };
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./config";
2
- export * from "./interface";
3
- export * from "./modalManager";
package/src/interface.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { BaseAdapterConfig, ChainNamespaceType, IProvider, IWeb3Auth, LoginMethodConfig, WALLET_ADAPTER_TYPE } from "@web3auth/base";
2
- export interface ModalConfig extends BaseAdapterConfig {
3
- loginMethods?: LoginMethodConfig;
4
- }
5
-
6
- export interface AdaptersModalConfig {
7
- chainNamespace: ChainNamespaceType;
8
- adapters?: Record<WALLET_ADAPTER_TYPE, ModalConfig>;
9
- }
10
-
11
- export interface IWeb3AuthModal extends IWeb3Auth {
12
- initModal(params?: { modalConfig?: Record<WALLET_ADAPTER_TYPE, ModalConfig> }): Promise<void>;
13
- connect(): Promise<IProvider | null>;
14
- }
@@ -1,354 +0,0 @@
1
- import {
2
- ADAPTER_CATEGORY,
3
- ADAPTER_EVENTS,
4
- ADAPTER_STATUS,
5
- BaseAdapterConfig,
6
- CustomChainConfig,
7
- getChainConfig,
8
- IBaseProvider,
9
- IProvider,
10
- log,
11
- LoginMethodConfig,
12
- WALLET_ADAPTER_TYPE,
13
- WALLET_ADAPTERS,
14
- WalletInitializationError,
15
- Web3AuthNoModalOptions,
16
- } from "@web3auth/base";
17
- import { CommonJRPCProvider } from "@web3auth/base-provider";
18
- import { Web3AuthNoModal } from "@web3auth/no-modal";
19
- import { getOpenloginDefaultOptions, OpenloginAdapter } from "@web3auth/openlogin-adapter";
20
- import { getAdapterSocialLogins, getUserLanguage, LOGIN_MODAL_EVENTS, LoginModal, OPENLOGIN_PROVIDERS, UIConfig } from "@web3auth/ui";
21
- import type { WalletConnectV2Adapter } from "@web3auth/wallet-connect-v2-adapter";
22
-
23
- import { defaultOtherModalConfig } from "./config";
24
- import { AdaptersModalConfig, IWeb3AuthModal, ModalConfig } from "./interface";
25
-
26
- export interface Web3AuthOptions extends Web3AuthNoModalOptions {
27
- /**
28
- * Config for configuring modal ui display properties
29
- */
30
- uiConfig?: Omit<UIConfig, "adapterListener">;
31
-
32
- /**
33
- * Private key provider for your chain namespace
34
- */
35
- privateKeyProvider: IBaseProvider<string>;
36
- }
37
-
38
- export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
39
- public loginModal: LoginModal;
40
-
41
- readonly options: Web3AuthOptions;
42
-
43
- private modalConfig: AdaptersModalConfig = defaultOtherModalConfig;
44
-
45
- constructor(options: Web3AuthOptions) {
46
- super(options);
47
- this.options = { ...options };
48
-
49
- if (!this.options.uiConfig) this.options.uiConfig = {};
50
- if (!this.options.uiConfig.defaultLanguage) this.options.uiConfig.defaultLanguage = getUserLanguage(this.options.uiConfig.defaultLanguage);
51
- if (!this.options.uiConfig.mode) this.options.uiConfig.mode = "auto";
52
- if (!this.coreOptions.privateKeyProvider) throw WalletInitializationError.invalidParams("privateKeyProvider is required");
53
-
54
- this.loginModal = new LoginModal({
55
- ...this.options.uiConfig,
56
- adapterListener: this,
57
- });
58
- this.subscribeToLoginModalEvents();
59
- }
60
-
61
- public setModalConfig(modalConfig: AdaptersModalConfig): void {
62
- super.checkInitRequirements();
63
- this.modalConfig = modalConfig;
64
- }
65
-
66
- public async initModal(params?: { modalConfig?: Record<WALLET_ADAPTER_TYPE, ModalConfig> }): Promise<void> {
67
- super.checkInitRequirements();
68
- await this.loginModal.initModal();
69
- const providedChainConfig = this.options.chainConfig;
70
- // TODO: get stuff from dashboard here
71
- // merge default adapters with the custom configured adapters.
72
- const allAdapters = [...new Set([...Object.keys(this.modalConfig.adapters || {}), ...Object.keys(this.walletAdapters)])];
73
-
74
- const adapterConfigurationPromises = allAdapters.map(async (adapterName) => {
75
- // start with the default config of adapter.
76
- let adapterConfig = this.modalConfig.adapters?.[adapterName] || {
77
- label: adapterName,
78
- showOnModal: true,
79
- showOnMobile: true,
80
- showOnDesktop: true,
81
- };
82
-
83
- // override the default config of adapter if some config is being provided by the user.
84
- if (params?.modalConfig?.[adapterName]) {
85
- adapterConfig = { ...adapterConfig, ...params.modalConfig[adapterName] };
86
- }
87
- (this.modalConfig.adapters as Record<WALLET_ADAPTER_TYPE, ModalConfig>)[adapterName] = adapterConfig as ModalConfig;
88
-
89
- // check if adapter is configured/added by user and exist in walletAdapters map.
90
- const adapter = this.walletAdapters[adapterName];
91
- log.debug("adapter config", adapterName, this.modalConfig.adapters?.[adapterName].showOnModal, adapter);
92
-
93
- // if adapter is not custom configured then check if it is available in default adapters.
94
- // and if adapter is not hidden by user
95
- if (!adapter && this.modalConfig.adapters?.[adapterName].showOnModal) {
96
- // Adapters to be shown on modal should be pre-configured.
97
- if (adapterName === WALLET_ADAPTERS.OPENLOGIN) {
98
- const defaultOptions = getOpenloginDefaultOptions();
99
- const { clientId, useCoreKitKey, chainConfig, web3AuthNetwork, sessionTime, privateKeyProvider } = this.coreOptions;
100
- const finalChainConfig = {
101
- ...getChainConfig(providedChainConfig.chainNamespace, this.coreOptions.chainConfig?.chainId),
102
- ...chainConfig,
103
- } as CustomChainConfig;
104
- if (!privateKeyProvider) {
105
- throw WalletInitializationError.invalidParams("privateKeyProvider is required");
106
- }
107
- const finalOpenloginAdapterSettings = {
108
- ...defaultOptions.adapterSettings,
109
- clientId,
110
- network: web3AuthNetwork,
111
- whiteLabel: this.options.uiConfig,
112
- };
113
- if (this.options.uiConfig.uxMode) {
114
- finalOpenloginAdapterSettings.uxMode = this.options.uiConfig.uxMode;
115
- }
116
- const openloginAdapter = new OpenloginAdapter({
117
- ...defaultOptions,
118
- clientId,
119
- useCoreKitKey,
120
- chainConfig: { ...finalChainConfig },
121
- adapterSettings: finalOpenloginAdapterSettings,
122
- sessionTime,
123
- web3AuthNetwork,
124
- privateKeyProvider,
125
- });
126
- this.walletAdapters[adapterName] = openloginAdapter;
127
- return adapterName;
128
- }
129
- throw WalletInitializationError.invalidParams(`Adapter ${adapterName} is not configured`);
130
- } else if (adapter?.type === ADAPTER_CATEGORY.IN_APP || adapter?.type === ADAPTER_CATEGORY.EXTERNAL || adapterName === this.cachedAdapter) {
131
- if (!this.modalConfig.adapters?.[adapterName].showOnModal) return;
132
- // add client id to adapter, same web3auth client id can be used in adapter.
133
- // this id is being overridden if user is also passing client id in adapter's constructor.
134
- this.walletAdapters[adapterName].setAdapterSettings({
135
- clientId: this.options.clientId,
136
- sessionTime: this.options.sessionTime,
137
- web3AuthNetwork: this.options.web3AuthNetwork,
138
- useCoreKitKey: this.coreOptions.useCoreKitKey,
139
- });
140
-
141
- // if adapter doesn't have any chainConfig then we will set the chainConfig based of passed chainNamespace
142
- // and chainNamespace.
143
- if (!adapter.chainConfigProxy) {
144
- const chainConfig = {
145
- ...getChainConfig(providedChainConfig.chainNamespace, this.coreOptions.chainConfig?.chainId),
146
- ...this.coreOptions.chainConfig,
147
- } as CustomChainConfig;
148
- this.walletAdapters[adapterName].setAdapterSettings({ chainConfig });
149
- }
150
-
151
- if (adapterName === WALLET_ADAPTERS.OPENLOGIN) {
152
- const openloginAdapter = this.walletAdapters[adapterName] as OpenloginAdapter;
153
- if (this.coreOptions.privateKeyProvider) {
154
- if (openloginAdapter.currentChainNamespace !== this.coreOptions.privateKeyProvider.currentChainConfig.chainNamespace) {
155
- throw WalletInitializationError.incompatibleChainNameSpace(
156
- "private key provider is not compatible with provided chainNamespace for openlogin adapter"
157
- );
158
- }
159
- openloginAdapter.setAdapterSettings({ privateKeyProvider: this.coreOptions.privateKeyProvider });
160
- }
161
- if (this.options.uiConfig?.uxMode) {
162
- openloginAdapter.setAdapterSettings({ uxMode: this.options.uiConfig.uxMode });
163
- }
164
- openloginAdapter.setAdapterSettings({ whiteLabel: this.options.uiConfig });
165
- if (!openloginAdapter.privateKeyProvider) {
166
- throw WalletInitializationError.invalidParams("privateKeyProvider is required for openlogin adapter");
167
- }
168
- } else if (adapterName === WALLET_ADAPTERS.WALLET_CONNECT_V2) {
169
- const walletConnectAdapter = this.walletAdapters[adapterName] as WalletConnectV2Adapter;
170
- walletConnectAdapter.setAdapterSettings({
171
- adapterSettings: {
172
- walletConnectInitOptions: {
173
- // Using a default wallet connect project id for web3auth modal integration
174
- projectId: "d3c63f19f9582f8ba48e982057eb096b", // TODO: get from dashboard
175
- },
176
- },
177
- });
178
- }
179
-
180
- return adapterName;
181
- }
182
- });
183
-
184
- const adapterNames = await Promise.all(adapterConfigurationPromises);
185
- const hasInAppWallets = Object.values(this.walletAdapters).some((adapter) => {
186
- if (adapter.type !== ADAPTER_CATEGORY.IN_APP) return false;
187
- if (this.modalConfig.adapters?.[adapter.name]?.showOnModal !== true) return false;
188
- if (!this.modalConfig.adapters?.[adapter.name]?.loginMethods) return true;
189
- const mergedLoginMethods = getAdapterSocialLogins(
190
- adapter.name,
191
- (this.modalConfig.adapters as Record<WALLET_ADAPTER_TYPE, ModalConfig>)[adapter.name]?.loginMethods
192
- );
193
- if (Object.values(mergedLoginMethods).some((method: LoginMethodConfig[keyof LoginMethodConfig]) => method.showOnModal)) return true;
194
- return false;
195
- });
196
- log.debug(hasInAppWallets, this.walletAdapters, adapterNames, "hasInAppWallets");
197
-
198
- // Now, initialize the adapters.
199
- const initPromises = adapterNames.map(async (adapterName) => {
200
- if (!adapterName) return;
201
- try {
202
- const adapter = this.walletAdapters[adapterName];
203
- // only initialize a external adapter here if it is a cached adapter.
204
- if (this.cachedAdapter !== adapterName && adapter.type === ADAPTER_CATEGORY.EXTERNAL) {
205
- return;
206
- }
207
- // in-app wallets or cached wallet (being connected or already connected) are initialized first.
208
- // if adapter is configured then only initialize in app or cached adapter.
209
- // external wallets are initialized on INIT_EXTERNAL_WALLET event.
210
- this.subscribeToAdapterEvents(adapter);
211
- if (adapter.status === ADAPTER_STATUS.NOT_READY) await adapter.init({ autoConnect: this.cachedAdapter === adapterName });
212
- // note: not adding cachedWallet to modal if it is external wallet.
213
- // adding it later if no in-app wallets are available.
214
- if (adapter.type === ADAPTER_CATEGORY.IN_APP) {
215
- this.initializeInAppWallet(adapterName);
216
- }
217
- } catch (error) {
218
- log.error(error, "error while initializing adapter");
219
- }
220
- });
221
-
222
- this.commonJRPCProvider = await CommonJRPCProvider.getProviderInstance({ chainConfig: this.coreOptions.chainConfig as CustomChainConfig });
223
- this.status = ADAPTER_STATUS.READY;
224
- await Promise.all(initPromises);
225
-
226
- const hasExternalWallets = allAdapters.some((adapterName) => {
227
- return this.walletAdapters[adapterName]?.type === ADAPTER_CATEGORY.EXTERNAL && this.modalConfig.adapters?.[adapterName].showOnModal;
228
- });
229
-
230
- if (hasExternalWallets) {
231
- this.loginModal.initExternalWalletContainer();
232
- }
233
-
234
- // variable to check if we have any in app wallets
235
- // currently all default in app and external wallets can be hidden or shown based on config.
236
- if (!hasInAppWallets && hasExternalWallets) {
237
- // if no in app wallet is available then initialize external wallets in modal
238
- await this.initExternalWalletAdapters(false, { showExternalWalletsOnly: true });
239
- }
240
- }
241
-
242
- public async connect(): Promise<IProvider | null> {
243
- // if (!this.loginModal.initialized) throw new Error("Login modal is not initialized");
244
- // if already connected return provider
245
- if (this.connectedAdapterName && this.status === ADAPTER_STATUS.CONNECTED && this.provider) return this.provider;
246
- this.loginModal.open();
247
- return new Promise((resolve, reject) => {
248
- this.once(ADAPTER_EVENTS.CONNECTED, () => {
249
- return resolve(this.provider);
250
- });
251
- this.once(ADAPTER_EVENTS.ERRORED, (err: unknown) => {
252
- return reject(err);
253
- });
254
- this.once(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, (visibility: boolean) => {
255
- // modal is closed but user is not connected to any wallet.
256
- if (!visibility && this.status !== ADAPTER_STATUS.CONNECTED) {
257
- return reject(new Error("User closed the modal"));
258
- }
259
- });
260
- });
261
- }
262
-
263
- private async initExternalWalletAdapters(externalWalletsInitialized: boolean, options?: { showExternalWalletsOnly: boolean }): Promise<void> {
264
- if (externalWalletsInitialized) return;
265
- const adaptersConfig: Record<string, BaseAdapterConfig> = {};
266
- Object.keys(this.walletAdapters).forEach(async (adapterName) => {
267
- const adapter = this.walletAdapters[adapterName];
268
- if (adapter?.type === ADAPTER_CATEGORY.EXTERNAL) {
269
- log.debug("init external wallet", this.cachedAdapter, adapterName);
270
- this.subscribeToAdapterEvents(adapter);
271
- // we are not initializing cached adapter here as it is already being initialized in initModal before.
272
- if (this.cachedAdapter === adapterName) {
273
- return;
274
- }
275
- if (adapter.status === ADAPTER_STATUS.NOT_READY) {
276
- await adapter
277
- .init({ autoConnect: this.cachedAdapter === adapterName })
278
- .then(() => {
279
- adaptersConfig[adapterName] = (this.modalConfig.adapters as Record<WALLET_ADAPTER_TYPE, ModalConfig>)[adapterName];
280
- this.loginModal.addWalletLogins(adaptersConfig, { showExternalWalletsOnly: !!options?.showExternalWalletsOnly });
281
- return undefined;
282
- })
283
- .catch((error) => log.error(error, "error while initializing adapter"));
284
- } else if (adapter.status === ADAPTER_STATUS.READY) {
285
- adaptersConfig[adapterName] = (this.modalConfig.adapters as Record<WALLET_ADAPTER_TYPE, ModalConfig>)[adapterName];
286
- this.loginModal.addWalletLogins(adaptersConfig, { showExternalWalletsOnly: !!options?.showExternalWalletsOnly });
287
- }
288
- }
289
- });
290
- }
291
-
292
- private initializeInAppWallet(adapterName: string): void {
293
- log.info("adapterInitResults", adapterName);
294
- if (this.walletAdapters[adapterName].type === ADAPTER_CATEGORY.IN_APP) {
295
- this.loginModal.addSocialLogins(
296
- adapterName,
297
- getAdapterSocialLogins(adapterName, (this.modalConfig.adapters as Record<WALLET_ADAPTER_TYPE, ModalConfig>)[adapterName]?.loginMethods),
298
- this.options.uiConfig?.loginMethodsOrder || OPENLOGIN_PROVIDERS,
299
- {
300
- ...this.options.uiConfig,
301
- loginGridCol: this.options.uiConfig?.loginGridCol || 3,
302
- primaryButton: this.options.uiConfig?.primaryButton || "socialLogin",
303
- }
304
- );
305
- }
306
- }
307
-
308
- private subscribeToLoginModalEvents(): void {
309
- this.loginModal.on(LOGIN_MODAL_EVENTS.LOGIN, async (params: { adapter: WALLET_ADAPTER_TYPE; loginParams: unknown }) => {
310
- try {
311
- await this.connectTo<unknown>(params.adapter, params.loginParams);
312
- } catch (error) {
313
- log.error(`Error while connecting to adapter: ${params.adapter}`, error);
314
- }
315
- });
316
- this.loginModal.on(LOGIN_MODAL_EVENTS.INIT_EXTERNAL_WALLETS, async (params: { externalWalletsInitialized: boolean }) => {
317
- await this.initExternalWalletAdapters(params.externalWalletsInitialized);
318
- });
319
- this.loginModal.on(LOGIN_MODAL_EVENTS.DISCONNECT, async () => {
320
- try {
321
- await this.logout();
322
- } catch (error) {
323
- log.error(`Error while disconnecting`, error);
324
- }
325
- });
326
- this.loginModal.on(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, async (visibility: boolean) => {
327
- log.debug("is login modal visible", visibility);
328
- this.emit(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, visibility);
329
- const adapter = this.walletAdapters[WALLET_ADAPTERS.WALLET_CONNECT_V2];
330
- if (adapter) {
331
- const walletConnectStatus = adapter?.status;
332
- log.debug("trying refreshing wc session", visibility, walletConnectStatus);
333
- if (visibility && (walletConnectStatus === ADAPTER_STATUS.READY || walletConnectStatus === ADAPTER_STATUS.CONNECTING)) {
334
- log.debug("refreshing wc session");
335
-
336
- // refreshing session for wallet connect whenever modal is opened.
337
- try {
338
- adapter.connect();
339
- } catch (error) {
340
- log.error(`Error while disconnecting to wallet connect in core`, error);
341
- }
342
- }
343
- if (
344
- !visibility &&
345
- this.status === ADAPTER_STATUS.CONNECTED &&
346
- (walletConnectStatus === ADAPTER_STATUS.READY || walletConnectStatus === ADAPTER_STATUS.CONNECTING)
347
- ) {
348
- log.debug("this stops wc adapter from trying to reconnect once proposal expires");
349
- adapter.status = ADAPTER_STATUS.READY;
350
- }
351
- }
352
- });
353
- }
354
- }