# **Getting Started with the Compact Compiler on Midnight** ***Introduction to Compact*** Compact is a domain-specific language (DSL) based on TypeScript, used to write smart contracts on the Midnight protocol. It allows for concise expression of contracts and integrates seamlessly with TypeScript. This language empowers developers to create privacy-preserving smart contracts, utilizing zero-knowledge proofs (ZKPs) to ensure data protection. ***Setting Up the Compact Compiler*** To start using the Compact compiler, follow these steps: 1. *Download the Compiler:* Obtain the Compact compiler from the Midnight devnet releases repository. Choose the appropriate package for your operating system (Linux or macOS). ``` mkdir ~/my-binaries/compactc cd ~/my-binaries/compactc unzip ~/Downloads/compactc-<platform>.zip ``` 2. *Verify Installation:* Check that the compiler runs correctly. ``` ./compactc --version ``` Ensure that you have GNU C Library (glibc) version 2.35 or greater installed on your system. You can check this with: ``` ldd --version ``` 3. *Configure Environment Variables:* Set the `COMPACT_HOME` environment variable to the directory where the Compact compiler is installed. ``` export COMPACT_HOME='/absolute/path/to/compactc' ``` Add this directory to your `PATH` variable to run `compactc` as a shell command. 4. *Authorize Applications on macOS:* If using macOS, add `compactc` and `zkir` to your authorized applications set. * Right-click on `compactc` in Finder, select Open, and confirm. * Go to System Settings > Privacy & Security and allow `compactc`. * Repeat for `zkir`. ***Optional: VSCode Extension*** Midnight provides a Visual Studio Code (VSCode) extension for creating and editing contracts in Compact. This extension offers syntax highlighting, live dynamic checking, and templates. 1. *Download the Extension:* Obtain the VSCode extension from the Midnight devnet releases repository. 3. *Install the Extension:* * Open the Extensions pane in VSCode. * Click the `...` symbol and select "Install from VSIX..." * Locate the downloaded file and install it. ***Writing a Simple Contract*** Here’s a basic example of a Compact contract, such as the one behind the counter DApp: ``` include "std"; ledger { // public state round: Counter; } // transition function changing public state export circuit increment(): Void { ledger.round.increment(1); } ``` * *Ledger Declaration:* Specifies the public state of the contract, which includes a single field `round` of type `Counter`. * *Circuit Function:* Defines a public operation `increment` that increments the `round` counter by 1. ***Generated Source*** Running the Compact compiler generates several files: * *contract:* TypeScript/JavaScript API definitions for the contract. * *zkir:* Intermediate representations for the circuits. * *keys:* Prover and verifier keys for each circuit. These files are organized into directories under `managed/counter` within your project. For example, the `increment` circuit will have corresponding files in these directories. ***Advanced Contract Features*** Compact supports more complex constructs such as enumerations and structured types, multiple circuits, and private state manipulation through witness functions. ***Example: Detailed Counter Contract*** Let's dive deeper into the counter contract example found in `examples/counter/contract/src/counter.compact`: ``` include "std"; ledger { // public state round: Counter; } export circuit increment(): Void { ledger.round.increment(1); } ``` 1. *Public State:* Declares a `round` field of type `Counter` visible on the Midnight blockchain. 2. *Increment Circuit:* Provides a public operation that increments the `round` counter by 1. This contract demonstrates a simple use of Compact to define a smart contract with a single public state and operation. ***Generated TypeScript Definitions*** After compiling the contract, TypeScript definitions are generated, including: * *Circuits:* Functions corresponding to the circuits in the contract. * *Ledger:* Public state fields, such as `round`. * *Witnesses:* Types for private state manipulation, if any. ***Integration with TypeScript*** Developers can write the business logic of their DApps in TypeScript, leveraging the Compact compiler to handle the smart contract aspects. This integration allows for a seamless development experience using familiar tools and languages. ***Conclusion*** The Compact compiler on Midnight provides a robust and user-friendly environment for developing privacy-preserving smart contracts. By leveraging TypeScript and advanced cryptographic techniques, developers can create secure and compliant DApps. Whether you are new to blockchain development or an experienced developer, Compact offers the tools and flexibility needed to build innovative applications on the Midnight protocol. For more detailed information, visit the [Midnight Documentation](https://docs.midnight.network/).