@web3auth/no-modal 10.0.0-beta.3 → 10.0.0-beta.4

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.
Files changed (107) hide show
  1. package/dist/lib.cjs/base/connector/constants.js +2 -1
  2. package/dist/lib.cjs/base/wallet/index.js +1 -3
  3. package/dist/lib.cjs/connectors/auth-connector/authConnector.js +3 -1
  4. package/dist/lib.cjs/index.js +2 -0
  5. package/dist/lib.cjs/noModal.js +20 -3
  6. package/dist/lib.cjs/plugins/nft-checkout-plugin/plugin.js +2 -2
  7. package/dist/lib.cjs/plugins/wallet-services-plugin/plugin.js +1 -1
  8. package/dist/lib.cjs/react/Web3AuthProvider.js +19 -0
  9. package/dist/lib.cjs/react/{wallet-services-plugin/WalletServicesContext.js → context/WalletServicesInnerContext.js} +18 -33
  10. package/dist/lib.cjs/react/{no-modal → context}/Web3AuthInnerContext.js +13 -78
  11. package/dist/lib.cjs/react/hooks/useCheckout.js +45 -0
  12. package/dist/lib.cjs/react/hooks/useEnableMFA.js +30 -0
  13. package/dist/lib.cjs/react/hooks/useIdentityToken.js +42 -0
  14. package/dist/lib.cjs/react/hooks/useManageMFA.js +30 -0
  15. package/dist/lib.cjs/react/hooks/useSwap.js +45 -0
  16. package/dist/lib.cjs/react/hooks/useSwitchChain.js +32 -0
  17. package/dist/lib.cjs/react/hooks/useWalletConnectScanner.js +45 -0
  18. package/dist/lib.cjs/react/hooks/useWalletServicesPlugin.js +19 -0
  19. package/dist/lib.cjs/react/hooks/useWalletUI.js +45 -0
  20. package/dist/lib.cjs/react/hooks/useWeb3Auth.js +28 -0
  21. package/dist/lib.cjs/react/hooks/useWeb3AuthConnect.js +44 -0
  22. package/dist/lib.cjs/react/hooks/useWeb3AuthDisconnect.js +30 -0
  23. package/dist/lib.cjs/react/{no-modal/hooks/useWeb3Auth.js → hooks/useWeb3AuthInner.js} +9 -9
  24. package/dist/lib.cjs/react/hooks/useWeb3AuthUser.js +50 -0
  25. package/dist/lib.cjs/react/index.js +26 -9
  26. package/dist/lib.cjs/react/wagmi/index.js +7 -0
  27. package/dist/lib.cjs/react/wagmi/provider.js +186 -0
  28. package/dist/lib.cjs/types/base/connector/baseConnector.d.ts +4 -5
  29. package/dist/lib.cjs/types/base/connector/constants.d.ts +1 -0
  30. package/dist/lib.cjs/types/base/connector/interfaces.d.ts +7 -2
  31. package/dist/lib.cjs/types/base/core/IWeb3Auth.d.ts +17 -4
  32. package/dist/lib.cjs/types/base/hooks/index.d.ts +3 -23
  33. package/dist/lib.cjs/types/base/wallet/index.d.ts +16 -21
  34. package/dist/lib.cjs/types/connectors/auth-connector/authConnector.d.ts +2 -2
  35. package/dist/lib.cjs/types/connectors/index.d.ts +1 -0
  36. package/dist/lib.cjs/types/connectors/metamask-connector/index.d.ts +1 -1
  37. package/dist/lib.cjs/types/noModal.d.ts +2 -2
  38. package/dist/lib.cjs/types/plugins/nft-checkout-plugin/plugin.d.ts +2 -2
  39. package/dist/lib.cjs/types/plugins/wallet-services-plugin/plugin.d.ts +2 -2
  40. package/dist/lib.cjs/types/react/context/WalletServicesInnerContext.d.ts +6 -0
  41. package/dist/lib.cjs/types/react/{no-modal → context}/Web3AuthInnerContext.d.ts +1 -1
  42. package/dist/lib.cjs/types/react/hooks/index.d.ts +13 -0
  43. package/dist/lib.cjs/types/react/hooks/useCheckout.d.ts +8 -0
  44. package/dist/lib.cjs/types/react/hooks/useEnableMFA.d.ts +7 -0
  45. package/dist/lib.cjs/types/react/hooks/useIdentityToken.d.ts +13 -0
  46. package/dist/lib.cjs/types/react/hooks/useManageMFA.d.ts +7 -0
  47. package/dist/lib.cjs/types/react/hooks/useSwap.d.ts +8 -0
  48. package/dist/lib.cjs/types/react/hooks/useSwitchChain.d.ts +7 -0
  49. package/dist/lib.cjs/types/react/hooks/useWalletConnectScanner.d.ts +8 -0
  50. package/dist/lib.cjs/types/react/hooks/useWalletUI.d.ts +8 -0
  51. package/dist/lib.cjs/types/react/hooks/useWeb3Auth.d.ts +3 -0
  52. package/dist/lib.cjs/types/react/hooks/useWeb3AuthConnect.d.ts +9 -0
  53. package/dist/lib.cjs/types/react/hooks/useWeb3AuthDisconnect.d.ts +9 -0
  54. package/dist/lib.cjs/types/react/hooks/useWeb3AuthInner.d.ts +2 -0
  55. package/dist/lib.cjs/types/react/hooks/useWeb3AuthUser.d.ts +9 -0
  56. package/dist/lib.cjs/types/react/index.d.ts +3 -2
  57. package/dist/lib.cjs/types/react/{no-modal/interfaces.d.ts → interfaces.d.ts} +8 -3
  58. package/dist/lib.cjs/types/react/wagmi/index.d.ts +1 -0
  59. package/dist/lib.cjs/types/react/wagmi/interface.d.ts +4 -0
  60. package/dist/lib.cjs/types/react/wagmi/provider.d.ts +4 -0
  61. package/dist/lib.cjs/types/vue/no-modal/interfaces.d.ts +2 -2
  62. package/dist/lib.esm/base/connector/constants.js +2 -1
  63. package/dist/lib.esm/base/wallet/index.js +1 -3
  64. package/dist/lib.esm/connectors/auth-connector/authConnector.js +3 -1
  65. package/dist/lib.esm/index.js +1 -0
  66. package/dist/lib.esm/noModal.js +20 -3
  67. package/dist/lib.esm/plugins/nft-checkout-plugin/plugin.js +2 -2
  68. package/dist/lib.esm/plugins/wallet-services-plugin/plugin.js +1 -1
  69. package/dist/lib.esm/react/Web3AuthProvider.js +17 -0
  70. package/dist/lib.esm/react/context/WalletServicesInnerContext.js +80 -0
  71. package/dist/lib.esm/react/{no-modal → context}/Web3AuthInnerContext.js +14 -79
  72. package/dist/lib.esm/react/hooks/useCheckout.js +43 -0
  73. package/dist/lib.esm/react/hooks/useEnableMFA.js +28 -0
  74. package/dist/lib.esm/react/hooks/useIdentityToken.js +40 -0
  75. package/dist/lib.esm/react/hooks/useManageMFA.js +28 -0
  76. package/dist/lib.esm/react/hooks/useSwap.js +43 -0
  77. package/dist/lib.esm/react/hooks/useSwitchChain.js +30 -0
  78. package/dist/lib.esm/react/hooks/useWalletConnectScanner.js +43 -0
  79. package/dist/lib.esm/react/hooks/useWalletServicesPlugin.js +17 -0
  80. package/dist/lib.esm/react/hooks/useWalletUI.js +43 -0
  81. package/dist/lib.esm/react/hooks/useWeb3Auth.js +26 -0
  82. package/dist/lib.esm/react/hooks/useWeb3AuthConnect.js +42 -0
  83. package/dist/lib.esm/react/hooks/useWeb3AuthDisconnect.js +28 -0
  84. package/dist/lib.esm/react/{no-modal/hooks/useWeb3Auth.js → hooks/useWeb3AuthInner.js} +9 -9
  85. package/dist/lib.esm/react/hooks/useWeb3AuthUser.js +48 -0
  86. package/dist/lib.esm/react/index.js +14 -5
  87. package/dist/lib.esm/react/wagmi/index.js +1 -0
  88. package/dist/lib.esm/react/wagmi/provider.js +184 -0
  89. package/dist/noModal.umd.min.js +1 -1
  90. package/package.json +22 -6
  91. package/dist/lib.cjs/react/no-modal/Web3AuthProvider.js +0 -15
  92. package/dist/lib.cjs/react/wallet-services-plugin/WalletServicesProvider.js +0 -16
  93. package/dist/lib.cjs/react/wallet-services-plugin/hooks/useWalletServicesPlugin.js +0 -26
  94. package/dist/lib.cjs/types/react/no-modal/hooks/index.d.ts +0 -1
  95. package/dist/lib.cjs/types/react/no-modal/hooks/useWeb3Auth.d.ts +0 -2
  96. package/dist/lib.cjs/types/react/no-modal/index.d.ts +0 -3
  97. package/dist/lib.cjs/types/react/wallet-services-plugin/WalletServicesContext.d.ts +0 -7
  98. package/dist/lib.cjs/types/react/wallet-services-plugin/WalletServicesProvider.d.ts +0 -7
  99. package/dist/lib.cjs/types/react/wallet-services-plugin/hooks/index.d.ts +0 -1
  100. package/dist/lib.cjs/types/react/wallet-services-plugin/index.d.ts +0 -4
  101. package/dist/lib.cjs/types/react/wallet-services-plugin/interfaces.d.ts +0 -5
  102. package/dist/lib.esm/react/no-modal/Web3AuthProvider.js +0 -13
  103. package/dist/lib.esm/react/wallet-services-plugin/WalletServicesContext.js +0 -95
  104. package/dist/lib.esm/react/wallet-services-plugin/WalletServicesProvider.js +0 -14
  105. package/dist/lib.esm/react/wallet-services-plugin/hooks/useWalletServicesPlugin.js +0 -24
  106. /package/dist/lib.cjs/types/react/{no-modal/Web3AuthProvider.d.ts → Web3AuthProvider.d.ts} +0 -0
  107. /package/dist/lib.cjs/types/react/{wallet-services-plugin/hooks → hooks}/useWalletServicesPlugin.d.ts +0 -0
