Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions sdk/msp-client/src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { NonceResponse, Session, UserInfo } from "../types.js";
import { getAddress, type WalletClient } from "viem";
import { ModuleBase } from "../base.js";

const DEFAULT_SIWE_VERIFY_RETRY = 10;
const DEFAULT_SIWE_VERIFY_BACKOFF_MS = 100;

export class AuthModule extends ModuleBase {
/**
* Request nonce for SIWE.
Expand Down Expand Up @@ -34,7 +37,11 @@ export class AuthModule extends ModuleBase {
* Full SIWE flow using a `WalletClient`.
* - Derives address, fetches nonce, signs message, verifies and stores session.
*/
async SIWE(wallet: WalletClient, signal?: AbortSignal): Promise<Session> {
async SIWE(
wallet: WalletClient,
retry = DEFAULT_SIWE_VERIFY_RETRY,
signal?: AbortSignal
): Promise<Session> {
// Resolve the current active account from the WalletClient.
// - Browser wallets (e.g., MetaMask) surface the user-selected address here.
// - Viem/local wallets must set `wallet.account` explicitly before calling.
Expand All @@ -53,7 +60,21 @@ export class AuthModule extends ModuleBase {
// Sign using the active account resolved above (string or Account object)
const signature = await wallet.signMessage({ account, message });

return this.verify(message, signature, signal);
// Retry verify up to `retry` times
let lastError: unknown;
for (let attemptIndex = 0; attemptIndex < retry; attemptIndex++) {
try {
return await this.verify(message, signature, signal);
} catch (err) {
lastError = err;
await this.delay(DEFAULT_SIWE_VERIFY_BACKOFF_MS);
}
}
throw lastError instanceof Error ? lastError : new Error("SIWE verification failed");
}

private async delay(ms: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, ms));
}

/**
Expand Down
Loading