Skip to main content

Try it Live

Run SIWE examples in the interactive playground

BrandedSiwe

Branded type for SIWE messages with type safety.

Overview

BrandedMessage provides type-safe SIWE message representation using TypeScript’s structural typing. All SIWE operations work with BrandedMessage type.

Type Definition

Generic Parameters

TDomain

  • Type: string literal type
  • Default: string
  • Purpose: Exact domain typing
  • Example: "example.com" vs string

TAddress

  • Type: AddressType
  • Default: AddressType
  • Purpose: Type-safe Ethereum address (20 bytes)

TUri

  • Type: string literal type
  • Default: string
  • Purpose: Exact URI typing
  • Example: "https://example.com" vs string

TVersion

  • Type: string literal type
  • Default: string
  • Purpose: Version typing (always “1” currently)

TChainId

  • Type: number literal type
  • Default: number
  • Purpose: Exact chain ID typing
  • Example: 1 vs number

Field Types

Required Fields

Optional Fields

Type Examples

Basic Usage

Literal Type Preservation

With Optional Fields

Type-Safe Functions

Namespace Pattern

BrandedSiwe follows namespace pattern for tree-shakeable methods:

Usage

Type Guards

Runtime Type Checking

Validation Type Guard

Signature

65-byte ECDSA signature (r + s + v)

ValidationResult

Discriminated union for validation results

ValidationError

Discriminated union for error types

Type Safety Benefits

Compile-Time Checks

Inference

Optional Field Handling

Pattern Details

Data-Based Architecture

All Siwe code follows data-based pattern:
  • Data: TypeScript interfaces (BrandedMessage)
  • Methods: Namespace functions operating on data
  • No classes: Functions take data as first argument

Tree-Shakeable

Individual functions can be imported:

Type Conventions

  • BrandedMessage - Message type
  • Signature - Signature type
  • ValidationResult - Result type
  • ValidationError - Error type
All follow Branded* or *Result naming.

Implementation Notes

  • Plain objects: No class instantiation overhead
  • Immutable operations: Functions don’t mutate inputs
  • Type-safe: Full TypeScript type checking
  • Serializable: JSON-compatible data structures
  • No brand field: Uses structural typing, no runtime brand

See Also