Alexis Bruneteau 1a42b4d83b feat: Implement blockchain-based election storage with cryptographic security
Elections are now immutably recorded to blockchain with:
- SHA-256 hash chain for integrity (prevents tampering)
- RSA-PSS signatures for authentication
- Candidate verification via SHA-256 hash
- Tamper detection on every verification
- Complete audit trail

Changes:
- backend/blockchain_elections.py: Core blockchain implementation (ElectionBlock, ElectionsBlockchain)
- backend/init_blockchain.py: Startup initialization to sync existing elections
- backend/services.py: ElectionService.create_election() with automatic blockchain recording
- backend/main.py: Added blockchain initialization on startup
- backend/routes/elections.py: Already had /api/elections/blockchain and /{id}/blockchain-verify endpoints
- test_blockchain_election.py: Comprehensive test suite for blockchain integration
- BLOCKCHAIN_ELECTION_INTEGRATION.md: Full technical documentation
- BLOCKCHAIN_QUICK_START.md: Quick reference guide
- BLOCKCHAIN_IMPLEMENTATION_SUMMARY.md: Implementation summary

API Endpoints:
- GET /api/elections/blockchain - Returns complete blockchain
- GET /api/elections/{id}/blockchain-verify - Verifies election integrity

Test:
  python3 test_blockchain_election.py

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-07 03:01:11 +01:00

57 lines
1.2 KiB
Python

"""
Application FastAPI principale.
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .config import settings
from .database import init_db, get_db
from .routes import router
from .init_blockchain import initialize_elections_blockchain
# Initialiser la base de données
init_db()
# Initialiser la blockchain avec les élections existantes
try:
db = next(get_db())
initialize_elections_blockchain(db)
db.close()
except Exception as e:
print(f"Warning: Failed to initialize blockchain on startup: {e}")
# Créer l'application FastAPI
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
debug=settings.debug
)
# Configuration CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # À restreindre en production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Inclure les routes
app.include_router(router)
@app.get("/health")
async def health_check():
"""Vérifier l'état de l'application"""
return {"status": "ok", "version": settings.app_version}
@app.get("/")
async def root():
"""Endpoint root"""
return {
"name": settings.app_name,
"version": settings.app_version,
"docs": "/docs"
}