# E-Voting System - Complete Project Overview ## ๐Ÿ“‹ Project Summary This is a **secure e-voting system** with **post-quantum cryptography** and **blockchain integration**. The system uses: - **Backend**: FastAPI (Python) with blockchain and cryptographic operations - **Frontend**: Next.js 14+ (React) with TypeScript - **Database**: MySQL - **Blockchain**: Proof-of-Authority (PoA) consensus with validator nodes - **Cryptography**: Hybrid signatures (RSA-PSS + ML-DSA Dilithium) + Hybrid encryption (ML-KEM Kyber + ElGamal) --- ## ๐Ÿ—๏ธ Architecture ### Deployment Models - **Single Node**: All services in one docker-compose - **Multi-Node**: Distributed validators with PoA consensus (docker-compose.multinode.yml) - **Development**: Hot-reload setup with docker-compose.dev.yml ### Core Components #### 1. Backend (`/backend`) - **Auth System**: JWT-based authentication - **Cryptography**: Post-quantum key generation & management - **Blockchain**: Immutable vote recording with hash chain - **Database**: Election, voter, vote, and blockchain storage - **Validators**: PoA consensus network - **Services**: - User registration & authentication - Election management - Vote submission & verification - Blockchain integrity checks #### 2. Frontend (`/frontend`) - **Dashboard**: Main control center for voters and administrators - **Pages**: - `/dashboard` - Main dashboard - `/dashboard/blockchain` - Blockchain viewer & verifier - `/dashboard/results` - Election results - `/auth/login` - Authentication - `/auth/register` - User registration - **Components**: Blockchain visualizer, forms, data tables #### 3. Blockchain Module (`/backend/blockchain.py`) - Vote storage with SHA-256 hash chain - RSA-PSS signatures on blocks - Tamper detection - Candidate hash verification #### 4. Validator Network (`/validator`) - Proof-of-Authority consensus - Block validation - State synchronization --- ## ๐Ÿ” Security Features ### Cryptography ``` Signatures: โ”œโ”€ RSA-PSS (Classical) โ””โ”€ ML-DSA-65 Dilithium (Post-Quantum) โ†’ FIPS 204 Encryption: โ”œโ”€ ML-KEM-768 Kyber (Post-Quantum) โ†’ FIPS 203 โ””โ”€ ElGamal (Classical) Hashing: โ””โ”€ SHA-256 (Quantum-Resistant) ``` ### Blockchain Security - **Immutability**: Each block linked to previous via SHA-256 hash - **Authentication**: RSA-PSS signatures on each block - **Integrity**: Hash verification chain - **Consensus**: PoA validators ensure consistency ### Data Protection - JWT tokens for session management - Password hashing - Encrypted vote storage - Database encryption ready --- ## ๐Ÿš€ Startup Process ### Normal Startup (Docker) ```bash docker-compose up -d # Frontend: http://localhost:3000 # API Docs: http://localhost:8000/docs # Database: localhost:3306 ``` ### Multi-Node (Blockchain Network) ```bash docker-compose -f docker-compose.multinode.yml up -d # Multiple validator nodes with PoA consensus # Test election automatically created ``` --- ## ๐Ÿ—‚๏ธ File Structure ``` e-voting-system/ โ”œโ”€โ”€ backend/ โ”‚ โ”œโ”€โ”€ main.py # FastAPI app entry point โ”‚ โ”œโ”€โ”€ auth.py # JWT authentication โ”‚ โ”œโ”€โ”€ blockchain.py # Blockchain implementation โ”‚ โ”œโ”€โ”€ blockchain_elections.py # Election-blockchain binding โ”‚ โ”œโ”€โ”€ blockchain_client.py # PoA validator client โ”‚ โ”œโ”€โ”€ models.py # Database models โ”‚ โ”œโ”€โ”€ schemas.py # Request/response schemas โ”‚ โ”œโ”€โ”€ services.py # Business logic โ”‚ โ”œโ”€โ”€ config.py # Configuration โ”‚ โ”œโ”€โ”€ database.py # Database setup โ”‚ โ”œโ”€โ”€ crypto/ # Cryptographic operations โ”‚ โ””โ”€โ”€ routes/ โ”‚ โ”œโ”€โ”€ auth.py # Authentication endpoints โ”‚ โ”œโ”€โ”€ elections.py # Election endpoints โ”‚ โ””โ”€โ”€ votes.py # Vote & blockchain endpoints โ”‚ โ”œโ”€โ”€ frontend/ โ”‚ โ”œโ”€โ”€ app/ โ”‚ โ”‚ โ”œโ”€โ”€ page.tsx # Home page โ”‚ โ”‚ โ”œโ”€โ”€ auth/ # Authentication pages โ”‚ โ”‚ โ”œโ”€โ”€ dashboard/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ page.tsx # Main dashboard โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ blockchain/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ page.tsx # Blockchain viewer โ”‚ โ”‚ โ””โ”€โ”€ api/ # API proxy routes โ”‚ โ”œโ”€โ”€ components/ โ”‚ โ”‚ โ”œโ”€โ”€ blockchain-visualizer.tsx โ”‚ โ”‚ โ”œโ”€โ”€ blockchain-viewer.tsx โ”‚ โ”‚ โ””โ”€โ”€ ... โ”‚ โ””โ”€โ”€ lib/ โ”‚ โ”œโ”€โ”€ api.ts # API client utilities โ”‚ โ””โ”€โ”€ api-config.ts # API configuration โ”‚ โ”œโ”€โ”€ validator/ โ”‚ โ”œโ”€โ”€ validator.py # PoA validator logic โ”‚ โ””โ”€โ”€ ... โ”‚ โ”œโ”€โ”€ docker/ โ”‚ โ”œโ”€โ”€ Dockerfile.backend โ”‚ โ”œโ”€โ”€ Dockerfile.frontend โ”‚ โ”œโ”€โ”€ Dockerfile.validator โ”‚ โ”œโ”€โ”€ nginx.conf โ”‚ โ””โ”€โ”€ init.sql โ”‚ โ”œโ”€โ”€ tests/ โ”‚ โ”œโ”€โ”€ test_blockchain.py โ”‚ โ””โ”€โ”€ test_blockchain_election.py โ”‚ โ”œโ”€โ”€ docker-compose.yml # Single node โ”œโ”€โ”€ docker-compose.dev.yml # Development โ”œโ”€โ”€ docker-compose.multinode.yml # PoA network โ”œโ”€โ”€ pyproject.toml # Python dependencies โ”œโ”€โ”€ pytest.ini # Test configuration โ””โ”€โ”€ [Documentation files] โ”œโ”€โ”€ README.md โ”œโ”€โ”€ GETTING_STARTED.md โ”œโ”€โ”€ BLOCKCHAIN_FLOW.md โ”œโ”€โ”€ PHASE_3_INTEGRATION.md โ””โ”€โ”€ ... ``` --- ## ๐Ÿ”„ Vote Flow ### 1. User Registration ``` User โ†’ Registration Form โ”œโ”€ Username/Password โ”œโ”€ Generate hybrid cryptographic keys (RSA + Dilithium + Kyber) โ””โ”€ Store encrypted keys in database ``` ### 2. User Authentication ``` User โ†’ Login Form โ”œโ”€ Verify credentials โ””โ”€ Issue JWT token ``` ### 3. Vote Submission ``` Voter โ†’ Select candidate โ”œโ”€ Encrypt vote (ML-KEM + ElGamal) โ”œโ”€ Sign with hybrid signature (RSA-PSS + Dilithium) โ”œโ”€ Submit to backend โ””โ”€ Backend: โ”œโ”€ Verify signature โ”œโ”€ Record encrypted vote โ”œโ”€ Create blockchain transaction โ”œโ”€ Broadcast to PoA validators โ””โ”€ Await consensus confirmation ``` ### 4. Blockchain Recording ``` Vote Transaction โ†’ Create Block โ”œโ”€ Hash previous block โ”œโ”€ Include encrypted vote โ”œโ”€ Include voter signature โ”œโ”€ Sign block with RSA-PSS โ”œโ”€ Chain to previous block โ””โ”€ Store in all validator nodes ``` ### 5. Results Verification ``` Admin/Observer โ†’ View Blockchain โ”œโ”€ Request blockchain for election โ”œโ”€ Verify chain integrity โ”œโ”€ Check all block signatures โ”œโ”€ Confirm vote count โ””โ”€ Decrypt and display results ``` --- ## ๐Ÿงช Testing ### Run Unit Tests ```bash pytest tests/ -v ``` ### Test Election Blockchain ```bash python3 test_blockchain_election.py ``` ### Manual Testing ```bash # Check API documentation curl http://localhost:8000/docs # Register user curl -X POST http://localhost:8000/api/auth/register \ -H "Content-Type: application/json" \ -d '{"username": "test", "password": "test123", "email": "test@example.com"}' # Login curl -X POST http://localhost:8000/api/auth/login \ -H "Content-Type: application/json" \ -d '{"username": "test", "password": "test123"}' # Get active elections curl -H "Authorization: Bearer " \ http://localhost:8000/api/elections/active # Submit vote curl -X POST http://localhost:8000/api/votes/submit \ -H "Authorization: Bearer " \ -H "Content-Type: application/json" \ -d '{"election_id": 1, "candidate_id": 1}' # Get blockchain curl -H "Authorization: Bearer " \ http://localhost:8000/api/votes/blockchain?election_id=1 # Verify blockchain curl -X POST http://localhost:8000/api/votes/verify-blockchain \ -H "Authorization: Bearer " \ -H "Content-Type: application/json" \ -d '{"election_id": 1}' ``` --- ## ๐Ÿ“Š Database Schema ### Key Tables - **users**: User accounts with hybrid keys - **elections**: Election metadata and status - **candidates**: Candidates in each election - **votes**: Encrypted vote records - **blockchain_blocks**: Immutable vote records - **validators**: PoA validator nodes - **admin_users**: Administrator accounts --- ## ๐ŸŒ API Endpoints ### Authentication - `POST /api/auth/register` - User registration - `POST /api/auth/login` - User login - `POST /api/auth/logout` - User logout ### Elections - `GET /api/elections/active` - List active elections - `GET /api/elections/{id}` - Get election details - `GET /api/elections/{id}/candidates` - Get candidates ### Voting - `POST /api/votes/submit` - Submit encrypted vote - `GET /api/votes/blockchain` - Get blockchain state - `POST /api/votes/verify-blockchain` - Verify chain integrity - `GET /api/votes/results` - Get election results ### Admin - `POST /api/admin/elections` - Create election - `PUT /api/admin/elections/{id}` - Update election - `POST /api/admin/elections/{id}/activate` - Activate election --- ## ๐Ÿ”ง Configuration ### Environment Variables ```bash # Backend DATABASE_URL=mysql://user:pass@localhost/voting_db JWT_SECRET=your-secret-key BACKEND_URL=http://localhost:8000 # Frontend NEXT_PUBLIC_API_URL=http://localhost:8000 NEXT_PUBLIC_BACKEND_URL=http://localhost:8000 # Validators POA_VALIDATOR_COUNT=3 POA_CONSENSUS_THRESHOLD=2 ``` --- ## ๐Ÿ“ˆ Recent Updates (Phase 3) - โœ… Proof-of-Authority (PoA) consensus network - โœ… Multi-validator blockchain synchronization - โœ… Blockchain integrity verification - โœ… Post-quantum cryptography (ML-DSA, ML-KEM) - โœ… Hybrid encryption & signing - โœ… Comprehensive logging system - โœ… Docker multi-node deployment - โœ… Blockchain visualizer UI component --- ## ๐Ÿ› Known Issues & Recent Fixes ### Recently Fixed (Current Session) 1. โœ… **truncateHash errors**: Added null/undefined checks 2. โœ… **Missing election_id in verify endpoint**: Fixed NextJS proxy to pass body params as query string ### In Progress - Validator failover & recovery - Performance optimization for large elections - Audit logging --- ## ๐Ÿ“š Documentation Index | Document | Purpose | |----------|---------| | `README.md` | Project overview | | `GETTING_STARTED.md` | Quick start guide | | `BLOCKCHAIN_FLOW.md` | Detailed blockchain architecture | | `PHASE_3_INTEGRATION.md` | PoA integration details | | `BLOCKCHAIN_ELECTION_INTEGRATION.md` | Election-blockchain binding | | `POA_QUICK_REFERENCE.md` | API reference | | `BLOCKCHAIN_DASHBOARD_FIX.md` | Dashboard fixes (new) | | `BLOCKCHAIN_DASHBOARD_QUICK_FIX.md` | Quick fix summary (new) | --- ## ๐ŸŽฏ Next Steps to Try 1. **Start the system**: ```bash docker-compose up -d ``` 2. **Access the frontend**: - Visit http://localhost:3000 - Register a new account - Login 3. **Create an election** (admin only): - Use admin credentials - Create election with candidates 4. **Vote**: - Select an election - Vote for a candidate - Watch blockchain record it 5. **Verify**: - Go to blockchain dashboard - Click "Vรฉrifier l'intรฉgritรฉ de la chaรฎne" - View blockchain integrity verification --- ## ๐Ÿ†˜ Troubleshooting ### Database connection issues ```bash # Check MySQL container docker ps | grep mysql # View database logs docker logs ``` ### Validator consensus issues ```bash # Check validator logs docker logs # Restart validators docker-compose restart validator ``` ### Frontend API connection ```bash # Check proxy is working curl http://localhost:3000/api/elections/active # Check backend is running curl http://localhost:8000/docs ``` --- **Last Updated**: 2025-11-10 **Version**: 3.0 with PoA Blockchain **Status**: Production-ready with post-quantum cryptography