Skip to content

createSiweMessage

Creates EIP-4361 formatted message.

Import

import { createSiweMessage } from 'viem/siwe'

Usage

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
})

Returns

string

EIP-4361 formatted message.

Parameters

address

  • Type: Address

The Ethereum address performing the signing.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', 
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
})

chainId

  • Type: number

The EIP-155 Chain ID to which the session is bound.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  chainId: 1, 
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
})

domain

  • Type: string

RFC 3986 authority that is requesting the signing.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com', 
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
})

nonce

  • Type: string

A random string typically chosen by the relying party and used to prevent replay attacks.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz', 
  uri: 'https://example.com/path',
  version: '1',
})

uri

  • Type: string

RFC 3986 URI referring to the resource that is the subject of the signing (as in the subject of a claim).

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path', 
  version: '1',
})

version

  • Type: '1'

The current version of the SIWE Message.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1', 
})

expirationTime (optional)

  • Type: Date

Time when the signed authentication message is no longer valid.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
  expirationTime: new Date(),  
})

issuedAt (optional)

  • Type: Date

Time when the message was generated, typically the current time.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
  issuedAt: new Date(),  
})

notBefore (optional)

  • Type: Date

Time when the signed authentication message will become valid.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
  notBefore: new Date(),  
})

requestId (optional)

  • Type: string

A system-specific identifier that may be used to uniquely refer to the sign-in request.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
  requestId: '123e4567-e89b-12d3-a456-426614174000', 
})

resources (optional)

  • Type: string[]

A list of information or references to information the user wishes to have resolved as part of authentication by the relying party.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
  resources: [ 
    'https://example.com/foo', 
    'https://example.com/bar', 
    'https://example.com/baz', 
  ], 
})

scheme (optional)

  • Type: string

RFC 3986 URI scheme of the origin of the request.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
  scheme: 'https', 
})

statement (optional)

  • Type: string

A human-readable ASCII assertion that the user will sign.

import { createSiweMessage } from 'viem/siwe'
 
const message = createSiweMessage({
  address: '0xa0cf798816d4b9b9866b5330eea46a18382f251e',
  chainId: 1,
  domain: 'example.com',
  nonce: 'foobarbaz',
  uri: 'https://example.com/path',
  version: '1',
  statement: 'I accept the ExampleOrg Terms of Service: https://example.com/tos', 
})