File size: 4,057 Bytes
7c9931b
 
 
 
 
 
 
 
 
 
 
25226bc
 
 
 
 
 
 
 
 
 
 
 
 
 
7c9931b
 
 
 
11676d1
7c9931b
 
11676d1
7c9931b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8676362
11676d1
 
 
 
8676362
 
11676d1
 
25226bc
 
 
 
11676d1
8676362
11676d1
 
7c9931b
 
11676d1
7c9931b
11676d1
7c9931b
25226bc
 
 
 
 
 
7c9931b
25226bc
 
 
 
11676d1
25226bc
7c9931b
 
8676362
7c9931b
 
11676d1
 
 
 
25226bc
11676d1
 
 
 
 
 
 
25226bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import streamlit as st
import hashlib
import json
import time

# Blockchain Class
class Blockchain:
    def __init__(self):
        self.chain = []
        self.voters = set()
        self.load_data()
        if not self.chain:
            self.create_genesis_block()

    def create_genesis_block(self):
        """Creates the first block in the blockchain."""
        genesis_block = {
            "index": 1,
            "timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
            "votes": {},
            "previous_hash": "0",
            "hash": self.hash_block({}, "0"),
        }
        self.chain.append(genesis_block)
        self.save_data()

    def create_block(self, votes, previous_hash):
        block = {
            "index": len(self.chain) + 1,
            "timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
            "votes": votes,
            "previous_hash": previous_hash,
            "hash": self.hash_block(votes, previous_hash),
        }
        self.chain.append(block)
        self.save_data()
        return block

    def hash_block(self, votes, previous_hash):
        block_string = json.dumps(votes, sort_keys=True) + previous_hash
        return hashlib.sha256(block_string.encode()).hexdigest()

    def get_latest_block(self):
        return self.chain[-1] if self.chain else None

    def save_data(self):
        with open("votes.json", "w") as f:
            json.dump({"chain": self.chain, "voters": list(self.voters)}, f, indent=4)

    def load_data(self):
        try:
            with open("votes.json", "r") as f:
                data = json.load(f)
                self.chain = data.get("chain", [])
                self.voters = set(data.get("voters", []))
        except FileNotFoundError:
            self.chain = []
            self.voters = set()

# Initialize Blockchain
blockchain = Blockchain()

# Set page config
st.set_page_config(page_title="Blockchain Voting", page_icon="πŸ—³", layout="wide")

st.markdown("<h1>πŸ—³ Secure Blockchain-Based Voting System</h1>", unsafe_allow_html=True)

# Sidebar for Voter Input
st.sidebar.header("πŸ” Voter Authentication")
voter_id = st.sidebar.text_input("πŸ”‘ Enter your Unique Voter ID:", max_chars=10)

# Hash the voter ID for security
def hash_voter_id(voter_id):
    return hashlib.sha256(voter_id.encode()).hexdigest()

# Candidate Selection
st.sidebar.header("πŸ—³ Vote Now")
candidates = ["BJP", "Shiv Sena", "NCP", "NOTA"]
selected_candidate = st.sidebar.radio("Select your candidate:", candidates)

# Voting Button
if st.sidebar.button("βœ… Submit Vote"):
    if not voter_id:
        st.sidebar.warning("⚠ Please enter a valid Voter ID!")
    else:
        voter_hash = hash_voter_id(voter_id)  # Hash voter ID
        if voter_hash in blockchain.voters:
            st.sidebar.error("❌ You have already voted!")
        else:
            last_block = blockchain.get_latest_block()
            new_votes = last_block["votes"].copy() if last_block else {}

            new_votes[selected_candidate] = new_votes.get(selected_candidate, 0) + 1
            blockchain.create_block(new_votes, last_block["hash"] if last_block else "0")
            blockchain.voters.add(voter_hash)  # Store hashed ID
            blockchain.save_data()

            st.sidebar.success(f"βœ… Vote cast for {selected_candidate}!")

# Display Vote Count
st.markdown("<h2 style='text-align: center; color:#ffcc00;'>πŸ“Š Live Vote Count</h2>", unsafe_allow_html=True)
latest_block = blockchain.get_latest_block()

if latest_block:
    with st.container():
        col1, col2, col3, col4 = st.columns(4)
        vote_data = latest_block["votes"]

        col1.metric(label="πŸ”Ή BJP", value=vote_data.get("BJP", 0))
        col2.metric(label="πŸ”Ή Shiv Sena", value=vote_data.get("Shiv Sena", 0))
        col3.metric(label="πŸ”Ή NCP", value=vote_data.get("NCP", 0))
        col4.metric(label="πŸ”Ή NOTA", value=vote_data.get("NOTA", 0))

# Show Blockchain Data
if st.checkbox("πŸ“œ View Blockchain Data"):
    st.json(blockchain.chain)