@web3auth/modal 9.7.0 → 10.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/dist/lib.cjs/node_modules/style-inject/dist/style-inject.es.js +28 -0
- package/dist/lib.cjs/{config.js → packages/modal/src/config.js} +17 -29
- package/dist/lib.cjs/{index.js → packages/modal/src/index.js} +7 -0
- package/dist/lib.cjs/packages/modal/src/modalManager.js +376 -0
- package/dist/lib.cjs/packages/modal/src/react/Web3AuthProvider.js +15 -0
- package/dist/lib.cjs/packages/modal/src/react/context/Web3AuthInnerContext.js +199 -0
- package/dist/lib.cjs/packages/modal/src/react/hooks/useWeb3Auth.js +15 -0
- package/dist/lib.cjs/packages/modal/src/react/index.js +12 -0
- package/dist/lib.cjs/packages/modal/src/ui/assets/arrow-left-dark.svg.js +7 -0
- package/dist/lib.cjs/packages/modal/src/ui/assets/arrow-left-light.svg.js +7 -0
- package/dist/lib.cjs/packages/modal/src/ui/assets/x-dark.svg.js +7 -0
- package/dist/lib.cjs/packages/modal/src/ui/assets/x-light.svg.js +7 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/AdapterLoader.js +108 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Button/Button.js +34 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Button/styles.css.js +8 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/ExternalWallet/ExternalWalletButton.js +44 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/ExternalWallet/ExternalWalletConnect.js +67 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/ExternalWallet/ExternalWalletDetails.js +34 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/ExternalWallet/ExternalWalletHeader.js +46 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/ExternalWallet/ExternalWalletInstall.js +167 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/ExternalWallets.js +273 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Footer.js +20 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Header.js +81 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Icon.js +73 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Image.js +51 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Loader.js +59 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/Modal.js +244 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/SelfCustodyViaWeb3Auth.js +30 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/SocialLoginPasswordless.js +141 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/SocialLogins.js +148 -0
- package/dist/lib.cjs/packages/modal/src/ui/components/WalletConnect.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/config.js +17 -0
- package/dist/lib.cjs/packages/modal/src/ui/context/ThemeContext.js +9 -0
- package/dist/lib.cjs/packages/modal/src/ui/css/web3auth.css.js +8 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/dutch.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/english.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/french.json.js +82 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/german.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/japanese.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/korean.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/mandarin.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/portuguese.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/spanish.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/i18n/turkish.json.js +84 -0
- package/dist/lib.cjs/packages/modal/src/ui/interfaces.js +26 -0
- package/dist/lib.cjs/packages/modal/src/ui/localeImport.js +25 -0
- package/dist/lib.cjs/packages/modal/src/ui/loginModal.js +309 -0
- package/dist/lib.cjs/packages/modal/src/ui/utils.js +71 -0
- package/dist/lib.cjs/packages/modal/src/vue/Web3AuthProvider.js +208 -0
- package/dist/lib.cjs/packages/modal/src/vue/composables/useWeb3Auth.js +12 -0
- package/dist/lib.cjs/packages/modal/src/vue/index.js +9 -0
- package/dist/lib.esm/node_modules/style-inject/dist/style-inject.es.js +26 -0
- package/dist/lib.esm/{config.js → packages/modal/src/config.js} +12 -24
- package/dist/lib.esm/{index.js → packages/modal/src/index.js} +1 -0
- package/dist/lib.esm/packages/modal/src/modalManager.js +374 -0
- package/dist/lib.esm/packages/modal/src/react/Web3AuthProvider.js +13 -0
- package/dist/lib.esm/packages/modal/src/react/context/Web3AuthInnerContext.js +196 -0
- package/dist/lib.esm/packages/modal/src/react/hooks/useWeb3Auth.js +13 -0
- package/dist/lib.esm/packages/modal/src/react/index.js +3 -0
- package/dist/lib.esm/packages/modal/src/ui/assets/arrow-left-dark.svg.js +5 -0
- package/dist/lib.esm/packages/modal/src/ui/assets/arrow-left-light.svg.js +5 -0
- package/dist/lib.esm/packages/modal/src/ui/assets/x-dark.svg.js +5 -0
- package/dist/lib.esm/packages/modal/src/ui/assets/x-light.svg.js +5 -0
- package/dist/lib.esm/packages/modal/src/ui/components/AdapterLoader.js +106 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Button/Button.js +32 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Button/styles.css.js +6 -0
- package/dist/lib.esm/packages/modal/src/ui/components/ExternalWallet/ExternalWalletButton.js +42 -0
- package/dist/lib.esm/packages/modal/src/ui/components/ExternalWallet/ExternalWalletConnect.js +65 -0
- package/dist/lib.esm/packages/modal/src/ui/components/ExternalWallet/ExternalWalletDetails.js +32 -0
- package/dist/lib.esm/packages/modal/src/ui/components/ExternalWallet/ExternalWalletHeader.js +44 -0
- package/dist/lib.esm/packages/modal/src/ui/components/ExternalWallet/ExternalWalletInstall.js +165 -0
- package/dist/lib.esm/packages/modal/src/ui/components/ExternalWallets.js +271 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Footer.js +18 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Header.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Icon.js +71 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Image.js +49 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Loader.js +57 -0
- package/dist/lib.esm/packages/modal/src/ui/components/Modal.js +242 -0
- package/dist/lib.esm/packages/modal/src/ui/components/SelfCustodyViaWeb3Auth.js +28 -0
- package/dist/lib.esm/packages/modal/src/ui/components/SocialLoginPasswordless.js +139 -0
- package/dist/lib.esm/packages/modal/src/ui/components/SocialLogins.js +146 -0
- package/dist/lib.esm/packages/modal/src/ui/components/WalletConnect.js +82 -0
- package/dist/lib.esm/packages/modal/src/ui/config.js +13 -0
- package/dist/lib.esm/packages/modal/src/ui/context/ThemeContext.js +7 -0
- package/dist/lib.esm/packages/modal/src/ui/css/web3auth.css.js +6 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/dutch.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/english.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/french.json.js +77 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/german.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/japanese.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/korean.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/mandarin.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/portuguese.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/spanish.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/i18n/turkish.json.js +79 -0
- package/dist/lib.esm/packages/modal/src/ui/interfaces.js +20 -0
- package/dist/lib.esm/packages/modal/src/ui/localeImport.js +23 -0
- package/dist/lib.esm/packages/modal/src/ui/loginModal.js +307 -0
- package/dist/lib.esm/packages/modal/src/ui/utils.js +65 -0
- package/dist/lib.esm/packages/modal/src/vue/Web3AuthProvider.js +206 -0
- package/dist/lib.esm/packages/modal/src/vue/composables/useWeb3Auth.js +10 -0
- package/dist/lib.esm/packages/modal/src/vue/index.js +2 -0
- package/dist/modal.cjs.js +5676 -470
- package/dist/modal.umd.min.js +1 -1
- package/dist/modal.umd.min.js.LICENSE.txt +45 -0
- package/dist/types/config.d.ts +6 -6
- package/dist/types/index.d.ts +1 -0
- package/dist/types/interface.d.ts +5 -5
- package/dist/types/modalManager.d.ts +11 -12
- package/dist/types/react/Web3AuthProvider.d.ts +3 -0
- package/dist/types/react/context/Web3AuthInnerContext.d.ts +4 -0
- package/dist/types/react/context/index.d.ts +1 -0
- package/dist/types/react/hooks/index.d.ts +1 -0
- package/dist/types/react/hooks/useWeb3Auth.d.ts +2 -0
- package/dist/types/react/index.d.ts +4 -0
- package/dist/types/react/interfaces.d.ts +18 -0
- package/dist/types/ui/components/AdapterLoader.d.ts +11 -0
- package/dist/types/ui/components/Button/Button.d.ts +16 -0
- package/dist/types/ui/components/Button/index.d.ts +1 -0
- package/dist/types/ui/components/ExternalWallet/ExternalWalletButton.d.ts +7 -0
- package/dist/types/ui/components/ExternalWallet/ExternalWalletConnect.d.ts +9 -0
- package/dist/types/ui/components/ExternalWallet/ExternalWalletDetails.d.ts +9 -0
- package/dist/types/ui/components/ExternalWallet/ExternalWalletHeader.d.ts +8 -0
- package/dist/types/ui/components/ExternalWallet/ExternalWalletInstall.d.ts +8 -0
- package/dist/types/ui/components/ExternalWallets.d.ts +17 -0
- package/dist/types/ui/components/Footer.d.ts +1 -0
- package/dist/types/ui/components/Header.d.ts +8 -0
- package/dist/types/ui/components/Icon.d.ts +9 -0
- package/dist/types/ui/components/Image.d.ts +12 -0
- package/dist/types/ui/components/Loader.d.ts +10 -0
- package/dist/types/ui/components/Modal.d.ts +16 -0
- package/dist/types/ui/components/SelfCustodyViaWeb3Auth.d.ts +1 -0
- package/dist/types/ui/components/SocialLoginPasswordless.d.ts +16 -0
- package/dist/types/ui/components/SocialLogins.d.ts +14 -0
- package/dist/types/ui/components/WalletConnect.d.ts +8 -0
- package/dist/types/ui/config.d.ts +4 -0
- package/dist/types/ui/context/ThemeContext.d.ts +3 -0
- package/dist/types/ui/helper/countryData.d.ts +7 -0
- package/dist/types/ui/i18n/index.d.ts +11 -0
- package/dist/types/ui/index.d.ts +4 -0
- package/dist/types/ui/interfaces.d.ts +102 -0
- package/dist/types/ui/localeImport.d.ts +3 -0
- package/dist/types/ui/loginModal.d.ts +27 -0
- package/dist/types/ui/utils.d.ts +12 -0
- package/dist/types/vue/Web3AuthProvider.d.ts +13 -0
- package/dist/types/vue/composables/index.d.ts +1 -0
- package/dist/types/vue/composables/useWeb3Auth.d.ts +2 -0
- package/dist/types/vue/index.d.ts +3 -0
- package/dist/types/vue/interfaces.d.ts +42 -0
- package/package.json +100 -43
- package/dist/lib.cjs/modalManager.js +0 -441
- package/dist/lib.esm/modalManager.js +0 -439
- package/dist/modal.esm.js +0 -513
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SOLANA_CONNECTORS, CHAIN_NAMESPACES, EVM_CONNECTORS } from '@web3auth/no-modal';
|
|
2
2
|
|
|
3
3
|
const defaultSolanaDappModalConfig = {
|
|
4
4
|
chainNamespace: CHAIN_NAMESPACES.SOLANA,
|
|
5
|
-
|
|
6
|
-
[
|
|
7
|
-
label: "Torus Wallet",
|
|
8
|
-
showOnModal: true,
|
|
9
|
-
showOnMobile: true,
|
|
10
|
-
showOnDesktop: true
|
|
11
|
-
},
|
|
12
|
-
[SOLANA_ADAPTERS.AUTH]: {
|
|
5
|
+
connectors: {
|
|
6
|
+
[SOLANA_CONNECTORS.AUTH]: {
|
|
13
7
|
label: "Auth",
|
|
14
8
|
showOnModal: true,
|
|
15
9
|
showOnMobile: true,
|
|
@@ -19,20 +13,14 @@ const defaultSolanaDappModalConfig = {
|
|
|
19
13
|
};
|
|
20
14
|
const defaultEvmDappModalConfig = {
|
|
21
15
|
chainNamespace: CHAIN_NAMESPACES.EIP155,
|
|
22
|
-
|
|
23
|
-
[
|
|
24
|
-
label: "Torus Wallet",
|
|
25
|
-
showOnModal: true,
|
|
26
|
-
showOnMobile: true,
|
|
27
|
-
showOnDesktop: true
|
|
28
|
-
},
|
|
29
|
-
[EVM_ADAPTERS.AUTH]: {
|
|
16
|
+
connectors: {
|
|
17
|
+
[EVM_CONNECTORS.AUTH]: {
|
|
30
18
|
label: "Auth",
|
|
31
19
|
showOnModal: true,
|
|
32
20
|
showOnMobile: true,
|
|
33
21
|
showOnDesktop: true
|
|
34
22
|
},
|
|
35
|
-
[
|
|
23
|
+
[EVM_CONNECTORS.WALLET_CONNECT_V2]: {
|
|
36
24
|
label: "Wallet Connect",
|
|
37
25
|
showOnModal: true,
|
|
38
26
|
showOnMobile: true,
|
|
@@ -42,8 +30,8 @@ const defaultEvmDappModalConfig = {
|
|
|
42
30
|
};
|
|
43
31
|
const defaultSolanaWalletModalConfig = {
|
|
44
32
|
chainNamespace: CHAIN_NAMESPACES.SOLANA,
|
|
45
|
-
|
|
46
|
-
[
|
|
33
|
+
connectors: {
|
|
34
|
+
[SOLANA_CONNECTORS.AUTH]: {
|
|
47
35
|
label: "Auth",
|
|
48
36
|
showOnModal: true,
|
|
49
37
|
showOnMobile: true,
|
|
@@ -53,8 +41,8 @@ const defaultSolanaWalletModalConfig = {
|
|
|
53
41
|
};
|
|
54
42
|
const defaultEvmWalletModalConfig = {
|
|
55
43
|
chainNamespace: CHAIN_NAMESPACES.EIP155,
|
|
56
|
-
|
|
57
|
-
[
|
|
44
|
+
connectors: {
|
|
45
|
+
[EVM_CONNECTORS.AUTH]: {
|
|
58
46
|
label: "Auth",
|
|
59
47
|
showOnModal: true,
|
|
60
48
|
showOnMobile: true,
|
|
@@ -64,8 +52,8 @@ const defaultEvmWalletModalConfig = {
|
|
|
64
52
|
};
|
|
65
53
|
const defaultOtherModalConfig = {
|
|
66
54
|
chainNamespace: CHAIN_NAMESPACES.OTHER,
|
|
67
|
-
|
|
68
|
-
[
|
|
55
|
+
connectors: {
|
|
56
|
+
[EVM_CONNECTORS.AUTH]: {
|
|
69
57
|
label: "Auth",
|
|
70
58
|
showOnModal: true,
|
|
71
59
|
showOnMobile: true,
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { defaultEvmDappModalConfig, defaultEvmWalletModalConfig, defaultOtherModalConfig, defaultSolanaDappModalConfig, defaultSolanaWalletModalConfig, walletRegistryUrl } from './config.js';
|
|
2
2
|
export { Web3Auth } from './modalManager.js';
|
|
3
|
+
export * from '@web3auth/no-modal';
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import { Web3AuthNoModal, cloneDeep, CONNECTOR_EVENTS, WalletInitializationError, CONNECTOR_STATUS, fetchProjectConfig, log, fetchWalletRegistry, WALLET_CONNECTORS, LOGIN_PROVIDER, CONNECTOR_NAMES, CONNECTOR_CATEGORY } from '@web3auth/no-modal';
|
|
4
|
+
import deepmerge from 'deepmerge';
|
|
5
|
+
import { defaultOtherModalConfig, walletRegistryUrl } from './config.js';
|
|
6
|
+
import { capitalizeFirstLetter, AUTH_PROVIDERS } from './ui/config.js';
|
|
7
|
+
import { LOGIN_MODAL_EVENTS } from './ui/interfaces.js';
|
|
8
|
+
import { LoginModal } from './ui/loginModal.js';
|
|
9
|
+
import { getUserLanguage, getConnectorSocialLogins } from './ui/utils.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
|
+
}
|
|
20
|
+
setModalConfig(modalConfig) {
|
|
21
|
+
super.checkInitRequirements();
|
|
22
|
+
this.modalConfig = modalConfig;
|
|
23
|
+
}
|
|
24
|
+
async initModal(params) {
|
|
25
|
+
super.checkInitRequirements();
|
|
26
|
+
super.initCachedConnectorAndChainId();
|
|
27
|
+
// get project config and wallet registry
|
|
28
|
+
const {
|
|
29
|
+
projectConfig,
|
|
30
|
+
walletRegistry
|
|
31
|
+
} = await this.getProjectAndWalletConfig(params);
|
|
32
|
+
this.options.uiConfig = deepmerge(cloneDeep(projectConfig.whitelabel || {}), this.options.uiConfig || {});
|
|
33
|
+
if (!this.options.uiConfig.defaultLanguage) this.options.uiConfig.defaultLanguage = getUserLanguage(this.options.uiConfig.defaultLanguage);
|
|
34
|
+
if (!this.options.uiConfig.mode) this.options.uiConfig.mode = "light";
|
|
35
|
+
|
|
36
|
+
// init login modal
|
|
37
|
+
this.loginModal = new LoginModal(_objectSpread(_objectSpread({}, this.options.uiConfig), {}, {
|
|
38
|
+
connectorListener: this,
|
|
39
|
+
chainNamespace: this.currentChain.chainNamespace,
|
|
40
|
+
walletRegistry
|
|
41
|
+
}));
|
|
42
|
+
this.subscribeToLoginModalEvents();
|
|
43
|
+
await this.loginModal.initModal();
|
|
44
|
+
|
|
45
|
+
// setup common JRPC provider
|
|
46
|
+
await this.setupCommonJRPCProvider();
|
|
47
|
+
|
|
48
|
+
// initialize connectors
|
|
49
|
+
this.on(CONNECTOR_EVENTS.CONNECTORS_UPDATED, ({
|
|
50
|
+
connectors: newConnectors
|
|
51
|
+
}) => this.initConnectors({
|
|
52
|
+
connectors: newConnectors,
|
|
53
|
+
projectConfig,
|
|
54
|
+
modalConfig: params
|
|
55
|
+
}));
|
|
56
|
+
await this.loadConnectors({
|
|
57
|
+
projectConfig
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// initialize plugins
|
|
61
|
+
await this.initPlugins();
|
|
62
|
+
}
|
|
63
|
+
async connect() {
|
|
64
|
+
if (!this.loginModal) throw WalletInitializationError.notReady("Login modal is not initialized");
|
|
65
|
+
// if already connected return provider
|
|
66
|
+
if (this.connectedConnectorName && this.status === CONNECTOR_STATUS.CONNECTED && this.provider) return this.provider;
|
|
67
|
+
this.loginModal.open();
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
this.once(CONNECTOR_EVENTS.CONNECTED, () => {
|
|
70
|
+
return resolve(this.provider);
|
|
71
|
+
});
|
|
72
|
+
this.once(CONNECTOR_EVENTS.ERRORED, err => {
|
|
73
|
+
return reject(err);
|
|
74
|
+
});
|
|
75
|
+
this.once(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, visibility => {
|
|
76
|
+
// modal is closed but user is not connected to any wallet.
|
|
77
|
+
if (!visibility && this.status !== CONNECTOR_STATUS.CONNECTED) {
|
|
78
|
+
return reject(new Error("User closed the modal"));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async getProjectAndWalletConfig(params) {
|
|
84
|
+
// get project config
|
|
85
|
+
let projectConfig;
|
|
86
|
+
try {
|
|
87
|
+
var _this$options$account;
|
|
88
|
+
projectConfig = await fetchProjectConfig(this.options.clientId, this.options.web3AuthNetwork, (_this$options$account = this.options.accountAbstractionConfig) === null || _this$options$account === void 0 ? void 0 : _this$options$account.smartAccountType);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
log.error("Failed to fetch project configurations", e);
|
|
91
|
+
throw WalletInitializationError.notReady("failed to fetch project configurations", e);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// get wallet registry
|
|
95
|
+
let walletRegistry = {
|
|
96
|
+
others: {},
|
|
97
|
+
default: {}
|
|
98
|
+
};
|
|
99
|
+
if (!(params !== null && params !== void 0 && params.hideWalletDiscovery)) {
|
|
100
|
+
try {
|
|
101
|
+
walletRegistry = await fetchWalletRegistry(walletRegistryUrl);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
log.error("Failed to fetch wallet registry", e);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
projectConfig,
|
|
108
|
+
walletRegistry
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async initConnectors({
|
|
112
|
+
connectors,
|
|
113
|
+
projectConfig,
|
|
114
|
+
modalConfig
|
|
115
|
+
}) {
|
|
116
|
+
// filter connectors based on config
|
|
117
|
+
const filteredConnectorNames = await this.filterConnectors(modalConfig, projectConfig);
|
|
118
|
+
|
|
119
|
+
// initialize connectors based on availability
|
|
120
|
+
const {
|
|
121
|
+
hasInAppConnectors,
|
|
122
|
+
hasExternalConnectors
|
|
123
|
+
} = await this.checkConnectorAvailability(filteredConnectorNames, modalConfig);
|
|
124
|
+
if (hasInAppConnectors) {
|
|
125
|
+
await this.initInAppAndCachedConnectors(connectors, filteredConnectorNames);
|
|
126
|
+
// show connect button if external wallets are available
|
|
127
|
+
if (hasExternalConnectors) this.loginModal.initExternalWalletContainer();
|
|
128
|
+
} else if (hasExternalConnectors) {
|
|
129
|
+
// if no in app wallet is available then initialize external wallets in modal
|
|
130
|
+
await this.initExternalConnectors(connectors, false, {
|
|
131
|
+
showExternalWalletsOnly: true
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// emit ready event if connector is ready
|
|
136
|
+
if (this.status === CONNECTOR_STATUS.NOT_READY) {
|
|
137
|
+
this.status = CONNECTOR_STATUS.READY;
|
|
138
|
+
this.emit(CONNECTOR_EVENTS.READY);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async filterConnectors(params, projectConfig) {
|
|
142
|
+
// update auth connector config
|
|
143
|
+
const {
|
|
144
|
+
sms_otp_enabled: smsOtpEnabled
|
|
145
|
+
} = projectConfig;
|
|
146
|
+
if (smsOtpEnabled !== undefined) {
|
|
147
|
+
var _params, _params$modalConfig$W;
|
|
148
|
+
// TODO: use the new login config method
|
|
149
|
+
const connectorConfig = {
|
|
150
|
+
[WALLET_CONNECTORS.AUTH]: {
|
|
151
|
+
label: WALLET_CONNECTORS.AUTH,
|
|
152
|
+
loginMethods: {
|
|
153
|
+
[LOGIN_PROVIDER.SMS_PASSWORDLESS]: {
|
|
154
|
+
name: LOGIN_PROVIDER.SMS_PASSWORDLESS,
|
|
155
|
+
showOnModal: smsOtpEnabled,
|
|
156
|
+
showOnDesktop: smsOtpEnabled,
|
|
157
|
+
showOnMobile: smsOtpEnabled
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
if (!((_params = params) !== null && _params !== void 0 && _params.modalConfig)) params = {
|
|
163
|
+
modalConfig: {}
|
|
164
|
+
};
|
|
165
|
+
const localSmsOtpEnabled = (_params$modalConfig$W = params.modalConfig[WALLET_CONNECTORS.AUTH]) === null || _params$modalConfig$W === void 0 || (_params$modalConfig$W = _params$modalConfig$W.loginMethods) === null || _params$modalConfig$W === void 0 || (_params$modalConfig$W = _params$modalConfig$W[LOGIN_PROVIDER.SMS_PASSWORDLESS]) === null || _params$modalConfig$W === void 0 ? void 0 : _params$modalConfig$W.showOnModal;
|
|
166
|
+
if (localSmsOtpEnabled === true && smsOtpEnabled === false) {
|
|
167
|
+
throw WalletInitializationError.invalidParams("must enable sms otp on dashboard in order to utilise it");
|
|
168
|
+
}
|
|
169
|
+
params.modalConfig = deepmerge(connectorConfig, cloneDeep(params.modalConfig));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// merge default connectors with the custom configured connectors.
|
|
173
|
+
const allConnectorNames = [...new Set([...Object.keys(this.modalConfig.connectors || {}), ...this.connectors.map(connector => connector.name)])];
|
|
174
|
+
const connectorConfigurationPromises = allConnectorNames.map(async connectorName => {
|
|
175
|
+
var _this$modalConfig$con, _params2, _this$modalConfig$con2, _this$modalConfig$con3;
|
|
176
|
+
// start with the default config of connector.
|
|
177
|
+
let connectorConfig = ((_this$modalConfig$con = this.modalConfig.connectors) === null || _this$modalConfig$con === void 0 ? void 0 : _this$modalConfig$con[connectorName]) || {
|
|
178
|
+
label: CONNECTOR_NAMES[connectorName] || connectorName.split("-").map(capitalizeFirstLetter).join(" "),
|
|
179
|
+
showOnModal: true,
|
|
180
|
+
showOnMobile: true,
|
|
181
|
+
showOnDesktop: true
|
|
182
|
+
};
|
|
183
|
+
// override the default config of connector if some config is being provided by the user.
|
|
184
|
+
if ((_params2 = params) !== null && _params2 !== void 0 && (_params2 = _params2.modalConfig) !== null && _params2 !== void 0 && _params2[connectorName]) {
|
|
185
|
+
connectorConfig = _objectSpread(_objectSpread({}, connectorConfig), params.modalConfig[connectorName]);
|
|
186
|
+
}
|
|
187
|
+
this.modalConfig.connectors[connectorName] = connectorConfig;
|
|
188
|
+
|
|
189
|
+
// check if connector is configured/added by user and exist in connectors map.
|
|
190
|
+
const connector = this.getConnector(connectorName);
|
|
191
|
+
log.debug("connector config", connectorName, (_this$modalConfig$con2 = this.modalConfig.connectors) === null || _this$modalConfig$con2 === void 0 ? void 0 : _this$modalConfig$con2[connectorName].showOnModal, connector);
|
|
192
|
+
|
|
193
|
+
// if connector is not custom configured then check if it is available in default connectors.
|
|
194
|
+
// and if connector is not hidden by user
|
|
195
|
+
if (!connector && (_this$modalConfig$con3 = this.modalConfig.connectors) !== null && _this$modalConfig$con3 !== void 0 && _this$modalConfig$con3[connectorName].showOnModal) {
|
|
196
|
+
throw WalletInitializationError.invalidParams(`Connector ${connectorName} is not configured`);
|
|
197
|
+
} else if ((connector === null || connector === void 0 ? void 0 : connector.type) === CONNECTOR_CATEGORY.IN_APP || (connector === null || connector === void 0 ? void 0 : connector.type) === CONNECTOR_CATEGORY.EXTERNAL || connectorName === this.cachedConnector) {
|
|
198
|
+
var _this$modalConfig$con4;
|
|
199
|
+
if (!((_this$modalConfig$con4 = this.modalConfig.connectors) !== null && _this$modalConfig$con4 !== void 0 && _this$modalConfig$con4[connectorName].showOnModal)) return;
|
|
200
|
+
if (connectorName === WALLET_CONNECTORS.WALLET_CONNECT_V2) {
|
|
201
|
+
const {
|
|
202
|
+
wallet_connect_enabled: walletConnectEnabled
|
|
203
|
+
} = projectConfig;
|
|
204
|
+
if (walletConnectEnabled === false) {
|
|
205
|
+
var _this$modalConfig$con5, _this$modalConfig$con6, _this$modalConfig$con7;
|
|
206
|
+
// override user specified config by hiding wallet connect
|
|
207
|
+
this.modalConfig.connectors = _objectSpread(_objectSpread({}, (_this$modalConfig$con5 = this.modalConfig.connectors) !== null && _this$modalConfig$con5 !== void 0 ? _this$modalConfig$con5 : {}), {}, {
|
|
208
|
+
[WALLET_CONNECTORS.WALLET_CONNECT_V2]: _objectSpread(_objectSpread({}, (_this$modalConfig$con6 = (_this$modalConfig$con7 = this.modalConfig.connectors) === null || _this$modalConfig$con7 === void 0 ? void 0 : _this$modalConfig$con7[WALLET_CONNECTORS.WALLET_CONNECT_V2]) !== null && _this$modalConfig$con6 !== void 0 ? _this$modalConfig$con6 : {}), {}, {
|
|
209
|
+
showOnModal: false
|
|
210
|
+
})
|
|
211
|
+
});
|
|
212
|
+
this.modalConfig.connectors[WALLET_CONNECTORS.WALLET_CONNECT_V2].showOnModal = false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return connectorName;
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
const connectorNames = await Promise.all(connectorConfigurationPromises);
|
|
219
|
+
return connectorNames.filter(name => name !== undefined);
|
|
220
|
+
}
|
|
221
|
+
async checkConnectorAvailability(connectorNames, modalConfig) {
|
|
222
|
+
// currently all default in app and external wallets can be hidden or shown based on config.
|
|
223
|
+
// check if in app connectors are available
|
|
224
|
+
const hasInAppConnectors = this.connectors.some(connector => {
|
|
225
|
+
var _this$modalConfig$con8, _this$modalConfig$con9, _connector$name;
|
|
226
|
+
if (connector.type !== CONNECTOR_CATEGORY.IN_APP) return false;
|
|
227
|
+
if (((_this$modalConfig$con8 = this.modalConfig.connectors) === null || _this$modalConfig$con8 === void 0 || (_this$modalConfig$con8 = _this$modalConfig$con8[connector.name]) === null || _this$modalConfig$con8 === void 0 ? void 0 : _this$modalConfig$con8.showOnModal) !== true) return false;
|
|
228
|
+
if (!((_this$modalConfig$con9 = this.modalConfig.connectors) !== null && _this$modalConfig$con9 !== void 0 && (_this$modalConfig$con9 = _this$modalConfig$con9[connector.name]) !== null && _this$modalConfig$con9 !== void 0 && _this$modalConfig$con9.loginMethods)) return true;
|
|
229
|
+
const mergedLoginMethods = getConnectorSocialLogins(connector.name, (_connector$name = this.modalConfig.connectors[connector.name]) === null || _connector$name === void 0 ? void 0 : _connector$name.loginMethods);
|
|
230
|
+
if (Object.values(mergedLoginMethods).some(method => method.showOnModal)) return true;
|
|
231
|
+
return false;
|
|
232
|
+
});
|
|
233
|
+
log.debug(hasInAppConnectors, this.connectors, connectorNames, "hasInAppWallets");
|
|
234
|
+
|
|
235
|
+
// check if external connectors are available
|
|
236
|
+
const hasExternalConnectors = connectorNames.some(connectorName => {
|
|
237
|
+
var _this$getConnector, _this$modalConfig$con10;
|
|
238
|
+
// if wallet connect connector is available but hideWalletDiscovery is true then don't consider it as external wallet
|
|
239
|
+
if (connectorName === WALLET_CONNECTORS.WALLET_CONNECT_V2 && modalConfig !== null && modalConfig !== void 0 && modalConfig.hideWalletDiscovery) return false;
|
|
240
|
+
return ((_this$getConnector = this.getConnector(connectorName)) === null || _this$getConnector === void 0 ? void 0 : _this$getConnector.type) === CONNECTOR_CATEGORY.EXTERNAL && ((_this$modalConfig$con10 = this.modalConfig.connectors) === null || _this$modalConfig$con10 === void 0 ? void 0 : _this$modalConfig$con10[connectorName].showOnModal);
|
|
241
|
+
});
|
|
242
|
+
return {
|
|
243
|
+
hasInAppConnectors,
|
|
244
|
+
hasExternalConnectors
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
async initInAppAndCachedConnectors(connectors, connectorNames) {
|
|
248
|
+
await Promise.all(connectorNames.map(async connectorName => {
|
|
249
|
+
const connector = connectors.find(x => x.name === connectorName);
|
|
250
|
+
if (!connector) return;
|
|
251
|
+
try {
|
|
252
|
+
// skip if connector is already initialized
|
|
253
|
+
if (connector.status !== CONNECTOR_STATUS.NOT_READY) return;
|
|
254
|
+
|
|
255
|
+
// only initialize a external connectors here if it is a cached connector.
|
|
256
|
+
if (this.cachedConnector !== connectorName && connector.type === CONNECTOR_CATEGORY.EXTERNAL) return;
|
|
257
|
+
|
|
258
|
+
// in-app wallets or cached wallet (being connected or already connected) are initialized first.
|
|
259
|
+
// if connector is configured then only initialize in app or cached connector.
|
|
260
|
+
// external wallets are initialized on INIT_EXTERNAL_WALLET event.
|
|
261
|
+
this.subscribeToConnectorEvents(connector);
|
|
262
|
+
const initialChain = this.getInitialChainIdForConnector(connector);
|
|
263
|
+
await connector.init({
|
|
264
|
+
autoConnect: this.cachedConnector === connectorName,
|
|
265
|
+
chainId: initialChain.chainId
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// note: not adding cachedWallet to modal if it is external wallet.
|
|
269
|
+
// adding it later if no in-app wallets are available.
|
|
270
|
+
if (connector.type === CONNECTOR_CATEGORY.IN_APP) {
|
|
271
|
+
var _connectorName, _this$options$uiConfi, _this$options$uiConfi2, _this$options$uiConfi3;
|
|
272
|
+
log.info("connectorInitResults", connectorName);
|
|
273
|
+
const loginMethods = getConnectorSocialLogins(connectorName, (_connectorName = this.modalConfig.connectors[connectorName]) === null || _connectorName === void 0 ? void 0 : _connectorName.loginMethods);
|
|
274
|
+
this.loginModal.addSocialLogins(connectorName, loginMethods, ((_this$options$uiConfi = this.options.uiConfig) === null || _this$options$uiConfi === void 0 ? void 0 : _this$options$uiConfi.loginMethodsOrder) || AUTH_PROVIDERS, _objectSpread(_objectSpread({}, this.options.uiConfig), {}, {
|
|
275
|
+
loginGridCol: ((_this$options$uiConfi2 = this.options.uiConfig) === null || _this$options$uiConfi2 === void 0 ? void 0 : _this$options$uiConfi2.loginGridCol) || 3,
|
|
276
|
+
primaryButton: ((_this$options$uiConfi3 = this.options.uiConfig) === null || _this$options$uiConfi3 === void 0 ? void 0 : _this$options$uiConfi3.primaryButton) || "socialLogin"
|
|
277
|
+
}));
|
|
278
|
+
}
|
|
279
|
+
} catch (error) {
|
|
280
|
+
log.error(error, "error while initializing connector ", connectorName);
|
|
281
|
+
}
|
|
282
|
+
}));
|
|
283
|
+
}
|
|
284
|
+
async initExternalConnectors(connectors, externalWalletsInitialized, options) {
|
|
285
|
+
if (externalWalletsInitialized) return;
|
|
286
|
+
const connectorsConfig = {};
|
|
287
|
+
// we do it like this because we don't want one slow connector to delay the load of the entire external wallet section.
|
|
288
|
+
connectors.forEach(async connector => {
|
|
289
|
+
const connectorName = connector.name;
|
|
290
|
+
if ((connector === null || connector === void 0 ? void 0 : connector.type) === CONNECTOR_CATEGORY.EXTERNAL) {
|
|
291
|
+
log.debug("init external wallet", this.cachedConnector, connectorName, connector.status);
|
|
292
|
+
this.subscribeToConnectorEvents(connector);
|
|
293
|
+
// we are not initializing cached connector here as it is already being initialized in initModal before.
|
|
294
|
+
if (this.cachedConnector === connectorName) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (connector.status === CONNECTOR_STATUS.NOT_READY) {
|
|
298
|
+
try {
|
|
299
|
+
const initialChain = this.getInitialChainIdForConnector(connector);
|
|
300
|
+
await connector.init({
|
|
301
|
+
autoConnect: this.cachedConnector === connectorName,
|
|
302
|
+
chainId: initialChain.chainId
|
|
303
|
+
});
|
|
304
|
+
const connectorModalConfig = this.modalConfig.connectors[connectorName];
|
|
305
|
+
connectorsConfig[connectorName] = _objectSpread(_objectSpread({}, connectorModalConfig), {}, {
|
|
306
|
+
isInjected: connector.isInjected
|
|
307
|
+
});
|
|
308
|
+
this.loginModal.addWalletLogins(connectorsConfig, {
|
|
309
|
+
showExternalWalletsOnly: !!(options !== null && options !== void 0 && options.showExternalWalletsOnly)
|
|
310
|
+
});
|
|
311
|
+
} catch (error) {
|
|
312
|
+
log.error(error, "error while initializing connector", connectorName);
|
|
313
|
+
}
|
|
314
|
+
} else if (connector.status === CONNECTOR_STATUS.READY || connector.status === CONNECTOR_STATUS.CONNECTING) {
|
|
315
|
+
// we use connecting status for wallet connect
|
|
316
|
+
const connectorModalConfig = this.modalConfig.connectors[connectorName];
|
|
317
|
+
connectorsConfig[connectorName] = _objectSpread(_objectSpread({}, connectorModalConfig), {}, {
|
|
318
|
+
isInjected: connector.isInjected
|
|
319
|
+
});
|
|
320
|
+
this.loginModal.addWalletLogins(connectorsConfig, {
|
|
321
|
+
showExternalWalletsOnly: !!(options !== null && options !== void 0 && options.showExternalWalletsOnly)
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
subscribeToLoginModalEvents() {
|
|
328
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.LOGIN, async params => {
|
|
329
|
+
try {
|
|
330
|
+
await this.connectTo(params.connector, params.loginParams);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
log.error(`Error while connecting to connector: ${params.connector}`, error);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.INIT_EXTERNAL_WALLETS, async params => {
|
|
336
|
+
await this.initExternalConnectors(this.connectors, params.externalWalletsInitialized);
|
|
337
|
+
});
|
|
338
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.DISCONNECT, async () => {
|
|
339
|
+
try {
|
|
340
|
+
await this.logout();
|
|
341
|
+
} catch (error) {
|
|
342
|
+
log.error(`Error while disconnecting`, error);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
this.loginModal.on(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, async visibility => {
|
|
346
|
+
log.debug("is login modal visible", visibility);
|
|
347
|
+
this.emit(LOGIN_MODAL_EVENTS.MODAL_VISIBILITY, visibility);
|
|
348
|
+
const wcConnector = this.getConnector(WALLET_CONNECTORS.WALLET_CONNECT_V2);
|
|
349
|
+
if (wcConnector) {
|
|
350
|
+
const walletConnectStatus = wcConnector === null || wcConnector === void 0 ? void 0 : wcConnector.status;
|
|
351
|
+
log.debug("trying refreshing wc session", visibility, walletConnectStatus);
|
|
352
|
+
if (visibility && (walletConnectStatus === CONNECTOR_STATUS.READY || walletConnectStatus === CONNECTOR_STATUS.CONNECTING)) {
|
|
353
|
+
log.debug("refreshing wc session");
|
|
354
|
+
|
|
355
|
+
// refreshing session for wallet connect whenever modal is opened.
|
|
356
|
+
try {
|
|
357
|
+
const initialChain = this.getInitialChainIdForConnector(wcConnector);
|
|
358
|
+
wcConnector.connect({
|
|
359
|
+
chainId: initialChain.chainId
|
|
360
|
+
});
|
|
361
|
+
} catch (error) {
|
|
362
|
+
log.error(`Error while disconnecting to wallet connect in core`, error);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (!visibility && this.status === CONNECTOR_STATUS.CONNECTED && (walletConnectStatus === CONNECTOR_STATUS.READY || walletConnectStatus === CONNECTOR_STATUS.CONNECTING)) {
|
|
366
|
+
log.debug("this stops wc connector from trying to reconnect once proposal expires");
|
|
367
|
+
wcConnector.status = CONNECTOR_STATUS.READY;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export { Web3Auth };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createElement } from 'react';
|
|
2
|
+
import { Web3AuthInnerProvider } from './context/Web3AuthInnerContext.js';
|
|
3
|
+
|
|
4
|
+
function Web3AuthProvider({
|
|
5
|
+
config,
|
|
6
|
+
children
|
|
7
|
+
}) {
|
|
8
|
+
return /*#__PURE__*/createElement(Web3AuthInnerProvider, {
|
|
9
|
+
config
|
|
10
|
+
}, children);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { Web3AuthProvider };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { WalletInitializationError, WalletLoginError, CONNECTOR_EVENTS, CONNECTOR_STATUS } from '@web3auth/no-modal';
|
|
2
|
+
import { createContext, useState, useCallback, useEffect, useMemo, createElement } from 'react';
|
|
3
|
+
import { Web3Auth } from '../../modalManager.js';
|
|
4
|
+
|
|
5
|
+
const Web3AuthInnerContext = /*#__PURE__*/createContext(null);
|
|
6
|
+
function Web3AuthInnerProvider(params) {
|
|
7
|
+
const {
|
|
8
|
+
children,
|
|
9
|
+
config
|
|
10
|
+
} = params;
|
|
11
|
+
const [web3Auth, setWeb3Auth] = useState(null);
|
|
12
|
+
const [isConnecting, setIsConnecting] = useState(false);
|
|
13
|
+
const [isInitializing, setIsInitializing] = useState(false);
|
|
14
|
+
const [initError, setInitError] = useState(null);
|
|
15
|
+
const [connectError, setConnectError] = useState(null);
|
|
16
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
17
|
+
const [provider, setProvider] = useState(null);
|
|
18
|
+
const [userInfo, setUserInfo] = useState(null);
|
|
19
|
+
const [isMFAEnabled, setIsMFAEnabled] = useState(false);
|
|
20
|
+
const [isInitialized, setIsInitialized] = useState(false);
|
|
21
|
+
const [status, setStatus] = useState(null);
|
|
22
|
+
const getPlugin = useCallback(name => {
|
|
23
|
+
if (!web3Auth) throw WalletInitializationError.notReady();
|
|
24
|
+
return web3Auth.getPlugin(name);
|
|
25
|
+
}, [web3Auth]);
|
|
26
|
+
const enableMFA = useCallback(async loginParams => {
|
|
27
|
+
if (!web3Auth) throw WalletInitializationError.notReady();
|
|
28
|
+
if (!isConnected) throw WalletLoginError.notConnectedError();
|
|
29
|
+
await web3Auth.enableMFA(loginParams);
|
|
30
|
+
const localUserInfo = await web3Auth.getUserInfo();
|
|
31
|
+
setUserInfo(localUserInfo);
|
|
32
|
+
setIsMFAEnabled(localUserInfo.isMfaEnabled || false);
|
|
33
|
+
}, [web3Auth, isConnected]);
|
|
34
|
+
const manageMFA = useCallback(async loginParams => {
|
|
35
|
+
if (!web3Auth) throw WalletInitializationError.notReady();
|
|
36
|
+
if (!isConnected) throw WalletLoginError.notConnectedError();
|
|
37
|
+
await web3Auth.manageMFA(loginParams);
|
|
38
|
+
}, [web3Auth, isConnected]);
|
|
39
|
+
const logout = useCallback(async (logoutParams = {
|
|
40
|
+
cleanup: false
|
|
41
|
+
}) => {
|
|
42
|
+
if (!web3Auth) throw WalletInitializationError.notReady();
|
|
43
|
+
if (!isConnected) throw WalletLoginError.notConnectedError();
|
|
44
|
+
await web3Auth.logout(logoutParams);
|
|
45
|
+
}, [web3Auth, isConnected]);
|
|
46
|
+
const connect = useCallback(async () => {
|
|
47
|
+
if (!web3Auth) throw WalletInitializationError.notReady();
|
|
48
|
+
try {
|
|
49
|
+
setConnectError(null);
|
|
50
|
+
setIsConnecting(true);
|
|
51
|
+
const localProvider = await web3Auth.connect();
|
|
52
|
+
return localProvider;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
setConnectError(error);
|
|
55
|
+
throw error;
|
|
56
|
+
} finally {
|
|
57
|
+
setIsConnecting(false);
|
|
58
|
+
}
|
|
59
|
+
}, [web3Auth]);
|
|
60
|
+
const authenticateUser = useCallback(async () => {
|
|
61
|
+
if (!web3Auth) throw WalletInitializationError.notReady();
|
|
62
|
+
return web3Auth.authenticateUser();
|
|
63
|
+
}, [web3Auth]);
|
|
64
|
+
const switchChain = useCallback(chainParams => {
|
|
65
|
+
if (!web3Auth) throw WalletInitializationError.notReady();
|
|
66
|
+
return web3Auth.switchChain(chainParams);
|
|
67
|
+
}, [web3Auth]);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
const resetHookState = () => {
|
|
70
|
+
setProvider(null);
|
|
71
|
+
setUserInfo(null);
|
|
72
|
+
setIsMFAEnabled(false);
|
|
73
|
+
setIsConnected(false);
|
|
74
|
+
setStatus(null);
|
|
75
|
+
};
|
|
76
|
+
resetHookState();
|
|
77
|
+
const {
|
|
78
|
+
web3AuthOptions
|
|
79
|
+
} = config;
|
|
80
|
+
const web3AuthInstance = new Web3Auth(web3AuthOptions);
|
|
81
|
+
setWeb3Auth(web3AuthInstance);
|
|
82
|
+
}, [config]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
async function init() {
|
|
85
|
+
try {
|
|
86
|
+
setInitError(null);
|
|
87
|
+
setIsInitializing(true);
|
|
88
|
+
const {
|
|
89
|
+
modalConfig,
|
|
90
|
+
hideWalletDiscovery
|
|
91
|
+
} = config;
|
|
92
|
+
if (modalConfig) {
|
|
93
|
+
await web3Auth.initModal({
|
|
94
|
+
modalConfig,
|
|
95
|
+
hideWalletDiscovery
|
|
96
|
+
});
|
|
97
|
+
} else {
|
|
98
|
+
await web3Auth.initModal();
|
|
99
|
+
}
|
|
100
|
+
} catch (error) {
|
|
101
|
+
setInitError(error);
|
|
102
|
+
} finally {
|
|
103
|
+
setIsInitializing(false);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (web3Auth) init();
|
|
107
|
+
}, [web3Auth, config]);
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
const addState = async () => {
|
|
110
|
+
setProvider(web3Auth.provider);
|
|
111
|
+
const userState = await web3Auth.getUserInfo();
|
|
112
|
+
setUserInfo(userState);
|
|
113
|
+
setIsMFAEnabled((userState === null || userState === void 0 ? void 0 : userState.isMfaEnabled) || false);
|
|
114
|
+
};
|
|
115
|
+
const resetState = () => {
|
|
116
|
+
setProvider(null);
|
|
117
|
+
setUserInfo(null);
|
|
118
|
+
setIsMFAEnabled(false);
|
|
119
|
+
};
|
|
120
|
+
if (web3Auth) {
|
|
121
|
+
if (isConnected) addState();else resetState();
|
|
122
|
+
}
|
|
123
|
+
}, [web3Auth, isConnected]);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
const notReadyListener = () => setStatus(web3Auth.status);
|
|
126
|
+
const readyListener = () => {
|
|
127
|
+
setStatus(web3Auth.status);
|
|
128
|
+
setIsInitialized(true);
|
|
129
|
+
};
|
|
130
|
+
const connectedListener = () => {
|
|
131
|
+
setStatus(web3Auth.status);
|
|
132
|
+
// we do this because of rehydration issues. status connected is fired first but web3auth sdk is not ready yet.
|
|
133
|
+
if (web3Auth.status === CONNECTOR_STATUS.CONNECTED) {
|
|
134
|
+
setIsInitialized(true);
|
|
135
|
+
setIsConnected(true);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const disconnectedListener = () => {
|
|
139
|
+
setStatus(web3Auth.status);
|
|
140
|
+
setIsConnected(false);
|
|
141
|
+
};
|
|
142
|
+
const connectingListener = () => {
|
|
143
|
+
setStatus(web3Auth.status);
|
|
144
|
+
};
|
|
145
|
+
const errorListener = () => {
|
|
146
|
+
setStatus(CONNECTOR_STATUS.ERRORED);
|
|
147
|
+
};
|
|
148
|
+
if (web3Auth) {
|
|
149
|
+
// web3Auth is initialized here.
|
|
150
|
+
setStatus(web3Auth.status);
|
|
151
|
+
web3Auth.on(CONNECTOR_EVENTS.NOT_READY, notReadyListener);
|
|
152
|
+
web3Auth.on(CONNECTOR_EVENTS.READY, readyListener);
|
|
153
|
+
web3Auth.on(CONNECTOR_EVENTS.CONNECTED, connectedListener);
|
|
154
|
+
web3Auth.on(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
|
|
155
|
+
web3Auth.on(CONNECTOR_EVENTS.CONNECTING, connectingListener);
|
|
156
|
+
web3Auth.on(CONNECTOR_EVENTS.ERRORED, errorListener);
|
|
157
|
+
}
|
|
158
|
+
return () => {
|
|
159
|
+
if (web3Auth) {
|
|
160
|
+
web3Auth.off(CONNECTOR_EVENTS.NOT_READY, notReadyListener);
|
|
161
|
+
web3Auth.off(CONNECTOR_EVENTS.READY, readyListener);
|
|
162
|
+
web3Auth.off(CONNECTOR_EVENTS.CONNECTED, connectedListener);
|
|
163
|
+
web3Auth.off(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
|
|
164
|
+
web3Auth.off(CONNECTOR_EVENTS.CONNECTING, connectingListener);
|
|
165
|
+
web3Auth.off(CONNECTOR_EVENTS.ERRORED, errorListener);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}, [web3Auth]);
|
|
169
|
+
const value = useMemo(() => {
|
|
170
|
+
return {
|
|
171
|
+
web3Auth,
|
|
172
|
+
isConnected,
|
|
173
|
+
isInitialized,
|
|
174
|
+
provider,
|
|
175
|
+
userInfo,
|
|
176
|
+
isMFAEnabled,
|
|
177
|
+
status,
|
|
178
|
+
connect,
|
|
179
|
+
enableMFA,
|
|
180
|
+
manageMFA,
|
|
181
|
+
logout,
|
|
182
|
+
authenticateUser,
|
|
183
|
+
switchChain,
|
|
184
|
+
getPlugin,
|
|
185
|
+
isInitializing,
|
|
186
|
+
isConnecting,
|
|
187
|
+
initError,
|
|
188
|
+
connectError
|
|
189
|
+
};
|
|
190
|
+
}, [web3Auth, isConnected, isInitialized, provider, userInfo, isMFAEnabled, status, getPlugin, connect, enableMFA, manageMFA, logout, authenticateUser, switchChain, isInitializing, isConnecting, initError, connectError]);
|
|
191
|
+
return /*#__PURE__*/createElement(Web3AuthInnerContext.Provider, {
|
|
192
|
+
value
|
|
193
|
+
}, children);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export { Web3AuthInnerContext, Web3AuthInnerProvider };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { WalletInitializationError } from '@web3auth/no-modal';
|
|
2
|
+
import { useContext } from 'react';
|
|
3
|
+
import { Web3AuthInnerContext } from '../context/Web3AuthInnerContext.js';
|
|
4
|
+
|
|
5
|
+
const useWeb3Auth = () => {
|
|
6
|
+
const context = useContext(Web3AuthInnerContext);
|
|
7
|
+
if (!context) {
|
|
8
|
+
throw WalletInitializationError.fromCode(1000, "usage of useWeb3Auth not wrapped in `Web3AuthContextProvider`.");
|
|
9
|
+
}
|
|
10
|
+
return context;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { useWeb3Auth };
|