@web3auth/no-modal 10.6.0 → 10.8.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.
Files changed (28) hide show
  1. package/dist/lib.cjs/base/utils.js +1 -1
  2. package/dist/lib.cjs/react/context/Web3AuthInnerContext.js +21 -1
  3. package/dist/lib.cjs/react/hooks/useChain.js +16 -0
  4. package/dist/lib.cjs/react/index.js +2 -0
  5. package/dist/lib.cjs/react/solana/hooks/useSolanaWallet.js +14 -7
  6. package/dist/lib.cjs/react/wagmi/provider.js +19 -4
  7. package/dist/lib.cjs/types/base/hooks/index.d.ts +3 -0
  8. package/dist/lib.cjs/types/react/hooks/index.d.ts +1 -0
  9. package/dist/lib.cjs/types/react/hooks/useChain.d.ts +6 -0
  10. package/dist/lib.cjs/types/react/hooks/useWeb3Auth.d.ts +1 -1
  11. package/dist/lib.cjs/types/vue/composables/index.d.ts +1 -0
  12. package/dist/lib.cjs/types/vue/composables/useChain.d.ts +7 -0
  13. package/dist/lib.cjs/vue/composables/useChain.js +24 -0
  14. package/dist/lib.cjs/vue/index.js +2 -0
  15. package/dist/lib.cjs/vue/solana/composables/useSolanaWallet.js +24 -9
  16. package/dist/lib.cjs/vue/wagmi/provider.js +21 -8
  17. package/dist/lib.esm/base/utils.js +1 -1
  18. package/dist/lib.esm/react/context/Web3AuthInnerContext.js +21 -1
  19. package/dist/lib.esm/react/hooks/useChain.js +14 -0
  20. package/dist/lib.esm/react/index.js +1 -0
  21. package/dist/lib.esm/react/solana/hooks/useSolanaWallet.js +12 -7
  22. package/dist/lib.esm/react/wagmi/provider.js +18 -3
  23. package/dist/lib.esm/vue/composables/useChain.js +22 -0
  24. package/dist/lib.esm/vue/index.js +1 -0
  25. package/dist/lib.esm/vue/solana/composables/useSolanaWallet.js +25 -10
  26. package/dist/lib.esm/vue/wagmi/provider.js +21 -8
  27. package/dist/noModal.umd.min.js +1 -1
  28. package/package.json +7 -7
@@ -1,4 +1,5 @@
1
1
  export { Web3AuthProvider } from './Web3AuthProvider.js';
2
+ export { useChain } from './composables/useChain.js';
2
3
  export { useCheckout } from './composables/useCheckout.js';
3
4
  export { useEnableMFA } from './composables/useEnableMFA.js';
4
5
  export { useFunding } from './composables/useFunding.js';
@@ -1,20 +1,28 @@
1
1
  import { Connection } from '@solana/web3.js';
2
- import { ref, shallowRef, watch } from 'vue';
2
+ import { ref, shallowRef, computed, watch } from 'vue';
3
3
  import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers';
4
4
  import { SolanaWallet } from '../../../providers/solana-provider/solanaWallet.js';
5
5
  import { useWeb3Auth } from '../../composables/useWeb3Auth.js';
6
+ import { useChain } from '../../composables/useChain.js';
6
7
 
7
8
  const useSolanaWallet = () => {
8
9
  const {
9
10
  provider,
10
11
  web3Auth
11
12
  } = useWeb3Auth();
13
+ const {
14
+ chainNamespace
15
+ } = useChain();
12
16
  const accounts = ref([]);
13
17
  const solanaWallet = shallowRef(null);
14
18
  const connection = shallowRef(null);
19
+ const isSolana = computed(() => chainNamespace.value === CHAIN_NAMESPACES.SOLANA);
15
20
  const setupWallet = async () => {
16
- var _web3Auth$value, _web3Auth$value2;
17
- if (!((_web3Auth$value = web3Auth.value) !== null && _web3Auth$value !== void 0 && (_web3Auth$value = _web3Auth$value.currentChain) !== null && _web3Auth$value !== void 0 && _web3Auth$value.chainNamespace) || web3Auth.value.currentChain.chainNamespace !== CHAIN_NAMESPACES.SOLANA) {
21
+ var _web3Auth$value;
22
+ if (!isSolana.value) {
23
+ return;
24
+ }
25
+ if (!provider.value) {
18
26
  return;
19
27
  }
20
28
  solanaWallet.value = new SolanaWallet(provider.value);
@@ -22,19 +30,26 @@ const useSolanaWallet = () => {
22
30
  if ((result === null || result === void 0 ? void 0 : result.length) > 0) {
23
31
  accounts.value = result;
24
32
  }
25
- connection.value = new Connection((_web3Auth$value2 = web3Auth.value) === null || _web3Auth$value2 === void 0 || (_web3Auth$value2 = _web3Auth$value2.currentChain) === null || _web3Auth$value2 === void 0 ? void 0 : _web3Auth$value2.rpcTarget);
33
+ if ((_web3Auth$value = web3Auth.value) !== null && _web3Auth$value !== void 0 && (_web3Auth$value = _web3Auth$value.currentChain) !== null && _web3Auth$value !== void 0 && _web3Auth$value.rpcTarget) {
34
+ connection.value = new Connection(web3Auth.value.currentChain.rpcTarget);
35
+ }
36
+ };
37
+ const resetWallet = () => {
38
+ solanaWallet.value = null;
39
+ accounts.value = null;
40
+ connection.value = null;
26
41
  };
27
42
  if (provider.value && !solanaWallet.value) {
28
43
  setupWallet();
29
44
  }
30
- watch(provider, async newVal => {
31
- if (!newVal && solanaWallet.value) {
32
- solanaWallet.value = null;
33
- accounts.value = null;
34
- connection.value = null;
45
+ watch([provider, chainNamespace], async ([newProvider, newChainNamespace]) => {
46
+ if (!newProvider || newChainNamespace !== CHAIN_NAMESPACES.SOLANA) {
47
+ if (solanaWallet.value) {
48
+ resetWallet();
49
+ }
35
50
  return;
36
51
  }
37
- if (newVal && !solanaWallet.value) {
52
+ if (newProvider && !solanaWallet.value) {
38
53
  setupWallet();
39
54
  }
40
55
  }, {
@@ -13,11 +13,14 @@ import { CHAIN_NAMESPACES } from '@toruslabs/base-controllers';
13
13
  import { WalletInitializationError } from '../../base/errors/index.js';
14
14
 
15
15
  const WEB3AUTH_CONNECTOR_ID = "web3auth";
16
+ function getWeb3authConnector(config) {
17
+ return config.connectors.find(c => c.id === WEB3AUTH_CONNECTOR_ID);
18
+ }
16
19
 
17
20
  // Helper to initialize connectors for the given wallets
18
21
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
22
  async function setupConnector(provider, config) {
20
- let connector = config.connectors.find(c => c.id === WEB3AUTH_CONNECTOR_ID);
23
+ let connector = getWeb3authConnector(config);
21
24
  if (connector) return connector;
22
25
 
23
26
  // Create new connector if not already existing
@@ -54,8 +57,15 @@ async function connectWeb3AuthWithWagmi(connector, config) {
54
57
  status: "connected"
55
58
  }));
56
59
  }
57
- async function disconnectWeb3AuthFromWagmi(config) {
60
+ function resetConnectorState(config) {
58
61
  config._internal.connectors.setState(prev => prev.filter(c => c.id !== WEB3AUTH_CONNECTOR_ID));
62
+ config.connectors.filter(c => c.id !== WEB3AUTH_CONNECTOR_ID);
63
+ }
64
+ async function disconnectWeb3AuthFromWagmi(config) {
65
+ var _config$storage3, _config$storage4;
66
+ const connector = getWeb3authConnector(config);
67
+ await Promise.all([(_config$storage3 = config.storage) === null || _config$storage3 === void 0 ? void 0 : _config$storage3.setItem(`${connector === null || connector === void 0 ? void 0 : connector.id}.disconnected`, true), (_config$storage4 = config.storage) === null || _config$storage4 === void 0 ? void 0 : _config$storage4.removeItem("injected.connected")]);
68
+ resetConnectorState(config);
59
69
  config.setState(state => _objectSpread(_objectSpread({}, state), {}, {
60
70
  chainId: state.chainId,
61
71
  connections: new Map(),
@@ -81,6 +91,12 @@ const Web3AuthWagmiProvider = defineComponent({
81
91
  onDisconnect: async () => {
82
92
  log.info("Disconnected from wagmi");
83
93
  if (isConnected.value) await disconnect();
94
+ const connector = getWeb3authConnector(wagmiConfig);
95
+ // reset wagmi connector state if the provider handles disconnection because of the accountsChanged event
96
+ // from the connected provider
97
+ if (connector) {
98
+ resetConnectorState(wagmiConfig);
99
+ }
84
100
  }
85
101
  });
86
102
  watch(isConnected, async newIsConnected => {
@@ -215,12 +231,12 @@ const WagmiProvider = defineComponent({
215
231
  }, props.config));
216
232
  }
217
233
  };
218
- watch(isInitialized, newIsInitialized => {
234
+ watch(isInitialized, (newIsInitialized, prevIsInitialized) => {
219
235
  var _web3Auth$value2;
220
236
  (_web3Auth$value2 = web3Auth.value) === null || _web3Auth$value2 === void 0 || _web3Auth$value2.setAnalyticsProperties({
221
237
  wagmi_enabled: true
222
238
  });
223
- if (newIsInitialized && !finalConfig.value) {
239
+ if (newIsInitialized && !prevIsInitialized) {
224
240
  finalConfig.value = defineWagmiConfig();
225
241
  hydrateWagmiConfig();
226
242
  configKey.value = randomId();
@@ -237,10 +253,7 @@ const WagmiProvider = defineComponent({
237
253
  };
238
254
  },
239
255
  render() {
240
- if (!this.finalConfig) return null;
241
- return h(Web3AuthWagmiInnerProvider,
242
- // This key is used to remount the provider when the config changes.
243
- {
256
+ return h(Web3AuthWagmiInnerProvider, {
244
257
  config: this.finalConfig,
245
258
  key: this.configKey
246
259
  }, {