A comprehensive monorepo template for building privacy-preserving applications on the Midnight blockchain
The Midnight Counter App Template is a full-featured monorepo showcasing how to build privacy-preserving decentralized applications on the Midnight blockchain. This template demonstrates smart contract development, wallet integration, credential management, and age verification using zero-knowledge proofs.
- 🔒 Privacy-Preserving Smart Contracts - Built with Compact language for zero-knowledge computations
- 👤 Age Verification System - Credential management with ZK proofs
- 🌐 Multi-Platform Support - Web UI, CLI tools, and API libraries
- 🔐 Wallet Integration - Seamless integration with Midnight Lace wallet
- 🧪 Testing Framework - Comprehensive unit tests and simulation tools
- 🛠️ Developer Tools - CLI utilities for contract deployment and testing
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Web App UI │ │ CLI Tools │ │ Smart Contract │
│ │ │ │ │ │
│ • React Frontend│ │ • Contract Deploy│ │ • Counter Logic │
│ • Wallet Connect│◄──►│ • Credential Mgmt│◄──►│ • Age Verify │
│ • Age Verify │ │ • Testing Utils │ │ • ZK Proofs │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │ │
└───────────────────────┼───────────────────────┘
│
┌──────────────────┐
│ Counter API │
│ │
│ • Core Logic │
│ • Provider Setup │
│ • Type Defs │
└──────────────────┘
apps/web/- React web application with Material-UI- Interactive counter interface
- Midnight Lace wallet integration
- Age verification forms
- Real-time contract state updates
-
packages/contracts/counter/- Smart contract implementation- Compact language source code
- Unit tests and simulators
- ZK circuit generation
-
packages/api/counter/- Unified API layer- Browser and Node.js compatibility
- Provider abstractions
- Contract interaction utilities
-
packages/cli/counter/- Command-line interface- Contract deployment tools
- Credential management system
- Development and testing utilities
packages/ui/- Reusable React componentspackages/eslint-config/- Shared linting configurationpackages/typescript-config/- TypeScript configurationspackages/compact/- Smart contract compilation tools
- Node.js (v18 or higher)
- Yarn package manager
- Midnight Lace Wallet (for web UI)
# Install dependencies
yarn install
# Build all packages
yarn build# Start the React web app
yarn startThe web application will be available at http://localhost:3000
# Run CLI on testnet (without proof server)
yarn counter-cli-remote
# Run CLI with integrated proof server
yarn counter-cli-remote-psThe CLI provides an interactive menu with the following options:
- 🚀 Deploy Contract - Deploy a new counter contract
- 🔗 Connect to Contract - Connect to an existing contract
- ➕ Increment Counter - Increment the counter value (requires age verification)
- 📊 View Counter - Display current counter value
- 👤 Set Credentials - Configure your age verification credentials
- ✅ Check Verification - View your verification status
The CLI includes a comprehensive credential management system:
# Set your credentials (interactive prompts)
Enter your first name: John
Enter your last name: Doe
Enter your birth year (YYYY): 1990
Enter your birth month (1-12): 5
Enter your birth day (1-31): 15Note: You must be at least 21 years old to increment the counter, as verified through zero-knowledge proofs.
The counter contract demonstrates:
- Public State Management - Maintains a counter value on the blockchain
- Age Verification - Requires proof of being 21+ to increment
- Credential Storage - Securely stores user credentials in private state
- Zero-Knowledge Proofs - Verifies age without revealing exact birthdate
// Public ledger state
export ledger round: Counter;
// Private state for credentials
circuit privateState: CredentialSubject;
// Increment function with age verification
export circuit increment(): [] {
// Verify user is at least 21 years old
require(isAtLeast21(privateState.birth_timestamp));
// Increment the counter
round.increment(1);
}
# Compile the smart contract
yarn compact
# Run contract tests
yarn test-contract
# Generate ZK circuits (production)
yarn compact --zk# Run API integration tests
yarn test-api
# Run contract simulation tests
yarn test-contract# Build all packages
yarn build
# Build specific package
yarn workspace @midnight-ntwrk/counter-contract build# Lint all packages
yarn lint
# Fix linting issues
yarn lint:fix# Type check all packages
yarn typecheckThis template showcases how to implement privacy-preserving credential management:
- Private State Storage - User credentials stored securely in contract private state
- Zero-Knowledge Age Verification - Prove you're 21+ without revealing exact age
- Wallet-Based Identity - Credentials tied to wallet public keys for security
- Compact Smart Contracts - Privacy-focused smart contract language
- ZK Circuit Generation - Automatic zero-knowledge proof generation
- Wallet Provider Integration - Seamless wallet connectivity
- State Management - Both public and private state handling
We welcome contributions! Please read our contribution guidelines and:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow TypeScript best practices
- Add tests for new features
- Update documentation as needed
- Ensure all linting passes
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
- Documentation: Comprehensive guides in each package
- Issues: Report bugs and request features via GitHub Issues
- Community: Join the Midnight Network community for discussions
Built with ❤️ for the Midnight Network ecosystem
The counter-contract subdirectory contains:
- the smart contract
- some unit tests to test the smart contract
Compile the contract:
yarn compactYou should see the following output from npm and the Compact compiler:
> compact
> compactc --skip-zk packages/contracts/counter/src/counter.compact packages/contracts/counter/src/managed/counter
Compactc version: 0.24.0The compiler will complete very quickly because we've instructed it to skip ZK key generation with the option --skip-zk. The compiler's output files will be placed in the directory packages/contracts/counter/src/managed/counter.
Run contract's tests:
yarn test-contractTest Files 1 passed (1) - Tests 3 passed (3)
Contributions are welcome! Please open issues or pull requests as needed.
For more details, see the README files in each package or app directory.