The SiweMessage class is the primary interface for creating, formatting, and verifying Sign-In with Ethereum (EIP-4361) messages. You can instantiate it using either a raw SIWE message string or a partial configuration object.
Key Capabilities
- Instantiation: Create a message object from a string (parsed via ABNF) or an object.
- Message Preparation: Generate the exact string required for EIP-191 signing using
prepareMessage() or toMessage(). - Verification: Validate a signature against the message using the
verify() method.
Fields
domain: The RFC 4501 DNS authority requesting the signature.address: The Ethereum address performing the signing (EIP-55 checksummed).chainId: The EIP-155 Chain ID.nonce: A random token (at least 8 alphanumeric characters) to prevent replay attacks.uri: The resource subject to the signing.version: The current version of the message (e.g., '1').statement: An optional human-readable ASCII assertion.expirationTime / notBefore: ISO 8601 datetime strings for validity windows.resources: An array of RFC 3986 URIs for the user to resolve.
import { SiweMessage } from 'siwe';
// 1. Create a message from an object
const message = new SiweMessage({
domain: 'example.com',
address: '0x...',
statement: 'Sign in with your Ethereum account',
uri: 'https://example.com',
version: '1',
chainId: 1,
nonce: 'random-nonce-string',
});
// 2. Get the string to be signed by the user
const textToSign = message.prepareMessage();
// 3. Verify the signature after the user signs it
const siweResponse = await message.verify({
signature: '0x...',
// optional: scheme, domain, nonce, time
});
if (siweResponse.success) {
console.log('Verified!', siweResponse.data);
} else {
console.error('Verification failed:', siweResponse.error);
}