SIWE

The SIWE utilities provide functions for creating and formatting Sign in with Ethereum (SIWE) messages according to the EIP-4361 standard.

Import

import { createSiweMessageString } from 'ethereum-identity-kit'
 
// For server side usage
import { createSiweMessageString } from 'ethereum-identity-kit/utils'

Functions

createSiweMessageString

Creates a properly formatted SIWE message string from the provided parameters according to the EIP-4361 specification.

const message = createSiweMessageString({
  domain: 'example.com',
  address: '0x1234567890123456789012345678901234567890',
  uri: 'https://example.com',
  version: '1',
  chainId: 1,
  nonce: 'abcd1234',
  issuedAt: '2023-10-01T12:00:00.000Z',
  statement: 'Sign in to access your account',
  expirationTime: '2023-10-01T12:05:00.000Z',
})

Parameters

ParameterTypeDescriptionRequired
domainstringThe domain that is requesting the signingYes
addressstringEthereum address performing the signingYes
uristringA URI the user is signing in toYes
versionstringCurrent version of the SIWE Message (should be “1”)Yes
chainIdnumberEIP-155 Chain ID to which the session is boundYes
noncestringRandomized token to prevent replay attacksYes
issuedAtstringISO 8601 datetime string of when the message was generatedYes
statementstringHuman-readable ASCII assertion that the user will signNo
expirationTimestringISO 8601 datetime string of when the signed authentication expiresNo
notBeforestringISO 8601 datetime string of when the signed authentication becomes validNo
requestIdstringSystem-specific identifier that may be used to refer back to the requestNo
resourcesstring[]List of information or references to information the user wishes to have resolvedNo
schemestringThe scheme of the URI (typically omitted for web applications)No

Return Value

Returns a string containing the properly formatted SIWE message that conforms to the EIP-4361 standard.

Example Output

example.com wants you to sign in with your Ethereum account:
0x1234567890123456789012345678901234567890

Sign in to access your account

URI: https://example.com
Version: 1
Chain ID: 1
Nonce: abcd1234
Issued At: 2023-10-01T12:00:00.000Z
Expiration Time: 2023-10-01T12:05:00.000Z

Example with All Parameters

const message = createSiweMessageString({
  scheme: 'https',
  domain: 'example.com',
  address: '0x1234567890123456789012345678901234567890',
  statement: 'I accept the ExampleOrg Terms of Service: https://example.com/tos',
  uri: 'https://example.com/login',
  version: '1',
  chainId: 1,
  nonce: 'abcd1234',
  issuedAt: '2023-10-01T12:00:00.000Z',
  expirationTime: '2023-10-01T12:05:00.000Z',
  notBefore: '2023-10-01T11:55:00.000Z',
  requestId: 'request-123',
  resources: ['https://example.com/my-web2-claim.json', 'https://example.com/my-web3-claim.json'],
})

Technical Details

  • The function formats the message according to the exact specification in EIP-4361
  • Required fields are always included in a specific order
  • Optional fields are only included if provided and non-empty
  • Resources are formatted as a bulleted list if provided
  • The message structure ensures compatibility with SIWE verification libraries

Use Cases

  • Authentication: Creating messages for wallet-based authentication
  • Session Management: Generating time-bound authentication tokens
  • Access Control: Creating signed messages for resource access
  • Integration: Building SIWE-compatible authentication flows