Skip to content

Commit 96e78dc

Browse files
committed
docs: add basic doc comments for SIWE authentication flow [skip ci]
1 parent 47d272a commit 96e78dc

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

apps/leaderboard-backend/src/auth/challengeGenerator.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/**
2+
* Authentication Challenge Generator
3+
*
4+
* This module handles the creation of EIP-4361 (Sign-In with Ethereum) compliant
5+
* authentication challenges using Viem's SIWE utilities.
6+
*
7+
* The flow is as follows:
8+
* 1. Client requests a challenge with their wallet address
9+
* 2. Server generates a challenge message using createSiweMessage
10+
* 3. Server stores the challenge in the database
11+
* 4. Client signs the message with their wallet
12+
* 5. Client sends the signature back for verification
13+
*/
14+
115
import type { Address, Hex } from "@happy.tech/common"
216
import { hashMessage } from "viem"
317
import { createSiweMessage, generateSiweNonce } from "viem/siwe"
@@ -36,9 +50,21 @@ export interface ChallengeMessage {
3650
}
3751

3852
/**
39-
* Generates a secure challenge message for authentication
40-
* Following EIP-4361 (Sign-In with Ethereum) standard
41-
* Includes nonce and timestamps to prevent replay attacks
53+
* Generates a secure challenge message for authentication following EIP-4361 (SIWE) standard
54+
*
55+
* This function creates a standardized message for wallet-based authentication that includes:
56+
* - Domain and URI information to identify the application
57+
* - Wallet address of the user attempting to authenticate
58+
* - A cryptographically secure nonce to prevent replay attacks
59+
* - Timestamps for issuance and expiration to limit the validity period
60+
* - Optional resources that the user will be able to access
61+
* - Optional request ID for tracking purposes
62+
*
63+
* The message is formatted according to the EIP-4361 standard using Viem's SIWE utilities.
64+
* A hash of the message is also generated for database storage and verification.
65+
*
66+
* @param options Configuration options including wallet address and optional parameters
67+
* @returns A challenge message object with all data needed for storage and verification
4268
*/
4369
export function generateChallengeMessage(options: ChallengeMessageOptions): ChallengeMessage {
4470
const { walletAddress, requestId, resources } = options

apps/leaderboard-backend/src/auth/verifySignature.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* Signature Verification Module
3+
*
4+
* This module handles cryptographic verification of signatures against wallet addresses
5+
* using the ERC-1271 standard for Smart Contract Accounts.
6+
*
7+
* The verification process works as follows:
8+
* 1. The message is hashed to create a message hash
9+
* 2. An RPC call is made to the wallet's isValidSignature method
10+
* 3. The wallet implementation validates the signature according to ERC-1271
11+
* 4. The result is interpreted as a boolean indicating validity
12+
*/
13+
114
import type { Address, Hex } from "@happy.tech/common"
215
import { abis } from "@happy.tech/contracts/boop/anvil"
316
import { http, createPublicClient, hashMessage } from "viem"
@@ -17,10 +30,10 @@ export const publicClient = createPublicClient({
1730
* Verifies a signature against a wallet address using ERC-1271 standard
1831
* Makes an RPC call to the smart contract account for signature verification
1932
*
20-
* @param walletAddress - The wallet address to verify against
21-
* @param message - The message that was signed
22-
* @param signature - The signature to verify
23-
* @returns Promise<boolean> - Whether the signature is valid
33+
* @param walletAddress - The wallet address that allegedly signed the message
34+
* @param message - The original message that was signed (plain text)
35+
* @param signature - The signature produced by the wallet (hex string)
36+
* @returns Promise<boolean> - True if the signature is valid, false otherwise
2437
*/
2538
export async function verifySignature(walletAddress: Address, message: string, signature: Hex): Promise<boolean> {
2639
try {

apps/leaderboard-backend/src/repositories/AuthRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ export class AuthRepository {
175175
/**
176176
* Mark a challenge as used to prevent replay attacks
177177
* @param walletAddress The wallet address that requested the challenge
178-
* @param message The message that was signed
179-
* @returns True if the challenge was found and marked as used
178+
* @param message The EIP-4361 compliant message that was signed
179+
* @returns True if the challenge was found and marked as used, false otherwise
180180
*/
181181
async markChallengeAsUsed(walletAddress: Address, message: string): Promise<boolean> {
182182
try {

apps/leaderboard-backend/src/routes/api/authRoutes.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@ import {
1414
AuthVerifyValidation,
1515
} from "../../validation/auth"
1616

17+
/**
18+
* The authentication flow consists of these main steps:
19+
*
20+
* 1. Challenge Generation (/auth/challenge):
21+
* - Client sends their wallet address
22+
* - Server creates a unique challenge message
23+
* - Server returns the message to be signed
24+
*
25+
* 2. Signature Verification (/auth/verify):
26+
* - Client signs the message and sends back signature
27+
* - Server validates message format using SIWE utilities
28+
* - Server checks database for valid challenge
29+
* - Server verifies signature on-chain
30+
* - Server creates session and returns token
31+
*
32+
* 3. Session Management:
33+
* - /auth/me - Get current user info
34+
* - /auth/sessions - List all active sessions
35+
* - /auth/logout - End the current session
36+
*/
37+
1738
export default new Hono()
1839
/**
1940
* Request a challenge to sign
2041
* POST /auth/challenge
21-
*
22-
* Generates an EIP-4361 (Sign-In with Ethereum) compliant challenge message
23-
* for the user to sign with their wallet.
2442
*/
2543
.post("/challenge", AuthChallengeDescription, AuthChallengeValidation, async (c) => {
2644
try {

0 commit comments

Comments
 (0)