@web3auth/modal 10.7.0 → 10.8.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.
Files changed (29) hide show
  1. package/dist/lib.cjs/packages/modal/src/config.js +1 -1
  2. package/dist/lib.cjs/packages/modal/src/react/context/Web3AuthInnerContext.js +21 -1
  3. package/dist/lib.cjs/packages/modal/src/react/hooks/useChain.js +16 -0
  4. package/dist/lib.cjs/packages/modal/src/react/index.js +2 -0
  5. package/dist/lib.cjs/packages/modal/src/react/solana/hooks/useSolanaWallet.js +31 -6
  6. package/dist/lib.cjs/packages/modal/src/react/wagmi/provider.js +18 -2
  7. package/dist/lib.cjs/packages/modal/src/ui/components/ConnectWallet/ConnectWallet.js +1 -1
  8. package/dist/lib.cjs/packages/modal/src/vue/composables/useChain.js +24 -0
  9. package/dist/lib.cjs/packages/modal/src/vue/index.js +2 -0
  10. package/dist/lib.cjs/packages/modal/src/vue/solana/composables/useSolanaWallet.js +24 -9
  11. package/dist/lib.cjs/packages/modal/src/vue/wagmi/provider.js +18 -2
  12. package/dist/lib.cjs/types/react/hooks/index.d.ts +1 -0
  13. package/dist/lib.cjs/types/react/hooks/useChain.d.ts +6 -0
  14. package/dist/lib.cjs/types/react/hooks/useWeb3Auth.d.ts +1 -1
  15. package/dist/lib.cjs/types/vue/composables/index.d.ts +1 -0
  16. package/dist/lib.cjs/types/vue/composables/useChain.d.ts +7 -0
  17. package/dist/lib.esm/packages/modal/src/config.js +1 -1
  18. package/dist/lib.esm/packages/modal/src/react/context/Web3AuthInnerContext.js +21 -1
  19. package/dist/lib.esm/packages/modal/src/react/hooks/useChain.js +14 -0
  20. package/dist/lib.esm/packages/modal/src/react/index.js +1 -0
  21. package/dist/lib.esm/packages/modal/src/react/solana/hooks/useSolanaWallet.js +30 -7
  22. package/dist/lib.esm/packages/modal/src/react/wagmi/provider.js +18 -2
  23. package/dist/lib.esm/packages/modal/src/ui/components/ConnectWallet/ConnectWallet.js +1 -1
  24. package/dist/lib.esm/packages/modal/src/vue/composables/useChain.js +22 -0
  25. package/dist/lib.esm/packages/modal/src/vue/index.js +1 -0
  26. package/dist/lib.esm/packages/modal/src/vue/solana/composables/useSolanaWallet.js +25 -10
  27. package/dist/lib.esm/packages/modal/src/vue/wagmi/provider.js +18 -2
  28. package/dist/modal.umd.min.js +1 -1
  29. package/package.json +3 -3
@@ -1,6 +1,7 @@
1
1
  import { Connection } from '@solana/web3.js';
2
2
  import { CHAIN_NAMESPACES, SolanaWallet } from '@web3auth/no-modal';
3
- import { ref, shallowRef, watch } from 'vue';
3
+ import { ref, shallowRef, computed, watch } from 'vue';
4
+ import { useChain } from '../../composables/useChain.js';
4
5
  import '@web3auth/no-modal/vue';
5
6
  import { useWeb3Auth } from '../../composables/useWeb3Auth.js';
6
7
 
@@ -9,12 +10,19 @@ const useSolanaWallet = () => {
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
  }, {
@@ -12,11 +12,14 @@ import { useWeb3AuthDisconnect } from '../composables/useWeb3AuthDisconnect.js';
12
12
  import { defaultWagmiConfig } from './constants.js';
13
13
 
14
14
  const WEB3AUTH_CONNECTOR_ID = "web3auth";
15
+ function getWeb3authConnector(config) {
16
+ return config.connectors.find(c => c.id === WEB3AUTH_CONNECTOR_ID);
17
+ }
15
18
 
16
19
  // Helper to initialize connectors for the given wallets
17
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
21
  async function setupConnector(provider, config) {
19
- let connector = config.connectors.find(c => c.id === WEB3AUTH_CONNECTOR_ID);
22
+ let connector = getWeb3authConnector(config);
20
23
  if (connector) return connector;
21
24
 
22
25
  // Create new connector if not already existing
@@ -53,8 +56,15 @@ async function connectWeb3AuthWithWagmi(connector, config) {
53
56
  status: "connected"
54
57
  }));
55
58
  }
56
- async function disconnectWeb3AuthFromWagmi(config) {
59
+ function resetConnectorState(config) {
57
60
  config._internal.connectors.setState(prev => prev.filter(c => c.id !== WEB3AUTH_CONNECTOR_ID));
61
+ config.connectors.filter(c => c.id !== WEB3AUTH_CONNECTOR_ID);
62
+ }
63
+ async function disconnectWeb3AuthFromWagmi(config) {
64
+ var _config$storage3, _config$storage4;
65
+ const connector = getWeb3authConnector(config);
66
+ 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")]);
67
+ resetConnectorState(config);
58
68
  config.setState(state => _objectSpread(_objectSpread({}, state), {}, {
59
69
  chainId: state.chainId,
60
70
  connections: new Map(),
@@ -80,6 +90,12 @@ const Web3AuthWagmiProvider = defineComponent({
80
90
  onDisconnect: async () => {
81
91
  log.info("Disconnected from wagmi");
82
92
  if (isConnected.value) await disconnect();
93
+ const connector = getWeb3authConnector(wagmiConfig);
94
+ // reset wagmi connector state if the provider handles disconnection because of the accountsChanged event
95
+ // from the connected provider
96
+ if (connector) {
97
+ resetConnectorState(wagmiConfig);
98
+ }
83
99
  }
84
100
  });
85
101
  watch(isConnected, async newIsConnected => {