Auth Server
Dedicated authentication server — routes, security model, and setup
Overview
StarkFi includes a dedicated authentication server (server/) that bridges Privy authentication to app-managed Starknet wallets. It ensures the CLI client never directly processes or stores sensitive Privy API keys.
The server is built with Hono and handles four critical functions:
- Email OTP — Sends and verifies one-time passwords via Privy
- Wallet Management — Creates and retrieves app-managed Starknet wallets
- Transaction Signing — Proxies signing requests to Privy's Trusted Execution Environment (TEE)
- Paymaster Proxy — Forwards gas abstraction requests to the AVNU Paymaster
Important: The auth server must be running before using any StarkFi CLI or MCP commands.
Security Model
The server employs a strict security architecture designed around access minimization and key isolation:
| Security Layer | How It Works |
|---|---|
| TEE Isolation | Private keys generated by Privy never leave the Trusted Execution Environment. Neither the server nor the CLI can access raw key material. Wallets are app-managed, so the server (via PRIVY_APP_SECRET) can request signatures for any user's wallet; operating it is a custodial role. |
| JWT Authorization | All signing and paymaster endpoints require a valid HS256 JWT. Verification checks the signature, exp (required), iss = starkfi-server and aud = starkfi-cli. Tokens are issued on successful OTP verification with a 7-day expiration: the CLI has no refresh flow, so a shorter lifetime would force frequent interactive OTP logins for long-running MCP clients. |
| Session Revocation | Each token carries the user's session epoch, stored in the user's Privy custom metadata next to server_wallet_id. POST /auth/logout increments the epoch, which revokes every token issued to that account before it, on every server instance. Every authenticated request reads the epoch from Privy, so these endpoints fail closed (HTTP 502) when Privy's API is unreachable. |
| Rate Limiting | Auth endpoints: 5 req/min per IP. Signing endpoints: 30 req/min per IP. Paymaster: 60 req/min per IP, 30 req/min and 50 sponsored executions/day per user. Limits are in-memory and per instance. |
| CORS | Configurable allowlist via ALLOWED_ORIGINS environment variable. |
| Secure Headers | X-Content-Type-Options, X-Frame-Options, and other security headers applied globally. |
| Body Limit | Maximum 1 MB request body size to prevent abuse. |
| Graceful Shutdown | Handles SIGTERM/SIGINT with a 5-second force-kill timeout. |
API Endpoints
Authentication
| Endpoint | Auth | Rate Limit | Description |
|---|---|---|---|
POST /auth/login | None | 5 req/min | Sends an email OTP to the provided address. |
POST /auth/verify | None | 5 req/min | Validates the OTP, provisions a wallet, and returns a JWT. |
POST /auth/logout | JWT | 5 req/min | Revokes every session token of the caller's account (all devices). |
Wallet
| Endpoint | Auth | Rate Limit | Description |
|---|---|---|---|
POST /wallet/find | JWT | — | Retrieves the Starknet wallet address for the authenticated user. |
Transaction Signing
| Endpoint | Auth | Rate Limit | Description |
|---|---|---|---|
POST /sign/hash | JWT | 30 req/min | Signs a raw Starknet payload hash via Privy TEE. |
Paymaster
| Endpoint | Auth | Rate Limit | Description |
|---|---|---|---|
POST /paymaster | JWT | 60/min/IP, 30/min/user, 50 executions/day/user | Forwards only paymaster_buildTransaction and paymaster_executeTransaction (the calls starknet.js makes for sponsored execution) to the AVNU Paymaster. Other methods and JSON-RPC batches are rejected with HTTP 400. |
Health
| Endpoint | Auth | Description |
|---|---|---|
GET / | None | Returns server name, version, and status. |
GET /health | None | Basic uptime probe for monitoring. |
Environment Variables
| Variable | Required | Description |
|---|---|---|
PRIVY_APP_ID | Yes | Application ID from the Privy Dashboard. |
PRIVY_APP_SECRET | Yes | Secret key for app-managed wallet access. Keep this secure. |
JWT_SECRET | Yes | High-entropy string (32+ characters) for JWT signing. |
PORT | No | Server port. Defaults to 3001. |
NODE_ENV | No | Runtime environment: development, production, or test. Defaults to development. |
ALLOWED_ORIGINS | No | Comma-separated domains for CORS (e.g., https://starkfi.app). |
PUBLIC_URL | No | Public-facing URL of the server (for CORS and redirects). |
AVNU_API_KEY | No | AVNU Paymaster API key. Required for developer-sponsored (gasfree) transactions. |
AVNU_PAYMASTER_URL | No | AVNU Paymaster endpoint URL. Defaults to https://starknet.paymaster.avnu.fi/v1. |
TRUST_PROXY | No | Where rate limiting reads the client IP. false (default): the TCP peer; X-Forwarded-For, X-Real-IP and CF-Connecting-IP are ignored because clients can forge them. cloudflare: CF-Connecting-IP (only if the origin is reachable solely through Cloudflare). A number N: the Nth X-Forwarded-For entry from the right, for N trusted proxy hops (1 behind a single platform load balancer). |
Local Setup
cd server
pnpm install
cp .env.example .env # Configure your environment variables
pnpm devThe server starts at http://localhost:3001 by default.
Verify It's Running
curl http://localhost:3001/health
# → {"status":"ok"}Middleware Stack
All requests pass through the following middleware in order:
| Middleware | Purpose |
|---|---|
requestId() | Assigns a unique ID for request tracing |
logger() | Logs incoming requests to stdout |
secureHeaders() | Applies security headers |
bodyLimit() | Enforces 1 MB max body size |
cors() | CORS with configurable origin allowlist |
Authentication Flow
CLI Auth Server Privy
│ │ │
├─ POST /auth/login ─────▶│── sendOTP() ──────────▶│
│ │ │
│ (user checks email) │ │
│ │ │
├─ POST /auth/verify ────▶│── verifyOTP() ────────▶│
│ │◀─ walletId + pubKey ───│
│◀─ JWT + session ────────│ │
│ │ │
├─ POST /sign/hash ──────▶│── sign(hash) ─────────▶│ (TEE)
│◀─ signature ────────────│◀─ signature ──────────│The CLI stores the JWT locally and attaches it to all subsequent requests. Private keys never leave Privy's TEE — the server only proxies signing requests.
See Also
- Authentication — User-facing auth guide (login, session management)
- Security Architecture — TEE isolation, JWT security, rate limiting
- Development & Contributing — Local server setup instructions
Last updated on