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
| Contract | Purpose | Key Ledgers |
|---|---|---|
| SessionBilling | Relay session usage billing | session_starts, accumulated_fees, total_sessions |
| FeePool | Fee normalization pool (GhostTracking) | pool_balance, total_normalized, user_deposits |
| ShardRegistry | On-chain shard hash verification (GhostShard) | shard_hashes, shard_locations, wallet_locked |
| FeeReserve | Admin-controlled fee subsidy reserve | reserve_balance, total_deposited, total_withdrawn |
| FungibleToken | Generic 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:
- Loads compiled contract artifacts from
build/ - Connects to the Midnight node via the SDK
- Deploys each contract and records the contract addresses
- 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.