Privacy Architecture in VoidDex: Integrating Railgun's zkSNARK System
How VoidDex implements private token swaps using Railgun's zero-knowledge proof system, covering the shield/unshield flow, client-side proof generation, and the Waku P2P broadcaster network.
Building a privacy-first DEX aggregator means understanding how zero-knowledge proofs can hide transaction details while still allowing on-chain verification. VoidDex integrates with Railgun to provide this privacy layer, letting users swap tokens without exposing their activity on the blockchain.
Client-Side First: Your Keys Never Leave
Before diving into the technical details, the most important architectural decision: all sensitive operations happen in your browser. The VoidDex server never sees:
- Your wallet private key
- Your Railgun viewing key (used to decrypt your private balance)
- Your Railgun spending key (used to generate proofs)
- Your unencrypted private balance
- Which UTXOs belong to you
The server only handles non-sensitive operations: fetching DEX quotes, optimizing routes, and relaying already-signed transactions. Zero-knowledge proofs are generated entirely in-browser using WASM modules. This isn't just a security feature - it's the foundation of the entire privacy model. If the server could see your keys, there would be no privacy.
The Privacy Problem
Every transaction on Ethereum is public. When you swap tokens on Uniswap, anyone can see your address, the tokens involved, and the amounts. This creates several problems:
- Front-running: MEV bots see your pending transaction and execute their own first
- Sandwich attacks: Bots manipulate prices before and after your swap
- Surveillance: Your entire financial history is linked to your address
- Profiling: Exchanges and services can analyze your trading patterns
Railgun solves this with zkSNARKs (Zero-Knowledge Succinct Non-Interactive Arguments of Knowledge), which let you prove you have tokens without revealing which specific tokens are yours.
How Railgun Privacy Works
Railgun maintains a privacy pool of shielded tokens. When you shield tokens, they enter this pool and become indistinguishable from other shielded tokens. The system uses a UTXO model similar to Bitcoin but with zero-knowledge proofs.
Your private balance consists of encrypted UTXOs (Unspent Transaction Outputs) that only you can decrypt using your viewing key. When you want to spend, you generate a proof that:
- You own valid UTXOs in the pool
- The UTXOs haven't been spent before
- The amounts add up correctly
The proof reveals none of the actual UTXO details.
Client-Side Architecture
All privacy operations in VoidDex happen in the browser. The Railgun SDK loads WASM modules for cryptographic operations:
TypeScript
Wallet creation generates the cryptographic keys needed for privacy:
TypeScript
The Shield Operation
Shielding moves tokens from your public wallet into the privacy pool:
TypeScript
The shield transaction is public (everyone sees you deposited tokens), but your subsequent activity with those tokens is private.
Private Transfers
Once tokens are shielded, you can transfer them privately to other Railgun addresses:
TypeScript
The blockchain sees that someone transferred some amount of tokens, but not who or how much specifically.
Private Swaps via Adapt Contracts
The key innovation for VoidDex is Railgun's "adapt contract" system. It allows private funds to interact with any DeFi protocol:
The flow works like this:
- Your browser generates a proof spending your private UTXOs
- The proof specifies VoidDex Router as the "adapt contract"
- Railgun verifies the proof and calls the router with the tokens
- The router executes the swap on Uniswap/Curve/etc.
- Output tokens are automatically re-shielded to your private balance
TypeScript
Waku P2P Broadcasting
For maximum privacy, VoidDex uses Waku P2P to broadcast transactions instead of direct RPC calls:
TypeScript
The broadcaster nodes receive encrypted transactions, verify the proofs, pay the gas, and submit to the blockchain. Users pay two types of fees: a fixed 0.25% Railgun unshield fee (which goes to the Railgun protocol), and a dynamic broadcaster fee that varies per-transaction based on current gas costs.
Balance Scanning
To see your private balance, the SDK scans the blockchain for encrypted notes addressed to your viewing key:
TypeScript
This scanning is computationally intensive on first load (needs to process historical blocks) but incremental afterward.
Security Considerations
Viewing Key Protection: The viewing key lets anyone see your balance. VoidDex encrypts it with your password before storing in localStorage:
TypeScriptasync function secureViewingKey(viewingKey: string, password: string) { const salt = crypto.getRandomValues(new Uint8Array(16)); const key = await deriveKey(password, salt); const encrypted = await encrypt(viewingKey, key); localStorage.setItem('voiddex_vk', JSON.stringify({ salt: Array.from(salt), ciphertext: encrypted, })); }
Spending Key Security: The spending key never touches VoidDex servers. It's derived from your mnemonic and used only in-browser for proof generation.
Proof Verification: All proofs are verified on-chain by Railgun's contracts. Invalid proofs are rejected, protecting against malicious inputs.
Privacy Limitations
Railgun privacy has some limitations:
- Shield/Unshield Visibility: These transactions are public; observers know you're using Railgun
- Timing Analysis: Sophisticated observers might correlate shield/unshield timing
- Amount Inference: If pool liquidity is low, amounts might be inferable
- RPC Privacy: Your RPC provider sees your queries (mitigated by Waku)
For best privacy:
- Shield tokens and wait before using them
- Don't unshield exact amounts you shielded
- Use the Waku broadcaster instead of direct RPC
- Maintain some shielded balance at all times