๐Ÿ›ก๏ธ VoteGuard Documentation

Complete guide to the blockchain-based secure voting system โ€” from setup to API reference.

๐Ÿ“– Overview

VoteGuard is a production-ready electronic voting system that combines traditional database authentication with Ethereum blockchain technology for transparent, tamper-proof vote recording.

Every vote is encrypted, digitally signed, and recorded as an immutable transaction on the Sepolia Ethereum testnet, ensuring no vote can ever be altered or deleted.

โ›“๏ธ

Blockchain Voting

All votes recorded immutably on Ethereum Sepolia testnet

๐Ÿ”

Two-Factor Auth

Password + Email OTP verification for every login

๐Ÿ›๏ธ

Govt Registry

Citizen ID verified against PostgreSQL government database

๐Ÿ“œ

Smart Contracts

Solidity contracts manage elections, candidates & votes

๐Ÿ”‘

RSA Encryption

End-to-end encryption via RSA-2048 key exchange

๐Ÿงพ

Vote Receipts

Cryptographic receipt hashes for vote verification

๐Ÿ—๏ธ Architecture

Layer Technology Purpose
Frontend Next.js 14, React, Tailwind CSS Voter portal, admin panel, ballot UI
Backend API Node.js, Express.js REST API, auth, vote processing
Auth Database PostgreSQL (Supabase) + Prisma User identity & government registry
Blockchain Ethereum Sepolia, Solidity 0.8.20 Elections, candidates, votes (immutable)
RPC Provider Alchemy (free tier) Blockchain read/write access
Security JWT, bcrypt, RSA-2048, OTP Authentication & encryption
๐Ÿ’ก
Hybrid Architecture: PostgreSQL stores only identity & auth data. All voting data (elections, candidates, votes, audit logs) lives on the Ethereum blockchain for tamper-proof transparency.

๐Ÿ“‹ Prerequisites

๐Ÿ“ฆ Installation

1. Clone the Repository

bash
git clone https://github.com/mouniksai/vote-guard.git
cd vote-guard

2. Install Backend Dependencies

bash
cd vote-guard-server
npm install

3. Install Frontend Dependencies

bash
cd ..   # back to root
npm install

๐Ÿ”ง Environment Configuration

Backend โ€” vote-guard-server/.env

env
# Server
PORT=5001

# Database (Supabase / PostgreSQL)
DATABASE_URL="postgresql://user:password@host:5432/vote_guard?schema=public"

# Blockchain
BLOCKCHAIN_NETWORK=sepolia
CONTRACT_ADDRESS=0xE08b2c325F4e64DDb7837b6a4b1443935473ECB2
ALCHEMY_API_KEY=your_alchemy_key
SEPOLIA_PRIVATE_KEY=your_wallet_private_key

# Security
JWT_SECRET=your_super_secret_key

# Email (for OTP)
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password

Frontend โ€” .env.local

env
NEXT_PUBLIC_API_URL=http://localhost:5001
NEXT_PUBLIC_CONTRACT_ADDRESS=0xE08b2c325F4e64DDb7837b6a4b1443935473ECB2
NEXT_PUBLIC_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/your_key
NEXT_PUBLIC_ALCHEMY_API_KEY=your_alchemy_key
NEXT_PUBLIC_CHAIN_ID=11155111
โš ๏ธ
Never commit .env files to Git. Use dedicated test wallets and never purchase test ETH.

๐Ÿ—„๏ธ Database Setup

VoteGuard uses Prisma ORM with PostgreSQL. Only user identity & authentication data is stored in the database.

1. Generate Prisma Client

bash
npx prisma generate

2. Run Migrations

bash
# Development (prototyping)
npx prisma db push

# Production (versioned)
npx prisma migrate dev --name init

Schema Overview

Model Table Purpose
GovtRegistry govt_registry Government citizen records (name, DOB, constituency)
User users Registered voter accounts (username, password hash, OTP, role)

โ›“๏ธ Blockchain Setup

The smart contract is already deployed on Sepolia testnet. All team members connect to the same contract.

Property Value
Contract Address 0xE08b2c325F4e64DDb7837b6a4b1443935473ECB2
Network Sepolia Testnet (Chain ID: 11155111)
Solidity Version 0.8.20
Etherscan View on Etherscan โ†’

Compile & Test Contracts (optional)

bash
cd vote-guard-server
npx hardhat compile
npx hardhat test

๐Ÿš€ Running the Project

1. Start the Backend

bash
cd vote-guard-server
npm run dev
โœ…
You should see: โœ… VOTEGUARD SERVER RUNNING! with blockchain connection confirmed on port 5001.

2. Start the Frontend

bash
cd ..   # root directory
npm run dev

Open http://localhost:3000 in your browser.

3. Connect MetaMask

  1. Click "Connect Wallet" on any voting page
  2. Switch MetaMask to Sepolia Test Network
  3. Approve the network switch
  4. You're ready to vote!

๐Ÿ”’ Security Features

๐Ÿ”

JWT + HttpOnly Cookies

Stateless auth with secure session cookies resistant to XSS

๐Ÿ”‘

RSA-2048 Key Exchange

Asymmetric encryption for secure client-server communication

๐Ÿ“ฑ

Two-Factor Auth (2FA)

Email OTP verification required for every login

๐Ÿงฌ

bcrypt Password Hashing

Salted hashing with cost factor 10 for password storage

โ›“๏ธ

Blockchain Immutability

Votes cannot be altered once recorded on Ethereum

๐Ÿงพ

Cryptographic Receipts

SHA-256 receipt hashes for independent vote verification

๐Ÿณ Deployment

Docker (Backend)

dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npx prisma generate
EXPOSE 5001
CMD ["node", "server.js"]
bash
docker build -t voteguard-server .
docker run -p 5001:5001 --env-file .env voteguard-server

Docker (Frontend)

bash
docker build -t voteguard-frontend .
docker run -p 3000:3000 voteguard-frontend

๐Ÿ†˜ Troubleshooting

โ“ Backend won't start?

Verify SEPOLIA_PRIVATE_KEY and ALCHEMY_API_KEY are set correctly in .env. Ensure CONTRACT_ADDRESS matches the deployed contract.

โ“ Wrong network error in MetaMask?

Open MetaMask โ†’ Settings โ†’ Advanced โ†’ Enable "Show test networks" โ†’ Switch to Sepolia.

โ“ Slow transactions?

Sepolia transactions take 15-30 seconds. Check transaction status on Etherscan.

โ“ Team members see different data?

Ensure everyone uses the same CONTRACT_ADDRESS and BLOCKCHAIN_NETWORK=sepolia in their .env files.

โ“ OTP email not received?

Check EMAIL_USER and EMAIL_PASS in .env. For Gmail, use an App Password (not your regular password).

โ“ Prisma errors?

Run npx prisma generate followed by npx prisma db push to sync the schema.