Sign In With Ethereum (SIWE)
The Sign in with Ethereum (SIWE) plugin allows users to authenticate using their Ethereum wallets following the ERC-4361 standard. This plugin provides flexibility by allowing you to implement your own message verification and nonce generation logic.
Installation
Migrate the database
Run the migration or generate the schema to add the necessary fields and tables to the database.
See the Schema section to add the fields manually.
Add the Client Plugin
Usage
Generate a Nonce
Before signing a SIWE message, you need to generate a nonce for the wallet address:
Sign In with Ethereum
After generating a nonce and creating a SIWE message, verify the signature to authenticate:
Configuration Options
Server Options
The SIWE plugin accepts the following configuration options:
- domain: The domain name of your application (required for SIWE message generation)
- emailDomainName: The email domain name for creating user accounts when not using anonymous mode. Defaults to the domain from your base URL
- anonymous: Whether to allow anonymous sign-ins without requiring an email. Default is
true
- getNonce: Function to generate a unique nonce for each sign-in attempt. You must implement this function to return a cryptographically secure random string. Must return a
Promise<string>
- verifyMessage: Function to verify the signed SIWE message. Receives message details and should return
Promise<boolean>
- ensLookup: Optional function to lookup ENS names and avatars for Ethereum addresses
Client Options
The SIWE client plugin doesn't require any configuration options, but you can pass them if needed for future extensibility:
Schema
The SIWE plugin adds a walletAddress
table to store user wallet associations:
Field | Type | Description |
---|---|---|
id | string | Primary key |
userId | string | Reference to user.id |
address | string | Ethereum wallet address |
chainId | number | Chain ID (e.g., 1 for Ethereum mainnet) |
isPrimary | boolean | Whether this is the user's primary wallet |
createdAt | date | Creation timestamp |
Example Implementation
Here's a complete example showing how to implement SIWE authentication: