Skip to main content

Usage

Requirements​

Install the Web3Modal SIWE package.​

npm i @web3modal/siwe siwe

Configure your SIWE Client​

import { SiweMessage } from 'siwe'
import { createSIWEConfig } from '@web3modal/siwe'
import type { SIWECreateMessageArgs, SIWESession, SIWEVerifyMessageArgs } from '@web3modal/core'

const siweConfig = createSIWEConfig({
createMessage: ({ nonce, address, chainId }: SIWECreateMessageArgs) =>
new SiweMessage({
version: '1',
domain: window.location.host,
uri: window.location.origin,
address,
chainId,
nonce,
// Human-readable ASCII assertion that the user will sign, and it must not contain `\n`.
statement: 'Sign in With Ethereum.'
}).prepareMessage(),
getNonce: async () => {
// Fetch nonce from your SIWE server
const nonce = await getNonce()
if (!nonce) {
throw new Error('Failed to get nonce!')
}

return nonce
},
getSession: async () => {
// Fetch currently authenticated user
const session = await getSession()
if (!session) {
throw new Error('Failed to get session!')
}

const { address, chainId } = session

return { address, chainId }
},
verifyMessage: async ({ message, signature }: SIWEVerifyMessageArgs) => {
try {
// Use your SIWE server to verify if the message and the signature are valid
// Your back-end will tipically rely on SiweMessage(message).validate(signature)
const isValid = await validateMessage({ message, signature })

return isValid
} catch (error) {
return false
}
},
signOut: async () => {
try {
// Sign out by calling the relevant endpoint on your back-end
await signOut()

return true
} catch (error) {
return false
}
}
})

Initialize Web3Modal with your siweConfig.​

createWeb3Modal({
siweConfig,
// Refer to https://docs.walletconnect.com/web3modal/react/about for the other options.
wagmiConfig, // or ethersConfig
projectId,
chains
})

SIWE Config reference​

interface SIWEConfig {
// Required
getNonce: () => Promise<string>
createMessage: (args: SIWECreateMessageArgs) => string
verifyMessage: (args: SIWEVerifyMessageArgs) => Promise<boolean>
getSession: () => Promise<SIWESession | null>
signOut: () => Promise<boolean>

// Optional
onSignIn?: (session?: SIWESession) => void
onSignOut?: () => void
// Defaults to true
enabled?: boolean
// In milliseconds, defaults to 5 minutes
nonceRefetchIntervalMs?: number
// In milliseconds, defaults to 5 minutes
sessionRefetchIntervalMs?: number
// Defaults to true
signOutOnDisconnect?: boolean
// Defaults to true
signOutOnAccountChange?: boolean
// Defaults to true
signOutOnNetworkChange?: boolean
}

Required​

getNonce​

The getNonce method functions as a safeguard against spoofing, akin to a CSRF token. The siwe package provides a generateNonce() helper, or you can utilize an existing CSRF token from your backend if available.

createMessage​

The official siwe package offers a straightforward method for generating an EIP-4361-compatible message, which can subsequently be authenticated using the same package. The nonce parameter is derived from your getNonce endpoint, while the address and chainId variables are sourced from the presently connected wallet.

verifyMessage​

The verifyMessage method should lean on the siwe package's new

SiweMessage(message).validate(signature)

to ensure the message is valid, has not been tampered with, and has been appropriately signed by the wallet address.

getSession​

The backend session should store the associated address and chainId and return it via the getSession method.

signOut​

The users session can be destroyed calling signOut.

Optional​

onSignIn (session?: SIWESession) => void​

Callback when user signs in.

onSignOut () => void​

Callback when user signs out.

enabled boolean - defaults to true​

Whether or not to enable SIWE. Defaults to true.

nonceRefetchIntervalMs number - defaults to 300000ms (5 minutes)​

How often to refetch the nonce, in milliseconds.

sessionRefetchIntervalMs number - defaults to 300000ms (5 minutes)​

How often to refetch the session, in milliseconds.

signOutOnDisconnect boolean - defaults to true​

Whether or not to sign out when the user disconnects their wallet.

signOutOnAccountChange boolean - defaults to true​

Users will be signed out and redirected to the SIWE view to sign a new message in order to keep the SIWE session in sync with the connected account.

signOutOnNetworkChange boolean - defaults to true​

Users will be signed out and redirected to the SIWE view to sign a new message in order to keep the SIWE session in sync with the connected account/network.