|
| 1 | +# Vocdoni DAVINCI SDK - Script Example |
| 2 | + |
| 3 | +A comprehensive end-to-end demonstration of the Vocdoni DAVINCI SDK showcasing the complete privacy-preserving voting process using zero-knowledge proofs. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Overview](#overview) |
| 8 | +- [Prerequisites](#prerequisites) |
| 9 | +- [Installation](#installation) |
| 10 | +- [Configuration](#configuration) |
| 11 | +- [Usage](#usage) |
| 12 | +- [What the Script Does](#what-the-script-does) |
| 13 | +- [Process Flow](#process-flow) |
| 14 | +- [Understanding the Output](#understanding-the-output) |
| 15 | +- [Architecture](#architecture) |
| 16 | +- [Troubleshooting](#troubleshooting) |
| 17 | +- [Additional Resources](#additional-resources) |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +This script demonstrates a complete voting workflow using the Vocdoni DAVINCI SDK, including: |
| 22 | + |
| 23 | +- **Privacy-Preserving Voting**: Uses zk-SNARKs to ensure vote privacy while maintaining verifiability |
| 24 | +- **Multi-Question Elections**: Creates an election with two questions (favorite color and transportation preference) |
| 25 | +- **Smart Contract Integration**: Interacts with Ethereum smart contracts on Sepolia testnet |
| 26 | +- **Complete Lifecycle**: From census creation to result tallying |
| 27 | +- **Cryptographic Operations**: Handles encryption, proof generation, and verification |
| 28 | + |
| 29 | +### Key Technologies |
| 30 | + |
| 31 | +- **zk-SNARKs**: Zero-knowledge proofs for private voting |
| 32 | +- **Ethereum**: Smart contract deployment and interaction |
| 33 | +- **Vocdoni Sequencer**: Off-chain vote processing and aggregation |
| 34 | +- **TypeScript**: Type-safe development environment |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +Before running this script, ensure you have: |
| 39 | + |
| 40 | +### Software Requirements |
| 41 | +- **Node.js** (v18 or higher) |
| 42 | +- **Yarn** package manager |
| 43 | +- **Git** for cloning repositories |
| 44 | + |
| 45 | +### Blockchain Requirements |
| 46 | +- **Ethereum Wallet** with a private key |
| 47 | +- **Sepolia Testnet ETH** for transaction fees |
| 48 | +- **RPC Provider** (Infura, Alchemy, or similar) |
| 49 | + |
| 50 | +### API Access |
| 51 | +- **Vocdoni API Endpoint** (development or production) |
| 52 | + |
| 53 | +## Installation |
| 54 | + |
| 55 | +1. **Clone the repository** (if not already done): |
| 56 | + ```bash |
| 57 | + git clone https://github.com/vocdoni/DAVINCI-sdk.git |
| 58 | + cd DAVINCI-sdk |
| 59 | + ``` |
| 60 | + |
| 61 | +2. **Install root dependencies and build**: |
| 62 | + ```bash |
| 63 | + yarn install && yarn build |
| 64 | + ``` |
| 65 | + |
| 66 | +3. **Navigate to the script example**: |
| 67 | + ```bash |
| 68 | + cd examples/script |
| 69 | + ``` |
| 70 | + |
| 71 | +4. **Install script dependencies**: |
| 72 | + ```bash |
| 73 | + yarn install |
| 74 | + ``` |
| 75 | + |
| 76 | +## Configuration |
| 77 | + |
| 78 | +1. **Copy the environment template**: |
| 79 | + ```bash |
| 80 | + cp .env.example .env |
| 81 | + ``` |
| 82 | + |
| 83 | +2. **Configure environment variables** in `.env`: |
| 84 | + |
| 85 | + ```env |
| 86 | + # Vocdoni API endpoint |
| 87 | + API_URL=https://sequencer1.davinci.vote |
| 88 | +
|
| 89 | + # Sepolia RPC endpoint (get from Infura, Alchemy, etc.) |
| 90 | + SEPOLIA_RPC=https://sepolia.infura.io/v3/YOUR_PROJECT_ID |
| 91 | +
|
| 92 | + # Private key for transaction signing (without 0x prefix) |
| 93 | + PRIVATE_KEY=your_private_key_here |
| 94 | +
|
| 95 | + # Optional: Custom contract addresses |
| 96 | + ORGANIZATION_REGISTRY_ADDRESS= |
| 97 | + PROCESS_REGISTRY_ADDRESS= |
| 98 | + ``` |
| 99 | + |
| 100 | +### Environment Variables Explained |
| 101 | + |
| 102 | +| Variable | Description | Required | Example | |
| 103 | +|----------|-------------|----------|---------| |
| 104 | +| `API_URL` | Vocdoni API endpoint | ✅ | `https://sequencer1.davinci.vote` | |
| 105 | +| `SEPOLIA_RPC` | Ethereum Sepolia RPC URL | ✅ | `https://sepolia.infura.io/v3/...` | |
| 106 | +| `PRIVATE_KEY` | Wallet private key (no 0x) | ✅ | `abcd1234...` | |
| 107 | +| `ORGANIZATION_REGISTRY_ADDRESS` | Custom org registry address | ❌ | `0x1234...` | |
| 108 | +| `PROCESS_REGISTRY_ADDRESS` | Custom process registry address | ❌ | `0x5678...` | |
| 109 | + |
| 110 | +### Getting Required Values |
| 111 | + |
| 112 | +#### Sepolia RPC URL |
| 113 | +1. Sign up at [Infura](https://infura.io/) or [Alchemy](https://alchemy.com/) |
| 114 | +2. Create a new project |
| 115 | +3. Copy the Sepolia endpoint URL |
| 116 | + |
| 117 | +#### Private Key |
| 118 | +1. Export from MetaMask: Settings → Security & Privacy → Reveal Private Key |
| 119 | +2. **⚠️ Security Warning**: Never share your private key or commit it to version control |
| 120 | + |
| 121 | +#### Sepolia ETH |
| 122 | +- Get free testnet ETH from [Sepolia Faucet](https://sepoliafaucet.com/) |
| 123 | +- You'll need ~0.01 ETH for transaction fees |
| 124 | + |
| 125 | +## Usage |
| 126 | + |
| 127 | +Run the complete demonstration: |
| 128 | + |
| 129 | +```bash |
| 130 | +yarn start |
| 131 | +``` |
| 132 | + |
| 133 | +The script will execute all 19 steps automatically, taking approximately 5-10 minutes to complete. |
| 134 | + |
| 135 | +### Expected Output |
| 136 | + |
| 137 | +The script provides detailed console output with: |
| 138 | +- ✅ Success indicators for completed steps |
| 139 | +- ℹ️ Information messages for ongoing processes |
| 140 | +- 🚀 Progress indicators for major phases |
| 141 | +- 📊 Final election results |
| 142 | + |
| 143 | +## What the Script Does |
| 144 | + |
| 145 | +The script performs a complete end-to-end voting process in 19 steps: |
| 146 | + |
| 147 | +### Phase 1: API Setup & Census Creation (Steps 1-6) |
| 148 | +1. **Ping API** - Verify connection to Vocdoni sequencer |
| 149 | +2. **Fetch Info** - Get zk-circuit URLs and contract addresses |
| 150 | +3. **Create Census** - Initialize a new voter registry |
| 151 | +4. **Add Participants** - Register 10 test voters with weights |
| 152 | +5. **Verify Participants** - Confirm all voters were added |
| 153 | +6. **Fetch Census Root** - Get Merkle root and size |
| 154 | + |
| 155 | +### Phase 2: Election Setup (Steps 7-10) |
| 156 | +7. **Push Metadata** - Upload election information and questions |
| 157 | +8. **Create Process** - Initialize voting process via sequencer |
| 158 | +9. **Deploy On-Chain** - Register process on Ethereum |
| 159 | +10. **Verify On-Chain** - Confirm smart contract state |
| 160 | + |
| 161 | +### Phase 3: Vote Generation & Submission (Steps 11-17) |
| 162 | +11. **Generate Proof Inputs** - Create zk-SNARK inputs for each voter |
| 163 | +12. **Generate Proofs** - Compute zero-knowledge proofs |
| 164 | +13. **Wait for Process** - Ensure voting is ready |
| 165 | +14. **Submit Votes** - Send encrypted votes with proofs |
| 166 | +15. **Check Vote Status** - Verify submission success |
| 167 | +16. **Wait for Processing** - Allow votes to be settled |
| 168 | +17. **Verify Votes** - Confirm all votes were recorded |
| 169 | + |
| 170 | +### Phase 4: Results & Completion (Steps 18-19) |
| 171 | +18. **End Process** - Close voting and trigger result calculation |
| 172 | +19. **Show Results** - Display final tallied results |
| 173 | + |
| 174 | +## Process Flow |
| 175 | + |
| 176 | +```mermaid |
| 177 | +graph TD |
| 178 | + A[Start Script] --> B[Setup API Connection] |
| 179 | + B --> C[Create Census] |
| 180 | + C --> D[Add 10 Test Participants] |
| 181 | + D --> E[Create Election Metadata] |
| 182 | + E --> F[Initialize Voting Process] |
| 183 | + F --> G[Deploy Smart Contract] |
| 184 | + G --> H[Generate zk-SNARK Proofs] |
| 185 | + H --> I[Submit Encrypted Votes] |
| 186 | + I --> J[Wait for Vote Processing] |
| 187 | + J --> K[End Voting Process] |
| 188 | + K --> L[Calculate & Display Results] |
| 189 | + L --> M[Complete ✅] |
| 190 | +
|
| 191 | + style A fill:#e1f5fe |
| 192 | + style M fill:#c8e6c9 |
| 193 | + style H fill:#fff3e0 |
| 194 | + style I fill:#fff3e0 |
| 195 | +``` |
| 196 | + |
| 197 | +## Understanding the Output |
| 198 | + |
| 199 | +### Console Output Sections |
| 200 | + |
| 201 | +#### Step Progress |
| 202 | +``` |
| 203 | +[Step 1] Ping the HTTP API |
| 204 | +✔ API reachable |
| 205 | +
|
| 206 | +[Step 2] Fetch zk‐circuit & on‐chain contract info |
| 207 | + circuitUrl: https://... |
| 208 | + contracts: {...} |
| 209 | +``` |
| 210 | + |
| 211 | +#### Vote Generation |
| 212 | +``` |
| 213 | + • 0x1234... votes: |
| 214 | + Q1: Blue (choice array: [0, 1, 0, 0]) |
| 215 | + Q2: Car (choice array: [1, 0, 0, 0]) |
| 216 | + • 0x1234... → voteId=0xabcd... |
| 217 | +``` |
| 218 | + |
| 219 | +#### Final Results |
| 220 | +``` |
| 221 | +Election Results: |
| 222 | +
|
| 223 | +Question 1: What is your favorite color? |
| 224 | +Red (0): 2 |
| 225 | +Blue (1): 3 |
| 226 | +Green (2): 1 |
| 227 | +Yellow (3): 4 |
| 228 | +
|
| 229 | +Question 2: What is your preferred transportation? |
| 230 | +Car (0): 4 |
| 231 | +Bike (1): 2 |
| 232 | +Public Transport (2): 1 |
| 233 | +Walking (3): 3 |
| 234 | +``` |
| 235 | + |
| 236 | +### Key Metrics |
| 237 | + |
| 238 | +- **Participants**: 10 test voters with weighted voting power |
| 239 | +- **Questions**: 2 multiple-choice questions with 4 options each |
| 240 | +- **Vote Privacy**: All votes are encrypted and use zero-knowledge proofs |
| 241 | +- **Verification**: Every vote is cryptographically verified |
| 242 | + |
| 243 | +## Architecture |
| 244 | + |
| 245 | +### Zero-Knowledge Proof System |
| 246 | + |
| 247 | +The script uses **zk-SNARKs** (Zero-Knowledge Succinct Non-Interactive Arguments of Knowledge) to ensure: |
| 248 | + |
| 249 | +- **Privacy**: Vote choices remain secret |
| 250 | +- **Integrity**: Votes cannot be tampered with |
| 251 | +- **Verifiability**: Anyone can verify vote validity without seeing the content |
| 252 | + |
| 253 | +### Smart Contract Integration |
| 254 | + |
| 255 | +```mermaid |
| 256 | +graph LR |
| 257 | + A[Voter] --> B[zk-SNARK Proof] |
| 258 | + B --> C[Vocdoni Sequencer] |
| 259 | + C --> D[Ethereum Smart Contract] |
| 260 | + D --> E[On-Chain Results] |
| 261 | + |
| 262 | + style B fill:#fff3e0 |
| 263 | + style C fill:#e8f5e8 |
| 264 | + style D fill:#e3f2fd |
| 265 | +``` |
| 266 | + |
| 267 | +### Data Flow |
| 268 | + |
| 269 | +1. **Census Creation**: Merkle tree of eligible voters |
| 270 | +2. **Vote Encryption**: Votes encrypted with election public key |
| 271 | +3. **Proof Generation**: zk-SNARK proves vote validity without revealing content |
| 272 | +4. **Sequencer Processing**: Off-chain aggregation and verification |
| 273 | +5. **On-Chain Settlement**: Final results published to Ethereum |
| 274 | + |
| 275 | +## Troubleshooting |
| 276 | + |
| 277 | +### Common Issues |
| 278 | + |
| 279 | +#### "API_URL environment variable is required" |
| 280 | +- **Cause**: Missing or empty `.env` file |
| 281 | +- **Solution**: Copy `.env.example` to `.env` and configure variables |
| 282 | + |
| 283 | +#### "insufficient funds for intrinsic transaction cost" |
| 284 | +- **Cause**: Not enough Sepolia ETH for gas fees |
| 285 | +- **Solution**: Get more testnet ETH from [Sepolia Faucet](https://sepoliafaucet.com/) |
| 286 | + |
| 287 | +#### "Process not ready yet, checking again in 10 seconds..." |
| 288 | +- **Cause**: Normal behavior - process initialization takes time |
| 289 | +- **Solution**: Wait for the process to become ready (usually 30-60 seconds) |
| 290 | + |
| 291 | +#### "Proof verification failed" |
| 292 | +- **Cause**: zk-SNARK circuit or input issues |
| 293 | +- **Solution**: Check API connectivity and try again |
| 294 | + |
| 295 | +#### Connection timeout errors |
| 296 | +- **Cause**: Network issues or API downtime |
| 297 | +- **Solution**: |
| 298 | + - Check internet connection |
| 299 | + - Verify API_URL is correct |
| 300 | + - Try again later if API is down |
| 301 | + |
| 302 | +### Debug Tips |
| 303 | + |
| 304 | +1. **Enable verbose logging**: The script already provides detailed output |
| 305 | +2. **Check transaction status**: Use [Sepolia Etherscan](https://sepolia.etherscan.io/) to verify transactions |
| 306 | +3. **Verify API status**: Test API connectivity with `curl $API_URL/ping` |
| 307 | +4. **Check balances**: Ensure wallet has sufficient Sepolia ETH |
| 308 | + |
| 309 | +### Getting Help |
| 310 | + |
| 311 | +If you encounter issues: |
| 312 | + |
| 313 | +1. Check the [troubleshooting section](#troubleshooting) above |
| 314 | +2. Review the console output for specific error messages |
| 315 | +3. Verify all prerequisites are met |
| 316 | +4. Check the [Vocdoni documentation](https://davinci.vote/) |
| 317 | + |
| 318 | +## Additional Resources |
| 319 | + |
| 320 | +### Documentation |
| 321 | +- [Vocdoni Documentation](https://davinci.vote/) |
| 322 | +- [DAVINCI SDK Reference](https://github.com/vocdoni/davinci-sdk) |
| 323 | +- [Smart Contract Documentation](https://github.com/vocdoni/davinci-contracts) |
| 324 | + |
| 325 | +### Related Examples |
| 326 | +- [UI Example](../ui/) - Web interface for the voting system |
| 327 | +- [Integration Tests](../../test/) - Additional SDK usage examples |
| 328 | + |
| 329 | +### Technical Resources |
| 330 | +- [zk-SNARKs Explained](https://z.cash/technology/zksnarks/) |
| 331 | +- [Ethereum Development](https://ethereum.org/developers/) |
| 332 | +- [Vocdoni Whitepaper](https://whitepaper.vocdoni.io/) |
| 333 | + |
| 334 | +--- |
| 335 | + |
| 336 | +**Note**: This is a demonstration script for development and testing purposes. For production use, implement proper error handling, security measures, and user interface components. |
0 commit comments