Create a fair evoting application to ensure correct election results with Hyperledger Fabric and IBM Blockchain Platform (Local)
- Clone the Repo
- Start the Fabric Runtime
- Install Contract
- Instantiate Contract
- Export Connection Details
- Run the App
Note: This repo assumes you have VSCode and IBM Blockchain VSCode extension installed. If you don't, first install the latest version of VSCode, and then install the IBM Blockchain VSCode extension ensuring you have the correct system requirements to run the extension. You will need Docker as this is how the extension builds a development Hyperledger Fabric network with a click of a button.
Git clone this repo onto your computer in the destination of your choice, then go into the web-app folder:
HoreaPorutiu$ git clone https://github.com/IBM/evote
-
If you get errors like the gRPC error, you may need to download an earlier version of VSCode (1.39) (here)[https://code.visualstudio.com/updates/v1_39]. Note that if you are using Mac, make sure the VSCode in your ~/Applications folder shows version 1.39 when you click on show details. You may need to move newer version into the trash, and then empty the trash for the older version to work.
-
First, we need to go to our IBM Blockchain Extension. Click on the IBM Blockchain icon in the left side of VSCode (It looks like a square).
-
Next, start your local fabric by clicking on 1 Org Local Fabric in the FABRIC ENVIRONMENTS pane.
-
Once the runtime is finished starting (this might take a couple of minutes), under Local Fabric you should see Smart Contracts and a section for both installed and instantiated.
🚨🚨🚨Note that the gif shows the process for importing a contract, but uses a different smart contract. The process is the same, so use the gifs for help, but know that we are using the voterContract.🚨🚨🚨
-
Next, we have to import our contract before we can install it. Click on View -> Open Command Pallette -> Import Smart Contract. Next, click on the
[email protected]
file that is in theevote/contract
folder. -
Under FABRIC ENVIRONMENTS let's click on + Install and choose the peer that is available. Then the extension will ask you which package to install. Choose [email protected].
-
Lastly, we need to instantiate the contract to be able to submit transactions on our network. Click on + Instantiate and then choose [email protected].
-
When prompted for a private data collection, or and endorsement policy, hit
enter
on your keyboard, which will take all of the defaults. -
This will instantiate the smart contract. This may take some time. You should see the contract under the instantiated tab on the left-hand side, once it is finished instantiating.
- Connect to the "Local Fabric - Org1" gateway as
admin
. Right click on the 3 dot menu on the FABRIC GATEWAYS pane andExport Connection Profile
Save this file to <git_tree>/evote/web-app/server/fabric_connection.json. Note that this gif is here to show you the VSCode interface, it saves the files somewhere else.
- 🚨Under the
FABRIC WALLETS
pane, click on1 Org Local Fabric - Org1 Wallet
. Note this is very important, if you click on the Orderer wallet at the top, the application will not work! 🚨 - Export the and save the wallet as
wallet
to <git_tree>/evote/web-app/server/wallet. - Your final directory structure should look like the image below:
Next, update the config.json
file so it looks like this:
{
"connection_file": "fabric_connection.json",
"appAdmin": "admin",
"appAdminSecret": "adminpw",
"orgMSPID": "Org1MSP",
"caName": "ca.org1.example.com",
"userName": "V1",
"gatewayDiscovery": { "enabled": true, "asLocalhost": true }
}
This will ensure we use the admin identity that is stored in our wallet to sign transactions,
and let the network know that the transactions that are coming from this certain identity were
first signed off by the certificate authority with the name ca.org1.example.com
.
To run the app, we will need to install dependencies for both our front-end and our back-end.
- First, navigate to the
server
directory, and install the node dependencies.cd evote/web-app/server npm install
- Then, start the server:
npm start
- If all goes well, you should see the following in your terminal:
> [email protected] start /Users/[email protected]/Workdir/testDir/July7/evote/web-app/server > ./node_modules/nodemon/bin/nodemon.js src/app.js [nodemon] 1.19.1 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node src/app.js`
- First, navigate to the
client
directory, and install the node dependencies.cd evote/web-app/client npm install
- Then, start the client:
npm run serve
- If all goes well, you should see the following in your terminal:
DONE Compiled successfully in 6803ms 11:48:20 App running at: - Local: http://localhost:8080/ - Network: unavailable Note that the development build is not optimized. To create a production build, run npm run build.
Nice. We're pretty much ready to submit transactions on our contract. Go to http://localhost:8080/ to see your app.
Go ahead and register a voter, login with your VoterId, and submit a vote. Have fun! :)