惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
Jina AI
Jina AI
N
Netflix TechBlog - Medium
GbyAI
GbyAI
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
I
Intezer
T
Tor Project blog
P
Palo Alto Networks Blog
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
Cloudbric
Cloudbric
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
Last Week in AI
Last Week in AI
S
Security Affairs
博客园 - Franky
F
Fortinet All Blogs
量子位
M
MIT News - Artificial intelligence
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
S
Secure Thoughts
V
Visual Studio Blog
AI
AI
美团技术团队
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
T
Threatpost
Cyberwarzone
Cyberwarzone

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Build shielded token mint, transfer, and burn flows in Compact
Ritwika Kanc · 2026-05-12 · via DEV Community

Shielded tokens let Midnight applications move value while keeping sensitive
transaction details private. In this tutorial, you will build a small Compact
project that mints shielded value, transfers it, burns it, and verifies the
full lifecycle with a Vitest test suite.

The core idea is simple but important: a newly minted shielded coin is not yet a
committed ledger coin. Fresh coins use ShieldedCoinInfo and can be spent
immediately with sendImmediateShielded. Coins that already exist in the ledger
use QualifiedShieldedCoinInfo, which includes the Merkle tree index required
by sendShielded.

You will use that distinction to build three practical flows: minting to the
current contract, transferring committed value to a user, and burning both fresh
and committed shielded value. Along the way, you will also handle change outputs
and nonce derivation, two details that matter in real wallet and DApp code.

By the end of this tutorial, you will:

  • Create a Compact contract that mints shielded tokens to itself.
  • Use evolveNonce to derive mint nonces safely.
  • Transfer committed shielded coins with sendShielded.
  • Burn committed and freshly minted coins with shieldedBurnAddress.
  • Use sendImmediateShielded for coins created in the same transaction.
  • Write Vitest tests for minting, transfers, burns, change, nonce reuse, and the Merkle timing rule.

Prerequisites

You need:

  • Node.js 20 or newer.
  • npm.
  • The Midnight Compact toolchain.
  • Basic TypeScript knowledge.
  • Basic familiarity with Compact circuits and ledgers.

Install or update the Compact toolchain from the Midnight documentation, then
check that the command is available:

compact check

Enter fullscreen mode Exit fullscreen mode

Expected result:

compact: Latest version available: ...

Enter fullscreen mode Exit fullscreen mode

On Windows, run the Compact toolchain from WSL or make sure the Midnight
compact binary appears before C:\Windows\System32\compact.exe in your
PATH. Windows also has a system command named compact, and that command is
not the Midnight compiler.

What you will build

The finished demo project has this structure:

shielded-token-operations/
|-- package.json
|-- tsconfig.json
|-- src/
|   |-- shielded-token-lifecycle.compact
|   |-- witnesses.ts
|   `-- model/
|       `-- shielded-token-model.ts
`-- test/
    `-- shielded-token-lifecycle.test.ts

Enter fullscreen mode Exit fullscreen mode

The Compact file contains the contract. The witness file manages local nonce
state. The TypeScript model gives you deterministic tests without needing a live
node. The Vitest file proves the flows and catches the mistakes developers tend
to make when they mix fresh and committed shielded coins.

Part 0: Choose the right shielded API

Before writing code, map each operation to the correct standard library helper.
Most bugs in shielded token examples come from choosing the right idea but the
wrong helper.

Use mintShieldedToken when the contract creates a new shielded token. It
returns ShieldedCoinInfo, which describes a fresh output from the current
transaction.

Use sendImmediateShielded when that fresh output is spent in the same
transaction. This is the right tool for atomic flows such as mint-and-send or
mint-and-burn.

Use sendShielded when the coin already exists in the ledger. This requires
QualifiedShieldedCoinInfo, not plain ShieldedCoinInfo, because the committed
coin must include its Merkle tree position.

Use shieldedBurnAddress() when the send target should destroy the shielded
value. Burning is not a separate token primitive in this example; it is a send
to a special recipient.

Keep this decision table nearby while building:

Operation                      Input coin type              Helper
Mint new shielded value        none                         mintShieldedToken
Spend fresh minted value       ShieldedCoinInfo             sendImmediateShielded
Spend later committed value    QualifiedShieldedCoinInfo    sendShielded
Burn fresh minted value        ShieldedCoinInfo             sendImmediateShielded + shieldedBurnAddress
Burn committed value           QualifiedShieldedCoinInfo    sendShielded + shieldedBurnAddress

Enter fullscreen mode Exit fullscreen mode

