@web3auth/modal 8.12.7 → 9.0.0-alpha.1
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/LICENSE +2 -2
- package/dist/lib.cjs/config.js +85 -0
- package/dist/lib.cjs/index.js +14 -0
- package/dist/lib.cjs/modalManager.js +426 -0
- package/dist/lib.esm/config.js +78 -0
- package/dist/lib.esm/index.js +2 -0
- package/dist/lib.esm/modalManager.js +424 -0
- package/dist/modal.cjs.js +76 -69
- package/dist/modal.esm.js +66 -58
- package/dist/modal.umd.min.js +1 -1
- package/dist/modal.umd.min.js.LICENSE.txt +0 -2
- package/dist/types/config.d.ts +1 -0
- package/dist/types/interface.d.ts +1 -1
- package/package.json +10 -11
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import { LOGIN_PROVIDER, getAuthDefaultOptions, AuthAdapter } from '@web3auth/auth-adapter';
|
|
4
|
+
import { cloneDeep, WalletInitializationError, fetchProjectConfig, log, fetchWalletRegistry, WALLET_ADAPTERS, getChainConfig, ADAPTER_CATEGORY, ADAPTER_STATUS, ADAPTER_EVENTS } from '@web3auth/base';
|
|
5
|
+
import { CommonJRPCProvider } from '@web3auth/base-provider';
|
|
6
|
+
import { Web3AuthNoModal } from '@web3auth/no-modal';
|
|
7
|
+
import { getUserLanguage, LoginModal, capitalizeFirstLetter, getAdapterSocialLogins, LOGIN_MODAL_EVENTS, AUTH_PROVIDERS } from '@web3auth/ui';
|
|
8
|
+
import deepmerge from 'deepmerge';
|
|
9
|
+
import { defaultOtherModalConfig, walletRegistryUrl } from './config.js';
|
|
10
|
+
|
|
11
|
+
class Web3Auth extends Web3AuthNoModal {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super(options);
|
|
14
|
+
_defineProperty(this, "loginModal", void 0);
|
|
15
|
+
_defineProperty(this, "options", void 0);
|
|
16
|
+
_defineProperty(this, "modalConfig", cloneDeep(defaultOtherModalConfig));
|
|
17
|
+
this.options = _objectSpread({}, options);
|
|
18
|
+
if (!this.options.uiConfig) this.options.uiConfig = {};
|
|
19
|
+
if (!this.coreOptions.privateKeyProvider) throw WalletInitializationError.invalidParams("privateKeyProvider is required");
|
|
20
|
+
}
|
|
21
|
+
setModalConfig(modalConfig) {
|
|
22
|
+
super.checkInitRequirements();
|
|
23
|
+
this.modalConfig = modalConfig;
|
|
24
|
+
}
|
|
25
|
+
async initModal(params) {
|
|
26
|
+
super.checkInitRequirements();
|
|
27
|
+
let projectConfig;
|
|
28
|
+
try {
|
|
29
|
+
projectConfig = await fetchProjectConfig(this.options.clientId, this.options.web3AuthNetwork);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
log.error("Failed to fetch project configurations", e);
|
|
32
|
+
throw WalletInitializationError.notReady("failed to fetch project configurations", e);
|
|
33
|
+
}
|
|
34
|
+
const {
|
|
35
|
+
whitelabel
|
|
36
|
+
} = projectConfig;
|
|
37
|
+
this.options.uiConfig = deepmerge(cloneDeep(whitelabel), this.options.uiConfig);
|
|
38
|
+
if (!this.options.uiConfig.defaultLanguage) this.options.uiConfig.defaultLanguage = getUserLanguage(this.options.uiConfig.defaultLanguage);
|
|
39
|
+
if (!this.options.uiConfig.mode) this.options.uiConfig.mode = "light";
|
|
40
|
+
let walletRegistry = {};
|
|
41
|
+
try {
|
|
42
|
+
walletRegistry = await fetchWalletRegistry(walletRegistryUrl);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
log.error("Failed to fetch wallet registry", e);
|
|
45
|
+
}
|
|
46
|
+
this.loginModal = new LoginModal(_objectSpread(_objectSpread({}, this.options.uiConfig), {}, {
|
|
47
|
+
adapterListener: this,
|
|
48
|
+
chainNamespace: this.options.chainConfig.chainNamespace,
|
|
49
|
+
walletRegistry
|
|
50
|
+
}));
|
|
51
|
+
this.subscribeToLoginModalEvents();
|
|
52
|
+
const {
|
|
53
|
+
sms_otp_enabled: smsOtpEnabled,
|
|
54
|
+
whitelist,
|
|
55
|
+
key_export_enabled: keyExportEnabled
|
|
56
|
+
} = projectConfig;
|
|
57
|
+
if (smsOtpEnabled !== undefined) {
|
|
58
|
+
var _params;
|
|
59
|
+
const adapterConfig = {
|
|
60
|
+
[WALLET_ADAPTERS.AUTH]: {
|
|
61
|
+
label: WALLET_ADAPTERS.AUTH,
|
|
62
|
+
loginMethods: {
|
|
63
|
+
[LOGIN_PROVIDER.SMS_PASSWORDLESS]: {
|
|
64
|
+
name: LOGIN_PROVIDER.SMS_PASSWORDLESS,
|
|
65
|
+
showOnModal: smsOtpEnabled,
|
|
66
|
+
showOnDesktop: smsOtpEnabled,
|
|
67
|
+
showOnMobile: smsOtpEnabled
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
if (!((_params = params) !== null && _params !== void 0 && _params.modalConfig)) params = {
|
|
73
|
+
modalConfig: {}
|
|
74
|
+
};
|
|
75
|
+
params.modalConfig = deepmerge(cloneDeep(params.modalConfig), adapterConfig);
|
|
76
|
+
}
|
|
77
|
+
await this.loginModal.initModal();
|
|
78
|
+
const providedChainConfig = this.options.chainConfig;
|
|
79
|
+
// merge default adapters with the custom configured adapters.
|
|
80
|
+
const allAdapters = [...new Set([...Object.keys(this.modalConfig.adapters || {}), ...Object.keys(this.walletAdapters)])];
|
|
81
|
+
const adapterConfigurationPromises = allAdapters.map(async adapterName => {
|
|
82
|
+
var _this$modalConfig$ada, _params2, _this$modalConfig$ada2, _this$modalConfig$ada3;
|
|
83
|
+
// start with the default config of adapter.
|
|
84
|
+
let adapterConfig = ((_this$modalConfig$ada = this.modalConfig.adapters) === null || _this$modalConfig$ada === void 0 ? void 0 : _this$modalConfig$ada[adapterName]) || {
|
|
85
|
+
label: adapterName.split("-").map(capitalizeFirstLetter).join(" "),
|
|
86
|
+
showOnModal: true,
|
|
87
|
+
showOnMobile: true,
|
|
88
|
+
showOnDesktop: true
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// override the default config of adapter if some config is being provided by the user.
|
|
92
|
+
if ((_params2 = params) !== null && _params2 !== void 0 && (_params2 = _params2.modalConfig) !== null && _params2 !== void 0 && _params2[adapterName]) {
|
|
93
|
+
adapterConfig = _objectSpread(_objectSpread({}, adapterConfig), params.modalConfig[adapterName]);
|
|
94
|
+
}
|
|
95
|
+
this.modalConfig.adapters[adapterName] = adapterConfig;
|
|
96
|
+
|
|
97
|
+
// check if adapter is configured/added by user and exist in walletAdapters map.
|
|
98
|
+
const adapter = this.walletAdapters[adapterName];
|
|
99
|
+
log.debug("adapter config", adapterName, (_this$modalConfig$ada2 = this.modalConfig.adapters) === null || _this$modalConfig$ada2 === void 0 ? void 0 : _this$modalConfig$ada2[adapterName].showOnModal, adapter);
|
|
100
|
+
|
|
101
|
+
// if adapter is not custom configured then check if it is available in default adapters.
|
|
102
|
+
// and if adapter is not hidden by user
|
|
103
|
+
if (!adapter && (_this$modalConfig$ada3 = this.modalConfig.adapters) !== null && _this$modalConfig$ada3 !== void 0 && _this$modalConfig$ada3[adapterName].showOnModal) {
|
|
104
|
+
// Adapters to be shown on modal should be pre-configured.
|
|
105
|
+
if (adapterName === WALLET_ADAPTERS.AUTH) {
|
|
106
|
+
var _this$coreOptions$cha;
|
|
107
|
+
const defaultOptions = getAuthDefaultOptions();
|
|
108
|
+
const {
|
|
109
|
+
clientId,
|
|
110
|
+
useCoreKitKey,
|
|
111
|
+
chainConfig,
|
|
112
|
+
web3AuthNetwork,
|
|
113
|
+
sessionTime,
|
|
114
|
+
privateKeyProvider
|
|
115
|
+
} = this.coreOptions;
|
|
116
|
+
const finalChainConfig = _objectSpread(_objectSpread({}, getChainConfig(providedChainConfig.chainNamespace, (_this$coreOptions$cha = this.coreOptions.chainConfig) === null || _this$coreOptions$cha === void 0 ? void 0 : _this$coreOptions$cha.chainId)), chainConfig);
|
|
117
|
+
if (!privateKeyProvider) {
|
|
118
|
+
throw WalletInitializationError.invalidParams("privateKeyProvider is required");
|
|
119
|
+
}
|
|
120
|
+
const finalAuthAdapterSettings = _objectSpread(_objectSpread({}, defaultOptions.adapterSettings), {}, {
|
|
121
|
+
clientId,
|
|
122
|
+
network: web3AuthNetwork,
|
|
123
|
+
whiteLabel: this.options.uiConfig
|
|
124
|
+
});
|
|
125
|
+
if (smsOtpEnabled !== undefined) {
|
|
126
|
+
finalAuthAdapterSettings.loginConfig = {
|
|
127
|
+
[LOGIN_PROVIDER.SMS_PASSWORDLESS]: {
|
|
128
|
+
showOnModal: smsOtpEnabled,
|
|
129
|
+
showOnDesktop: smsOtpEnabled,
|
|
130
|
+
showOnMobile: smsOtpEnabled,
|
|
131
|
+
showOnSocialBackupFactor: smsOtpEnabled
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (whitelist) {
|
|
136
|
+
finalAuthAdapterSettings.originData = whitelist.signed_urls;
|
|
137
|
+
}
|
|
138
|
+
if (this.options.uiConfig.uxMode) {
|
|
139
|
+
finalAuthAdapterSettings.uxMode = this.options.uiConfig.uxMode;
|
|
140
|
+
}
|
|
141
|
+
const authAdapter = new AuthAdapter(_objectSpread(_objectSpread({}, defaultOptions), {}, {
|
|
142
|
+
clientId,
|
|
143
|
+
useCoreKitKey,
|
|
144
|
+
chainConfig: _objectSpread({}, finalChainConfig),
|
|
145
|
+
adapterSettings: finalAuthAdapterSettings,
|
|
146
|
+
sessionTime,
|
|
147
|
+
web3AuthNetwork,
|
|
148
|
+
privateKeyProvider
|
|
149
|
+
}));
|
|
150
|
+
this.walletAdapters[adapterName] = authAdapter;
|
|
151
|
+
return adapterName;
|
|
152
|
+
}
|
|
153
|
+
throw WalletInitializationError.invalidParams(`Adapter ${adapterName} is not configured`);
|
|
154
|
+
} else if ((adapter === null || adapter === void 0 ? void 0 : adapter.type) === ADAPTER_CATEGORY.IN_APP || (adapter === null || adapter === void 0 ? void 0 : adapter.type) === ADAPTER_CATEGORY.EXTERNAL || adapterName === this.cachedAdapter) {
|
|
155
|
+
var _this$modalConfig$ada4;
|
|
156
|
+
if (!((_this$modalConfig$ada4 = this.modalConfig.adapters) !== null && _this$modalConfig$ada4 !== void 0 && _this$modalConfig$ada4[adapterName].showOnModal)) return;
|
|
157
|
+
// add client id to adapter, same web3auth client id can be used in adapter.
|
|
158
|
+
// this id is being overridden if user is also passing client id in adapter's constructor.
|
|
159
|
+
this.walletAdapters[adapterName].setAdapterSettings({
|
|
160
|
+
clientId: this.options.clientId,
|
|
161
|
+
sessionTime: this.options.sessionTime,
|
|
162
|
+
web3AuthNetwork: this.options.web3AuthNetwork,
|
|
163
|
+
useCoreKitKey: this.coreOptions.useCoreKitKey
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// if adapter doesn't have any chainConfig then we will set the chainConfig based of passed chainNamespace
|
|
167
|
+
// and chainNamespace.
|
|
168
|
+
if (!adapter.chainConfigProxy) {
|
|
169
|
+
var _this$coreOptions$cha2;
|
|
170
|
+
const chainConfig = _objectSpread(_objectSpread({}, getChainConfig(providedChainConfig.chainNamespace, (_this$coreOptions$cha2 = this.coreOptions.chainConfig) === null || _this$coreOptions$cha2 === void 0 ? void 0 : _this$coreOptions$cha2.chainId)), this.coreOptions.chainConfig);
|
|
171
|
+
this.walletAdapters[adapterName].setAdapterSettings({
|
|
172
|
+
chainConfig
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (adapterName === WALLET_ADAPTERS.AUTH) {
|
|
176
|
+
var _this$options$uiConfi;
|
|
177
|
+
const authAdapter = this.walletAdapters[adapterName];
|
|
178
|
+
if (this.coreOptions.privateKeyProvider) {
|
|
179
|
+
if (authAdapter.currentChainNamespace !== this.coreOptions.privateKeyProvider.currentChainConfig.chainNamespace) {
|
|
180
|
+
throw WalletInitializationError.incompatibleChainNameSpace("private key provider is not compatible with provided chainNamespace for auth adapter");
|
|
181
|
+
}
|
|
182
|
+
authAdapter.setAdapterSettings({
|
|
183
|
+
privateKeyProvider: this.coreOptions.privateKeyProvider
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
if (smsOtpEnabled !== undefined) {
|
|
187
|
+
authAdapter.setAdapterSettings({
|
|
188
|
+
loginConfig: {
|
|
189
|
+
[LOGIN_PROVIDER.SMS_PASSWORDLESS]: {
|
|
190
|
+
showOnModal: smsOtpEnabled,
|
|
191
|
+
showOnDesktop: smsOtpEnabled,
|
|
192
|
+
showOnMobile: smsOtpEnabled,
|
|
193
|
+
showOnSocialBackupFactor: smsOtpEnabled
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
if (whitelist) {
|
|
199
|
+
authAdapter.setAdapterSettings({
|
|
200
|
+
originData: whitelist.signed_urls
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if ((_this$options$uiConfi = this.options.uiConfig) !== null && _this$options$uiConfi !== void 0 && _this$options$uiConfi.uxMode) {
|
|
204
|
+
authAdapter.setAdapterSettings({
|
|
205
|
+
uxMode: this.options.uiConfig.uxMode
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
authAdapter.setAdapterSettings({
|
|
209
|
+
whiteLabel: this.options.uiConfig
|
|
210
|
+
});
|
|
211
|
+
if (!authAdapter.privateKeyProvider) {
|
|
212
|
+
throw WalletInitializationError.invalidParams("privateKeyProvider is required for auth adapter");
|
|
213
|
+
}
|
|
214
|
+
} else if (adapterName === WALLET_ADAPTERS.WALLET_CONNECT_V2) {
|
|
215
|
+
const walletConnectAdapter = this.walletAdapters[adapterName];
|
|
216
|
+
const {
|
|
217
|
+
wallet_connect_enabled: walletConnectEnabled,
|
|
218
|
+
wallet_connect_project_id: walletConnectProjectId
|
|
219
|
+
} = projectConfig;
|
|
220
|
+
if (walletConnectEnabled === false) {
|
|
221
|
+
var _this$modalConfig$ada5, _this$modalConfig$ada6, _this$modalConfig$ada7;
|
|
222
|
+
// override user specified config by hiding wallet connect
|
|
223
|
+
this.modalConfig.adapters = _objectSpread(_objectSpread({}, (_this$modalConfig$ada5 = this.modalConfig.adapters) !== null && _this$modalConfig$ada5 !== void 0 ? _this$modalConfig$ada5 : {}), {}, {
|
|
224
|
+
[WALLET_ADAPTERS.WALLET_CONNECT_V2]: _objectSpread(_objectSpread({}, (_this$modalConfig$ada6 = (_this$modalConfig$ada7 = this.modalConfig.adapters) === null || _this$modalConfig$ada7 === void 0 ? void 0 : _this$modalConfig$ada7[WALLET_ADAPTERS.WALLET_CONNECT_V2]) !== null && _this$modalConfig$ada6 !== void 0 ? _this$modalConfig$ada6 : {}), {}, {
|
|
225
|
+
showOnModal: false
|
|
226
|
+
})
|
|
227
|
+
});
|
|
228
|
+
this.modalConfig.adapters[WALLET_ADAPTERS.WALLET_CONNECT_V2].showOnModal = false;
|
|
229
|
+
} else {
|
|
230
|
+
var _walletConnectAdapter;
|
|
231
|
+
if (!(walletConnectAdapter !== null && walletConnectAdapter !== void 0 && (_walletConnectAdapter = walletConnectAdapter.adapterOptions) !== null && _walletConnectAdapter !== void 0 && (_walletConnectAdapter = _walletConnectAdapter.adapterSettings) !== null && _walletConnectAdapter !== void 0 && (_walletConnectAdapter = _walletConnectAdapter.walletConnectInitOptions) !== null && _walletConnectAdapter !== void 0 && _walletConnectAdapter.projectId) && !walletConnectProjectId) throw WalletInitializationError.invalidParams("Invalid wallet connect project id. Please configure it on the dashboard");
|
|
232
|
+
if (walletConnectProjectId) {
|
|
233
|
+
walletConnectAdapter.setAdapterSettings({
|
|
234
|
+
adapterSettings: {
|
|
235
|
+
walletConnectInitOptions: {
|
|
236
|
+
projectId: walletConnectProjectId
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return adapterName;
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
const adapterNames = await Promise.all(adapterConfigurationPromises);
|
|
247
|
+
const hasInAppWallets = Object.values(this.walletAdapters).some(adapter => {
|
|
248
|
+
var _this$modalConfig$ada8, _this$modalConfig$ada9, _adapter$name;
|
|
249
|
+
if (adapter.type !== ADAPTER_CATEGORY.IN_APP) return false;
|
|
250
|
+
if (((_this$modalConfig$ada8 = this.modalConfig.adapters) === null || _this$modalConfig$ada8 === void 0 || (_this$modalConfig$ada8 = _this$modalConfig$ada8[adapter.name]) === null || _this$modalConfig$ada8 === void 0 ? void 0 : _this$modalConfig$ada8.showOnModal) !== true) return false;
|
|
251
|
+
if (!((_this$modalConfig$ada9 = this.modalConfig.adapters) !== null && _this$modalConfig$ada9 !== void 0 && (_this$modalConfig$ada9 = _this$modalConfig$ada9[adapter.name]) !== null && _this$modalConfig$ada9 !== void 0 && _this$modalConfig$ada9.loginMethods)) return true;
|
|
252
|
+
const mergedLoginMethods = getAdapterSocialLogins(adapter.name, (_adapter$name = this.modalConfig.adapters[adapter.name]) === null || _adapter$name === void 0 ? void 0 : _adapter$name.loginMethods);
|
|
253
|
+
if (Object.values(mergedLoginMethods).some(method => method.showOnModal)) return true;
|
|
254
|
+
return false;
|
|
255
|
+
});
|
|
256
|
+
log.debug(hasInAppWallets, this.walletAdapters, adapterNames, "hasInAppWallets");
|
|
257
|
+
|
|
258
|
+
// Now, initialize the adapters.
|
|
259
|
+
const initPromises = adapterNames.map(async adapterName => {
|
|
260
|
+
if (!adapterName) return;
|
|
261
|
+
try {
|
|
262
|
+
const adapter = this.walletAdapters[adapterName];
|
|
263
|
+
// only initialize a external adapter here if it is a cached adapter.
|
|
264
|
+
if (this.cachedAdapter !== adapterName && adapter.type === ADAPTER_CATEGORY.EXTERNAL) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
// in-app wallets or cached wallet (being connected or already connected) are initialized first.
|
|
268
|
+
// if adapter is configured then only initialize in app or cached adapter.
|
|
269
|
+
// external wallets are initialized on INIT_EXTERNAL_WALLET event.
|
|
270
|
+
this.subscribeToAdapterEvents(adapter);
|
|
271
|
+
if (adapter.status === ADAPTER_STATUS.NOT_READY) await adapter.init({
|
|
272
|
+
autoConnect: this.cachedAdapter === adapterName
|
|
273
|
+
});
|
|
274
|
+
// note: not adding cachedWallet to modal if it is external wallet.
|
|
275
|
+
// adding it later if no in-app wallets are available.
|
|
276
|
+
if (adapter.type === ADAPTER_CATEGORY.IN_APP) {
|
|
277
|
+
this.initializeInAppWallet(adapterName);
|
|
278
|
+
}
|
|
279
|
+
} catch (error) {
|
|
280
|
+
log.error(error, "error while initializing adapter ", adapterName);
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
this.commonJRPCProvider = await CommonJRPCProvider.getProviderInstance({
|
|
284
|
+
chainConfig: this.coreOptions.chainConfig
|
|
285
|
+
});
|
|
286
|
+
if (typeof keyExportEnabled === "boolean") {
|
|
287
|
+
this.coreOptions.privateKeyProvider.setKeyExportFlag(keyExportEnabled);
|
|
288
|
+
// dont know if we need to do this.
|
|
289
|
+
this.commonJRPCProvider.setKeyExportFlag(keyExportEnabled);
|
|
290
|
+
}
|
|
291
|
+
await Promise.all(initPromises);
|
|
292
|
+
if (this.status === ADAPTER_STATUS.NOT_READY) {
|
|
293
|
+
this.status = ADAPTER_STATUS.READY;
|
|
294
|
+
this.emit(ADAPTER_EVENTS.READY);
|
|
295
|
+
}
|
|
296
|
+
const hasExternalWallets = allAdapters.some(adapterName => {
|
|
297
|
+
var _this$walletAdapters$, _this$modalConfig$ada10;
|
|
298
|
+
return ((_this$walletAdapters$ = this.walletAdapters[adapterName]) === null || _this$walletAdapters$ === void 0 ? void 0 : _this$walletAdapters$.type) === ADAPTER_CATEGORY.EXTERNAL && ((_this$modalConfig$ada10 = this.modalConfig.adapters) === null || _this$modalConfig$ada10 === void 0 ? void 0 : _this$modalConfig$ada10[adapterName].showOnModal);
|
|
299
|
+
});
|
|
300
|
+
if (hasExternalWallets) {
|
|
301
|
+
this.loginModal.initExternalWalletContainer();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// variable to check if we have any in app wallets
|
|
305
|
+
// currently all default in app and external wallets can be hidden or shown based on config.
|
|
306
|
+
if (!hasInAppWallets && hasExternalWallets) {
|
|
307
|
+
// if no in app wallet is available then initialize external wallets in modal
|
|
308
|
+
await this.initExternalWalletAdapters(false, {
|
|
309
|
+
showExternalWalletsOnly: true
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
async connect() {
|
|
314
|
+
if (!this.loginModal) throw WalletInitializationError.notReady("Login modal is not initialized");
|
|
315
|
+
// if already connected return provider
|
|
316
|
+
if (this.connectedAdapterName && this.status === ADAPTER_STATUS.CONNECTED && this.provider) return this.provider;
|
|
317
|
+
this.loginModal.open();
|
|
318
|
+
return new Promise((resolve, reject) => {
|
|
319
|
+
this.once(ADAPTER_EVENTS.CONNECTED, () => {
|
|
320
|
+
return resolve(this.provider);
|
|
321
|
+
});
|
|
322
|
+
this.once(ADAPTER_EVENTS.ERRORED, err => {
|
|
323
|
+
return reject(err);
|
|
324
|
+
});
|
|
325
|
+
this.once(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, visibility => {
|
|
326
|
+
// modal is closed but user is not connected to any wallet.
|
|
327
|
+
if (!visibility && this.status !== ADAPTER_STATUS.CONNECTED) {
|
|
328
|
+
return reject(new Error("User closed the modal"));
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
async initExternalWalletAdapters(externalWalletsInitialized, options) {
|
|
334
|
+
if (externalWalletsInitialized) return;
|
|
335
|
+
const adaptersConfig = {};
|
|
336
|
+
Object.keys(this.walletAdapters).forEach(async adapterName => {
|
|
337
|
+
const adapter = this.walletAdapters[adapterName];
|
|
338
|
+
if ((adapter === null || adapter === void 0 ? void 0 : adapter.type) === ADAPTER_CATEGORY.EXTERNAL) {
|
|
339
|
+
log.debug("init external wallet", this.cachedAdapter, adapterName);
|
|
340
|
+
this.subscribeToAdapterEvents(adapter);
|
|
341
|
+
// we are not initializing cached adapter here as it is already being initialized in initModal before.
|
|
342
|
+
if (this.cachedAdapter === adapterName) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (adapter.status === ADAPTER_STATUS.NOT_READY) {
|
|
346
|
+
await adapter.init({
|
|
347
|
+
autoConnect: this.cachedAdapter === adapterName
|
|
348
|
+
}).then(() => {
|
|
349
|
+
const adapterModalConfig = this.modalConfig.adapters[adapterName];
|
|
350
|
+
adaptersConfig[adapterName] = _objectSpread(_objectSpread({}, adapterModalConfig), {}, {
|
|
351
|
+
isInjected: adapter.isInjected
|
|
352
|
+
});
|
|
353
|
+
this.loginModal.addWalletLogins(adaptersConfig, {
|
|
354
|
+
showExternalWalletsOnly: !!(options !== null && options !== void 0 && options.showExternalWalletsOnly)
|
|
355
|
+
});
|
|
356
|
+
return undefined;
|
|
357
|
+
}).catch(error => log.error(error, "error while initializing adapter", adapterName));
|
|
358
|
+
} else if (adapter.status === ADAPTER_STATUS.READY) {
|
|
359
|
+
const adapterModalConfig = this.modalConfig.adapters[adapterName];
|
|
360
|
+
adaptersConfig[adapterName] = _objectSpread(_objectSpread({}, adapterModalConfig), {}, {
|
|
361
|
+
isInjected: adapter.isInjected
|
|
362
|
+
});
|
|
363
|
+
this.loginModal.addWalletLogins(adaptersConfig, {
|
|
364
|
+
showExternalWalletsOnly: !!(options !== null && options !== void 0 && options.showExternalWalletsOnly)
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
initializeInAppWallet(adapterName) {
|
|
371
|
+
log.info("adapterInitResults", adapterName);
|
|
372
|
+
if (this.walletAdapters[adapterName].type === ADAPTER_CATEGORY.IN_APP) {
|
|
373
|
+
var _adapterName, _this$options$uiConfi2, _this$options$uiConfi3, _this$options$uiConfi4;
|
|
374
|
+
this.loginModal.addSocialLogins(adapterName, getAdapterSocialLogins(adapterName, (_adapterName = this.modalConfig.adapters[adapterName]) === null || _adapterName === void 0 ? void 0 : _adapterName.loginMethods), ((_this$options$uiConfi2 = this.options.uiConfig) === null || _this$options$uiConfi2 === void 0 ? void 0 : _this$options$uiConfi2.loginMethodsOrder) || AUTH_PROVIDERS, _objectSpread(_objectSpread({}, this.options.uiConfig), {}, {
|
|
375
|
+
loginGridCol: ((_this$options$uiConfi3 = this.options.uiConfig) === null || _this$options$uiConfi3 === void 0 ? void 0 : _this$options$uiConfi3.loginGridCol) || 3,
|
|
376
|
+
primaryButton: ((_this$options$uiConfi4 = this.options.uiConfig) === null || _this$options$uiConfi4 === void 0 ? void 0 : _this$options$uiConfi4.primaryButton) || "socialLogin"
|
|
377
|
+
}));
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
subscribeToLoginModalEvents() {
|
|
381
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.LOGIN, async params => {
|
|
382
|
+
try {
|
|
383
|
+
await this.connectTo(params.adapter, params.loginParams);
|
|
384
|
+
} catch (error) {
|
|
385
|
+
log.error(`Error while connecting to adapter: ${params.adapter}`, error);
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.INIT_EXTERNAL_WALLETS, async params => {
|
|
389
|
+
await this.initExternalWalletAdapters(params.externalWalletsInitialized);
|
|
390
|
+
});
|
|
391
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.DISCONNECT, async () => {
|
|
392
|
+
try {
|
|
393
|
+
await this.logout();
|
|
394
|
+
} catch (error) {
|
|
395
|
+
log.error(`Error while disconnecting`, error);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, async visibility => {
|
|
399
|
+
log.debug("is login modal visible", visibility);
|
|
400
|
+
this.emit(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, visibility);
|
|
401
|
+
const adapter = this.walletAdapters[WALLET_ADAPTERS.WALLET_CONNECT_V2];
|
|
402
|
+
if (adapter) {
|
|
403
|
+
const walletConnectStatus = adapter === null || adapter === void 0 ? void 0 : adapter.status;
|
|
404
|
+
log.debug("trying refreshing wc session", visibility, walletConnectStatus);
|
|
405
|
+
if (visibility && (walletConnectStatus === ADAPTER_STATUS.READY || walletConnectStatus === ADAPTER_STATUS.CONNECTING)) {
|
|
406
|
+
log.debug("refreshing wc session");
|
|
407
|
+
|
|
408
|
+
// refreshing session for wallet connect whenever modal is opened.
|
|
409
|
+
try {
|
|
410
|
+
adapter.connect();
|
|
411
|
+
} catch (error) {
|
|
412
|
+
log.error(`Error while disconnecting to wallet connect in core`, error);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (!visibility && this.status === ADAPTER_STATUS.CONNECTED && (walletConnectStatus === ADAPTER_STATUS.READY || walletConnectStatus === ADAPTER_STATUS.CONNECTING)) {
|
|
416
|
+
log.debug("this stops wc adapter from trying to reconnect once proposal expires");
|
|
417
|
+
adapter.status = ADAPTER_STATUS.READY;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export { Web3Auth };
|