Confidential Voting Developer Tutorial
Learn how to use Secret Network smart contracts to encrypt and decrypt votes on Polygon testnet.
Intro
In this tutorial you will learn how to encrypt and decrypt votes on the EVM with Secret Network smart contracts so that you can build confidential voting on any EVM chain of your choosing. You will be working with two smart contracts:
EVM smart contract deployed on Polygon testnet (voting contract)
Secret Network smart contract deployed on Secret testnet (decryption contract)
The EVM contract stores encrypted proposals and votes, and the Secret contract decrypts the stored votes and reveals them in a query.
See a live demo here, configured for Polygon testnet! (To use the demo, make sure Polygon testnet is added to your Metamask wallet)
You will start by configuring your developer environment and then learn how to generate cryptographic keys in a Secret Network smart contract which you will use to encrypt votes on the EVM.
Getting Started
To get started, clone the Secret Labs examples repo:
EVM Prerequisites
Secret Network Prerequisites
Configuring Environment Variables
cd
into examples/evm-confidential-voting/polygon:
Install the node dependencies:
Update the env
file with your Secret Network wallet mnemonic, EVM wallet private key, and Infura API key:
Make sure your Infura API key is configured for Polygon Matic testnet 😎
Next, generate encryption keys for your EVM contract and automatically add them to your env
file by running create_keys.js
:
Now you are ready to upload the smart contracts! 🚀
Upload and Instantiate Secret contract
cd
into examples/evm-confidential-voting/secret-network:
Compile the Secret Network smart contract:
If you are on a Mac and run into compilation error:
error occurred: Command “clang”
Make sure you have the latest version of Xcode installed and then update your clang path by running the following in your terminal:
cargo clean
AR=/opt/homebrew/opt/llvm/bin/llvm-ar CC=/opt/homebrew/opt/llvm/bin/clang cargo build --release --target wasm32-unknown-unknown
See here for instructions on updating your clang path.
cd
into examples/evm-confidential-voting/secret-network/node
Install the node dependencies:
Upload the Secret Network smart contract:
Upon successful upload a codeHash
and contract address
is returned:
Update the env
file with your codeHash
and contract address
:
Execute Secret Network Smart Contract
Now that your Secret Network smart contract is instantiated, you can execute the contract to generate encryption keys as well as decrypt encrypted messages. To learn more about the encryption schema, see the EVM encryption docs here.
Create Keys
To create encryption keys, run node create_keys
:
After you generate your keys successfully, query your public key:
Which returns your public key as a string
:
Add the public_key to your env
file:
Now it's time to upload a Voting contract to the EVM, which you will use to store encrypted votes that can only be decrypted by your Secret Network smart contract.
Upload and Instantiate Polygon Smart Contract
cd
into examples/evm-confidential-voting/polygon:
Compile your Solidity smart contract:
Once the contract is compiled successfully, upload the contract to Polygon testnet:
Note the contract address:
Add the Polygon testnet contract address to your env
file:
Execute Polygon Testnet Smart Contract
Now that your Polygon smart contract is instantiated, you can execute the contract to create voting proposals as well as vote on existing proposals. You can review the solidity contract here.
Create Voting Proposal
To create a proposal, you must include a proposal description (a "yes" or "no" question) as well as a quorum number, which is the number of unique wallet addresses required to vote on the proposal before it closes.
For testing purposes, set quorum
to 1 unless you want to test with multiple wallet addresses
Open create_proposal.js
and update the proposal_description to a "yes" or "no" question of your choice:
Then run create_proposal.js
:
A transaction hash
will be returned upon successful execution:
You can query the proposal by running query_by_proposal_id
:
Be sure to update the proposalId
in query_by_proposal_id.js with the proposalId
you want to query!
Which returns your proposal:
Each time you create a proposal, the proposalId
is incremented by 1. Your first proposalId
is 1, your next proposalId
will be 2, and so on.
Vote on Proposal
Now it's time to vote on the proposal you created. Open vote.js
and update your proposal answer to either "yes" or "no" in the msg object:
proposal.id
and proposal.description
will match the proposal info you input for getProposalById.
This means that each time you vote, you need to make sure you update the proposal_id
number that you pass to getProposalById()
so that it matches the proposal you want to vote on!
Once you have updated your vote
and proposalId
, execute the vote script:
Your encrypted data and transaction hash are returned upon successful execution:
Decrypt Votes
Now it's time to decrypt your vote! First, query that the vote was successfully added to the proposal by running query_proposal_votes.js
:
Be sure to update the proposalId
with the proposal you want to query.
query_proposal_vote
returns your encrypted vote for the supplied proposalId
:
Run decrypt.js
to decrypt the vote:
In decrypt.js
, update the proposalId
with the proposal you want to query.
Which returns your decrypted vote:
Conclusion
Congrats! You have now deployed smart contracts on Polygon and Secret Network and implemented private cross-chain voting. If you have any questions or run into any issues, post them on the Secret Developer Discord and somebody will assist you shortly.
Last updated