The tutorial code follows this table exactly. If you change the API shape later,
re-run the tests that check immediate sends and Merkle timing.

Part 1: Create the package

Create a folder for the demo project:

mkdir shielded-token-operations
cd shielded-token-operations

Enter fullscreen mode Exit fullscreen mode

Initialize the package:

npm init -y

Enter fullscreen mode Exit fullscreen mode

Install the test and TypeScript dependencies:

npm install --save-dev typescript vitest @types/node

Enter fullscreen mode Exit fullscreen mode

Update package.json with these scripts:

{
  "scripts": {
    "compact": "compact compile src/shielded-token-lifecycle.compact ./src/managed/shielded-token-lifecycle",
    "test": "vitest run",
    "typecheck": "tsc -p tsconfig.json --noEmit"
  }
}

Enter fullscreen mode Exit fullscreen mode

Create tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "types": ["node", "vitest"]
  },
  "include": ["src/**/*.ts", "test/**/*.ts"]
}

Enter fullscreen mode Exit fullscreen mode

This setup lets you run fast local tests while still keeping a direct Compact
compile command for the smart contract.

Part 2: Write the Compact contract

Create the source folder:

mkdir -p src
touch src/shielded-token-lifecycle.compact

Enter fullscreen mode Exit fullscreen mode

Start the contract with the language pragma, the standard library import, and
two public ledger fields:

pragma language_version >= 0.20;

import CompactStandardLibrary;

export ledger mintedOperations: Counter;
export ledger totalBurned: Uint<128>;

constructor() {
  totalBurned = 0;
}

Enter fullscreen mode Exit fullscreen mode

mintedOperations is a simple counter used by the example. totalBurned
records the amount this contract has intentionally sent to the shielded burn
address.

Next, add a witness for local nonce seed management:

witness localNonceSeed(): Bytes<32>;

Enter fullscreen mode Exit fullscreen mode

The witness value is private state supplied by the TypeScript layer. The
contract will combine it with a public nonce index using evolveNonce.

Part 3: Mint shielded tokens

Add a circuit that mints directly to the current contract:

export circuit mint_to_contract(
  domainSep: Bytes<32>,
  value: Uint<64>,
  nonce: Bytes<32>
): ShieldedCoinInfo {
  assert(disclose(value) > 0, "mint amount must be non-zero");

  const coin = mintShieldedToken(
    disclose(domainSep),
    disclose(value),
    disclose(nonce),
    right<ZswapCoinPublicKey, ContractAddress>(kernel.self())
  );

  mintedOperations.increment(1);
  return coin;
}

Enter fullscreen mode Exit fullscreen mode

mintShieldedToken returns a ShieldedCoinInfo. That is a fresh output. It has
a nonce, color, and value, but it does not have a Merkle index yet.

The recipient is:

right<ZswapCoinPublicKey, ContractAddress>(kernel.self())

Enter fullscreen mode Exit fullscreen mode

That means the new shielded coin belongs to the current contract. This is useful
when the contract should later spend the coin after it appears in the ledger.

Now add a second minting circuit that derives its nonce:

export circuit mint_with_local_nonce(
  domainSep: Bytes<32>,
  value: Uint<64>,
  nonceIndex: Uint<128>
): ShieldedCoinInfo {
  assert(disclose(value) > 0, "mint amount must be non-zero");

  const nonce = disclose(evolveNonce(disclose(nonceIndex), localNonceSeed()));
  const coin = mintShieldedToken(
    disclose(domainSep),
    disclose(value),
    nonce,
    right<ZswapCoinPublicKey, ContractAddress>(kernel.self())
  );

  mintedOperations.increment(1);
  return coin;
}

Enter fullscreen mode Exit fullscreen mode

evolveNonce takes an index and a prior nonce/seed. The disclose(...) wrapper
around the evolved nonce is intentional. Compact treats witness-derived values
as private by default, and a minted coin returns nonce-derived data. This wrapper
declares that the derived nonce value may be used in that public-facing result
without disclosing the raw witness seed.

Part 4: Transfer a committed shielded coin

Add a committed transfer circuit:

export circuit send_committed(
  input: QualifiedShieldedCoinInfo,
  recipient: ZswapCoinPublicKey,
  value: Uint<128>
): ShieldedSendResult {
  assert(disclose(value) > 0, "send amount must be non-zero");
  assert(disclose(input).value >= disclose(value), "send amount exceeds coin value");

  return sendShielded(
    disclose(input),
    left<ZswapCoinPublicKey, ContractAddress>(disclose(recipient)),
    disclose(value)
  );
}

Enter fullscreen mode Exit fullscreen mode

