DEV Community

Martin Rivero
Martin Rivero

Posted on

Build Your First Privacy-Preserving App on Midnight Network

If you’ve been curious about privacy-first blockchain applications, the Midnight Network (MN) is an exciting playground. With MN, you can build DApps that use Zero-Knowledge Proofs (ZKPs) to protect sensitive data while still interacting with the blockchain in a verifiable way. The magic? Selective disclosure—users can prove specific information without exposing everything.

Here’s a practical guide to get started.

Quick Start with create-mn-app

The create-mn-app CLI scaffolds Midnight Network applications with zero configuration. It comes preconfigured with TypeScript, hot reloading, and wallet generation. Dependencies for Node.js, Docker, and the Compact compiler are managed automatically.

bash
npx create-mn-app my-app
cd my-app
npm run setup

Enter fullscreen mode Exit fullscreen mode

Available templates include:

  • Hello World: Message storage with ZKPs
  • Counter DApp: Increment/decrement with ZKPs

Other templates like Bulletin Board, DEX, and Midnight Kitties are in development.

Manual Setup

Prerequisites

  • Node.js >= 20.x (install via NVM)
  • Basic command-line knowledge
  • Code editor (e.g., Visual Studio Code)
bash
nvm install 20

Enter fullscreen mode Exit fullscreen mode

Create Your Project

mkdir my-mn-app
cd my-mn-app
npm init -y
mkdir src contracts
touch contracts/hello-world.compact
Enter fullscreen mode Exit fullscreen mode

Define the Smart Contract

Add the pragma version:

pragma language_version 0.17;
Enter fullscreen mode Exit fullscreen mode

Create a ledger to store your data:

export ledger message: Opaque<"string">;
Enter fullscreen mode Exit fullscreen mode

Define a circuit to update your ledger:

export circuit storeMessage(customMessage: Opaque<"string">): [] {
  message = disclose(customMessage);
}
Enter fullscreen mode Exit fullscreen mode

disclose is required to intentionally make the data public. Compact enforces privacy by default.

Compile the Smart Contract

compact compile contracts/hello-world.compact contracts/managed/hello-world
Enter fullscreen mode Exit fullscreen mode

This generates:

  • contract/: Compiled contract artifacts
  • keys/: Cryptographic keys for ZKPs
  • zkir/: Zero-Knowledge Intermediate Representation
  • compiler/: Intermediate files for building

With this setup, you can start building privacy-preserving DApps on Midnight Network. Users can interact securely while sensitive data stays private. The combination of Compact contracts + ZKPs ensures scalable, secure, and verifiable privacy on-chain.

Happy coding! 🛠️

🚀 Next Steps

In the upcoming parts of this series, we’ll dive into how to deploy an MN app and then explore how to interact with an MN app. Stay tuned to continue building privacy-preserving DApps on the Midnight Network! 🌌


Top comments (0)