- Add truncateLightningAddress utility (shows first 2 chars + ******) - Backend: Include winner_address in past-wins API response - Frontend: Display truncated address in past winners list - Telegram: Add truncated address to draw announcements for transparency Example: username@blink.sv -> us******@blink.sv
Lightning Lottery Backend API
Bitcoin Lightning Network powered lottery system with automatic payouts to Lightning Addresses.
Features
- Public Ticket Purchasing: Anonymous ticket purchases via Lightning invoices
- Automated Draws: Scheduled draws using cryptographically secure RNG
- Instant Payouts: Automatic payouts to Lightning Addresses
- Nostr Authentication: Optional user accounts via Nostr signatures
- Multiple Cycle Types: Hourly, daily, weekly, and monthly jackpots
- Admin Interface: Manual draw triggers and payout management
Tech Stack
- Runtime: Node.js with TypeScript
- Framework: Express.js
- Database: PostgreSQL
- Lightning: LNbits integration
- Scheduler: node-cron
Setup
Prerequisites
- Node.js 18+
- PostgreSQL 14+
- LNbits instance
Installation
- Install dependencies:
npm install
- Configure environment variables:
cp .env.example .env
# Edit .env with your configuration
- Setup database:
# Create database
createdb lightning_lotto
# Run schema
psql lightning_lotto < src/database/schema.sql
Development
npm run dev
Production
npm run build
npm start
Scheduler (Optional Separate Process)
npm run scheduler
API Endpoints
Public Endpoints
GET /jackpot/next- Get next upcoming cyclePOST /jackpot/buy- Create ticket purchaseGET /tickets/:id- Get ticket status
User Endpoints (Nostr Auth)
POST /auth/nostr- Authenticate with NostrGET /me- Get user profilePATCH /me/lightning-address- Update Lightning AddressGET /me/tickets- Get user's ticketsGET /me/wins- Get user's wins
Admin Endpoints
GET /admin/cycles- List all cyclesPOST /admin/cycles/:id/run-draw- Manually run drawGET /admin/payouts- List payoutsPOST /admin/payouts/:id/retry- Retry failed payout
Webhook Endpoints
POST /webhooks/lnbits/payment- LNbits payment notification
Configuration
See .env.example for all configuration options.
Required Environment Variables
DATABASE_URL- PostgreSQL connection stringLNBITS_API_BASE_URL- LNbits API URLLNBITS_ADMIN_KEY- LNbits admin/invoice keyJWT_SECRET- Secret for JWT signingADMIN_API_KEY- Admin endpoint authentication
Database Schema
The system uses 7 main tables:
lotteries- Lottery configurationjackpot_cycles- Draw cyclesticket_purchases- Purchase recordstickets- Individual ticketspayouts- Winner payoutsusers- Nostr user accountsdraw_logs- Audit trail
Security
- JWT tokens for user authentication
- Admin API key for admin endpoints
- Webhook signature verification
- Rate limiting on all endpoints
- Idempotent payment processing
- Transaction-based ticket issuance
Scheduler Tasks
The scheduler runs three main tasks:
- Cycle Generator: Creates future cycles (every 5 minutes)
- Draw Executor: Triggers draws at scheduled times (every minute)
- Payout Retry: Retries failed payouts with exponential backoff (every 10 minutes)
License
MIT