Skip to content

Commit

Permalink
fix: siwe tests (#2262)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir authored May 13, 2024
1 parent 474177c commit c8419dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { customElement } from '@web3modal/ui'
import { LitElement, html } from 'lit'
import { state } from 'lit/decorators.js'
import { SIWEController } from '../../../core/controller/SIWEController.js'
import { W3mFrameRpcConstants } from '@web3modal/wallet'

@customElement('w3m-connecting-siwe-view')
export class W3mConnectingSiweView extends LitElement {
Expand Down Expand Up @@ -95,8 +96,12 @@ export class W3mConnectingSiweView extends LitElement {

return session
} catch (error) {
SnackController.showError('Signature declined')

const preferredAccountType = AccountController.state.preferredAccountType
if (preferredAccountType === W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT) {
SnackController.showError('This application might not support Smart Accounts')
} else {
SnackController.showError('Signature declined')
}
SIWEController.setStatus('error')

return EventsController.sendEvent({
Expand Down
52 changes: 17 additions & 35 deletions packages/siwe/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import {
ConnectionController,
RouterUtil,
RouterController,
StorageUtil,
ModalController,
SnackController
StorageUtil
} from '@web3modal/core'

import { NetworkUtil } from '@web3modal/common'
import { ConstantsUtil } from '../core/utils/ConstantsUtil.js'
import { W3mFrameRpcConstants } from '@web3modal/wallet'

// -- Client -------------------------------------------------------------------- //
export class Web3ModalSIWEClient {
Expand Down Expand Up @@ -117,42 +114,27 @@ export class Web3ModalSIWEClient {
goBack: false,
replace: true,
onCancel() {
ConnectionController.disconnect().then(() => ModalController.close())
RouterController.replace('ConnectingSiwe')
}
})
}
try {
const signature = await ConnectionController.signMessage(message)
const isValid = await this.methods.verifyMessage({ message, signature })
if (!isValid) {
let errorMessage = 'Error verifying SIWE signature'
if (
AccountController.state.preferredAccountType ===
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT
) {
errorMessage = 'This application might not support Smart Account connections'
}
const signature = await ConnectionController.signMessage(message)
const isValid = await this.methods.verifyMessage({ message, signature })
if (!isValid) {
throw new Error('Error verifying SIWE signature')
}

throw new Error(errorMessage)
}

const session = await this.methods.getSession()
if (!session) {
throw new Error('Error verifying SIWE signature')
}
if (this.methods.onSignIn) {
this.methods.onSignIn(session)
}

RouterUtil.navigateAfterNetworkSwitch()

return session
} catch (error) {
SnackController.showError((error as Error).message)
await ConnectionController.disconnect()
ModalController.close()
throw error
const session = await this.methods.getSession()
if (!session) {
throw new Error('Error verifying SIWE signature')
}
if (this.methods.onSignIn) {
this.methods.onSignIn(session)
}

RouterUtil.navigateAfterNetworkSwitch()

return session
}

async signOut() {
Expand Down

0 comments on commit c8419dd

Please sign in to comment.