From d204b062244f9f2bd5c1945fb2376ea191f3c9a1 Mon Sep 17 00:00:00 2001 From: Enes Date: Wed, 29 May 2024 15:26:25 +0300 Subject: [PATCH] refactor(siwe): enable signout options (#2341) --- apps/laboratory/src/utils/SiweUtils.ts | 2 ++ packages/core/src/utils/CoreHelperUtil.ts | 8 +++-- .../scaffold/src/modal/w3m-modal/index.ts | 32 +++++++++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/apps/laboratory/src/utils/SiweUtils.ts b/apps/laboratory/src/utils/SiweUtils.ts index 18498eceb0..14e18445d8 100644 --- a/apps/laboratory/src/utils/SiweUtils.ts +++ b/apps/laboratory/src/utils/SiweUtils.ts @@ -4,6 +4,8 @@ import { createSIWEConfig, formatMessage } from '@web3modal/siwe' import { WagmiConstantsUtil } from '../utils/WagmiConstants' export const siweConfig = createSIWEConfig({ + signOutOnAccountChange: true, + signOutOnNetworkChange: true, // We don't require any async action to populate params but other apps might // eslint-disable-next-line @typescript-eslint/require-await getMessageParams: async () => ({ diff --git a/packages/core/src/utils/CoreHelperUtil.ts b/packages/core/src/utils/CoreHelperUtil.ts index 1f6533a3b5..68fbeb4a5e 100644 --- a/packages/core/src/utils/CoreHelperUtil.ts +++ b/packages/core/src/utils/CoreHelperUtil.ts @@ -50,8 +50,12 @@ export const CoreHelperUtil = { return Date.now() + ConstantsUtil.FOUR_MINUTES_MS }, - getPlainAddress(caipAddress: CaipAddress) { - return caipAddress.split(':')[2] + getNetworkId(caipAddress: CaipAddress | undefined) { + return caipAddress?.split(':')[1] + }, + + getPlainAddress(caipAddress: CaipAddress | undefined) { + return caipAddress?.split(':')[2] }, async wait(milliseconds: number) { diff --git a/packages/scaffold/src/modal/w3m-modal/index.ts b/packages/scaffold/src/modal/w3m-modal/index.ts index eda45549cc..7f2b349a7a 100644 --- a/packages/scaffold/src/modal/w3m-modal/index.ts +++ b/packages/scaffold/src/modal/w3m-modal/index.ts @@ -2,6 +2,7 @@ import { AccountController, ApiController, ConnectionController, + CoreHelperUtil, EventsController, ModalController, OptionsController, @@ -167,19 +168,38 @@ export class W3mModal extends LitElement { if (!this.connected || this.loading) { return } - const hasNetworkChanged = this.caipAddress && this.caipAddress !== caipAddress + const previousAddress = CoreHelperUtil.getPlainAddress(this.caipAddress) + const newAddress = CoreHelperUtil.getPlainAddress(caipAddress) + const previousNetworkId = CoreHelperUtil.getNetworkId(this.caipAddress) + const newNetworkId = CoreHelperUtil.getNetworkId(caipAddress) this.caipAddress = caipAddress + if (this.isSiweEnabled) { const { SIWEController } = await import('@web3modal/siwe') const session = await SIWEController.getSession() - if (session && hasNetworkChanged) { - await SIWEController.signOut() - this.onSiweNavigation() - } else { - this.onSiweNavigation() + // If the address has changed and signOnAccountChange is enabled, sign out + if (session && previousAddress && newAddress && previousAddress !== newAddress) { + if (SIWEController.state._client?.options.signOutOnAccountChange) { + await SIWEController.signOut() + this.onSiweNavigation() + } + + return + } + + // If the network has changed and signOnNetworkChange is enabled, sign out + if (session && previousNetworkId && newNetworkId && previousNetworkId !== newNetworkId) { + if (SIWEController.state._client?.options.signOutOnNetworkChange) { + await SIWEController.signOut() + this.onSiweNavigation() + } + + return } + + this.onSiweNavigation() } }