The input type matters. sendShielded spends a QualifiedShieldedCoinInfo.
That type represents an existing shielded coin in the ledger. It includes the
Merkle tree position:

struct QualifiedShieldedCoinInfo {
  nonce: Bytes<32>;
  color: Bytes<32>;
  value: Uint<128>;
  mtIndex: Uint<64>;
}

Enter fullscreen mode Exit fullscreen mode

The result type is ShieldedSendResult:

struct ShieldedSendResult {
  change: Maybe<ShieldedCoinInfo>;
  sent: ShieldedCoinInfo;
}

Enter fullscreen mode Exit fullscreen mode

If you send the full input value, change is empty. If you send less than the
input value, change contains a new contract-owned shielded coin. Your
application must keep track of that change output, wait for it to be committed,
and later spend it as a qualified coin.

Part 5: Burn shielded tokens

Burning is a send to the special burn recipient returned by
shieldedBurnAddress().

Add a committed burn circuit:

export circuit burn_committed(
  input: QualifiedShieldedCoinInfo,
  value: Uint<128>
): ShieldedSendResult {
  assert(disclose(value) > 0, "burn amount must be non-zero");
  assert(disclose(input).value >= disclose(value), "burn amount exceeds coin value");

  const result = sendShielded(
    disclose(input),
    shieldedBurnAddress(),
    disclose(value)
  );

  totalBurned = (totalBurned + disclose(value)) as Uint<128>;
  return result;
}

Enter fullscreen mode Exit fullscreen mode

This works like a committed transfer, except the recipient is the burn address.
The result can still include change. Burning 40 from a 90 value coin burns
40 and returns 50 as change.

Now add a fresh burn circuit:

export circuit burn_fresh(
  domainSep: Bytes<32>,
  mintValue: Uint<64>,
  mintNonce: Bytes<32>,
  burnValue: Uint<128>
): ShieldedSendResult {
  assert(disclose(mintValue) > 0, "mint amount must be non-zero");
  assert(disclose(burnValue) > 0, "burn amount must be non-zero");

  const coin = mintShieldedToken(
    disclose(domainSep),
    disclose(mintValue),
    disclose(mintNonce),
    right<ZswapCoinPublicKey, ContractAddress>(kernel.self())
  );

  assert(coin.value >= disclose(burnValue), "burn amount exceeds minted value");

  const result = sendImmediateShielded(
    coin,
    shieldedBurnAddress(),
    disclose(burnValue)
  );

  mintedOperations.increment(1);
  totalBurned = (totalBurned + disclose(burnValue)) as Uint<128>;
  return result;
}

Enter fullscreen mode Exit fullscreen mode

Use sendImmediateShielded here because the coin was created in the same
transaction. It has not been committed yet, so it cannot be spent with
sendShielded.

Part 6: Add atomic mint and send

Atomic mint-and-send is the same fresh-coin pattern. Add this circuit:

export circuit mint_and_send(
  domainSep: Bytes<32>,
  mintValue: Uint<64>,
  mintNonce: Bytes<32>,
  recipient: ZswapCoinPublicKey,
  sendValue: Uint<128>
): ShieldedSendResult {
  assert(disclose(mintValue) > 0, "mint amount must be non-zero");
  assert(disclose(sendValue) > 0, "send amount must be non-zero");

  const coin = mintShieldedToken(
    disclose(domainSep),
    disclose(mintValue),
    disclose(mintNonce),
    right<ZswapCoinPublicKey, ContractAddress>(kernel.self())
  );

  assert(coin.value >= disclose(sendValue), "send amount exceeds minted value");

  const result = sendImmediateShielded(
    coin,
    left<ZswapCoinPublicKey, ContractAddress>(disclose(recipient)),
    disclose(sendValue)
  );

  mintedOperations.increment(1);
  return result;
}

Enter fullscreen mode Exit fullscreen mode

This circuit mints to the contract and immediately sends part or all of the
fresh coin to a user public key. If mintValue is larger than sendValue, the
returned change belongs to the contract.

Part 7: Add the TypeScript witness

Create the witness file:

touch src/witnesses.ts

Enter fullscreen mode Exit fullscreen mode

Add a private state type and a witness implementation:

import { createHash } from "node:crypto";

export type ShieldedTokenPrivateState = {
  readonly nonceSeed: Uint8Array;
  readonly nextNonceIndex: bigint;
};

type WitnessContext<PrivateState> = {
  readonly privateState: PrivateState;
};

