@web3auth/no-modal 8.12.4 → 9.0.0-alpha.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
- Copyright Web3Auth Inc. 2023. All rights reserved.
1
+ Copyright Torus Labs Private Limited 2023. All rights reserved.
2
2
 
3
- You acknowledge and agree that Web3Auth Inc. (Web3Auth) (or Web3Auth's licensors) own all legal rights, titles and interests in and to the work, software, application, source code, documentation and any other documents in this repository (collectively, the “Program”), including any intellectual property rights which subsist in the Program (whether those rights happen to be registered or not, and wherever in the world those rights may exist), whether in source code or any other form.
3
+ You acknowledge and agree that Torus Labs Private Limited (Web3Auth) (or Web3Auth's licensors) own all legal rights, titles and interests in and to the work, software, application, source code, documentation and any other documents in this repository (collectively, the “Program”), including any intellectual property rights which subsist in the Program (whether those rights happen to be registered or not, and wherever in the world those rights may exist), whether in source code or any other form.
4
4
 
5
5
  Subject to the limited license below, you may not (and you may not permit anyone else to) distribute, publish, copy, modify, merge, combine with another program, create derivative works of, reverse engineer, decompile or otherwise attempt to extract the source code of, the Program or any part thereof, except that you may contribute to this repository.
6
6
 
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var noModal = require('./noModal.js');
4
+
5
+
6
+
7
+ exports.Web3AuthNoModal = noModal.Web3AuthNoModal;
@@ -0,0 +1,343 @@
1
+ 'use strict';
2
+
3
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
4
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
5
+ var auth = require('@web3auth/auth');
6
+ var base = require('@web3auth/base');
7
+ var baseProvider = require('@web3auth/base-provider');
8
+ var openloginAdapter = require('@web3auth/openlogin-adapter');
9
+ var deepmerge = require('deepmerge');
10
+
11
+ const ADAPTER_CACHE_KEY = "Web3Auth-cachedAdapter";
12
+ class Web3AuthNoModal extends auth.SafeEventEmitter {
13
+ constructor(options) {
14
+ var _options$chainConfig, _options$chainConfig2, _options$chainConfig3, _options$chainConfig4;
15
+ super();
16
+ _defineProperty(this, "coreOptions", void 0);
17
+ _defineProperty(this, "connectedAdapterName", null);
18
+ _defineProperty(this, "status", base.ADAPTER_STATUS.NOT_READY);
19
+ _defineProperty(this, "cachedAdapter", null);
20
+ _defineProperty(this, "walletAdapters", {});
21
+ _defineProperty(this, "commonJRPCProvider", null);
22
+ _defineProperty(this, "plugins", {});
23
+ _defineProperty(this, "storage", "localStorage");
24
+ if (!options.clientId) throw base.WalletInitializationError.invalidParams("Please provide a valid clientId in constructor");
25
+ if (options.enableLogging) base.log.enableAll();else base.log.setLevel("error");
26
+ if (!options.privateKeyProvider && !options.chainConfig) {
27
+ throw base.WalletInitializationError.invalidParams("Please provide chainConfig or privateKeyProvider");
28
+ }
29
+ options.chainConfig = options.chainConfig || options.privateKeyProvider.currentChainConfig;
30
+ if (!((_options$chainConfig = options.chainConfig) !== null && _options$chainConfig !== void 0 && _options$chainConfig.chainNamespace) || !Object.values(base.CHAIN_NAMESPACES).includes((_options$chainConfig2 = options.chainConfig) === null || _options$chainConfig2 === void 0 ? void 0 : _options$chainConfig2.chainNamespace)) throw base.WalletInitializationError.invalidParams("Please provide a valid chainNamespace in chainConfig");
31
+ if (options.storageKey === "session") this.storage = "sessionStorage";
32
+ this.cachedAdapter = base.storageAvailable(this.storage) ? window[this.storage].getItem(ADAPTER_CACHE_KEY) : null;
33
+ this.coreOptions = _objectSpread(_objectSpread({}, options), {}, {
34
+ chainConfig: _objectSpread(_objectSpread({}, base.getChainConfig((_options$chainConfig3 = options.chainConfig) === null || _options$chainConfig3 === void 0 ? void 0 : _options$chainConfig3.chainNamespace, (_options$chainConfig4 = options.chainConfig) === null || _options$chainConfig4 === void 0 ? void 0 : _options$chainConfig4.chainId) || {}), options.chainConfig)
35
+ });
36
+ this.subscribeToAdapterEvents = this.subscribeToAdapterEvents.bind(this);
37
+ }
38
+ get connected() {
39
+ return Boolean(this.connectedAdapterName);
40
+ }
41
+ get provider() {
42
+ if (this.status !== base.ADAPTER_STATUS.NOT_READY && this.commonJRPCProvider) {
43
+ return this.commonJRPCProvider;
44
+ }
45
+ return null;
46
+ }
47
+ set provider(_) {
48
+ throw new Error("Not implemented");
49
+ }
50
+ async init() {
51
+ this.commonJRPCProvider = await baseProvider.CommonJRPCProvider.getProviderInstance({
52
+ chainConfig: this.coreOptions.chainConfig
53
+ });
54
+ let projectConfig;
55
+ try {
56
+ projectConfig = await base.fetchProjectConfig(this.coreOptions.clientId, this.coreOptions.web3AuthNetwork);
57
+ } catch (e) {
58
+ base.log.error("Failed to fetch project configurations", e);
59
+ throw base.WalletInitializationError.notReady("failed to fetch project configurations", e);
60
+ }
61
+ const initPromises = Object.keys(this.walletAdapters).map(async adapterName => {
62
+ this.subscribeToAdapterEvents(this.walletAdapters[adapterName]);
63
+ // if adapter doesn't have any chain config yet then set it based on provided namespace and chainId.
64
+ // if no chainNamespace or chainId is being provided, it will connect with mainnet.
65
+ if (!this.walletAdapters[adapterName].chainConfigProxy) {
66
+ const providedChainConfig = this.coreOptions.chainConfig;
67
+ if (!providedChainConfig.chainNamespace) throw base.WalletInitializationError.invalidParams("Please provide chainNamespace in chainConfig");
68
+ this.walletAdapters[adapterName].setAdapterSettings({
69
+ chainConfig: providedChainConfig,
70
+ sessionTime: this.coreOptions.sessionTime,
71
+ clientId: this.coreOptions.clientId,
72
+ web3AuthNetwork: this.coreOptions.web3AuthNetwork,
73
+ useCoreKitKey: this.coreOptions.useCoreKitKey
74
+ });
75
+ } else {
76
+ this.walletAdapters[adapterName].setAdapterSettings({
77
+ sessionTime: this.coreOptions.sessionTime,
78
+ clientId: this.coreOptions.clientId,
79
+ web3AuthNetwork: this.coreOptions.web3AuthNetwork,
80
+ useCoreKitKey: this.coreOptions.useCoreKitKey
81
+ });
82
+ }
83
+ if (adapterName === base.WALLET_ADAPTERS.OPENLOGIN) {
84
+ const openloginAdapter$1 = this.walletAdapters[adapterName];
85
+ const {
86
+ whitelabel
87
+ } = projectConfig;
88
+ this.coreOptions.uiConfig = deepmerge(base.cloneDeep(whitelabel), this.coreOptions.uiConfig);
89
+ if (!this.coreOptions.uiConfig.mode) this.coreOptions.uiConfig.mode = "light";
90
+ const {
91
+ sms_otp_enabled: smsOtpEnabled,
92
+ whitelist,
93
+ key_export_enabled: keyExportEnabled
94
+ } = projectConfig;
95
+ if (smsOtpEnabled !== undefined) {
96
+ openloginAdapter$1.setAdapterSettings({
97
+ loginConfig: {
98
+ [openloginAdapter.LOGIN_PROVIDER.SMS_PASSWORDLESS]: {
99
+ showOnModal: smsOtpEnabled,
100
+ showOnDesktop: smsOtpEnabled,
101
+ showOnMobile: smsOtpEnabled,
102
+ showOnSocialBackupFactor: smsOtpEnabled
103
+ }
104
+ }
105
+ });
106
+ }
107
+ if (whitelist) {
108
+ openloginAdapter$1.setAdapterSettings({
109
+ originData: whitelist.signed_urls
110
+ });
111
+ }
112
+ if (typeof keyExportEnabled === "boolean") {
113
+ this.coreOptions.privateKeyProvider.setKeyExportFlag(keyExportEnabled);
114
+ // dont know if this is required or not.
115
+ this.commonJRPCProvider.setKeyExportFlag(keyExportEnabled);
116
+ }
117
+ if (this.coreOptions.privateKeyProvider) {
118
+ if (openloginAdapter$1.currentChainNamespace !== this.coreOptions.privateKeyProvider.currentChainConfig.chainNamespace) {
119
+ throw base.WalletInitializationError.incompatibleChainNameSpace("private key provider is not compatible with provided chainNamespace for openlogin adapter");
120
+ }
121
+ openloginAdapter$1.setAdapterSettings({
122
+ privateKeyProvider: this.coreOptions.privateKeyProvider
123
+ });
124
+ }
125
+ openloginAdapter$1.setAdapterSettings({
126
+ whiteLabel: this.coreOptions.uiConfig
127
+ });
128
+ if (!openloginAdapter$1.privateKeyProvider) {
129
+ throw base.WalletInitializationError.invalidParams("privateKeyProvider is required for openlogin adapter");
130
+ }
131
+ } else if (adapterName === base.WALLET_ADAPTERS.WALLET_CONNECT_V2) {
132
+ const walletConnectAdapter = this.walletAdapters[adapterName];
133
+ const {
134
+ wallet_connect_enabled: walletConnectEnabled,
135
+ wallet_connect_project_id: walletConnectProjectId
136
+ } = projectConfig;
137
+ if (walletConnectEnabled === false) {
138
+ throw base.WalletInitializationError.invalidParams("Please enable wallet connect v2 addon on dashboard");
139
+ }
140
+ if (!walletConnectProjectId) throw base.WalletInitializationError.invalidParams("Invalid wallet connect project id. Please configure it on the dashboard");
141
+ walletConnectAdapter.setAdapterSettings({
142
+ adapterSettings: {
143
+ walletConnectInitOptions: {
144
+ projectId: walletConnectProjectId
145
+ }
146
+ }
147
+ });
148
+ }
149
+ return this.walletAdapters[adapterName].init({
150
+ autoConnect: this.cachedAdapter === adapterName
151
+ }).catch(e => base.log.error(e, adapterName));
152
+ });
153
+ await Promise.all(initPromises);
154
+ if (this.status === base.ADAPTER_STATUS.NOT_READY) {
155
+ this.status = base.ADAPTER_STATUS.READY;
156
+ this.emit(base.ADAPTER_EVENTS.READY);
157
+ }
158
+ }
159
+ getAdapter(adapterName) {
160
+ return this.walletAdapters[adapterName] || null;
161
+ }
162
+ configureAdapter(adapter) {
163
+ this.checkInitRequirements();
164
+ const providedChainConfig = this.coreOptions.chainConfig;
165
+ if (!providedChainConfig.chainNamespace) throw base.WalletInitializationError.invalidParams("Please provide chainNamespace in chainConfig");
166
+ const adapterAlreadyExists = this.walletAdapters[adapter.name];
167
+ if (adapterAlreadyExists) throw base.WalletInitializationError.duplicateAdapterError(`Wallet adapter for ${adapter.name} already exists`);
168
+ if (adapter.adapterNamespace !== base.ADAPTER_NAMESPACES.MULTICHAIN && adapter.adapterNamespace !== providedChainConfig.chainNamespace) throw base.WalletInitializationError.incompatibleChainNameSpace(`This wallet adapter belongs to ${adapter.adapterNamespace} which is incompatible with currently used namespace: ${providedChainConfig.chainNamespace}`);
169
+ if (adapter.adapterNamespace === base.ADAPTER_NAMESPACES.MULTICHAIN && adapter.currentChainNamespace && providedChainConfig.chainNamespace !== adapter.currentChainNamespace) {
170
+ // chainConfig checks are already validated in constructor so using typecast is safe here.
171
+ adapter.setAdapterSettings({
172
+ chainConfig: providedChainConfig
173
+ });
174
+ }
175
+ this.walletAdapters[adapter.name] = adapter;
176
+ return this;
177
+ }
178
+ clearCache() {
179
+ if (!base.storageAvailable(this.storage)) return;
180
+ window[this.storage].removeItem(ADAPTER_CACHE_KEY);
181
+ this.cachedAdapter = null;
182
+ }
183
+ async addChain(chainConfig) {
184
+ if (this.status === base.ADAPTER_STATUS.CONNECTED && this.connectedAdapterName) return this.walletAdapters[this.connectedAdapterName].addChain(chainConfig);
185
+ if (this.commonJRPCProvider) {
186
+ return this.commonJRPCProvider.addChain(chainConfig);
187
+ }
188
+ throw base.WalletInitializationError.notReady(`No wallet is ready`);
189
+ }
190
+ async switchChain(params) {
191
+ if (this.status === base.ADAPTER_STATUS.CONNECTED && this.connectedAdapterName) return this.walletAdapters[this.connectedAdapterName].switchChain(params);
192
+ if (this.commonJRPCProvider) {
193
+ return this.commonJRPCProvider.switchChain(params);
194
+ }
195
+ throw base.WalletInitializationError.notReady(`No wallet is ready`);
196
+ }
197
+
198
+ /**
199
+ * Connect to a specific wallet adapter
200
+ * @param walletName - Key of the walletAdapter to use.
201
+ */
202
+ async connectTo(walletName, loginParams) {
203
+ if (!this.walletAdapters[walletName] || !this.commonJRPCProvider) throw base.WalletInitializationError.notFound(`Please add wallet adapter for ${walletName} wallet, before connecting`);
204
+ const provider = await this.walletAdapters[walletName].connect(loginParams);
205
+ this.commonJRPCProvider.updateProviderEngineProxy(provider.provider || provider);
206
+ return this.provider;
207
+ }
208
+ async logout(options = {
209
+ cleanup: false
210
+ }) {
211
+ if (this.status !== base.ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw base.WalletLoginError.notConnectedError(`No wallet is connected`);
212
+ await this.walletAdapters[this.connectedAdapterName].disconnect(options);
213
+ }
214
+ async getUserInfo() {
215
+ base.log.debug("Getting user info", this.status, this.connectedAdapterName);
216
+ if (this.status !== base.ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw base.WalletLoginError.notConnectedError(`No wallet is connected`);
217
+ return this.walletAdapters[this.connectedAdapterName].getUserInfo();
218
+ }
219
+ async enableMFA(loginParams) {
220
+ if (this.status !== base.ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw base.WalletLoginError.notConnectedError(`No wallet is connected`);
221
+ if (this.connectedAdapterName !== base.WALLET_ADAPTERS.OPENLOGIN) throw base.WalletLoginError.unsupportedOperation(`EnableMFA is not supported for this adapter.`);
222
+ return this.walletAdapters[this.connectedAdapterName].enableMFA(loginParams);
223
+ }
224
+ async authenticateUser() {
225
+ if (this.status !== base.ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw base.WalletLoginError.notConnectedError(`No wallet is connected`);
226
+ return this.walletAdapters[this.connectedAdapterName].authenticateUser();
227
+ }
228
+ addPlugin(plugin) {
229
+ if (this.plugins[plugin.name]) throw base.WalletInitializationError.duplicateAdapterError(`Plugin ${plugin.name} already exist`);
230
+ if (plugin.pluginNamespace !== base.PLUGIN_NAMESPACES.MULTICHAIN && plugin.pluginNamespace !== this.coreOptions.chainConfig.chainNamespace) throw base.WalletInitializationError.incompatibleChainNameSpace(`This plugin belongs to ${plugin.pluginNamespace} namespace which is incompatible with currently used namespace: ${this.coreOptions.chainConfig.chainNamespace}`);
231
+ this.plugins[plugin.name] = plugin;
232
+ if (this.status === base.ADAPTER_STATUS.CONNECTED && this.connectedAdapterName) {
233
+ // web3auth is already connected. can initialize plugins
234
+ this.connectToPlugins({
235
+ adapter: this.connectedAdapterName
236
+ });
237
+ }
238
+ return this;
239
+ }
240
+ getPlugin(name) {
241
+ return this.plugins[name] || null;
242
+ }
243
+ subscribeToAdapterEvents(walletAdapter) {
244
+ walletAdapter.on(base.ADAPTER_EVENTS.CONNECTED, async data => {
245
+ if (!this.commonJRPCProvider) throw base.WalletInitializationError.notFound(`CommonJrpcProvider not found`);
246
+ const {
247
+ provider
248
+ } = data;
249
+ this.commonJRPCProvider.updateProviderEngineProxy(provider.provider || provider);
250
+ this.status = base.ADAPTER_STATUS.CONNECTED;
251
+ this.connectedAdapterName = data.adapter;
252
+ this.cacheWallet(data.adapter);
253
+ base.log.debug("connected", this.status, this.connectedAdapterName);
254
+ this.connectToPlugins(data);
255
+ this.emit(base.ADAPTER_EVENTS.CONNECTED, _objectSpread({}, data));
256
+ });
257
+ walletAdapter.on(base.ADAPTER_EVENTS.DISCONNECTED, async () => {
258
+ // get back to ready state for rehydrating.
259
+ this.status = base.ADAPTER_STATUS.READY;
260
+ if (base.storageAvailable(this.storage)) {
261
+ const cachedAdapter = window[this.storage].getItem(ADAPTER_CACHE_KEY);
262
+ if (this.connectedAdapterName === cachedAdapter) {
263
+ this.clearCache();
264
+ }
265
+ }
266
+ base.log.debug("disconnected", this.status, this.connectedAdapterName);
267
+ await Promise.all(Object.values(this.plugins).map(plugin => {
268
+ return plugin.disconnect().catch(error => {
269
+ // swallow error if adapter doesn't supports this plugin.
270
+ if (error.code === 5211) {
271
+ return;
272
+ }
273
+ // throw error;
274
+ base.log.error(error);
275
+ });
276
+ }));
277
+ this.connectedAdapterName = null;
278
+ this.emit(base.ADAPTER_EVENTS.DISCONNECTED);
279
+ });
280
+ walletAdapter.on(base.ADAPTER_EVENTS.CONNECTING, data => {
281
+ this.status = base.ADAPTER_STATUS.CONNECTING;
282
+ this.emit(base.ADAPTER_EVENTS.CONNECTING, data);
283
+ base.log.debug("connecting", this.status, this.connectedAdapterName);
284
+ });
285
+ walletAdapter.on(base.ADAPTER_EVENTS.ERRORED, data => {
286
+ this.status = base.ADAPTER_STATUS.ERRORED;
287
+ this.clearCache();
288
+ this.emit(base.ADAPTER_EVENTS.ERRORED, data);
289
+ base.log.debug("errored", this.status, this.connectedAdapterName);
290
+ });
291
+ walletAdapter.on(base.ADAPTER_EVENTS.ADAPTER_DATA_UPDATED, data => {
292
+ base.log.debug("adapter data updated", data);
293
+ this.emit(base.ADAPTER_EVENTS.ADAPTER_DATA_UPDATED, data);
294
+ });
295
+ walletAdapter.on(base.ADAPTER_EVENTS.CACHE_CLEAR, data => {
296
+ base.log.debug("adapter cache clear", data);
297
+ if (base.storageAvailable(this.storage)) {
298
+ this.clearCache();
299
+ }
300
+ });
301
+ }
302
+ checkInitRequirements() {
303
+ if (this.status === base.ADAPTER_STATUS.CONNECTING) throw base.WalletInitializationError.notReady("Already pending connection");
304
+ if (this.status === base.ADAPTER_STATUS.CONNECTED) throw base.WalletInitializationError.notReady("Already connected");
305
+ if (this.status === base.ADAPTER_STATUS.READY) throw base.WalletInitializationError.notReady("Adapter is already initialized");
306
+ }
307
+ cacheWallet(walletName) {
308
+ if (!base.storageAvailable(this.storage)) return;
309
+ window[this.storage].setItem(ADAPTER_CACHE_KEY, walletName);
310
+ this.cachedAdapter = walletName;
311
+ }
312
+ connectToPlugins(data) {
313
+ Object.values(this.plugins).map(async plugin => {
314
+ try {
315
+ if (!plugin.SUPPORTED_ADAPTERS.includes(data.adapter)) {
316
+ return;
317
+ }
318
+ if (plugin.status === base.PLUGIN_STATUS.CONNECTED) return;
319
+ const {
320
+ openloginInstance
321
+ } = this.walletAdapters[this.connectedAdapterName];
322
+ const {
323
+ options,
324
+ sessionId,
325
+ sessionNamespace
326
+ } = openloginInstance || {};
327
+ await plugin.initWithWeb3Auth(this, options.whiteLabel);
328
+ await plugin.connect({
329
+ sessionId,
330
+ sessionNamespace
331
+ });
332
+ } catch (error) {
333
+ // swallow error if connector adapter doesn't supports this plugin.
334
+ if (error.code === 5211) {
335
+ return;
336
+ }
337
+ base.log.error(error);
338
+ }
339
+ });
340
+ }
341
+ }
342
+
343
+ exports.Web3AuthNoModal = Web3AuthNoModal;
@@ -0,0 +1 @@
1
+ export { Web3AuthNoModal } from './noModal.js';