Learn how to use Secret Network smart contracts to encrypt and decrypt votes on Polygon testnet.
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.
To get started, clone the Secret Labs examples repo:
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! 🚀
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
:
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.
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.
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:
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.
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.
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:
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:
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.
Learn how to use Secret Network smart contracts to encrypt and decrypt votes on Polygon testnet.
Learn how to use SecretPath to vote confidentially on the EVM
SecretPath enables EVM developers to use Secret Network as a Confidential Computation Layer (CCL) for all EVM-compatible chains.
In this developer tutorial, you will learn how to use SecretPath to enable confidential voting on the EVM.
See a fullstack cross-chain voting demo here.
At a high level, you can think of SecretPath as a confidential bridge that passes encrypted data from your EVM chain to a Secret Network smart contract where the data remains encrypted.
To work with SecretPath, you must first create a Secret smart contract that stores the encrypted data that you want to send from the EVM. For our purposes, we have created a Secret smart contract with 2 functionalities:
Create proposals
Vote on existing proposals
You create and vote on proposals from the EVM, and then that data is sent to your Secret smart contract via SecretPath where it remains encrypted . Pretty cool, right!? 😎 Let's start by examining our Secret voting contract, and then we will breakdown how to send messages to it from the EVM with SecretPath.
To get started, clone the examples repo:
cd into secretpath-tutorials/secretpath-voting/voting-contract:
Open contract.rs and examine the match
statement at line 67:
This handle msg
is where you define the functionality of your SecretPath contract. For our purposes, we have written the functions create_proposal
and create_vote
. You can examine those functions in more detail if you'd like and make adjustments as you see fit 🤓.
Update the env
file with your Secret Network wallet mnemonic, and rename it ".env" instead of ".env.example"
Compile the contract
cd
into voting-contract/node:
Install the node dependencies
Set SecretPath parameters:
Open upload.js and configure the SecretPath gatewayAddress
, gatewayHash
, and gatewayPublicKey:
gatewayAddress, gatewayHash
, and gatewayPublicKey
are needed for instantiating contracts that utilize SecretPath and can be found in the docs here. You will always use these same 3 parameters for instantiating a SecretPath-compatible contract on testnet.
Upload and instantiate the contract:
Upon successful upload and instantiation, add the contract codeHash
and address
to your env
.
Now that you have instantiated your confidential voting contract on Secret Network, it's time to pass your encrypted data from the EVM to Secret Network. Remember the create_proposal
and create_vote
functions from the Secret contract? Now you will execute those functions and send encrypted data to the voting contract! 🤯
Let's create and vote on your first proposal with SecretPath!
cd
into secretpath-voting/frontend
:
Install the dependencies
Configure env
Configure the env
with your confidential voting contractAddress
and codeHash.
Run the application
You should see the following React application running locally in the browser:
Now, create and vote on a proposal to understand the frontend functionality. Then, let's look at the underlying code to understand how we are passing encrypted data from the EVM to Secret Network 🙂
Passing Encrypted Data with SecretPath
As stated above, we have two functions we are executing with SecretPath: create_proposal
and create_vote
. In our React application, there are two corresponding components which execute these functions: CreateProposal and VoteonProposal.
Create a Voting Proposal
Open CreateProposal.js and navigate to the handleSubmit function, which contains all of our SecretPath logic.
The majority of the handleSubmit
function is boilerplate code used for SecretPath verification, signing, and converting contract inputs into correctly formatted packets and vice versa.
For our purposes, we only need to examine 2 lines of code, data
on line 88 and handle
on line 218.
data
is the encrypted data that we are passing from the EVM to the Secret Network voting contract. It takes a user input of name
, description,
and end_time
. This corresponds with the ProposalStoreMsg
in the Secret contract.
handle
is the function that is actually being called in the Secret contract that you deployed. You are passing the create_proposal
handle, which executes the create_proposal
function in your Secret voting contract.
Now that you have all of your SecretPath code configured, execute the frontend to send your voting proposal to the Secret contract!
Upon successful execution, your SecretPath transaction hash will be logged in the console.
Vote on a Proposal
Open VoteonProposal.js and navigate to the handleSubmit
function, which, again, contains all of our SecretPath logic.
data
is the encrypted data that we are passing from the EVM to the Secret Network voting contract. It takes a user input of vote
, ("yes" or "no"), wallet_address
(the wallet address of the voter), and index.
This corresponds with the VoteStoreMsg
in the Secret contract.
The voting contract is designed so that each proposal has an ascending index starting with 1. The first proposal you create is index 1, the second is index 2, etc. So when you vote, the React application passes the corresponding index of the proposal that is to be voted on 🙂
handle:
You are passing the create_vote
handle, which executes the create_vote
function in your Secret voting contract.
Execute the frontend to vote on an existing proposal and send the encrypted vote to the Secret contract!
Upon successful execution, your SecretPath transaction hash will be logged in the console.
Secret Queries - retrieving proposals and votes from Secret contract storage
Perhaps you are wondering how the React frontend queries the Secret voting contract to display the data that we pass from the EVM. This is possible with secret.js, the javascript SDK for Secret Network.
We have 2 query functions defined in our Secret voting contract, RetrieveProposals
and RetrieveVotes.
Once you have created proposals with votes, you can use execute these query functions with secret.js to:
These queried proposals and their associated votes are then displayed in our React frontend.
Congrats! You deployed your very own confidential voting contract on Secret Network and used SecretPath to send cross-chain encrypted votes on an EVM chain. See the fullstack demo here. You now have all of the tools you need to start building your own cross-chain SecretPath contracts on the EVM 🎉
Note: the end user of the application is not exposed to Secret Network and is only working directly in the EVM environment. However, the data is fully protected and cannot be viewed by anyone because it is stored in encrypted Secret contracts 😮💨
If you have any questions or run into any issues, post them on the Secret Developer Discord and somebody will assist you shortly.