Skip to content

Commit

Permalink
chore: life cycle methods for address change
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk committed Oct 14, 2024
1 parent bf2d666 commit d57d072
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
21 changes: 14 additions & 7 deletions packages/scaffold-ui/src/modal/w3m-modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,24 @@ export class W3mModal extends LitElement {
const nextConnected = caipAddress ? CoreHelperUtil.getPlainAddress(caipAddress) : undefined
const isSameAddress = prevConnected === nextConnected

this.caipAddress = caipAddress

if (nextConnected && !isSameAddress && this.isSiweEnabled) {
const { SIWEController } = await import('@reown/appkit-siwe')
const signed = AccountController.state.siweStatus === 'success'
try {
const { SIWEController } = await import('@reown/appkit-siwe')
const signed = AccountController.state.siweStatus === 'success'

if (!prevConnected && nextConnected) {
this.onSiweNavigation()
} else if (signed && prevConnected && nextConnected && prevConnected !== nextConnected) {
if (SIWEController.state._client?.options.signOutOnAccountChange) {
await SIWEController.signOut()
if (!prevConnected && nextConnected) {
this.onSiweNavigation()
} else if (signed && prevConnected && nextConnected && prevConnected !== nextConnected) {
if (SIWEController.state._client?.options.signOutOnAccountChange) {
await SIWEController.signOut()
this.onSiweNavigation()
}
}
} catch (err) {
this.caipAddress = prevCaipAddress
throw err
}
}

Expand Down
38 changes: 22 additions & 16 deletions packages/siwe/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,29 @@ export class AppKitSIWEClient {
})
}
const clientId = ConnectionController.state.wcClientId
const signature = await ConnectionController.signMessage(message)
const isValid = await this.methods.verifyMessage({ message, signature, clientId })
if (!isValid) {
throw new Error('Error verifying SIWE signature')
try {
const signature = await ConnectionController.signMessage(message)
const isValid = await this.methods.verifyMessage({ message, signature, clientId })
if (!isValid) {
throw new Error('Error verifying SIWE signature')
}

const session = await this.methods.getSession()

if (!session) {
throw new Error('Error verifying SIWE signature')
}

if (this.methods.onSignIn) {
await this.methods.onSignIn(session)
}

return session
} catch (err) {
const error = err as Error
// @ts-expect-error We don't really know what is the properties of thrown error from signMessage that it could be different based on the adapter
throw Error(error?.cause?.message || error?.message || error)
}

const session = await this.methods.getSession()

if (!session) {
throw new Error('Error verifying SIWE signature')
}

if (this.methods.onSignIn) {
await this.methods.onSignIn(session)
}

return session
}

async signOut() {
Expand Down

0 comments on commit d57d072

Please sign in to comment.