Aim :Develop a voting application using Hyperledger and Ethereum. Project Structure hyperledger-voting/ │ ├─ contracts/ │ └─ votingContract.js ├─ app.js ├─ package.json └─ public/ └─ index.html votingContract.js 'use strict'; const { Contract } = require('fabric-contract-api'); class VotingContract extends Contract { async initLedger(ctx) { console.log('Ledger initialized'); const elections = []; await ctx.stub.putState('ELECTIONS', Buffer.from(JSON.stringify(elections))); } async createElection(ctx, electionId, description, candidatesJSON) { const elections = await this.getElections(ctx); const exists = elections.find(e => e.electionId === electionId); if (exists) throw new Error(`Election ${electionId} already exists`); const candidates = JSON.parse(candidatesJSON).map(c => ({ name: c, votes: 0 })); const election = { electionId, description, candidates }; elections.push(election); await ctx.stub.putState('ELECTIONS', Buffer.from(JSON.stringify(elections))); return JSON.stringify(election); } async vote(ctx, electionId, candidateName) { const elections = await this.getElections(ctx); const election = elections.find(e => e.electionId === electionId); if (!election) throw new Error(`Election ${electionId} not found`); const candidate = election.candidates.find(c => c.name === candidateName); if (!candidate) throw new Error(`Candidate ${candidateName} not found`); candidate.votes += 1; await ctx.stub.putState('ELECTIONS', Buffer.from(JSON.stringify(elections))); return JSON.stringify(election); } async getElections(ctx) { const data = await ctx.stub.getState('ELECTIONS'); if (!data || data.length === 0) return []; return JSON.parse(data.toString()); } } module.exports = VotingContract; App.js const express = require('express'); const app = express(); const port = 3000; // Simulated Hyperledger ledger let elections = []; // Middleware app.use(express.json()); app.use(express.static('public')); // Create election app.post('/createElection', (req, res) => { const { electionId, description, candidates } = req.body; if (!electionId || !description || !candidates) { return res.status(400).send('Missing fields'); } const exists = elections.find(e => e.electionId === electionId); if (exists) { return res.status(400).send('Election already exists'); } const election = { electionId, description, candidates: candidates.map(c => ({ name: c, votes: 0 })) }; elections.push(election); res.json(election); }); // Vote app.post('/vote', (req, res) => { const { electionId, candidateName } = req.body; const election = elections.find(e => e.electionId === electionId); if (!election) { return res.status(404).send('Election not found'); } const candidate = election.candidates.find(c => c.name === candidateName); if (!candidate) { return res.status(404).send('Candidate not found'); } candidate.votes += 1; res.json(election); }); // Get all elections app.get('/getElections', (req, res) => { res.json(elections); }); // Start server app.listen(port, () => { console.log(`Hyperledger Voting app running at http://localhost:${port}`); }); Index.html