A comprehensive end-to-end demonstration of the Vocdoni DAVINCI SDK showcasing the complete privacy-preserving voting process using zero-knowledge proofs.
- Overview
- Prerequisites
- Installation
- Configuration
- Usage
- What the Script Does
- Process Flow
- Understanding the Output
- Architecture
- Troubleshooting
- Additional Resources
This script demonstrates a complete voting workflow using the Vocdoni DAVINCI SDK, including:
- Privacy-Preserving Voting: Uses zk-SNARKs to ensure vote privacy while maintaining verifiability
- Multi-Question Elections: Creates an election with two questions (favorite color and transportation preference)
- Smart Contract Integration: Interacts with Ethereum smart contracts
- Complete Lifecycle: From census creation to result tallying
- Cryptographic Operations: Handles encryption, proof generation, and verification
- zk-SNARKs: Zero-knowledge proofs for private voting
- Ethereum: Smart contract deployment and interaction
- Vocdoni Sequencer: Off-chain vote processing and aggregation
- TypeScript: Type-safe development environment
Before running this script, ensure you have:
- Node.js (v18 or higher)
- Yarn package manager
- Git for cloning repositories
- Ethereum Wallet with a private key
- Testnet ETH for transaction fees (on the blockchain the sequencer is using)
- RPC Provider (Infura, Alchemy, or similar)
- Vocdoni API Endpoint (development or production)
-
Clone the repository (if not already done):
git clone https://github.com/vocdoni/davinci-sdk.git cd davinci-sdk -
Install root dependencies and build:
yarn install && yarn build -
Navigate to the script example:
cd examples/script -
Install script dependencies:
yarn install
-
Copy the environment template:
cp .env.example .env
-
Configure environment variables in
.env:# Sequencer API endpoint (e.g., https://sequencer-dev.davinci.vote) SEQUENCER_API_URL= # Census API endpoint (e.g., https://c3-dev.davinci.vote) CENSUS_API_URL= # RPC endpoint for blockchain transactions (get from Infura, Alchemy, etc.) # The chain is determined by the sequencer configuration RPC_URL=https://sepolia.infura.io/v3/YOUR_PROJECT_ID # Private key for transaction signing (without 0x prefix) PRIVATE_KEY=your_private_key_here # (optional) CSP private key for CSP census operations (without 0x prefix) # If not provided, a random private key will be generated CSP_PRIVATE_KEY=
| Variable | Description | Required | Example |
|---|---|---|---|
SEQUENCER_API_URL |
Vocdoni sequencer API endpoint | ✅ | https://sequencer1.davinci.vote |
CENSUS_API_URL |
Vocdoni census API endpoint | ✅ | https://census1.davinci.vote |
RPC_URL |
Blockchain RPC URL | ✅ | https://sepolia.infura.io/v3/... |
PRIVATE_KEY |
Wallet private key (no 0x) | ✅ | abcd1234... |
ORGANIZATION_REGISTRY_ADDRESS |
Custom org registry address | ❌ | 0x1234... |
PROCESS_REGISTRY_ADDRESS |
Custom process registry address | ❌ | 0x5678... |
FORCE_SEQUENCER_ADDRESSES |
Use addresses from sequencer info | ❌ | true or false |
The script now includes a new environment variable FORCE_SEQUENCER_ADDRESSES that allows you to force the use of contract addresses from the sequencer's info endpoint instead of using environment variables or default addresses.
-
When
FORCE_SEQUENCER_ADDRESSES=false(default):- The script will first check for
PROCESS_REGISTRY_ADDRESSandORGANIZATION_REGISTRY_ADDRESSin environment variables - If not found, it will fall back to the default deployed addresses
- The script will first check for
-
When
FORCE_SEQUENCER_ADDRESSES=true:- The script will fetch contract addresses from the sequencer's
/infoendpoint - It will use the
processandorganizationaddresses from the sequencer response - If the sequencer doesn't provide valid addresses, the script will throw an error
- Environment variables and default addresses are ignored when this flag is set
- The script will fetch contract addresses from the sequencer's
- Dynamic Configuration: Contract addresses are automatically retrieved from the sequencer
- Environment Consistency: Ensures the script uses the same contract addresses that the sequencer is configured to use
- Reduced Configuration: No need to manually specify contract addresses in environment variables
- Sign up at Infura or Alchemy
- Create a new project
- Copy the RPC endpoint URL for the network your sequencer is using
- Export from MetaMask: Settings → Security & Privacy → Reveal Private Key
⚠️ Security Warning: Never share your private key or commit it to version control
- Get free testnet ETH from the appropriate faucet for your network
- You'll need ~0.01 ETH for transaction fees
Run the complete demonstration:
yarn startThe script will execute all 19 steps automatically, taking approximately 5-10 minutes to complete.
The script provides detailed console output with:
- ✅ Success indicators for completed steps
- ℹ️ Information messages for ongoing processes
- 🚀 Progress indicators for major phases
- 📊 Final election results
When FORCE_SEQUENCER_ADDRESSES=true, you'll see output like:
ℹ FORCE_SEQUENCER_ADDRESSES is enabled - will use contract addresses from sequencer info endpoint
ℹ Using PROCESS_REGISTRY_ADDRESS from sequencer info: 0x1234...
ℹ Using ORGANIZATION_REGISTRY_ADDRESS from sequencer info: 0x5678...
When FORCE_SEQUENCER_ADDRESSES=false, you'll see:
ℹ FORCE_SEQUENCER_ADDRESSES is disabled - will use environment variables or default addresses
ℹ Using default process registry address: 0xabcd...
ℹ Using default organization registry address: 0xefgh...
The script performs a complete end-to-end voting process in 19 steps:
- Ping API - Verify connection to Vocdoni sequencer
- Fetch Info - Get zk-circuit URLs and contract addresses
- Create Census - Initialize a new voter registry
- Add Participants - Register 10 test voters with weights
- Verify Participants - Confirm all voters were added
- Fetch Census Root - Get Merkle root and size
- Push Metadata - Upload election information and questions
- Create Process - Initialize voting process via sequencer
- Deploy On-Chain - Register process on Ethereum
- Verify On-Chain - Confirm smart contract state
- Generate Proof Inputs - Create zk-SNARK inputs for each voter
- Generate Proofs - Compute zero-knowledge proofs
- Wait for Process - Ensure voting is ready
- Submit Votes - Send encrypted votes with proofs
- Check Vote Status - Verify submission success
- Wait for Processing - Allow votes to be settled
- Verify Votes - Confirm all votes were recorded
- End Process - Close voting and trigger result calculation
- Show Results - Display final tallied results
graph TD
A[Start Script] --> B[Setup API Connection]
B --> C[Create Census]
C --> D[Add 10 Test Participants]
D --> E[Create Election Metadata]
E --> F[Initialize Voting Process]
F --> G[Deploy Smart Contract]
G --> H[Generate zk-SNARK Proofs]
H --> I[Submit Encrypted Votes]
I --> J[Wait for Vote Processing]
J --> K[End Voting Process]
K --> L[Calculate & Display Results]
L --> M[Complete ✅]
style A fill:#e1f5fe
style M fill:#c8e6c9
style H fill:#fff3e0
style I fill:#fff3e0
[Step 1] Ping the HTTP API
✔ API reachable
[Step 2] Fetch zk‐circuit & on‐chain contract info
circuitUrl: https://...
contracts: {...}
• 0x1234... votes:
Q1: Blue (choice array: [0, 1, 0, 0])
Q2: Car (choice array: [1, 0, 0, 0])
• 0x1234... → voteId=0xabcd...
Election Results:
Question 1: What is your favorite color?
Red (0): 2
Blue (1): 3
Green (2): 1
Yellow (3): 4
Question 2: What is your preferred transportation?
Car (0): 4
Bike (1): 2
Public Transport (2): 1
Walking (3): 3
- Participants: 10 test voters with weighted voting power
- Questions: 2 multiple-choice questions with 4 options each
- Vote Privacy: All votes are encrypted and use zero-knowledge proofs
- Verification: Every vote is cryptographically verified
The script uses zk-SNARKs (Zero-Knowledge Succinct Non-Interactive Arguments of Knowledge) to ensure:
- Privacy: Vote choices remain secret
- Integrity: Votes cannot be tampered with
- Verifiability: Anyone can verify vote validity without seeing the content
graph LR
A[Voter] --> B[zk-SNARK Proof]
B --> C[Vocdoni Sequencer]
C --> D[Ethereum Smart Contract]
D --> E[On-Chain Results]
style B fill:#fff3e0
style C fill:#e8f5e8
style D fill:#e3f2fd
- Census Creation: Merkle tree of eligible voters
- Vote Encryption: Votes encrypted with election public key
- Proof Generation: zk-SNARK proves vote validity without revealing content
- Sequencer Processing: Off-chain aggregation and verification
- On-Chain Settlement: Final results published to Ethereum
- Cause: Missing or empty
.envfile - Solution: Copy
.env.exampleto.envand configure variables
- Cause: Not enough testnet ETH for gas fees
- Solution: Get more testnet ETH from the appropriate faucet for your network
- Cause: Normal behavior - process initialization takes time
- Solution: Wait for the process to become ready (usually 30-60 seconds)
- Cause: zk-SNARK circuit or input issues
- Solution: Check API connectivity and try again
- Cause:
FORCE_SEQUENCER_ADDRESSES=truebut sequencer doesn't provide valid addresses - Solution:
- Check sequencer configuration
- Set
FORCE_SEQUENCER_ADDRESSES=falseto use environment variables or defaults - Manually set
PROCESS_REGISTRY_ADDRESSandORGANIZATION_REGISTRY_ADDRESS
- Cause: Network issues or API downtime
- Solution:
- Check internet connection
- Verify SEQUENCER_API_URL and CENSUS_API_URL are correct
- Try again later if API is down
- Enable verbose logging: The script already provides detailed output
- Check transaction status: Use the appropriate block explorer for your network to verify transactions
- Verify API status: Test API connectivity with
curl $SEQUENCER_API_URL/ping - Check balances: Ensure wallet has sufficient testnet ETH
If you encounter issues:
- Check the troubleshooting section above
- Review the console output for specific error messages
- Verify all prerequisites are met
- Check the Vocdoni documentation
- Vocdoni Documentation
- DaVinci SDK Documentation
- API Documentation
- Protocol Documentation
- Smart Contract Documentation
- UI Example - Web interface for the voting system
- Integration Tests - Additional SDK usage examples
For issues and questions:
- Check the GitHub Issues
- Join the Vocdoni Discord
- Join our Telegram
- Follow us on Twitter
- Read the Vocdoni Documentation
- Visit our Website
For enterprise support and custom integrations, contact us at info@vocdoni.io.
Note: This is a demonstration script for development and testing purposes. For production use, implement proper error handling, security measures, and user interface components.