Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear siwx sessions on disconnect #3471

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .changeset/rude-peaches-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@reown/appkit': patch
'@reown/appkit-core': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Clear SIWX sessions when calling ConnectionController.disconnect
25 changes: 23 additions & 2 deletions packages/appkit/src/tests/siwe.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ModalController, OptionsController, RouterController, SIWXUtil } from '@reown/appkit-core'
import {
ConnectionController,
ModalController,
OptionsController,
RouterController,
SIWXUtil
} from '@reown/appkit-core'
import { AppKit } from '@reown/appkit'
import * as networks from '@reown/appkit/networks'
import { createSIWEConfig, type AppKitSIWEClient } from '@reown/appkit-siwe'
Expand Down Expand Up @@ -57,10 +63,15 @@ describe('SIWE mapped to SIWX', () => {
})

// Wait for the appkit to be ready
await new Promise(resolve => setTimeout(resolve, 1))
await new Promise(resolve => setTimeout(resolve, 100))

// Set CAIP address to represent connected state
appkit.setCaipAddress('eip155:1:mock-address', 'eip155')
appkit.setCaipNetwork({
...networks.mainnet,
caipNetworkId: 'eip155:1',
chainNamespace: 'eip155'
})
})

it('should fulfill siwx', () => {
Expand Down Expand Up @@ -170,6 +181,16 @@ describe('SIWE mapped to SIWX', () => {
signature: 'mock-signature'
})
})

it('should clear sessions on Connection.disconnect', async () => {
const signOutSpy = vi.spyOn(siweConfig.methods, 'signOut')
const setSessionsSpy = vi.spyOn(OptionsController.state.siwx!, 'setSessions')

await ConnectionController.disconnect()

expect(signOutSpy).toHaveBeenCalled()
expect(setSessionsSpy).toHaveBeenCalledWith([])
})
})

describe('siweConfig should still work outside of AppKit', () => {
Expand Down
13 changes: 2 additions & 11 deletions packages/core/src/controllers/ConnectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ModalController } from './ModalController.js'
import { ConnectorController } from './ConnectorController.js'
import { EventsController } from './EventsController.js'
import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common'
import { OptionsController } from './OptionsController.js'
import { SIWXUtil } from '../utils/SIWXUtil.js'

// -- Types --------------------------------------------- //
export interface ConnectExternalOptions {
Expand Down Expand Up @@ -249,16 +249,7 @@ export const ConnectionController = {

async disconnect() {
try {
const siwx = OptionsController.state.siwx
if (siwx) {
const activeCaipNetwork = ChainController.getActiveCaipNetwork()
const address = CoreHelperUtil.getPlainAddress(ChainController.getActiveCaipAddress())

if (activeCaipNetwork && address) {
await siwx.revokeSession(activeCaipNetwork.caipNetworkId, address)
}
}

await SIWXUtil.clearSessions()
await ChainController.disconnect()
} catch (error) {
throw new Error('Failed to disconnect')
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/utils/SIWXUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ export const SIWXUtil = {
AccountController.state.preferredAccountType ===
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT
}
},
async clearSessions() {
const siwx = this.getSIWX()

if (siwx) {
await siwx.setSessions([])
}
}
}

Expand Down
Loading