Confidential Voting Developer Tutorial with SecretPath
Learn how to use SecretPath to vote confidentially on the EVM
Last updated
Was this helpful?
Learn how to use SecretPath to vote confidentially on the EVM
Last updated
Was this helpful?
enables EVM developers to use Secret Network as a Confidential Computation Layer (CCL) for .
In this developer tutorial, you will learn how to use SecretPath to enable confidential voting on the EVM.
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 :
cd into secretpath-tutorials/secretpath-voting/voting-contract:
Compile the contract
cd
into voting-contract/node:
Install the node dependencies
Set SecretPath parameters:
Upload and instantiate the contract:
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
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
Create a Voting Proposal
Now that you have all of your SecretPath code configured, execute the frontend to send your voting proposal to the Secret contract!
Vote on a Proposal
Execute the frontend to vote on an existing proposal and send the encrypted vote to the Secret contract!
Secret Queries - retrieving proposals and votes from Secret contract storage
These queried proposals and their associated votes are then displayed in our React frontend.
Open and examine the match
statement at :
This handle msg
is where you define the functionality of your SecretPath contract. For our purposes, we have written the functions and . You can examine those functions in more detail if you'd like and make adjustments as you see fit ๐ค.
Update the file with your Secret Network wallet mnemonic, and rename it ".env" instead of ".env.example"
Open 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 . You will always use these same 3 parameters for instantiating a SecretPath-compatible contract on testnet.
Upon successful upload and instantiation, add the contract codeHash
and address
to your.
Configure the with your confidential voting contractAddress
and codeHash.
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: and .
Open CreateProposal.js and navigate to the function, which contains all of our SecretPath logic.
For our purposes, we only need to examine 2 lines of code, data
and handle
on
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 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 function in your Secret voting contract.
Upon successful execution, your SecretPath will be logged in the console.
Open and navigate to the function, which, again, contains all of our SecretPath logic.
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 in the Secret contract.
handle:
You are passing the create_vote
handle, which executes the function in your Secret voting contract.
Upon successful execution, your SecretPath will be logged in the console.
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 , the javascript SDK for Secret Network.
We have 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:
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 . You now have all of the tools you need to start building your own cross-chain SecretPath contracts on the EVM ๐
If you have any questions or run into any issues, post them on the and somebody will assist you shortly.
Let's dive a little deeper into the boilerplate SecretPath code to understand how our data is encrypted, signed, and formatted by SecretPath. The following comments are for the function of our CreateProposal component: