Installation

Install @siws/core and @siws/better-auth to add Solana wallet authentication to your application.

Install @siws/core

@siws/core is a core library for building and verifying SIWS messages.

pnpm add @siws/core

Requirements: Node.js >= 18. No native dependencies -- uses @noble/curves and bs58.

Usage

import { SiwsMessageBuilder, generateNonce, verifySignature } from '@siws/core'
 
// Build a message
const message = new SiwsMessageBuilder()
  .domain('example.com')
  .address('7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU')
  .uri('https://example.com/auth')
  .nonce(generateNonce())
  .issuedAt(new Date())
  .cluster('mainnet-beta')
  .build()
 
const messageString = message.toMessage()
 
// Verify a signature
const result = await verifySignature(messageString, signature)

Install @siws/better-auth

A better-auth plugin with automatic nonce management, user creation, and session handling.

pnpm add @siws/better-auth

Peer dependency: better-auth >= 1.0.0

Setup

Server:

auth.ts
auth.ts
import { betterAuth } from 'better-auth'
import { siws } from '@siws/better-auth'
 
export const auth = betterAuth({
  plugins: [
    siws({
      statement: 'Sign in to MyApp',
    }),
  ],
})

Client:

auth-client.ts
auth-client.ts
import { createAuthClient } from 'better-auth/client'
import { siwsClient } from '@siws/better-auth/client'
 
export const auth = createAuthClient({
  plugins: [siwsClient()],
})
 
// Get nonce and message
const { nonce, message } = await auth.siws.getNonce({
  address: walletAddress,
  cluster: 'mainnet-beta',
})
 
// Sign with wallet and verify
const signature = await wallet.signMessage(new TextEncoder().encode(message))
const result = await auth.siws.verify({
  message,
  signature: bs58.encode(signature),
  address: walletAddress,
})