Skip to main content

Smart Contracts

Knight Shield uses 5 Compact smart contracts deployed on the Midnight Network. Compact is Midnight's domain-specific language for privacy-preserving smart contracts with built-in zero-knowledge proof generation.

Compact Language

Compact contracts define:

  • Ledgers — on-chain state (private by default)
  • Circuits — functions that read/write ledger state, compiled to ZK circuits
  • Witnesses — off-chain data provided by the caller during proof generation

When compiled, each contract produces:

  • JS bindings — TypeScript-callable interface for managed state and witnesses
  • ZK proving keys — cryptographic keys for proof generation
  • ZKIR — zero-knowledge intermediate representation

Compilation

pnpm compile:contracts

This runs compactc (v0.29.0) on each .compact file in packages/contracts/ and outputs artifacts to build/.

Contract Inventory

ContractPurposeKey Ledgers
SessionBillingRelay session usage billingsession_starts, accumulated_fees, total_sessions
FeePoolFee normalization pool (GhostTracking)pool_balance, total_normalized, user_deposits
ShardRegistryOn-chain shard hash verification (GhostShard)shard_hashes, shard_locations, wallet_locked
FeeReserveAdmin-controlled fee subsidy reservereserve_balance, total_deposited, total_withdrawn
FungibleTokenGeneric token (mint/burn/transfer)balances, total_supply, metadata

Deployment

Contracts are deployed to the Midnight preprod network using the deploy script:

pnpm deploy:contracts

This uses tsx to run scripts/deploy-contracts.ts, which:

  1. Loads compiled contract artifacts from build/
  2. Connects to the Midnight node via the SDK
  3. Deploys each contract and records the contract addresses
  4. Initializes contracts with default parameters

Contract Interaction

From TypeScript, contracts are accessed through the Midnight SDK's contract deployment providers:

import { KnightShieldWallet } from '@knight-shield/core';

const wallet = await KnightShieldWallet.fromSeed(seed, 'preprod');
const bridge = wallet.createProviderBridge();

// Use bridge to interact with deployed contracts

ZK Proof Flow

User action → JS binding → Witness data + Circuit inputs
→ Proof Server generates ZK proof
→ Transaction submitted with proof
→ Midnight node verifies proof + updates ledger

Every contract call generates a zero-knowledge proof that validates the state transition without revealing private inputs.