Skip to main content

The Contract

pragma language_version >= 0.23;

import CompactStandardLibrary;

// On-chain storage for the message
export ledger message: Opaque<"string">;

// Store a new message
export circuit storeMessage(customMessage: Opaque<"string">): [] {
    message = disclose(customMessage);
}

// Retrieve the current message
export circuit getMessage(): Opaque<"string"> {
    return message;
}

How It Works

Pragma Directive

pragma language_version >= 0.23;
Specifies the minimum Compact compiler version this contract supports.

Import Statement

import CompactStandardLibrary;
Provides built-in types like Opaque, Bytes, and stdlib functions.

Ledger Declaration

export ledger message: Opaque<"string">;
Declares on-chain state. Opaque<"string"> stores a variable-length byte sequence whose value is opaque to the circuit (hashed inside ZK, full value visible off-chain).

Circuits (Functions)

export circuit storeMessage(customMessage: Opaque<"string">): [] {
    message = disclose(customMessage);
}
circuit declares a function that compiles to a zero-knowledge circuit. export makes it callable from outside the contract. [] is the empty-tuple unit type — the circuit returns nothing.

The disclose() Function

message = disclose(customMessage);
Opaque<"string"> values are treated as witness-tainted: the compiler refuses to write them to the public ledger unless you explicitly disclose them. disclose(x) is a compiler annotation that says “I know this value flows from a private/witness source to a public location and that’s intentional.” If you tried message = customMessage without disclose, the compiler would reject it with:
potential witness-value disclosure must be declared but is not
Every circuit parameter requires disclose() when it flows directly into a ledger write, regardless of its type. The compiler is conservative: it doesn’t know whether a given call site supplies the parameter from a public source or from witness data, so it forces you to declare the disclosure explicitly at every ledger boundary.

What’s Next

First App - Counter

Build an interactive counter with validation

Primitive Types

Learn about Compact’s type system