@@ -2,7 +2,7 @@ import { createContext, useState, useCallback, useEffect, useMemo, createElement
2
2
  import '@toruslabs/base-controllers';
3
3
  import '@babel/runtime/helpers/defineProperty';
4
4
  import '@web3auth/auth';
5
- import { WalletInitializationError, WalletLoginError } from '../../base/errors/index.js';
5
+ import { WalletInitializationError } from '../../base/errors/index.js';
6
6
  import '../../base/wallet/index.js';
7
7
  import { CONNECTOR_EVENTS, CONNECTOR_STATUS } from '../../base/connector/constants.js';
8
8
  import 'jwt-decode';
@@ -20,16 +20,13 @@ function Web3AuthInnerProvider(params) {
20
20
  config
21
21
  } = params;
22
22
  const [web3Auth, setWeb3Auth] = useState(null);
23
- const [isConnecting, setIsConnecting] = useState(false);
24
23
  const [isInitializing, setIsInitializing] = useState(false);
25
24
  const [initError, setInitError] = useState(null);
26
- const [connectError, setConnectError] = useState(null);
27
25
  const [isConnected, setIsConnected] = useState(false);
28
26
  const [provider, setProvider] = useState(null);
29
- const [userInfo, setUserInfo] = useState(null);
30
- const [isMFAEnabled, setIsMFAEnabled] = useState(false);
31
27
  const [isInitialized, setIsInitialized] = useState(false);
32
28
  const [status, setStatus] = useState(null);
29
+ const [isMFAEnabled, setIsMFAEnabled] = useState(false);
33
30
  const getPlugin = useCallback(name => {
34
31
  if (!web3Auth) throw WalletInitializationError.notReady();
35
32
  return web3Auth.getPlugin(name);
@@ -37,8 +34,6 @@ function Web3AuthInnerProvider(params) {
37
34
  useEffect(() => {
38
35
  const resetHookState = () => {
39
36
  setProvider(null);
40
- setUserInfo(null);
41
- setIsMFAEnabled(false);
42
37
  setIsConnected(false);
43
38
  setStatus(null);
44
39
  };
@@ -69,40 +64,25 @@ function Web3AuthInnerProvider(params) {
69
64
  controller.abort();
70
65
  };
71
66
  }, [web3Auth]);
72
- useEffect(() => {
73
- const addState = async () => {
74
- setProvider(web3Auth.provider);
75
- const userState = await web3Auth.getUserInfo();
76
- setUserInfo(userState);
77
- setIsMFAEnabled((userState === null || userState === void 0 ? void 0 : userState.isMfaEnabled) || false);
78
- };
79
- const resetState = () => {
80
- setProvider(null);
81
- setUserInfo(null);
82
- setIsMFAEnabled(false);
83
- };
84
- if (web3Auth) {
85
- if (isConnected) addState();else resetState();
86
- }
87
- }, [web3Auth, isConnected]);
88
- // TODO: don't throw error in init, connect in v9
89
67
  useEffect(() => {
90
68
  const notReadyListener = () => setStatus(CONNECTOR_STATUS.NOT_READY);
91
69
  const readyListener = () => {
92
70
  setStatus(web3Auth.status);
93
71
  setIsInitialized(true);
94
72
  };
95
- const connectedListener = () => {
73
+ const connectedListener = data => {
96
74
  setStatus(web3Auth.status);
97
75
  // we do this because of rehydration issues. status connected is fired first but web3auth sdk is not ready yet.
98
76
  if (web3Auth.status === CONNECTOR_STATUS.CONNECTED) {
99
77
  setIsInitialized(true);
100
78
  setIsConnected(true);
79
+ setProvider(data.provider);
101
80
  }
102
81
  };
103
82
  const disconnectedListener = () => {
104
83
  setStatus(web3Auth.status);
105
84
  setIsConnected(false);
85
+ setProvider(null);
106
86
  };
107
87
  const connectingListener = () => {
108
88
  setStatus(web3Auth.status);
@@ -110,6 +90,9 @@ function Web3AuthInnerProvider(params) {
110
90
  const errorListener = () => {
111
91
  setStatus(CONNECTOR_STATUS.ERRORED);
112
92
  };
93
+ const mfaEnabledListener = isMFAEnabled => {
94
+ if (typeof isMFAEnabled === "boolean") setIsMFAEnabled(isMFAEnabled);
95
+ };
113
96
  if (web3Auth) {
114
97
  // web3Auth is initialized here.
115
98
  setStatus(web3Auth.status);
@@ -119,6 +102,7 @@ function Web3AuthInnerProvider(params) {
119
102
  web3Auth.on(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
120
103
  web3Auth.on(CONNECTOR_EVENTS.CONNECTING, connectingListener);
121
104
  web3Auth.on(CONNECTOR_EVENTS.ERRORED, errorListener);
105
+ web3Auth.on(CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
122
106
  }
123
107
  return () => {
124
108
  if (web3Auth) {
@@ -128,73 +112,24 @@ function Web3AuthInnerProvider(params) {
128
112
  web3Auth.off(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
129
113
  web3Auth.off(CONNECTOR_EVENTS.CONNECTING, connectingListener);
130
114
  web3Auth.off(CONNECTOR_EVENTS.ERRORED, errorListener);
115
+ web3Auth.off(CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
131
116
  }
132
117
  };
133
118
  }, [web3Auth]);
134
- const enableMFA = useCallback(async loginParams => {
135
- if (!web3Auth) throw WalletInitializationError.notReady();
136
- if (!isConnected) throw WalletLoginError.notConnectedError();
137
- await web3Auth.enableMFA(loginParams);
138
- const localUserInfo = await web3Auth.getUserInfo();
139
- setUserInfo(localUserInfo);
140
- setIsMFAEnabled(localUserInfo.isMfaEnabled || false);
141
- }, [web3Auth, isConnected]);
142
- const manageMFA = useCallback(async loginParams => {
143
- if (!web3Auth) throw WalletInitializationError.notReady();
144
- if (!isConnected) throw WalletLoginError.notConnectedError();
145
- await web3Auth.manageMFA(loginParams);
146
- }, [web3Auth, isConnected]);
147
- const logout = useCallback(async (logoutParams = {
148
- cleanup: false
149
- }) => {
150
- if (!web3Auth) throw WalletInitializationError.notReady();
151
- if (!isConnected) throw WalletLoginError.notConnectedError();
152
- await web3Auth.logout(logoutParams);
153
- }, [web3Auth, isConnected]);
154
- const connectTo = useCallback(async (walletName, loginParams) => {
155
- if (!web3Auth) throw WalletInitializationError.notReady();
156
- try {
157
- setConnectError(null);
158
- setIsConnecting(true);
159
- const localProvider = await web3Auth.connectTo(walletName, loginParams);
160
- return localProvider;
161
- } catch (error) {
162
- setConnectError(error);
163
- throw error;
164
- } finally {
165
- setIsConnecting(false);
166
- }
167
- }, [web3Auth]);
168
- const authenticateUser = useCallback(async () => {
169
- if (!web3Auth) throw WalletInitializationError.notReady();
170
- return web3Auth.authenticateUser();
171
- }, [web3Auth]);
172
- const switchChain = useCallback(chainParams => {
173
- if (!web3Auth) throw WalletInitializationError.notReady();
174
- return web3Auth.switchChain(chainParams);
175
- }, [web3Auth]);
176
119
  const value = useMemo(() => {
177
120
  return {
178
121
  web3Auth,
179
122
  isConnected,
180
123
  isInitialized,
181
124
  provider,
182
- userInfo,
183
- isMFAEnabled,
184
125
  status,
185
- getPlugin,
186
- connectTo,
187
- enableMFA,
188
- manageMFA,
189
- logout,
190
- authenticateUser,
191
- switchChain,
192
126
  isInitializing,
193
- isConnecting,
194
127
  initError,
195
- connectError
128
+ isMFAEnabled,
129
+ getPlugin,
130
+ setIsMFAEnabled
196
131
  };
197
- }, [web3Auth, isConnected, isInitialized, provider, userInfo, isMFAEnabled, status, connectTo, getPlugin, enableMFA, manageMFA, logout, authenticateUser, switchChain, isConnecting, isInitializing, initError, connectError]);
132
+ }, [web3Auth, isConnected, isInitialized, provider, status, getPlugin, isInitializing, initError, isMFAEnabled, setIsMFAEnabled]);
198
133
  return createElement(Web3AuthInnerContext.Provider, {
199
134
  value
200
135
  }, children);
@@ -0,0 +1,43 @@
1
+ import { useState, useCallback } from 'react';
2
+ import '@toruslabs/base-controllers';
3
+ import '@babel/runtime/helpers/defineProperty';
4
+ import '@web3auth/auth';
5
+ import '../../base/errors/index.js';
6
+ import '../../base/wallet/index.js';
7
+ import '../../base/connector/constants.js';
8
+ import 'jwt-decode';
9
+ import '../../base/loglevel.js';
10
+ import { WalletServicesPluginError } from '../../base/plugin/errors.js';
11
+ import '../../base/plugin/IPlugin.js';
12
+ import '@toruslabs/constants';
13
+ import '@toruslabs/http-helpers';
14
+ import { useWalletServicesPlugin } from './useWalletServicesPlugin.js';
15
+
16
+ const useCheckout = () => {
17
+ const {
18
+ plugin,
19
+ ready
20
+ } = useWalletServicesPlugin();
21
+ const [loading, setLoading] = useState(false);
22
+ const [error, setError] = useState(null);
23
+ const showCheckout = useCallback(async showCheckoutParams => {
24
+ setLoading(true);
25
+ setError(null);
26
+ try {
27
+ if (!plugin) throw WalletServicesPluginError.notInitialized();
28
+ if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
29
+ await plugin.showCheckout(showCheckoutParams);
30
+ } catch (error) {
31
+ setError(error);
32
+ } finally {
33
+ setLoading(false);
34
+ }
35
+ }, [plugin, ready]);
36
+ return {
37
+ loading,
38
+ error,
39
+ showCheckout
40
+ };
41
+ };
42
+
43
+ export { useCheckout };
@@ -0,0 +1,28 @@
1
+ import { useState, useCallback } from 'react';
2
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
3
+
4
+ const useEnableMFA = () => {
5
+ const {
6
+ web3Auth
7
+ } = useWeb3AuthInner();
8
+ const [loading, setLoading] = useState(false);
9
+ const [error, setError] = useState(null);
10
+ const enableMFA = useCallback(async params => {
11
+ setLoading(true);
12
+ setError(null);
13
+ try {
14
+ await web3Auth.enableMFA(params);
15
+ } catch (error) {
16
+ setError(error);
17
+ } finally {
18
+ setLoading(false);
19
+ }
20
+ }, [web3Auth]);
21
+ return {
22
+ loading,
23
+ error,
24
+ enableMFA
25
+ };
26
+ };
27
+
28
+ export { useEnableMFA };
@@ -0,0 +1,40 @@
1
+ import { useState, useCallback, useEffect } from 'react';
2
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
3
+
4
+ const useIdentityToken = () => {
5
+ const {
6
+ web3Auth,
7
+ isConnected
8
+ } = useWeb3AuthInner();
9
+ const [loading, setLoading] = useState(false);
10
+ const [error, setError] = useState(null);
11
+ const [token, setToken] = useState(null);
12
+ const authenticateUser = useCallback(async () => {
13
+ setLoading(true);
14
+ setError(null);
15
+ try {
16
+ const userAuthInfo = await web3Auth.authenticateUser();
17
+ if (userAuthInfo !== null && userAuthInfo !== void 0 && userAuthInfo.idToken) {
18
+ setToken(userAuthInfo.idToken);
19
+ }
20
+ return userAuthInfo === null || userAuthInfo === void 0 ? void 0 : userAuthInfo.idToken;
21
+ } catch (error) {
22
+ setError(error);
23
+ } finally {
24
+ setLoading(false);
25
+ }
26
+ }, [web3Auth]);
27
+ useEffect(() => {
28
+ if (!isConnected && token) {
29
+ setToken(null);
30
+ }
31
+ }, [isConnected, token]);
32
+ return {
33
+ loading,
34
+ error,
35
+ token,
36
+ authenticateUser
37
+ };
38
+ };
39
+
40
+ export { useIdentityToken };
@@ -0,0 +1,28 @@
1
+ import { useState, useCallback } from 'react';
2
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
3
+
4
+ const useManageMFA = () => {
5
+ const {
6
+ web3Auth
7
+ } = useWeb3AuthInner();
8
+ const [loading, setLoading] = useState(false);
9
+ const [error, setError] = useState(null);
10
+ const manageMFA = useCallback(async params => {
11
+ setLoading(true);
12
+ setError(null);
13
+ try {
14
+ await web3Auth.manageMFA(params);
15
+ } catch (error) {
16
+ setError(error);
17
+ } finally {
18
+ setLoading(false);
19
+ }
20
+ }, [web3Auth]);
21
+ return {
22
+ loading,
23
+ error,
24
+ manageMFA
25
+ };
26
+ };
27
+
28
+ export { useManageMFA };
@@ -0,0 +1,43 @@
1
+ import { useState, useCallback } from 'react';
2
+ import '@toruslabs/base-controllers';
3
+ import '@babel/runtime/helpers/defineProperty';
4
+ import '@web3auth/auth';
5
+ import '../../base/errors/index.js';
6
+ import '../../base/wallet/index.js';
7
+ import '../../base/connector/constants.js';
8
+ import 'jwt-decode';
9
+ import '../../base/loglevel.js';
10
+ import { WalletServicesPluginError } from '../../base/plugin/errors.js';
11
+ import '../../base/plugin/IPlugin.js';
12
+ import '@toruslabs/constants';
13
+ import '@toruslabs/http-helpers';
14
+ import { useWalletServicesPlugin } from './useWalletServicesPlugin.js';
15
+
16
+ const useSwap = () => {
17
+ const {
18
+ plugin,
19
+ ready
20
+ } = useWalletServicesPlugin();
21
+ const [loading, setLoading] = useState(false);
22
+ const [error, setError] = useState(null);
23
+ const showSwap = useCallback(async showSwapParams => {
24
+ setLoading(true);
25
+ setError(null);
26
+ try {
27
+ if (!plugin) throw WalletServicesPluginError.notInitialized();
28
+ if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
29
+ await plugin.showSwap(showSwapParams);
30
+ } catch (error) {
31
+ setError(error);
32
+ } finally {
33
+ setLoading(false);
34
+ }
35
+ }, [plugin, ready]);
36
+ return {
37
+ loading,
38
+ error,
39
+ showSwap
40
+ };
41
+ };
42
+
43
+ export { useSwap };
@@ -0,0 +1,30 @@
1
+ import { useState, useCallback } from 'react';
2
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
3
+
4
+ const useSwitchChain = () => {
5
+ const {
6
+ web3Auth
7
+ } = useWeb3AuthInner();
8
+ const [loading, setLoading] = useState(false);
9
+ const [error, setError] = useState(null);
10
+ const switchChain = useCallback(async chainId => {
11
+ setLoading(true);
12
+ setError(null);
13
+ try {
14
+ await web3Auth.switchChain({
15
+ chainId
16
+ });
17
+ } catch (error) {
18
+ setError(error);
19
+ } finally {
20
+ setLoading(false);
21
+ }
22
+ }, [web3Auth]);
23
+ return {
24
+ loading,
25
+ error,
26
+ switchChain
27
+ };
28
+ };
29
+
30
+ export { useSwitchChain };
@@ -0,0 +1,43 @@
1
+ import { useState, useCallback } from 'react';
2
+ import '@toruslabs/base-controllers';
3
+ import '@babel/runtime/helpers/defineProperty';
4
+ import '@web3auth/auth';
5
+ import '../../base/errors/index.js';
6
+ import '../../base/wallet/index.js';
7
+ import '../../base/connector/constants.js';
8
+ import 'jwt-decode';
9
+ import '../../base/loglevel.js';
10
+ import { WalletServicesPluginError } from '../../base/plugin/errors.js';
11
+ import '../../base/plugin/IPlugin.js';
12
+ import '@toruslabs/constants';
13
+ import '@toruslabs/http-helpers';
14
+ import { useWalletServicesPlugin } from './useWalletServicesPlugin.js';
15
+
16
+ const useWalletConnectScanner = () => {
17
+ const {
18
+ plugin,
19
+ ready
20
+ } = useWalletServicesPlugin();
21
+ const [loading, setLoading] = useState(false);
22
+ const [error, setError] = useState(null);
23
+ const showWalletConnectScanner = useCallback(async showWalletConnectScannerParams => {
24
+ if (!plugin) throw WalletServicesPluginError.notInitialized();
25
+ if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
26
+ setLoading(true);
27
+ setError(null);
28
+ try {
29
+ await plugin.showWalletConnectScanner(showWalletConnectScannerParams);
30
+ } catch (error) {
31
+ setError(error);
32
+ } finally {
33
+ setLoading(false);
34
+ }
35
+ }, [plugin, ready]);
36
+ return {
37
+ loading,
38
+ error,
39
+ showWalletConnectScanner
40
+ };
41
+ };
42
+
43
+ export { useWalletConnectScanner };
@@ -0,0 +1,17 @@
1
+ import { useContext } from 'react';
2
+ import { WalletServicesContext } from '../context/WalletServicesInnerContext.js';
3
+
4
+ const useWalletServicesPlugin = () => {
5
+ const {
6
+ ready,
7
+ plugin,
8
+ connecting
9
+ } = useContext(WalletServicesContext);
10
+ return {
11
+ ready,
12
+ plugin,
13
+ connecting
14
+ };
15
+ };
16
+
17
+ export { useWalletServicesPlugin };
@@ -0,0 +1,43 @@
1
+ import { useState, useCallback } from 'react';
2
+ import '@toruslabs/base-controllers';
3
+ import '@babel/runtime/helpers/defineProperty';
4
+ import '@web3auth/auth';
5
+ import '../../base/errors/index.js';
6
+ import '../../base/wallet/index.js';
7
+ import '../../base/connector/constants.js';
8
+ import 'jwt-decode';
9
+ import '../../base/loglevel.js';
10
+ import { WalletServicesPluginError } from '../../base/plugin/errors.js';
11
+ import '../../base/plugin/IPlugin.js';
12
+ import '@toruslabs/constants';
13
+ import '@toruslabs/http-helpers';
14
+ import { useWalletServicesPlugin } from './useWalletServicesPlugin.js';
15
+
16
+ const useWalletUI = () => {
17
+ const {
18
+ plugin,
19
+ ready
20
+ } = useWalletServicesPlugin();
21
+ const [loading, setLoading] = useState(false);
22
+ const [error, setError] = useState(null);
23
+ const showWalletUI = useCallback(async showWalletUiParams => {
24
+ setLoading(true);
25
+ setError(null);
26
+ try {
27
+ if (!plugin) throw WalletServicesPluginError.notInitialized();
28
+ if (!ready) throw WalletServicesPluginError.walletPluginNotConnected();
29
+ await plugin.showWalletUi(showWalletUiParams);
30
+ } catch (error) {
31
+ setError(error);
32
+ } finally {
33
+ setLoading(false);
34
+ }
35
+ }, [plugin, ready]);
36
+ return {
37
+ loading,
38
+ error,
39
+ showWalletUI
40
+ };
41
+ };
42
+
43
+ export { useWalletUI };
@@ -0,0 +1,26 @@
1
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
2
+
3
+ const useWeb3Auth = () => {
4
+ const {
5
+ initError,
6
+ isConnected,
7
+ isInitialized,
8
+ isInitializing,
9
+ provider,
10
+ status,
11
+ web3Auth,
12
+ getPlugin
13
+ } = useWeb3AuthInner();
14
+ return {
15
+ initError,
16
+ isConnected,
17
+ isInitialized,
18
+ isInitializing,
19
+ provider,
20
+ status,
21
+ web3Auth,
22
+ getPlugin
23
+ };
24
+ };
25
+
26
+ export { useWeb3Auth };
@@ -0,0 +1,42 @@
1
+ import { useState, useEffect, useCallback } from 'react';
2
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
3
+
4
+ const useWeb3AuthConnect = () => {
5
+ const context = useWeb3AuthInner();
6
+ const {
7
+ web3Auth,
8
+ isConnected
9
+ } = context;
10
+ const [loading, setLoading] = useState(false);
11
+ const [error, setError] = useState(null);
12
+ const [connectorName, setConnectorName] = useState(null);
13
+ useEffect(() => {
14
+ if (!isConnected && connectorName) {
15
+ setConnectorName(null);
16
+ }
17
+ }, [isConnected, connectorName]);
18
+ const connect = useCallback(async (connector, params) => {
19
+ setLoading(true);
20
+ setError(null);
21
+ try {
22
+ const provider = await web3Auth.connectTo(connector, params);
23
+ if (provider) {
24
+ setConnectorName(web3Auth.connectedConnectorName);
25
+ }
26
+ return provider;
27
+ } catch (error) {
28
+ setError(error);
29
+ } finally {
30
+ setLoading(false);
31
+ }
32
+ }, [web3Auth]);
33
+ return {
34
+ isConnected,
35
+ loading,
36
+ error,
37
+ connectorName,
38
+ connect
39
+ };
40
+ };
41
+
42
+ export { useWeb3AuthConnect };
@@ -0,0 +1,28 @@
1
+ import { useState, useCallback } from 'react';
2
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
3
+
4
+ const useWeb3AuthDisconnect = () => {
5
+ const {
6
+ web3Auth
7
+ } = useWeb3AuthInner();
8
+ const [loading, setLoading] = useState(false);
9
+ const [error, setError] = useState(null);
10
+ const disconnect = useCallback(async options => {
11
+ setLoading(true);
12
+ setError(null);
13
+ try {
14
+ await web3Auth.logout(options);
15
+ } catch (error) {
16
+ setError(error);
17
+ } finally {
18
+ setLoading(false);
19
+ }
20
+ }, [web3Auth]);
21
+ return {
22
+ loading,
23
+ error,
24
+ disconnect
25
+ };
26
+ };
27
+
28
+ export { useWeb3AuthDisconnect };
@@ -2,18 +2,18 @@ import { useContext } from 'react';
2
2
  import '@toruslabs/base-controllers';
3
3
  import '@babel/runtime/helpers/defineProperty';
4
4
  import '@web3auth/auth';
5
- import { WalletInitializationError } from '../../../base/errors/index.js';
6
- import '../../../base/wallet/index.js';
7
- import '../../../base/connector/constants.js';
5
+ import { WalletInitializationError } from '../../base/errors/index.js';
6
+ import '../../base/wallet/index.js';
7
+ import '../../base/connector/constants.js';
8
8
  import 'jwt-decode';
9
- import '../../../base/loglevel.js';
10
- import '../../../base/plugin/errors.js';
11
- import '../../../base/plugin/IPlugin.js';
9
+ import '../../base/loglevel.js';
10
+ import '../../base/plugin/errors.js';
11
+ import '../../base/plugin/IPlugin.js';
12
12
  import '@toruslabs/constants';
13
13
  import '@toruslabs/http-helpers';
14
- import { Web3AuthInnerContext } from '../Web3AuthInnerContext.js';
14
+ import { Web3AuthInnerContext } from '../context/Web3AuthInnerContext.js';
15
15
 
16
- const useWeb3Auth = () => {
16
+ const useWeb3AuthInner = () => {
17
17
  const context = useContext(Web3AuthInnerContext);
18
18
  if (!context) {
19
19
  throw WalletInitializationError.fromCode(1000, "usage of useWeb3Auth not wrapped in `Web3AuthContextProvider`.");
@@ -21,4 +21,4 @@ const useWeb3Auth = () => {
21
21
  return context;
22
22
  };
23
23
 
24
- export { useWeb3Auth };
24
+ export { useWeb3AuthInner };
@@ -0,0 +1,48 @@
1
+ import { useState, useCallback, useEffect } from 'react';
2
+ import { useWeb3AuthInner } from './useWeb3AuthInner.js';
3
+
4
+ const useWeb3AuthUser = () => {
5
+ const {
6
+ web3Auth,
7
+ isConnected,
8
+ isMFAEnabled,
9
+ setIsMFAEnabled
10
+ } = useWeb3AuthInner();
11
+ const [userInfo, setUserInfo] = useState(null);
12
+ const [loading, setLoading] = useState(false);
13
+ const [error, setError] = useState(null);
14
+ const getUserInfo = useCallback(async () => {
15
+ setLoading(true);
16
+ setError(null);
17
+ try {
18
+ const userInfo = await web3Auth.getUserInfo();
19
+ setUserInfo(userInfo);
20
+ return userInfo;
21
+ } catch (error) {
22
+ setError(error);
23
+ } finally {
24
+ setLoading(false);
25
+ }
26
+ }, [web3Auth]);
27
+ useEffect(() => {
28
+ const saveUserInfo = async () => {
29
+ const userInfo = await getUserInfo();
30
+ setUserInfo(userInfo);
31
+ setIsMFAEnabled((userInfo === null || userInfo === void 0 ? void 0 : userInfo.isMfaEnabled) || false);
32
+ };
33
+ if (isConnected && !userInfo) saveUserInfo();
34
+ if (!isConnected && userInfo) {
35
+ setUserInfo(null);
36
+ setIsMFAEnabled(false);
37
+ }
38
+ }, [isConnected, userInfo, getUserInfo, setIsMFAEnabled]);
39
+ return {
40
+ loading,
41
+ error,
42
+ userInfo,
43
+ isMFAEnabled,
44
+ getUserInfo
45
+ };
46
+ };
47
+
48
+ export { useWeb3AuthUser };