<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>Midnight Docs Blog</title>
        <link>https://docs.midnight.network/blog</link>
        <description>Midnight Docs Blog</description>
        <lastBuildDate>Thu, 05 Mar 2026 00:00:00 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <item>
            <title><![CDATA[Get your project on the map]]></title>
            <link>https://docs.midnight.network/blog/get-your-project-on-the-map</link>
            <guid>https://docs.midnight.network/blog/get-your-project-on-the-map</guid>
            <pubDate>Thu, 05 Mar 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[Three quick updates to make sure your work is visible, counted, and recognized as part of the Midnight ecosystem.]]></description>
            <content:encoded><![CDATA[<p><em>Three quick updates to make sure your work is visible, counted, and recognized as part of the Midnight ecosystem.</em></p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="why-this-matters">Why this matters<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#why-this-matters" class="hash-link" aria-label="Direct link to Why this matters" title="Direct link to Why this matters">​</a></h2>
<p>Midnight is approaching mainnet. The ecosystem already has hundreds of active developers building with Compact, deploying on Preprod, and shipping real applications. But <strong>activity that isn't attributed doesn't get counted</strong>, and that invisibility has real consequences for the ecosystem you're helping to build.</p>
<p>Establishing a clear public record of development activity ensures that the broader blockchain industry recognizes the growth of Midnight Network. Accurate attribution is a functional necessity for the ecosystem and establishes a standardized framework for documenting technical contributions.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="the-attribution-process">The attribution process<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#the-attribution-process" class="hash-link" aria-label="Direct link to The attribution process" title="Direct link to The attribution process">​</a></h2>
<p><a href="https://www.developerreport.com/" target="_blank" rel="noopener noreferrer"><strong>Electric Capital</strong></a> produces the most widely cited developer report in blockchain. Investors, protocols, and media use it to understand which ecosystems are growing, which are stagnant, and where to pay attention. Their methodology is public: they track GitHub commits, contributor counts, and repository metadata across every major blockchain ecosystem. If your repo doesn't have the right metadata, their tooling won't associate it with Midnight, regardless of how active or high-quality the work is.</p>
<p>Midnight is submitting its ecosystem map to Electric Capital for the first time, ahead of mainnet. This is a <strong>one-time window</strong> to establish a credible public record of where the ecosystem stands from day one. Developer counts from the weeks surrounding a mainnet launch are captured in a rolling window that informs how new ecosystems are ranked and covered. Ecosystems that show up in that window with strong numbers get recognized. Ecosystems that don't, start from a deficit they spend months correcting.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="compounding-developer-results">Compounding developer results<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#compounding-developer-results" class="hash-link" aria-label="Direct link to Compounding developer results" title="Direct link to Compounding developer results">​</a></h2>
<p>The changes below take under five minutes per repository. They are small, permanent, and consequential. Beyond writing code, implementing these metadata standards is a high-imact action for ecosystem growth.  Every repo that completes them is a developer who gets credited for building on Midnight.</p>
<table><thead><tr><th style="text-align:left">TL;DR: Midnight's developer ecosystem is real and growing. These three steps make sure that growth is visible to the tools and reports that the wider industry pays attention to.</th></tr></thead></table>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="step-1-add-github-topics">Step 1: Add GitHub topics<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#step-1-add-github-topics" class="hash-link" aria-label="Direct link to Step 1: Add GitHub topics" title="Direct link to Step 1: Add GitHub topics">​</a></h2>
<p>GitHub topics are how Electric Capital (and other ecosystem trackers) programmatically identify which repos belong to which ecosystem. Without them, even actively maintained repos are invisible to the tooling.</p>
<p>To add topics to your repository:</p>
<ol>
<li>
<p>Go to your repository on GitHub</p>
</li>
<li>
<p>In the right-hand sidebar, find the About section</p>
</li>
<li>
<p>Click the ⚙️ gear icon next to it</p>
</li>
<li>
<p>In the Topics field, add the relevant topics from the table below</p>
</li>
<li>
<p>Click Save changes</p>
</li>
</ol>
<table><thead><tr><th style="text-align:left">Topic</th><th style="text-align:left">When to use</th></tr></thead><tbody><tr><td style="text-align:left"><strong><code>midnightntwrk</code></strong></td><td style="text-align:left">Required for all Midnight ecosystem projects</td></tr><tr><td style="text-align:left"><strong><code>compact</code></strong></td><td style="text-align:left">Add only if your project uses the Compact language</td></tr></tbody></table>
<div class="theme-admonition theme-admonition-warning admonition_xJq3 alert alert--warning"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>warning</div><div class="admonitionContent_BuS1"><p>Do not use: <code>midnight</code>, <code>midnight-network</code>, <code>midnight-compact</code>, or <code>midnight-ecosystem</code>. These variants are not tracked and will not associate your project with the ecosystem.</p></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="step-2-add-one-attribution-sentence-to-your-readme">Step 2: Add one attribution sentence to your README<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#step-2-add-one-attribution-sentence-to-your-readme" class="hash-link" aria-label="Direct link to Step 2: Add one attribution sentence to your README" title="Direct link to Step 2: Add one attribution sentence to your README">​</a></h2>
<p>Electric Capital's tooling scans README files for signals that confirm a repo's relationship to an ecosystem. One sentence, placed near the top of your README, is sufficient. Please use <strong>exact wording</strong> from the table below. Customising the phrasing means the automated system may not recognize it.</p>
<table><thead><tr><th style="text-align:left">Your project type</th><th style="text-align:left">Attribution sentence</th></tr></thead><tbody><tr><td style="text-align:left">dApps, contracts, or tooling that run directly on Midnight</td><td style="text-align:left"><em>"This project is built on the Midnight Network."</em></td></tr><tr><td style="text-align:left">SDKs, infrastructure, wallets, or services</td><td style="text-align:left"><em>"This project integrates with the Midnight Network."</em></td></tr><tr><td style="text-align:left">Developer tooling, frameworks, or libraries</td><td style="text-align:left"><em>"This project extends the Midnight Network with additional developer tooling."</em></td></tr></tbody></table>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="step-3-open-a-pr-to-the-awesome-dapps-list">Step 3: Open a PR to the Awesome dApps list<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#step-3-open-a-pr-to-the-awesome-dapps-list" class="hash-link" aria-label="Direct link to Step 3: Open a PR to the Awesome dApps list" title="Direct link to Step 3: Open a PR to the Awesome dApps list">​</a></h2>
<p>The Midnight <a href="http://github.com/midnightntwrk/midnight-awesome-dapps" target="_blank" rel="noopener noreferrer">Awesome dApps list</a> is the community-maintained directory of ecosystem projects. Opening a PR adds your project to that record and makes it discoverable by other developers, partners, and the teams evaluating the ecosystem.</p>
<p>Submit your project at: <a href="https://github.com/midnightntwrk/midnight-awesome-dapps" target="_blank" rel="noopener noreferrer">github.com/midnightntwrk/midnight-awesome-dapps</a>.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="earn-zealy-points-while-youre-at-it">Earn Zealy points while you're at it!<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#earn-zealy-points-while-youre-at-it" class="hash-link" aria-label="Direct link to Earn Zealy points while you're at it!" title="Direct link to Earn Zealy points while you're at it!">​</a></h2>
<p>Each of the three steps above has a corresponding quest on Zealy. Complete them to earn points and appear on the Midnight ecosystem leaderboard. Links to the three quests are below.</p>
<ul>
<li>
<p><strong>Quest 1:</strong> <a href="https://zealy.io/cw/midnightnetwork/questboard/admin/2b5850a3-c343-44c1-9478-71aae189837c" target="_blank" rel="noopener noreferrer">Add the midnightntwrk GitHub topic</a></p>
</li>
<li>
<p><strong>Quest 2:</strong> <a href="https://zealy.io/cw/midnightnetwork/questboard/admin/1c837b8e-b266-419e-a282-a0ea1eee884e" target="_blank" rel="noopener noreferrer">Add the attribution sentence to your README</a></p>
</li>
<li>
<p><strong>Quest 3:</strong> <a href="https://zealy.io/cw/midnightnetwork/questboard/admin/63ed7bac-6c9f-4387-8bb3-36d861d3ec97?tab=properties" target="_blank" rel="noopener noreferrer">Open a PR to the Midnight Awesome dApps list</a></p>
</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="three-steps-once-per-repo-permanent-record">Three steps, once per repo, permanent record<a href="https://docs.midnight.network/blog/get-your-project-on-the-map#three-steps-once-per-repo-permanent-record" class="hash-link" aria-label="Direct link to Three steps, once per repo, permanent record" title="Direct link to Three steps, once per repo, permanent record">​</a></h2>
<p>These changes don't affect how your code works. They affect how your work is seen. Midnight's first public developer count will be used in every industry report, investor briefing, and ecosystem comparison that references the network for years to come. Your repo being in that count matters.</p>
<p><em>Questions? Find us in the Midnight Discord in <strong>#dev-chat</strong>, or reply to this post.</em></p>]]></content:encoded>
            <category>ecosystem</category>
            <category>midnight</category>
            <category>community</category>
            <category>devrel</category>
        </item>
        <item>
            <title><![CDATA[Testnet-02 Transition & The Roadmap to Mōhalu]]></title>
            <link>https://docs.midnight.network/blog/testnet-02-transition</link>
            <guid>https://docs.midnight.network/blog/testnet-02-transition</guid>
            <pubDate>Tue, 27 Jan 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[As we progress through the Hilo phase and prepare for the upcoming milestones of the Midnight Network, we are adjusting testing environments to ensure a seamless transition to a decentralized mainnet.]]></description>
            <content:encoded><![CDATA[<p>As we progress through the <strong>Hilo</strong> phase and prepare for the upcoming milestones of the Midnight Network, we are adjusting testing environments to ensure a seamless transition to a decentralized mainnet.</p>
<p>Midnight <code>Testnet-02</code> has been a monumental success, serving as the crucible for Midnight’s zero-knowledge capabilities. We want to extend our deepest gratitude to the over <strong>180+</strong> Cardano Stake Pool Operators (SPOs) who actively participated in the <code>Testnet-02</code> environment and their ongoing support for the Midnight project.</p>
<p>In previous communications, we signaled that SPOs would migrate directly to the Midnight <code>Preview</code> environment at this stage. However, as we refine the roadmap toward the <strong>Kūkolu</strong> (Federated Mainnet) phase, we have made a strategic pivot.</p>
<p>While <code>Testnet-02</code> validators may continue to run their nodes until <strong>February 28, 2026</strong>, the Midnight <code>Preview</code> environment will be temporarily maintained by core engineering. This shift allows us to:</p>
<ul>
<li>Perform high velocity testing and rapid iterations.</li>
<li>Finalize the integration of novel features such as the DUST Capacity Exchange.</li>
<li>Ensure a stable, rapid-test environment for core development that would otherwise cause unnecessary disruption and downtime for independent SPOs.</li>
</ul>
<table><thead><tr><th>Phase</th><th>Milestone</th><th>SPO &amp; Validator Role</th></tr></thead><tbody><tr><td>Hilo</td><td>Token Genesis</td><td>Initial liquidity and NIGHT "Glacier Drop" on Cardano.</td></tr><tr><td>Kūkolu</td><td>Federated Mainnet</td><td>Ecosystem partners maintain stability during rapid development.</td></tr><tr><td>Mōhalu</td><td>Incentivized Mainnet</td><td>SPO Onboarding begins including other testing.</td></tr><tr><td>Hua</td><td>Full Decentralization</td><td>Block production transitions to community.</td></tr></tbody></table>
<p>This keeps the structure clean and focused on phases, milestones, and roles.</p>
<p><em>Please note: All dates are estimates.</em></p>
<p>We acknowledge that this change differs from previous expectations, but we believe this path ensures the highest quality environment for SPOs to return for <strong>Mōhalu</strong>.</p>
<div class="theme-admonition theme-admonition-important admonition_xJq3 alert alert--info"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>important</div><div class="admonitionContent_BuS1"><h2 class="anchor anchorWithStickyNavbar_LWe7" id="validator-documentation-status">Validator documentation status<a href="https://docs.midnight.network/blog/testnet-02-transition#validator-documentation-status" class="hash-link" aria-label="Direct link to Validator documentation status" title="Direct link to Validator documentation status">​</a></h2><p>To avoid confusion during this transition, <strong>current validator documentation will be temporarily unpublished</strong> or hidden from the main site. This ensures that <strong>when you return to the docs for the Mōhalu phase, they will be 100% accurate</strong>, refreshed, and aligned with the new architecture.</p></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="direct-action-on-your-feedback">Direct action on your feedback<a href="https://docs.midnight.network/blog/testnet-02-transition#direct-action-on-your-feedback" class="hash-link" aria-label="Direct link to Direct action on your feedback" title="Direct link to Direct action on your feedback">​</a></h2>
<p>We recently conducted a feedback survey to understand your operational experience. We heard your feedback regarding documentation, setup friction, and infrastructure control. In preparation for the <strong>Mōhalu</strong> phase, we are implementing the following changes:</p>
<ul>
<li>We are moving away from a Docker-only distribution. We are now delivering pre-compiled binaries for <code>midnight-node</code> and restructuring our documentation to be binary and source-first. While containerization is useful, we recognize that barebones instructions are fundamental for professional SPO infrastructure.</li>
<li>The next node version will completely remove the registration wizard, allowing for a more direct and scriptable registration process.</li>
<li>We are prioritizing new architecture recommendation for secrets management and air-gapped signing guidance. We recognize that for a privacy-focused chain, the security of our validators' keys is paramount.</li>
<li>In the lead up to Mōhalu, we promise more structued communication cadence and dedicated SPO/ validator calls.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="staying-informed-subscribe-to-midnight-validator-updates">Staying informed: Subscribe to Midnight Validator Updates<a href="https://docs.midnight.network/blog/testnet-02-transition#staying-informed-subscribe-to-midnight-validator-updates" class="hash-link" aria-label="Direct link to Staying informed: Subscribe to Midnight Validator Updates" title="Direct link to Staying informed: Subscribe to Midnight Validator Updates">​</a></h2>
<p>👉 To ensure you receive the latest technical specifications, onboarding guides, and timeline adjustments for the <strong>Mōhalu</strong> phase, we encourage all SPOs to subscribe via the Midnight Validator Digest: <a href="https://mpc.midnight.network/midnight-validator-digest" target="_blank" rel="noopener noreferrer">https://mpc.midnight.network/midnight-validator-digest</a></p>
<p>👉 Join other validators and builders in the Midnight Discord server: <a href="https://discord.com/invite/midnightnetwork" target="_blank" rel="noopener noreferrer">https://discord.com/invite/midnightnetwork</a></p>]]></content:encoded>
            <category>testnet</category>
            <category>spos</category>
        </item>
        <item>
            <title><![CDATA[Midnight MCP - AI-assisted development for Compact smart contracts]]></title>
            <link>https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development</link>
            <guid>https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development</guid>
            <pubDate>Wed, 21 Jan 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[AI coding assistants like Claude, GitHub Copilot, and Cursor have transformed how developers write code. But they have a fundamental limitation: they only know what was in their training data.]]></description>
            <content:encoded><![CDATA[<p>AI coding assistants like Claude, GitHub Copilot, and Cursor have transformed how developers write code. But they have a fundamental limitation: they only know what was in their training data.</p>
<p>Compact, Midnight's smart contract language, isn't in that training data. When you ask an AI assistant to write a Compact contract, it hallucinates. It invents syntax that doesn't exist, references functions that were never defined, and produces code that fails at compile time.</p>
<p>Midnight MCP solves this problem.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-is-mcp">What is MCP?<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#what-is-mcp" class="hash-link" aria-label="Direct link to What is MCP?" title="Direct link to What is MCP?">​</a></h2>
<p>The <em>Model Context Protocol</em> (MCP) is an open standard that allows AI assistants to access external tools and data sources. Instead of relying solely on training data, an AI assistant with MCP can query live documentation, search codebases, and call APIs.</p>
<p>Midnight MCP is an MCP server purpose-built for Midnight development. It gives AI assistants:</p>
<ul>
<li><strong>Indexed knowledge</strong> of 102 Midnight repositories</li>
<li><strong>Real compiler validation</strong> before showing you code</li>
<li><strong>Semantic search</strong> across documentation and examples</li>
<li><strong>Version-aware syntax references</strong> for Compact</li>
</ul>
<p>When you ask Claude to write a Compact contract, it queries Midnight MCP for the correct syntax, generates the code, validates it against the real compiler, and only shows you working code.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="the-problem-with-ai-generated-compact-code">The problem with AI-generated Compact code<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#the-problem-with-ai-generated-compact-code" class="hash-link" aria-label="Direct link to The problem with AI-generated Compact code" title="Direct link to The problem with AI-generated Compact code">​</a></h2>
<p>Consider this prompt:</p>
<p>"Write a simple counter contract in Compact"</p>
<p>Without Midnight MCP, an AI assistant might generate:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">contract Counter {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    state count: Int = 0;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    function increment(): Void {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        count = count + 1;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>This looks plausible. It's also completely wrong:</p>
<ul>
<li>Compact uses <code>ledger</code> for state, not <code>state</code>.</li>
<li>There is no <code>Int</code> type in Compact. It uses <code>Uint&lt;32&gt;</code>, <code>Field</code>, and other specific types.</li>
<li><code>Void</code> doesn't exist. Compact uses <code>[]</code> for the unit type.</li>
<li>State mutations require witness functions, not direct assignment.</li>
</ul>
<p>The AI hallucinated a language that resembles Solidity but isn't Compact. A developer unfamiliar with Compact might spend hours debugging code that was never valid.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="install-midnight-mcp">Install Midnight MCP<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#install-midnight-mcp" class="hash-link" aria-label="Direct link to Install Midnight MCP" title="Direct link to Install Midnight MCP">​</a></h2>
<p>Midnight MCP requires no API keys and installs in under 60 seconds. Add the appropriate configuration to your AI assistant based on the tool you use.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="claude-desktop">Claude Desktop<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#claude-desktop" class="hash-link" aria-label="Direct link to Claude Desktop" title="Direct link to Claude Desktop">​</a></h3>
<p>On macOS, edit <code>~/Library/Application Support/Claude/claude_desktop_config.json</code>. Add the following configuration to enable Midnight MCP:</p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token property">"mcpServers"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token property">"midnight"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token property">"command"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"npx"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token property">"args"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"-y"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"midnight-mcp@latest"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="cursor">Cursor<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#cursor" class="hash-link" aria-label="Direct link to Cursor" title="Direct link to Cursor">​</a></h3>
<p>Create or edit <code>.cursor/mcp.json</code> in your project root. You can also configure it globally at <code>~/.cursor/mcp.json</code> on macOS/Linux or <code>%USERPROFILE%\.cursor\mcp.json</code> on Windows. Add the following configuration:</p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token property">"mcpServers"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token property">"midnight"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token property">"command"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"npx"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token property">"args"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"-y"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"midnight-mcp@latest"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="vs-code-with-github-copilot">VS Code with GitHub Copilot<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#vs-code-with-github-copilot" class="hash-link" aria-label="Direct link to VS Code with GitHub Copilot" title="Direct link to VS Code with GitHub Copilot">​</a></h3>
<p>Create or edit <code>.vscode/mcp.json</code> in your project. Add the following configuration to connect Copilot to Midnight MCP:</p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token property">"servers"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token property">"midnight"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token property">"command"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"npx"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token property">"args"</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"-y"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"midnight-mcp@latest"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>After adding the configuration, restart your AI assistant. You now have access to <a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#29-tools-for-midnight-development">29 Midnight-specific tools</a>.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="how-midnight-mcp-works">How Midnight MCP works<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#how-midnight-mcp-works" class="hash-link" aria-label="Direct link to How Midnight MCP works" title="Direct link to How Midnight MCP works">​</a></h2>
<p>Midnight MCP operates as a local server that connects your AI assistant to Midnight's ecosystem.</p>
<p><img decoding="async" loading="lazy" alt="Midnight MCP Architecture" src="https://docs.midnight.network/assets/images/mcp-flow-6300d117555db9ec395e0f419d22de5d.png" width="3323" height="1570" class="img_ev3q"></p>
<p>When you ask for a Compact contract:</p>
<ol>
<li>The AI assistant calls <code>midnight-get-latest-syntax</code> to retrieve current Compact syntax.</li>
<li>It generates code using the correct patterns.</li>
<li>It calls <code>midnight-compile-contract</code> to validate against the real compiler.</li>
<li>If compilation fails, it reads the error, fixes the code, and retries.</li>
<li>You receive verified, working code.</li>
</ol>
<p>This compile-validate-fix loop happens automatically. You never see the broken intermediate versions.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="real-compiler-integration">Real compiler integration<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#real-compiler-integration" class="hash-link" aria-label="Direct link to Real compiler integration" title="Direct link to Real compiler integration">​</a></h2>
<p>Most code-generation tools rely on pattern matching and hope. Midnight MCP validates code against the actual Compact compiler hosted.</p>
<p>The compiler catches errors that static analysis cannot, including:</p>
<ul>
<li><strong>Type mismatches</strong>: Using <code>Field</code> where <code>Uint&lt;64&gt;</code> is expected</li>
<li><strong>Sealed field violations</strong>: Attempting to access sealed state incorrectly</li>
<li><strong>Disclose rule errors</strong>: Missing or malformed privacy annotations</li>
<li><strong>Unbound identifiers</strong>: References to undefined variables or types</li>
</ul>
<p>When the compiler returns an error, the response includes the exact line number and column:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">success: false</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">message: "Line 12:8 - unbound identifier 'totalSupply'"</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">errorDetails:</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  line: 12</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  column: 8</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  errorType: error</span><br></span></code></pre></div></div>
<p>The AI assistant uses this information to fix the code and try again.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="graceful-fallback">Graceful fallback<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#graceful-fallback" class="hash-link" aria-label="Direct link to Graceful fallback" title="Direct link to Graceful fallback">​</a></h3>
<p>If the hosted compiler is unavailable, Midnight MCP falls back to static analysis. The response indicates which validation method was used:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">validationType: "compiler"           # Real compiler validation</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">validationType: "static-analysis-fallback"  # Compiler unavailable</span><br></span></code></pre></div></div>
<p>You always receive validation. The tool never fails silently.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="semantic-search-across-102-repositories">Semantic search across 102 repositories<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#semantic-search-across-102-repositories" class="hash-link" aria-label="Direct link to Semantic search across 102 repositories" title="Direct link to Semantic search across 102 repositories">​</a></h2>
<p>Today, Midnight MCP indexes every non-archived repository in the Midnight ecosystem:</p>
<ul>
<li>All 88 repositories from <code>midnightntwrk</code>.</li>
<li>14 community and partner repositories, including OpenZeppelin contracts and hackathon winners.</li>
</ul>
<p>The search is semantic, not keyword-based. For example, a prompt like "find code that handles shielded transactions" returns relevant results even if those exact words don't appear in the code.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">Prompt: "How do I implement a token with transfer limits?"</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">midnight-search-compact returns:</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">- Token contract examples from midnight-examples</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">- Rate limiting patterns from community repos</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">- Relevant documentation sections</span><br></span></code></pre></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="29-tools-for-midnight-development">29 tools for Midnight development<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#29-tools-for-midnight-development" class="hash-link" aria-label="Direct link to 29 tools for Midnight development" title="Direct link to 29 tools for Midnight development">​</a></h2>
<p>Midnight MCP provides 29 tools organized by function.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="search-tools">Search tools<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#search-tools" class="hash-link" aria-label="Direct link to Search tools" title="Direct link to Search tools">​</a></h3>
<p>Use these tools to find code, documentation, and examples across the Midnight ecosystem.</p>
<table><thead><tr><th style="text-align:left">Tool</th><th style="text-align:left">Purpose</th></tr></thead><tbody><tr><td style="text-align:left"><code>midnight-search-compact</code></td><td style="text-align:left">Search Compact language code across indexed repos</td></tr><tr><td style="text-align:left"><code>midnight-search-docs</code></td><td style="text-align:left">Search official Midnight documentation</td></tr><tr><td style="text-align:left"><code>midnight-search-typescript</code></td><td style="text-align:left">Search TypeScript SDK implementations</td></tr><tr><td style="text-align:left"><code>midnight-fetch-docs</code></td><td style="text-align:left">Fetch live documentation from docs.midnight.network</td></tr></tbody></table>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="analysis-tools">Analysis tools<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#analysis-tools" class="hash-link" aria-label="Direct link to Analysis tools" title="Direct link to Analysis tools">​</a></h3>
<p>Use these tools to validate, analyze, and review Compact contracts.</p>
<table><thead><tr><th style="text-align:left">Tool</th><th style="text-align:left">Purpose</th></tr></thead><tbody><tr><td style="text-align:left"><code>midnight-compile-contract</code></td><td style="text-align:left">Validate code against the real Compact compiler</td></tr><tr><td style="text-align:left"><code>midnight-analyze-contract</code></td><td style="text-align:left">Run 15 static security checks</td></tr><tr><td style="text-align:left"><code>midnight-review-contract</code></td><td style="text-align:left">AI-powered security review</td></tr><tr><td style="text-align:left"><code>midnight-extract-contract-structure</code></td><td style="text-align:left">Parse contract structure and exports</td></tr></tbody></table>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="generation-tools">Generation tools<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#generation-tools" class="hash-link" aria-label="Direct link to Generation tools" title="Direct link to Generation tools">​</a></h3>
<p>Use these tools to create new contracts and documentation.</p>
<table><thead><tr><th style="text-align:left">Tool</th><th style="text-align:left">Purpose</th></tr></thead><tbody><tr><td style="text-align:left"><code>midnight-generate-contract</code></td><td style="text-align:left">Generate contracts from natural language descriptions</td></tr><tr><td style="text-align:left"><code>midnight-document-contract</code></td><td style="text-align:left">Generate documentation in Markdown or JSDoc format</td></tr></tbody></table>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="repository-tools">Repository tools<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#repository-tools" class="hash-link" aria-label="Direct link to Repository tools" title="Direct link to Repository tools">​</a></h3>
<p>Use these tools to access files and syntax references from Midnight repositories.</p>
<table><thead><tr><th style="text-align:left">Tool</th><th style="text-align:left">Purpose</th></tr></thead><tbody><tr><td style="text-align:left"><code>midnight-get-file</code></td><td style="text-align:left">Retrieve files from any indexed Midnight repository</td></tr><tr><td style="text-align:left"><code>midnight-get-file-at-version</code></td><td style="text-align:left">Get file content at a specific version</td></tr><tr><td style="text-align:left"><code>midnight-compare-syntax</code></td><td style="text-align:left">Compare syntax between Compact versions</td></tr><tr><td style="text-align:left"><code>midnight-get-latest-syntax</code></td><td style="text-align:left">Current Compact syntax reference</td></tr><tr><td style="text-align:left"><code>midnight-get-repo-context</code></td><td style="text-align:left">Everything needed to start coding (compound tool)</td></tr><tr><td style="text-align:left"><code>midnight-list-examples</code></td><td style="text-align:left">List available example contracts</td></tr></tbody></table>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="version-management-tools">Version management tools<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#version-management-tools" class="hash-link" aria-label="Direct link to Version management tools" title="Direct link to Version management tools">​</a></h3>
<p>Use these tools to manage upgrades and track changes between Compact versions.</p>
<table><thead><tr><th style="text-align:left">Tool</th><th style="text-align:left">Purpose</th></tr></thead><tbody><tr><td style="text-align:left"><code>midnight-upgrade-check</code></td><td style="text-align:left">Full upgrade analysis (compound tool)</td></tr><tr><td style="text-align:left"><code>midnight-check-breaking-changes</code></td><td style="text-align:left">Identify breaking changes between versions</td></tr><tr><td style="text-align:left"><code>midnight-get-migration-guide</code></td><td style="text-align:left">Step-by-step migration instructions</td></tr></tbody></table>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="resources-and-prompts">Resources and prompts<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#resources-and-prompts" class="hash-link" aria-label="Direct link to Resources and prompts" title="Direct link to Resources and prompts">​</a></h2>
<p>Beyond tools, Midnight MCP provides nine built-in resources and five interactive prompts.</p>
<p><em>Resources</em> are always-available references that provide quick access to syntax and examples:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">midnight://syntax/latest      Current Compact syntax</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">midnight://examples/counter   Counter contract example</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">midnight://examples/token     Token contract example</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">midnight://docs/compact       Compact language reference</span><br></span></code></pre></div></div>
<p><em>Prompts</em> are templates for common tasks that guide you through specific workflows:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">create-compact-contract      Start a new contract</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">debug-compact-error          Fix compilation errors</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">security-review              Full security audit</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">compare-compact-versions     Migration assistance</span><br></span></code></pre></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="architecture">Architecture<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#architecture" class="hash-link" aria-label="Direct link to Architecture" title="Direct link to Architecture">​</a></h2>
<p>Midnight MCP is built for reliability:</p>
<ul>
<li><strong>Token efficiency</strong>: Outputs YAML by default (20-30% fewer tokens than JSON)</li>
<li><strong>Compound tools</strong>: Single calls that combine multiple operations</li>
<li><strong>Graceful degradation</strong>: Falls back to cached data when services are unavailable</li>
<li><strong>Progress notifications</strong>: Real-time updates during long operations</li>
</ul>
<p>The codebase is fully tested with 206 tests across 10 test suites.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="whats-next">What's next<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#whats-next" class="hash-link" aria-label="Direct link to What's next" title="Direct link to What's next">​</a></h2>
<p>Midnight MCP is open source and actively developed. The roadmap includes:</p>
<ul>
<li><strong>Full ZK circuit output parsing</strong> from compiler results.</li>
<li><strong>Contract deployment</strong> directly from AI chat.</li>
<li><strong>TypeScript SDK integration</strong> for automatic prover code generation.</li>
<li><strong>Local devnet interaction</strong> for querying balances and submitting transactions.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="learn-more">Learn more<a href="https://docs.midnight.network/blog/midnight-mcp-ai-assisted-development#learn-more" class="hash-link" aria-label="Direct link to Learn more" title="Direct link to Learn more">​</a></h2>
<p>Explore the source code and contribute:</p>
<p>→ <a href="https://github.com/Olanetsoft/midnight-mcp" target="_blank" rel="noopener noreferrer">GitHub repository</a></p>
<p>→ <a href="https://npmjs.com/package/midnight-mcp" target="_blank" rel="noopener noreferrer">npm package</a></p>
<p>→ <a href="https://github.com/Olanetsoft/midnight-mcp/blob/main/docs/API.md" target="_blank" rel="noopener noreferrer">API documentation</a></p>
<p><em>Midnight MCP is a community project. Contributions, issues, and feature requests are welcome.</em></p>]]></content:encoded>
            <category>compact</category>
            <category>mcp</category>
            <category>ai</category>
            <category>developer-tools</category>
        </item>
        <item>
            <title><![CDATA[Architecture Overview DUST and Network Usage]]></title>
            <link>https://docs.midnight.network/blog/dust-architecture</link>
            <guid>https://docs.midnight.network/blog/dust-architecture</guid>
            <pubDate>Fri, 02 Jan 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[To understand this system, it helps to use an analogy.]]></description>
            <content:encoded><![CDATA[<p>To understand this system, it helps to use an analogy.</p>
<ul>
<li><strong>Night</strong> is like a <strong>Solar Panel</strong>. It is a valuable asset you hold.</li>
<li><strong>Dust</strong> is like <strong>Electricity</strong>. It represents the computational throughput or gas generated by the Solar Panel (Night).</li>
<li><strong>Usage</strong>. You consume the Electricity (DUST) as gas to power your operations on the network.</li>
</ul>
<p>Unlike standard cryptocurrencies where you have a static balance (e.g., "I have 5 coins"), your Dust balance changes dynamically based on time and the status of your Night tokens.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="dust-and-network-usage">Dust and network usage<a href="https://docs.midnight.network/blog/dust-architecture#dust-and-network-usage" class="hash-link" aria-label="Direct link to Dust and network usage" title="Direct link to Dust and network usage">​</a></h2>
<p>Dust operates similarly to, but separately from, <a href="https://github.com/midnightntwrk/midnight-ledger/blob/main/spec/zswap.md" target="_blank" rel="noopener noreferrer">Zswap</a>. Dust operates as the resource credit system for Midnight. It function</p>
<ul>
<li><strong>Shielded and Non-transferable:</strong> Dust is a shielded capacity resource <em>only</em> for gas. It cannot be transferred between users.</li>
<li><strong>Dynamic Capacity:</strong> The available gas in a Dust UTXO is dynamically computed and derived from an associated Night UTXO.</li>
<li><strong>Growth &amp; Decay:</strong> The computed value grows over time to a maximum based on its Night UTXO, and decays to zero after its Night UTXO is spent.</li>
<li><strong>Non-Persistent:</strong> The system may redistribute it on hardforks. (Note: The Midnight protocol reserves the right to modify Dust allocation rules, e.g., for garbage collection).</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="design-overview">Design overview<a href="https://docs.midnight.network/blog/dust-architecture#design-overview" class="hash-link" aria-label="Direct link to Design overview" title="Direct link to Design overview">​</a></h2>
<p>Similar to Zswap, Dust is built on hashes and the commitment/nullifier paradigm. Each Dust UTXO has a <strong>commitment</strong> inserted into an append-only Merkle tree upon creation, and a <strong>nullifier</strong> inserted into a nullifier set upon spending.</p>
<p>A Dust "spend" is a 1-to-1 "transfer" (Self-Spend):</p>
<ol>
<li><strong>Input:</strong> 1 Dust UTXO (nullifier).</li>
<li><strong>Output:</strong> 1 Dust UTXO (commitment).</li>
<li><strong>Fee:</strong> A public declaration of fees paid.</li>
</ol>
<p>It includes a zero-knowledge proof that:</p>
<ol>
<li>The input is valid and exists in the Merkle tree.</li>
<li>The output value equals the <em>updated</em> input value minus the gas consumed.</li>
<li>The output nullifier is correct, and the owner remains the same.</li>
</ol>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="lifecycle-night-generates-dust">Lifecycle: NIGHT generates DUST<a href="https://docs.midnight.network/blog/dust-architecture#lifecycle-night-generates-dust" class="hash-link" aria-label="Direct link to Lifecycle: NIGHT generates DUST" title="Direct link to Lifecycle: NIGHT generates DUST">​</a></h3>
<p>Conceptually, Dust is generated over time by held <em>Night</em> UTXOs. As long as a backing Night UTXO remains unspent, the associated Dust UTXO generates value up to a cap ($\rho$). Once the backing Night is spent, the Dust UTXO "decays" to zero.</p>
<p>The following diagram illustrates this lifecycle:</p>
<!-- -->
<p>The rate of generation depends on the amount of night held ($N$), the ratio of the Dust cap to Night held ($\rho$), and "time to cap" ($\Delta$).</p>
<p><strong>Spending Rules:</strong></p>
<ul>
<li>Dust may be spent multiple times; a new UTXO is always created, even if its value is zero.</li>
<li>Spending during decay is permitted and does not change the decay rate.</li>
<li>Once the backing Night is spent, Dust immediately starts to decay, even if it was still in the generation phase.</li>
<li>If only a portion of backing Night is spent, the change creates a new Night UTXO (starting fresh Dust generation), while the old Dust UTXO decays.</li>
</ul>
<p><strong>Implementation Note:</strong>
In practice, value is not processed continuously. It is calculated <strong>at the time of spend</strong> using metadata ("generation info"):</p>
<ol>
<li>Creation time of the Dust UTXO.</li>
<li>Creation time of the backing Night UTXO.</li>
<li>Deletion time of the backing Night UTXO.</li>
</ol>
<p>Since Dust and Night use different keys, a <strong>Registration Table</strong> links Night public keys to Dust public keys. A new Dust UTXO is created if and only if a Night UTXO is created <em>and</em> its key has a table entry.</p>
<p><strong>The Grace Period:</strong>
Because Dust usage is shielded, the value is computed for the time of <em>transaction creation</em>. To account for network delays, the protocol defines a <strong>Dust Grace Period</strong> (for example, 3 hours). A transaction is accepted if its timestamp is within this window relative to the block time.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="preliminaries">Preliminaries<a href="https://docs.midnight.network/blog/dust-architecture#preliminaries" class="hash-link" aria-label="Direct link to Preliminaries" title="Direct link to Preliminaries">​</a></h2>
<p>DUST uses ZK-friendly hashes.</p>
<div class="language-rust codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-rust codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token type-definition class-name">DustSecretKey</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token class-name">Fr</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token type-definition class-name">DustPublicKey</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token namespace">field</span><span class="token namespace punctuation" style="color:rgb(248, 248, 242)">::</span><span class="token class-name">Hash</span><span class="token operator">&lt;</span><span class="token class-name">DustSecretKey</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>Dust UTXOs have owners, values, and <strong>nonces</strong>. Nonces evolve deterministically to enable wallet recovery.</p>
<ul>
<li><strong>First Dust UTXO:</strong> Nonce derived from the originating Night UTXO intent hash.</li>
<li><strong>Subsequent Dust UTXOs:</strong> Nonce derived from the previous sequence number and owner's secret key.</li>
</ul>
<div class="language-rust codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-rust codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">struct</span><span class="token plain"> </span><span class="token type-definition class-name">DustOutput</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    initial_value</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">u128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain">   </span><span class="token comment" style="color:rgb(98, 114, 164)">// Specks at creation</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    owner</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">DustPublicKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    nonce</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token namespace">field</span><span class="token namespace punctuation" style="color:rgb(248, 248, 242)">::</span><span class="token class-name">Hash</span><span class="token operator">&lt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token class-name">InitialNonce</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">u32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token class-name">Fr</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    seq</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">u32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    ctime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">Timestamp</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>State components include the commitment tree, nullifier set, and root history:</p>
<div class="language-rust codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-rust codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">struct</span><span class="token plain"> </span><span class="token type-definition class-name">DustUtxoState</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    commitments</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">MerkleTree</span><span class="token operator">&lt;</span><span class="token class-name">DustCommitment</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    commitments_first_free</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">usize</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    nullifiers</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">Set</span><span class="token operator">&lt;</span><span class="token class-name">DustNullifier</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    root_history</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">TimeFilterMap</span><span class="token operator">&lt;</span><span class="token class-name">MerkleTreeRoot</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="initial-dust-parameters">Initial DUST parameters<a href="https://docs.midnight.network/blog/dust-architecture#initial-dust-parameters" class="hash-link" aria-label="Direct link to Initial DUST parameters" title="Direct link to Initial DUST parameters">​</a></h2>
<ul>
<li><strong>Night Unit:</strong> <code>Star (1 Night = 10^6 Stars)</code></li>
<li><strong>Dust Unit:</strong> <code>Speck (1 Dust = 10^15 Specks)</code></li>
</ul>
<div class="language-rust codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-rust codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> </span><span class="token constant" style="color:rgb(189, 147, 249)">INITIAL_DUST_PARAMETERS</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">DustParameters</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    night_dust_ratio </span><span class="token operator">=</span><span class="token plain"> </span><span class="token number">5_000_000_000</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// 5 DUST per NIGHT</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    generation_decay_rate </span><span class="token operator">=</span><span class="token plain"> </span><span class="token number">8_267</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// ~1 week generation time</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    dust_grace_period </span><span class="token operator">=</span><span class="token plain"> </span><span class="token class-name">Duration</span><span class="token punctuation" style="color:rgb(248, 248, 242)">::</span><span class="token function" style="color:rgb(80, 250, 123)">from_hours</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">3</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="dust-actions">Dust actions<a href="https://docs.midnight.network/blog/dust-architecture#dust-actions" class="hash-link" aria-label="Direct link to Dust actions" title="Direct link to Dust actions">​</a></h2>
<p>Users influence Dust state via <strong>Intents</strong>.</p>
<div class="language-rust codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-rust codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">struct</span><span class="token plain"> </span><span class="token type-definition class-name">DustActions</span><span class="token operator">&lt;</span><span class="token class-name">S</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token class-name">P</span><span class="token operator">&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    spends</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">Vec</span><span class="token operator">&lt;</span><span class="token class-name">DustSpend</span><span class="token operator">&lt;</span><span class="token class-name">P</span><span class="token operator">&gt;&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    registrations</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">Vec</span><span class="token operator">&lt;</span><span class="token class-name">DustRegistration</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    ctime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">Timestamp</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="registrations-and-fees">Registrations and fees<a href="https://docs.midnight.network/blog/dust-architecture#registrations-and-fees" class="hash-link" aria-label="Direct link to Registrations and fees" title="Direct link to Registrations and fees">​</a></h3>
<p><code>DustRegistration</code> links a Night key to a Dust key.</p>
<ul>
<li>Registrations happen sequentially.</li>
<li>If a registration transaction uses Night inputs that <em>were not yet generating Dust</em> (meaning that they had no previous registration), the system can "backdate" the registration to use the Dust those inputs <em>would have generated</em> to pay for the gas.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="generating-dust">Generating DUST<a href="https://docs.midnight.network/blog/dust-architecture#generating-dust" class="hash-link" aria-label="Direct link to Generating DUST" title="Direct link to Generating DUST">​</a></h2>
<p>Night inputs/outputs trigger updates to a <strong>Dust Generation Tree</strong>.</p>
<ul>
<li><strong>DustGenerationInfo:</strong> Stores the amount of Night, the owner, and the <code>dtime</code> (deletion time).</li>
<li><strong>Address Map:</strong> Links Night Addresses -&gt; Dust Addresses.</li>
</ul>
<div class="language-rust codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-rust codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">struct</span><span class="token plain"> </span><span class="token type-definition class-name">DustGenerationInfo</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    value</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">u128</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    owner</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">DustPublicKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    nonce</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">InitialNonce</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    dtime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token class-name">Timestamp</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// Set to MAX if Night is unspent</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="dust-value-and-spends">Dust value and spends<a href="https://docs.midnight.network/blog/dust-architecture#dust-value-and-spends" class="hash-link" aria-label="Direct link to Dust value and spends" title="Direct link to Dust value and spends">​</a></h2>
<p>The value of a Dust UTXO is calculated based on four linear time segments:</p>
<ol>
<li><strong>Generating:</strong> From creation to Capacity (or Night spend).</li>
<li><strong>Constant (Max):</strong> At capacity until Night spend.</li>
<li><strong>Decaying:</strong> From Night spend until value hits zero.</li>
<li><strong>Constant (Zero):</strong> Forever after.</li>
</ol>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="the-spend-transaction">The Spend transaction<a href="https://docs.midnight.network/blog/dust-architecture#the-spend-transaction" class="hash-link" aria-label="Direct link to The Spend transaction" title="Direct link to The Spend transaction">​</a></h3>
<p>A <code>DustSpend</code> consumes a UTXO and creates a new one with updated value minus fees.</p>
<!-- -->
<p>The validation logic (<code>dust_spend_valid</code>) ensures:</p>
<ul>
<li><code>commitment_merkle_tree</code> contains the input.</li>
<li><code>dust_spend.old_nullifier</code> matches the derived nullifier.</li>
<li><code>updated_value</code> covers the fee.</li>
<li><code>new_commitment</code> is correctly formed.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="wallet-recovery">Wallet recovery<a href="https://docs.midnight.network/blog/dust-architecture#wallet-recovery" class="hash-link" aria-label="Direct link to Wallet recovery" title="Direct link to Wallet recovery">​</a></h2>
<p>Wallets recover funds by:</p>
<ol>
<li>Identifying owned Night UTXOs (the start of the chain).</li>
<li>Linearly searching for commitments corresponding to sequence numbers ($0, 1, 2...$).</li>
<li><strong>Privacy:</strong> Wallets should query commitments using bit-prefixes (stochastic filtering) rather than exact lookups to preserve privacy against the indexing service.</li>
</ol>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="the-implementation">The implementation<a href="https://docs.midnight.network/blog/dust-architecture#the-implementation" class="hash-link" aria-label="Direct link to The implementation" title="Direct link to The implementation">​</a></h2>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="dust-generation-from-cardano-night-token--full-process">DUST Generation from Cardano NIGHT Token — Full Process<a href="https://docs.midnight.network/blog/dust-architecture#dust-generation-from-cardano-night-token--full-process" class="hash-link" aria-label="Direct link to DUST Generation from Cardano NIGHT Token — Full Process" title="Direct link to DUST Generation from Cardano NIGHT Token — Full Process">​</a></h3>
<p>The generation of <strong>DUST</strong> from the Cardano <strong>NIGHT</strong> token (cNIGHT) is a cross-chain process managed by the <strong>Native Token Observation Pallet</strong> (<code>pallet_cnight_observation</code>) on the Midnight partner chain.</p>
<!-- -->
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="summary-of-the-full-flow">Summary of the full flow<a href="https://docs.midnight.network/blog/dust-architecture#summary-of-the-full-flow" class="hash-link" aria-label="Direct link to Summary of the full flow" title="Direct link to Summary of the full flow">​</a></h3>
<p>1/ User registers their Cardano reward address + DUST public key on Cardano.</p>
<p>2/ Any time a cNIGHT UTXO is created (received) or spent (sent) by that address, the event is broadcast.</p>
<p>3–4/ The Midnight pallet validates that exactly one valid registration exists and observes the cNIGHT activity.</p>
<p>5–6/ It determines whether it’s a receive (creation) or send (destruction) of cNIGHT → generates corresponding DUST creation or destruction event.</p>
<p>7–9/ All events in a block are batched, wrapped into a single system transaction via LedgerApi, and executed on the Midnight ledger.</p>
<p>→ Final result: DUST supply and UTXOs are updated 1:1 with cNIGHT movements on Cardano.</p>
<hr>
<p>👉 <strong>Now that you have an overview of DUST, checkout the <a href="https://github.com/midnightntwrk/midnight-ledger/blob/main/spec/dust.md" target="_blank" rel="noopener noreferrer">DUST spec</a> on Github!</strong></p>
<p>👉 <strong>Get involved in <a href="https://raw.githubusercontent.com/midnightntwrk/midnight-node/refs/heads/main/primitives/mainchain-follower/src/data_source/cnight_observation.rs" target="_blank" rel="noopener noreferrer">the code</a> on Github!</strong></p>]]></content:encoded>
            <category>night</category>
            <category>dust</category>
        </item>
        <item>
            <title><![CDATA[Hacktoberfest Contributor Guide]]></title>
            <link>https://docs.midnight.network/blog/hacktoberfest25</link>
            <guid>https://docs.midnight.network/blog/hacktoberfest25</guid>
            <pubDate>Wed, 15 Oct 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[Hacktoberfest is a global open-source event held every October, where developers contribute pull requests to public repositories to earn digital rewards.]]></description>
            <content:encoded><![CDATA[<p><strong><a href="https://hacktoberfest.com/" target="_blank" rel="noopener noreferrer">Hacktoberfest</a></strong> is a global open-source event held every October, where developers contribute pull requests to public repositories to earn digital rewards.<br>
<!-- -->Midnight is joining the celebration by inviting contributors to improve its open-source ecosystem — from smart contracts and DApps to core libraries and documentation.</p>
<p>Review the guide to get started.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="1-registration">1. Registration<a href="https://docs.midnight.network/blog/hacktoberfest25#1-registration" class="hash-link" aria-label="Direct link to 1. Registration" title="Direct link to 1. Registration">​</a></h2>
<p>To participate, register at <strong><a href="https://hacktoberfest.com/auth/" target="_blank" rel="noopener noreferrer">https://hacktoberfest.com/auth/</a></strong>.</p>
<p>All opportunities to contribute to Midnight’s open source ecosystem will be available through our <strong><a href="https://github.com/midnightntwrk/community-hub" target="_blank" rel="noopener noreferrer">Community Hub</a></strong>.</p>
<p>Participants are encouraged to submit six (6) high-quality pull or merge requests between <strong>October 1 and October 31</strong> to earn the official Hacktoberfest digital badge.<br>
<!-- -->Review our <strong><a href="https://github.com/midnightntwrk/community-hub/blob/main/CONTRIBUTING.md" target="_blank" rel="noopener noreferrer">Contribution Guidelines</a></strong> before submitting any work.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="2-hacktoberfest-participation">2. Hacktoberfest Participation<a href="https://docs.midnight.network/blog/hacktoberfest25#2-hacktoberfest-participation" class="hash-link" aria-label="Direct link to 2. Hacktoberfest Participation" title="Direct link to 2. Hacktoberfest Participation">​</a></h2>
<p>Look for issues labeled <strong><code>hacktoberfest</code></strong> or browse through issues submitted by other community members to find contribution opportunities.</p>
<ul>
<li><strong>Docs:</strong> Submit pull requests to the <strong><a href="https://github.com/midnightntwrk/midnight-docs" target="_blank" rel="noopener noreferrer">Midnight Docs repository</a></strong>.</li>
<li><strong>Core Libraries:</strong> Submit pull requests to <strong><a href="https://github.com/midnightntwrk/midnight-js" target="_blank" rel="noopener noreferrer">MidnightJs</a></strong>.</li>
<li><strong>No-code / Low-code:</strong> Create pull requests in the <strong><a href="https://github.com/midnightntwrk/community-hub" target="_blank" rel="noopener noreferrer">Community Hub</a></strong> using the <a href="https://github.com/midnightntwrk/community-hub/tree/main/Events" target="_blank" rel="noopener noreferrer">submission template</a>.</li>
</ul>
<p>Once reviewed and approved, qualifying issues will be labeled <strong><code>hacktoberfest-accepted</code></strong>.</p>
<p>Have an idea others can contribute to? Create an issue in the <strong><a href="https://github.com/midnightntwrk/community-hub" target="_blank" rel="noopener noreferrer">Community Hub</a></strong> using the following templates:</p>
<ul>
<li><code>dapp-proposal</code></li>
<li><code>content-proposal</code></li>
<li><code>documentation improvement</code></li>
</ul>
<p>To earn additional Midnight-specific rewards, visit our <strong><a href="https://zealy.io/cw/midnightnetwork/questboard/" target="_blank" rel="noopener noreferrer">Zealy quests</a></strong> and complete the tasks under the <a href="https://zealy.io/cw/midnightnetwork/questboard/ffd38a52-cc39-42b4-9880-5b64c18face3/3a29aec7-690c-45a0-a17d-93ea78e65011" target="_blank" rel="noopener noreferrer">Hacktoberfest Competition quest</a>.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="3-rewards">3. Rewards<a href="https://docs.midnight.network/blog/hacktoberfest25#3-rewards" class="hash-link" aria-label="Direct link to 3. Rewards" title="Direct link to 3. Rewards">​</a></h2>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="️-holopin-badge">🎖️ Holopin Badge<a href="https://docs.midnight.network/blog/hacktoberfest25#%EF%B8%8F-holopin-badge" class="hash-link" aria-label="Direct link to 🎖️ Holopin Badge" title="Direct link to 🎖️ Holopin Badge">​</a></h3>
<p>Receive the official Hacktoberfest digital badge for contributing to open source.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="-swag">👕 Swag<a href="https://docs.midnight.network/blog/hacktoberfest25#-swag" class="hash-link" aria-label="Direct link to 👕 Swag" title="Direct link to 👕 Swag">​</a></h3>
<p>The first 10,000 participants who successfully complete six (6) accepted PRs/MRs will receive the official Hacktoberfest T-shirt. (Terms &amp; Conditions apply.)</p>
<p>Top 3 on the <strong><a href="https://zealy.io/cw/midnightnetwork/leaderboard/bdd692a0-9543-4e56-8820-6c09f4e2a771" target="_blank" rel="noopener noreferrer">Zealy Hacktoberfest leaderboard</a></strong> win a Midnight T-shirt.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="-treenation-reward">🌳 Treenation Reward<a href="https://docs.midnight.network/blog/hacktoberfest25#-treenation-reward" class="hash-link" aria-label="Direct link to 🌳 Treenation Reward" title="Direct link to 🌳 Treenation Reward">​</a></h3>
<p>Every sixth PR/MR qualifying for a T-shirt will also trigger a Treenation tree-planting contribution, helping make the world a greener place.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="4-acceptable-contribution-types">4. Acceptable Contribution Types<a href="https://docs.midnight.network/blog/hacktoberfest25#4-acceptable-contribution-types" class="hash-link" aria-label="Direct link to 4. Acceptable Contribution Types" title="Direct link to 4. Acceptable Contribution Types">​</a></h2>
<p>Contributions may include, but are not limited to:</p>
<ul>
<li>Code updates to MidnightJs</li>
<li>Technical documentation</li>
<li>User experience testing</li>
<li>Technical blog posts or tutorials</li>
<li>Case studies or written articles</li>
<li>Copywriting or editing</li>
<li>Translations</li>
<li>Talks or presentations</li>
<li>Event organization</li>
<li>Podcasts or interviews</li>
<li>Social media campaigns</li>
<li>Blog or video content</li>
<li>Graphic design or visual assets</li>
</ul>
<hr>
<p>Ready to start contributing?<br>
<!-- -->Explore open issues on the <strong><a href="https://github.com/midnightntwrk/community-hub" target="_blank" rel="noopener noreferrer">Midnight Community Hub</a></strong> and make your mark this Hacktoberfest!</p>]]></content:encoded>
            <category>community</category>
            <category>beginner</category>
        </item>
        <item>
            <title><![CDATA[Welcome to the Midnight Community Hub! ✨]]></title>
            <link>https://docs.midnight.network/blog/community-hub</link>
            <guid>https://docs.midnight.network/blog/community-hub</guid>
            <pubDate>Tue, 16 Sep 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[The Community Hub serves as a collaborative platform for gathering and managing community-driven contributions. Whether you're proposing new content, requesting features, reporting issues, or ideation for dApps, this repo is your entry point. We use GitHub's built-in features like issues, projects, and automations to ensure ideas are triaged efficiently and turned into actionable tasks.]]></description>
            <content:encoded><![CDATA[<p>The Community Hub serves as a collaborative platform for gathering and managing community-driven contributions. Whether you're proposing new content, requesting features, reporting issues, or ideation for dApps, this repo is your entry point. We use GitHub's built-in features like issues, projects, and automations to ensure ideas are triaged efficiently and turned into actionable tasks.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="how-to-contribute">How to Contribute<a href="https://docs.midnight.network/blog/community-hub#how-to-contribute" class="hash-link" aria-label="Direct link to How to Contribute" title="Direct link to How to Contribute">​</a></h2>
<p>Ready to jump in? It's as simple as <a href="https://github.com/midnightntwrk/community-hub/issues/new/choose" target="_blank" rel="noopener noreferrer"><strong>creating an issue</strong></a>.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="our-workflow-from-idea-to-reality">Our Workflow: From Idea to Reality<a href="https://docs.midnight.network/blog/community-hub#our-workflow-from-idea-to-reality" class="hash-link" aria-label="Direct link to Our Workflow: From Idea to Reality" title="Direct link to Our Workflow: From Idea to Reality">​</a></h2>
<!-- -->
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="step-1-the-community-board-triage">Step 1: The Community Board (Triage)<a href="https://docs.midnight.network/blog/community-hub#step-1-the-community-board-triage" class="hash-link" aria-label="Direct link to Step 1: The Community Board (Triage)" title="Direct link to Step 1: The Community Board (Triage)">​</a></h2>
<p>All new issues land here for initial review. This board is public, so everyone can see what's being proposed.</p>
<!-- -->
<ul>
<li><code>New</code>: Your issue has been submitted and is awaiting review.</li>
<li><code>In Triage</code>: The triage committee is actively reviewing your submission for validity, clarity, and priority.</li>
<li><code>Needs</code> Discussion: The issue requires more feedback or clarification from the community or the original poster.</li>
<li><code>Rejected</code>: The issue is out-of-scope or invalid. We will always provide a clear explanation.</li>
</ul>
<p>Once an issue is approved, it gets a triaged label and is automatically moved to the next stage.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="step-2-the-grab-n-go-board-development">Step 2: The Grab n' Go Board (Development)<a href="https://docs.midnight.network/blog/community-hub#step-2-the-grab-n-go-board-development" class="hash-link" aria-label="Direct link to Step 2: The Grab n' Go Board (Development)" title="Direct link to Step 2: The Grab n' Go Board (Development)">​</a></h2>
<p>This board contains approved, ready-to-work-on tasks. It's the community's backlog!</p>
<!-- -->
<ul>
<li><code>Ready</code>: Triaged issues waiting for a contributor to pick them up. Look for labels like good-first-issue if you're new!</li>
<li><code>In Progress</code>: A contributor has assigned themselves the issue and is actively working on it.</li>
<li><code>Done</code>: The work is complete and the issue has been closed (this happens automatically when a linked PR is merged).</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="choosing-the-right-issue-template">Choosing the Right Issue Template<a href="https://docs.midnight.network/blog/community-hub#choosing-the-right-issue-template" class="hash-link" aria-label="Direct link to Choosing the Right Issue Template" title="Direct link to Choosing the Right Issue Template">​</a></h2>
<p>To help us categorize and review your ideas faster, please use the best template for your submission:</p>
<ul>
<li>📝 <code>Content Proposal</code>: Suggest new articles, tutorials, or educational resources to engage the community.</li>
<li>💡 <code>Feature Request/Suggestion</code>: Propose new features or enhancements for our tools and processes.</li>
<li>🐛 <code>Bug Report</code>: Report any defects, errors, or unexpected behavior. Please include clear reproduction steps!</li>
<li>🌐 <code>dApp Proposal</code>: Share your vision for a new decentralized application, integration, or improvement.</li>
</ul>
<p>Selecting a template automatically applies the right labels, helping our automation keep things organized.</p>
<p>Our goal is to foster a truly collaborative environment where the collective wisdom of our community helps us build more robust, community-driven solutions.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="why-now-the-developer-relations-perspective">Why Now? The Developer Relations Perspective<a href="https://docs.midnight.network/blog/community-hub#why-now-the-developer-relations-perspective" class="hash-link" aria-label="Direct link to Why Now? The Developer Relations Perspective" title="Direct link to Why Now? The Developer Relations Perspective">​</a></h2>
<p>You might be asking, "Why is the Developer Relations (DevRel) team launching this?" That's a great question, and it gets to the very core of our mission!</p>
<p>At its heart, Developer Relations is about empowering developers.</p>
<p>The Community Board is a natural, powerful extension of this mission. Here’s why it’s so crucial for us:</p>
<ol>
<li>Direct Feedback Loop: Before, collecting structured feedback could be spread across multiple channels. The Community Board centralizes this, giving us a clear, prioritized view of what matters most to you.</li>
<li>Transparency and Prioritization: You'll be able to see what other developers are asking for, upvote ideas you support, and even watch as issues move from "Under Review" to "Planned" to "Shipped." This transparency helps you understand our roadmap and how your contributions fit in.</li>
<li>Community-Driven Development: We genuinely believe that the best products are built in collaboration with their users. The board allows us to tap into the collective intelligence of our community, surfacing the most impactful ideas and validating them with real-world needs.</li>
<li>Building Stronger Relationships: By giving you a clear, actionable way to contribute, we hope to build an even stronger, more engaged community where everyone feels invested in our shared success.</li>
</ol>]]></content:encoded>
            <category>community</category>
        </item>
        <item>
            <title><![CDATA[Compact Deep Dive Part 3 - The On-chain Runtime]]></title>
            <link>https://docs.midnight.network/blog/compact-3</link>
            <guid>https://docs.midnight.network/blog/compact-3</guid>
            <pubDate>Wed, 20 Aug 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[This blog post is the third part of the Compact Deep Dive series, which explores how Compact contracts work on the Midnight network.]]></description>
            <content:encoded><![CDATA[<p>This blog post is the third part of the Compact Deep Dive series, which explores how Compact contracts work on the Midnight network.
Each article focuses on a different technical topic and can be read on its own, but together they provide a fuller picture of how Compact functions in practice.
The first two parts can be found here:</p>
<ul>
<li><a href="https://docs.midnight.network/blog/compact" target="_blank" rel="noopener noreferrer">Part 1: Top-level Contract Structure</a></li>
<li><a href="https://docs.midnight.network/blog/compact-2" target="_blank" rel="noopener noreferrer">Part 2: Circuits and Witnesses</a></li>
</ul>
<p>This article looks at the on-chain runtime and how public ledger state updates happen in your DApp.</p>
<h1>The Bulletin Board's <code>post</code> Circuit</h1>
<p>In the previous parts we used the Bulletin Board example DApp to take a look at contract structure and the implementation of circuits and witnesses.
We saw that a contract's exported circuits, such as <code>post</code> from the Bulletin Board example, had an outer "wrapper" that called the actual implementation.
We will now take a look at that actual implementation.</p>
<p>Recall the Compact implementation of <code>post</code>:</p>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">circuit</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">post</span><span class="token plain">(newMessage: </span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;): [] {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">assert</span><span class="token plain">(state == State.VACANT, </span><span class="token string" style="color:rgb(255, 121, 198)">"Attempted to post to an occupied board"</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  poster = </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">disclose</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token plain">(), instance </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Field</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  message = </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">disclose</span><span class="token plain">(some&lt;</span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;&gt;(newMessage));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  state = State.OCCUPIED;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>It reads the <code>state</code> field from the contract's public ledger state and asserts that the bulletin board is vacant.
Then it calls the witness <code>localSecretKey</code> and uses the return value to derive <code>poster</code>, a commitment to the post's author.
Finally, it updates three ledger fields (<code>poster</code>, <code>message</code>, and <code>state</code>).</p>
<p>We compiled the contract with version 0.25.0 of the Compact compiler.
Note that this is a different version than the one used in parts one and two of this series.
If you use a different version, the implementation details might be different.
The implementation of <code>post</code> is in a JavaScript function called <code>_post_0</code>.
Every line of this function has something new to learn about the way that Compact works, so we will go line by line through it.
The first line of this function corresponds to the first line of the Compact circuit:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">_post_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter">context</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token parameter"> partialProofData</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token parameter"> newMessage_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">assert</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    _descriptor_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">fromValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token maybe-class-name">Contract</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_query</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">dup</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">n</span><span class="token operator">:</span><span class="token plain"> </span><span class="token number">0</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">idx</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                   </span><span class="token literal-property property">pushPath</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                   </span><span class="token literal-property property">path</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                     </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">tag</span><span class="token operator">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'value'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                       </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">0n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">popeq</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                     </span><span class="token literal-property property">result</span><span class="token operator">:</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">value</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token operator">===</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token string" style="color:rgb(255, 121, 198)">'Attempted to post to an occupied board'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>(Here and below we have reformatted the JavaScript code to fit better on screen.  The only change is the indentation.)
We will read this line of JavaScript from the inside out.
The very first subexpression evaluated in the Compact <code>post</code> circuit is simply <code>state</code>, which is a read of the ledger's <code>state</code> field.
This also must be the first thing evaluated in the JavaScript implementation.
It is implemented by a call to a compiler-generated static method <code>_query</code> on the <code>Contract</code> class.
(We will take a closer look at <code>Contract._query</code> in the next article in this series.)</p>
<p>Recall what is happening when <code>_post_0</code> is executed in your DApp.
The JavaScript code is running locally on a user's machine, with full access to private data from the witnesses it uses.
After running the code, the proof server will be used to construct a zero-knowledge (ZK) proof that it did run that code.
Then, a transaction will be submitted to the Midnight chain.
If the chain verifies the proof, the public ledger state updates specified by the circuit implementation will be performed.</p>
<p>The Midnight node uses the <a href="https://docs.midnight.network/develop/how-midnight-works/impact" target="_blank" rel="noopener noreferrer">Impact VM</a>,
a stack-based virtual machine, to perform public ledger state updates.
This machine executes a program in a bytecode language called <strong>Impact</strong>.
We use the same Impact VM to perform updates to a local copy of the public state when we are running your DApp's code locally before submitting a transaction.</p>
<h1>The On-Chain Runtime and the Impact VM</h1>
<p>The Compact runtime package includes an embedded version of the on-chain runtime.
This is the exact same Rust code that is used to implement the Midnight ledger on the Midnight network nodes.
For the Compact runtime, it is compiled to WebAssembly and imported as the <a href="https://docs.midnight.network/develop/reference/midnight-api/onchain-runtime/" target="_blank" rel="noopener noreferrer">on-chain runtime</a> package.
You can actually use this package directly in your DApp, though much of it is re-exported by the higher-level Compact runtime.
Sometimes the Compact runtime provides wrapped versions that have a higher-level API
(specifically, using JavaScript representations instead of the ledger's native binary representation).</p>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>If you use the on-chain runtime package directly in your DApp, it is very important to remember that you are working with a snapshot of the public state.
This snapshot was obtained from a Midnight indexer, but while your DApp is running, the chain itself is progressing with other transactions for the same contract.
That's part of what it means for a DApp to be "decentralized".</p></div></div>
<p>The Impact VM performs public ledger state updates, immediately in your DApp's copy and later (when the transaction is run) on chain.
There are a host of reasons to use the exact same VM:</p>
<ul>
<li>Transaction semantics will be identical.</li>
<li>It saves engineering work to implement it (and update it and maintain it) once.</li>
<li>We will need the Impact program to compute fees for the transaction.</li>
<li>The ZK proof will be about this <strong>specific</strong> Impact program.</li>
</ul>
<p>The Impact VM is a stack-based machine.
Transactions operate on a stack of values, which are encoded in the ledger's binary representation.
The transaction always starts with three values on the stack:</p>
<ol>
<li>First (at the base of the stack), a <strong>context</strong> object for the transaction.</li>
<li>Second, an <strong>effects</strong> object collecting actions performed by the transaction.</li>
<li>Third, the contract's public state.</li>
</ol>
<p>When a transaction completes, these three values are left on the stack in the same order.
Values on the VM's stack are immutable.
For instance, a state update is accomplished by <strong>replacing</strong> the state third on the stack with a new one
(rather than <strong>mutating</strong> the existing state value).</p>
<p>The third argument to <code>Contract._query</code> is an array of <code>Op</code>s which are JavaScript representations of Impact VM instructions.
<code>Op</code> is a type <a href="https://docs.midnight.network/develop/reference/midnight-api/onchain-runtime/type-aliases/Op" target="_blank" rel="noopener noreferrer">defined</a> in the on-chain runtime.
Transactions will use a <a href="https://docs.midnight.network/develop/how-midnight-works/impact#programs" target="_blank" rel="noopener noreferrer">binary encoding</a> of these instructions.
The array of instructions is a partial Impact program.
A whole program will be built up for the transaction, usually with multiple calls to <code>Contract._query</code>.</p>
<h1>Ledger Read Operations</h1>
<p>The Impact code for the ledger read of the field <code>state</code> from above was:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">dup</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">n</span><span class="token operator">:</span><span class="token plain"> </span><span class="token number">0</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">idx</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">           </span><span class="token literal-property property">pushPath</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">           </span><span class="token literal-property property">path</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">             </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">tag</span><span class="token operator">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'value'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">               </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">0n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                        </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">popeq</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">             </span><span class="token literal-property property">result</span><span class="token operator">:</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><br></span></code></pre></div></div>
<p>The first instruction is <code>dup 0</code>.
Instructions will find their inputs on the Impact VM's stack.
They can also have operands which are encoded as part of the instruction, such as the zero in <code>dup 0</code>.</p>
<p>The <code>dup N</code> instruction <strong>duplicates</strong> a value on the stack.
The value is found <em>N</em> elements below the top of the stack, so <em>N</em>=0 is the top of the stack.
Since this is the first Impact instruction in a <code>post</code> transaction, that will be the public ledger state value.</p>
<p>Before <code>dup 0</code>, the Impact VM's stack had <code>&lt;context effects ledger0&gt;</code> in order from the base to the top of the stack.
<code>ledger0</code> is the initial ledger state of this transaction.
After <code>dup 0</code>, it will have <code>&lt;context effects ledger0 ledger0&gt;</code> with two copies of the ledger state on top of the stack.
This allows subsequent instructions which consume values to execute without losing the public ledger state.</p>
<p>The second instruction is <code>idx [0]</code>.
This instruction <strong>indexes</strong> into the top value on the stack, extracting a subpart of it.
It removes the top value on the stack and replaces it with the extracted subpart.
The instruction's operand is a path, a sequence of ledger-encoded values (or a special tag <code>stack</code>).
The two different encodings of values, and the descriptors that convert between them, were described in <a href="https://docs.midnight.network/blog/compact-2#descriptors" target="_blank" rel="noopener noreferrer">Part 2</a>.
Here the instruction uses <code>_descriptor_9</code>:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> _descriptor_9 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactTypeUnsignedInteger</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">255n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>This is an instance of <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/classes/CompactTypeUnsignedInteger" target="_blank" rel="noopener noreferrer"><code>CompactTypeUnsignedInteger</code></a>
from the Compact runtime.
It takes a maximum value and a size in bytes.
So this is the descriptor for a one byte unsigned integer with a maximum value of 255 (that is, just an unsigned byte).
The value <code>0n</code> is the public ledger state index that the Compact compiler has assigned to the <code>state</code> field.</p>
<p>Impact has four different indexing instructions: <code>idx</code>, <code>idxc</code> (cached), <code>idxp</code> (push path), and <code>idxpc</code> (push path, cached).
In the JavaScript representation of instructions, we simply use a pair of boolean properties giving the same four variants.
<code>cached</code> is true when the value being accessed has already been accessed (that is, read or written) in the same transaction.
It can potentially be used for assigning different fees for cached and uncached accesses.
In this case, we have not accessed the <code>state</code> ledger field yet in the transaction.
<code>pushPath</code> is a variant that leaves a path in place on the stack for a subsequent write operation.</p>
<p>Before <code>idx [0]</code>, the VM's stack had <code>&lt;context effects ledger0 ledger0&gt;</code>.
After <code>idx [0]</code>, it will have <code>&lt;context effects ledger0 state&gt;</code> (where <code>ledger0</code> is the entire public ledger state and <code>state</code> is the value of the field with that name).</p>
<p>The third instruction is <code>popeq</code>.
This instruction <strong>pops</strong> the top value from the VM's stack.
The Impact VM can run in two different modes.
It runs in <strong>gathering</strong> mode when it runs locally in a DApp.
It runs in <strong>verifying</strong> mode when it executes a contract's public ledger state updates on chain.
In gathering mode the popped value is collected (it's used by <code>Contract._query</code> as the result of this ledger read).
In verifying mode the VM will ensure that the popped value is equal to the instruction's operand.
Here we are running in gathering mode, so the instruction's <code>result</code> operand is the JavaScript <code>undefined</code> value.</p>
<p>Before <code>popeq</code>, the VM's stack had <code>&lt;context effects ledger0 state&gt;</code>.
After <code>popeq</code>, it will have <code>&lt;context effects ledger0&gt;</code>.</p>
<p>These three simple VM instructions implement a top-level ledger read.</p>
<h1>Assert in Circuits</h1>
<p>The <code>post</code> circuit asserts that the bulletin board's state is vacant.
This is implemented by the JavaScript code from above (with the Impact instructions elided):</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">__compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">assert</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  _descriptor_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">fromValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token maybe-class-name">Contract</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_query</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token comment" style="color:rgb(98, 114, 164)">/* Impact code */</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">value</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token operator">===</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token string" style="color:rgb(255, 121, 198)">'Attempted to post to an occupied board'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p><code>Contract._query</code> returns an object with a ledger-encoded result.
<code>_descriptor_0</code> is the descriptor for the the <code>State</code> enumeration type, so <code>_descriptor_0.fromValue</code> is used to convert it to one of the enumeration values <code>VACANT=0</code> or <code>OCCUPIED=1</code>.
This value is compared via JavaScript's strict equality operator to the vacant value (zero).
The assertion itself is implemented by a call to the Compact runtime's <code>assert</code> function.
If the condition is not true, this will cause the DApp's <code>post</code> transaction to fail without ever reaching the Midnight chain.</p>
<p>Provided that the assertion succeeds when running the DApp locally, the ZK proof will prove that it suceeded.
When the proof is verified on chain, the Midnight network will then know that this assertion was true.</p>
<p>The on-chain Impact program will not directly assert this property (that <code>state</code> was <code>State.VACANT</code>).
However, when the <code>popeq</code> instruction is run in verifying mode, it will have an operand that indicates the actual value that was popped locally in the DApp.
So specifically, if the DApp successfully built a <code>post</code> transaction, that instruction will be <code>popeq 0</code>.</p>
<p>This is subtly different from asserting that the <code>state</code> field was 0.
If the <code>assert</code> were removed from the Compact program and the ledger field read remained,
then the Impact program run on chain could have either <code>popeq 0</code> or <code>popeq 1</code> depending on the actual value that was read by the DApp.</p>
<h1>Circuit and Witness Calls</h1>
<p>The second line of the Compact <code>post</code> circuit is:</p>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">poster = </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">disclose</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token plain">(), instance </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Field</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;));</span><br></span></code></pre></div></div>
<p>This is a write to the ledger's <code>poster</code> field.
The right-hand side of the write operation is implemented by the JavaScript code:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> tmp_0 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_publicKey_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_localSecretKey_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">convert_bigint_to_Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token number">32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    _descriptor_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">fromValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token maybe-class-name">Contract</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_query</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">dup</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">n</span><span class="token operator">:</span><span class="token plain"> </span><span class="token number">0</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">idx</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                   </span><span class="token literal-property property">pushPath</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                   </span><span class="token literal-property property">path</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                     </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">tag</span><span class="token operator">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'value'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                       </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">2n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">popeq</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">true</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                     </span><span class="token literal-property property">result</span><span class="token operator">:</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">value</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>The compiler has named this value <code>tmp_0</code> so it can refer to it later.</p>
<p>The call to the witness <code>localSecretKey</code> has been compiled into a call to the <code>_localSecretKey_0</code> method of the contract.
Recall from <a href="https://docs.midnight.network/blog/compact-2#witnesses" target="_blank" rel="noopener noreferrer">Part 2</a> that this method is a wrapper around the DApp-provided witness implementation.
The wrapper performs some type checks on the witness return value, and it records that return value as one of the transaction's private inputs.</p>
<p>The Impact code above implements the read of the ledger's <code>instance</code> field.
It is nearly the same as the code for the read of <code>state</code>.
The difference is that the Compact compiler has assigned <code>instance</code> to index 2.</p>
<p>The field <code>instance</code> has a <code>Counter</code> ledger type.
Reading it in Compact <a href="https://docs.midnight.network/develop/reference/compact/ledger-adt#read-1" target="_blank" rel="noopener noreferrer">gives a <code>Uint&lt;64&gt;</code></a>.
The ledger representation of this value is converted to the JavaScript representation (<code>bigint</code>) using <code>_descriptor_1</code>:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> _descriptor_1 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactTypeUnsignedInteger</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">18446744073709551615n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">8</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>The maximum value here is the maximum unsigned 64-bit integer and the size in bytes is 8.</p>
<h1>Type Casts in Compact</h1>
<p>The Compact code has a sequence of type casts <code>instance as Field as Bytes&lt;32&gt;</code>.
<code>instance</code> is a <code>Counter</code>, reading it from the ledger gives a Compact value with type <code>Uint&lt;64&gt;</code>.
This cannot be directly cast to <code>Bytes&lt;32&gt;</code> so the contract uses an intermediate cast to <code>Field</code>.</p>
<p>There are three distinct kinds of type casts in Compact: upcasts, downcasts, and so-called "cross casts".
An upcast is from a type to one of its supertypes.
Such a cast will change the static type as seen by the compiler, but it will have no effect at runtime.
For example, casting from <code>Uint&lt;64&gt;</code> to <code>Field</code> is an upcast.
That cast was performed statically (that is, by the compiler) but there is no JavaScript code here to implement it.
A downcast is from a type to one of its subtypes.
Such a cast will not normally change a value's representation, but it will require a runtime check in the compiler-generated JavaScript code.
A cross cast is from a type to an unrelated type (with respect to subtyping).
For example, casting a <code>Field</code> to <code>Bytes&lt;32&gt;</code> is a cross cast.
These type casts will normally require a representation change.
The Compact runtime function <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/functions/convert_bigint_to_Uint8Array" target="_blank" rel="noopener noreferrer"><code>convert_bigint_to_Uint8Array</code></a>
performs the representation change in this case.</p>
<p>Then there was a call in Compact to the circuit <code>publicKey</code>.
Here we see that the call goes directly to the JavaScript implementation method <code>_publicKey_0</code>, and not to the exported circuit's <code>publicKey</code> wrapper.
Recall from <a href="https://docs.midnight.network/blog/compact-2#what-are-wrappers-for" target="_blank" rel="noopener noreferrer">Part 2</a> that the circuit wrapper performed some runtime type checks that we do not need when we call from Compact to Compact
(the Compact type system guarantees that these checks aren't needed).
More importantly, a circuit's wrapper established a fresh proof data object for a transaction.
Since the call to <code>publicKey</code> is part of the <code>post</code> transaction that the DApp is building, it should reuse the existing proof data.</p>
<p>Notice that the <code>disclose</code> operator in Compact doesn't do anything at all at runtime.
<code>disclose</code> was necessary because a value computed from the witness <code>localSecretKey</code> was exposed on chain.
This is an instruction to the Compact compiler to allow this value to be exposed, but it has no runtime effect.</p>
<h1>Ledger Writes</h1>
<p>The right-hand side value named <code>tmp_0</code> in JavaScript should be written to the ledger field <code>poster</code>.
The code to do that is in the next line of JavaScript code:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token maybe-class-name">Contract</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_query</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">push</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">storage</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">              </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access maybe-class-name">StateValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">newCell</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">3n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                  </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">encode</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">push</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">storage</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">true</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">              </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access maybe-class-name">StateValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">newCell</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">tmp_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                  </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">encode</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">ins</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">n</span><span class="token operator">:</span><span class="token plain"> </span><span class="token number">1</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>This is again implemented by a partial Impact program.
The Impact code above implements a ledger cell write (in contrast to the read we saw above).</p>
<p>The first instruction is <code>push 3</code>.
Impact's <code>push</code> instruction is the dual to the <code>pop</code> instruction.
Impact has two different variants: <code>push</code> and <code>pushs</code>.
In the JavaScript representation of Impact instructions, they are distinguished by a boolean <code>storage</code> property.
<code>storage</code> is false to indicate that the value is kept solely in the Impact VM's memory and will not be written to the ledger.</p>
<p>The second instruction is <code>pushs tmp_0</code>.
The instruction is <code>pushs</code> because <code>storage</code> is true (this value will be written to the ledger).
The instruction's operand will be the actual <code>Bytes&lt;32&gt;</code> value of <code>tmp_0</code>.
As you would expect, <code>_descriptor_2</code> is the descriptor for <code>Bytes&lt;32&gt;</code>, used to convert the JavaScript value to a ledger value:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> _descriptor_2 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactTypeBytes</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>Before executing these two instructions, the Impact VM's stack had <code>&lt;context effects ledger0&gt;</code>.
After executing these two instructions, it will have <code>&lt;context effects ledger0 3 tmp_0&gt;</code>.</p>
<p>The next instruction is <code>ins 1</code> which <strong>inserts</strong> into a value on the VM's stack.
Impact's <code>ins</code> instruction is the dual to the <code>idx</code> instruction we saw used for ledger reads.
The value to insert is on top of the stack (that is, the <code>Bytes&lt;32&gt;</code> value of <code>tmp_0</code>).
Underneath that is a path consisting of <em>N</em> elements, where <em>N</em> is the operand of the <code>ins</code> instruction.
In this case, the path has length 1, so it is the singleton sequence <code>[3]</code>.
This is the index the Compact compiler has assigned to the top-level ledger field <code>poster</code>.</p>
<p>The insert instruction removes the value under the path (namely, the public ledger state)
and replaces it with a new copy of that value with the location denoted by the path updated to have the new value.</p>
<p>Before <code>ins 1</code>, the Impact VM's stack had <code>&lt;context effects ledger0 3 tmp_0&gt;</code>.
After <code>ins 1</code>, it will have <code>&lt;context effects ledger1&gt;</code> where <code>ledger1</code> is a new public ledger state representing the write to <code>poster</code>.</p>
<p>The remainder of <code>_post_0</code> has two more ledger writes and a return of the Compact empty tuple represented by the JavaScript empty array:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> tmp_1 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_some_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">newMessage_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token maybe-class-name">Contract</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_query</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">push</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">storage</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">              </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access maybe-class-name">StateValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">newCell</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">1n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                  </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">encode</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">push</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">storage</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">true</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">              </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access maybe-class-name">StateValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">newCell</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_5</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">tmp_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                  </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_5</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">encode</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">ins</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">n</span><span class="token operator">:</span><span class="token plain"> </span><span class="token number">1</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token maybe-class-name">Contract</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_query</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">push</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">storage</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">              </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access maybe-class-name">StateValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">newCell</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">0n</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                  </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_9</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">encode</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">push</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">storage</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">true</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">              </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access maybe-class-name">StateValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">newCell</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                  </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">encode</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">ins</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">cached</span><span class="token operator">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">n</span><span class="token operator">:</span><span class="token plain"> </span><span class="token number">1</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>Take a look and see if you can see how the code above works.</p>
<h1><code>publicKey</code></h1>
<p>There is one more way that the on-chain runtime is used by Compact.
If we take a look at the implementation of the <code>publicKey</code> circuit (the actual implementation, not the wrapper for the exported circuit):</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">_publicKey_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter">sk_0</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token parameter"> instance_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">_persistentHash_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">98</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">98</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">111</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">97</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">114</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">100</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">58</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">112</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">107</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">58</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                 instance_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                 sk_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>We see that the padded string literal has been compiled to an explicit <code>Uint8Array</code>.
We also see a call to a <code>_persistentHash_0</code> method, which is the Compact standard library's <a href="https://docs.midnight.network/develop/reference/compact/compact-std-library/exports#persistenthash" target="_blank" rel="noopener noreferrer"><code>persistentHash</code> circuit</a>.</p>
<p>The compiler has included a JavaScript implementation of this circuit:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">_persistentHash_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter">value_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> result_0 </span><span class="token operator">=</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">persistentHash</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">_descriptor_7</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> value_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>This is a call to the Compact runtime's <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/functions/persistentHash" target="_blank" rel="noopener noreferrer"><code>persistentHash</code> function</a>.
This function in the Compact runtime is a thin wrapper around the on-chain runtime's own <a href="https://docs.midnight.network/develop/reference/midnight-api/onchain-runtime/functions/persistentHash" target="_blank" rel="noopener noreferrer"><code>persistentHash</code></a>.
The difference is that the on-chain runtime's version operates on the ledger encoding of values,
and the Compact runtime's version uses the descriptor it is passed to do the relevant conversion.</p>
<p>So this is the final capability of the on-chain runtime that we will look at here:
it contains implementations of functions like <code>persistentHash</code> that are shared between the DApp and the Midnight node.</p>
<p>In the next article in this series, we will look at how partial Impact programs get collected together
and how the compiler-generated JavaScript code produces so-called public inputs and public outputs for the ZK proof that will be generated.</p>]]></content:encoded>
            <category>compact</category>
        </item>
        <item>
            <title><![CDATA[Introduction to the FungibleToken contract on Midnight]]></title>
            <link>https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin</link>
            <guid>https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin</guid>
            <pubDate>Wed, 20 Aug 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[Introduction to the FungibleToken contract on Midnight]]></description>
            <content:encoded><![CDATA[<h2 class="anchor anchorWithStickyNavbar_LWe7" id="introduction-to-the-fungibletoken-contract-on-midnight">Introduction to the FungibleToken contract on Midnight<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#introduction-to-the-fungibletoken-contract-on-midnight" class="hash-link" aria-label="Direct link to Introduction to the FungibleToken contract on Midnight" title="Direct link to Introduction to the FungibleToken contract on Midnight">​</a></h2>
<p><strong>Midnight</strong> is a privacy-first blockchain designed to bring <em>privacy</em> to decentralized applications. It achieves this through <a href="https://docs.midnight.network/develop/how-midnight-works/" target="_blank" rel="noopener noreferrer">zero-knowledge proofs, programmable data protection, and developer-friendly tools</a> like <strong>Compact</strong>, a TypeScript-based DSL (Domain-Specific Language) for writing privacy-aware smart contracts.</p>
<p><a href="https://docs.openzeppelin.com/" target="_blank" rel="noopener noreferrer"><strong>OpenZeppelin</strong></a> is renowned in the Ethereum ecosystem for its battle-tested smart contract libraries, which have secured trillions in on-chain value. Recently, <a href="https://midnight.network/blog/openzeppelin-joins-forces-with-midnight-to-shape-the-future-of-privacy-preserving-smart" target="_blank" rel="noopener noreferrer">OpenZeppelin partnered with Midnight</a> to bring comparable tooling to the Compact ecosystem, adapting familiar standards like ERC-20 into privacy-preserving variants.</p>
<p>In the Ethereum world, the <strong>ERC-20</strong> standard defines a fungible token with public ledger functions like <code>balanceOf</code>, <code>transfer</code>, <code>approve</code>, etc. It exposes transaction data transparently and lacks built-in privacy. The <strong>FungibleToken</strong> contract on Midnight draws inspiration from this, but operates within Midnight’s zero-knowledge, selective-disclosure framework.</p>
<p>Fungible tokens are a cornerstone of the blockchain ecosystem, representing digital assets that are interchangeable – much like traditional currency. On various blockchains, these tokens power a wide array of applications, from facilitating seamless transactions and enabling decentralized finance (DeFi) protocols to representing ownership in digital communities and driving the mechanics of in-game economies.</p>
<p>Unlike unique non-fungible tokens (NFTs), the value of one fungible token is identical to another of the same type, making them ideal for use cases requiring divisibility and ease of exchange. Their widespread adoption underscores their importance in building liquid and interconnected digital economies.</p>
<p>In this article, you'll learn about the core features of the contract, including how it manages ledger state variables, its key entry points and circuits for operations like minting, burning, and transferring tokens, and the essential safety and utility functions provided by the <strong>Utils</strong> and <strong>Initializable</strong> modules.<br>
<!-- -->By understanding how these components fit together, you’ll gain insight into how the <strong>FungibleToken</strong> contract balances fungibility, usability, and privacy, providing an essential building block for privacy-preserving DeFi, identity, and tokenized assets on Midnight.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="features-of-the-fungibletoken-contract">Features of the FungibleToken Contract<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#features-of-the-fungibletoken-contract" class="hash-link" aria-label="Direct link to Features of the FungibleToken Contract" title="Direct link to Features of the FungibleToken Contract">​</a></h2>
<p>The FungibleToken contract on Midnight utilizes ledger state variables to keep track of balances, allowances, total supply, name, symbol, and decimals. Its functionality is exposed through "circuits" (entry points) like <code>Mint</code>, <code>Burn</code>, <code>Transfer</code>, <code>Approve</code>, <code>TransferFrom</code>, and <code>Initialize</code>, all of which enforce specific zero-knowledge validated transitions and maintain the integrity of the token's state.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="ledger-state-variables">Ledger State Variables<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#ledger-state-variables" class="hash-link" aria-label="Direct link to Ledger State Variables" title="Direct link to Ledger State Variables">​</a></h3>
<p>In Compact, the contract defines a structured <strong>state</strong> storing token balances and allowances—similar to ERC-20. The <code>_balances</code> map keeps track of the users’ token balances and is updated when a transfer occurs. The <code>_allowances</code> map keeps track of the permission given to specific users to spend tokens on behalf of another user:</p>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> _balances: </span><span class="token class-name">Map</span><span class="token plain">&lt;Either&lt;ZswapCoinPublicKey, ContractAddress&gt;, </span><span class="token class-name">Uint</span><span class="token plain">&lt;</span><span class="token number">128</span><span class="token plain">&gt;&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> _allowances: </span><span class="token class-name">Map</span><span class="token plain">&lt;Either&lt;ZswapCoinPublicKey, ContractAddress&gt;, </span><span class="token class-name">Map</span><span class="token plain">&lt;Either&lt;ZswapCoinPublicKey, ContractAddress&gt;, </span><span class="token class-name">Uint</span><span class="token plain">&lt;</span><span class="token number">128</span><span class="token plain">&gt;&gt;&gt;;</span><br></span></code></pre></div></div>
<p>These values live in the contract's ledger and are updated through transactions sent to the contract.</p>
<p>There are other values in the ledger that are set when the contract is deployed:</p>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> _totalSupply: </span><span class="token class-name">Uint</span><span class="token plain">&lt;</span><span class="token number">128</span><span class="token plain">&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">sealed</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> _name: </span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">sealed</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> _symbol: </span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">sealed</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> _decimals: </span><span class="token class-name">Uint</span><span class="token plain">&lt;</span><span class="token number">8</span><span class="token plain">&gt;;</span><br></span></code></pre></div></div>
<p>These values provide different information about the token managed by the contract, its total supply, its name, its symbol, and its decimal (for display).</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="entry-points-and-circuits">Entry Points and Circuits<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#entry-points-and-circuits" class="hash-link" aria-label="Direct link to Entry Points and Circuits" title="Direct link to Entry Points and Circuits">​</a></h3>
<p>In Compact, entry points are defined as <strong>circuits</strong> (akin to Solidity functions), each modelling a zero-knowledge validated transition. The difference between a circuit entry point and a circuit is that the entry point is callable via a transaction, while the non-entry point circuit is internal. Core circuits include:</p>
<ul>
<li>
<p><strong><code>Mint</code> / <code>Burn</code></strong> (to mint new tokens or burn existing tokens).</p>
</li>
<li>
<p><strong><code>Transfer</code></strong>: to move tokens between addresses.</p>
</li>
<li>
<p><strong><code>Approve</code></strong>, <strong><code>TransferFrom</code></strong>: standard ERC-20-style delegation mechanisms.</p>
</li>
<li>
<p><strong><code>Initialize</code></strong>: via the <strong>Initializable</strong> module for contract setup.</p>
</li>
</ul>
<p>Each circuit enforces necessary constraints — for example, ensuring sufficient balance, managing allowance decrements, and preserving total supply.</p>
<p>In the next step of the contract lifecycle, the different metadata stored in the ledger of the contract are safely initialized.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="initialization-and-metadata">Initialization and Metadata**<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#initialization-and-metadata" class="hash-link" aria-label="Direct link to Initialization and Metadata**" title="Direct link to Initialization and Metadata**">​</a></h2>
<p>The following circuits define the essential setup and retrieval logic for the fungible token metadata and users’ balances, enforcing correct initialization.</p>
<ul>
<li>
<p><strong><code>initialize(name_, symbol_, decimals_)</code></strong><br>
<!-- -->One-time setup. Calls <code>Initializable_initialize()</code>, then stores the (disclosed) name, symbol, and decimals. Every other public circuit asserts that the contract is initialized first.</p>
</li>
<li>
<p><strong><code>name()</code> / <code>symbol()</code> / <code>decimals()</code> / <code>totalSupply()</code></strong><br>
<!-- -->Simple getters that first assert initialized, then return the sealed (read only) ledger values.</p>
</li>
<li>
<p><strong><code>balanceOf(account)</code></strong><br>
<!-- -->Safe map lookup that returns <code>0</code> if the account isn’t present (to prevent contract failure if the key is absent).</p>
</li>
</ul>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="the-transfer-family">The Transfer Family<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#the-transfer-family" class="hash-link" aria-label="Direct link to The Transfer Family" title="Direct link to The Transfer Family">​</a></h2>
<p>The <code>FungibleToken</code> contract's <strong>transfer</strong> circuits manage token movement. Key circuits include: <code>transfer</code> for safe user-initiated transfers, <code>_unsafeTransfer</code> for internal token movement, <code>_transfer</code> for administrative transfers, <code>_unsafeUncheckedTransfer</code> for low-level token movement, and <code>_update</code> as the central accounting function for all token operations.</p>
<p>These are split into <strong>safe</strong> and <strong>unsafe</strong> variants because <strong>sending to contract addresses is currently disallowed</strong> (until contract-to-contract interactions are supported).</p>
<p>“Safe” circuits enforce that policy; “unsafe” ones let you bypass it—explicitly marked as dangerous in comments.</p>
<ul>
<li>
<p><strong><code>transfer(to, value)</code> → <code>Boolean</code></strong><br>
<!-- -->Safe user-initiated transfer: rejects if <code>to</code> is a <code>ContractAddress</code>. Internally, it just forwards to the unsafe variant after the check.</p>
</li>
<li>
<p><strong><code>_unsafeTransfer(to, value)</code> → <code>Boolean</code></strong><br>
<!-- -->Owner is the caller (<code>left(ownPublicKey())</code>). Moves value using the unchecked internal mover, then returns <code>true</code>.</p>
</li>
<li>
<p><strong><code>_transfer(from, to, value)</code> → <code>[]</code></strong><br>
<!-- -->Admin/extension hook that moves tokens <strong>from an arbitrary <code>from</code></strong> (not necessarily the caller). Still enforces the “no contracts as <code>to</code>” rule and then uses the same mover underneath.</p>
</li>
<li>
<p><strong><code>_unsafeUncheckedTransfer(from, to, value)</code> → <code>[]</code></strong><br>
<!-- -->The low-level mover checks that neither side is the zero/burn address and then delegates the actual accounting to <code>_update</code>.</p>
</li>
<li>
<p><strong><code>_update(from, to, value)</code> → <code>[]</code></strong><br>
<strong>Central accounting function</strong> used by all mint/burn/transfer paths. It’s an internal circuit; it cannot be called via a transaction.</p>
<ul>
<li>
<p>If <code>from</code> is zero, the <strong>mint</strong> circuit is called, it asserts no <code>uint128</code> overflow, and increases <code>_totalSupply</code>.</p>
</li>
<li>
<p>Else, it deducts from <code>from</code> balance (or reverts on insufficient funds).</p>
</li>
<li>
<p>If <code>to</code> is zero, the <strong>burn</strong> circuit is called, and it decreases <code>_totalSupply</code>.</p>
</li>
<li>
<p>Else, it adds to <code>to</code> balance.<br>
<!-- -->This single function guarantees the invariants for every movement of value.</p>
</li>
</ul>
</li>
</ul>
<p>The "transfer family" circuits ensure secure token movement, with "safe" variants disallowing transfers to contract addresses and "unsafe" variants providing lower-level control.<br>
<!-- -->This leads us to explore how allowances function, enabling delegated token spending.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="allowances-approve--spend--transferfrom">Allowances (approve / spend / transferFrom)<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#allowances-approve--spend--transferfrom" class="hash-link" aria-label="Direct link to Allowances (approve / spend / transferFrom)" title="Direct link to Allowances (approve / spend / transferFrom)">​</a></h2>
<p>This section details the allowance mechanisms within the FungibleToken contract, which enable users to delegate spending permissions to other addresses. These circuits facilitate secure, approved transfers on behalf of an owner without directly exposing their private keys.</p>
<ul>
<li>
<p><strong><code>allowance(owner, spender)</code></strong><br>
<!-- -->Read the nested <code>_allowances</code> map, returning <code>0</code> when keys are missing (no revert).</p>
</li>
<li>
<p><strong><code>approve(spender, value)</code> → <code>Boolean</code></strong><br>
<!-- -->The owner is the caller. Forwards to <code>_approve(owner, spender, value)</code> and returns <code>true</code>.</p>
</li>
<li>
<p><strong><code>transferFrom(from, to, value)</code> → <code>Boolean</code></strong><br>
<!-- -->Safe delegated transfer: enforces the “no contract receiver” rule, then defers to <code>_unsafeTransferFrom</code>.</p>
</li>
<li>
<p><strong><code>_unsafeTransferFrom(from, to, value)</code> → <code>Boolean</code></strong><br>
<!-- -->The spender is the caller. First spends allowance via <code>_spendAllowance(from, spender, value)</code>, then moves value using <code>_unsafeUncheckedTransfer</code>. Returns <code>true</code>.</p>
</li>
<li>
<p><strong><code>_approve(owner, spender, value)</code> → <code>[]</code></strong><br>
<!-- -->It ensures that both the owner and the spender are valid, creates the owner’s entry in the map if needed, and then writes the allowance. (This mirrors OZ’s ERC-20 pattern of public <code>approve()</code> → internal <code>_approve()</code>.)</p>
</li>
<li>
<p><strong><code>_spendAllowance(owner, spender, value)</code> → <code>[]</code></strong><br>
<!-- -->It deducts from the allowance unless it’s “infinite.” The implementation treats <strong><code>MAX_UINT128</code></strong> as infinite: if <code>currentAllowance == MAX</code>, it doesn’t decrement; otherwise, it asserts <code>currentAllowance ≥ value</code> and writes back <code>currentAllowance - value</code>.<br>
<!-- -->This is important because it supports “no-friction approvals” by letting apps set MAX once.</p>
</li>
</ul>
<p>So, we just covered how allowances let people delegate token spending—basically, giving others permission to move their tokens. Up next, we'll dive into how we create and delete tokens in the contract.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="minting-and-burning">Minting and Burning<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#minting-and-burning" class="hash-link" aria-label="Direct link to Minting and Burning" title="Direct link to Minting and Burning">​</a></h2>
<p>Here's how the FungibleToken contract handles making and destroying tokens. We'll dive into the <code>_mint</code> and <code>_burn</code> functions, showing what they do and how they link up with the main accounting system.</p>
<ul>
<li>
<p><strong><code>_mint(account, value)</code></strong> (safe) → <code>[]</code><br>
<!-- -->It forbids minting <strong>to</strong> a contract address (same contract-to-contract restriction), then forwards to <code>_unsafeMint</code>.</p>
</li>
<li>
<p><strong><code>_unsafeMint(account, value)</code></strong> → <code>[]</code><br>
<!-- -->It validates the receiver’s address, then calls <code>_update(burnAddress(), account, value)</code>—i.e., mint is modelled as a transfer <strong>from</strong> the burn/zero address.</p>
</li>
<li>
<p><strong><code>_burn(account, value)</code></strong> → <code>[]</code><br>
<!-- -->It validates the sender’s address, then calls <code>_update(account, burnAddress(), value)</code>—i.e., burn is a transfer <strong>to</strong> the burn/zero address.<br>
<em>Note:</em> The actual notion of “zero/burn” address is standardized in the Utils module; you can also see helpers like <code>Utils_isKeyOrAddressZero</code> and <code>Utils_isContractAddress</code>.</p>
</li>
</ul>
<p>Because mint and burn also route through <code>_update</code>, <strong>total supply is adjusted in exactly one place,</strong> and the same safety checks apply across all flows (including the <code>uint128</code> overflow check on mint).</p>
<p>The mint and burn circuits, by using the <code>_update</code> function, make sure the total supply adjustments are always consistent and that all token flows get the same safety checks.<br>
<!-- -->Now, let's dive into the extra safety and utility stuff that the <code>Utils</code> and <code>Initializable</code> modules bring to the table.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="safety-and-utility-glue-from-utils-and-initializable">Safety and Utility Glue (from <code>Utils</code> and <code>Initializable</code>)<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#safety-and-utility-glue-from-utils-and-initializable" class="hash-link" aria-label="Direct link to safety-and-utility-glue-from-utils-and-initializable" title="Direct link to safety-and-utility-glue-from-utils-and-initializable">​</a></h2>
<p>This section explores how the <code>Utils</code> and <code>Initializable</code> modules provide essential safeguards and helpful functionalities. These components are vital for ensuring the contract's integrity and enabling secure, well-managed operations.</p>
<ul>
<li>
<p><strong>Initialization guards</strong>: The <code>Initializable_initialize</code> and <code>Initializable_assertInitialized</code> functions serve as crucial initialization guards within the <code>Initializable</code> contract. These safeguards ensure that a contract's state is properly set up only once and that subsequent operations only proceed if the contract has been correctly initialized. Every circuit that interacts with or modifies the contract's state is designed to invoke the <code>assert</code> function, reinforcing the integrity of the initialization process.</p>
</li>
<li>
<p><strong>Address helpers</strong>:</p>
<ul>
<li>
<p><code>Utils_isContractAddress(either)</code> distinguishes user keys from contract addresses.</p>
</li>
<li>
<p><code>Utils_isKeyOrAddressZero(either)</code> detects the zero/burn address used in <code>_update</code>, <code>_unsafeUncheckedTransfer</code>, etc.<br>
<!-- -->These support the temporary “no contract receiver” policy and zero-address checks.</p>
</li>
</ul>
</li>
</ul>
<p>The <strong>Utils</strong> and <strong>Initializable</strong> modules provide crucial safety and utility functions, ensuring the contract's proper setup and secure operation. Now, let's look at how all these different parts of the <strong>FungibleToken</strong> contract work together.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="how-the-pieces-fit-together">How the Pieces Fit Together<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#how-the-pieces-fit-together" class="hash-link" aria-label="Direct link to How the Pieces Fit Together" title="Direct link to How the Pieces Fit Together">​</a></h2>
<p>This part shows how everything in the FungibleToken contract is hooked up. Whether it's you sending tokens, someone else doing it for you, or tokens being created or destroyed, it all funnels through a few key functions and ultimately lands in the main <code>_update</code> function to keep track of everything.</p>
<ul>
<li>
<p>User transfer:<br>
<code>transfer</code> → <em>(safe check)</em> → <code>_unsafeTransfer</code> → <code>_unsafeUncheckedTransfer</code> → <code>_update</code> (balances/supply)</p>
</li>
<li>
<p>Delegated transfer:<br>
<code>transferFrom</code> → <em>(safe check)</em> → <code>_unsafeTransferFrom</code> → <code>_spendAllowance</code> → <code>_unsafeUncheckedTransfer</code> → <code>_update</code></p>
</li>
<li>
<p>Mint/Burn:<br>
<code>_mint/_unsafeMint</code> or <code>_burn</code> → <code>_update</code> (with zero/burn address on one side)</p>
</li>
</ul>
<p>This section illustrates how various token operations, from user transfers to minting and burning, ultimately funnel through the central <code>_update</code> function for consistent accounting. Now, let's summarize the key takeaways of the <strong>FungibleToken</strong> contract on Midnight.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>The <strong>FungibleToken</strong> Compact contract on Midnight is a privacy-aware reimagining of the ERC-20 standard. It maintains the familiar token interfaces—balances, transfers, approvals—but encodes them as ZK-validated circuits within Compact, enabling private, verifiable execution. The contract’s state and logic are shielded by design, exposing only proofs rather than raw data to the blockchain.</p>
<p>The ERC-20 standard revolutionized the crypto landscape by providing a common framework for creating and managing digital assets, fostering interoperability, and accelerating the growth of decentralized applications. For Midnight, an ERC-20-based token is crucial as it leverages this established standard while integrating ZK-privacy, offering a familiar yet enhanced experience for developers and users seeking both functionality and confidentiality.</p>
<p>This model contrasts sharply with ERC-20 on Ethereum, where all token movements and balances are fully transparent. Here, Midnight allows selective disclosure: users and applications choose what to reveal. The <strong>FungibleToken</strong> contract thus balances fungibility, usability, and privacy—providing an essential building block for privacy-preserving DeFi, identity, and tokenized assets on Midnight.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="links">Links<a href="https://docs.midnight.network/blog/funbigle-token-contract-open-zeppelin#links" class="hash-link" aria-label="Direct link to Links" title="Direct link to Links">​</a></h2>
<p>Delve deeper into the code, contracts, and comprehensive documentation to enhance your understanding and development skills. These resources are invaluable for building robust and innovative solutions.</p>
<ul>
<li>FungibleToken contract: <a href="https://github.com/OpenZeppelin/compact-contracts/blob/main/contracts/fungibleToken/src/FungibleToken.compact" target="_blank" rel="noopener noreferrer">FungibleToken.compact on Github</a></li>
<li>OpenZeppelin documentation: <a href="https://docs.openzeppelin.com/contracts-compact/0.0.1/fungibletoken" target="_blank" rel="noopener noreferrer">docs.openzeppelin.com</a></li>
<li>ERC20 standard: <a href="https://ethereum.org/en/developers/docs/standards/tokens/erc-20/" target="_blank" rel="noopener noreferrer">ethereum.org</a></li>
<li>Midnight docs: <a href="https://docs.midnight.network/" target="_blank" rel="noopener noreferrer">Midnight Developer Documentation</a></li>
<li>Do you have any questions?: <a href="https://forum.midnight.network/" target="_blank" rel="noopener noreferrer">Midnight Forum</a></li>
</ul>]]></content:encoded>
            <category>midnight</category>
            <category>compact</category>
        </item>
        <item>
            <title><![CDATA[From Frustration to Framework: Building on Midnight]]></title>
            <link>https://docs.midnight.network/blog/from-frustration-to-framework</link>
            <guid>https://docs.midnight.network/blog/from-frustration-to-framework</guid>
            <pubDate>Wed, 13 Aug 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[After participating in three hackathons this year, I thought I had a good idea of what to expect. Then came the fourth. It began like the others: an idea, a challenge, a blank terminal screen. But this one took an unexpected turn, one I didn't see coming.]]></description>
            <content:encoded><![CDATA[<p>After participating in three hackathons this year, I thought I had a good idea of what to expect. Then came the fourth. It began like the others: an idea, a challenge, a blank terminal screen. But this one took an unexpected turn, one I didn't see coming.</p>
<p>I had recently left my software engineering role to explore zero-knowledge protocols, an area in which I didn’t have much experience. It was a risk, but one I felt I had to take. I chose to pursue this path not only because zero-knowledge cryptography is built on one of the most elegant branches of mathematics, but also I believe privacy on the blockchain or on the internet is fundamental and should never be an afterthought.</p>
<p>One day, my mentor casually mentioned the <a href="https://www.africanblockchamp.com/" target="_blank" rel="noopener noreferrer">African Blockchain Championship</a> and suggested I look into the Zero-Knowledge track. That small nudge became a turning point.</p>
<p>That’s when I discovered <a href="https://midnight.network/" target="_blank" rel="noopener noreferrer">Midnight</a>!</p>
<h1>Meet Midnight: Privacy on the Blockchain</h1>
<p>Midnight is a zero-knowledge proof-based privacy chain developed by Input Output, the parent company of Cardano. As a partner chain of Cardano, it aims to bring rational privacy through zero-knowledge technology to the broader ecosystem. They weren’t just building another blockchain—they were laying the foundation for private, decentralized computation. After diving into the documentation and setting up the tools, everything was finally in place. All that remained… was to begin. Only later did I realize: this wasn’t just the start of another hackathon—it was the beginning of something much bigger.</p>
<h1>Building on Midnight</h1>
<p>Hacking on Midnight was challenging, as building in a testnet environment requires constant learning and adaptation to major changes. Within just a few weeks, significant network updates rendered the existing examples incompatible with the latest SDK versions. As a result, an example had to be cloned and debugged line by line. The example repository was dense, filled with layers of files and directories, and figuring out where to start was a challenge in itself.</p>
<p>Fortunately, Midnight’s smart contract language, <a href="https://docs.midnight.network/develop/reference/compact/" target="_blank" rel="noopener noreferrer">Compact</a>, shares a syntax similar to TypeScript, which eased the learning curve. I pushed forward, determined to build something meaningful and make my mark—but in the end, my attempt fell short. With midterm exams approaching and the hackathon submission deadline close by, I had to make a difficult decision. I decided not to submit a project to the African Blockchain Championship hackathon. Still, a quiet sense of regret lingered. I couldn’t help but wonder: what if I had just pushed a <em>little</em> harder?</p>
<h1>The Extended Deadline</h1>
<p>About a week later, I received an email from the ABC team announcing that the hackathon deadline had been extended. I was thrilled, and I promised myself I’d give it everything this time. It wasn’t long before I was back at it. A few weeks had passed since I’d last touched the project, and during that time, both the network and SDKs had been updated. I gathered my gear, updated my environment, and set out with renewed determination to finally build something solid. I wrote my contract and began debugging, but each change introduced a new layer of complexity. Every update required changes across multiple files. It was frustrating, exhausting, but strangely thrilling. I began thinking of ways to simplify the chaos, to make the process less painful, more intuitive. That’s when I started creating small scripts to streamline the workflow.</p>
<p>Earlier that month, I had joined the <a href="https://discord.com/invite/midnightnetwork" target="_blank" rel="noopener noreferrer">Midnight Discord</a> to ask questions. One week, I jumped on a Discord community call, and to my surprise, I learned about an ongoing Midnight tooling hackathon that was happening asynchronously. Right then, it clicked: <em>this</em> was the hackathon I was meant to be part of from the start.</p>
<h1>From Personal Tool to Shared Project</h1>
<p>What started as a personal tool suddenly took on a bigger purpose. The hackathon provided me with the opportunity to share my work with others and contribute to shaping the future of smart contract development on Midnight. I took a little detour from my original hackathon submission—promising myself I’d return to it—and began refining the tool, aiming not just to build something functional, but something genuinely useful for newcomers to the ecosystem.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="scaffold-midnight--create-midnight-app">Scaffold Midnight &amp; Create Midnight App<a href="https://docs.midnight.network/blog/from-frustration-to-framework#scaffold-midnight--create-midnight-app" class="hash-link" aria-label="Direct link to Scaffold Midnight &amp; Create Midnight App" title="Direct link to Scaffold Midnight &amp; Create Midnight App">​</a></h2>
<p>That’s how <a href="http://github.com/kaleababayneh/scaffold-midnight" target="_blank" rel="noopener noreferrer"><strong>Scaffold Midnight</strong></a> was born, a cloneable GitHub starter template. But I didn’t stop there. I realized it would be even more useful as an npm library, something developers could install with all dependencies bundled and start using right away. So, I polished the project and released the first version as <a href="https://www.npmjs.com/package/create-midnight-app" target="_blank" rel="noopener noreferrer"><strong>create-midnight-app</strong></a>. But I didn’t want to stop at solving just one problem. Every issue I fixed revealed another. Through multiple iterations, I kept refining the library. It’s now at version 2.1.7, and I’m actively working to make it compatible with the latest release of Compact. It’s far from perfect, and that’s what makes it exciting. Every day, I wake up with a new idea, a new feature to add, or a better way to improve the developer experience!</p>
<p><img decoding="async" loading="lazy" src="https://ibin.co/8rMdmI75JRDl.png" alt="Create Midnight App" class="img_ev3q"></p>
<p>At its core, <strong>create-midnight-app</strong> is a scaffolding npm library for Midnight that automates the entire setup process. Developers can focus solely on writing their Compact contracts; the tool takes care of the rest. It handles wallet creation, faucet requests, balance checks, API configuration, CLI updates, and even function and file change imports are updated automatically. It reduces initial application setup time from over half an hour to just a few minutes.</p>
<p>I had several euphoric moments while working on this project, but one instance in particular, stands out.</p>
<p>One feature I was eager to build was a way for developers to request testnet tokens without ever leaving their code editor. The simplest idea was to send a request from the terminal directly to the official <a href="https://docs.midnight.network/blog/%20https://midnight.network/test-faucet">Midnight faucet</a>. But there was a catch—the site is protected by <a href="https://www.cloudflare.com/application-services/products/turnstile/" target="_blank" rel="noopener noreferrer">Cloudflare Turnstile</a>, which blocks automated requests to prevent spam and abuse. As a hacker, I started looking for a workaround. After some Googling and tips from friends, I tested several paid tools that claimed to bypass CAPTCHA protections. I spent an entire afternoon trying them, but none worked reliably. That’s when I remembered the genesis wallet, whose private key is publicly available. So, I implemented a workaround: instead of calling the faucet, I simulated a faucet transaction by transferring tokens directly from the genesis wallet to the user’s wallet. It’s not a long-term solution, and I know it won’t scale—but for now, it works and helps streamline the developer experience.</p>
<h1>Submission and Recognition</h1>
<p>I finally submitted my project, and the judges’ reactions were incredibly <a href="https://youtu.be/xrCEBIBqS-Y?t=4453" target="_blank" rel="noopener noreferrer">encouraging</a>—it gave me a renewed sense of inspiration. With that momentum, I turned my focus back to my original hackathon: the African Blockchain Championship. For this challenge, I built an anonymous, censorship-resistant voting platform. In many African countries—across 34 nations—roughly 30–40% of citizens no longer view voting as a fair or trustworthy process (Afrobarometer, 2023). That’s why anonymous voting isn’t just a feature—it’s a necessity. My project, <a href="https://github.com/kaleababayneh/zkvote-midnight" target="_blank" rel="noopener noreferrer"><strong>Privote</strong></a>, aims to address this critical issue by leveraging zero-knowledge technology to protect voter identity and integrity.</p>
<p>As the submission deadline approached, I ran into a major hurdle: integrating the wallet into the frontend. I reached out to one of the Developer Relations Engineers, who kindly shared some example code. Unfortunately, I couldn’t get it working in time. With the clock ticking, I had to improvise. I quickly built a custom Chrome extension that enabled wallet interaction—running through the terminal on the backend—just in time for submission. While the current version still has some privacy limitations, the smart contract compiles successfully, and users can interact with it on the Midnight testnet—offering a real glimpse of what decentralized, anonymous voting could look like in practice.</p>
<p><img decoding="async" loading="lazy" src="https://ibin.co/8rMho4816dhp.png" alt="Private Hackathon" class="img_ev3q"></p>
<h1>The Midnight Community</h1>
<p>Even though there aren’t many resources available yet, the Midnight community more than makes up for it. Almost every question asked on Discord gets a response. It’s a space where people genuinely support each other—even during hackathons, when we’re technically competing and have little incentive to help other teams. In fact, during the hackathon, I often found myself answering questions in the Telegram group. I even hosted a workshop at the ABC hackathon on how to use <strong>Create Midnight App</strong>, helping others get started quickly and navigate the ecosystem more easily.</p>
<p>I also want to take a moment to thank the Midnight team for the recognition they gave me—it truly meant a lot. Beyond placing first in the CLI track, the support and appreciation I received from the community were genuinely heartwarming. I even had the chance to join a community call—not just as a participant this time, but as a <a href="https://x.com/MidnightNtwrk/status/1942969613496758398" target="_blank" rel="noopener noreferrer">speaker</a>—to share my journey and the lessons I’d learned along the way.</p>
<h1>Why Now is the Time to Build on Midnight</h1>
<p>And for you—my readers—this is honestly the best time to get involved in the Midnight ecosystem. There’s <em>almost always</em> a hackathon happening (seriously, it's kind of wild!). Right now, they’re even running a <a href="https://midnight.network/hackathon/midnight-mini-dapp-hackathon" target="_blank" rel="noopener noreferrer">mini DApp hackathon</a>. It’s an excellent opportunity to learn, build, and get your project noticed.</p>
<p><img decoding="async" loading="lazy" src="https://ibin.co/8rMcCFquPRqS.png" alt="Midnight Dapp Hackathon" class="img_ev3q"></p>
<p>As for me, I’m currently building something new for the hackathon. While I enjoy working on projects, I’ve found even more fulfillment in developing infrastructure tools. I have a long-term goal of building a web-based Midnight playground where anyone can start writing Compact code and begin interacting with the Midnight testnet without needing to install anything. I hope to continue collaborating with the Midnight team and growing my skills—especially in zero-knowledge protocols—as I continue this journey.</p>]]></content:encoded>
            <category>midnight</category>
            <category>zero-knowledge</category>
            <category>hackathon</category>
            <category>blockchain</category>
            <category>compact</category>
        </item>
        <item>
            <title><![CDATA[Introducing the Compact Developer Tools]]></title>
            <link>https://docs.midnight.network/blog/compact-developer-tools</link>
            <guid>https://docs.midnight.network/blog/compact-developer-tools</guid>
            <pubDate>Fri, 01 Aug 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[We are proud to announce the launch of the Compact developer tools!]]></description>
            <content:encoded><![CDATA[<p>We are proud to announce the launch of the Compact developer tools!
Today we are launching <code>compact</code>, a command-line tool that is used for installing the Compact toolchain (e.g., the compiler),
keeping it up to date, and invoking the toolchain tools themselves.
The developer tools are now the "official" supported way to install and update the toolchain and to invoke other Compact tooling such as the Compact compiler.</p>
<p>To avoid confusion, we will make a careful distinction between the "Compact toolchain" and the "Compact developer tools".
The toolchain contains the compiler and will eventually contain other tooling that is specific to a particular version of the Compact language.
The developer tools include the updater tools and will eventually support other tools that work consistently across different versions of the language.
In the initial release, the Compact developer tools are primarily used to manage the installation of a Compact toolchain, but they will support additional tooling in the future.</p>
<p>There is no new release of the Compact toolchain yet, but you can already start using the new developer tools to install and invoke the current version of the compiler.</p>
<h1>The Old Toolchain Installation Method</h1>
<p>Before this new release, installing the Compact toolchain was a manual and tedious process:</p>
<ol>
<li>Developers had to download a ZIP file for a specific compiler version and platform architecture</li>
<li>They then had to extract it to a directory included in their PATH</li>
<li>On macOS, they would have to give permission to run the two different binaries (the compiler and the ZK-key generator).</li>
</ol>
<p>This process would have to be repeated each time there was a new Compact toolchain release.
Keeping multiple versions installed at once was also cumbersome and hard to manage.</p>
<h1>The New Toolchain Installation Method</h1>
<p>The new way to install the Compact toolchain starts with a one-time installation of the <code>compact</code> command-line tool.
Once installed, the developer tools can update themselves automatically.
To install, run the following command:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">curl --proto '=https' --tlsv1.2 -LsSf https://github.com/midnightntwrk/compact/releases/latest/download/compact-installer.sh | sh</span><br></span></code></pre></div></div>
<p>This command will download and run a shell script.
It will instruct you how to add the binary directory it uses to your <code>PATH</code> environment variable.</p>
<p>Once you've done this, the <code>compact</code> command line tool is available to use.
This tool has a number of useful subcommands that can be invoked.
For instance, to <strong>update</strong> the toolchain to the latest version, you will run the command:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact update</span><br></span></code></pre></div></div>
<p>The output will look something like this (on an Apple Silicon macOS machine, for instance):</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: aarch64-darwin -- 0.24.0 -- installed</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: aarch64-darwin -- 0.24.0 -- default.</span><br></span></code></pre></div></div>
<p>This subcommand will switch to the latest version of the toolchain available for your architecture.
As of today, this is version 0.24.0 which is reported by the tool.
The <code>compact</code> tool will download the toolchain artifacts if necessary (you will see a progress meter while it downloads).
If you have already downloaded the artifacts, the tool will simply switch the default version to be the latest version.</p>
<p>When there is a new Compact toolchain release, such as 0.25.0, you will use the same subcommand as above to update to that new version.</p>
<p>You can <strong>check</strong> if there is a new version available using the <code>check</code> subcommand like this:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact check</span><br></span></code></pre></div></div>
<p>If there is a new version available, you will see something like:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: aarch64-darwin -- Update Available -- 0.24.0</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: Latest version available: 0.25.0.</span><br></span></code></pre></div></div>
<p>This is reporting that you are on version 0.24.0 and that 0.25.0 is available.</p>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>You will not actually see this output until there is a new version available.
Instead, you will see that you are on the latest version:</p><div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: aarch64-darwin -- Up to date -- 0.24.0</span><br></span></code></pre></div></div></div></div>
<h1>Switching Toolchain Versions</h1>
<p>You can also "update" to any other available toolchain, including previous ones.
You can <strong>list</strong> all the versions available with the command:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact list</span><br></span></code></pre></div></div>
<p>The output will look something like this:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: available versions</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">→ 0.24.0 - x86_macos, aarch64_macos, x86_linux</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  0.23.0 - aarch64_macos, x86_linux</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  0.22.0 - x86_macos, x86_linux</span><br></span></code></pre></div></div>
<p>This is showing the different versions, and which platforms they are available for.
The arrow indicates the current default version, 0.24.0 in this case.
This default is whichever you have currently set as the default (via <code>compact update</code>).</p>
<p>You can pass the flag <code>--installed</code> (or <code>-i</code>) to <code>compact list</code> to see the versions that are actually downloaded locally:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact list --installed</span><br></span></code></pre></div></div>
<p>The output will probably look like this:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: installed versions</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">→ 0.24.0</span><br></span></code></pre></div></div>
<p>The update subcommand can be used with a specific version to switch to that version.
For example:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">$ compact update 0.23.0</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: aarch64-darwin -- 0.23.0 -- installed</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: aarch64-darwin -- 0.23.0 -- default.</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">$ compact list --installed</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact: installed versions</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  0.24.0</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">→ 0.23.0</span><br></span></code></pre></div></div>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>If you are on an Intel x86 macOS computer, you will not be able to update to version 0.23.0 because it was not available for that architecture.
You can try the same commands above with version 0.22.0 instead.</p></div></div>
<p>You can now switch back to the latest version with <code>compact update</code> or <code>compact update 0.24.0</code>.
This time, nothing will be downloaded because that version is already installed locally.</p>
<h1>Invoking the Compiler</h1>
<p>In addition to keeping the toolchain updated, the <code>compact</code> tool will also be the official supported way to invoke all the toolchain tools themselves.
For the time being, the only such tool is the compiler, but we will be building out more tools in the future.
The compiler can be invoked with the <code>compile</code> subcommand:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact compile &lt;contract file&gt; &lt;output directory&gt;</span><br></span></code></pre></div></div>
<p>This will use the current default version of the toolchain (the one indicated by the arrow in <code>compact list</code>).</p>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>If you are using macOS, you no longer have to explicitly give permission to run the compiler and ZK-key generation binaries.</p></div></div>
<p>You can override the default to use a specific (already installed) version by including the version number after a plus (<code>+</code>) sign like:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">compact compile +0.23.0 &lt;contract file&gt; &lt;output directory&gt;</span><br></span></code></pre></div></div>
<p>Looking ahead, we plan to remove the <code>compactc</code> executable.
Going forward, <code>compact compile</code> will become the standard way to invoke the Compact compiler.</p>
<h1>Built-in Help</h1>
<p>The <code>compact</code> tool and all of its subcommands have detailed help pages provided by the tools themselves.
You can see these in two different ways, either using the <code>help</code> subcommand or by using the <code>--help</code> flag.</p>
<p>For the Compact tool itself, you can invoke <code>compact help</code> or <code>compact --help</code> and you will see the exact same help page.
This page shows all the subcommands (<code>compile</code> is currently listed at the bottom of the page).
It also shows common command line options.</p>
<p>For a specific subcommand, such as <code>update</code>, you can see detailed help either via <code>compact help update</code> or <code>compact update --help</code>.
You can use the help pages to find all the subcommands and their options.</p>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>Because of the way that the tools are currently implemented, you cannot get compiler help via <code>compact help compile</code>.
Instead, you will have to use <code>compact compile --help</code>.</p></div></div>
<h1>Versions</h1>
<p>The developer tools are separately versioned from the toolchain (i.e., compiler) itself.
This is because the tools are capable of managing multiple versions of the toolchain.</p>
<p>You can see the version of the developer tools with <code>compact --version</code>.
That should currently be 0.1.0.
You can also see the version of subcommands like <code>compact update --version</code>.
Currently, built-in subcommands (that is, ones implemented by the developer tools, not the toolchain) all have the same version as the developer tools.</p>
<p>You can find the version of the toolchain with <code>compact list -i</code> or <code>compact check</code> (though the latter will make an Internet check to see if there is a new version).
You can also find it from the compiler via <code>compact compile --version</code>.
This should currently be 0.24.0 if you are on the latest version.</p>
<p>As before, the Compact language itself is versioned separately from the toolchain.
You can find this from the compiler via <code>compact compile --language-version</code>.</p>
<h1>Keeping the Developer Tools Up to Date</h1>
<p>Once they are installed, the developer tools are capable of keeping themselves up to date.
You can check for updates with <code>compact self check</code>, and you can update to the latest version of the tool with <code>compact self update</code>.</p>
<p>We have not provided a way to go back to a previous version of the developer tools themselves, because you generally won't need to do that.
You don't necessarily have to update the developer tools themselves all the time, either.
You <strong>will</strong> however want to update the developer tools themselves when we make more developer tools available.
For example, a future release will include a Compact formatter available via a <code>compact format</code> subcommand.
You will need to update the developer tools themselves to get access to the formatter tool.
We will always announce such updates in the release notes for both the toolchain and the developer tools.</p>
<h1>How it Works</h1>
<p>The tool is currently a simple one, but its architecture is flexible and it will eventually support many more different developer tools.
If you look at the help pages, you will see that the tools take a <code>--directory</code> command-line flag which tells them where to find the toolchain.
By default this will be the directory <code>.compact</code> in your home directory.</p>
<p>If you look in that directory, you will see two subdirectories: <code>bin</code> and <code>versions</code>.
The <code>versions</code> subdirectory contains separate subdirectories for each installed version, which contain the toolchain artifacts for that version.
The <code>bin</code> subdirectory represents the default version, which is just a symbolic link to one of the installed versions.</p>
<p>The command line tool is capable of checking for updates and downloading and unzipping toolchain artifacts if necessary.
Switching between installed versions just changes the <code>bin</code> symbolic link to point to a different installed toolchain.
Invoking the toolchain itself just invokes the corresponding default executables in the <code>bin</code> directory, or for a specific version if necessary.</p>]]></content:encoded>
            <category>compact</category>
        </item>
        <item>
            <title><![CDATA[Learning Web3 from the Ground Up - Understanding Ethereum]]></title>
            <link>https://docs.midnight.network/blog/web3-intro-understanding-ethereum</link>
            <guid>https://docs.midnight.network/blog/web3-intro-understanding-ethereum</guid>
            <pubDate>Fri, 25 Jul 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[This past week, I started the SheFi course—a program designed to help women dive deeper into Web3 and decentralized finance. One of the first topics it covered was Ethereum: what it is, why it matters, and how it fits into the broader decentralized ecosystem.]]></description>
            <content:encoded><![CDATA[<p>This past week, I started the SheFi course—a program designed to help women dive deeper into Web3 and decentralized finance. One of the first topics it covered was Ethereum: what it is, why it matters, and how it fits into the broader decentralized ecosystem.</p>
<p>To build on what I learned there, I read two excellent overviews: <a href="https://medium.com/the-coinbase-blog/a-beginners-guide-to-ethereum-46dd486ceecf" target="_blank" rel="noopener noreferrer">“A Beginner’s Guide to Ethereum” from Coinbase</a> and <a href="https://medium.com/@oneofthemanymatts/getting-up-to-speed-on-ethereum-63ed28821bbe" target="_blank" rel="noopener noreferrer">“Getting Up to Speed on Ethereum” by Matt</a>. Both helped frame Ethereum not just as a cryptocurrency, but as a foundational platform for Web3 applications. Here’s what stood out and why Ethereum remains vital in a rapidly evolving cryptocurrency ecosystem.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="ethereum-in-a-nutshell">Ethereum in a Nutshell<a href="https://docs.midnight.network/blog/web3-intro-understanding-ethereum#ethereum-in-a-nutshell" class="hash-link" aria-label="Direct link to Ethereum in a Nutshell" title="Direct link to Ethereum in a Nutshell">​</a></h2>
<p>Ethereum is a decentralized computing platform. Think of it like a global computer that anyone can access, but no single person or organization controls it. This is possible because Ethereum is powered by a blockchain—a network of nodes around the world that maintain and validate a shared record of activity. What makes Ethereum special is that it doesn’t just store transactions; it can also run applications. These apps are powered by smart contracts, which are programs deployed to the blockchain that execute exactly as written.</p>
<p>The Ethereum network is designed to be open, programmable, and permissionless. That means anyone can build on it, and no one needs approval to use it. This structure has enabled Ethereum to become the foundation for a wide range of decentralized applications (dApps), including financial tools such as lending protocols and stablecoins, as well as NFT marketplaces and decentralized identity systems.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="why-ethereum-matters">Why Ethereum Matters<a href="https://docs.midnight.network/blog/web3-intro-understanding-ethereum#why-ethereum-matters" class="hash-link" aria-label="Direct link to Why Ethereum Matters" title="Direct link to Why Ethereum Matters">​</a></h2>
<p>Ethereum matters because it transforms the internet from a read-write platform into a read-write-execute environment. That is, it enables not just the sharing of information, but also the execution of logic—without relying on centralized intermediaries. This shift opens the door to creating systems that are more transparent, resilient, and accessible.</p>
<p>One of Ethereum’s most significant breakthroughs is that it allows developers to write and deploy their own logic onto the network through smart contracts. These contracts can define rules, manage value, and interact with other contracts—all while being trustless and tamper-proof. Once deployed, smart contracts can’t be changed or censored, which means users can interact with them without worrying about backend changes or manipulation.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="ether-the-fuel-of-the-ethereum-network">Ether: The Fuel of the Ethereum Network<a href="https://docs.midnight.network/blog/web3-intro-understanding-ethereum#ether-the-fuel-of-the-ethereum-network" class="hash-link" aria-label="Direct link to Ether: The Fuel of the Ethereum Network" title="Direct link to Ether: The Fuel of the Ethereum Network">​</a></h2>
<p>Ether (ETH) is the native currency of Ethereum. While many people think of ETH primarily as a cryptocurrency—something you can buy, sell, or hold—its most important role as the fuel for computation. Every operation on the Ethereum network costs gas, and ETH is what you use to pay for it. This is a crucial aspect of Ethereum’s security and incentive structure, ensuring that resources are utilized responsibly and the network remains resilient to spam.</p>
<p>When you send a transaction, deploy a smart contract, or interact with a dApp, you’re paying for the network to process that request. The more complex the operation, the more gas it consumes. Gas prices fluctuate based on network demand, and users can choose how much to pay to incentivize faster confirmation. This dynamic creates a market for block space, encouraging developers to write more efficient code.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="the-power-of-smart-contracts">The Power of Smart Contracts<a href="https://docs.midnight.network/blog/web3-intro-understanding-ethereum#the-power-of-smart-contracts" class="hash-link" aria-label="Direct link to The Power of Smart Contracts" title="Direct link to The Power of Smart Contracts">​</a></h2>
<p>Smart contracts are at the heart of what makes Ethereum powerful. These are self-executing programs that live on the blockchain and enforce rules automatically. You don’t need to trust a company or a person; you can trust the code. Smart contracts enable a wide range of use cases, including lending platforms like Aave, decentralized exchanges like Uniswap, and even games like Axie Infinity.</p>
<p>Once a smart contract is deployed, it’s public and verifiable. Anyone can inspect the code, interact with it, and build on top of it. This openness fosters a culture of composability—developers can plug into existing contracts, remix them, or build entirely new systems that work together. This is why Ethereum is often referred to as a “LEGO set for financial and digital tools.”</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="ethereum-today-and-tomorrow">Ethereum Today and Tomorrow<a href="https://docs.midnight.network/blog/web3-intro-understanding-ethereum#ethereum-today-and-tomorrow" class="hash-link" aria-label="Direct link to Ethereum Today and Tomorrow" title="Direct link to Ethereum Today and Tomorrow">​</a></h2>
<p>Ethereum has come a long way since its launch in 2015. It’s no longer just a niche experiment; it’s the most widely used smart contract platform in the world. However, with that growth has come challenges. Network congestion and high gas fees have made it difficult for some users to interact with dApps affordably. To address these issues, Ethereum has undergone significant upgrades, most notably the transition from proof-of-work to proof-of-stake, known as “The Merge.”</p>
<p>Proof-of-stake is a consensus mechanism that allows Ethereum to secure the network using validators who lock up ETH rather than relying on energy-intensive mining. This shift has dramatically reduced Ethereum’s energy usage and paved the way for future scaling efforts, such as sharding and rollups. Rollups are a technique for batching many transactions into a single proof, which can then be submitted to the Ethereum mainnet. This helps scale the network without sacrificing security or decentralization.</p>
<p>The Ethereum ecosystem is also growing through layer 2 networks, which are built on top of Ethereum and handle transactions off-chain while still relying on Ethereum for settlement and security. Projects like Arbitrum, Optimism, and zkSync are working to make Ethereum faster and cheaper for everyday users.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="how-ethereum-connects-with-midnight">How Ethereum Connects with Midnight<a href="https://docs.midnight.network/blog/web3-intro-understanding-ethereum#how-ethereum-connects-with-midnight" class="hash-link" aria-label="Direct link to How Ethereum Connects with Midnight" title="Direct link to How Ethereum Connects with Midnight">​</a></h2>
<p>While Ethereum serves as the foundational smart contract platform for much of Web3, Midnight is designed to complement and extend its capabilities—especially in terms of privacy.</p>
<p>In practical terms, this means that developers who are comfortable with the Ethereum ecosystem —its wallets, development environments, and smart contract models—can interact with Midnight using similar patterns. For example, Midnight supports a Solidity-like programming approach through its Compact smart contract language, which compiles to verifiable circuits that enforce privacy constraints. This allows developers to build private versions of applications, such as voting systems, token transfers, and identity verification, that would typically be public on the Ethereum blockchain.</p>
<p>Ethereum offers programmability and decentralization, but by default, all on-chain transactions are transparent. Midnight fills in the missing privacy layer. You could imagine a workflow where public assets and smart contracts live on Ethereum, but sensitive data or confidential logic is handled by Midnight—bridging the two networks for richer, privacy-preserving applications.</p>
<p>Together, Ethereum and Midnight can be seen as complementary parts of a broader decentralized stack: Ethereum provides openness and composability, while Midnight adds selective disclosure and private computation, enabling developers to build secure and compliant applications in ways that Ethereum alone can’t.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/web3-intro-understanding-ethereum#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>Ethereum isn’t just a cryptocurrency—it’s a platform for building a new kind of internet. By combining programmable logic with decentralized infrastructure, Ethereum enables applications that are open, transparent, and user-owned. It’s the foundation of decentralized finance (DeFi), non-fungible tokens (NFTs), and numerous experiments in online interaction.</p>
<p>For me, starting with SheFi and reading through these resources helped ground my understanding of what Ethereum is and why it matters. It’s not just about buying coins—it’s about reshaping how we think about trust, ownership, and value in a digital world.</p>
<p>I’ll be back next week as I continue diving into the core building blocks of Web3. If you’re just starting out too, I highly recommend giving <a href="https://medium.com/the-coinbase-blog/a-beginners-guide-to-ethereum-46dd486ceecf" target="_blank" rel="noopener noreferrer">Coinbase’s primer</a> and <a href="https://medium.com/@oneofthemanymatts/getting-up-to-speed-on-ethereum-63ed28821bbe" target="_blank" rel="noopener noreferrer">Matt’s breakdown</a> a read.</p>]]></content:encoded>
            <category>intro</category>
            <category>blockchain</category>
            <category>beginner</category>
            <category>web3</category>
        </item>
        <item>
            <title><![CDATA[Midnight’s First Step Toward Open Source – Say Hello to example-counter and example-bboard]]></title>
            <link>https://docs.midnight.network/blog/open-sourcing-examples</link>
            <guid>https://docs.midnight.network/blog/open-sourcing-examples</guid>
            <pubDate>Fri, 11 Jul 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[Midnight's Developer Relations team is actively shaping how developers learn, build, and experiment with privacy-first apps. In this post, we take a closer look at how DevRel is lowering the barrier to entry for new builders, supporting open-source tools, and helping the community navigate the unique challenges of developing on a privacy-preserving blockchain.]]></description>
            <content:encoded><![CDATA[<p>Midnight's Developer Relations team is actively shaping how developers learn, build, and experiment with privacy-first apps. In this post, we take a closer look at how DevRel is lowering the barrier to entry for new builders, supporting open-source tools, and helping the community navigate the unique challenges of developing on a privacy-preserving blockchain.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="from-zip-files-to-github-opening-the-gates">From ZIP Files to GitHub: Opening the Gates<a href="https://docs.midnight.network/blog/open-sourcing-examples#from-zip-files-to-github-opening-the-gates" class="hash-link" aria-label="Direct link to From ZIP Files to GitHub: Opening the Gates" title="Direct link to From ZIP Files to GitHub: Opening the Gates">​</a></h2>
<p>Midnight is entering a new phase in our developer journey. Until now, developers had to download ZIP files from our documentation site just to get started. No versioning, no collaboration, no visibility.</p>
<p>That changes today.</p>
<p>The DevRel team is proud to share our first two open source example repositories: <a href="https://github.com/midnightntwrk/example-counter" target="_blank" rel="noopener noreferrer"><code>example-counter</code></a> and <a href="https://github.com/midnightntwrk/example-bboard" target="_blank" rel="noopener noreferrer"><code>example-bboard</code></a>. These projects are more than demos. They’re intentionally scoped, composable references designed to help developers explore what’s possible with Midnight’s privacy-first architecture.</p>
<p>While our broader engineering team continues to open source core protocol components, these repos mark a different kind of milestone: showing others how to build on top of Midnight’s foundation. With versioned code and public visibility, developers can now fork, remix, and build with confidence.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="a-simple-contract-with-a-purpose">A Simple Contract With a Purpose<a href="https://docs.midnight.network/blog/open-sourcing-examples#a-simple-contract-with-a-purpose" class="hash-link" aria-label="Direct link to A Simple Contract With a Purpose" title="Direct link to A Simple Contract With a Purpose">​</a></h2>
<p><a href="https://github.com/midnightntwrk/example-counter" target="_blank" rel="noopener noreferrer"><code>example-counter</code></a> is a minimal dApp that introduces the fundamentals of building on Midnight. It walks through the full development flow:</p>
<ul>
<li>Writing a Compact smart contract</li>
<li>Running and compiling locally</li>
<li>Using public state</li>
<li>Deploying to the Midnight testnet</li>
<li>Sending transactions</li>
</ul>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">pragma</span><span class="token plain"> language_version &gt;= </span><span class="token number">0.16</span><span class="token plain">;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> CompactStandardLibrary;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// public state</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> round: </span><span class="token class-name">Counter</span><span class="token plain">;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// transition function changing public state</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">circuit</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">increment</span><span class="token plain">(): [] {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    round.</span><span class="token function" style="color:rgb(80, 250, 123)">increment</span><span class="token plain">(</span><span class="token number">1</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>It’s designed to build foundational literacy in Compact and Midnight’s dev tooling. It’s a simple but powerful way to learn Compact and become familiar with the development flow on Midnight.</p>
<p>Think of it as a <code>“Hello, World”</code> for privacy-enhancing dApp development.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="enforcing-rules-without-revealing-identity">Enforcing Rules Without Revealing Identity<a href="https://docs.midnight.network/blog/open-sourcing-examples#enforcing-rules-without-revealing-identity" class="hash-link" aria-label="Direct link to Enforcing Rules Without Revealing Identity" title="Direct link to Enforcing Rules Without Revealing Identity">​</a></h2>
<p><a href="https://github.com/midnightntwrk/example-bboard" target="_blank" rel="noopener noreferrer"><code>example-bboard</code></a> goes further. It simulates a public bulletin board, where users can post messages, and only the original author can remove their own post.</p>
<p>It’s a small app that teaches a big concept: Midnight enables identity-aware rules without revealing identity.</p>
<p>This repo covers how to:</p>
<ul>
<li>Combine public and private state in a Compact contract</li>
<li>Use zero-knowledge proofs to enforce permissions</li>
<li>Validate ownership locally without exposing identity</li>
<li>Design UI flows that respect user privacy by default</li>
</ul>
<p>It’s especially useful for teams exploring anonymous messaging, private voting, or access control without identity leakage.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-it-means-to-build-in-the-open">What It Means to Build in the Open<a href="https://docs.midnight.network/blog/open-sourcing-examples#what-it-means-to-build-in-the-open" class="hash-link" aria-label="Direct link to What It Means to Build in the Open" title="Direct link to What It Means to Build in the Open">​</a></h2>
<p>Speaking personally, I’ve spent most of my DevRel career working in open source. But this is the first time I’ve been part of a company transitioning into it.</p>
<p>Open sourcing code isn’t just a technical milestone. It’s a culture shift. Releasing code publicly changes how a team thinks and operates.</p>
<p>It means moving from perfectionism to progress. From private Slack threads to public GitHub Issues. From “we’ll fix it internally” to “anyone can help improve it.”</p>
<p>It’s a commitment to building alongside others, not just for them.</p>
<p>These first example repos mark the beginning of that shift for Midnight. They’re designed to help others learn by doing and to demonstrate that <strong>privacy and collaboration can coexist</strong>.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="docs-are-up-next">Docs Are Up Next<a href="https://docs.midnight.network/blog/open-sourcing-examples#docs-are-up-next" class="hash-link" aria-label="Direct link to Docs Are Up Next" title="Direct link to Docs Are Up Next">​</a></h2>
<p>We’re also preparing to open source our [Midnight Docs repo(<a href="https://github.com/midnightntwrk/midnight-docs" target="_blank" rel="noopener noreferrer">https://github.com/midnightntwrk/midnight-docs</a>)].</p>
<p>This is a major step toward transparency, trust, and co-creation. We want the documentation to grow alongside the ecosystem, which means making it easier for contributors to suggest improvements, fix issues, and help shape the developer experience.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="whats-next">What’s Next?<a href="https://docs.midnight.network/blog/open-sourcing-examples#whats-next" class="hash-link" aria-label="Direct link to What’s Next?" title="Direct link to What’s Next?">​</a></h2>
<p>This is just the start. In the coming months, the DevRel team will:</p>
<ul>
<li>Publish more example dApps (and build them from scratch in the open)</li>
<li>Create reusable components and templates</li>
<li>Contribute improvements upstream to core tooling</li>
<li>Document real-world use cases for easier onboarding</li>
</ul>
<p>We’re building in the open, for those who want to build with us.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="explore-the-code">Explore the Code<a href="https://docs.midnight.network/blog/open-sourcing-examples#explore-the-code" class="hash-link" aria-label="Direct link to Explore the Code" title="Direct link to Explore the Code">​</a></h2>
<ul>
<li>
<p><a href="https://github.com/midnightntwrk/example-counter" target="_blank" rel="noopener noreferrer"><code>example-counter</code></a></p>
</li>
<li>
<p><a href="https://github.com/midnightntwrk/example-bboard" target="_blank" rel="noopener noreferrer"><code>example-bboard</code></a></p>
</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="join-the-conversation">Join the Conversation<a href="https://docs.midnight.network/blog/open-sourcing-examples#join-the-conversation" class="hash-link" aria-label="Direct link to Join the Conversation" title="Direct link to Join the Conversation">​</a></h2>
<p>We’d love to hear your feedback, contributions, and ideas. Drop into <a href="https://discord.gg/midnightnetwork" target="_blank" rel="noopener noreferrer">Discord</a>, open an issue, or fork a repo and show us what you’ve built.</p>]]></content:encoded>
            <category>dapp</category>
            <category>beginner</category>
            <category>web3</category>
            <category>intro</category>
        </item>
        <item>
            <title><![CDATA[Learning Web3 from the Ground Up - What Are Zero-Knowledge Proofs (ZKPs)?]]></title>
            <link>https://docs.midnight.network/blog/web3-intro-what-are-zkps</link>
            <guid>https://docs.midnight.network/blog/web3-intro-what-are-zkps</guid>
            <pubDate>Thu, 10 Jul 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[Zero-knowledge proofs let you prove you know something without revealing what you know. Here's how this cryptographic breakthrough is revolutionizing privacy in Web3 applications.]]></description>
            <content:encoded><![CDATA[<p>Zero-knowledge proofs let you prove you know something without revealing what you know. Here's how this cryptographic breakthrough is revolutionizing privacy in Web3 applications.</p>
<p>As I continue exploring the foundations of Web3, last week I focused on <a href="https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact" target="_blank" rel="noopener noreferrer">Smart Contracts and Compact</a>—Midnight’s purpose-built smart contract language. Together, these two elements form the foundation for defining and enforcing logic in privacy-preserving applications.</p>
<p>But logic alone isn’t enough in a decentralized system, especially when privacy is a priority. That’s where zero-knowledge proofs (ZKPs) come in.</p>
<p>ZKPs enable the verification of a statement's truth without revealing the underlying justification. They let applications enforce smart contract rules while keeping sensitive data private—an essential capability for building systems that balance trust, utility, and confidentiality.</p>
<p>In this post, I’ll break down what zero-knowledge proofs are, how they work, and how Midnight uses them to support privacy, compliance, and secure dApp development.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-are-zero-knowledge-proofs">What Are Zero-Knowledge Proofs?<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#what-are-zero-knowledge-proofs" class="hash-link" aria-label="Direct link to What Are Zero-Knowledge Proofs?" title="Direct link to What Are Zero-Knowledge Proofs?">​</a></h2>
<p>A zero-knowledge proof allows someone (the prover) to prove to another party (the verifier) that a specific statement is true, without revealing any additional information beyond the fact itself.</p>
<p>Think of it like this: instead of handing over your complete ID to prove you're over 18, you could use a ZKP to prove you're above the age threshold, without disclosing your exact birthday, name, or ID number.</p>
<p>This cryptographic technique is built on three foundational properties:</p>
<ul>
<li>Completeness: If the statement is true, the prover can convince the verifier that it is true.</li>
<li>Soundness: If the statement is false, no dishonest prover can convince the verifier that it is true.</li>
<li>Zero-knowledge: The verifier learns nothing beyond the truth of the statement.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="interactive-vs-non-interactive-proofs">Interactive vs. Non-Interactive Proofs<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#interactive-vs-non-interactive-proofs" class="hash-link" aria-label="Direct link to Interactive vs. Non-Interactive Proofs" title="Direct link to Interactive vs. Non-Interactive Proofs">​</a></h2>
<p>Early ZKPs required a back-and-forth exchange between prover and verifier—these are called interactive proofs. But in decentralized systems, interaction isn’t always feasible. That’s where non-interactive proofs come in.</p>
<p>Non-interactive ZKPs let a prover generate a single proof that anyone can verify at any time, with no additional input. Protocols like zk-SNARKs and zk-STARKs make this possible, often using techniques like the <a href="https://www.zkdocs.com/docs/zkdocs/protocol-primitives/fiat-shamir/" target="_blank" rel="noopener noreferrer">Fiat–Shamir heuristic</a> to simulate interaction using cryptographic hashes.</p>
<p>This makes non-interactive ZKPs ideal for blockchains, where proofs need to be public, reusable, and verifiable by anyone.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="types-of-zero-knowledge-proof-systems">Types of Zero-Knowledge Proof Systems<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#types-of-zero-knowledge-proof-systems" class="hash-link" aria-label="Direct link to Types of Zero-Knowledge Proof Systems" title="Direct link to Types of Zero-Knowledge Proof Systems">​</a></h2>
<p>Different ZKP systems come with different tradeoffs in terms of size, speed, and trust requirements:</p>
<ul>
<li>Groth16 (zk-SNARK): Very small proofs, fast verification, but requires a trusted setup per circuit.</li>
<li>Plonk (zk-SNARK): Slightly larger proofs, slower verification, but only needs a one-time universal setup.</li>
<li>Bulletproofs: No trusted setup, but proof size grows with complexity.</li>
<li>zk-STARKs: Transparent (no trusted setup), post-quantum secure, but with large proof sizes.</li>
</ul>
<p>Midnight utilizes a Plonk-derived system called <a href="https://electriccoin.co/blog/explaining-halo-2/" target="_blank" rel="noopener noreferrer">Halo 2</a>, which supports recursive proofs and eliminates trusted setup in specific configurations, making it an ideal choice for privacy-preserving dApps that require both flexibility and scalability.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="how-do-zkps-work">How Do ZKPs Work?<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#how-do-zkps-work" class="hash-link" aria-label="Direct link to How Do ZKPs Work?" title="Direct link to How Do ZKPs Work?">​</a></h2>
<p>The idea behind zero-knowledge proofs might seem almost magical: one party (the prover) can prove to another (the verifier) that a statement is true, without revealing the <em>why</em>. But this is no sleight of hand—it’s grounded in rigorous cryptography and clever use of mathematical structures.</p>
<p>At the heart of a ZKP is a “statement” that the prover wants to validate without revealing the underlying secret. That statement gets encoded into a circuit, which represents the logical rules or constraints that need to be satisfied. The prover then uses a special cryptographic key, known as the proving key, along with the secret input (called the witness), to generate a succinct proof. The verifier can then check this proof using a verifying key, confirming the rules were followed, without ever seeing the private input.</p>
<p>This process makes ZKPs ideal for privacy-preserving smart contracts. In traditional systems, all data must be made public to verify that an action is legitimate. But with ZKPs, we can shift the trust model: rather than inspecting the data itself, the verifier checks a cryptographic proof that the data complies with the rules.</p>
<p>Midnight takes this further by embedding ZKPs directly into the structure and execution of smart contracts. In Midnight, the verification key of a compiled Compact smart contract becomes the identity of that contract on-chain. When users interact with it, they’re not just calling a function—they’re submitting proof that they've complied with the logic defined by that contract, without revealing their inputs.</p>
<p>Here's a high-level flow of how this works:</p>
<ol>
<li>Setup: A circuit is created. A proving key and a verifying key are generated.</li>
<li>Proof Generation: The prover uses their secret (a witness) and the proving key to generate a cryptographic proof.</li>
<li>Verification: The verifier uses the verifying key and the proof to confirm the statement is true, without learning anything else.</li>
</ol>
<p>This process ensures that the smart contract logic is executed exactly as intended, without exposing the underlying data.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="midnights-approach-to-zkps">Midnight’s Approach to ZKPs<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#midnights-approach-to-zkps" class="hash-link" aria-label="Direct link to Midnight’s Approach to ZKPs" title="Direct link to Midnight’s Approach to ZKPs">​</a></h2>
<p>Midnight utilizes zero-knowledge proofs not only for theoretical privacy but also to enable real-world compliance and usability. Let’s walk through a few key elements of how ZKPs show up on the Midnight network:</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="confidential-transactions">Confidential Transactions<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#confidential-transactions" class="hash-link" aria-label="Direct link to Confidential Transactions" title="Direct link to Confidential Transactions">​</a></h3>
<p>Users can transfer assets on Midnight without revealing the sender, receiver, or amount. ZKPs are used to prove that a transaction is valid (i.e., no double spending, balances add up) without revealing specific details. This ensures both privacy and integrity.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="selective-disclosure-and-compliance">Selective Disclosure and Compliance<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#selective-disclosure-and-compliance" class="hash-link" aria-label="Direct link to Selective Disclosure and Compliance" title="Direct link to Selective Disclosure and Compliance">​</a></h3>
<p>Midnight supports “shielded” and “unshielded” data modes. You can keep your transaction data private by default, but also selectively disclose specific information (via a ZKP or viewing key) to a regulator, auditor, or another authorized party.</p>
<p>This enables use cases like “prove I’m a citizen of Country X” or “prove I’m not sanctioned” without revealing your complete identity or wallet history.</p>
<p>For more information about selective disclosure, read my overview <a href="https://docs.midnight.network/blog/web3-intro-selective-disclosure" target="_blank" rel="noopener noreferrer">here</a>!</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="decentralized-identity">Decentralized Identity<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#decentralized-identity" class="hash-link" aria-label="Direct link to Decentralized Identity" title="Direct link to Decentralized Identity">​</a></h3>
<p>ZKPs are key to proving facts about identity without revealing full credentials. Midnight supports workflows like proving KYC status (“A trusted provider has verified me”) using a proof, not a document.</p>
<p>This allows users to interact with services that require identity checks—such as exchanges or marketplaces—without exposing sensitive personal data.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="scalability-via-recursive-proofs">Scalability via Recursive Proofs<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#scalability-via-recursive-proofs" class="hash-link" aria-label="Direct link to Scalability via Recursive Proofs" title="Direct link to Scalability via Recursive Proofs">​</a></h3>
<p>ZKPs also compress large computations into tiny proofs. That makes them ideal for batching transactions or reducing blockchain bloat. Midnight’s use of recursive proofs allows smart contracts and transactions to scale more efficiently than traditional blockchains.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="real-world-use-cases-on-midnight">Real-World Use Cases on Midnight<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#real-world-use-cases-on-midnight" class="hash-link" aria-label="Direct link to Real-World Use Cases on Midnight" title="Direct link to Real-World Use Cases on Midnight">​</a></h2>
<p>Some examples of ZKP-powered applications on Midnight are:</p>
<ul>
<li>Voting: Prove you’re eligible to vote and that your vote was counted, without revealing your identity or your vote.</li>
<li>Whistleblowing: Submit a report anonymously, proving only that you’re authorized (e.g., an employee).</li>
<li>Private Asset Swaps: Use Midnight’s Zswap to exchange tokens confidentially without revealing terms or participants.</li>
<li>KYC Onboarding: Prove compliance status to a platform or dApp without exposing documents.</li>
<li>Tokenized Real-World Assets: Keep ownership private while maintaining auditable transfer records. These use cases show how ZKPs on Midnight turn privacy into a practical feature, not just a theoretical promise. They lay the groundwork for a future where developers can build secure, compliant, and data-protecting applications by default.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/web3-intro-what-are-zkps#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>Zero-knowledge proofs bring trust to trustless systems. They make it possible to prove something is true—like compliance with laws, asset ownership, or user eligibility—without revealing any sensitive details.</p>
<p>Midnight makes ZKPs more than theoretical. They’re deeply integrated into its smart contract architecture, transaction model, and developer tooling. Paired with the Compact language (covered in last week’s post), they create a robust foundation for building privacy-first decentralized applications.</p>
<p>If you’re interested in learning more, check out:</p>
<ul>
<li><a href="https://docs.midnight.network/academy/module-3" target="_blank" rel="noopener noreferrer">Midnight Developer Academy Module 3</a></li>
<li><a href="https://docs.midnight.network/learn/understanding-midnights-technology/zero-knowledge-proofs" target="_blank" rel="noopener noreferrer">Zero-Knowledge Proofs</a> in the <a href="https://docs.midnight.network/" target="_blank" rel="noopener noreferrer">Midnight Docs</a></li>
</ul>
<p>And if you’ve come across excellent ZKP explanations or tutorials, I’d love to hear about them—please share your favorites!</p>
<p>See you next week as I continue to unpack the core ideas that power privacy in Web3!</p>]]></content:encoded>
            <category>intro</category>
            <category>blockchain</category>
            <category>beginner</category>
            <category>web3</category>
        </item>
        <item>
            <title><![CDATA[How to query the blockchain?]]></title>
            <link>https://docs.midnight.network/blog/query-the-blockchain</link>
            <guid>https://docs.midnight.network/blog/query-the-blockchain</guid>
            <pubDate>Fri, 04 Jul 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[The Midnight blockchain introduces a novel approach to data privacy and compliance in decentralized applications (DApps), leveraging zero-knowledge proofs (ZKPs) and a dual-token system.]]></description>
            <content:encoded><![CDATA[<p>The Midnight blockchain introduces a novel approach to data privacy and compliance in decentralized applications (DApps), leveraging zero-knowledge proofs (ZKPs) and a dual-token system.</p>
<p>This article provides a comprehensive guide on connecting to the Midnight blockchain, and you will learn how to listen for new blocks and extract data from these blocks using the Polkadot API and the various methods it provides to connect to Substrate-based blockchains, including Midnight.</p>
<blockquote>
<p>See the code in action <a href="https://github.com/claudebarde/midnight-mempool" target="_blank" rel="noopener noreferrer">in this repo</a>, a simple app that watches the Midnight mempool!</p>
</blockquote>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="understanding-midnights-architecture"><strong>Understanding Midnight's Architecture</strong><a href="https://docs.midnight.network/blog/query-the-blockchain#understanding-midnights-architecture" class="hash-link" aria-label="Direct link to understanding-midnights-architecture" title="Direct link to understanding-midnights-architecture">​</a></h2>
<p>Midnight is designed to address the limitations of traditional blockchains regarding data privacy. It does so through:</p>
<ul>
<li><a href="https://www.google.com/url?q=https://docs.midnight.network/learn/glossary%23zero-knowledge-proof-zkp&amp;sa=D&amp;source=docs&amp;ust=1751469460016839&amp;usg=AOvVaw0GBVs0ZrYgq8Eggpat3hpn" target="_blank" rel="noopener noreferrer">Zero-Knowledge Proofs (ZKPs)</a>: Enable verification of data without revealing the data itself.</li>
<li><a href="https://www.google.com/url?q=https://docs.midnight.network/learn/introduction/native-token/about-dust&amp;sa=D&amp;source=docs&amp;ust=1751469460016900&amp;usg=AOvVaw1yvGgY8EL1yJB3W61g1LQq" target="_blank" rel="noopener noreferrer">Dual-Token System</a>: Uses NIGHT (unshielded) for governance and DUST (shielded) for transaction payments.</li>
<li><a href="https://www.google.com/url?q=https://docs.midnight.network/develop/reference/compact/&amp;sa=D&amp;source=docs&amp;ust=1751469460016917&amp;usg=AOvVaw0DQvxLVoHOxx7vIhTKZFnG" target="_blank" rel="noopener noreferrer">Compact Smart Contract Language</a>: A TypeScript-based language facilitating the development of privacy-preserving smart contracts.</li>
</ul>
<p>These features make Midnight suitable for applications requiring data confidentiality, such as healthcare records, financial transactions, and identity verification.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="connecting-to-the-midnight-blockchain">Connecting to the Midnight Blockchain<a href="https://docs.midnight.network/blog/query-the-blockchain#connecting-to-the-midnight-blockchain" class="hash-link" aria-label="Direct link to Connecting to the Midnight Blockchain" title="Direct link to Connecting to the Midnight Blockchain">​</a></h2>
<p>The <a href="https://polkadot.js.org/docs/api/" target="_blank" rel="noopener noreferrer"><code>@polkadot/api</code></a> library provides a WebSocket provider (WsProvider) to connect to Substrate-based blockchains, which includes Midnight.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="installation">Installation<a href="https://docs.midnight.network/blog/query-the-blockchain#installation" class="hash-link" aria-label="Direct link to Installation" title="Direct link to Installation">​</a></h3>
<p>First, ensure you have the necessary packages installed:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">npm</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">install</span><span class="token plain"> @polkadot/api  </span><br></span></code></pre></div></div>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="establishing-a-connection">Establishing a Connection<a href="https://docs.midnight.network/blog/query-the-blockchain#establishing-a-connection" class="hash-link" aria-label="Direct link to Establishing a Connection" title="Direct link to Establishing a Connection">​</a></h3>
<p>Use the WsProvider to connect to a Midnight node:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword module" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> </span><span class="token imports punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token imports"> </span><span class="token imports maybe-class-name">ApiPromise</span><span class="token imports punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token imports"> </span><span class="token imports maybe-class-name">WsProvider</span><span class="token imports"> </span><span class="token imports punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token keyword module" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'@polkadot/api'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// Define the WebSocket endpoint for the Midnight node  </span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> wsProvider </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">WsProvider</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'wss://rpc.testnet.midnight.network'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// Create the API instance  </span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> api </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">await</span><span class="token plain"> </span><span class="token maybe-class-name">ApiPromise</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">create</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">provider</span><span class="token operator">:</span><span class="token plain"> wsProvider </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span></code></pre></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="listening-for-new-blocks">Listening for New Blocks<a href="https://docs.midnight.network/blog/query-the-blockchain#listening-for-new-blocks" class="hash-link" aria-label="Direct link to Listening for New Blocks" title="Direct link to Listening for New Blocks">​</a></h2>
<p>Once connected, you can subscribe to new block headers to listen for new blocks:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">api</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">rpc</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">chain</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">subscribeNewHeads</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter">lastHeader</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token arrow operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token console class-name">console</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">log</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">New block #</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">lastHeader</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">number</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string string" style="color:rgb(255, 121, 198)"> has been added</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span></code></pre></div></div>
<p>This function logs the block number each time a new block is added to the chain.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="extracting-data-from-blocks">Extracting Data from Blocks<a href="https://docs.midnight.network/blog/query-the-blockchain#extracting-data-from-blocks" class="hash-link" aria-label="Direct link to Extracting Data from Blocks" title="Direct link to Extracting Data from Blocks">​</a></h2>
<p>To extract detailed information from each new block, such as transactions and events, you can use the following approach:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">api</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">rpc</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">chain</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">subscribeNewHeads</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">async</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter">lastHeader</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token arrow operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> blockHash </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">await</span><span class="token plain"> api</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">rpc</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">chain</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">getBlockHash</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">lastHeader</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">number</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> signedBlock </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">await</span><span class="token plain"> api</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">rpc</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">chain</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">getBlock</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">blockHash</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> allEvents </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">await</span><span class="token plain"> api</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">query</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">system</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">events</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">at</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">blockHash</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token console class-name">console</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">log</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">nBlock #</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">lastHeader</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">number</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  signedBlock</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">block</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">extrinsics</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">forEach</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter">extrinsic</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token parameter"> index</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token arrow operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token console class-name">console</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">log</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">nExtrinsic </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">index</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string string" style="color:rgb(255, 121, 198)">: </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">extrinsic</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">method</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">section</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string string" style="color:rgb(255, 121, 198)">.</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">extrinsic</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">method</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">method</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token console class-name">console</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">log</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">Arguments: </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">extrinsic</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">args</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation method function property-access" style="color:rgb(80, 250, 123)">map</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string interpolation parameter">arg</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token template-string interpolation"> </span><span class="token template-string interpolation arrow operator">=&gt;</span><span class="token template-string interpolation"> arg</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation method function property-access" style="color:rgb(80, 250, 123)">toString</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation method function property-access" style="color:rgb(80, 250, 123)">join</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string interpolation string" style="color:rgb(255, 121, 198)">', '</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  allEvents</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">forEach</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token parameter"> event</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token parameter"> phase </span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token parameter"> index</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token arrow operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token console class-name">console</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">log</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">nEvent </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">index</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string string" style="color:rgb(255, 121, 198)">: </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">event</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">section</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string string" style="color:rgb(255, 121, 198)">.</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">event</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">method</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token console class-name">console</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">log</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">Phase: </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">phase</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation method function property-access" style="color:rgb(80, 250, 123)">toString</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token console class-name">console</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">log</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">Data: </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">event</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">data</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation method function property-access" style="color:rgb(80, 250, 123)">toString</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain">  </span><br></span></code></pre></div></div>
<p>This script subscribes to new block headers, retrieves the full block and its associated events, and logs detailed information about each extrinsic (transaction) and event.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-is-an-extrinsic">What is an extrinsic?<a href="https://docs.midnight.network/blog/query-the-blockchain#what-is-an-extrinsic" class="hash-link" aria-label="Direct link to What is an extrinsic?" title="Direct link to What is an extrinsic?">​</a></h2>
<p>In the context of Substrate-based blockchains like Midnight, an extrinsic refers to any piece of information originating from outside the blockchain's runtime that is submitted to the network for inclusion in a block. Extrinsics are the primary means by which users and external entities interact with the blockchain, enabling state changes and other operations.</p>
<p>There are three main types of extrinsics:</p>
<ul>
<li><strong>Signed Extrinsics</strong>: These are transactions submitted by users that include a digital signature. They often involve actions like transferring tokens or interacting with smart contracts. The signature ensures authenticity and allows the network to charge transaction fees.</li>
<li><strong>Unsigned Extrinsics</strong>: These are submitted without a signature and are typically used for operations that don't require user authentication. They might be subject to custom validation logic to prevent misuse.</li>
<li><strong>Inherent Extrinsics</strong>: Generated by the blockchain itself, these are used for system-level operations, such as setting the timestamp for a new block. They are not propagated through the network like other extrinsics but are included by block producers during block creation.</li>
</ul>
<p>Understanding extrinsics is crucial for developers working with Substrate-based blockchains, as they represent the fundamental mechanism for enacting changes and interacting with the network's state.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/query-the-blockchain#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>By utilizing the WsProvider from the @polkadot/api library, developers can effectively connect to the Midnight blockchain, monitor new blocks in real-time, and extract pertinent data for their applications. This approach provides a robust foundation for building privacy-focused decentralized applications on the Midnight network.</p>
<blockquote>
<p>For more information and detailed documentation, visit the <a href="https://midnight.network/developer-hub" target="_blank" rel="noopener noreferrer">Midnight Developer Hub</a>.</p>
</blockquote>]]></content:encoded>
            <category>midnight</category>
            <category>javascript</category>
        </item>
        <item>
            <title><![CDATA[Compact Deep Dive Part 2 - Circuits and Witnesses]]></title>
            <link>https://docs.midnight.network/blog/compact-2</link>
            <guid>https://docs.midnight.network/blog/compact-2</guid>
            <pubDate>Wed, 25 Jun 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[This blog post is part of the Compact Deep Dive series, which explores how Compact contracts work on the Midnight network.]]></description>
            <content:encoded><![CDATA[<p>This blog post is part of the Compact Deep Dive series, which explores how Compact contracts work on the Midnight network.
Each article focuses on a different technical topic and can be read on its own, but together they provide a fuller picture of how Compact functions in practice.
<a href="https://docs.midnight.network/blog/compact" target="_blank" rel="noopener noreferrer">Compact Deep Dive - Part 1</a> looked at the TypeScript API of the Compact compiler-generated contract implementation.
If you haven’t read it yet, we encourage you to start there.
This article looks at how circuits and witnesses are actually implemented in JavaScript code generated by the Compact compiler.</p>
<p>In part 1, we used the Bulletin Board tutorial contract as an example.
We compiled it with the Compact compiler and started looking at the files generated by the compiler.
We used Compact toolchain version 0.24.0.
Recall the caveat from part 1 that the generated code is an implementation detail of the platform.
We freely change it, so it can be different when different versions of the compiler are used.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="circuits">Circuits<a href="https://docs.midnight.network/blog/compact-2#circuits" class="hash-link" aria-label="Direct link to Circuits" title="Direct link to Circuits">​</a></h2>
<p>We looked at the bulletin board contract’s <code>post</code> circuit.
The compiler generated a TypeScript declaration file that includes a declaration for the circuit:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">post</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> newMessage_0</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitResults</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>Recall that the types <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/CircuitContext" target="_blank" rel="noopener noreferrer"><code>CircuitContext</code></a> and <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/CircuitResults" target="_blank" rel="noopener noreferrer"><code>CircuitResults</code></a> came from the Compact runtime used by the compiler.</p>
<p>In the file <code>index.cjs</code> the compiler has generated a JavaScript implementation of this circuit.
You will find this in the <code>contract</code> subdirectory of the compiler output directory you gave on the <code>compact</code> command line when compiling the contract’s Compact source code.</p>
<p>The implementation is installed as a property on the <code>circuits</code> object of the contract in class <code>Contract</code>’s constructor.
The entirety of the code is here (we will drill down into this in the rest of this section):</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">circuits</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function-variable function" style="color:rgb(80, 250, 123)">post</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter spread operator">...</span><span class="token parameter">args_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token arrow operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">args_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">length</span><span class="token plain"> </span><span class="token operator">!==</span><span class="token plain"> </span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">throw</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactError</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">post: expected 2 arguments (as invoked from Typescript), received </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">args_1</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">length</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> contextOrig_0 </span><span class="token operator">=</span><span class="token plain"> args_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> newMessage_0 </span><span class="token operator">=</span><span class="token plain"> args_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token operator">!</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">typeof</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">===</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'object'</span><span class="token plain"> </span><span class="token operator">&amp;&amp;</span><span class="token plain"> contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">originalState</span><span class="token plain"> </span><span class="token operator">!=</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token plain"> </span><span class="token operator">&amp;&amp;</span><span class="token plain"> contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">transactionContext</span><span class="token plain"> </span><span class="token operator">!=</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">type_error</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'post'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                  </span><span class="token string" style="color:rgb(255, 121, 198)">'argument 1 (as invoked from Typescript)'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                  </span><span class="token string" style="color:rgb(255, 121, 198)">'bboard.compact line 26 char 1'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                  </span><span class="token string" style="color:rgb(255, 121, 198)">'CircuitContext'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                  contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> context </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token spread operator">...</span><span class="token plain">contextOrig_0 </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> partialProofData </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token literal-property property">input</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">newMessage_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token literal-property property">output</span><span class="token operator">:</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token literal-property property">publicTranscript</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">      </span><span class="token literal-property property">privateTranscriptOutputs</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> result_0 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">#_post_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> newMessage_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">output</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">result</span><span class="token operator">:</span><span class="token plain"> result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">context</span><span class="token operator">:</span><span class="token plain"> context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">proofData</span><span class="token operator">:</span><span class="token plain"> partialProofData </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><br></span></code></pre></div></div>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="runtime-type-checks">Runtime Type Checks<a href="https://docs.midnight.network/blog/compact-2#runtime-type-checks" class="hash-link" aria-label="Direct link to Runtime Type Checks" title="Direct link to Runtime Type Checks">​</a></h3>
<p>The first part of this implementation is so-called “boilerplate” code that is generated by the compiler for every circuit.
Every circuit has essentially the same code, differing only slightly depending on the number and names of the arguments, file names, and source code positions.
Let’s focus on that code first:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">args_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">length</span><span class="token plain"> </span><span class="token operator">!==</span><span class="token plain"> </span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">throw</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactError</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">post: expected 2 arguments (as invoked from Typescript), received </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">args_1</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">length</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> contextOrig_0 </span><span class="token operator">=</span><span class="token plain"> args_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> newMessage_0 </span><span class="token operator">=</span><span class="token plain"> args_1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token operator">!</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">typeof</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">===</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'object'</span><span class="token plain"> </span><span class="token operator">&amp;&amp;</span><span class="token plain"> contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">originalState</span><span class="token plain"> </span><span class="token operator">!=</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token plain"> </span><span class="token operator">&amp;&amp;</span><span class="token plain"> contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">transactionContext</span><span class="token plain"> </span><span class="token operator">!=</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">type_error</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'post'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                              </span><span class="token string" style="color:rgb(255, 121, 198)">'argument 1 (as invoked from Typescript)'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                              </span><span class="token string" style="color:rgb(255, 121, 198)">'bboard.compact line 26 char 1'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                              </span><span class="token string" style="color:rgb(255, 121, 198)">'CircuitContext'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                              contextOrig_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> context </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token spread operator">...</span><span class="token plain">contextOrig_0 </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>There are some runtime type checks here.
First, we check that the actual number of arguments passed matches the expected number.
In this case, that’s two (the second one is the circuit’s parameter <code>newMessage</code> and the first one is the <code>CircuitContext</code> parameter inserted by the compiler).
If not, we’ll throw an exception.</p>
<p>This is one way that Compact differs from TypeScript.
The JavaScript code produced by the TypeScript compiler does not check argument counts or types at run time because
it assumes that the calling code has also passed the TypeScript type checker.
Because the correctness of your contracts will depend on it, we do not assume that.
Instead, the generated JavaScript code will perform the appropriate checks at run time.</p>
<p>A <code>const</code> binding is used to give the first argument a name based on <code>contextOrig</code> (always) and the second argument is named based on whatever name we used in the Compact source code.
The suffixes like <code>_0</code> added on variable names is the way that the compiler ensures that it always generates unique names.
Then we have some runtime type checks that the first argument actually satisfies the interface for <code>CircuitContext</code> defined in the Compact runtime.</p>
<p>Finally, we copy the original <code>CircuitContext</code> and name it <code>context</code>.
We do this so that we can mutate the copy without changing the original one that was passed to us.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="proof-data">Proof Data<a href="https://docs.midnight.network/blog/compact-2#proof-data" class="hash-link" aria-label="Direct link to Proof Data" title="Direct link to Proof Data">​</a></h3>
<p>The big-picture view of a <code>post</code> transaction is that it runs the JavaScript implementation of the circuit, with full access to the private state provided by its witnesses.
Then we ask the proof server to generate a zero-knowledge (ZK) proof that the circuit ran as expected.
Specifically, we prove that we know the private data required to produce the observed on-chain behavior—without revealing that private data.</p>
<p>In order to do that, we have to collect some information about running the circuit in the so-called “proof data”.
We next initialize that data:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> partialProofData </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token literal-property property">input</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">newMessage_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token literal-property property">output</span><span class="token operator">:</span><span class="token plain"> </span><span class="token keyword nil" style="color:rgb(189, 147, 249);font-style:italic">undefined</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token literal-property property">publicTranscript</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token literal-property property">privateTranscriptOutputs</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>This is a JavaScript value that satisfies the TypeScript interface <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/ProofData" target="_blank" rel="noopener noreferrer"><code>ProofData</code></a> from the Compact runtime.
To understand this, we need to look inside of the definition.  That definition (from the version of the Compact runtime used by the Compact compiler version 0.24.0) is:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">interface</span><span class="token plain"> </span><span class="token class-name">ProofData</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  input</span><span class="token operator">:</span><span class="token plain"> AlignedValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  output</span><span class="token operator">:</span><span class="token plain"> AlignedValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  privateTranscriptOutputs</span><span class="token operator">:</span><span class="token plain"> AlignedValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  publicTranscript</span><span class="token operator">:</span><span class="token plain"> Op</span><span class="token operator">&lt;</span><span class="token plain">AlignedValue</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>This is called <code>partialProofData</code> because it will not necessarily contain <strong>all</strong> the proof data that we need.
When we run the JavaScript code for the circuit off chain, some conditional branches will be skipped.
In these cases, the proof will require 'dummy' data to fill in for branches that were not taken.
We’ll explore that in more detail later in the series.</p>
<p>The initial value is kind of like the boilerplate we saw before.
The properties <code>input</code>, <code>output</code>, and <code>publicTranscript</code> have default initial values.
The initial value of <code>input</code> depends on the number and types of the parameters to the circuit in Compact:</p>
<p>From the TypeScript declaration of ProofData, we can see that <code>input</code> has type <code>AlignedValue</code>.
This is the type alias <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/type-aliases/AlignedValue" target="_blank" rel="noopener noreferrer"><code>AlignedValue</code></a> from the Compact runtime.
And from the TypeScript declaration for that we can see that it has a pair of properties, <code>alignment</code> and <code>value</code>.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="descriptors">Descriptors<a href="https://docs.midnight.network/blog/compact-2#descriptors" class="hash-link" aria-label="Direct link to Descriptors" title="Direct link to Descriptors">​</a></h3>
<p>To fully understand this, let’s take a look at the Compact runtime TypeScript declaration of AlignedValue:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">AlignedValue</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  alignment</span><span class="token operator">:</span><span class="token plain"> Alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  value</span><span class="token operator">:</span><span class="token plain"> Value</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>We’ll gloss over <code>Alignment</code>, but it’s instructive to see what <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/type-aliases/Value" target="_blank" rel="noopener noreferrer"><code>Value</code></a> is:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">Value</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>So an aligned value is an alignment tag of some kind and a value which is an array of <code>Uint8Arrays</code>.
This is the ledger’s encoding of Compact values in the on-chain runtime.
That’s a different encoding from the JavaScript encoding of the same value.</p>
<p>We have two different representations for the same values.
One is native JavaScript objects.
The other is a binary encoding, used in the on-chain runtime.
To convert between these two representations, we use so-called “descriptors”.
They are the JavaScript representation of Compact types.
More specifically, they are objects implementing the <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/CompactType" target="_blank" rel="noopener noreferrer"><code>CompactType</code></a> interface.
They have three methods: <code>toValue</code> to convert from a JavaScript value to an on-chain value, <code>fromValue</code> to convert from an on-chain value to a JavaScript value, and <code>alignment</code> to return the alignment of the on-chain value.</p>
<p>The code for the <code>ProofData</code>’s input (representing the <code>post</code> circuit’s <code>newMessage</code> argument) was:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token literal-property property">input</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">newMessage_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><br></span></code></pre></div></div>
<p>The compiler generates top-level <code>const</code> bindings in JavaScript for a number of descriptors that it has used in the generated JavaScript code.
If we look at <code>_descriptor_4</code> in <code>index.cjs</code> we see:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> _descriptor_4 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactTypeOpaqueString</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>It’s an instance of a descriptor for a Compact value of type <code>Opaque&lt;"string"&gt;</code>.
The Compact runtime defines descriptor classes for all the Compact types, such as <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/classes/CompactTypeOpaqueString" target="_blank" rel="noopener noreferrer"><code>CompactTypeOpaqueString</code></a>.</p>
<p>The JavaScript representation of the Compact type <code>Opaque&lt;"string"&gt;</code> is as a JavaScript string and the ledger representation is as a (tagged) single-element array consisting of the JavaScript string’s UTF-8 encoding.
The proof data we will build up while executing the circuit contains ledger representations of values, so we use the descriptor’s <code>toValue</code> method to convert the <code>newMessage</code> parameter to that representation.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="wrapping-it-up">Wrapping it Up<a href="https://docs.midnight.network/blog/compact-2#wrapping-it-up" class="hash-link" aria-label="Direct link to Wrapping it Up" title="Direct link to Wrapping it Up">​</a></h3>
<p>Finally, we have a last little bit of code:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> result_0 </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">#_post_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> newMessage_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">output</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">result</span><span class="token operator">:</span><span class="token plain"> result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">context</span><span class="token operator">:</span><span class="token plain"> context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">proofData</span><span class="token operator">:</span><span class="token plain"> partialProofData </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>This calls a method on the contract named <code>#_post_0</code>, which contains the <strong>actual</strong> implementation of the <code>post</code> circuit.
What we’ve been looking at (the <code>post</code> method) is a mostly boilerplate wrapper around this implementation.
The implementation method takes the context and passes along the arguments, along with the proof data object we’ve constructed for it.</p>
<p>Then after it returns, we will set the <code>output</code> property of the proof data.
The way that’s set depends on the return type of the Compact circuit.
In this case it was the Compact type <code>[]</code>, so that’s relatively uninteresting.
If you take a look at the <code>takeDown</code> circuit in the same contract, you will see the slightly more interesting:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">output</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_4</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>Remember that <code>_descriptor_4</code> was the one for the Compact type <code>Opaque&lt;"string"&gt;</code>, and the proof data has ledger values.
The circuit implementation in JavaScript will return a JavaScript value, so we need to encode it into the ledger’s representation using <code>toValue</code> and <code>alignment</code>.</p>
<p>And finally, we return the result of the circuit invocation.
Recall that this was a <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/CircuitResults" target="_blank" rel="noopener noreferrer"><code>CircuitResults&lt;T, []&gt;</code></a> (where <code>T</code> is the contract’s private state type).
That interface is in the Compact runtime and it looks like:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">interface</span><span class="token plain"> </span><span class="token class-name">CircuitResults</span><span class="token class-name operator">&lt;</span><span class="token class-name constant" style="color:rgb(189, 147, 249)">T</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token class-name"> </span><span class="token class-name constant" style="color:rgb(189, 147, 249)">U</span><span class="token class-name operator">&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  context</span><span class="token operator">:</span><span class="token plain"> CircuitContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  proofData</span><span class="token operator">:</span><span class="token plain"> ProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  result</span><span class="token operator">:</span><span class="token plain"> </span><span class="token constant" style="color:rgb(189, 147, 249)">U</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>We will look more closely into the actual circuit implementation <code>#_post_0</code> in the next article in this series.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="what-are-wrappers-for">What are Wrappers For?<a href="https://docs.midnight.network/blog/compact-2#what-are-wrappers-for" class="hash-link" aria-label="Direct link to What are Wrappers For?" title="Direct link to What are Wrappers For?">​</a></h3>
<p>Why do we wrap the implementation in this way?
There are a couple of reasons.</p>
<p>First, the mostly boilerplate code we have been looking at is used when we call the circuit from JavaScript code in our DApp.
So it needs to have the extra runtime checks that we see for safety.
But when we call the circuit from another Compact circuit, we do not need these type checks.
The Compact type system guarantees that we don’t need extra runtime checks, so we can directly call the implementation function (such as <code>#_post_0</code>).</p>
<p>And second, when we call a Compact circuit from another one, that is considered part of the same transaction that we are constructing.
So in that case, we don’t want to construct a fresh <code>ProofData</code> object to pass in, and we don’t want to box up the results in a <code>CircuitResults</code> object.
We only need to do that at the outermost circuit call, coming from the DApp’s JavaScript code.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="witnesses">Witnesses<a href="https://docs.midnight.network/blog/compact-2#witnesses" class="hash-link" aria-label="Direct link to Witnesses" title="Direct link to Witnesses">​</a></h2>
<p>Let’s take a quick look at the implementation of witnesses.
Our contract has one, and we expect it to be passed in when we construct the contract.
The constructor for class <code>Contract</code> has some runtime type checking code for that:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">constructor</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter spread operator">...</span><span class="token parameter">args_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">args_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">length</span><span class="token plain"> </span><span class="token operator">!==</span><span class="token plain"> </span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">throw</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactError</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token template-string string" style="color:rgb(255, 121, 198)">Contract constructor: expected 1 argument, received </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">${</span><span class="token template-string interpolation">args_0</span><span class="token template-string interpolation punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token template-string interpolation property-access">length</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token template-string template-punctuation string" style="color:rgb(255, 121, 198)">`</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> witnesses_0 </span><span class="token operator">=</span><span class="token plain"> args_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">typeof</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">witnesses_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">!==</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'object'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">throw</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactError</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'first (witnesses) argument to Contract constructor is not an object'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">typeof</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">witnesses_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">localSecretKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">!==</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'function'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">throw</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">new</span><span class="token plain"> </span><span class="token class-name">__compactRuntime</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token class-name">CompactError</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'first (witnesses) argument to Contract constructor does not contain a function-valued field named localSecretKey'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">witnesses</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> witnesses_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>Then, probably more interesting, the witnesses are also wrapped.
The contract has a method for each witness, like:</p>
<div class="language-javascript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-javascript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token function" style="color:rgb(80, 250, 123)">#_localSecretKey_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token parameter">context</span><span class="token parameter punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token parameter"> partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> witnessContext_0 </span><span class="token operator">=</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">witnessContext</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token function" style="color:rgb(80, 250, 123)">ledger</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">transactionContext</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">state</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">currentPrivateState</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">transactionContext</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">address</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">nextPrivateState_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">this</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">witnesses</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">witnessContext_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  context</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">currentPrivateState</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> nextPrivateState_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token operator">!</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">buffer</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">instanceof</span><span class="token plain"> </span><span class="token class-name">ArrayBuffer</span><span class="token plain"> </span><span class="token operator">&amp;&amp;</span><span class="token plain"> result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token constant" style="color:rgb(189, 147, 249)">BYTES_PER_ELEMENT</span><span class="token plain"> </span><span class="token operator">===</span><span class="token plain"> </span><span class="token number">1</span><span class="token plain"> </span><span class="token operator">&amp;&amp;</span><span class="token plain"> result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">length</span><span class="token plain"> </span><span class="token operator">===</span><span class="token plain"> </span><span class="token number">32</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">type_error</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'localSecretKey'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                </span><span class="token string" style="color:rgb(255, 121, 198)">'return value'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                </span><span class="token string" style="color:rgb(255, 121, 198)">'bboard.compact line 24 char 1'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                </span><span class="token string" style="color:rgb(255, 121, 198)">'Bytes&lt;32&gt;'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  partialProofData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token property-access">privateTranscriptOutputs</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">push</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token literal-property property">value</span><span class="token operator">:</span><span class="token plain"> _descriptor_2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">toValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token literal-property property">alignment</span><span class="token operator">:</span><span class="token plain"> _descriptor_2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token method function property-access" style="color:rgb(80, 250, 123)">alignment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword control-flow" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> result_0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>The circuit wrapper <code>post</code> was used when we called the circuit from JavaScript code, and bypassed (by directly calling the implementation <code>#_post_0</code>) when we called it from Compact code.
For witnesses, the situation is reversed.
You can call your witnesses (like <code>witnesses.localSecretKey</code>) all you want from JavaScript code and we don’t care and won’t even observe it.
But if you call them from a Compact circuit, we will need to know about it, so we’ll call a wrapper (like <code>localSecretKey_0</code>).</p>
<p>Your witness implementation expects to have a <code>WitnessContext</code> argument passed to it, so here we’ll construct one to pass in.
It contains the ledger, current private state, and the contract’s address.
We get the JavaScript representation of the public ledger state using the compiler-generated <code>ledger</code> function that we looked at in part 1 of this series.</p>
<p>Then we actually invoke your witness implementation, getting the result and a new private state.
We mutate the context to update the private state (remember, we copied the original context before invoking a circuit implementation so it was safe to mutate the copied context).</p>
<p>Next, we have runtime type checks after the witness returns.
This is for the same reason that we check arguments coming in to circuits from JavaScript: we don’t control the witness implementation and we need to make sure that the return value is as expected for the circuit.
Compact’s type safety depends on these runtime checks.</p>
<p>Finally, we record the witness’s return value in the proof data's private transcript that we are building while running the outermost Compact circuit call.
This is private data and so for the ZK proof we construct the proof server will need to know it (so the proof server can prove that it knows the private data).</p>
<p>In the next article in this series, "Compact Deep Dive Part 3: The On-Chain Runtime", we will take a closer look at the actual implementation of the <code>post</code> circuit from the bulletin board contract and see how it uses the on-chain runtime.</p>]]></content:encoded>
            <category>compact</category>
        </item>
        <item>
            <title><![CDATA[Learning Web3 from the Ground Up - Smart Contracts and the Compact Language]]></title>
            <link>https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact</link>
            <guid>https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact</guid>
            <pubDate>Fri, 20 Jun 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[As I continue my journey through the world of Web3, I’ve been digging into the building blocks that make privacy-first platforms like Midnight possible (you can catch up on previous posts in the Midnight Dev Diaries!). This week, I focused on two foundational pieces that go hand in hand: smart contracts on Midnight and the Compact language that powers them.]]></description>
            <content:encoded><![CDATA[<p>As I continue my journey through the world of Web3, I’ve been digging into the building blocks that make privacy-first platforms like Midnight possible (you can catch up on previous posts in the Midnight Dev Diaries!). This week, I focused on two foundational pieces that go hand in hand: smart contracts on Midnight and the Compact language that powers them.</p>
<p>Smart contracts aren’t new to blockchain, but Midnight’s approach is unique. Instead of executing code directly on-chain, Midnight uses smart contracts to define rules that are enforced off-chain through zero-knowledge proofs. At the center of this system is <a href="https://docs.midnight.network/develop/reference/compact/compact-reference" target="_blank" rel="noopener noreferrer">Compact</a>—a purpose-built language designed to make writing these privacy-preserving contracts both secure and practical.</p>
<p>To better understand how this works in practice, I also worked through <a href="https://docs.midnight.network/academy/module-2" target="_blank" rel="noopener noreferrer">Module 2 of the Midnight Developer Academy</a>, which covers key concepts such as contract structure, circuit types, and the relationship between the contract and the external logic that executes it.</p>
<p>In this post, I’ll break down what makes smart contracts on Midnight different, how Compact is structured, and how the two work together to enable provable logic without exposing sensitive data.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-is-a-smart-contract-on-midnight">What Is a Smart Contract on Midnight?<a href="https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact#what-is-a-smart-contract-on-midnight" class="hash-link" aria-label="Direct link to What Is a Smart Contract on Midnight?" title="Direct link to What Is a Smart Contract on Midnight?">​</a></h2>
<p>At a high level, a smart contract is an autonomous program stored on the blockchain. It defines rules for interaction, checks that those rules are met, and records results—all without needing a trusted third party to enforce them.</p>
<p>But Midnight isn’t just any blockchain. It’s a privacy-first platform built with compliance in mind, and that means its smart contracts need to do more than just run logic. They need to protect private data, enable selective disclosure, and uphold security and trust even in adversarial environments.</p>
<p>That’s where Compact comes in—a custom domain-specific language explicitly designed to write smart contracts on Midnight.</p>
<p>Instead of executing logic directly on-chain like many traditional smart contracts, Compact contracts define the logic to be proven, not the execution environment itself. The contract encodes the rules that must be followed, which are then used as the foundation for a zero-knowledge proof. The actual execution—like processing a transaction, generating a credential, or submitting a vote—happens externally, often in a DApp, backend service, or API.</p>
<p>That external system runs the necessary logic and <strong>proves</strong> it followed the contract’s rules by generating a cryptographic proof. The blockchain doesn’t need to know the details—it just verifies that the proof is valid. This separation is what makes Midnight’s model both scalable and privacy-preserving.</p>
<p>In other words: the contract sets the rules, your app runs the logic, and the blockchain verifies the outcome, without ever seeing the sensitive inputs.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-is-compact">What Is Compact?<a href="https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact#what-is-compact" class="hash-link" aria-label="Direct link to What Is Compact?" title="Direct link to What Is Compact?">​</a></h2>
<p>Compact is a statically typed, privacy-focused smart contract language tailored to the unique goals of the Midnight network. If you've worked with JavaScript or TypeScript before, you'll find Compact refreshingly approachable. It’s a domain-specific language (DSL) designed to define zero-knowledge proof logic using statically typed circuits that map to off-chain computations. But don’t let the simple syntax fool you—Compact is engineered for privacy-preserving computation using zero-knowledge proofs (ZKPs) under the hood. (For a more technical deep dive into compact, check out part 1 of Kevin Millikin's Compact Deep Dive <a href="https://docs.midnight.network/blog/compact" target="_blank" rel="noopener noreferrer">here</a>!)</p>
<p>A Compact contract is made up of three core components:</p>
<ul>
<li><strong>Ledger</strong>: The public, on-chain state of the contract.</li>
<li><strong>Circuits</strong>: Off-chain logic that can manipulate data, produce proofs, and interact with the ledger.</li>
<li><strong>Witnesses</strong>: Privately held data, only known to the entity executing the contract.</li>
</ul>
<p>Compact also supports <a href="https://docs.midnight.network/develop/reference/compact/explicit-disclosure#conclusion" target="_blank" rel="noopener noreferrer">explicit disclosure</a>, meaning developers can define exactly what data gets revealed (and to whom). This provides fine-grained control over what is visible on-chain and what remains private, essential for use cases such as private asset transfers, age-gated access, or confidential voting.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="circuits-and-privacy">Circuits and Privacy<a href="https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact#circuits-and-privacy" class="hash-link" aria-label="Direct link to Circuits and Privacy" title="Direct link to Circuits and Privacy">​</a></h2>
<p>One of the most essential takeaways from Module 2 of the Midnight Developer Academy is how circuits work. In Compact, circuits are the functions that users or systems can call to trigger actions. But unlike Web2 APIs or EVM contract functions, these circuits don’t execute logic directly on the blockchain. Instead, they run off-chain and generate zero-knowledge proofs that the logic was followed.</p>
<p>In practice, that means:</p>
<ul>
<li>The contract logic is public.</li>
<li>The data used during execution is private.</li>
<li>The outcome of the logic (e.g., “the rules were followed”) is publicly verifiable using a proof.</li>
</ul>
<p>This is where Midnight’s model shows its strength—users can prove they followed the rules without revealing the exact data that triggered the result. Think “I proved I’m eligible to vote” without revealing who I am or what I voted for.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="from-theory-to-practice">From Theory to Practice<a href="https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact#from-theory-to-practice" class="hash-link" aria-label="Direct link to From Theory to Practice" title="Direct link to From Theory to Practice">​</a></h2>
<p>In traditional smart contract platforms, everything you do is public by default: data, logic, inputs, outputs. That’s useful for transparency, but it's not always practical, especially for sensitive use cases like health data, financial records, or identity verification.</p>
<p>Midnight flips that default. Privacy is built in, and developers opt into disclosure only when it’s needed.</p>
<p>Some examples of what you might build with Compact on Midnight:</p>
<ul>
<li>A voting system where only the outcome is public.</li>
<li>An access control system that verifies user attributes without revealing identities.</li>
<li>A token transfer app that hides amounts or senders while still allowing public verification.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/web3-intro-smart-contracts-and-compact#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>Smart contracts are the foundation of decentralized applications—but at Midnight, they’re built for a privacy-first future. Instead of executing every piece of logic on-chain, Midnight separates the contract's logic from its execution. With Compact, developers define the rules of a zero-knowledge proof system, enabling applications to prove facts without exposing sensitive data. It’s a practical shift toward secure, scalable, and user-protective Web3 design.</p>
<p>At Midnight, this isn’t just a theoretical improvement—it’s core to how the platform operates. Compact smart contracts let developers write verifiable, auditable logic while keeping inputs and execution off-chain. Combined with ZKPs and shielded transactions, they create an infrastructure that supports real-world use cases without compromising on privacy or decentralization.</p>
<p>If you're learning how to build smart contracts in Web3—or just curious how these pieces fit together—I highly recommend diving into <a href="https://docs.midnight.network/academy/module-2" target="_blank" rel="noopener noreferrer">Module 2 of the Midnight Developer Academy</a> and reviewing the <a href="https://docs.midnight.network/develop/reference/compact/compact-reference" target="_blank" rel="noopener noreferrer">Compact Language Reference</a>. These resources break down the concepts step by step and show how Midnight’s architecture turns advanced cryptography into practical developer tools.</p>
<p>I’ll be back next week with more insights as I continue to unpack the pieces that make privacy-first systems work in the decentralized world.</p>]]></content:encoded>
            <category>intro</category>
            <category>blockchain</category>
            <category>beginner</category>
            <category>web3</category>
        </item>
        <item>
            <title><![CDATA[Compact Deep Dive Part 1 - Top-level Contract Structure]]></title>
            <link>https://docs.midnight.network/blog/compact</link>
            <guid>https://docs.midnight.network/blog/compact</guid>
            <pubDate>Tue, 17 Jun 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[This post is part of the Compact Deep Dive series, which explores how Compact contracts work on the Midnight network.]]></description>
            <content:encoded><![CDATA[<p>This post is part of the Compact Deep Dive series, which explores how Compact contracts work on the Midnight network.
Each article focuses on a different technical topic and can be read on its own, but together they provide a fuller picture of how Compact functions in practice.
The articles assume that you are familiar with Compact to the level of detail covered in the <a href="https://docs.midnight.network/develop/tutorial/" target="_blank" rel="noopener noreferrer">developer tutorial</a>.
Some posts in this series take a deep technical dive into the implementation details of ZK proofs and the Midnight on-chain runtime.</p>
<p>These insights reflect the current architecture, but since they describe low-level mechanics, they may change as the platform evolves.</p>
<p>One caveat to keep in mind is that almost everything here should be considered an implementation detail.
That means that the details are not stable, and they can and will change as we see fit.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="overview-of-the-bulletin-board-contract">Overview of the Bulletin Board Contract<a href="https://docs.midnight.network/blog/compact#overview-of-the-bulletin-board-contract" class="hash-link" aria-label="Direct link to Overview of the Bulletin Board Contract" title="Direct link to Overview of the Bulletin Board Contract">​</a></h2>
<p>We'll use our old favorite, <a href="https://docs.midnight.network/develop/tutorial/creating/bboard-contract#compiling-the-contract" target="_blank" rel="noopener noreferrer">Bulletin Board</a> as an example.
We're compiling this with Compact toolchain version 0.24.0.
The Compact language is a moving target as we introduce and change features, so this code may not compile with any Compact toolchain version other than 0.24.0.</p>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>All the code in this section is Compact code</p></div></div>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">pragma</span><span class="token plain"> language_version </span><span class="token number">0.16</span><span class="token plain">;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> CompactStandardLibrary;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">enum</span><span class="token plain"> State {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  VACANT,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  OCCUPIED</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> state: State;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> message: Maybe&lt;</span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> instance: </span><span class="token class-name">Counter</span><span class="token plain">;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> poster: </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">constructor</span><span class="token plain">() {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  state = State.VACANT;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  message = none&lt;</span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;&gt;();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  instance.</span><span class="token function" style="color:rgb(80, 250, 123)">increment</span><span class="token plain">(</span><span class="token number">1</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">witness</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token plain">(): </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">circuit</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">post</span><span class="token plain">(newMessage: </span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;): [] {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">assert</span><span class="token plain">(state == State.VACANT, </span><span class="token string" style="color:rgb(255, 121, 198)">"Attempted to post to an occupied board"</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  poster = </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">disclose</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token plain">(), instance </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Field</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  message = </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">disclose</span><span class="token plain">(some&lt;</span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;&gt;(newMessage));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  state = State.OCCUPIED;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">circuit</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">takeDown</span><span class="token plain">(): </span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt; {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">assert</span><span class="token plain">(state == State.OCCUPIED, </span><span class="token string" style="color:rgb(255, 121, 198)">"Attempted to take down post from an empty board"</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">assert</span><span class="token plain">(poster == </span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token plain">(), instance </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Field</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;), </span><span class="token string" style="color:rgb(255, 121, 198)">"Attempted to take down post, but not the current poster"</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> formerMsg = message.value;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  state = State.VACANT;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  instance.</span><span class="token function" style="color:rgb(80, 250, 123)">increment</span><span class="token plain">(</span><span class="token number">1</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  message = none&lt;</span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;&gt;();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> formerMsg;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">circuit</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token plain">(sk: </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;, instance: </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;): </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt; {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> persistentHash&lt;</span><span class="token class-name">Vector</span><span class="token plain">&lt;</span><span class="token number">3</span><span class="token plain">, </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;&gt;&gt;([</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">pad</span><span class="token plain">(</span><span class="token number">32</span><span class="token plain">, </span><span class="token string" style="color:rgb(255, 121, 198)">"bboard:pk:"</span><span class="token plain">), instance, sk]);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>We’ll focus on the <code>post</code> circuit.
First, let’s refresh your memory about the basics of the contract.
It declares a Compact <code>enum</code> type for the bulletin board’s state, and it declares some ledger fields.
Three of them are mutable ledger cells and one of them is a <code>Counter</code>:</p>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">enum</span><span class="token plain"> State {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  VACANT,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  OCCUPIED</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> state: State;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> message: Maybe&lt;</span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> instance: </span><span class="token class-name">Counter</span><span class="token plain">;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">ledger</span><span class="token plain"> poster: </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;;</span><br></span></code></pre></div></div>
<p>There is a witness that has access to private state.
This is a foreign function implemented in JavaScript or TypeScript.
It looks up the user’s secret key somehow and returns it.
The <code>post</code> circuit makes sure that the bulletin board is in the <code>VACANT</code> state and, if so, uses the witness to get the secret key and updates the three cells in the ledger:</p>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">witness</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token plain">(): </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">circuit</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">post</span><span class="token plain">(newMessage: </span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;): [] {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">assert</span><span class="token plain">(state == State.VACANT, </span><span class="token string" style="color:rgb(255, 121, 198)">"Attempted to post to an occupied board"</span><span class="token plain">);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  poster = </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">disclose</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token plain">(</span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token plain">(), instance </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Field</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  message = </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">disclose</span><span class="token plain">(some&lt;</span><span class="token class-name">Opaque</span><span class="token plain">&lt;</span><span class="token string" style="color:rgb(255, 121, 198)">"string"</span><span class="token plain">&gt;&gt;(newMessage));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  state = State.OCCUPIED;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>There is also a helper circuit called <code>publicKey</code>, which is used by both the <code>post</code> and <code>takeDown</code> circuits.<br>
<!-- -->Declaring <code>publicKey</code> as exported has two effects:</p>
<ul>
<li>It makes the circuit callable from TypeScript or JavaScript.</li>
<li>It makes it a contract entry point, allowing <code>publicKey</code> transactions to be submitted to the Midnight blockchain.</li>
</ul>
<div class="language-compact codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-compact codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">circuit</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token plain">(sk: </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;, instance: </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;): </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt; {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> persistentHash&lt;</span><span class="token class-name">Vector</span><span class="token plain">&lt;</span><span class="token number">3</span><span class="token plain">, </span><span class="token class-name">Bytes</span><span class="token plain">&lt;</span><span class="token number">32</span><span class="token plain">&gt;&gt;&gt;([</span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">pad</span><span class="token plain">(</span><span class="token number">32</span><span class="token plain">, </span><span class="token string" style="color:rgb(255, 121, 198)">"bboard:pk:"</span><span class="token plain">), instance, sk]);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre></div></div>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="a-look-under-the-hood">A Look Under the Hood<a href="https://docs.midnight.network/blog/compact#a-look-under-the-hood" class="hash-link" aria-label="Direct link to A Look Under the Hood" title="Direct link to A Look Under the Hood">​</a></h2>
<p>Presuming <code>compact</code> is in your path, you can compile the Bulletin Board contract directly.
If the code is in a file named <code>bboard.compact</code>, change to the directory where that file is located. Then run <code>compact compile</code>, passing the source file and an output directory:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">$ compact compile bboard.compact bboard-out</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">Compact version: 0.24.0</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">Compiling 2 circuits:</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  circuit "post" (k=14, rows=10070)  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  circuit "takeDown" (k=14, rows=10087)  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">Overall progress [====================] 2/2</span><br></span></code></pre></div></div>
<p>Let’s take a look at what the compiler has generated:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">$ ls bboard-out</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">compiler        contract        keys            zkir</span><br></span></code></pre></div></div>
<p>There are four subdirectories.
<code>compiler</code> has some metadata about the contract that will be used by composable contracts, which we can just ignore for now.
<code>keys</code> and <code>zkir</code> are related to the ZK proofs which we will talk about in a later article.
The compiler has translated the contract code into a JavaScript implementation, which is in the contract subdirectory.
We’ll focus on that first.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">$ ls bboard-out/contract</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">index.cjs       index.cjs.map   index.d.cts</span><br></span></code></pre></div></div>
<p>There are three files here.
<code>index.cjs</code> is the JavaScript implementation of the contract.
(It is JavaScript source code, the <code>.cjs</code> extension means that it uses the CommonJS module system.)
There is a source map file <code>index.cjs.map</code> that can be used for debugging.
It connects the JavaScript implementation in <code>index.cjs</code> back to the original Compact source code that was in <code>bboard.compact</code>.
Finally, there is a TypeScript declaration file <code>index.d.cts</code> for the JavaScript implementation.
This allows the JavaScript code in <code>index.cjs</code> to be called from TypeScript (and importantly, type checked by the TypeScript compiler).</p>
<p>We have chosen this strategy (a JavaScript implementation with a TypeScript declaration file, instead of a pure TypeScript implementation) for a couple of reasons.
First, it lets us generate runtime checks in the JavaScript code for things that wouldn’t be checked by TypeScript’s type system.
And second, we can generate a source map for debugging that maps the implementation back to the Compact source code.
The source map you would get from the TypeScript compiler would only connect the TypeScript compiler generated JavaScript code back to Compact compiler generated TypeScript, not all the way back to the original Compact source code.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="typescript-declaration">TypeScript Declaration<a href="https://docs.midnight.network/blog/compact#typescript-declaration" class="hash-link" aria-label="Direct link to TypeScript Declaration" title="Direct link to TypeScript Declaration">​</a></h2>
<p>Now, let’s take a first look at the structure of the contract’s implementation by looking inside the TypeScript declaration file <code>index.d.cts</code>.
We will walk through this file, though not necessarily in order.
Before reading further, we encourage you to compile the code to generate this file and take a look at it yourself.</p>
<p>Remember, this was generated with Compact toolchain version 0.24.0.
If you try the same thing with a different version, you might see different implementation details.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="the-compact-runtime">The Compact Runtime<a href="https://docs.midnight.network/blog/compact#the-compact-runtime" class="hash-link" aria-label="Direct link to The Compact Runtime" title="Direct link to The Compact Runtime">​</a></h3>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>All the code in this section is written in TypeScript.</p></div></div>
<p>The very first thing you will see is:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">import</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">as</span><span class="token plain"> __compactRuntime </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">from</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'@midnight-ntwrk/compact-runtime'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>This imports the Node.js package <code>@midnight-ntwrk/compact-runtime</code> which is an API used by the Compact compiler’s generated JavaScript code.
Separating it out like this decouples the runtime from the compiler implementation, and it means that the generated JavaScript code can be smaller.
We’ll see later that the Compact runtime is quite complex (it re-exports a large part of the on-chain runtime which is implemented in Rust and compiled to WebAssembly).</p>
<p>You can even import this package in your DApp to have your own access to Compact runtime types and functions if necessary.
The API documentation for the Compact runtime is available in the <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/" target="_blank" rel="noopener noreferrer">Midnight documentation</a>.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="compact-enum-types">Compact <code>enum</code> Types<a href="https://docs.midnight.network/blog/compact#compact-enum-types" class="hash-link" aria-label="Direct link to compact-enum-types" title="Direct link to compact-enum-types">​</a></h3>
<p>The bulletin board contract declared a Compact <code>enum</code> type for the bulletin board’s state (vacant or occupied).
This type was exported (via the <code>export</code> keyword) which makes it available to a DApp’s TypeScript or JavaScript implementation, so there is a declaration in the TypeScript declaration file:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">enum</span><span class="token plain"> State </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> </span><span class="token constant" style="color:rgb(189, 147, 249)">VACANT</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token constant" style="color:rgb(189, 147, 249)">OCCUPIED</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token number">1</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>If this <code>enum</code> type was not exported in Compact, we would not see this declaration.
Then, whenever this type appeared in the contract’s API (like in a circuit parameter or in the ledger) we would instead see the the underlying TypeScript representation type <code>number</code>.
(Try it and see!
Remove the <code>export</code> keyword for the <code>enum</code> in the Compact contract.
Note that if you merely want to look at the generated TypeScript or JavaScript contract code you can skip ZK key generation by passing the command-line flag <code>--skip-zk</code> to the Compact compiler.
This will run much faster.)</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="the-compact-ledger">The Compact Ledger<a href="https://docs.midnight.network/blog/compact#the-compact-ledger" class="hash-link" aria-label="Direct link to The Compact Ledger" title="Direct link to The Compact Ledger">​</a></h3>
<p>In Compact, the contract’s public state is established by ledger declarations.
The compiler collects all of these and exposes them to the DApp in the form of a ledger type:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">Ledger</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> state</span><span class="token operator">:</span><span class="token plain"> State</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> message</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> is_some</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">boolean</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> value</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> instance</span><span class="token operator">:</span><span class="token plain"> bigint</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> poster</span><span class="token operator">:</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>It has read-only properties for all the ledger fields.
They are read-only in TypeScript, because <strong>updating</strong> the ledger actually requires a transaction to be submitted to the chain.
A DApp, however, can freely read them from (a snapshot of) the public ledger state.</p>
<p>We mentioned before that <code>State</code> appears in this API because we exported the Compact <code>enum</code> type <code>State</code>.
Notice that the Compact standard library type <code>Maybe</code> does not appear in this API.
Instead, the ledger field <code>message</code> has the underlying TypeScript type.
That’s because we didn’t export the standard library’s <code>Maybe</code> type.
We could do that with <code>export { Maybe }</code> at the top level of our Compact contract, and then we would instead see:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">Maybe</span><span class="token class-name operator">&lt;</span><span class="token class-name">a</span><span class="token class-name operator">&gt;</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> is_some</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">boolean</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> value</span><span class="token operator">:</span><span class="token plain"> a </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">Ledger</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> state</span><span class="token operator">:</span><span class="token plain"> State</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> message</span><span class="token operator">:</span><span class="token plain"> Maybe</span><span class="token operator">&lt;</span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> instance</span><span class="token operator">:</span><span class="token plain"> bigint</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">readonly</span><span class="token plain"> poster</span><span class="token operator">:</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>There is also a declaration of a function that gives us (a read-only snapshot of) the public ledger state, returning a TypeScript value of type <code>Ledger</code> as declared above:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">declare</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">function</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">ledger</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">state</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">StateValue</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> Ledger</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>This function takes a value of the Compact runtime’s type <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/classes/StateValue" target="_blank" rel="noopener noreferrer"><code>StateValue</code></a>.
We will see in Part 2 of this series how this function is used to pass a <code>Ledger</code> to witnesses.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="compact-circuits">Compact Circuits<a href="https://docs.midnight.network/blog/compact#compact-circuits" class="hash-link" aria-label="Direct link to Compact Circuits" title="Direct link to Compact Circuits">​</a></h3>
<p>Our contract had three exported circuits.
By exporting them, they are made available to be called by the DApp’s TypeScript or JavaScript code, and they form the contract’s entry points.
We can see declarations for them in the TypeScript declaration file, in two different places:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">ImpureCircuits</span><span class="token class-name operator">&lt;</span><span class="token class-name constant" style="color:rgb(189, 147, 249)">T</span><span class="token class-name operator">&gt;</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">post</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> newMessage_0</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitResults</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">takeDown</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitResults</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">PureCircuits</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">sk_0</span><span class="token operator">:</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> instance_0</span><span class="token operator">:</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">Circuits</span><span class="token class-name operator">&lt;</span><span class="token class-name constant" style="color:rgb(189, 147, 249)">T</span><span class="token class-name operator">&gt;</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">post</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> newMessage_0</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitResults</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">takeDown</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitResults</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">publicKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            sk_0</span><span class="token operator">:</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            instance_0</span><span class="token operator">:</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">CircuitResults</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> Uint8Array</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>The <code>post</code> and <code>takeDown</code> circuits are <em>impure</em>.
In Compact, this basically means that they access (even if only by reading) the public state and/or they invoke witnesses.
They are declared in the type <code>ImpureCircuits&lt;T&gt;</code>.
The generic type parameter <code>T</code> here is the type of the contract's private state.
The Compact compiler doesn't know what that type is (nor does it need to); it's up to the DApp developer to fill that in.</p>
<p>Recall the Compact signature of the <code>post</code> circuit was <code>circuit post(new_message: Opaque&lt;"string"&gt;): []</code>.
We can see that the TypeScript API for this circuit is predictably derived from the Compact signature, with a few differences.</p>
<p>First, the circuit takes an extra first argument of type <code>CircuitContext&lt;T&gt;</code>.
This is an interface declared in the <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/CircuitContext" target="_blank" rel="noopener noreferrer">Compact runtime</a>.
It contains an encapsulation of the contract's on-chain and private state, some separate Zswap state, and a representation of what the on-chain context would be if the circuit were actually executing on chain (though note, this JavaScript code is <strong>not</strong> what's executed on chain).</p>
<p>Second, we can see that the Compact type <code>Opaque&lt;"string"&gt;</code> is represented by the TypeScript type <code>string</code>.
One goal of Compact is that the TypeScript representation of Compact types is always predictable.</p>
<p>And third, we can see that the return type (in Compact it was <code>[]</code>) is actually <code>CircuitResults&lt;T, []&gt;</code>.
This is another interface declared in the <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/CircuitResults" target="_blank" rel="noopener noreferrer">Compact runtime</a>.
It has the actual return value with TypeScript type <code>[]</code>, as well as some proof data required to construct the ZK proof and a new <code>CircuitContext&lt;T&gt;</code> representing the public and private state after running the circuit.</p>
<p>We won’t focus on <code>takeDown</code> here, but you can see that its signature is similarly and predictably derived from the signature of the circuit in Compact.</p>
<p>The helper circuit <code>publicKey</code> is <em>pure</em>.
This means that it does not access the public state or invoke witnesses (i.e., it's not impure).
It is declared in the type <code>PureCircuits</code>.
Pure circuits are ones that can run without an instance of the contract.
Specifically, they do not need access to the ledger state and they do not have access to private state.
You can see that here, because the extra first <code>CircuitContext</code> argument is missing, and the return value is a bare TypeScript type rather than a <code>CircuitResult</code>.
The type <code>PureCircuits</code> is not generic, there is no type parameter <code>T</code> needed to represent the type of the private state.</p>
<p>Finally, these declarations are repeated in the type <code>Circuits&lt;T&gt;</code>.
The declarations of <code>post</code> and <code>takeDown</code> are exactly the same as before, but the declaration of <code>publicKey</code> has the signature of an impure circuit.
This is so that the DApp can make a <code>publicKey</code> transaction, without having to worry about the details of whether it's pure or not.</p>
<p>There are implementations of these circuits in the compiler-generated JavaScript code for the contract, which we will look at in the next article in this series.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="compact-witnesses">Compact Witnesses<a href="https://docs.midnight.network/blog/compact#compact-witnesses" class="hash-link" aria-label="Direct link to Compact Witnesses" title="Direct link to Compact Witnesses">​</a></h3>
<p>We had a single witness declaration in our contract, which is also reflected in the contract’s TypeScript API:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> </span><span class="token class-name">Witnesses</span><span class="token class-name operator">&lt;</span><span class="token class-name constant" style="color:rgb(189, 147, 249)">T</span><span class="token class-name operator">&gt;</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">localSecretKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">WitnessContext</span><span class="token operator">&lt;</span><span class="token plain">Ledger</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> Uint8Array</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></span></code></pre></div></div>
<p>The witness’s signature is also predictably derived from the Compact witness declaration.
It has an extra first argument of type <code>WitnessContext&lt;Ledger, T&gt;</code>.
This interface is declared in the <a href="https://docs.midnight.network/develop/reference/midnight-api/compact-runtime/interfaces/WitnessContext" target="_blank" rel="noopener noreferrer">Compact runtime</a>.
It contains a snapshot of the public ledger state, the contract’s private state of type <code>T</code>, and the contract’s address.
The witness returns a tuple (a two-element TypeScript array) consisting of a new private state of type <code>T</code> and the Compact return value.
The Compact type <code>Bytes&lt;32&gt;</code> is represented by the underlying TypeScript type Uint8Array.</p>
<p>The DApp implementation is responsible for providing a witness with this signature when constructing the contract.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="the-contract-type">The Contract Type<a href="https://docs.midnight.network/blog/compact#the-contract-type" class="hash-link" aria-label="Direct link to The Contract Type" title="Direct link to The Contract Type">​</a></h3>
<p>Finally, there is a declaration of the contract type:</p>
<div class="language-typescript codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-typescript codeBlock_bY9V thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">declare</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">class</span><span class="token plain"> </span><span class="token class-name">Contract</span><span class="token class-name operator">&lt;</span><span class="token class-name constant" style="color:rgb(189, 147, 249)">T</span><span class="token class-name punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token class-name"> </span><span class="token class-name constant" style="color:rgb(189, 147, 249)">W</span><span class="token class-name"> </span><span class="token class-name keyword" style="color:rgb(189, 147, 249);font-style:italic">extends</span><span class="token class-name"> Witnesses</span><span class="token class-name operator">&lt;</span><span class="token class-name constant" style="color:rgb(189, 147, 249)">T</span><span class="token class-name operator">&gt;</span><span class="token class-name"> </span><span class="token class-name operator">=</span><span class="token class-name"> Witnesses</span><span class="token class-name operator">&lt;</span><span class="token class-name constant" style="color:rgb(189, 147, 249)">T</span><span class="token class-name operator">&gt;&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  witnesses</span><span class="token operator">:</span><span class="token plain"> </span><span class="token constant" style="color:rgb(189, 147, 249)">W</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  circuits</span><span class="token operator">:</span><span class="token plain"> Circuits</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  impureCircuits</span><span class="token operator">:</span><span class="token plain"> ImpureCircuits</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">constructor</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">witnesses</span><span class="token operator">:</span><span class="token plain"> </span><span class="token constant" style="color:rgb(189, 147, 249)">W</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">initialState</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">context</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ConstructorContext</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> __compactRuntime</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ConstructorResult</span><span class="token operator">&lt;</span><span class="token constant" style="color:rgb(189, 147, 249)">T</span><span class="token operator">&gt;</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">export</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">declare</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> pureCircuits</span><span class="token operator">:</span><span class="token plain"> PureCircuits</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></span></code></pre></div></div>
<p>It is parameterized over the private state type and the witness type, and it has witnesses, circuits, impure circuits, a constructor, and an initial state all using the type declarations seen above.
The pure circuits are a top-level TypeScript value (rather than a contract property), representing the fact that they do not need an instance of the contract.</p>
<p>The contract itself is implemented by the compiler-generated JavaScript code in <code>index.cjs</code>.
In the next article in this series, "Compact Deep Dive – Part 2: Circuits and Witnesses", we’ll begin examining how circuits and witnesses are implemented in the generated code.</p>]]></content:encoded>
            <category>compact</category>
        </item>
        <item>
            <title><![CDATA[Learning Web3 from the Ground Up - Understanding Selective Disclosure]]></title>
            <link>https://docs.midnight.network/blog/web3-intro-selective-disclosure</link>
            <guid>https://docs.midnight.network/blog/web3-intro-selective-disclosure</guid>
            <pubDate>Fri, 13 Jun 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[As I delve deeper into the world of Web3, I've been working my way through foundational concepts to have a better understanding of the industry (check out my last five posts on the Midnight Dev Diaries!). This week, I dove into one of the most practical and privacy-focused ideas I’ve encountered so far: selective disclosure.]]></description>
            <content:encoded><![CDATA[<p>As I delve deeper into the world of Web3, I've been working my way through foundational concepts to have a better understanding of the industry (check out my last five posts on the <a href="https://docs.midnight.network/blog" target="_blank" rel="noopener noreferrer">Midnight Dev Diaries</a>!). This week, I dove into one of the most practical and privacy-focused ideas I’ve encountered so far: <strong>selective disclosure</strong>.</p>
<p>Selective disclosure is more than just a technical feature—it’s the working expression of a broader idea known as <strong>rational privacy</strong>. It allows individuals to prove or reveal only what’s necessary in a given interaction, keeping everything else private. In an ecosystem where trust must be earned without a central authority, this ability to protect personal data while still meeting legal or operational requirements is critical.</p>
<p>This post examines how selective disclosure operates, its benefits, and how platforms like Midnight are implementing it through privacy-first smart contracts.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-is-selective-disclosure">What Is Selective Disclosure?<a href="https://docs.midnight.network/blog/web3-intro-selective-disclosure#what-is-selective-disclosure" class="hash-link" aria-label="Direct link to What Is Selective Disclosure?" title="Direct link to What Is Selective Disclosure?">​</a></h2>
<p>In the Web3 world, privacy doesn’t mean keeping everything secret—it means sharing only what’s necessary. <a href="https://www.identity.com/what-is-selective-disclosure/" target="_blank" rel="noopener noreferrer"><strong>Selective disclosure</strong></a> is a privacy-preserving technique that enables individuals to disclose specific information for a given interaction while keeping all other personal data confidential.</p>
<p>This approach is a practical expression of a broader concept known as <a href="https://midnight.network/blog/midnight-at-consensus-2025-building-the-future-of-rational-privacy" target="_blank" rel="noopener noreferrer"><strong>rational privacy</strong></a>. Rational privacy is the idea that privacy shouldn’t be absolute or all-or-nothing—it should be contextual. It recognizes that in many real-world scenarios, users want to share <em>some</em> data to gain access to services or meet legal obligations, but they shouldn't be forced to give up all privacy and security in the process. It’s about striking a balance between personal control, usability, and regulatory compliance.</p>
<p>Selective disclosure is the technical implementation of rational privacy. It gives users and developers the cryptographic tools to protect sensitive information by default, while still enabling the right level of transparency when it’s required. This makes it especially relevant for public blockchain networks, where data is immutable and visible by design.</p>
<p>A typical example of selective disclosure in action is the use of Verifiable Credentials (VCs). VCs allow users to prove attributes—like age, residency, or educational background—without revealing the entire credential. This aligns with the principles of data minimization and privacy by design, ensuring individuals remain in control of their information.</p>
<p>This balance between usability and privacy is what makes selective disclosure so powerful—and so relevant for real-world blockchain applications. But beyond the technical implementation, it's worth asking: what are the practical benefits of using selective disclosure in the first place?</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="benefits-of-selective-disclosure">Benefits of Selective Disclosure<a href="https://docs.midnight.network/blog/web3-intro-selective-disclosure#benefits-of-selective-disclosure" class="hash-link" aria-label="Direct link to Benefits of Selective Disclosure" title="Direct link to Benefits of Selective Disclosure">​</a></h2>
<p>When integrated thoughtfully, selective disclosure brings both technical and human-centric advantages to decentralized systems. It enables individuals and organizations to meet privacy expectations and compliance standards without sacrificing functionality. Implementing selective disclosure offers several advantages:</p>
<ul>
<li><strong>Enhanced Privacy</strong>: Selective disclosure enables users to share only the data necessary for a specific interaction, rather than disclosing full credentials or identity documents. This dramatically reduces the surface area for privacy breaches and surveillance. Whether you’re accessing a service, submitting a form, or interacting with a smart contract, your unnecessary personal data stays out of view.</li>
<li><strong>Data Minimization and Compliance</strong>: This principle, core to privacy laws such as GDPR and HIPAA, requires that only the minimum amount of personal data necessary for a task is collected or processed. Selective disclosure naturally enforces this, making it easier for developers and organizations to design privacy-respecting systems by default.</li>
<li><strong>User Empowerment and Control</strong>: In traditional systems, users often hand over their entire dataset for one small verification (e.g., uploading an entire ID just to prove their age). Selective disclosure flips that dynamic, giving users the power to control what they share, when, and with whom. This supports a more user-centric data model, where individuals become the custodians of their credentials.</li>
<li><strong>Reduced Risk of Data Misuse</strong>: The less information that’s exposed, the lower the risk of that data being lost, stolen, or misused. Selective disclosure reduces the need for centralized data storage (e.g., big ID databases), limiting attack vectors for fraud and identity theft. Even if a transaction or credential presentation is intercepted, only the minimum disclosed attribute is visible, keeping the rest safe.</li>
<li><strong>Lower Liability and Operational Overhead for Businesses:</strong> Many organizations would prefer not to collect or store sensitive user data due to the associated risks, regulatory requirements, and administrative burdens. Selective disclosure offers a path forward, enabling users to demonstrate eligibility or compliance without requiring businesses to retain or manage that data. This can reduce costs, simplify operations, and improve overall data security posture.</li>
</ul>
<p>While selective disclosure introduces a powerful model for user-centric privacy, it also comes with its own set of technical and usability challenges. Understanding these limitations is essential for builders and organizations aiming to adopt it responsibly.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="potential-drawbacks">Potential Drawbacks<a href="https://docs.midnight.network/blog/web3-intro-selective-disclosure#potential-drawbacks" class="hash-link" aria-label="Direct link to Potential Drawbacks" title="Direct link to Potential Drawbacks">​</a></h2>
<p>While selective disclosure strengthens privacy and control, implementing it in real-world systems isn’t always straightforward. Here are some of the common challenges:</p>
<ul>
<li><strong>Complexity in Implementation</strong>: Selective disclosure relies on advanced cryptographic techniques, often zero-knowledge proofs or similar privacy-preserving protocols. These require careful design, precise execution, and deep security expertise. For developers new to Web3 or cryptography, like myself, the learning curve can be steep, and mistakes in implementation could compromise the intended privacy protections.</li>
<li><strong>Verification Difficulties</strong>: When only a fragment of a credential or statement is revealed, verifying its authenticity can become more complicated. Systems must be designed to ensure that partial disclosures can be trusted without needing access to the complete original data. This often requires additional metadata or proofs to be included, which can increase the overall complexity of the system and potentially slow down the verification process.</li>
<li><strong>Interoperability Challenges</strong>: Not all decentralized identity systems follow the same formats or standards. As a result, credentials issued by one platform may not be easily verifiable on another, especially when using advanced features like selective disclosure. Without widely adopted protocols and infrastructure, cross-platform compatibility can be limited, reducing the broader utility of selectively disclosed data.</li>
</ul>
<p>While these challenges are real, they’re not insurmountable. Much of the current work in privacy-preserving blockchain development, including projects like Midnight, focuses on making selective disclosure more practical, developer-friendly, and interoperable across various ecosystems.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="use-cases-of-selective-disclosure">Use Cases of Selective Disclosure<a href="https://docs.midnight.network/blog/web3-intro-selective-disclosure#use-cases-of-selective-disclosure" class="hash-link" aria-label="Direct link to Use Cases of Selective Disclosure" title="Direct link to Use Cases of Selective Disclosure">​</a></h2>
<p>Selective disclosure has powerful, real-world applications wherever individuals or organizations need to prove something without oversharing. It’s beneficial in systems that handle sensitive data but also require compliance, trust, and transparency. Here are a few domains where it shines:</p>
<ul>
<li><strong>Digital Identity Verification</strong>: Imagine proving you're over 18 to access a service—without uploading your driver's license or disclosing your full birthdate and address. Selective disclosure lets you present only the relevant fact (e.g., “over 18”) as a verifiable statement, not the whole credential.</li>
<li><strong>Financial Transactions</strong>: Financial platforms often need to confirm identity or transaction legitimacy (for KYC/AML compliance), but users shouldn’t have to share their entire financial history or wallet activity. Selective disclosure enables sharing only what’s legally necessary—no more, no less.</li>
<li><strong>Healthcare</strong>: Patients may need to prove their vaccination status or medical eligibility without disclosing their entire health record. Selective disclosure lets them share only the required attribute (e.g., “vaccinated for X”) while keeping the rest private.</li>
<li><strong>Access Control</strong>: Whether entering a venue or accessing online services, users may be granted or denied access based on attributes like membership, role, or credentials. Selective disclosure can enforce this access logic without revealing unnecessary identifying information.</li>
</ul>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="selective-disclosure-in-the-midnight-network">Selective Disclosure in the Midnight Network<a href="https://docs.midnight.network/blog/web3-intro-selective-disclosure#selective-disclosure-in-the-midnight-network" class="hash-link" aria-label="Direct link to Selective Disclosure in the Midnight Network" title="Direct link to Selective Disclosure in the Midnight Network">​</a></h3>
<p>At Midnight, selective disclosure isn’t just a privacy feature—it’s a core design principle grounded in rational privacy. Rational privacy recognizes that while sensitive data must be protected, real-world applications also require controlled visibility for regulatory, legal, or operational reasons.</p>
<p>Take financial transactions, for example. A decentralized financial platform built on Midnight may want to keep user balances, transfers, and transaction histories private. However, under anti-money laundering (AML) or know-your-customer (KYC) regulations, certain information may need to be revealed—but only to authorized parties, and only under specific legal conditions.</p>
<p>This is where selective disclosure and zero-knowledge proofs come together. Using Midnight's Compact smart contract language, developers can build logic that proves a transaction is valid (e.g., below a certain threshold, or compliant with regulatory filters) without revealing the full transaction details. Meanwhile, if a regulatory audit is triggered, users or auditors could selectively disclose the required pieces of information—nothing more.</p>
<p>In this way, Midnight enables builders to create applications that respect individual privacy <em>and</em> comply with regulatory requirements. It’s a practical application of selective disclosure that mirrors how real-world trust operates: share only what’s needed, only when it’s needed, and only with the right people.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/web3-intro-selective-disclosure#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>Selective disclosure offers a flexible and powerful approach to privacy—one that meets people where they are, allowing them to control their information. It enables individuals to share just enough information to satisfy requirements, without exposing more than necessary. Whether you're verifying an identity, proving a credential, or complying with financial regulations, selective disclosure makes privacy practical and effective.</p>
<p>At Midnight, this concept becomes more than theoretical. It’s embedded into the platform’s architecture through Compact smart contracts and zero-knowledge proofs, allowing builders to create systems that balance user protection with real-world needs. It’s rational privacy in action.</p>
<p>If you're exploring how to build for privacy in Web3—or just learning how these systems work—I encourage you to check out the <a href="https://docs.midnight.network/" target="_blank" rel="noopener noreferrer">Midnight docs</a> and <a href="https://docs.midnight.network/academy" target="_blank" rel="noopener noreferrer">Academy</a> for more examples and technical insight, as well as a detailed list of further reading about selective disclosure below:</p>
<ul>
<li><a href="https://www.identity.com/what-is-selective-disclosure/" target="_blank" rel="noopener noreferrer">What Is Selective Disclosure? – Identity.com</a></li>
<li><a href="https://www.dock.io/post/selective-disclosure" target="_blank" rel="noopener noreferrer">Selective Disclosure Guide – Dock.io</a></li>
<li><a href="https://frostbrowntodd.com/a-refresher-on-selective-disclosure-and-anti-fraud-rules/" target="_blank" rel="noopener noreferrer">A Refresher on Selective Disclosure and Anti-Fraud Rules – Frost Brown Todd</a></li>
<li><a href="https://docs.midnight.network/academy/module-3" target="_blank" rel="noopener noreferrer">Midnight Academy Module 3 – Midnight Docs</a></li>
<li><a href="https://docs.midnight.network/develop/reference/compact/explicit-disclosure" target="_blank" rel="noopener noreferrer">Explicit Disclosure Reference – Midnight Docs</a></li>
<li><a href="https://docs.midnight.network/learn/introduction/use-cases/digital-assets" target="_blank" rel="noopener noreferrer">Digital Assets Use Cases – Midnight Docs</a></li>
</ul>
<p>And, as always, if you’ve come across any great learning resources on privacy or identity in Web3, I’d love to see them—drop a comment or reply with your favorites!</p>
<p>I'll be back next week with more insights as I continue unpacking the key building blocks that make decentralized systems work.</p>]]></content:encoded>
            <category>intro</category>
            <category>blockchain</category>
            <category>beginner</category>
            <category>web3</category>
        </item>
        <item>
            <title><![CDATA[Learning Web3 from the Ground Up - Unpacking Merkle Trees and DAOs]]></title>
            <link>https://docs.midnight.network/blog/web3-intro-merkle-trees-and-dao</link>
            <guid>https://docs.midnight.network/blog/web3-intro-merkle-trees-and-dao</guid>
            <pubDate>Fri, 06 Jun 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[As I continue my Web3 learning journey, I've mostly been working through Cardano Academy and Midnight Academy and documenting my findings in the Midnight Dev Diaries. But this week, I decided to branch out and explore a broader range of beginner-friendly resources across the ecosystem. That led me to two foundational concepts that kept coming up: Merkle Trees and Decentralized Autonomous Organizations (DAOs).]]></description>
            <content:encoded><![CDATA[<p>As I continue my Web3 learning journey, I've mostly been working through <a href="https://cardanofoundation.org/academy" target="_blank" rel="noopener noreferrer">Cardano Academy</a> and <a href="https://docs.midnight.network/academy/" target="_blank" rel="noopener noreferrer">Midnight Academy</a> and documenting my findings in the <a href="https://docs.midnight.network/blog" target="_blank" rel="noopener noreferrer">Midnight Dev Diaries</a>. But this week, I decided to branch out and explore a broader range of beginner-friendly resources across the ecosystem. That led me to two foundational concepts that kept coming up: Merkle Trees and Decentralized Autonomous Organizations (DAOs).</p>
<p>These two topics sit at the heart of blockchain's promise—ensuring transparency, verifying data integrity, and enabling decentralized governance. I've broken down the key takeaways below, and if you're curious to go deeper, check out the list of helpful resources I've included at the end.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="merkle-trees-ensuring-data-integrity">Merkle Trees: Ensuring Data Integrity<a href="https://docs.midnight.network/blog/web3-intro-merkle-trees-and-dao#merkle-trees-ensuring-data-integrity" class="hash-link" aria-label="Direct link to Merkle Trees: Ensuring Data Integrity" title="Direct link to Merkle Trees: Ensuring Data Integrity">​</a></h2>
<p>A Merkle Tree, also known as a hash tree, is a data structure that enables efficient and secure verification of large data sets. At its core, it is a special kind of <a href="https://www.geeksforgeeks.org/binary-tree-data-structure/" target="_blank" rel="noopener noreferrer">binary tree</a>—a structure many Web2 developers will recognize. In a binary tree, each node has at most two children. What makes Merkle Trees different is that instead of storing values directly, they store hashes. These hashes represent data blocks and provide a compact, tamper-evident summary of all the data in the tree.</p>
<p>Merkle Trees are crucial in blockchain because they allow a large set of data (like thousands of transactions) to be verified efficiently and securely. Instead of needing to check every transaction, you only need a small subset of hashes (called a Merkle proof) to confirm that a transaction is part of a block.</p>
<p>Here's a simplified breakdown of how a Merkle Tree is built:</p>
<ol>
<li>Start with data blocks (like transactions in a block).</li>
<li>Each data block is hashed individually. These hashes become the leaf nodes.</li>
<li>Pairs of leaf hashes are then hashed together to form the next level of the tree.</li>
<li>This process continues, combining hashes pairwise and moving up the tree until you're left with a single root hash, often called the Merkle Root.</li>
</ol>
<p>In short, each leaf node contains the hash of a data block, each non-leaf node contains the hash of its child nodes, and the root hash represents the entire data set. Any change to any single data block will change its hash, which then cascades up the tree, altering the Merkle Root. This property is crucial for blockchain systems, where data immutability and integrity are paramount.</p>
<p>In practice, Merkle Trees are used in:</p>
<ul>
<li>Blockchain Transactions: Ensuring that transactions within a block are untampered.</li>
<li>Proof of Reserves: Cryptocurrency exchanges use Merkle Trees to prove they hold sufficient assets to cover user deposits without revealing individual account balances.</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="daos-redefining-organizational-governance">DAOs: Redefining Organizational Governance<a href="https://docs.midnight.network/blog/web3-intro-merkle-trees-and-dao#daos-redefining-organizational-governance" class="hash-link" aria-label="Direct link to DAOs: Redefining Organizational Governance" title="Direct link to DAOs: Redefining Organizational Governance">​</a></h2>
<p>Decentralized Autonomous Organizations (DAOs) are organizations governed by smart contracts on a blockchain, eliminating the need for centralized leadership. They replace traditional top-down leadership with transparent, community-driven governance, all enforced through code—specifically, smart contracts.</p>
<p>In a DAO, rules and operations are programmed upfront and stored on-chain. That means anyone can inspect how decisions are made, how funds are allocated, and how proposals are handled. Everything is transparent and auditable by default. Instead of a single CEO or board, decisions are made collectively by the community. Token holders can vote on proposals, and the outcome reflects the weight of their stake in the project. This collective decision-making process is what makes DAOs decentralized by design. And because those rules are enforced automatically by smart contracts, DAOs can operate autonomously without relying on manual intervention or centralized gatekeepers. Once a proposal passes, the smart contract executes the decision just as it was coded—no waiting for someone to push a button.</p>
<p>However, DAOs also face challenges, including:</p>
<ul>
<li>Security Risks: Vulnerabilities in smart contracts can be exploited.</li>
<li>Governance Issues: Low voter participation and token concentration can lead to centralization.</li>
</ul>
<p>DAOs have been utilized in various domains, such as:</p>
<ul>
<li>Investment Funds: Pooling resources to invest in projects collectively.</li>
<li>Charitable Organizations: Deciding on fund allocation through member voting.</li>
<li>Protocol Governance: Managing and updating blockchain protocols.</li>
</ul>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="how-dao-voting-works-the-simple-version">How DAO Voting Works (The Simple Version)<a href="https://docs.midnight.network/blog/web3-intro-merkle-trees-and-dao#how-dao-voting-works-the-simple-version" class="hash-link" aria-label="Direct link to How DAO Voting Works (The Simple Version)" title="Direct link to How DAO Voting Works (The Simple Version)">​</a></h3>
<p>Most DAOs use token-based voting to reach decisions:</p>
<ol>
<li>Members hold a governance token, which gives them the right to vote.</li>
<li>A proposal is submitted (for example, "Fund a new development project").</li>
<li>Token holders vote for or against the proposal.</li>
<li>If a certain threshold is reached (like a quorum or majority), the proposal is accepted and automatically executed by smart contracts.</li>
</ol>
<p>Beyond on-chain voting, many DAOs utilize off-chain governance platforms, such as Snapshot (widely used for token-weighted voting), Aragon Voice, Tally (commonly paired with Governor contracts), or Coordinape (used for decentralized grants and peer voting). These tools offer flexibility, reduce gas costs, and enable more experimental governance setups.</p>
<p>The more tokens you hold, the more voting power you wield. This is similar to how shareholders vote in corporations, but the process is entirely on-chain, without intermediaries.</p>
<p>Some DAOs use <a href="https://blog.colony.io/what-is-quadratic-voting-a-guide-to-dao-decision-making/" target="_blank" rel="noopener noreferrer">quadratic voting</a> or other mechanisms to prevent whales (large holders) from dominating the process, but the core idea remains the same: the community drives the roadmap.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="interplay-between-merkle-trees-and-daos">Interplay Between Merkle Trees and DAOs<a href="https://docs.midnight.network/blog/web3-intro-merkle-trees-and-dao#interplay-between-merkle-trees-and-daos" class="hash-link" aria-label="Direct link to Interplay Between Merkle Trees and DAOs" title="Direct link to Interplay Between Merkle Trees and DAOs">​</a></h2>
<p>While Merkle Trees and DAOs serve different purposes in the blockchain world—one focused on data integrity, the other on governance—they complement each other in powerful ways. Together, DAOs can remain secure, transparent, and efficient even as they scale.</p>
<p>For example, instead of storing a complete list of eligible voters, a DAO can publish a Merkle Root representing that list. Individual voters then submit a short cryptographic proof (a Merkle proof) to confirm they're included. This keeps voting lightweight, private, and verifiable. The same method can be used to verify grant recipients or airdrop eligibility—ensuring integrity while saving space and gas costs.</p>
<p>Merkle Trees also act as a bridge between off-chain and on-chain activity. Whether it's tracking voting done through Snapshot or verifying community identities, these proofs give DAOs a scalable way to maintain trust and transparency—even as they grow.</p>
<p>Merkle Trees provide DAOs with the cryptographic foundation to scale governance without sacrificing transparency or efficiency. Whether it's verifying voter eligibility, proving fund distribution, or bridging off-chain data to on-chain actions, Merkle Trees help DAOs maintain trust and accountability as they grow. It's a powerful example of how foundational blockchain structures support and enhance decentralized decision-making.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/web3-intro-merkle-trees-and-dao#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>Together, Merkle Trees and DAOs demonstrate how blockchain technology strikes a balance between technical efficiency and community empowerment. Merkle Trees ensure that large sets of data—like transactions, votes, or recipient lists, can be verified quickly and securely. At the same time, DAOs introduce a way for communities to govern decentralized projects in a transparent and fair manner. When combined, they offer a practical solution for scaling trust without sacrificing decentralization. Whether you're verifying a vote, distributing funds, or proving membership, these tools help build the infrastructure that makes Web3 more than just a technological shift—it becomes a new model for how people coordinate, decide, and build together.</p>
<p>Exploring beyond the Cardano and Midnight Academies for the first time in this journey provided me with a broader understanding of how these technologies are explained and applied across the space. If you're learning along with me or want to dig deeper into any of the topics covered, here are some of the materials I found especially helpful:</p>
<ul>
<li><a href="https://www.youtube.com/watch?v=3AcQyTs_Es4" target="_blank" rel="noopener noreferrer">Video: Merkle Trees Explained – Simply Explained</a></li>
<li><a href="https://www.geeksforgeeks.org/introduction-to-merkle-tree/" target="_blank" rel="noopener noreferrer">Introduction to Merkle Trees – Geeks for Geeks</a></li>
<li><a href="https://limechain.tech/blog/what-are-daos" target="_blank" rel="noopener noreferrer">What Are DAOs – LimeChain Blog</a></li>
<li><a href="https://academy.moralis.io/blog/blockchain-transparency-exploring-merkle-trees-and-proof-of-reserves" target="_blank" rel="noopener noreferrer">Blockchain Transparency: Merkle Trees &amp; Proof of Reserves – Moralis</a></li>
<li><a href="https://www.bitpanda.com/academy/en/lessons/everything-you-need-to-know-about-merkle-trees/" target="_blank" rel="noopener noreferrer">Everything You Need to Know About Merkle Trees – Bitpanda Academy</a></li>
<li><a href="https://www.chainalysis.com/blog/introduction-to-decentralized-autonomous-organizations-daos/" target="_blank" rel="noopener noreferrer">Introduction to DAOs – Chainalysis</a></li>
</ul>
<p>And if you’ve come across other great resources while learning Web3 basics, I’d love to hear them—drop a comment or reply with your favorites! You can also check out the <a href="https://docs.midnight.network/" target="_blank" rel="noopener noreferrer">Midnight docs</a> for more technical deep dives and real-world use cases.</p>
<p>Stay tuned for next week's exploration into other core components of the decentralized world.</p>]]></content:encoded>
            <category>intro</category>
            <category>blockchain</category>
            <category>beginner</category>
            <category>web3</category>
        </item>
        <item>
            <title><![CDATA[Learning Web3 from the Ground Up - A Quick History of Blockchain and Why Tokens Matter]]></title>
            <link>https://docs.midnight.network/blog/web3-intro-history-and-tokens</link>
            <guid>https://docs.midnight.network/blog/web3-intro-history-and-tokens</guid>
            <pubDate>Fri, 30 May 2025 00:00:00 GMT</pubDate>
            <description><![CDATA[Explore how blockchain platforms have evolved from Bitcoin to Midnight and why tokens are essential to powering decentralized ecosystems.]]></description>
            <content:encoded><![CDATA[<p>Explore how blockchain platforms have evolved from Bitcoin to Midnight and why tokens are essential to powering decentralized ecosystems.</p>
<p>I’m still in the early stages of my Web3 learning journey (see my previous posts on the <a href="https://docs.midnight.network/blog" target="_blank" rel="noopener noreferrer">Midnight Dev Diaries</a>!), and each week unveils new insights and "aha" moments for me. This week, I delved deeper into the Intro to Blockchain section of <a href="https://academy.cardanofoundation.org/" target="_blank" rel="noopener noreferrer">Cardano Academy</a>, along with <a href="https://docs.midnight.network/academy/module-1" target="_blank" rel="noopener noreferrer">Module 1 of the Midnight Developer Academy</a>. Rather than just focusing on the technical aspects and step-by-step guides, I shifted my attention to understanding the evolution of blockchain platforms over time, particularly the journey from Bitcoin to more advanced systems like Midnight. This exploration also led me to reflect on the crucial role that tokens play in the functionality and sustainability of decentralized systems. Tokens aren't just mere digital assets; they serve as the economic backbone that incentivizes participation and secures the network. In this post, I want to share two significant topics that helped clarify my understanding this week:</p>
<ol>
<li>How blockchain platforms have progressed from Bitcoin to Midnight</li>
<li>How tokens keep decentralized systems running through economic incentives</li>
</ol>
<p>Let's jump in!</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="the-evolution-of-blockchain-platforms">The Evolution of Blockchain Platforms<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#the-evolution-of-blockchain-platforms" class="hash-link" aria-label="Direct link to The Evolution of Blockchain Platforms" title="Direct link to The Evolution of Blockchain Platforms">​</a></h2>
<p>When I first heard about blockchain, I mostly associated it with Bitcoin. However, after diving into more educational content, it became clear that this space has undergone multiple waves of innovation, each building on the last. Here's how we got from the first Bitcoin transaction to the emergence of privacy-focused blockchains like Midnight.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="bitcoin-2009-a-new-way-to-move-money">Bitcoin (2009): A New Way to Move Money<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#bitcoin-2009-a-new-way-to-move-money" class="hash-link" aria-label="Direct link to Bitcoin (2009): A New Way to Move Money" title="Direct link to Bitcoin (2009): A New Way to Move Money">​</a></h3>
<p>Blockchain began with Bitcoin, launched in 2009 by the pseudonymous Satoshi Nakamoto. It introduced the idea of a decentralized, peer-to-peer payment network where anyone could send money without relying on banks or intermediaries. Transactions were validated through <strong>proof-of-work</strong> and recorded on a public ledger that anyone could inspect—but no one could alter.</p>
<p>This was revolutionary. Bitcoin proved that decentralized digital money was possible—and, in 2010, someone even traded 10,000 BTC for two pizzas. That transaction is now famous for being the first real-world use of Bitcoin and the most expensive pizza ever bought.</p>
<p>Still, Bitcoin was narrowly focused: it could securely transfer value but not much else.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="ethereum-2015-the-rise-of-programmable-blockchains">Ethereum (2015): The Rise of Programmable Blockchains<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#ethereum-2015-the-rise-of-programmable-blockchains" class="hash-link" aria-label="Direct link to Ethereum (2015): The Rise of Programmable Blockchains" title="Direct link to Ethereum (2015): The Rise of Programmable Blockchains">​</a></h3>
<p>Ethereum took the core idea of blockchain and expanded it into something far more flexible. It introduced <strong>smart contracts</strong>—self-executing programs that live on the blockchain and run when certain conditions are met. This turned Ethereum into what its creators called a "world computer": a decentralized platform for building applications that couldn't be shut down or censored.</p>
<p>Ethereum is guided by six key principles:</p>
<ul>
<li><strong>Atomicity</strong>: All or nothing execution</li>
<li><strong>Synchrony</strong>: Consistent state across nodes</li>
<li><strong>Provenance</strong>: Trackable origins</li>
<li><strong>Permanence</strong>: Durable data storage</li>
<li><strong>Immortality</strong>: Apps that can't be turned off</li>
<li><strong>Immutability</strong>: Data that can't be changed once written</li>
</ul>
<p>These principles laid the groundwork for DeFi, NFTs, and DAOs, giving rise to an entire ecosystem of decentralized applications. However, this flexibility came with trade-offs, especially around scalability and high transaction fees.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="third-generation-platforms-cardano-and-beyond">Third-Generation Platforms: Cardano and Beyond<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#third-generation-platforms-cardano-and-beyond" class="hash-link" aria-label="Direct link to Third-Generation Platforms: Cardano and Beyond" title="Direct link to Third-Generation Platforms: Cardano and Beyond">​</a></h3>
<p>Enter the third generation of blockchains. These platforms aim to solve the Blockchain Trilemma: how to balance scalability, security, and decentralization—without sacrificing any of them.</p>
<p>Cardano, launched in 2017, is one of the most notable examples. It uses <strong>proof-of-stake</strong> to reduce energy consumption and improve efficiency. But what stood out most to me is that it was built from the ground up with formal research and peer-reviewed design—a very different approach from its predecessors.</p>
<p>Cardano also addresses security at the protocol level. Unlike Ethereum, where new tokens are created via smart contracts (which can introduce vulnerabilities), tokens on Cardano are treated as <strong>native assets</strong>. This means they inherit the same security guarantees as the platform's native currency, ADA, and don't require a custom contract to be created or maintained.</p>
<p>Other improvements include:</p>
<ul>
<li>Layer 2 scaling solutions like Hydra</li>
<li>Support for sidechains to improve interoperability</li>
<li>A focus on sustainability by reducing resource consumption through staking is also needed</li>
</ul>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="midnight-the-privacy-layer-of-the-future">Midnight: The Privacy Layer of the Future<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#midnight-the-privacy-layer-of-the-future" class="hash-link" aria-label="Direct link to Midnight: The Privacy Layer of the Future" title="Direct link to Midnight: The Privacy Layer of the Future">​</a></h3>
<p>Midnight represents a new phase in blockchain evolution focused on privacy and regulatory compliance.</p>
<p>It uses tools like <a href="https://docs.midnight.network/learn/understanding-midnights-technology/zero-knowledge-proofs" target="_blank" rel="noopener noreferrer">zero-knowledge proofs</a> to allow data to remain private on a public blockchain. At the same time, it supports selective disclosure, meaning users can prove compliance (for example, with regulations or audits) without revealing everything. In other words, Midnight blends the decentralization and transparency of blockchain with the privacy needed for real-world use cases—whether it's secure communication, confidential data sharing, or regulated financial applications.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="tokenomics-101-why-tokens-are-more-than-just-coins">Tokenomics 101: Why Tokens Are More Than Just Coins<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#tokenomics-101-why-tokens-are-more-than-just-coins" class="hash-link" aria-label="Direct link to Tokenomics 101: Why Tokens Are More Than Just Coins" title="Direct link to Tokenomics 101: Why Tokens Are More Than Just Coins">​</a></h2>
<p>Once I understood how blockchains evolved, the next big question was: How do they keep running without a central company in charge? The answer lies in tokenomics, the economic design behind each network.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="tokens-as-utility">Tokens as Utility<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#tokens-as-utility" class="hash-link" aria-label="Direct link to Tokens as Utility" title="Direct link to Tokens as Utility">​</a></h3>
<p>Every blockchain has a native token—Bitcoin has BTC, Ethereum has ETH, Cardano has ADA. But tokens aren't just money. Tokens are <strong>used</strong> to pay transaction fees, deploy contracts, access network features, and more.</p>
<p>This creates a built-in incentive system. If using the network requires tokens, and the network is valuable, then tokens have value too.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="tokens-as-incentives">Tokens as Incentives<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#tokens-as-incentives" class="hash-link" aria-label="Direct link to Tokens as Incentives" title="Direct link to Tokens as Incentives">​</a></h3>
<p>Blockchain networks need participants to validate transactions, secure the network, and maintain consensus. In return, they reward those participants with tokens.</p>
<p>For example:</p>
<ul>
<li>Miners earn BTC for adding blocks to the Bitcoin chain.</li>
<li>Validators earn ADA in Cardano's proof-of-stake system to secure the network.</li>
</ul>
<p>These rewards are often inflationary, meaning new tokens are regularly minted and distributed. However, that inflation funds the network's infrastructure.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="tokens-as-governance-tools"><strong>Tokens as Governance Tools</strong><a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#tokens-as-governance-tools" class="hash-link" aria-label="Direct link to tokens-as-governance-tools" title="Direct link to tokens-as-governance-tools">​</a></h3>
<p>Some tokens also allow holders to vote on decisions. In DAO-style systems, governance tokens let users decide protocol upgrades, fee structures, or treasury spending. This turns token holders into active stakeholders, not just passive investors.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="inflationary-vs-deflationary-models">Inflationary vs. Deflationary Models<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#inflationary-vs-deflationary-models" class="hash-link" aria-label="Direct link to Inflationary vs. Deflationary Models" title="Direct link to Inflationary vs. Deflationary Models">​</a></h3>
<p>The way a token's supply is designed can shape behavior:</p>
<ul>
<li>Inflationary models (e.g., Polkadot) continually mint new tokens to incentivize active participation.</li>
<li>Deflationary models (e.g., Bitcoin) cap the total supply or burn tokens to reduce circulation.</li>
</ul>
<p>Each model has trade-offs: Inflation can reward contributors but reduce long-term value, while deflation can increase scarcity but discourage spending.</p>
<p>Many modern projects use hybrid models. For example, Ethereum burns a portion of transaction fees to offset inflation while still issuing new tokens to validators.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="conclusion">Conclusion<a href="https://docs.midnight.network/blog/web3-intro-history-and-tokens#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion">​</a></h2>
<p>Understanding where blockchain originated and how tokens drive these systems provided me with a much clearer view of the ecosystem I'm entering. It's not just about code or decentralization for its sake. It's about designing systems where people are economically motivated to do the right thing. And just like the technology itself, the economics behind blockchains are still evolving.</p>
<p>If you're learning Web3 too or just curious how all the pieces fit together, be sure to take a look at the <a href="https://docs.midnight.network/" target="_blank" rel="noopener noreferrer">Midnight docs</a>, <a href="https://docs.midnight.network/blog" target="_blank" rel="noopener noreferrer">Dev Diaries</a>, and the newly published modules in the <a href="https://docs.midnight.network/academy/module-1" target="_blank" rel="noopener noreferrer">Midnight Developer Academy</a>.</p>
<p>More foundational concepts are coming next week—stay tuned!</p>]]></content:encoded>
            <category>intro</category>
            <category>blockchain</category>
            <category>beginner</category>
            <category>web3</category>
        </item>
    </channel>
</rss>