export const createShieldedTokenPrivateState = (
  nonceSeed = hashBytes32("shielded-token:demo-seed"),
): ShieldedTokenPrivateState => ({
  nonceSeed,
  nextNonceIndex: 0n,
});

export const witnesses = {
  localNonceSeed: ({ privateState }: WitnessContext<ShieldedTokenPrivateState>) => [
    {
      nonceSeed: privateState.nonceSeed,
      nextNonceIndex: privateState.nextNonceIndex + 1n,
    },
    privateState.nonceSeed,
  ],
};

export function hashBytes32(input: string): Uint8Array {
  return createHash("sha256").update(input).digest();
}

Enter fullscreen mode Exit fullscreen mode

The witness returns the nonce seed to Compact and advances local private state.
In a production DApp, persist this private state. Do not reset it casually, or
you may reuse nonce material.

Part 8: Build a local test model

Create a deterministic model for tests:

mkdir -p src/model
touch src/model/shielded-token-model.ts

Enter fullscreen mode Exit fullscreen mode

The model should mirror the standard library types closely:

export type ShieldedCoinInfo = {
  readonly nonce: string;
  readonly color: string;
  readonly value: bigint;
  readonly recipient: Recipient;
};

export type QualifiedShieldedCoinInfo = ShieldedCoinInfo & {
  readonly mtIndex: bigint;
};

export type ShieldedSendResult = {
  readonly sent: ShieldedCoinInfo;
  readonly change: ShieldedCoinInfo | null;
};

Enter fullscreen mode Exit fullscreen mode

Then model the two send paths:

export function sendShielded(
  input: QualifiedShieldedCoinInfo,
  recipient: Recipient,
  value: bigint | number,
): ShieldedSendResult {
  assertQualified(input);
  return splitCoin(input, recipient, value, "sendShielded");
}

export function sendImmediateShielded(
  input: ShieldedCoinInfo,
  target: Recipient,
  value: bigint | number,
): ShieldedSendResult {
  return splitCoin(input, target, value, "sendImmediateShielded");
}

Enter fullscreen mode Exit fullscreen mode

The important test helper is assertQualified. It rejects fresh coins that do
not have an mtIndex:

function assertQualified(
  input: ShieldedCoinInfo | QualifiedShieldedCoinInfo,
): asserts input is QualifiedShieldedCoinInfo {
  if (!("mtIndex" in input)) {
    throw new Error("sendShielded requires a committed coin with an mtIndex");
  }
}

Enter fullscreen mode Exit fullscreen mode

This is how the test suite makes the Merkle timing rule visible without running
a full network.

Part 9: Write the Vitest suite

Create the test file:

mkdir -p test
touch test/shielded-token-lifecycle.test.ts

Enter fullscreen mode Exit fullscreen mode

Start with a mint test:

it("mints a shielded coin to the contract with the expected color", () => {
  const harness = new ShieldedTokenHarness(DOMAIN);

  const coin = harness.mintToContract(1000n, NONCE);

  expect(coin.value).toBe(1000n);
  expect(coin.color).toEqual(tokenType(DOMAIN));
  expect(coin.recipient).toEqual(CONTRACT_SELF);
});

Enter fullscreen mode Exit fullscreen mode

Add a nonce test:

it("derives deterministic unique nonces with evolveNonce", () => {
  const first = evolveNonce(0n, SEED);
  const second = evolveNonce(1n, SEED);

  expect(first).not.toEqual(second);
  expect(evolveNonce(0n, SEED)).toEqual(first);
});

Enter fullscreen mode Exit fullscreen mode

Add the Merkle timing test:

it("requires a committed Merkle position before sendShielded can spend a coin", () => {
  const harness = new ShieldedTokenHarness(DOMAIN);
  const fresh = harness.mintToContract(25n, NONCE);

  expect(() =>
    sendShielded(fresh as unknown as QualifiedShieldedCoinInfo, ALICE, 5n),
  ).toThrow(/mtIndex/);
});

Enter fullscreen mode Exit fullscreen mode

Add committed transfer tests for partial and exact sends:

const qualified = harness.commit(harness.mintToContract(100n, NONCE));
const result = harness.sendCommitted(qualified, ALICE, 35n);

expect(result.sent.value).toBe(35n);
expect(result.change?.value).toBe(65n);

Enter fullscreen mode Exit fullscreen mode

Add burn tests for both paths:

const committed = harness.commit(harness.mintToContract(90n, NONCE));
const committedBurn = harness.burnCommitted(committed, 40n);
expect(committedBurn.sent.recipient).toEqual(BURN_ADDRESS);
expect(committedBurn.change?.value).toBe(50n);

