@web3auth/no-modal 8.0.0 → 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/dist/noModal.cjs.js +310 -8
- package/dist/noModal.esm.js +42 -8
- package/dist/noModal.umd.min.js +1 -2
- package/dist/noModal.umd.min.js.LICENSE.txt +0 -6
- package/package.json +13 -12
- package/dist/noModal.cjs.js.map +0 -1
- package/dist/noModal.esm.js.map +0 -1
- package/dist/noModal.umd.min.js.map +0 -1
- package/src/index.ts +0 -1
- package/src/noModal.ts +0 -333
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./noModal";
|
package/src/noModal.ts
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
import { SafeEventEmitter } from "@toruslabs/openlogin-jrpc";
|
|
2
|
-
import {
|
|
3
|
-
ADAPTER_EVENTS,
|
|
4
|
-
ADAPTER_NAMESPACES,
|
|
5
|
-
ADAPTER_STATUS,
|
|
6
|
-
ADAPTER_STATUS_TYPE,
|
|
7
|
-
CHAIN_NAMESPACES,
|
|
8
|
-
CONNECTED_EVENT_DATA,
|
|
9
|
-
CustomChainConfig,
|
|
10
|
-
getChainConfig,
|
|
11
|
-
IAdapter,
|
|
12
|
-
IBaseProvider,
|
|
13
|
-
IProvider,
|
|
14
|
-
IWeb3Auth,
|
|
15
|
-
log,
|
|
16
|
-
storageAvailable,
|
|
17
|
-
UserAuthInfo,
|
|
18
|
-
UserInfo,
|
|
19
|
-
WALLET_ADAPTER_TYPE,
|
|
20
|
-
WALLET_ADAPTERS,
|
|
21
|
-
WalletInitializationError,
|
|
22
|
-
WalletLoginError,
|
|
23
|
-
Web3AuthError,
|
|
24
|
-
Web3AuthNoModalOptions,
|
|
25
|
-
} from "@web3auth/base";
|
|
26
|
-
import { IPlugin, PLUGIN_NAMESPACES } from "@web3auth/base-plugin";
|
|
27
|
-
import { CommonJRPCProvider } from "@web3auth/base-provider";
|
|
28
|
-
import type { OpenloginAdapter } from "@web3auth/openlogin-adapter";
|
|
29
|
-
import type { WalletConnectV2Adapter } from "@web3auth/wallet-connect-v2-adapter";
|
|
30
|
-
|
|
31
|
-
const ADAPTER_CACHE_KEY = "Web3Auth-cachedAdapter";
|
|
32
|
-
export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth {
|
|
33
|
-
readonly coreOptions: Web3AuthNoModalOptions;
|
|
34
|
-
|
|
35
|
-
public connectedAdapterName: WALLET_ADAPTER_TYPE | null = null;
|
|
36
|
-
|
|
37
|
-
public status: ADAPTER_STATUS_TYPE = ADAPTER_STATUS.NOT_READY;
|
|
38
|
-
|
|
39
|
-
public cachedAdapter: string | null = null;
|
|
40
|
-
|
|
41
|
-
public walletAdapters: Record<string, IAdapter<unknown>> = {};
|
|
42
|
-
|
|
43
|
-
protected commonJRPCProvider: CommonJRPCProvider | null = null;
|
|
44
|
-
|
|
45
|
-
private plugins: Record<string, IPlugin> = {};
|
|
46
|
-
|
|
47
|
-
private storage: "sessionStorage" | "localStorage" = "localStorage";
|
|
48
|
-
|
|
49
|
-
constructor(options: Web3AuthNoModalOptions) {
|
|
50
|
-
super();
|
|
51
|
-
if (!options.clientId) throw WalletInitializationError.invalidParams("Please provide a valid clientId in constructor");
|
|
52
|
-
if (options.enableLogging) log.enableAll();
|
|
53
|
-
else log.setLevel("error");
|
|
54
|
-
if (!options.privateKeyProvider && !options.chainConfig) {
|
|
55
|
-
throw WalletInitializationError.invalidParams("Please provide chainConfig or privateKeyProvider");
|
|
56
|
-
}
|
|
57
|
-
options.chainConfig = options.chainConfig || options.privateKeyProvider.currentChainConfig;
|
|
58
|
-
if (!options.chainConfig?.chainNamespace || !Object.values(CHAIN_NAMESPACES).includes(options.chainConfig?.chainNamespace))
|
|
59
|
-
throw WalletInitializationError.invalidParams("Please provide a valid chainNamespace in chainConfig");
|
|
60
|
-
if (options.storageKey === "session") this.storage = "sessionStorage";
|
|
61
|
-
this.cachedAdapter = storageAvailable(this.storage) ? window[this.storage].getItem(ADAPTER_CACHE_KEY) : null;
|
|
62
|
-
|
|
63
|
-
this.coreOptions = {
|
|
64
|
-
...options,
|
|
65
|
-
chainConfig: {
|
|
66
|
-
...(getChainConfig(options.chainConfig?.chainNamespace, options.chainConfig?.chainId) || {}),
|
|
67
|
-
...options.chainConfig,
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
this.subscribeToAdapterEvents = this.subscribeToAdapterEvents.bind(this);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
get connected(): boolean {
|
|
74
|
-
return Boolean(this.connectedAdapterName);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get provider(): IProvider | null {
|
|
78
|
-
if (this.status !== ADAPTER_STATUS.NOT_READY && this.commonJRPCProvider) {
|
|
79
|
-
return this.commonJRPCProvider;
|
|
80
|
-
}
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
set provider(_: IProvider | null) {
|
|
85
|
-
throw new Error("Not implemented");
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public async init(): Promise<void> {
|
|
89
|
-
this.commonJRPCProvider = await CommonJRPCProvider.getProviderInstance({ chainConfig: this.coreOptions.chainConfig as CustomChainConfig });
|
|
90
|
-
// TODO: get stuff from dashboard here
|
|
91
|
-
// disable sms login
|
|
92
|
-
const initPromises = Object.keys(this.walletAdapters).map((adapterName) => {
|
|
93
|
-
this.subscribeToAdapterEvents(this.walletAdapters[adapterName]);
|
|
94
|
-
// if adapter doesn't have any chain config yet then set it based on provided namespace and chainId.
|
|
95
|
-
// if no chainNamespace or chainId is being provided, it will connect with mainnet.
|
|
96
|
-
if (!this.walletAdapters[adapterName].chainConfigProxy) {
|
|
97
|
-
const providedChainConfig = this.coreOptions.chainConfig;
|
|
98
|
-
if (!providedChainConfig.chainNamespace) throw WalletInitializationError.invalidParams("Please provide chainNamespace in chainConfig");
|
|
99
|
-
this.walletAdapters[adapterName].setAdapterSettings({
|
|
100
|
-
chainConfig: providedChainConfig,
|
|
101
|
-
sessionTime: this.coreOptions.sessionTime,
|
|
102
|
-
clientId: this.coreOptions.clientId,
|
|
103
|
-
web3AuthNetwork: this.coreOptions.web3AuthNetwork,
|
|
104
|
-
useCoreKitKey: this.coreOptions.useCoreKitKey,
|
|
105
|
-
});
|
|
106
|
-
} else {
|
|
107
|
-
this.walletAdapters[adapterName].setAdapterSettings({
|
|
108
|
-
sessionTime: this.coreOptions.sessionTime,
|
|
109
|
-
clientId: this.coreOptions.clientId,
|
|
110
|
-
web3AuthNetwork: this.coreOptions.web3AuthNetwork,
|
|
111
|
-
useCoreKitKey: this.coreOptions.useCoreKitKey,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
if (adapterName === WALLET_ADAPTERS.OPENLOGIN) {
|
|
115
|
-
const openloginAdapter = this.walletAdapters[adapterName] as OpenloginAdapter;
|
|
116
|
-
if (this.coreOptions.privateKeyProvider) {
|
|
117
|
-
if (openloginAdapter.currentChainNamespace !== this.coreOptions.privateKeyProvider.currentChainConfig.chainNamespace) {
|
|
118
|
-
throw WalletInitializationError.incompatibleChainNameSpace(
|
|
119
|
-
"private key provider is not compatible with provided chainNamespace for openlogin adapter"
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
openloginAdapter.setAdapterSettings({ privateKeyProvider: this.coreOptions.privateKeyProvider });
|
|
123
|
-
}
|
|
124
|
-
openloginAdapter.setAdapterSettings({ whiteLabel: this.coreOptions.uiConfig });
|
|
125
|
-
if (!openloginAdapter.privateKeyProvider) {
|
|
126
|
-
throw WalletInitializationError.invalidParams("privateKeyProvider is required for openlogin adapter");
|
|
127
|
-
}
|
|
128
|
-
} else if (adapterName === WALLET_ADAPTERS.WALLET_CONNECT_V2) {
|
|
129
|
-
const walletConnectAdapter = this.walletAdapters[adapterName] as WalletConnectV2Adapter;
|
|
130
|
-
walletConnectAdapter.setAdapterSettings({
|
|
131
|
-
adapterSettings: {
|
|
132
|
-
walletConnectInitOptions: {
|
|
133
|
-
// Using a default wallet connect project id for web3auth modal integration
|
|
134
|
-
projectId: "d3c63f19f9582f8ba48e982057eb096b", // TODO: get from dashboard
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return this.walletAdapters[adapterName].init({ autoConnect: this.cachedAdapter === adapterName }).catch((e) => log.error(e));
|
|
141
|
-
});
|
|
142
|
-
this.status = ADAPTER_STATUS.READY;
|
|
143
|
-
await Promise.all(initPromises);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
public getAdapter(adapterName: WALLET_ADAPTER_TYPE): IAdapter<unknown> | null {
|
|
147
|
-
return this.walletAdapters[adapterName] || null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
public configureAdapter(adapter: IAdapter<unknown>): Web3AuthNoModal {
|
|
151
|
-
this.checkInitRequirements();
|
|
152
|
-
const providedChainConfig = this.coreOptions.chainConfig;
|
|
153
|
-
|
|
154
|
-
if (!providedChainConfig.chainNamespace) throw WalletInitializationError.invalidParams("Please provide chainNamespace in chainConfig");
|
|
155
|
-
|
|
156
|
-
const adapterAlreadyExists = this.walletAdapters[adapter.name];
|
|
157
|
-
if (adapterAlreadyExists) throw WalletInitializationError.duplicateAdapterError(`Wallet adapter for ${adapter.name} already exists`);
|
|
158
|
-
if (adapter.adapterNamespace !== ADAPTER_NAMESPACES.MULTICHAIN && adapter.adapterNamespace !== providedChainConfig.chainNamespace)
|
|
159
|
-
throw WalletInitializationError.incompatibleChainNameSpace(
|
|
160
|
-
`This wallet adapter belongs to ${adapter.adapterNamespace} which is incompatible with currently used namespace: ${providedChainConfig.chainNamespace}`
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
adapter.adapterNamespace === ADAPTER_NAMESPACES.MULTICHAIN &&
|
|
165
|
-
adapter.currentChainNamespace &&
|
|
166
|
-
providedChainConfig.chainNamespace !== adapter.currentChainNamespace
|
|
167
|
-
) {
|
|
168
|
-
// chainConfig checks are already validated in constructor so using typecast is safe here.
|
|
169
|
-
adapter.setAdapterSettings({ chainConfig: providedChainConfig as CustomChainConfig });
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
this.walletAdapters[adapter.name] = adapter;
|
|
173
|
-
return this;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
public clearCache() {
|
|
177
|
-
if (!storageAvailable(this.storage)) return;
|
|
178
|
-
window[this.storage].removeItem(ADAPTER_CACHE_KEY);
|
|
179
|
-
this.cachedAdapter = null;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
public async addChain(chainConfig: CustomChainConfig): Promise<void> {
|
|
183
|
-
if (this.commonJRPCProvider) {
|
|
184
|
-
return this.commonJRPCProvider.addChain(chainConfig);
|
|
185
|
-
}
|
|
186
|
-
throw WalletInitializationError.notReady(`No wallet is ready`);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
public async switchChain(params: { chainId: string }): Promise<void> {
|
|
190
|
-
if (this.commonJRPCProvider) {
|
|
191
|
-
return this.commonJRPCProvider.switchChain(params);
|
|
192
|
-
}
|
|
193
|
-
throw WalletInitializationError.notReady(`No wallet is ready`);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Connect to a specific wallet adapter
|
|
198
|
-
* @param walletName - Key of the walletAdapter to use.
|
|
199
|
-
*/
|
|
200
|
-
async connectTo<T>(walletName: WALLET_ADAPTER_TYPE, loginParams?: T): Promise<IProvider | null> {
|
|
201
|
-
if (!this.walletAdapters[walletName] || !this.commonJRPCProvider)
|
|
202
|
-
throw WalletInitializationError.notFound(`Please add wallet adapter for ${walletName} wallet, before connecting`);
|
|
203
|
-
const provider = await this.walletAdapters[walletName].connect(loginParams);
|
|
204
|
-
this.commonJRPCProvider.updateProviderEngineProxy((provider as IBaseProvider<unknown>).provider || provider);
|
|
205
|
-
return this.provider;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
async logout(options: { cleanup: boolean } = { cleanup: false }): Promise<void> {
|
|
209
|
-
if (this.status !== ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw WalletLoginError.notConnectedError(`No wallet is connected`);
|
|
210
|
-
await this.walletAdapters[this.connectedAdapterName].disconnect(options);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async getUserInfo(): Promise<Partial<UserInfo>> {
|
|
214
|
-
log.debug("Getting user info", this.status, this.connectedAdapterName);
|
|
215
|
-
if (this.status !== ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw WalletLoginError.notConnectedError(`No wallet is connected`);
|
|
216
|
-
return this.walletAdapters[this.connectedAdapterName].getUserInfo();
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
async enableMFA<T>(loginParams?: T): Promise<void> {
|
|
220
|
-
if (this.status !== ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw WalletLoginError.notConnectedError(`No wallet is connected`);
|
|
221
|
-
if (this.connectedAdapterName !== WALLET_ADAPTERS.OPENLOGIN)
|
|
222
|
-
throw WalletLoginError.unsupportedOperation(`EnableMFA is not supported for this adapter.`);
|
|
223
|
-
return this.walletAdapters[this.connectedAdapterName].enableMFA(loginParams);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
async authenticateUser(): Promise<UserAuthInfo> {
|
|
227
|
-
if (this.status !== ADAPTER_STATUS.CONNECTED || !this.connectedAdapterName) throw WalletLoginError.notConnectedError(`No wallet is connected`);
|
|
228
|
-
return this.walletAdapters[this.connectedAdapterName].authenticateUser();
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
public addPlugin(plugin: IPlugin): IWeb3Auth {
|
|
232
|
-
if (this.plugins[plugin.name]) throw new Error(`Plugin ${plugin.name} already exist`);
|
|
233
|
-
if (plugin.pluginNamespace !== PLUGIN_NAMESPACES.MULTICHAIN && plugin.pluginNamespace !== this.coreOptions.chainConfig.chainNamespace)
|
|
234
|
-
throw new Error(
|
|
235
|
-
`This plugin belongs to ${plugin.pluginNamespace} namespace which is incompatible with currently used namespace: ${this.coreOptions.chainConfig.chainNamespace}`
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
this.plugins[plugin.name] = plugin;
|
|
239
|
-
return this;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
protected subscribeToAdapterEvents(walletAdapter: IAdapter<unknown>): void {
|
|
243
|
-
walletAdapter.on(ADAPTER_EVENTS.CONNECTED, async (data: CONNECTED_EVENT_DATA) => {
|
|
244
|
-
if (!this.commonJRPCProvider) throw WalletInitializationError.notFound(`CommonJrpcProvider not found`);
|
|
245
|
-
const { provider } = this.walletAdapters[data.adapter];
|
|
246
|
-
this.commonJRPCProvider.updateProviderEngineProxy((provider as IBaseProvider<unknown>).provider || provider);
|
|
247
|
-
this.status = ADAPTER_STATUS.CONNECTED;
|
|
248
|
-
this.connectedAdapterName = data.adapter;
|
|
249
|
-
this.cacheWallet(data.adapter);
|
|
250
|
-
log.debug("connected", this.status, this.connectedAdapterName);
|
|
251
|
-
|
|
252
|
-
Object.values(this.plugins).map(async (plugin) => {
|
|
253
|
-
try {
|
|
254
|
-
if (!plugin.SUPPORTED_ADAPTERS.includes(data.adapter)) {
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
await plugin.initWithWeb3Auth(this);
|
|
258
|
-
await plugin.connect();
|
|
259
|
-
} catch (error: unknown) {
|
|
260
|
-
// swallow error if connector adapter doesn't supports this plugin.
|
|
261
|
-
if ((error as Web3AuthError).code === 5211) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
log.error(error);
|
|
265
|
-
}
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
this.emit(ADAPTER_EVENTS.CONNECTED, { ...data } as CONNECTED_EVENT_DATA);
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
walletAdapter.on(ADAPTER_EVENTS.DISCONNECTED, async (data) => {
|
|
272
|
-
// get back to ready state for rehydrating.
|
|
273
|
-
this.status = ADAPTER_STATUS.READY;
|
|
274
|
-
if (storageAvailable(this.storage)) {
|
|
275
|
-
const cachedAdapter = window[this.storage].getItem(ADAPTER_CACHE_KEY);
|
|
276
|
-
if (this.connectedAdapterName === cachedAdapter) {
|
|
277
|
-
this.clearCache();
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
log.debug("disconnected", this.status, this.connectedAdapterName);
|
|
282
|
-
await Promise.all(
|
|
283
|
-
Object.values(this.plugins).map((plugin) => {
|
|
284
|
-
return plugin.disconnect().catch((error: Web3AuthError) => {
|
|
285
|
-
// swallow error if adapter doesn't supports this plugin.
|
|
286
|
-
if (error.code === 5211) {
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
// throw error;
|
|
290
|
-
log.error(error);
|
|
291
|
-
});
|
|
292
|
-
})
|
|
293
|
-
);
|
|
294
|
-
this.connectedAdapterName = null;
|
|
295
|
-
this.emit(ADAPTER_EVENTS.DISCONNECTED, data);
|
|
296
|
-
});
|
|
297
|
-
walletAdapter.on(ADAPTER_EVENTS.CONNECTING, (data) => {
|
|
298
|
-
this.status = ADAPTER_STATUS.CONNECTING;
|
|
299
|
-
this.emit(ADAPTER_EVENTS.CONNECTING, data);
|
|
300
|
-
log.debug("connecting", this.status, this.connectedAdapterName);
|
|
301
|
-
});
|
|
302
|
-
walletAdapter.on(ADAPTER_EVENTS.ERRORED, (data) => {
|
|
303
|
-
this.status = ADAPTER_STATUS.ERRORED;
|
|
304
|
-
this.clearCache();
|
|
305
|
-
this.emit(ADAPTER_EVENTS.ERRORED, data);
|
|
306
|
-
log.debug("errored", this.status, this.connectedAdapterName);
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
walletAdapter.on(ADAPTER_EVENTS.ADAPTER_DATA_UPDATED, (data) => {
|
|
310
|
-
log.debug("adapter data updated", data);
|
|
311
|
-
this.emit(ADAPTER_EVENTS.ADAPTER_DATA_UPDATED, data);
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
walletAdapter.on(ADAPTER_EVENTS.CACHE_CLEAR, (data) => {
|
|
315
|
-
log.debug("adapter cache clear", data);
|
|
316
|
-
if (storageAvailable(this.storage)) {
|
|
317
|
-
this.clearCache();
|
|
318
|
-
}
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
protected checkInitRequirements(): void {
|
|
323
|
-
if (this.status === ADAPTER_STATUS.CONNECTING) throw WalletInitializationError.notReady("Already pending connection");
|
|
324
|
-
if (this.status === ADAPTER_STATUS.CONNECTED) throw WalletInitializationError.notReady("Already connected");
|
|
325
|
-
if (this.status === ADAPTER_STATUS.READY) throw WalletInitializationError.notReady("Adapter is already initialized");
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
private cacheWallet(walletName: string) {
|
|
329
|
-
if (!storageAvailable(this.storage)) return;
|
|
330
|
-
window[this.storage].setItem(ADAPTER_CACHE_KEY, walletName);
|
|
331
|
-
this.cachedAdapter = walletName;
|
|
332
|
-
}
|
|
333
|
-
}
|