diff --git a/apps/laboratory/src/pages/library/wagmi-siwe.tsx b/apps/laboratory/src/pages/library/wagmi-siwe.tsx index 16ac2bf33b..67de4aeba3 100644 --- a/apps/laboratory/src/pages/library/wagmi-siwe.tsx +++ b/apps/laboratory/src/pages/library/wagmi-siwe.tsx @@ -1,5 +1,4 @@ import { createWeb3Modal } from '@web3modal/wagmi/react' -import { appKitAuthConfig } from '@web3modal/siwe' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { WagmiProvider } from 'wagmi' import { Web3ModalButtons } from '../../components/Web3ModalButtons' @@ -9,6 +8,7 @@ import { getWagmiConfig } from '../../utils/WagmiConstants' import { SiweData } from '../../components/Siwe/SiweData' import { ConstantsUtil } from '../../utils/ConstantsUtil' import { WagmiModalInfo } from '../../components/Wagmi/WagmiModalInfo' +import { siweConfig } from '../../utils/SiweUtils' const queryClient = new QueryClient() @@ -18,7 +18,7 @@ const modal = createWeb3Modal({ projectId: ConstantsUtil.ProjectId, enableAnalytics: true, metadata: ConstantsUtil.Metadata, - siweConfig: appKitAuthConfig, + siweConfig, enableOnramp: true, customWallets: ConstantsUtil.CustomWallets }) diff --git a/packages/core/src/controllers/ApiController.ts b/packages/core/src/controllers/ApiController.ts index 684f82f49a..ab95ea9473 100644 --- a/packages/core/src/controllers/ApiController.ts +++ b/packages/core/src/controllers/ApiController.ts @@ -228,29 +228,18 @@ export const ApiController = { ApiController.fetchFeaturedWallets(), ApiController.fetchRecommendedWallets(), ApiController.fetchNetworkImages(), - ApiController.fetchConnectorImages(), - ApiController.fetchProjectConfig() + ApiController.fetchConnectorImages() ] state.prefetchPromise = Promise.race([Promise.allSettled(promises), CoreHelperUtil.wait(3000)]) }, async fetchProjectConfig() { - const { isAnalyticsEnabled, isAppKitAuthEnabled } = await api.get({ - path: '/getProjectConfig', + const { isAnalyticsEnabled } = await api.get({ + path: '/getAnalyticsConfig', headers: ApiController._getApiHeaders() }) - // Only set the analytics state if it's not already set through the SDK config - if (OptionsController.state.enableAnalytics === undefined) { - OptionsController.setEnableAnalytics(isAnalyticsEnabled) - } - - if (isAppKitAuthEnabled) { - const { SIWEController, appKitAuthConfig } = await import('@web3modal/siwe') - SIWEController.setSIWEClient(appKitAuthConfig) - - OptionsController.setIsSiweEnabled(isAppKitAuthEnabled) - } + return { isAnalyticsEnabled, isAppKitAuthEnabled: true } } } diff --git a/packages/core/tests/controllers/ApiController.test.ts b/packages/core/tests/controllers/ApiController.test.ts index ee170d6543..afa2871fc0 100644 --- a/packages/core/tests/controllers/ApiController.test.ts +++ b/packages/core/tests/controllers/ApiController.test.ts @@ -507,7 +507,10 @@ describe('ApiController', () => { .spyOn(ApiController, 'fetchConnectorImages') .mockResolvedValue() - const fetchAnalyticsSpy = vi.spyOn(ApiController, 'fetchProjectConfig').mockResolvedValue() + const fetchAnalyticsSpy = vi.spyOn(ApiController, 'fetchProjectConfig').mockResolvedValue({ + isAnalyticsEnabled: true, + isAppKitAuthEnabled: false + }) ApiController.prefetch() diff --git a/packages/ethers/src/client.ts b/packages/ethers/src/client.ts index c72429ff57..96bb4283b7 100644 --- a/packages/ethers/src/client.ts +++ b/packages/ethers/src/client.ts @@ -197,12 +197,12 @@ export class Web3Modal extends Web3ModalScaffold { onUri(uri) }) - if (siweConfig?.options?.enabled) { - const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe') + const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe') + if (SIWEController.state._client) { const result = await WalletConnectProvider.authenticate({ - nonce: await siweConfig.getNonce(), + nonce: await SIWEController.getNonce(), methods: OPTIONAL_METHODS, - ...(await siweConfig.getMessageParams()) + ...(await SIWEController.state._client.getMessageParams()) }) // Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md const signedCacao = result?.auths?.[0] @@ -313,8 +313,8 @@ export class Web3Modal extends Web3ModalScaffold { const providerType = EthersStoreUtil.state.providerType localStorage.removeItem(EthersConstantsUtil.WALLET_ID) EthersStoreUtil.reset() - if (siweConfig?.options?.signOutOnDisconnect) { - const { SIWEController } = await import('@web3modal/siwe') + const { SIWEController } = await import('@web3modal/siwe') + if (SIWEController.state?._client?.options?.signOutOnDisconnect) { await SIWEController.signOut() } if ( diff --git a/packages/ethers5/src/client.ts b/packages/ethers5/src/client.ts index 944e4c50d0..28bfc2aa44 100644 --- a/packages/ethers5/src/client.ts +++ b/packages/ethers5/src/client.ts @@ -172,12 +172,12 @@ export class Web3Modal extends Web3ModalScaffold { onUri(uri) }) - if (siweConfig?.options?.enabled) { - const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe') + const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe') + if (SIWEController.state._client) { const result = await WalletConnectProvider.authenticate({ - nonce: await siweConfig.getNonce(), + nonce: await SIWEController.getNonce(), methods: OPTIONAL_METHODS, - ...(await siweConfig.getMessageParams()) + ...(await SIWEController.state._client.getMessageParams()) }) // Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md const signedCacao = result?.auths?.[0] @@ -286,8 +286,8 @@ export class Web3Modal extends Web3ModalScaffold { const providerType = EthersStoreUtil.state.providerType localStorage.removeItem(EthersConstantsUtil.WALLET_ID) EthersStoreUtil.reset() - if (siweConfig?.options?.signOutOnDisconnect) { - const { SIWEController } = await import('@web3modal/siwe') + const { SIWEController } = await import('@web3modal/siwe') + if (SIWEController.state._client?.options?.signOutOnDisconnect) { await SIWEController.signOut() } if (providerType === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) { diff --git a/packages/scaffold/src/client.ts b/packages/scaffold/src/client.ts index 88fef169b2..38ad617879 100644 --- a/packages/scaffold/src/client.ts +++ b/packages/scaffold/src/client.ts @@ -26,7 +26,8 @@ import { ThemeController, SnackController, RouterController, - EnsController + EnsController, + ApiController } from '@web3modal/core' import { setColorTheme, setThemeVariables } from '@web3modal/ui' import type { SIWEControllerClient } from '@web3modal/siwe' @@ -309,10 +310,17 @@ export class Web3ModalScaffold { NetworkController.setAllowUnsupportedChain(options.allowUnsupportedChain) } - if (options.siweControllerClient) { - const { SIWEController } = await import('@web3modal/siwe') + const { isAppKitAuthEnabled, isAnalyticsEnabled } = await ApiController.fetchProjectConfig() - SIWEController.setSIWEClient(options.siweControllerClient) + // Only set the analytics state if it's not already set through the SDK config + if (options.enableAnalytics === undefined) { + OptionsController.setEnableAnalytics(isAnalyticsEnabled) + } + + if (options.siweControllerClient || isAppKitAuthEnabled) { + const { SIWEController, appKitAuthConfig } = await import('@web3modal/siwe') + OptionsController.setIsSiweEnabled(true) + SIWEController.setSIWEClient(options.siweControllerClient ?? appKitAuthConfig) } ConnectionController.setClient(options.connectionControllerClient) diff --git a/packages/scaffold/src/partials/w3m-header/index.ts b/packages/scaffold/src/partials/w3m-header/index.ts index 8fe43822ae..8bafef2960 100644 --- a/packages/scaffold/src/partials/w3m-header/index.ts +++ b/packages/scaffold/src/partials/w3m-header/index.ts @@ -120,7 +120,6 @@ export class W3mHeader extends LitElement { // -- Private ------------------------------------------- // - // Temporarily added to test connecting with SIWE, replace with 'WhatIsAWallet' again when approved private onWalletHelp() { EventsController.sendEvent({ type: 'track', event: 'CLICK_WALLET_HELP' }) RouterController.push('WhatIsAWallet') @@ -129,7 +128,7 @@ export class W3mHeader extends LitElement { private async onClose() { if (OptionsController.state.isSiweEnabled) { const { SIWEController } = await import('@web3modal/siwe') - if (SIWEController.state.status !== 'success') { + if (SIWEController.state.status !== 'success' && this.isConnected) { await ConnectionController.disconnect() } } diff --git a/packages/siwe/core/controller/SIWEController.ts b/packages/siwe/core/controller/SIWEController.ts index 25efd39c16..b2d11396b0 100644 --- a/packages/siwe/core/controller/SIWEController.ts +++ b/packages/siwe/core/controller/SIWEController.ts @@ -71,7 +71,7 @@ export const SIWEController = { try { const client = this._getClient() const session = await client.getSession() - if (session) { + if (session?.address && session?.chainId) { this.setSession(session) this.setStatus('success') } diff --git a/packages/wagmi/src/client.ts b/packages/wagmi/src/client.ts index 51cf3ff669..4ac403e302 100644 --- a/packages/wagmi/src/client.ts +++ b/packages/wagmi/src/client.ts @@ -240,8 +240,8 @@ export class Web3Modal extends Web3ModalScaffold { disconnect: async () => { await disconnect(this.wagmiConfig) - if (siweConfig?.options?.signOutOnDisconnect) { - const { SIWEController } = await import('@web3modal/siwe') + const { SIWEController } = await import('@web3modal/siwe') + if (SIWEController.state._client?.options?.signOutOnDisconnect) { await SIWEController.signOut() } },