Siwe
Utility functions for working with EIP-4361: Sign-In with Ethereum
Examples
Below are some examples demonstrating common usages of the Siwe
module:
Creating a SIWE Message
SIWE messages can be created using Siwe.createMessage
:
import { Siwe } from 'ox'
Siwe.createMessage({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
chainId: 1,
domain: 'example.com',
nonce: 'foobarbaz',
uri: 'https://example.com/path',
version: '1',
})
"example.com wants you to sign in with your Ethereum account: 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e // @log: URI: https://example.com/path Version: 1 Chain ID: 1 Nonce: foobarbaz Issued At: 2023-02-01T00:00:00.000Z"
Generating SIWE Nonces
SIWE nonces can be generated using Siwe.generateNonce
:
import { Siwe } from 'ox'
Siwe.generateNonce()
'65ed4681d4efe0270b923ff5f4b097b1c95974dc33aeebecd5724c42fd86dfd25dc70b27ef836b2aa22e68f19ebcccc1'
Parsing a SIWE Message
SIWE messages can be parsed using Siwe.parseMessage
:
import { Siwe } from 'ox'
Siwe.parseMessage(`example.com wants you to sign in with your Ethereum account:
0xA0Cf798816D4b9b9866b5330EEa46a18382f251e
I accept the ExampleOrg Terms of Service: https://example.com/tos
URI: https://example.com/path
Version: 1
Chain ID: 1
Nonce: foobarbaz
Issued At: 2023-02-01T00:00:00.000Z`)
{ address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, domain: 'example.com', issuedAt: '2023-02-01T00:00:00.000Z', nonce: 'foobarbaz', statement: 'I accept the ExampleOrg Terms of Service: https://example.com/tos', uri: 'https://example.com/path', version: '1', }
Validating a SIWE Message
SIWE messages can be validated using Siwe.validateMessage
:
import { Siwe } from 'ox'
Siwe.validateMessage({
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
domain: 'example.com',
message: {
address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
chainId: 1,
domain: 'example.com',
nonce: 'foobarbaz',
uri: 'https://example.com/path',
version: '1',
},
nonce: 'foobarbaz',
})
true
Functions
Name | Description |
---|---|
Siwe.createMessage | Creates EIP-4361 formatted message. |
Siwe.generateNonce | Generates random EIP-4361 nonce. |
Siwe.isUri | Check if the given URI is a valid RFC 3986 URI. |
Siwe.parseMessage | EIP-4361 formatted message into message fields object. |
Siwe.validateMessage | Validates EIP-4361 message. |
Errors
Name | Description |
---|---|
Siwe.InvalidMessageFieldError | Thrown when a field in a SIWE Message is invalid. |
Types
Name | Description |
---|---|
Siwe.Message | EIP-4361 message fields. |