Skip to content

Commit

Permalink
refactor(siwe): enable signout options (#2341)
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk authored May 29, 2024
1 parent db3118d commit d204b06
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions apps/laboratory/src/utils/SiweUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => ({
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/utils/CoreHelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 26 additions & 6 deletions packages/scaffold/src/modal/w3m-modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AccountController,
ApiController,
ConnectionController,
CoreHelperUtil,
EventsController,
ModalController,
OptionsController,
Expand Down Expand Up @@ -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()
}
}

Expand Down

0 comments on commit d204b06

Please sign in to comment.