Skip to content

riusricardo/midnight-counter-playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

66 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒ™ Midnight Counter App Playground

Midnight Network TypeScript React Node.js

A comprehensive monorepo template for building privacy-preserving applications on the Midnight blockchain

๐Ÿš€ Overview

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.

โœจ Key Features

  • ๐Ÿ”’ 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

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   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      โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ฆ Project Structure

๐ŸŽฏ Applications

  • apps/web/ - React web application with Material-UI
    • Interactive counter interface
    • Midnight Lace wallet integration
    • Age verification forms
    • Real-time contract state updates

๐Ÿ“š Core Packages

  • 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

๐Ÿ› ๏ธ Supporting Packages

  • packages/ui/ - Reusable React components
  • packages/eslint-config/ - Shared linting configuration
  • packages/typescript-config/ - TypeScript configurations
  • packages/compact/ - Smart contract compilation tools

๐ŸŽฎ Getting Started

Prerequisites

  • Node.js (v18 or higher)
  • Yarn package manager
  • Midnight Lace Wallet (for web UI)

Installation

# Install dependencies
yarn install

# Build all packages
yarn build

๐ŸŒ Running the Web Application

# Start the React web app
yarn start

The web application will be available at http://localhost:3000

๐Ÿ’ป Using the CLI

Basic Counter Operations

# Run CLI on testnet (without proof server)
yarn counter-cli-remote

# Run CLI with integrated proof server
yarn counter-cli-remote-ps

CLI Features

The CLI provides an interactive menu with the following options:

  1. ๐Ÿš€ Deploy Contract - Deploy a new counter contract
  2. ๐Ÿ”— Connect to Contract - Connect to an existing contract
  3. โž• Increment Counter - Increment the counter value (requires age verification)
  4. ๐Ÿ“Š View Counter - Display current counter value
  5. ๐Ÿ‘ค Set Credentials - Configure your age verification credentials
  6. โœ… Check Verification - View your verification status

Credential Management

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): 15

Note: You must be at least 21 years old to increment the counter, as verified through zero-knowledge proofs.

๐Ÿ” Smart Contract Details

Contract Features

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

Contract Source

// 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);
}

Building the Contract

# Compile the smart contract
yarn compact

# Run contract tests
yarn test-contract

# Generate ZK circuits (production)
yarn compact --zk

๐Ÿงช Testing

Integration Tests

# Run API integration tests
yarn test-api

# Run contract simulation tests
yarn test-contract

๐Ÿ”ง Development

Building

# Build all packages
yarn build

# Build specific package
yarn workspace @midnight-ntwrk/counter-contract build

Linting

# Lint all packages
yarn lint

# Fix linting issues
yarn lint:fix

Type Checking

# Type check all packages
yarn typecheck

๐ŸŒŸ Key Concepts

Privacy-Preserving Credentials

This 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

Midnight Blockchain Integration

  • 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

๐Ÿ“– Documentation

๐Ÿค Contributing

We welcome contributions! Please read our contribution guidelines and:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow TypeScript best practices
  • Add tests for new features
  • Update documentation as needed
  • Ensure all linting passes

๐Ÿ“„ License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

๐Ÿ†˜ Support

  • 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

๐ŸŒ Website โ€ข ๐Ÿ“š Docs โ€ข ๐Ÿ’ฌ Community

yarn counter-cli-remote-ps ```

The Counter Contract

The counter-contract subdirectory contains:

Building the Smart Contract

Compile the contract:

yarn compact

You 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.0

The 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-contract

Test Files 1 passed (1) - Tests 3 passed (3)

Contributing

Contributions are welcome! Please open issues or pull requests as needed.


For more details, see the README files in each package or app directory.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors