Skip to main content
For the complete documentation index, see llms.txt

Create a Midnight DApp

create-mn-app CLI 도구를 사용하여 Midnight DApp을 스캐폴딩하는 방법을 설명합니다.

Prerequisites

Midnight DApp을 실행하려면 다음이 필요합니다:

Midnight CLI tool

create-mn-app CLI 도구는 Midnight DApp용 스타터 템플릿을 제공합니다. TypeScript 설정, 핫 리로딩, wallet 연동 로직이 미리 구성되어 있습니다.

새 DApp을 스캐폴딩하세요:

npx create-mn-app [project-name]
info

[project-name]을 새 DApp의 이름으로 교체하세요.

The CLI tool first prompts you to choose the type of project you want to create:

  • Contract
  • Full DApp

If you are new to Midnight development, then starting with contract is recommended. It provides a minimal setup that focuses on compiling and deploying a Compact smart contract before working with a full application scaffold.

Contract

Selecting Contract scaffolds a minimal project for deploying a Compact smart contract.

You are prompted to choose a contract template. Currently the available option is:

  • Hello world

The Hello world contract is a basic example that demonstrates how to deploy a Compact contract and interact with it from the CLI.

Full DApp

Selecting Full DApp scaffolds a complete decentralized application with contract logic, deployment scripts, and a CLI for interacting with the contract.

You are prompted to choose a DApp template. Available options include:

  • Counter
  • Bulletin board

The Counter template is an example that demonstrates how to maintain and update shared contract state by incrementing or decrementing a counter.

The Bulletin board template is a privacy-preserving message board that allows users to post and remove messages using zero-knowledge (ZK) proofs.

이 가이드에서는 첫 번째 프롬프트에서 Contract를 선택하고, 템플릿 선택 프롬프트에서 Hello world를 선택하세요. 나머지 프롬프트에 따라 설정을 완료하세요.

Full DApp을 선택한 경우, 각 템플릿의 전용 튜토리얼을 참고하세요:

다음과 유사한 출력이 나타납니다:

Creating a new Midnight app in /path/to/my-app.

Template: hello-world

✔ Project structure created
✔ Dependencies installed
✔ Git repository initialized
✔ Docker is ready for proof server
✔ Contract compiled successfully

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎉 Success! Your Midnight app is ready.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

완료되면 my-app 디렉토리로 이동하세요:

cd my-app

프로젝트 구조:

my-app/
├── contracts/
│ └── hello-world.compact # Compact smart contract
├── src/
│ ├── cli.ts # 배포된 contract와 상호작용
│ ├── deploy.ts # Preprod에 contract 배포
│ └── check-balance.ts # wallet 잔액 확인
├── docker-compose.yml # Proof server 설정
├── package.json
└── deployment.json # 배포 정보 (wallet seed와 contract 주소 포함)

contracts/hello-world.compact 파일에 Compact smart contract 코드가 있습니다.

pragma language_version 0.22;

export ledger message: Opaque<"string">;

export circuit storeMessage(newMessage: Opaque<"string">): [] {
message = disclose(newMessage);
}

Compact는 contract 로직을 정의하는 Midnight의 smart contract 언어입니다. TypeScript와 유사하지만 Midnight 런타임용으로 설계되었습니다.

docker-compose.yml 파일에는 proof server 설정이 포함되어 있습니다. Proof server는 Midnight 네트워크에서 smart contract용 ZK proof를 생성하며, contract를 배포하거나 상호작용하기 전에 반드시 실행 중이어야 합니다.

Set up the project

프로젝트 설정 스크립트를 실행하세요:

npm run setup

이 스크립트는 proof server 시작, contract 컴파일, Preprod 네트워크 배포를 순서대로 수행합니다.

새 wallet을 생성할지, 기존 wallet을 복원할지 선택하는 메뉴가 나타납니다. 새 wallet을 생성하면 seed와 wallet 주소가 표시됩니다. Seed는 복원 시 필요하므로 안전한 곳에 저장하세요.

note

Preprod faucet에서 tNIGHT token을 받아 wallet에 충전해야 합니다. tNIGHT는 Midnight blockchain에서 transaction 수수료 지불에 필요한 tDUST를 생성합니다.

Wallet에 자금을 충전하면 CLI가 자동으로 tDUST를 생성하고 Preprod 네트워크에 contract를 배포합니다. 몇 초 정도 소요됩니다.

배포가 완료되면 다음과 같은 출력이 표시됩니다:

  ✅ Contract deployed successfully!

Contract Address: b92d63e566cbcede6............

Saved to deployment.json

루트 디렉토리의 deployment.json 파일에 contract 배포 정보가 저장됩니다. CLI는 contract와 상호작용할 때 이 파일을 참조합니다.

Interact with the contract

Contract와 상호작용할 수 있는 CLI를 시작하세요:

npm run cli

Wallet seed를 입력하라는 프롬프트가 나타납니다. Wallet이 네트워크와 동기화되면 다음 메뉴로 contract와 상호작용할 수 있습니다:

1. Store a message
2. Read current message
3. Check wallet balance
4. Exit

원하는 옵션을 선택하고 프롬프트에 따라 진행하세요.

Templates

Midnight CLI 도구에서 제공하는 스타터 템플릿:

Hello world

Blockchain에 메시지를 저장하고 조회하는 간단한 DApp입니다.

npx create-mn-app my-app

Hello world는 기본 템플릿이므로 --template 플래그 없이 사용할 수 있으며, 의존성이 자동으로 설치됩니다.

Counter

카운터를 증가/감소시키는 간단한 DApp입니다.

npx create-mn-app my-app --template counter
cd my-app
npm install

자세한 내용은 Counter DApp 예제를 참고하세요.

Bulletin board

메시지를 게시하고 삭제할 수 있는 프라이버시 기반 게시판 DApp입니다.

npx create-mn-app my-app --template bboard
cd my-app
npm install

ZK proof를 활용해 온체인에서 신원을 드러내지 않으면서도 게시자와 메시지 소유자를 검증합니다. 자세한 내용은 Bulletin board DApp 예제를 참고하세요.

CLI options

create-mn-app으로 DApp을 만들 때 사용 가능한 옵션:

옵션설명
-t, --template <name>템플릿 선택: hello-world, counter, bboard
--use-npm/yarn/pnpm/bun특정 패키지 매니저 강제 사용
--skip-install의존성 설치 건너뛰기
--skip-gitgit 초기화 건너뛰기
--verbose상세 출력
-h, --help도움말
-V, --version버전 정보

Next steps

Midnight CLI 도구로 DApp을 스캐폴딩하는 방법을 익혔습니다. Hello world 튜토리얼에서 Compact 언어로 첫 번째 Midnight contract를 직접 작성해 보세요.