const freshBurn = harness.burnFresh(75n, NONCE, 75n);
expect(freshBurn.sent.recipient).toEqual(BURN_ADDRESS);
expect(freshBurn.change).toBeNull();

Enter fullscreen mode Exit fullscreen mode

Finally, test atomic mint-and-send:

const result = harness.mintAndSend(120n, NONCE, ALICE, 45n);

expect(result.sent.value).toBe(45n);
expect(result.sent.recipient).toEqual(ALICE);
expect(result.change?.value).toBe(75n);

Enter fullscreen mode Exit fullscreen mode

The completed suite should cover normal flows and edge cases: over-spend,
over-burn, zero-value operations, change reuse, fresh immediate sends, and color
preservation.

Add one more test for change reuse. This test proves that change from a partial
send is not just bookkeeping text; it is the next coin your application must
commit and track:

it("allows change from one transaction to be committed and spent later", () => {
  const harness = new ShieldedTokenHarness(DOMAIN);
  const qualified = harness.commit(harness.mintToContract(100n, NONCE));
  const first = harness.sendCommitted(qualified, ALICE, 30n);

  const change = harness.commit(first.change!);
  const second = harness.sendCommitted(change, BOB, 20n);

  expect(second.sent.value).toBe(20n);
  expect(second.change?.value).toBe(50n);
});

Enter fullscreen mode Exit fullscreen mode

This is the test that catches a common wallet integration bug. If the application
forgets to store first.change, the user may believe the remaining value is
still available, but the app will not know which output to qualify and spend
later.

Part 10: Run the checks

Run TypeScript:

npm run typecheck

Enter fullscreen mode Exit fullscreen mode

Expected output:

tsc -p tsconfig.json --noEmit

Enter fullscreen mode Exit fullscreen mode

Run the tests:

npm test

Enter fullscreen mode Exit fullscreen mode

Expected output:

Test Files  1 passed (1)
Tests  18 passed (18)

Enter fullscreen mode Exit fullscreen mode

Compile the Compact smart contract:

npm run compact

Enter fullscreen mode Exit fullscreen mode

Expected result:

Compilation successful

Enter fullscreen mode Exit fullscreen mode

The compile step creates generated contract files under src/managed. Those
generated files are build output and do not need to be committed.

For final review, record the three verification results together:

Compact compiler: passed
TypeScript: passed
Vitest: 18 tests passed

Enter fullscreen mode Exit fullscreen mode

Do not treat the Vitest result as a substitute for Compact compilation. The
tests prove the lifecycle model and edge cases. The compiler proves the Compact
syntax, types, witness disclosure, and standard library calls.

Troubleshooting

compact runs the wrong command on Windows

Problem:

Listing ... New files added to this directory will not be compressed.

Enter fullscreen mode Exit fullscreen mode

That is the Windows filesystem compression utility, not the Midnight compiler.

Fix:

  • Run the command from WSL, or
  • Put the Midnight Compact toolchain earlier in PATH than C:\Windows\System32.

The compiler complains about witness disclosure

Problem:

potential witness-value disclosure must be declared but is not

Enter fullscreen mode Exit fullscreen mode

Fix:

Wrap the evolved nonce in disclose(...):

const nonce = disclose(evolveNonce(disclose(nonceIndex), localNonceSeed()));

Enter fullscreen mode Exit fullscreen mode

This declares the intentional disclosure of the derived nonce value, not the raw
witness seed.

A later transfer fails because the coin has no mtIndex

Problem:

You are trying to call sendShielded with a fresh ShieldedCoinInfo.

Fix:

Use sendImmediateShielded if the coin was created in the same transaction. If
the coin was created in a previous transaction, wait until it is committed and
spend it as a QualifiedShieldedCoinInfo with the real Merkle index.

Change disappears from your app state

Problem:

A partial send or burn returns change, but the app does not store it.

Fix:

Always inspect ShieldedSendResult.change. If it is present, persist it and
track its later Merkle position. That change is the remaining balance.

Conclusion

You have built a complete shielded token lifecycle example:

  • mintShieldedToken creates fresh shielded coins.
  • evolveNonce gives you deterministic nonce derivation.
  • sendShielded spends committed coins with mtIndex.
  • sendImmediateShielded spends coins created in the same transaction.
  • shieldedBurnAddress turns a shielded send into a burn.
  • ShieldedSendResult.change carries the unspent remainder.
  • Vitest tests prove the normal paths and the common failure cases.

The most important lesson is the fresh-versus-committed distinction. If you keep
that boundary clear, shielded mint, transfer, and burn operations become much
easier to reason about.

Related resources: