├── .DS_Store
├── .deepsource.toml
├── .gitignore
├── README.md
├── app.py
├── app
├── __init__.py
├── __init__.pyc
├── templates
│ ├── base.html
│ └── index.html
├── views.py
└── views.pyc
├── reference-paper
└── Blockchain_Voting_System20200318-112191-yzyxvw-with-cover-page-v2.pdf
├── requirements.txt
├── screenshots
├── .DS_Store
├── 1.png
├── 2.png
├── 3.png
├── 4.png
├── 5.png
├── block.png
├── hash_block.png
├── hash_function.png
├── purposed_system.png
└── structure.png
└── service.py
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ramesh-adhikari/E-voting-system-using-blockchain-and-python/df204dede839fe6464ad8c66ed06dee5db6968b4/.DS_Store
--------------------------------------------------------------------------------
/.deepsource.toml:
--------------------------------------------------------------------------------
1 | version = 1
2 |
3 | test_patterns = [
4 |
5 | ]
6 |
7 | exclude_patterns = [
8 |
9 | ]
10 |
11 | [[analyzers]]
12 | name = 'python'
13 | enabled = true
14 | runtime_version = '3.x.x'
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | **/*.pyc
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # E-voting-system-using-blockchain-and-python
2 |
3 | ## Outline
4 | 1. Reference Research Paper
5 | 1. Abstract
6 | 2. Introduction
7 | 3. Blockchain
8 | 4. Ourposed System
9 | 5. Conclusion
10 | 2. Implementaiton of Blockchain in E-voting system
11 | 1. About application
12 | 2. Structure of Blockchain
13 | 3. Chain the blocks
14 | 4. Blockchain Proof of work
15 | 5. Add blocks to the chain
16 | 6. Mining
17 | 3. Instructions to run application
18 | 4. References
19 |
20 | ## 1. Reference Research Paper
21 | This project is based on the concept of research paper which was published in International Journal of Network Security & Its Applications (IJNSA) entitled **"A CONCEPTUAL SECURE BLOCKCHAIN- BASED ELECTRONIC VOTING SYSTEM"** by Ahmed Ben Ayed
22 |
23 | ### Review, Summary of **"A Conceptual Secure Blockchain-based Electronic Voting System"**
24 | [Paper Link](https://aircconline.com/ijnsa/V9N3/9317ijnsa01.pdf)
25 | OR
26 | [Researchgate] (https://www.researchgate.net/publication/341498272_A_CONCEPTUAL_SECURE_BLOCKCHAIN-BASED_ELECTRONIC_VOTING_SYSTEM)
27 | OR
28 | [This repository] ((https://github.com/adhikarir/E-voting-system-using-blockchain-and-python/blob/master/reference-paper/Blockchain_Voting_System20200318-112191-yzyxvw-with-cover-page-v2.pdf))
29 |
30 | #### Abstract
31 | This paper support the open source Blockchain technology to propose a design for a new electronic voting system that could be used in local or national elections.
32 |
33 | #### Introduction
34 | An e-Voting system has to have heightened security in order make sure it is available to voters but protected against outside influences changing votes from being cast, or keep a voter’s ballot from being tampered with. Many electronic voting systems rely on to hide the identity of voters. However, this technique does not provide total anonymity or integrity since many intelligence agencies around the world control different parts of the Internet which can allow them to identify or intercept votes.
35 |
36 | #### Blockchain
37 | Blockchain was first introduced by Satoshi Nakamoto (a pseudonym), who proposed a peer-to-peer payment system that allows cash transactions through the Internet without relying on trust or the need for a financial institution.
38 |
39 | Blockchain is an ordered data structure that contains blocks of transactions. Each block in the chain is linked to the previous block in the chain. The first block in the chain is referred to as the foundation of the stack. Each new block created gets layered on top of the previous block to form a stack called a Blockchain.
40 |
41 |
42 | All of the magic lies in the way this data is stored and added to the blockchain. A blockchain is essentially a linked-list containing ordered-data, with some constraints like below;
43 |
44 | * Blocks can't be modified once added; in other words, it is "append-only."
45 | * There are specific rules for appending data to it.
46 | * It's distributed in architecture.
47 | * Enforcing these constraints yields some highly desirable characteristics:
48 |
49 | * Immutability and durability of data
50 | * No single point of control or failure
51 | * A verifiable audit trail of the order in which data was added
52 |
53 | Each block in the stack is identified by a hash placed on the header. This hash is generated using the Secure Hash Algorithm (SHA-256) to generate an almost idiosyncratic fixed-size 256-bit hash.
54 | 
55 |
56 | Each header contains information that links a block to its previous block in the chain, which creates a chain linked to the very first block ever created, which is referred to as the foundation. The primary identifier of each block is the encrypted hash in its header. A digital fingerprint that was made combining two types of information: the information concerning the new block created, as well as the previous block in the chainAs soon as a block is created, it is sent over to the Blockchain. The system will keep an eye on incoming blocks and continuously update the chain when new blocks arrive.
57 | 
58 |
59 | #### Purposed System
60 | * Authentication: Only people already registered to vote can cast a vote. Our system will not
61 | support a registration process. Registration usually requires verification of certain information
62 | and documents to comply with current laws, which could not be done online in a secure manner.
63 | Therefore, the system should be able to verify voters’ identities against a previously verified
64 | database, and then let them vote only once.
65 | * Anonymity: The e-Voting system should not allow any links between voters’ identities and
66 | ballots. The voter has to remain anonymous during and after the election.
67 | * Accuracy: Votes must be accurate; every vote should be counted, and can’t be changed,
68 | duplicated or removed.
69 | * Verifiability: The system should be verifiable to make sure all votes are counted correctly.
70 | Beside the main requirement, our solution supports mobility, flexibility, and efficiency. However,
71 | we will limit this paper’s discussion to the four main requirements.
72 | 
73 |
74 | To ensure that the system is secure, the block will contain the previous voter’s information. If any of the blocks were compromised, then it would be easy to find out since all blocks are connected to each other. The Blockchain is decentralized and cannot be corrupted; no single point of failure exists. The Blockchain is where the actual voting takes place. The user’s vote gets sent to one of the nodes on the system, and the node then adds the vote to the Blockchain. The voting system will have a node in each district to ensure the system is decentralized.
75 |
76 | #### CONCLUSION
77 | This paper proposed an electronic voting system based on the Blockchain technology. The Blockchain will be publicly verifiable and distributed in a way that no one will be able to corrupt it.
78 |
79 |
80 |
81 |
82 | # Implementaiton of Blockchain in E-voting system
83 |
84 | ## About Applicaiton
85 | Let us briefly define the scope of our mini-application. The goal is to build an application that allows Voter to vote for the party they want with their Voter ID. One voter can only vote one time with their Unique Voter ID. Since the voted information will be stored on the blockchain, it'll be immutable and permanent. Users will interact with the application via a simple web interface.
86 |
87 | We'll follow a bottom-up approach to implement things. Let's begin by defining the structure of the data that we'll store in the blockchain. Three essential elements will identify a post (message posted by any user on our application):
88 |
89 | * voter_id (Voter with their voter ID)
90 | * party (Political parties, Leader)
91 | * timestamp
92 |
93 | We'll be storing data in our blockchain in a format that's widely used: JSON. Here's what a post stored in blockchain will look like:
94 |
95 | ```
96 | "transactions": [
97 | {
98 | "voter_id": "VOID001",
99 | "party": "Democratic Party",
100 | "timestamp": 1649571086.02753
101 | }
102 | ],
103 | ```
104 |
105 | The transactions are packed into blocks. A block can contain one or many transactions. The blocks containing the transactions are generated frequently and added to the blockchain. Because there can be multiple blocks, each block should have a unique id:
106 | ```
107 | class Block:
108 | def __init__(self, index, transactions, timestamp, previous_hash, nonce=0):
109 | self.index = index
110 | self.transactions = transactions
111 | self.timestamp = timestamp
112 | self.previous_hash = previous_hash
113 | self.nonce = nonce
114 | ```
115 |
116 | ### Structure of Blockchain
117 |
118 | 
119 | ### 1. Digital fingerprints to the blocks
120 | We'd like to prevent any kind of tampering in the data stored inside the block, and detection is the first step to that. To detect if the data in the block is tampered, we can use cryptographic hash functions.
121 |
122 | A hash function is a function that takes data of any size and produces data of a fixed size from it (called hash), which is generally used to identify the input. This project used sha256() hash function. We'll store the hash of the block in a field inside our Block object, and it'll act like a digital fingerprint (or signature) of data contained in it:
123 | ```
124 | def compute_hash(self):
125 | """
126 | A function that return the hash of the block contents.
127 | """
128 | block_string = json.dumps(self.__dict__, sort_keys=True)
129 | return sha256(block_string.encode()).hexdigest()
130 | ```
131 | ### 2. Chain the blocks
132 | We need a mechanism to make sure that any change in the previous blocks invalidates the entire chain. The Bitcoin way to do this is creating dependency among consecutive blocks by chaining them with the hash of block immediately previous to them. By chaining here, we mean to include the hash of the previous block in the current block in a new field called previous_hash.
133 |
134 | Okay, if every block is linked to the previous block by the previous_hash field, what about the very first block? The very first block is called the genesis block and can be generated either manually or by some unique logic. Let's add the previous_hash field to the Block class and implement the initial structure of our Blockchain class.
135 | 
136 | Now, if the content of any of the previous blocks changes,
137 |
138 | * The hash of that previous block would change.
139 | * This will lead to a mismatch with the previous_hash field in the next block.
140 | * Since the input data to compute the hash of any block also consists of the previous_hash field, the hash of the next block will also change.
141 | Ultimately, the entire chain following the replaced block is invalidated, and the only way to fix it is to recompute the entire chain.
142 |
143 | ### 3. Blockchain Proof of work
144 | Proof of Work(PoW) is the original consensus algorithm in a blockchain network. The algorithm is used to confirm the transaction and creates a new block to the chain. In this algorithm, miners (a group of people) compete against each other to complete the transaction on the network. The process of competing against each other is called mining. As soon as miners successfully created a valid block, he gets rewarded. The most famous application of Proof of Work(PoW) is Bitcoin.
145 |
146 | If we change the previous block, we can re-compute the hashes of all the following blocks quite easily and create a different valid blockchain. To prevent this, we will now exploit the asymmetry in efforts of hash functions that we discussed previously to make the task of calculating the hash difficult and random. Here's how we do this. Instead of accepting any hash for the block, we add some constraint to it. Let's add a constraint that our hash should start with "n leading zeroes" where n can be any positive integer.
147 |
148 | We know that unless we change the data of the block, the hash is not going to change, and of course, we don't want to change existing data. So, what do we do? Simple! We'll add some dummy data that we can change. Let's introduce a new field in our block called nonce. A nonce is a number that we'll keep on changing until we get a hash that satisfies our constraint. The nonce satisfying the constraint serves as proof that some computation has been performed. The number of zeroes specified in the constraint decides the "difficulty" of our Proof of Work algorithm (more the number of zeroes, harder it is to figure out the nonce).
149 |
150 | ### 4. Add blocks to the chain
151 | To add a block to the chain, we'll first have to verify that,
152 |
153 | * The data is untampered i.e., the Proof of Work provided is correct
154 | * The order of transactions is preserved i.e., the previous_hash field of the block to be added points to the hash of the latest block in our chain.
155 |
156 | ### 5. Mining
157 | Mining, in the context of blockchain technology, is the process of adding transactions to the large distributed public ledger of existing transactions, known as the blockchain. The term is best known for its association with bitcoin, though other technologies using the blockcahin employ mining.
158 |
159 | The transactions will be initially stored as a pool of unconfirmed transactions. The process of putting the unconfirmed transactions in a block and computing Proof of Work is known as the mining of blocks. Once the nonce satisfying our constraints is figured out, we can say that a block has been mined, and it can be put into the chain.
160 |
161 | ## Instructions to run the application
162 |
163 | Clone the project,
164 |
165 | ```sh
166 | $ git clone https://github.com/adhikarir/E-voting-system-using-blockchain-and-python.git
167 | ```
168 |
169 | Install the dependencies,
170 |
171 | ```sh
172 | $ cd E-voting-system-using-blockchain-and-python
173 | $ pip install -r requirements.txt
174 | ```
175 |
176 | Start a blockchain node server,
177 |
178 | ```sh
179 | # Windows users can follow this: https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery
180 | $ export FLASK_APP=service.py
181 | $ flask run --port 8000
182 | or
183 | python3 -m flask run --port 8000
184 | ```
185 |
186 | One instance of our blockchain node is now up and running at port 8000.
187 |
188 |
189 | Run the application on a different terminal session,
190 |
191 | ```sh
192 | $ python app.py
193 | ```
194 |
195 | The application should be up and running at [http://localhost:5000](http://localhost:5000).
196 |
197 | Here are a few screenshots
198 |
199 | ##### 1. Posting vote
200 |
201 | 
202 |
203 | ##### 2. Requesting the node to mine
204 |
205 | 
206 |
207 | ##### 3. Resyncing with the chain for updated data
208 |
209 | 
210 |
211 | ##### 4. Chain of the transaction
212 |
213 | 
214 |
215 | ##### 5. App screenshot
216 |
217 | 
218 |
219 | To play around by spinning off multiple custom nodes, use the `register_with/` endpoint to register a new node.
220 |
221 | Here's a sample scenario that you might wanna try,
222 |
223 | ```sh
224 | # Make sure you set the FLASK_APP environment variable to service.py before running these nodes
225 | # already running
226 | $ flask run --port 8000 or python3 -m flask run --port 8000&
227 | # spinning up new nodes
228 | $ flask run --port 8001 &
229 | $ flask run --port 8002 &
230 | ```
231 | You can use the following cURL requests to register the nodes at port 8001 and 8002 with the already running 8000.
232 | ```
233 | curl -X POST \
234 | http://127.0.0.1:8001/register_with \
235 | -H 'Content-Type: application/json' \
236 | -d '{"node_address": "http://127.0.0.1:8000"}'
237 | ```
238 | ```
239 | curl -X POST \
240 | http://127.0.0.1:8002/register_with \
241 | -H 'Content-Type: application/json' \
242 | -d '{"node_address": "http://127.0.0.1:8000"}'
243 | ```
244 |
245 | This will make the node at port 8000 aware of the nodes at port 8001 and 8002, and make the newer nodes sync the chain with the node 8000, so that they are able to actively participate in the mining process post registration.
246 |
247 | To update the node with which the frontend application syncs (default is localhost port 8000), change `CONNECTED_SERVICE_ADDRESS` field in the [views.py](/app/views.py) file.
248 |
249 | Once you do all this, you can run the application, create transactions (post vote via the web inteface), and once you mine the transactions, all the nodes in the network will update the chain. The chain of the nodes can also be inspected by inovking `/chain` endpoint using cURL.
250 |
251 | ```sh
252 | $ curl -X GET http://localhost:8001/chain
253 | $ curl -X GET http://localhost:8002/chain
254 | ```
255 |
256 | ## References
257 | 1. https://aircconline.com/ijnsa/V9N3/9317ijnsa01.pdf
258 | 2. https://www.geeksforgeeks.org/decentralized-voting-system-using-blockchain/
259 | 3. https://www.javatpoint.com/blockchain-proof-of-work
260 | 4. https://github.com/satwikkansal/python_blockchain_app/tree/ibm_blockchain_post
261 | 5. https://www.ibm.com/topics/what-is-blockchain
262 | 6. https://en.wikipedia.org/wiki/Blockchain
263 | 7. https://github.com/Abhiramborige/Online-Voting-Using-Blockchain
264 |
265 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 | from app import app
2 |
3 | app.secret_key = '6dbf23122cb5046cc5c0c1b245c75f8e43c59ca8ffeac292715e5078e631d0c9'
4 | app.config['SESSION_TYPE'] = 'filesystem'
5 |
6 | app.run(debug=True, port=5000)
7 |
--------------------------------------------------------------------------------
/app/__init__.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 |
3 | app = Flask(__name__)
4 |
5 | from app import views
--------------------------------------------------------------------------------
/app/__init__.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ramesh-adhikari/E-voting-system-using-blockchain-and-python/df204dede839fe6464ad8c66ed06dee5db6968b4/app/__init__.pyc
--------------------------------------------------------------------------------
/app/templates/base.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | {{ title }}
5 |
6 |
7 |
8 |