<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: midnight</title>
    <description>The latest articles tagged 'midnight' on DEV Community.</description>
    <link>https://dev.to/t/midnight</link>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tag/midnight"/>
    <language>en</language>
    <item>
      <title>A Travel Rule gate where the verdict is public and the customer data is not</title>
      <dc:creator>Fred Santana</dc:creator>
      <pubDate>Fri, 13 Nov 2026 19:50:19 +0000</pubDate>
      <link>https://dev.to/midnight-aliit/a-travel-rule-gate-where-the-verdict-is-public-and-the-customer-data-is-not-423h</link>
      <guid>https://dev.to/midnight-aliit/a-travel-rule-gate-where-the-verdict-is-public-and-the-customer-data-is-not-423h</guid>
      <description>&lt;p&gt;The FATF Travel Rule (Recommendation 16) asks a virtual-asset service provider to collect and pass on originator and beneficiary information with a transfer. Data-protection law asks the same provider to minimise that information and keep it away from anyone who does not need it. On a transparent chain those two demands collide: either the identity data goes on chain, or the compliance check happens somewhere nobody can verify.&lt;/p&gt;

&lt;p&gt;At DPO2U we are building a gate in Compact, Midnight's smart-contract language, that splits the problem along a different line: the verdict is public, the data is shielded. This post walks through how it works and where its limits are.&lt;/p&gt;

&lt;h2&gt;
  
  
  What goes on chain and what doesn't
&lt;/h2&gt;

&lt;p&gt;The contract's state separates cleanly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Public: a per-transfer verdict (PASS or FAIL), a coarse reason code, a root commitment of each party's data, the hash of the policy that produced the verdict, and the metadata of any disclosure grant.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shielded: names, account identifiers, document numbers and the amount. They enter the circuit as witnesses and never touch the ledger.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Party&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name_hash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;account_hash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;doc_hash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;vasp_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;originator_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Party&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;beneficiary_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Party&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;transfer_amount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The rule runs inside the proof
&lt;/h2&gt;

&lt;p&gt;evaluateTransfer reads the private witnesses and computes the verdict in-circuit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name_hash&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;account_hash&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt;
              &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name_hash&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;account_hash&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;above&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amt&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;threshold_cents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;id_ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;above&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;doc_hash&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;vasp_ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attested_vasps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vasp_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vasp_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason code is deliberately coarse: 1 missing party info, 2 missing originator ID above the threshold, 3 counterparty VASP not attested, 4 sanctions hit. An observer learns that a transfer failed and roughly why, not who was involved or how much moved.&lt;/p&gt;

&lt;p&gt;Two details matter for auditors. The amount is compared against the threshold inside the proof, so the threshold is public but the amount is not. And every verdict stores policy_hash, so you can later prove which version of the rule produced it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Per-field commitments, so one field can be opened alone
&lt;/h2&gt;

&lt;p&gt;Each party's data is committed field by field, with a label so that opening one field reveals nothing about the others:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="n"&gt;pure&lt;/span&gt; &lt;span class="n"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;field_commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;persistentCommit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The four commitments are hashed into a root stored on chain. A supervisor who receives one field off chain can recompute the root from that opening plus the other three commitments, and check it against the ledger.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linked contracts (Ledger 9)
&lt;/h2&gt;

&lt;p&gt;With contract-to-contract calls on Midnight, the gate checks two other contracts directly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the counterparty VASP passes if this VASP attested it or it holds a valid passport in a PsavPassport contract;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;releaseTransfer reverts unless this VASP itself holds a current attestation in a ComplianceRegistry contract.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasAttestation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self_company_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="s2"&gt;"VASP has no compliance attestation in the registry"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No off-chain orchestrator has to be trusted to have run those checks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signed sanctions lists
&lt;/h2&gt;

&lt;p&gt;Proving non-membership in a sanctions list inside the circuit is still an open problem for us (a naive Set.member on a private hash would publish the hash). What the gate does prove is which list the screening was made against: the list snapshot has to be signed with Schnorr over Jubjub by a registered provider, and still valid at block time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;assert(blockTimeLt(disclose(l.valid_until)), "Sanctions list expired");
const ok = jubjubSchnorrVerify&amp;lt;4&amp;gt;(sanctions_message(l.list_root, l.provider_id, l.issued_at, l.valid_until),
                                  l.signature, sanctions_provider_key.lookup(id));
assert(disclose(ok), "Sanctions list signature invalid");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The "not on the list" statement remains the VASP's, but it is now bound to a signed, unexpired list that is recorded with the verdict.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four eyes before anyone sees data
&lt;/h2&gt;

&lt;p&gt;A supervisor can get access, but not on the operator's say-so alone. The VASP owner requests a grant with a scope bitmask (name, account, document, VASP), an expiry and the hash of its legal basis. The grant stays inactive until a separate approver key, typically the DPO, calls approveDisclosure. The constructor refuses an approver equal to the owner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export circuit isGrantActive(grant_id: Bytes&amp;lt;32&amp;gt;): Boolean {
  return grant_scope.member(disclose(grant_id))
      &amp;amp;&amp;amp; grant_approved.member(disclose(grant_id))
      &amp;amp;&amp;amp; !grant_revoked.member(disclose(grant_id))
      &amp;amp;&amp;amp; blockTimeLt(grant_expires.lookup(disclose(grant_id)));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The data itself travels off chain, encrypted per grant. The chain holds the proof that access was requested, approved, scoped and time-limited.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;p&gt;Sanctions non-membership is attested by the VASP, not proven in-circuit.&lt;br&gt;
The counterparty VASP id is public on purpose: it is a regulated entity's identity, not customer data.&lt;br&gt;
The "viewing key" is an application-level per-grant key, not a Midnight wallet viewing key.&lt;br&gt;
The v10 version with linked contracts has been checked with compactc 0.36.0 and in the simulator; it has not been exercised on a public network yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Midnight
&lt;/h2&gt;

&lt;p&gt;disclose() is the part I keep coming back to. In Compact, private data cannot reach public state unless you wrap it explicitly, so the list of everything this gate makes public is literally a grep away. For a compliance product, that turns "we minimise data" from a policy statement into something an examiner can check in the code.&lt;/p&gt;

&lt;p&gt;If you are building for regulated finance on Midnight: which check would you move in-circuit first, sanctions non-membership or the amount threshold per corridor?&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>zeroknowledge</category>
      <category>privacy</category>
      <category>compliance</category>
    </item>
    <item>
      <title>The Chain Is a Clock, Not a Witness: Proof of Priority for Skate Clips on Midnight</title>
      <dc:creator>Wes Huber</dc:creator>
      <pubDate>Wed, 09 Sep 2026 20:12:59 +0000</pubDate>
      <link>https://dev.to/wbaxterh/the-chain-is-a-clock-not-a-witness-proof-of-priority-for-skate-clips-on-midnight-2017</link>
      <guid>https://dev.to/wbaxterh/the-chain-is-a-clock-not-a-witness-proof-of-priority-for-skate-clips-on-midnight-2017</guid>
      <description>&lt;p&gt;Skateboarding has had proof for forty years. It is called the clip. Land something, film it, post it, done. Two things are breaking that right now.&lt;/p&gt;

&lt;p&gt;The first is old. Footage gets saved for video parts, sometimes for a year or more, and posting early to prove you did it first burns the clip. "Who did it first" arguments get settled by upload dates and by who has the louder crew.&lt;/p&gt;

&lt;p&gt;The second is new. AI video is about to make a clip of a kickflip down a twelve stair cost nothing to produce. When that lands, the clip stops being proof, and a culture that cares this much about realness will want a way to say "this is real footage, filmed here, that day."&lt;/p&gt;

&lt;p&gt;I build TrickBook, a skate app with a few hundred riders on the App Store, and I have been working on a Midnight feature called Claimed to handle both. This post is about the one idea in the design that makes a blockchain worth having here, and about a one-line bug I found in the official docs pattern while building it. Everything below runs on a local devnet today, and the commands to reproduce it are at the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a commitment buys you
&lt;/h2&gt;

&lt;p&gt;A rider lands a trick. The app hashes the clip on the phone and builds a commitment over the clip hash, the trick, the spot, the capture time, a fresh salt, and a secret that never leaves the device:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct ClaimPreimage {
  tag: Bytes&amp;lt;32&amp;gt;,
  clipHash: Bytes&amp;lt;32&amp;gt;,
  trickId: Bytes&amp;lt;32&amp;gt;,
  spotId: Bytes&amp;lt;32&amp;gt;,
  capturedAt: Uint&amp;lt;64&amp;gt;,
  salt: Bytes&amp;lt;32&amp;gt;,
  secret: Bytes&amp;lt;32&amp;gt;
}

export circuit claimCommitment(
  secret: Bytes&amp;lt;32&amp;gt;, salt: Bytes&amp;lt;32&amp;gt;, clipHash: Bytes&amp;lt;32&amp;gt;,
  trickId: Bytes&amp;lt;32&amp;gt;, spotId: Bytes&amp;lt;32&amp;gt;, capturedAt: Uint&amp;lt;64&amp;gt;
): Bytes&amp;lt;32&amp;gt; {
  return persistentHash&amp;lt;ClaimPreimage&amp;gt;(ClaimPreimage {
    tag: pad(32, "tb:claim:v1"),
    clipHash: clipHash, trickId: trickId, spotId: spotId,
    capturedAt: capturedAt, salt: salt, secret: secret
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That 32-byte hash is the only thing that goes on chain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export ledger claims: HistoricMerkleTree&amp;lt;16, Bytes&amp;lt;32&amp;gt;&amp;gt;;

export circuit seal(commitment: Bytes&amp;lt;32&amp;gt;): [] {
  claims.insert(disclose(commitment));
  claimCount.increment(1);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nobody can read a trick, a spot, a date, or a rider out of that. The clip stays on the phone. Later, when the part drops or somebody starts an argument, the rider can reveal the body and prove it matches, exactly once, with a nullifier. That part is the standard commit and reveal pattern, and Midnight's &lt;code&gt;example-bboard&lt;/code&gt; template gets you most of the way there.&lt;/p&gt;

&lt;p&gt;The interesting question is the date. If the rider says "I sealed this before March 1," what proves it? Not the phone. The capture time inside the commitment is just a number the phone supplied, and a phone clock can be set to anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  The clock
&lt;/h2&gt;

&lt;p&gt;Every seal inserts one leaf into the Merkle tree, and every insert changes the tree's root. Midnight's &lt;code&gt;HistoricMerkleTree&lt;/code&gt; remembers the old roots as well as the current one, and &lt;code&gt;checkRoot&lt;/code&gt; accepts any root the tree has ever recorded.&lt;/p&gt;

&lt;p&gt;So the priority proof does not prove membership under the current root. It proves membership under the root that was current at a specific earlier block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export circuit provePriority(
  clipHash: Bytes&amp;lt;32&amp;gt;, trickId: Bytes&amp;lt;32&amp;gt;, spotId: Bytes&amp;lt;32&amp;gt;,
  capturedAt: Uint&amp;lt;64&amp;gt;, salt: Bytes&amp;lt;32&amp;gt;
): [Bytes&amp;lt;32&amp;gt;, Bytes&amp;lt;32&amp;gt;] {
  const secret = riderSecret();
  const c = claimCommitment(secret, salt, clipHash, trickId, spotId, capturedAt);
  const path = findClaimPath(c);
  assert(path.leaf == c, "path is not for this claim");
  assert(claims.checkRoot(disclose(merkleTreePathRoot&amp;lt;16, Bytes&amp;lt;32&amp;gt;&amp;gt;(path))), "no sealed claim matches");
  return [disclose(trickId), disclose(spotId)];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the leaf was under the tree's root at block 12, it was sealed by block 12. The chain stamped block 12 with a time nobody can rewind. The rider discloses the trick, the spot, and a root. Not the clip, not the capture time, not which leaf is theirs.&lt;/p&gt;

&lt;p&gt;Think of a notary's ledger that gets photographed at the end of every day. If your entry is in Tuesday's photo, you signed by Tuesday, whatever date you wrote next to your name. The chain is the camera. It is not a witness to the trick. It is a clock.&lt;/p&gt;

&lt;p&gt;Two things I had to verify before trusting this, because the docs do not spell either out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does the tree really keep every root?&lt;/strong&gt; Yes, until a contract calls &lt;code&gt;resetHistory()&lt;/code&gt;, which this one never does. The generated JavaScript for &lt;code&gt;claims.insert&lt;/code&gt; appends the new root into a history map and nothing ever removes one. And it holds live: after two seals the history held three roots, and a proof against the older one verified on chain after the current root had moved on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do you build a proof against an old root when the witness only sees current state?&lt;/strong&gt; Do not fight the witness. The indexer's &lt;code&gt;queryContractState&lt;/code&gt; takes a block height, so the service fetches the contract state as of that block, derives the Merkle path from that snapshot with the generated &lt;code&gt;findPathForLeaf&lt;/code&gt;, and hands that path to the witness for one call. The circuit's own &lt;code&gt;checkRoot&lt;/code&gt; over recorded history is what keeps that safe.&lt;/p&gt;

&lt;p&gt;The end-to-end run on a local devnet, ten checks, about three minutes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PASS: seal A [block 12]
PASS: seal B advances the root [block 15]
PASS: root after A is still in claims.history() [3 roots recorded]
PASS: reveal A discloses the body [block 19]
PASS: reveal A again rejected with 'claim already revealed'
PASS: provePriority A against the historic root (blockHeight 12 from the indexer)
PASS: provePriority discloses trick and spot only [kickflip at el-toro]
PASS: attest by the verifier
PASS: attest without the verifier secret rejected with 'not the verifier'
E2E RESULT: PASS (10/10 checks)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The one-line bug
&lt;/h2&gt;

&lt;p&gt;Look at &lt;code&gt;provePriority&lt;/code&gt; again. This line was not in my first version, and it is not in the concept page I learned the pattern from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;assert(path.leaf == c, "path is not for this claim");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is why it matters. A witness is plain TypeScript that runs outside the circuit. Midnight's docs are clear that witnesses are not cryptographically verified, and that the contract has to validate whatever they return. &lt;code&gt;findClaimPath&lt;/code&gt; returns a Merkle path. Every leaf in that tree, and every path to it, is public data. So without the binding line, a rider can compute a commitment for a claim they never sealed, have their witness hand back somebody else's perfectly valid path, and pass the root check.&lt;/p&gt;

&lt;p&gt;In my previous project, a verified-purchase review contract, that meant anyone could post a review without a purchase. In the docs' authorized-commitments example, it means any secret gets past "not authorized." The membership proof was proving that some leaf exists, not that your leaf exists.&lt;/p&gt;

&lt;p&gt;The fix is the single assert. The test that proves it bites is short too: pin another rider's perfectly valid path into the witness and call &lt;code&gt;provePriority&lt;/code&gt;. The circuit rejects it with &lt;code&gt;path is not for this claim&lt;/code&gt; in a tenth of a second, during local execution, before anything reaches the proof server. A real proof takes about twenty seconds, so you can tell from the timing alone whether the guard ran.&lt;/p&gt;

&lt;p&gt;The honest part is that Midnight's own security guide already says so. It calls the binding assert "the security-critical line" and shows exactly this shape. The concept page where most people first meet Merkle membership does not have it in either example, and does not link to the guide. I have a docs PR going in for that, and I fixed the review contract the same day.&lt;/p&gt;

&lt;p&gt;Three other compiler rules I learned by getting errors, all stricter than the examples suggest: every circuit parameter that flows into a ledger write needs &lt;code&gt;disclose()&lt;/code&gt; at the point of use, even public-looking ones like a trick id; &lt;code&gt;Set.member()&lt;/code&gt; with a witness-derived key is itself a disclosure point, so disclose the nullifier once when you bind it; and &lt;code&gt;checkRoot&lt;/code&gt; on a witness-supplied path needs &lt;code&gt;disclose(merkleTreePathRoot(path))&lt;/code&gt;. Five compile iterations for the first contract, zero for the second.&lt;/p&gt;

&lt;h2&gt;
  
  
  What none of this proves
&lt;/h2&gt;

&lt;p&gt;The chain proves who committed to what, and when. It never says the trick happened. A commitment over a synthetic clip seals just as well as one over a real one. Whether the footage is real is a separate question with separate answers: recording inside the app with a hash chain over the encoder output, device attestation, a sensor trace at capture, trick recognition, matching keyframes against the spot's photos. That all lives off chain, and only a tier bitmask gets anchored back to the commitment by a verifier key fixed at deploy.&lt;/p&gt;

&lt;p&gt;Riders never see any of this. No wallet, no token, no word Midnight. The first version is custodial, the app's backend holds the wallet and generates proofs, and the docs say so plainly. Getting the proving onto the phone is the next track.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;Claimed itself is not a standalone repo. It is a feature of TrickBook and ships inside it, so there is nothing to clone yet. The full design, including the parts about evidence and what the badges are allowed to claim, is in the TrickBook docs: &lt;a href="https://docs.thetrickbook.com/docs/features/claimed" rel="noopener noreferrer"&gt;https://docs.thetrickbook.com/docs/features/claimed&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you can run is the contract that taught me this, which is the same shape: &lt;a href="https://github.com/wbaxterh/vouched" rel="noopener noreferrer"&gt;wbaxterh/vouched&lt;/a&gt;, verified-purchase reviews, a commitment tree plus a nullifier set. Same binding assert, same forged-path test that pins another buyer's valid path into the witness and watches the circuit refuse it. You need Node 24, Docker, and the Compact toolchain (&lt;code&gt;compact&lt;/code&gt; CLI 0.5.1 with compiler 0.31.1). From the contract directory, &lt;code&gt;npm run compact&lt;/code&gt; builds the circuits. From the CLI directory, &lt;code&gt;npm run e2e&lt;/code&gt; brings up the standalone devnet in Docker, deploys, and runs the flow end to end.&lt;/p&gt;

&lt;p&gt;If you are building anything with Merkle membership on Midnight, grep your circuits for &lt;code&gt;checkRoot&lt;/code&gt; and make sure each one has a &lt;code&gt;path.leaf ==&lt;/code&gt; next to it. It took me two contracts to notice.&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>zeroknowledge</category>
      <category>blockchain</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Six things I learned moving USDM between Cardano and Midnight</title>
      <dc:creator>Tminus1sec</dc:creator>
      <pubDate>Thu, 03 Sep 2026 05:37:55 +0000</pubDate>
      <link>https://dev.to/tminus1s/five-things-i-learned-moving-usdm-between-cardano-and-midnight-4l2k</link>
      <guid>https://dev.to/tminus1s/five-things-i-learned-moving-usdm-between-cardano-and-midnight-4l2k</guid>
      <description>&lt;p&gt;Notes from building a cross-chain USDM transfer app and a private invoice contract on Midnight. Enterprise addresses, explorer link rules, a node error code, and why a contract cannot hold USDM today.&lt;/p&gt;




&lt;p&gt;I built two things on VIA's USDM cross-chain transfers over the past two weeks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/tomiin/tender-usdm" rel="noopener noreferrer"&gt;Tender&lt;/a&gt;&lt;/strong&gt; — a browser app that moves native USDM between Cardano Preprod and Midnight Preview, both directions, signed by the user's own wallets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/tomiin/privoice" rel="noopener noreferrer"&gt;Privoice&lt;/a&gt;&lt;/strong&gt; — a private invoice contract in Compact, settling in USDM, deployed on Midnight Preview.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eight transfers landed. One contract deployed with four on-chain interactions. Along the way I hit five things that are either not in the docs or are in the docs in a form that does not warn you about the consequence.&lt;/p&gt;

&lt;p&gt;None of this is a criticism of VIA's documentation, which is good. It is the gap between "correctly specified" and "you will not guess this at 2am".&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Your USDM arrives at an address your wallet will not show you
&lt;/h2&gt;

&lt;p&gt;This one cost me a day, and I suspect it has cost other people their submissions.&lt;/p&gt;

&lt;p&gt;Send USDM from Midnight to Cardano and it will not appear in Lace. The balance does not move. The transfer looks like it failed. It did not fail — the money is at an address your wallet does not watch.&lt;/p&gt;

&lt;p&gt;Here is the mechanism, from VIA's own message spec. The VILR payload has fixed-width fields:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Offset&lt;/th&gt;
&lt;th&gt;Width&lt;/th&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;76&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;28&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;source_depositor&lt;/code&gt; — payment key hash of the depositor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;136&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;32&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;destination_recipient&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And the rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;source_token&lt;/code&gt;, &lt;code&gt;destination_token&lt;/code&gt;, and &lt;code&gt;destination_recipient&lt;/code&gt; are each &lt;strong&gt;exactly 32 bytes. No shorter, no longer.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A Cardano &lt;strong&gt;base&lt;/strong&gt; address is 57 bytes: a header, a 28-byte payment credential, and a 28-byte stake credential. It does not fit in 32 bytes. What survives is the payment credential — which is exactly what &lt;code&gt;source_depositor&lt;/code&gt; is defined as.&lt;/p&gt;

&lt;p&gt;Reconstruct an address from a payment credential with no stake credential and you get an &lt;strong&gt;enterprise address&lt;/strong&gt;. Every time. By design.&lt;/p&gt;

&lt;p&gt;So the spec is correct and complete, and the consequence — &lt;em&gt;your funds land somewhere Lace does not display, and you will think the bridge ate them&lt;/em&gt; — is nowhere.&lt;/p&gt;

&lt;p&gt;I built the derivation into Tender so the app shows both balances side by side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cardano Preprod        990.000000 USDM
  977.000000 at your wallet address
   13.000000 at your enterprise address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VIA export &lt;code&gt;getEnterpriseAddress&lt;/code&gt; from the package, so they know. If you are building anything user-facing on this, derive it and show it.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. VIA Scan needs &lt;code&gt;0x&lt;/code&gt; on Midnight hashes and refuses it on Cardano ones
&lt;/h2&gt;

&lt;p&gt;Same transfer, two URLs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scan.vialabs.tech/tx/3c7daa78…9fda6034      →  Not found
scan.vialabs.tech/tx/0x3c7daa78…9fda6034    →  Delivered
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Cardano-source hash must be &lt;strong&gt;bare&lt;/strong&gt;. A Midnight-source hash must be &lt;strong&gt;&lt;code&gt;0x&lt;/code&gt;-prefixed&lt;/strong&gt;. Get it wrong and Scan tells you the transaction does not exist, on a transfer that completed.&lt;/p&gt;

&lt;p&gt;And one more: &lt;strong&gt;VIA Scan opens on Mainnet.&lt;/strong&gt; Every testnet message is invisible until you flip the network toggle.&lt;/p&gt;

&lt;p&gt;That is three separate ways to conclude a working transfer failed. I wrote one helper and stopped guessing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;viaScanHref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fromMidnight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fromMidnight&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0x&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`0x&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;VIA_SCAN&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/tx/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Testnet attests after one block. Mainnet is the fifty-minute one
&lt;/h2&gt;

&lt;p&gt;The docs say it plainly, but it is easy to read the wrong row:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Validators wait for source-chain confirmations before attesting a message: &lt;strong&gt;1 block on testnet&lt;/strong&gt;, 150 Cardano blocks (≈50 minutes) on mainnet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My fastest Preprod → Preview transfer delivered in &lt;strong&gt;1 minute 52 seconds&lt;/strong&gt;. So if you are on testnet and nothing has arrived after ten minutes, something is wrong — do not wait an hour because you saw "~55 min" somewhere.&lt;/p&gt;

&lt;p&gt;Which is how I noticed the next thing.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. A Compact contract cannot take custody of USDM on Preview today
&lt;/h2&gt;

&lt;p&gt;Privoice was originally designed to pool USDM inside the contract, so amounts could be hidden outright rather than just unlinked. Before writing it, I tested whether that was possible.&lt;/p&gt;

&lt;p&gt;I put two circuits in &lt;strong&gt;one contract&lt;/strong&gt; — same deploy, same wallet, same DUST state, same block window — differing by a single line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export circuit bump(): [] {
  bumps = (bumps + (1 as Uint&amp;lt;64&amp;gt;)) as Uint&amp;lt;64&amp;gt;;
}

export circuit deposit(color: Bytes&amp;lt;32&amp;gt;, amount: Uint&amp;lt;128&amp;gt;): [] {
  receiveUnshielded(disclose(color), disclose(amount));
  deposits = (deposits + (1 as Uint&amp;lt;64&amp;gt;)) as Uint&amp;lt;64&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Contract &lt;code&gt;c755041b16edcde0fb504e8967317d8de0cb2ef6a8593585877cd2a7e9c6e928&lt;/code&gt; on Preview:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;circuit&lt;/th&gt;
&lt;th&gt;result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;bump()&lt;/code&gt; — ledger write only&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;ACCEPTED&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;deposit()&lt;/code&gt; — ledger write + &lt;code&gt;receiveUnshielded&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;REJECTED, &lt;code&gt;Custom(192)&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Six attempts, each &lt;strong&gt;rebuilt and re-proved from scratch&lt;/strong&gt; rather than resubmitted, all rejected identically, about forty seconds after the control was accepted from the same wallet.&lt;/p&gt;

&lt;p&gt;Decoding &lt;code&gt;1010: Invalid Transaction: Custom error: 192&lt;/code&gt; matters here, so: &lt;strong&gt;&lt;code&gt;1010&lt;/code&gt; is upstream Substrate and carries no Midnight meaning.&lt;/strong&gt; The cause is the inner &lt;code&gt;u8&lt;/code&gt;. Look it up in &lt;code&gt;impl From&amp;lt;LedgerApiError&amp;gt; for u8&lt;/code&gt; in &lt;code&gt;midnight-node/ledger/src/versions/common/types.rs&lt;/code&gt; — not in a blog post, because the mapping changes across ledger versions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nn"&gt;MalformedError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InputsSignaturesLengthMismatch&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;192&lt;/code&gt; sits in a run of four checks that are all specifically about unshielded inputs and outputs: &lt;code&gt;InputsNotSorted&lt;/code&gt; (189), &lt;code&gt;OutputsNotSorted&lt;/code&gt; (190), &lt;code&gt;DuplicateInputs&lt;/code&gt; (191), &lt;code&gt;InputsSignaturesLengthMismatch&lt;/code&gt; (192).&lt;/p&gt;

&lt;p&gt;So Privoice settles at the &lt;strong&gt;application layer&lt;/strong&gt; — the contract records that settlement happened and never touches the token. That is a measured constraint, not a design preference, and the README says so with both contract addresses.&lt;/p&gt;

&lt;p&gt;Worth knowing if you are designing anything that wants a contract to hold USDM.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Unshielded inputs each need a signature, and forgetting it lands you on 192 too
&lt;/h2&gt;

&lt;p&gt;Related, and the practical version of the above. When you move USDM wallet-to-wallet with the SDK, the recipe must be &lt;strong&gt;signed before it is finalised&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recipe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;facade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transferTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;unshielded&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;outputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;USDM_TOKEN_COLOR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;receiverAddress&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;shieldedSecretKeys&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dustSecretKey&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payFees&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;signed&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;facade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signRecipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;keystore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;finalized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;facade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finalizeRecipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;txId&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;facade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submitTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;finalized&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Skip &lt;code&gt;signRecipe&lt;/code&gt; and the transaction reaches the node with fewer signatures than inputs — and you get &lt;code&gt;Custom(192)&lt;/code&gt; again, from a completely different cause than the contract case above. Same code, same error, unrelated bug.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. When the gateway stops, there is no other way to get USDM
&lt;/h2&gt;

&lt;p&gt;USDM is minted on Midnight &lt;strong&gt;solely&lt;/strong&gt; by VIA's cross-chain gateway delivering a&lt;br&gt;
message from Cardano. No faucet, no other issuance path. Which means if that&lt;br&gt;
gateway stops delivering, you cannot get USDM on Preview at all — and I found&lt;br&gt;
that out with a submission deadline in front of me.&lt;/p&gt;

&lt;p&gt;On 2 September I sent 10 USDM from my own bridge UI at 12:15 MDT. Nothing&lt;br&gt;
arrived. An hour later I sent 1 USDM using &lt;strong&gt;VIA's own CLI&lt;/strong&gt; as a control, to&lt;br&gt;
rule out my code. Both sat at &lt;em&gt;Awaiting Attestation&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The evidence said it was not me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both messages carried routing values matching VIA's published testnet table — chain ids &lt;code&gt;2273266 → 64364450&lt;/code&gt;, gateway &lt;code&gt;471dfe55…e73e485c&lt;/code&gt;, token colour &lt;code&gt;003bacd9…947d73&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Both &lt;code&gt;destinationRecipient&lt;/code&gt; fields decoded to my own address&lt;/li&gt;
&lt;li&gt;Cardano Preprod was healthy — the tip was ~260 blocks past my lock&lt;/li&gt;
&lt;li&gt;The docs say testnet attests after &lt;strong&gt;1 block&lt;/strong&gt;, and my 29 August transfer on the same route delivered in &lt;strong&gt;1m 52s&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;On VIA's global message feed, the last delivered message on that route was ~24 hours old. Mine were the only two since.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two independent clients, two package versions, ninety minutes apart, identical&lt;br&gt;
failure. That is about as clean a "not my code" result as you get.&lt;/p&gt;

&lt;p&gt;So I shipped anyway, and documented it. The payment leg is a parameter — one&lt;br&gt;
32-byte token colour — so I ran the settlement with NIGHT instead, and the&lt;br&gt;
README said exactly that: which token moved, which token the code defaults to,&lt;br&gt;
why the substitution was necessary, and both stuck message ids so a reviewer&lt;br&gt;
could check the claim themselves in thirty seconds.&lt;/p&gt;

&lt;p&gt;The next morning both messages delivered — &lt;strong&gt;16h 32m&lt;/strong&gt; and &lt;strong&gt;15h 4m&lt;/strong&gt;. VIA's&lt;br&gt;
scanner still labels those "normal". I re-ran the lifecycle with real USDM,&lt;br&gt;
paying a second address rather than my own, and removed the substitution note&lt;br&gt;
from the README, because it had stopped being true.&lt;/p&gt;

&lt;p&gt;Two things I would take from that. &lt;strong&gt;Delivery time on Preprod → Preview is not&lt;br&gt;
something to plan around&lt;/strong&gt; — the documented rule is one block and I saw sixteen&lt;br&gt;
hours. And when you are blocked by someone else's infrastructure, the move is&lt;br&gt;
to make the blocker verifiable rather than quietly omit it. Naming it cost&lt;br&gt;
nothing and would have survived review either way.&lt;/p&gt;




&lt;h2&gt;
  
  
  Two smaller ones, for whoever hits them next
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;DUST is not automatic.&lt;/strong&gt; Holding NIGHT does not generate DUST. It must be &lt;strong&gt;registered&lt;/strong&gt;. My wallet and my probe both read zero until I registered, and nothing tells you that is why.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The DUST wallet syncs separately, and far more slowly.&lt;/strong&gt; Most sync helpers wait on &lt;code&gt;state.unshielded.progress&lt;/code&gt;, which catches up in about a second — then declare success while dust sits at 42 of 183,000 events. A zero DUST balance in that state is correct, not a fault. Wait on &lt;code&gt;state.dust.progress&lt;/code&gt; explicitly, and save a checkpoint afterwards or you pay the cold sync every run.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I ended up with
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/tomiin/tender-usdm" rel="noopener noreferrer"&gt;Tender&lt;/a&gt;&lt;/strong&gt; — eight transfers, both directions, three of them signed in Lace rather than by the service. Enterprise-address derivation and correct explorer links built in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/tomiin/privoice" rel="noopener noreferrer"&gt;Privoice&lt;/a&gt;&lt;/strong&gt; — a Compact contract where only &lt;code&gt;persistentHash(amount, payer, memo, salt)&lt;/code&gt; goes on chain. The payer proves an invoice names them by reconstructing a commitment that matches, so identity is demonstrated rather than asserted. Deployed on Preview with &lt;code&gt;issue → acknowledge → pay → settle&lt;/code&gt;, all four accepted.&lt;/p&gt;

&lt;p&gt;Both repos have the full working code, the failed experiments, and the addresses to check any of this yourself.&lt;/p&gt;

&lt;p&gt;If you are starting on this today: derive the enterprise address, put &lt;code&gt;0x&lt;/code&gt; on your Midnight hashes, register your NIGHT, and read error codes from &lt;code&gt;types.rs&lt;/code&gt;. That is most of the day I lost, in one line.&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>compact</category>
      <category>midnightfordevs</category>
      <category>zk</category>
    </item>
    <item>
      <title>The Midnight wallet SDK changed its npm scope. Here is what to update.</title>
      <dc:creator>Cory Dabrowski</dc:creator>
      <pubDate>Thu, 20 Aug 2026 15:31:48 +0000</pubDate>
      <link>https://dev.to/chaps_grid/the-midnight-wallet-sdk-changed-its-npm-scope-here-is-what-to-update-5gem</link>
      <guid>https://dev.to/chaps_grid/the-midnight-wallet-sdk-changed-its-npm-scope-here-is-what-to-update-5gem</guid>
      <description>&lt;p&gt;If you installed the Midnight wallet SDK a while back and pinned the package names, your imports are now pointing at a deprecated scope. Nothing is broken yet. But the packages you depend on moved, and the old names are living on borrowed time.&lt;/p&gt;

&lt;p&gt;Here is what changed, why it matters, and the one gotcha that trips people up.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;The wallet SDK packages moved from the &lt;code&gt;@midnight-ntwrk&lt;/code&gt; scope (with a dash) to &lt;code&gt;@midnightntwrk&lt;/code&gt; (no dash).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@midnight-ntwrk/wallet-sdk-facade   -&amp;gt;   @midnightntwrk/wallet-sdk-facade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The old dashed packages still install, and the dashed scope still receives publishes. But the stable &lt;code&gt;latest&lt;/code&gt; tags have drifted apart: for most of the wallet SDK packages, a plain install of the dashed name resolves an older release than the no-dash name. So you want to move over.&lt;/p&gt;

&lt;p&gt;There is one exception to the rename. The release notes keep &lt;code&gt;@midnight-ntwrk/ledger-v8&lt;/code&gt; and &lt;code&gt;zkir-v2&lt;/code&gt; on the dashed scope, so leave those names alone. More on that below.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually changed
&lt;/h2&gt;

&lt;p&gt;Straight from the wallet SDK v1.2.0 release notes:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the npm scope has changed from &lt;code&gt;@midnight-ntwrk&lt;/code&gt; to &lt;code&gt;@midnightntwrk&lt;/code&gt; (no dash). New installs should depend on &lt;code&gt;@midnightntwrk/*&lt;/code&gt;. The old &lt;code&gt;@midnight-ntwrk/*&lt;/code&gt; packages continue to be published as a transitional alias during the migration window, so existing consumers keep working, but the dashed scope is deprecated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The alias is real. Publishes do land on both scopes: during a recent canary cycle, the exact same build showed up under both names within a minute of each other. But watch what a plain install actually resolves. Here is what &lt;code&gt;npm view &amp;lt;package&amp;gt; version&lt;/code&gt; returns for each scope, meaning the version behind the stable &lt;code&gt;latest&lt;/code&gt; tag (checked September 2, 2026):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;Dashed (old)&lt;/th&gt;
&lt;th&gt;No-dash (new)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;wallet-sdk-facade&lt;/td&gt;
&lt;td&gt;4.0.1&lt;/td&gt;
&lt;td&gt;4.1.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;wallet-sdk-hd&lt;/td&gt;
&lt;td&gt;3.0.2&lt;/td&gt;
&lt;td&gt;3.0.3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;wallet-sdk-shielded&lt;/td&gt;
&lt;td&gt;3.0.1&lt;/td&gt;
&lt;td&gt;3.0.2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;wallet-sdk-dust-wallet&lt;/td&gt;
&lt;td&gt;4.1.0&lt;/td&gt;
&lt;td&gt;4.2.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;wallet-sdk-unshielded-wallet&lt;/td&gt;
&lt;td&gt;3.1.0&lt;/td&gt;
&lt;td&gt;3.1.0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This table is a snapshot and will drift, so check it yourself. These two commands compare any package across the scopes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm view @midnightntwrk/wallet-sdk-facade version
npm view @midnight-ntwrk/wallet-sdk-facade version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four of the five packages resolve older on the dashed scope. I will not guess at why the tags differ. Whatever the mechanism, the observable state is what matters to your project: the newest stable releases are tagged on &lt;code&gt;@midnightntwrk&lt;/code&gt; first, and the dashed names can lag behind. Stay on the dashed names and you can quietly end up with older packages.&lt;/p&gt;

&lt;h2&gt;
  
  
  The gotcha: leave ledger-v8 and zkir-v2 alone
&lt;/h2&gt;

&lt;p&gt;When you do a find and replace across your project, it is tempting to swap every &lt;code&gt;@midnight-ntwrk&lt;/code&gt; for &lt;code&gt;@midnightntwrk&lt;/code&gt;. Hold on before you touch ledger-v8 and zkir-v2.&lt;/p&gt;

&lt;p&gt;The release notes are explicit:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Upstream, non-SDK dependencies remain on the dashed &lt;code&gt;@midnight-ntwrk&lt;/code&gt; scope (&lt;code&gt;@midnight-ntwrk/ledger-v8&lt;/code&gt;, &lt;code&gt;zkir-v2&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Both packages do exist on both scopes, and at identical versions: ledger-v8 is 8.1.1 whether you spell the scope with a dash or without, and zkir-v2 is 2.1.0 on both. So renaming them would not point you at a missing package. But there is harder evidence than the release notes for keeping them dashed. Look at the dependencies of the no-dash facade itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"@midnight-ntwrk/ledger-v8"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^8.1.0"&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="err"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;dashed,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;purpose&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"@midnightntwrk/wallet-sdk-shielded"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^3.0.2"&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="err"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;its&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;wallet-sdk&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;siblings,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;no-dash&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The SDK's own packages depend on the dashed ledger-v8, sitting right next to their no-dash siblings. If you rename ledger-v8 in your project, you now depend on a package the SDK itself does not use, and npm will happily install both copies side by side. Follow the notes. The wallet SDK packages move to no-dash, and ledger-v8 and zkir-v2 stay dashed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to fix your project
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Update your install command.&lt;/strong&gt; Move the wallet-sdk packages to no-dash. Keep ledger-v8 dashed.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @midnight-ntwrk/wallet-sdk-facade@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnight-ntwrk/wallet-sdk-hd@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnight-ntwrk/wallet-sdk-shielded@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnight-ntwrk/wallet-sdk-dust-wallet@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnight-ntwrk/wallet-sdk-unshielded-wallet@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnight-ntwrk/ledger-v8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @midnightntwrk/wallet-sdk-facade@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnightntwrk/wallet-sdk-hd@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnightntwrk/wallet-sdk-shielded@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnightntwrk/wallet-sdk-dust-wallet@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnightntwrk/wallet-sdk-unshielded-wallet@VERSION &lt;span class="se"&gt;\&lt;/span&gt;
            @midnight-ntwrk/ledger-v8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the last line did not change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Update your imports.&lt;/strong&gt; Same rule. Wallet SDK to no-dash, ledger stays dashed.&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WalletFacade&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@midnight-ntwrk/wallet-sdk-facade&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DustWallet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@midnight-ntwrk/wallet-sdk-dust-wallet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@midnight-ntwrk/ledger-v8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WalletFacade&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@midnightntwrk/wallet-sdk-facade&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DustWallet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@midnightntwrk/wallet-sdk-dust-wallet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@midnight-ntwrk/ledger-v8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Check your work.&lt;/strong&gt; After the change, search your project for any leftover dashed wallet-sdk references. There should be none. The only dashed names left should be ledger-v8 (and zkir-v2 if you use it).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-rn&lt;/span&gt; &lt;span class="s2"&gt;"@midnight-ntwrk/wallet-sdk"&lt;/span&gt; src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If that returns nothing, you are done. If it returns hits, those are the ones you missed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do you need to rush?
&lt;/h2&gt;

&lt;p&gt;No. The dashed packages still install and still work, so this is not an emergency. But two reasons to do it soon:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The newest stable releases are tagged on the no-dash scope, and the dashed facade already resolves a minor version behind. And here is the quiet part: the deprecation lives in the release notes, not in the npm registry. None of the dashed packages carry npm's &lt;code&gt;deprecated&lt;/code&gt; flag, so &lt;code&gt;npm install&lt;/code&gt; prints no warning at all. You fall behind silently.&lt;/li&gt;
&lt;li&gt;The release notes frame the dashed scope as a migration-window measure. They do not say when the window closes, but moving on your own schedule beats scrambling if it does.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fifteen minutes now saves you a broken install later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this comes from
&lt;/h2&gt;

&lt;p&gt;The details are in the Midnight wallet SDK v1.2.0 release notes on GitHub, which also point to ADR-0007 for the reasoning behind the rename. The version numbers above come straight from the npm registry, and the two &lt;code&gt;npm view&lt;/code&gt; commands earlier let you re-check them any time.&lt;/p&gt;

&lt;p&gt;That is the whole change. Move the wallet-sdk packages to &lt;code&gt;@midnightntwrk&lt;/code&gt;, leave ledger-v8 and zkir-v2 on &lt;code&gt;@midnight-ntwrk&lt;/code&gt;, and check for stragglers.&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>web3</category>
      <category>javascript</category>
      <category>privacy</category>
    </item>
    <item>
      <title>Don't update Compact maps on Midnight Undeployed</title>
      <dc:creator>Arun Nadarasa</dc:creator>
      <pubDate>Mon, 17 Aug 2026 21:58:51 +0000</pubDate>
      <link>https://dev.to/socialprescribing/dont-update-compact-maps-on-midnight-undeployed-35dl</link>
      <guid>https://dev.to/socialprescribing/dont-update-compact-maps-on-midnight-undeployed-35dl</guid>
      <description>&lt;p&gt;I spent an afternoon chasing &lt;code&gt;SubmissionError&lt;/code&gt; / &lt;code&gt;FiberFailure&lt;/code&gt; wrapping:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RpcError 1010: Invalid Transaction: Custom error: 117
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and a WASM panic that looks like &lt;code&gt;feesWithMargin&lt;/code&gt; / &lt;code&gt;transaction_merge&lt;/code&gt; &lt;strong&gt;Unreachable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The circuit compiled. The proof server was up. The genesis wallet had tDUST. The bug was in how I used Compact &lt;code&gt;Map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This post is the lesson: &lt;strong&gt;on Midnight Local Undeployed, do not update an existing Map key.&lt;/strong&gt; Insert a new key. If a value must become public, &lt;code&gt;disclose()&lt;/code&gt; it. And hashing a witness then checking &lt;code&gt;!= empty&lt;/code&gt; is not access control.&lt;/p&gt;

&lt;p&gt;I verified this against a real local stack, not a sketch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Versions I actually ran
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Piece&lt;/th&gt;
&lt;th&gt;Pin&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Compact language&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pragma language_version 0.23&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compact toolchain&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.31.x&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@midnight-ntwrk/compact-runtime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.16.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;@midnight-ntwrk/midnight-js-*&lt;/code&gt; / &lt;code&gt;testkit-js&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;4.1.1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@midnight-ntwrk/wallet-sdk&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;1.2.0&lt;/code&gt; (&lt;strong&gt;not&lt;/strong&gt; &lt;code&gt;wallet@5&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;midnightntwrk/midnight-node:0.22.5&lt;/code&gt; &lt;code&gt;:9944&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Indexer&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;midnightntwrk/indexer-standalone:4.0.2&lt;/code&gt; &lt;code&gt;:8088&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Proof server&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;midnightntwrk/proof-server:8.0.3&lt;/code&gt; &lt;code&gt;:6300&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Local Undeployed writes are &lt;strong&gt;server-append&lt;/strong&gt;. Lace does not sign them. &lt;code&gt;setNetworkId("undeployed")&lt;/code&gt; is a &lt;strong&gt;string&lt;/strong&gt; at midnight-js 4.1.1. The standalone chain funds genesis seed &lt;code&gt;…0002&lt;/code&gt;, not &lt;code&gt;…0001&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If those pins drift, you get a different class of failure (often &lt;strong&gt;196&lt;/strong&gt; = verifier mismatch). Don't mix public-network proof-server &lt;code&gt;8.1.0&lt;/code&gt; with this indexer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model in one paragraph
&lt;/h2&gt;

&lt;p&gt;A Compact circuit has three kinds of data:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Witnesses&lt;/strong&gt; — private inputs (&lt;code&gt;localSecretKey()&lt;/code&gt;). They never land on the public ledger unless you move them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Circuit-local values&lt;/strong&gt; — hashes, asserts, math. Still private until disclosed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ledger&lt;/strong&gt; — public state. The only way a private value becomes public is &lt;code&gt;disclose(...)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is why a reviewer can ask "why does this circuit need &lt;code&gt;disclose()&lt;/code&gt; here?" Because &lt;code&gt;credits.insert(pk, amount)&lt;/code&gt; without &lt;code&gt;disclose&lt;/code&gt; would try to write a private value into public state. The compiler/runtime will not let that slide.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I wrote first (this panics on Undeployed)
&lt;/h2&gt;

&lt;p&gt;The EVM instinct is a &lt;code&gt;balances&lt;/code&gt; map you overwrite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="mf"&gt;0.23&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;CompactStandardLibrary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;musdc:signer:v1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="c1"&gt;// WRONG on Undeployed: lookup + insert of the SAME key&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fromBal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fromBal&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toBal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toBal&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That compiles as Compact. On this Undeployed dust wallet it does &lt;strong&gt;not&lt;/strong&gt; survive &lt;code&gt;callTx&lt;/code&gt;. Updating an existing key trips the merge path. Symptom soup: &lt;code&gt;SubmissionError&lt;/code&gt;, &lt;code&gt;FiberFailure&lt;/code&gt;, Unreachable in &lt;code&gt;transaction_merge&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I hit the same class of bug twice: first on an NFT &lt;code&gt;owners&lt;/code&gt; map, then again on a token &lt;code&gt;balances&lt;/code&gt; map after I thought I had "fixed NFTs." Same ledger shape, same panic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What works: insert-only credit log
&lt;/h2&gt;

&lt;p&gt;Treat the public ledger as an &lt;strong&gt;append-only journal&lt;/strong&gt;. Reconstruct demo balances off-chain if you need a UI number.&lt;/p&gt;

&lt;p&gt;This is the token circuit I compiled and ran (&lt;code&gt;MidnightUSDC.compact&lt;/code&gt;, Compact 0.23):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="mf"&gt;0.23&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;CompactStandardLibrary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;credits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;credit_to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;faucet_claimed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;spent_nonces&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;faucet&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;musdc:signer:v1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;faucet_claimed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;already claimed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;faucet_claimed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;credits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000000&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;credit_to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fromPk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;musdc:signer:v1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;spent_nonces&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nonce spent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;amount zero&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;faucet_claimed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fromPk&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;signer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;spent_nonces&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;credits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;credit_to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What changed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Faucet&lt;/strong&gt; inserts once, keyed by signer pk. Never updated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transfer&lt;/strong&gt; inserts a &lt;strong&gt;new&lt;/strong&gt; row keyed by a fresh &lt;code&gt;nonce&lt;/code&gt;. Never &lt;code&gt;balances[from] = old - amount&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Every value that hits a ledger &lt;code&gt;insert&lt;/code&gt; is &lt;code&gt;disclose(...)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Spend auth is &lt;code&gt;faucet_claimed.member(fromPk)&lt;/code&gt;, not "the hash exists."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;NFT list/buy on the same stack uses the same rule: &lt;code&gt;owners&lt;/code&gt; insert on mint only; &lt;code&gt;sales&lt;/code&gt; append with a fresh random id; cancel writes a &lt;strong&gt;new&lt;/strong&gt; listing id instead of overwriting the old price key. Circuit name is &lt;code&gt;listSale&lt;/code&gt; — &lt;code&gt;list&lt;/code&gt; is a Compact keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;disclose()&lt;/code&gt; is on the insert
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fromPk&lt;/code&gt; is derived from a &lt;strong&gt;witness&lt;/strong&gt;. Left alone, it is private. &lt;code&gt;credits.insert&lt;/code&gt; writes &lt;strong&gt;public&lt;/strong&gt; Map state. &lt;code&gt;disclose(fromPk)&lt;/code&gt; is the explicit "this byte string may leave the circuit."&lt;/p&gt;

&lt;p&gt;Skip it and you either fail compile or you ship a circuit that cannot be proven against public ledger types. Skip &lt;code&gt;disclose&lt;/code&gt; on &lt;code&gt;nonce&lt;/code&gt; and the spent-nonce set cannot be checked by later transactions.&lt;/p&gt;

&lt;p&gt;That is the whole privacy model: private by default, public by &lt;code&gt;disclose&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The auth footgun that looks like privacy
&lt;/h2&gt;

&lt;p&gt;This also compiles, and it is not access control:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;movenft:minter:v1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;auth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any non-empty secret hashes to something that is not 32 zero bytes. I replaced it with a stored pk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;movenft:minter:v1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;empty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;minter_pk&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;minter_pk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// first call bootstraps&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;minter_pk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;minter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// later calls bind the witness&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same shape as &lt;code&gt;assert(pk == buyer)&lt;/code&gt; on a mandate circuit: compare the derived pk to a value the ledger already knows.&lt;/p&gt;

&lt;p&gt;On the token, &lt;code&gt;assert(fromPk != empty)&lt;/code&gt; was the same undercut. &lt;code&gt;faucet_claimed.member(fromPk)&lt;/code&gt; is the bind.&lt;/p&gt;

&lt;h2&gt;
  
  
  SDK side: don't cache the genesis wallet
&lt;/h2&gt;

&lt;p&gt;Undeployed appends reuse the deploy-time LevelDB private state. Mismatch → &lt;strong&gt;117&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Keep seed, store name, and private-state id in &lt;strong&gt;one module&lt;/strong&gt; imported by deploy &lt;strong&gt;and&lt;/strong&gt; every &lt;code&gt;callTx&lt;/code&gt; path. Open &lt;code&gt;MidnightWalletProvider&lt;/code&gt;, prove, submit, &lt;code&gt;stop()&lt;/code&gt; in &lt;code&gt;finally&lt;/code&gt;. A cached provider holds LevelDB open and the next HTTP request dies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;setNetworkId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@midnight-ntwrk/midnight-js-network-id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MidnightWalletProvider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@midnight-ntwrk/wallet-sdk&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;setNetworkId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undeployed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// string, not a runtime enum at 4.1.1&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wallet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;MidnightWalletProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;networkId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undeployed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;seedHex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;GENESIS_SEED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// …0002&lt;/span&gt;
  &lt;span class="na"&gt;indexerUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:8088/api/v4/graphql&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;indexerWsUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ws://localhost:8088/api/v4/graphql/ws&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;proofServerUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:6300&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;nodeUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ws://localhost:9944&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// CompiledContract + callTx&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;wallet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Need &lt;code&gt;import WebSocket from "ws"&lt;/code&gt; in Node. Compact 0.31 emits &lt;code&gt;contract/index.js&lt;/code&gt; (not only &lt;code&gt;.cjs&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  How I proved it wasn't folklore
&lt;/h2&gt;

&lt;p&gt;After rewriting maps to insert-only and binding the faucet set:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;compact compile contracts/MidnightUSDC.compact contracts/managed/midnight-usdc&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Wipe local private state, full genesis deploy&lt;/li&gt;
&lt;li&gt;Two sequential transfers succeeded&lt;/li&gt;
&lt;li&gt;Mint → list → buy e2e printed &lt;code&gt;E2E_OK&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you change circuits after deploy: compile → copy prover/verifier artefacts → wipe LevelDB → &lt;strong&gt;full&lt;/strong&gt; redeploy. Mixing new verifiers with old state is &lt;strong&gt;196&lt;/strong&gt;. Retrying the same UI click after 117 does not heal it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell my past self
&lt;/h2&gt;

&lt;p&gt;Design the public &lt;code&gt;Map&lt;/code&gt;s as journals &lt;strong&gt;before&lt;/strong&gt; the first Undeployed demo. Do not port ERC-20 storage slots into Compact and hope the dust wallet merges them. &lt;code&gt;disclose()&lt;/code&gt; is not ceremony — it is the privacy boundary. And &lt;code&gt;hash != empty&lt;/code&gt; is a compile-time-shaped lie.&lt;/p&gt;

&lt;p&gt;Working contracts and the insert-only token live in &lt;a href="https://github.com/arunnadarasa/zealymidnight" rel="noopener noreferrer"&gt;zealymidnight&lt;/a&gt; (&lt;code&gt;contracts/MidnightUSDC.compact&lt;/code&gt;, &lt;code&gt;contracts/MoveNft.compact&lt;/code&gt;).&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>zkp</category>
      <category>compact</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Your Midnight deploy will crash on a fresh install - here are the wallet-sdk patches (and why they happen)</title>
      <dc:creator>KOMARI Subheeksh</dc:creator>
      <pubDate>Sun, 09 Aug 2026 12:45:44 +0000</pubDate>
      <link>https://dev.to/komari_subheeksh_ced2cb4c/your-midnight-deploy-will-crash-on-a-fresh-install-here-are-the-wallet-sdk-patches-and-why-they-o3g</link>
      <guid>https://dev.to/komari_subheeksh_ced2cb4c/your-midnight-deploy-will-crash-on-a-fresh-install-here-are-the-wallet-sdk-patches-and-why-they-o3g</guid>
      <description>&lt;p&gt;Every Midnight developer eventually hits this. You clone a working project, run &lt;code&gt;npm install&lt;/code&gt;, and the deploy script that worked yesterday crashes with something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: state.pendingOutputs.values.map is not a function or its return value is not iterable
  at Object.pickAllCoins (CoreWallet.js:28:66)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or your wallet sync hangs for minutes and dies with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Wallet.Sync: [object Object]
  at file:///.../wallet-sdk-shielded/dist/v1/Sync.js:126:169
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first time this happened to me I assumed it was a Node version issue. It is not. It's a bug in the Midnight wallet SDK itself — a family of bugs, actually, spread across five packages — and the fix is a set of patches you have to re-apply after every &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The root cause: plain iterators without Iterator helpers
&lt;/h2&gt;

&lt;p&gt;The wallet SDK (version 1.2.0 core, shielded 3.0.2) defines its own ledger types — maps of transaction intents, outputs, and coin nonces. Those custom types implement &lt;code&gt;.entries()&lt;/code&gt;, &lt;code&gt;.values()&lt;/code&gt;, and &lt;code&gt;.keys()&lt;/code&gt; methods that return plain iterators.&lt;/p&gt;

&lt;p&gt;Here's the problem. Modern JavaScript's &lt;code&gt;Iterator&lt;/code&gt; protocol has helper methods: &lt;code&gt;.map()&lt;/code&gt;, &lt;code&gt;.filter()&lt;/code&gt;, &lt;code&gt;.find()&lt;/code&gt;, &lt;code&gt;.every()&lt;/code&gt;, &lt;code&gt;.toArray()&lt;/code&gt;. Native &lt;code&gt;Map&lt;/code&gt; and &lt;code&gt;Set&lt;/code&gt; objects get these helpers from &lt;code&gt;Iterator.prototype&lt;/code&gt; — and in Node 22+, you can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// works on native Map&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the SDK's custom ledger types return iterators that do NOT inherit from &lt;code&gt;Iterator.prototype&lt;/code&gt;. They're plain objects that only implement the &lt;code&gt;next()&lt;/code&gt; method. So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pendingOutputs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;span class="c1"&gt;// TypeError: state.pendingOutputs.values.map is not a function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not a Node version problem. It fails on every Node version, because the SDK's custom iterators never had the helper methods to begin with. The SDK code was written against an environment where these helpers existed (or the code was never run against the custom types), and it shipped assuming &lt;code&gt;.map()&lt;/code&gt; would be there.&lt;/p&gt;

&lt;h2&gt;
  
  
  The patch: spread into a real array first
&lt;/h2&gt;

&lt;p&gt;The fix for each crash site is mechanical: convert the plain iterator into a real array (which has all the methods) before chaining:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;- state.pendingOutputs.values().map(...)
&lt;/span&gt;&lt;span class="gi"&gt;+ [...state.pendingOutputs.values()].map(...)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are the affected files and the exact patches, verified against wallet-sdk-shielded 3.0.2 / unshielded 3.1.0 / facade 4.1.0 / dust-wallet 4.2.0.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;wallet-sdk-shielded/dist/v1/CoreWallet.js&lt;/code&gt; — &lt;code&gt;.values().map()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The crash from the top of this post. This one hits first because &lt;code&gt;pickAllCoins&lt;/code&gt; runs during coin selection, before any transaction is built.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/state.pendingOutputs.values().map/([...state.pendingOutputs.values()]).map/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/CoreWallet.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;code&gt;wallet-sdk-shielded/dist/v1/TransactionImbalances.js&lt;/code&gt; — &lt;code&gt;.entries().every()&lt;/code&gt; (x2)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/imbalances.guaranteed.entries().every/[...imbalances.guaranteed.entries()].every/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/TransactionImbalances.js
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/segmentImbalances.entries().every/[...segmentImbalances.entries()].every/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/TransactionImbalances.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;code&gt;wallet-sdk-shielded/dist/v1/TransactionOps.js&lt;/code&gt; — &lt;code&gt;.entries().filter()&lt;/code&gt; (x2)
&lt;/h3&gt;

&lt;p&gt;These call sites span multiple lines, so a plain &lt;code&gt;sed&lt;/code&gt; won't do. The chain looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;tx&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;imbalances&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Patch with a small Node script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/TransactionOps.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/tx&lt;/span&gt;&lt;span class="se"&gt;\n(\s&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\.&lt;/span&gt;&lt;span class="sr"&gt;imbalances&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="sr"&gt;0&lt;/span&gt;&lt;span class="se"&gt;\)\n\1\.&lt;/span&gt;&lt;span class="sr"&gt;entries&lt;/span&gt;&lt;span class="se"&gt;\(\)\n\1\.&lt;/span&gt;&lt;span class="sr"&gt;filter&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[...tx&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.imbalances(0)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.entries()]&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.filter(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/tx&lt;/span&gt;&lt;span class="se"&gt;\n(\s&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\.&lt;/span&gt;&lt;span class="sr"&gt;imbalances&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="sr"&gt;segment&lt;/span&gt;&lt;span class="se"&gt;\)\n\1\.&lt;/span&gt;&lt;span class="sr"&gt;entries&lt;/span&gt;&lt;span class="se"&gt;\(\)\n\1\.&lt;/span&gt;&lt;span class="sr"&gt;filter&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[...tx&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.imbalances(segment)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.entries()]&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.filter(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. &lt;code&gt;wallet-sdk-unshielded-wallet/dist/v1/TransactionOps.js&lt;/code&gt; — &lt;code&gt;.keys().toArray()&lt;/code&gt; + &lt;code&gt;.entries().filter().map().toArray()&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/transaction.intents?.keys().toArray()/Array.from(transaction.intents?.keys() ?? [])/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-unshielded-wallet/dist/v1/TransactionOps.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.entries().filter().map().toArray()&lt;/code&gt; chain is multi-line and also ends with &lt;code&gt;.toArray()&lt;/code&gt; that has no replacement — the cleanest approach is a Python one-liner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;node_modules/@midnight-ntwrk/wallet-sdk-unshielded-wallet/dist/v1/TransactionOps.js&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;const imbalances = transaction&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;            .imbalances(segment)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;            .entries()&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;            .filter&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;const imbalances = [...transaction&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;            .imbalances(segment)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;            .entries()]&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;            .filter&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.toArray();&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;        return Imbalances.fromEntries&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;        return Imbalances.fromEntries&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. &lt;code&gt;wallet-sdk-facade/dist/transaction.js&lt;/code&gt; — &lt;code&gt;.values().toArray()&lt;/code&gt; (x2)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/tx.intents?.values().toArray()/[...(tx.intents?.values() ?? [])]/g'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-facade/dist/transaction.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. &lt;code&gt;wallet-sdk-dust-wallet/dist/v1/Transacting.js&lt;/code&gt; — &lt;code&gt;.entries().find()&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node_modules/@midnight-ntwrk/wallet-sdk-dust-wallet/dist/v1/Transacting.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="sr"&gt;/const &lt;/span&gt;&lt;span class="se"&gt;\[&lt;/span&gt;&lt;span class="sr"&gt;_, imbalance&lt;/span&gt;&lt;span class="se"&gt;\]&lt;/span&gt;&lt;span class="sr"&gt; = transaction&lt;/span&gt;&lt;span class="se"&gt;\n(\s&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\.&lt;/span&gt;&lt;span class="sr"&gt;imbalances&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="sr"&gt;0, totalFee&lt;/span&gt;&lt;span class="se"&gt;\)\n\1\.&lt;/span&gt;&lt;span class="sr"&gt;entries&lt;/span&gt;&lt;span class="se"&gt;\(\)\n\1\.&lt;/span&gt;&lt;span class="sr"&gt;find&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;const [_, imbalance] = [...transaction&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.imbalances(0, totalFee)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.entries()]&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;$1.find(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bonus bug: &lt;code&gt;Set.difference()&lt;/code&gt; doesn't exist on older Node
&lt;/h2&gt;

&lt;p&gt;While patching &lt;code&gt;CoreWallet.js&lt;/code&gt;, you'll find a second landmine right next to the first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;coinNonces&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;definedNonces&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Set.prototype.difference()&lt;/code&gt; is ES2025 — it exists on Node 22.13+ and 24, but NOT on Node 20, which the Midnight toolchain still supports. On Node 20 this fails with &lt;code&gt;coinNonces.difference is not a function&lt;/code&gt; during wallet state restore.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/coinNonces.difference(definedNonces)/new Set([...coinNonces].filter(x =&amp;gt; !definedNonces.has(x)))/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/CoreWallet.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A correction on wallet sync hangs
&lt;/h2&gt;

&lt;p&gt;An earlier version of this post claimed the preprod RPC closes idle WebSocket connections and that client keepalive pings were needed. That was wrong. Direct testing against &lt;code&gt;wss://rpc.preprod.midnight.network&lt;/code&gt; shows an idle connection stays open well past a minute, and the server sends a ping every 30 seconds that the client auto-pongs — idle connections do not drop for lack of client keepalives. The verification snippet in the earlier version also passed &lt;code&gt;{ pingInterval, pingTimeout }&lt;/code&gt; to &lt;code&gt;new WebSocket()&lt;/code&gt;, which are not valid &lt;code&gt;ws&lt;/code&gt; options and are silently ignored, so it did not actually reproduce anything.&lt;/p&gt;

&lt;p&gt;The sync hang itself is real — long preprod syncs can still fail with &lt;code&gt;Wallet.Sync&lt;/code&gt; errors from the shielded/unshielded sync modules — but the idle-timeout explanation was incorrect. The practical workaround that got syncs moving was the sync-gap tolerance patch (see #8-10 below), and the honest advice is to capture the actual sync error before reaching for any patch. Treat the keepalive theory as retracted.&lt;/p&gt;

&lt;h2&gt;
  
  
  The complete patch script
&lt;/h2&gt;

&lt;p&gt;Here's everything in one place — save it as &lt;code&gt;patch-sdk.sh&lt;/code&gt; in your project and run it after every &lt;code&gt;npm install&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# Comprehensive Midnight wallet-sdk patches (11 patches, 13 fixes across 7 files)&lt;/span&gt;
&lt;span class="c"&gt;# Run after every `npm install` before deploying.&lt;/span&gt;
&lt;span class="c"&gt;# The SDK's custom ledger types define .entries()/.values()/.keys() that return&lt;/span&gt;
&lt;span class="c"&gt;# plain iterators WITHOUT Iterator helper methods (.filter, .map, .find, .every).&lt;/span&gt;
&lt;span class="c"&gt;# This is an SDK bug, not a Node version issue — affects all Node versions.&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt;

&lt;span class="nv"&gt;ROOT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ROOT&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"=== Patching Midnight wallet-sdk iterator bugs ==="&lt;/span&gt;

&lt;span class="c"&gt;# ── Shielded wallet (5 patches) ──&lt;/span&gt;

&lt;span class="c"&gt;# 1. CoreWallet.js — .values().map()&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/state.pendingOutputs.values().map/([...state.pendingOutputs.values()]).map/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/CoreWallet.js
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ CoreWallet.js"&lt;/span&gt;

&lt;span class="c"&gt;# 1b. CoreWallet.js — Set.difference() (ES2025, missing on Node 20)&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/coinNonces.difference(definedNonces)/new Set([...coinNonces].filter(x =&amp;gt; !definedNonces.has(x)))/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/CoreWallet.js
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ CoreWallet.js Set.difference"&lt;/span&gt;

&lt;span class="c"&gt;# 2. TransactionImbalances.js — .entries().every() (2 occurrences)&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/imbalances.guaranteed.entries().every/[...imbalances.guaranteed.entries()].every/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/TransactionImbalances.js
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/segmentImbalances.entries().every/[...segmentImbalances.entries()].every/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/TransactionImbalances.js
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ TransactionImbalances.js"&lt;/span&gt;

&lt;span class="c"&gt;# 3. TransactionOps.js (shielded) — .entries().filter() (2 occurrences)&lt;/span&gt;
node &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"
const fs = require('fs');
let f = 'node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/TransactionOps.js';
let c = fs.readFileSync(f, 'utf8');
c = c.replace(/tx&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="s2"&gt;+)&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;imbalances&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="s2"&gt;0&lt;/span&gt;&lt;span class="se"&gt;\)\n\1\.&lt;/span&gt;&lt;span class="s2"&gt;entries&lt;/span&gt;&lt;span class="se"&gt;\(\)\n\1\.&lt;/span&gt;&lt;span class="s2"&gt;filter&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="s2"&gt;/g,
  '[...tx&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.imbalances(0)&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.entries()]&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.filter(');
c = c.replace(/tx&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="s2"&gt;+)&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;imbalances&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="s2"&gt;segment&lt;/span&gt;&lt;span class="se"&gt;\)\n\1\.&lt;/span&gt;&lt;span class="s2"&gt;entries&lt;/span&gt;&lt;span class="se"&gt;\(\)\n\1\.&lt;/span&gt;&lt;span class="s2"&gt;filter&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="s2"&gt;/g,
  '[...tx&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.imbalances(segment)&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.entries()]&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.filter(');
fs.writeFileSync(f, c);
"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ TransactionOps.js (shielded)"&lt;/span&gt;

&lt;span class="c"&gt;# ── Unshielded wallet (2 patches in 1 file) ──&lt;/span&gt;

&lt;span class="c"&gt;# 4. TransactionOps.js (unshielded) — .keys().toArray()&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/transaction.intents?.keys().toArray()/Array.from(transaction.intents?.keys() ?? [])/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-unshielded-wallet/dist/v1/TransactionOps.js
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ TransactionOps.js keys (unshielded)"&lt;/span&gt;

&lt;span class="c"&gt;# 5. TransactionOps.js (unshielded) — .entries().filter().map().toArray()&lt;/span&gt;
&lt;span class="c"&gt;# The Node regex approach is fragile due to escaping. Python fallback is more reliable.&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;python3 &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"
f = 'node_modules/@midnight-ntwrk/wallet-sdk-unshielded-wallet/dist/v1/TransactionOps.js'
c = open(f).read()
c = c.replace(
    'const imbalances = transaction&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n            .imbalances(segment)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n            .entries()&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n            .filter',
    'const imbalances = [...transaction&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n            .imbalances(segment)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n            .entries()]&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n            .filter'
)
c = c.replace('.toArray();&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n        return Imbalances.fromEntries', ';&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n        return Imbalances.fromEntries')
open(f,'w').write(c)
print('ok')
"&lt;/span&gt; 2&amp;gt;/dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ TransactionOps.js imbalances (unshielded) [python]"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="c"&gt;# Node regex fallback&lt;/span&gt;
    node &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"
const fs = require('fs');
let f = 'node_modules/@midnight-ntwrk/wallet-sdk-unshielded-wallet/dist/v1/TransactionOps.js';
let c = fs.readFileSync(f, 'utf8');
c = c.replace(
  /const imbalances = transaction&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n(&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;s+)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.imbalances&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;(segment&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.entries&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.filter&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;(/,
  'const imbalances = [...transaction&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;1.imbalances(segment)&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;1.entries()]&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;1.filter('
);
c = c.replace(/&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.toArray&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;);&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n(&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;s+)return Imbalances&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.fromEntries/, ';&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;1return Imbalances.fromEntries');
fs.writeFileSync(f, c);
"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ TransactionOps.js imbalances (unshielded) [node]"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# ── Facade (1 patch, 2 occurrences) ──&lt;/span&gt;

&lt;span class="c"&gt;# 6. transaction.js — .values().toArray() (2 occurrences)&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/tx.intents?.values().toArray()/[...(tx.intents?.values() ?? [])]/g'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-facade/dist/transaction.js
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ transaction.js (facade)"&lt;/span&gt;

&lt;span class="c"&gt;# ── Dust wallet (1 patch) ──&lt;/span&gt;

&lt;span class="c"&gt;# 7. Transacting.js — .entries().find()&lt;/span&gt;
node &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"
const fs = require('fs');
let f = 'node_modules/@midnight-ntwrk/wallet-sdk-dust-wallet/dist/v1/Transacting.js';
let c = fs.readFileSync(f, 'utf8');
c = c.replace(
  /const &lt;/span&gt;&lt;span class="se"&gt;\[&lt;/span&gt;&lt;span class="s2"&gt;_, imbalance&lt;/span&gt;&lt;span class="se"&gt;\]&lt;/span&gt;&lt;span class="s2"&gt; = transaction&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="s2"&gt;+)&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;imbalances&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="s2"&gt;0, totalFee&lt;/span&gt;&lt;span class="se"&gt;\)\n\1\.&lt;/span&gt;&lt;span class="s2"&gt;entries&lt;/span&gt;&lt;span class="se"&gt;\(\)\n\1\.&lt;/span&gt;&lt;span class="s2"&gt;find&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="s2"&gt;/,
  'const [_, imbalance] = [...transaction&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.imbalances(0, totalFee)&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.entries()]&lt;/span&gt;&lt;span class="se"&gt;\n\$&lt;/span&gt;&lt;span class="s2"&gt;1.find('
);
fs.writeFileSync(f, c);
"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ Transacting.js (dust)"&lt;/span&gt;

&lt;span class="c"&gt;# ── Wallet sync workarounds (3 patches) ⚠️ UNSAFE — see note below ──&lt;/span&gt;

&lt;span class="c"&gt;# 8. Skip shielded sync&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/this.shielded.waitForSyncedState(),//'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-facade/dist/index.js
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ facade index.js"&lt;/span&gt;

&lt;span class="c"&gt;# 9-10. Increase sync gap tolerance&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/waitForSyncedState(allowedGap = 0n)/waitForSyncedState(allowedGap = 100000n)/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-unshielded-wallet/dist/UnshieldedWallet.js
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/waitForSyncedState(allowedGap = 0n)/waitForSyncedState(allowedGap = 100000n)/'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-dust-wallet/dist/DustWallet.js
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  ✓ sync gap tolerance"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"=== All 11 patches applied (13 fixes across 7 files) ==="&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Unsafe workarounds (patches #8-10).&lt;/strong&gt; These three do NOT fix a bug — they disable sync-completeness checks. Skipping the shielded &lt;code&gt;waitForSyncedState()&lt;/code&gt; and raising &lt;code&gt;allowedGap&lt;/code&gt; to 100000n let a wallet act on incomplete chain state, which risks acting on stale or missing data. Use them only for local dev / demos where a slightly-off balance is acceptable, and never on a wallet handling real funds. The iterator patches (#1-7) are safe; #8-10 are not, and were kept separate for that reason.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note: the unshielded &lt;code&gt;.entries().filter().map().toArray()&lt;/code&gt; chain is safest with the Python snippet above — the regex escaping in sed/node gets fragile. Check &lt;code&gt;node_modules/@midnight-ntwrk/wallet-sdk-unshielded-wallet/dist/v1/TransactionOps.js&lt;/code&gt; after patching and confirm no &lt;code&gt;.entries().filter()&lt;/code&gt; chain remains un-prefixed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to check whether you need the patches
&lt;/h2&gt;

&lt;p&gt;If your deploy crashes with any of these, you need them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;state.pendingOutputs.values.map is not a function
imbalances.guaranteed.entries().every is not a function
transaction.intents?.keys().toArray is not a function
coinNonces.difference is not a function
Wallet.Sync: [object Object]  (after a long hang on preprod)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you can grep your installed SDK directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;values()&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;map&lt;/span&gt;&lt;span class="se"&gt;\|\.&lt;/span&gt;&lt;span class="s2"&gt;entries()&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;every&lt;/span&gt;&lt;span class="se"&gt;\|\.&lt;/span&gt;&lt;span class="s2"&gt;keys()&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;toArray&lt;/span&gt;&lt;span class="se"&gt;\|\.&lt;/span&gt;&lt;span class="s2"&gt;difference("&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  node_modules/@midnight-ntwrk/wallet-sdk-shielded/dist/v1/CoreWallet.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why this matters
&lt;/h2&gt;

&lt;p&gt;Every Midnight project that deploys from a fresh clone hits this wall — including the official example repos, because the SDK is broken the same way for everyone. The patches are mechanical, but discovering them cost real debugging time, and they must be re-applied after every &lt;code&gt;npm install&lt;/code&gt; (the patches live in &lt;code&gt;node_modules&lt;/code&gt;, so a fresh install restores the broken code). Put &lt;code&gt;patch-sdk.sh&lt;/code&gt; in your repo, run it in CI before &lt;code&gt;npm run deploy&lt;/code&gt;, and your fresh-clone experience will match your working-tree experience.&lt;/p&gt;

&lt;p&gt;A more durable alternative is &lt;a href="https://www.npmjs.com/package/patch-package" rel="noopener noreferrer"&gt;patch-package&lt;/a&gt;: apply the patches once, run &lt;code&gt;npx patch-package @midnight-ntwrk/wallet-sdk-shielded ...&lt;/code&gt; to generate a &lt;code&gt;patches/&lt;/code&gt; directory, add a &lt;code&gt;postinstall&lt;/code&gt; script (&lt;code&gt;patch-package&lt;/code&gt;), and every &lt;code&gt;npm install&lt;/code&gt; re-applies them automatically — no manual re-run, and the patches are reviewed as part of your repo instead of living only in a script.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Verified against wallet-sdk 1.2.0, wallet-sdk-shielded 3.0.2, wallet-sdk-unshielded-wallet 3.1.0, wallet-sdk-facade 4.1.0, wallet-sdk-dust-wallet 4.2.0, proof-server 8.1.0.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>sdk</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>NIGHT Inflation Analysis · September 2026 · Supply was growing · trend cooling</title>
      <dc:creator>MrNasdog</dc:creator>
      <pubDate>Tue, 07 Jul 2026 09:50:22 +0000</pubDate>
      <link>https://dev.to/mrnasdog/night-inflation-analysis-july-2026-fully-minted-supply-roughly-steady-53hp</link>
      <guid>https://dev.to/mrnasdog/night-inflation-analysis-july-2026-fully-minted-supply-roughly-steady-53hp</guid>
      <description>&lt;p&gt;Originally published at &lt;a href="https://mrnasdog.com/research/night/inflation" rel="noopener noreferrer"&gt;NIGHT Inflation Analysis · September 2026 · Supply was growing · trend cooling&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  NIGHT Inflation Analysis · September 2026 · Supply was growing · trend cooling
&lt;/h1&gt;

&lt;p&gt;Supply was growing and the trend is cooling. Midnight mints no new NIGHT and burns none, yet the Pressure Framework reads NIGHT at &lt;strong&gt;+3.10%&lt;/strong&gt; over the last 90 days and &lt;strong&gt;0.00%&lt;/strong&gt; for the next 90, against a monitor reading of &lt;strong&gt;−0.06%&lt;/strong&gt;. The whole number is one event: on &lt;strong&gt;Jul 20 2026&lt;/strong&gt; an attacker drained the locked backing of the Cardano to BNB Chain bridge, so &lt;strong&gt;514.6M NIGHT&lt;/strong&gt; of new tradable claims appeared while the fixed &lt;strong&gt;24,000M NIGHT&lt;/strong&gt; total never moved.&lt;/p&gt;

&lt;h2&gt;
  
  
  The verdict, in one paragraph
&lt;/h2&gt;

&lt;p&gt;On a counted float of &lt;strong&gt;16,607.4M NIGHT&lt;/strong&gt;, the framework books &lt;strong&gt;514.6M NIGHT&lt;/strong&gt; of sell pressure and &lt;strong&gt;0&lt;/strong&gt; of buy pressure for the trailing 90 days, a net of &lt;strong&gt;+3.10%&lt;/strong&gt;, and &lt;strong&gt;0.00%&lt;/strong&gt; for the next 90 days because nothing is scheduled to cross into the float. The inflation monitor reads &lt;strong&gt;−0.06%&lt;/strong&gt;, a gap of &lt;strong&gt;3.16 percentage points&lt;/strong&gt;, well over the 0.5-point tolerance, so the overview carries a monitor-gap warning. The gap is explained, not closed: the monitor's counted float is a fixed sum of Midnight's launch allocations and cannot move, so it cannot see wrapped NIGHT losing its backing. The label for NIGHT is &lt;strong&gt;a non-minting token hit by a one-time float shock&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sell pressure: where new NIGHT comes from
&lt;/h2&gt;

&lt;p&gt;It does not come from minting. All &lt;strong&gt;24,000M NIGHT&lt;/strong&gt; were created once, as a Cardano native asset, and the Cardano ledger still records a single mint and no burn. Midnight block rewards are paid only out of a pre-funded Reserve, and that Reserve held &lt;strong&gt;6,000M NIGHT&lt;/strong&gt; on &lt;strong&gt;Sep 22 2026&lt;/strong&gt;, exactly its starting balance, with no transactions in the window. The network is still run by a small set of trusted block producers who are not paid rewards. Sell #1, protocol inflation, is &lt;strong&gt;0&lt;/strong&gt;, watched because a future upgrade will switch Reserve rewards on.&lt;/p&gt;

&lt;p&gt;Sell #2, vesting unlocks, is also &lt;strong&gt;0&lt;/strong&gt;, and this is the row most readers expect to be large. The Glacier Drop and Scavenger Mine airdrop gave the community &lt;strong&gt;4,547.4M NIGHT&lt;/strong&gt; that thaws in four 25% steps, with each claim's first step placed at random in the first 90 days from &lt;strong&gt;Dec 10 2025&lt;/strong&gt;. One full step fell inside this window. But the counted float already includes the whole claimed pool: &lt;strong&gt;8,400M&lt;/strong&gt; for the Midnight Foundation, plus &lt;strong&gt;3,660M&lt;/strong&gt; for Midnight TGE, plus &lt;strong&gt;4,547.4M&lt;/strong&gt; claimed equals &lt;strong&gt;16,607.4M NIGHT&lt;/strong&gt; exactly. A coin counts once, when it enters the float. A thawed airdrop coin leaving the redemption contract, which still held &lt;strong&gt;1,263.3M NIGHT&lt;/strong&gt; on Sep 22 2026, was already counted, so it adds nothing.&lt;/p&gt;

&lt;p&gt;Sell #3, Foundation and unscheduled unlocks, is &lt;strong&gt;0&lt;/strong&gt;: the only pots outside the float, the Treasury and the Lost-and-Found pool, did not move once. Sell #4, long-term locked or bankruptcy, is &lt;strong&gt;0&lt;/strong&gt;; there is no estate or trustee. The number comes from Sell #5, the bridge exploit, at &lt;strong&gt;514.6M NIGHT&lt;/strong&gt;. The Wanchain bridge held Cardano NIGHT locked as backing for wrapped NIGHT on BNB Chain. On Jul 20 2026 an attacker replayed a signature and withdrew &lt;strong&gt;515.2M NIGHT&lt;/strong&gt; in four transactions, taking the backing from &lt;strong&gt;527.0M&lt;/strong&gt; to &lt;strong&gt;11.8M NIGHT&lt;/strong&gt;. The &lt;strong&gt;526.5M&lt;/strong&gt; wrapped NIGHT were never retired and still move between large exchange wallets. So the attacker's coins and the wrapped copies now both trade, and about 300M NIGHT was sold within hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Buy pressure: where new NIGHT goes
&lt;/h2&gt;

&lt;p&gt;Nowhere. Buy #1, programmatic buyback, is &lt;strong&gt;0&lt;/strong&gt;: no programme to buy NIGHT on the market exists or was announced. Buy #2, protocol fee burn, is &lt;strong&gt;0&lt;/strong&gt; by design. Midnight transactions are paid in DUST, a shielded resource that holding NIGHT generates over time, so NIGHT itself is never spent on fees or destroyed. Both burn surfaces were read: the Cardano ledger still counts 24,000M NIGHT with no burn ever recorded, and the wrapped supply on BNB Chain has not changed since &lt;strong&gt;Jul 21 2026&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Buy #3, Foundation buy, is &lt;strong&gt;0&lt;/strong&gt;. Midnight TGE's unused tokens are meant to go back to the Reserve one day, which would take them out of the float, but the Reserve did not change. Buy #4, new long-term lock, is &lt;strong&gt;0&lt;/strong&gt;. Moving NIGHT onto the Midnight network locks it on Cardano and unlocks it there, so it remains one coin with one count and removes nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Foundation and overhang
&lt;/h2&gt;

&lt;p&gt;The overhang on NIGHT is large and mostly already counted. The Midnight Foundation was allocated &lt;strong&gt;8,400M NIGHT&lt;/strong&gt; and Midnight TGE &lt;strong&gt;3,660M NIGHT&lt;/strong&gt;, both unlocked from launch and together half of all supply; how much each still holds is not published by wallet. Both sit inside the counted float, so a sale by either would move coins inside the float rather than add to it, but it would still be selling into the market, and the Foundation has no fixed release calendar. The pots outside the float are smaller and locked: the on-chain Treasury at &lt;strong&gt;1,200M NIGHT&lt;/strong&gt;, locked until on-chain governance exists, and the Lost-and-Found pool at &lt;strong&gt;192.6M NIGHT&lt;/strong&gt;, waiting for a claim phase with no date. The Reserve's &lt;strong&gt;6,000M NIGHT&lt;/strong&gt; opens only as block rewards.&lt;/p&gt;

&lt;p&gt;The Reserve, Treasury and Lost-and-Found balances are read from the chain at every rebuild; the Foundation and Midnight TGE balances are checked by hand. If any of these balances falls between refreshes by more than a known schedule explains, that outflow enters Sell #3 at the next refresh. The bridge's remaining backing of &lt;strong&gt;11.8M NIGHT&lt;/strong&gt; and the &lt;strong&gt;526.5M&lt;/strong&gt; wrapped supply are read at the same rebuilds.&lt;/p&gt;

&lt;h2&gt;
  
  
  How NIGHT compares to other capped, non-minting tokens
&lt;/h2&gt;

&lt;p&gt;NIGHT sits in a small class of tokens whose whole supply exists from day one. Unlike a halving chain such as Bitcoin, which still mints on every block, or an uncapped staking chain paying rewards from new coins, Midnight has no mint at all. Its future issuance is a draw-down of the &lt;strong&gt;6,000M NIGHT&lt;/strong&gt; Reserve on a slowing curve, which has not started. On the pure issuance axis NIGHT is as tight as any token in the catalogue.&lt;/p&gt;

&lt;p&gt;What sets NIGHT apart this quarter is custody risk. Most capped tokens that read positive do so because of a vesting unlock, a dated and predictable release. NIGHT read positive because a bridge failed. A wrapped token is a promise that the original sits locked somewhere, and when that lock is emptied the promise keeps trading while the original trades too. Any token bridged to other chains carries this exposure; the size of the risk is the size of the locked backing, not the size of the cap.&lt;/p&gt;

&lt;p&gt;Against exchange tokens that run a buyback or a burn, NIGHT has nothing on the buy side. Fees go to DUST, which decays and cannot be sold, so network use never removes NIGHT from supply. Holding NIGHT pays for usage; it does not shrink the float.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to watch in the next 90 days
&lt;/h2&gt;

&lt;p&gt;First, the bridge: Wanchain has shut it and offered the attacker a bounty, and any return of funds that re-locks backing would reverse part of the &lt;strong&gt;514.6M NIGHT&lt;/strong&gt; reading. Second, the last airdrop thaw step ends on &lt;strong&gt;Dec 4 2026&lt;/strong&gt;; it adds nothing to the float, but it frees the last frozen airdrop coins for sale. Third, the start of Reserve block rewards, planned for the Mōhalu phase when Cardano stake pool operators join; no date is set, and the first reward would open the Reserve for the first time. Fourth, the Lost-and-Found claim phase, which would release up to &lt;strong&gt;192.6M NIGHT&lt;/strong&gt; with no thaw. Fifth, any on-chain governance launch, which is the only key to the &lt;strong&gt;1,200M NIGHT&lt;/strong&gt; Treasury.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The MrNasdog Pressure Framework reads Midnight's NIGHT at &lt;strong&gt;+3.10%&lt;/strong&gt; over the trailing 90 days and &lt;strong&gt;0.00%&lt;/strong&gt; for the next 90: supply was growing, trend cooling. NIGHT mints nothing and burns nothing, and its airdrop thaw was already inside the float; the whole reading is the &lt;strong&gt;Jul 20 2026&lt;/strong&gt; bridge drain that left &lt;strong&gt;514.6M&lt;/strong&gt; wrapped NIGHT without backing. The key risks are custody failures like that one and the &lt;strong&gt;12,060M NIGHT&lt;/strong&gt; allocated, unlocked, to the Foundation and Midnight TGE. The ceiling holds firm at &lt;strong&gt;24,000M NIGHT&lt;/strong&gt;, with the &lt;strong&gt;6,000M&lt;/strong&gt; Reserve still untouched.&lt;/p&gt;

&lt;p&gt;MrNasdog Pressure Framework analysis of NIGHT, Metric 1 — Inflation. Data + explanation only. Not financial advice. Updated Sep 23 2026.&lt;/p&gt;

</description>
      <category>crypto</category>
      <category>night</category>
      <category>midnight</category>
      <category>cardano</category>
    </item>
    <item>
      <title>Shielded Token Contracts on Midnight: Real Errors, Real Fixes</title>
      <dc:creator>Elliot lucky</dc:creator>
      <pubDate>Wed, 01 Jul 2026 03:07:56 +0000</pubDate>
      <link>https://dev.to/codebigint_01/shielded-token-contracts-on-midnight-real-errors-real-fixes-4fc7</link>
      <guid>https://dev.to/codebigint_01/shielded-token-contracts-on-midnight-real-errors-real-fixes-4fc7</guid>
      <description>&lt;p&gt;&lt;em&gt;Written from months of grinding on shielded liquidity DeFi protocols on Midnight.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;If you've been trying to build anything serious with shielded fungible tokens on Midnight lending protocols, liquidity pools, DEXes you've probably hit some walls that the documentation doesn't fully prepare you for. The Midnight programming model around shielded tokens is genuinely different from anything in the EVM world, and a lot of the intuitions you carry from Solidity or even other ZK environments will get you into trouble fast.&lt;/p&gt;

&lt;p&gt;This post is a breakdown of the most impactful errors and misconceptions I ran into while building shielded liquidity DeFi contracts using Midnight's Compact language. These are not theoretical every single one of these either broke a circuit or caused a proof server failure at some point. I'll walk through what the issue is, why it happens, and what the correct pattern looks like.&lt;/p&gt;




&lt;h2&gt;
  
  
  Background: How Shielded Tokens Actually Work Under the Hood
&lt;/h2&gt;

&lt;p&gt;Before we get into the errors, let's get clear on the underlying mechanics because this context is what makes the errors make sense.&lt;/p&gt;

&lt;p&gt;Midnight uses a protocol called &lt;strong&gt;Zswap&lt;/strong&gt; for shielded token operations. When a user sends tokens to your contract by calling &lt;code&gt;receiveShielded&lt;/code&gt;, what actually happens is more involved than it looks on the surface.&lt;/p&gt;

&lt;p&gt;When your circuit calls &lt;code&gt;receiveShielded(coin)&lt;/code&gt;, the Compact runtime records a shielded receive obligation in the transaction being constructed. At this point, the proof server kicks in to generate the ZK proof for your circuit. But here's the thing your circuit only describes what the &lt;em&gt;contract side&lt;/em&gt; is doing. The transaction still needs to be &lt;em&gt;balanced&lt;/em&gt;: the tokens being received by the contract have to come from somewhere.&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;wallet&lt;/strong&gt; gets involved through an internal mechanism that runs beneath your circuit. The wallet looks at the &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; you're receiving the coin's color (token type) and value and finds a matching UTXO in the user's private coin set. It then generates a &lt;strong&gt;Zswap ownership proof&lt;/strong&gt; a ZK proof that proves the wallet owns a valid commitment in the global shielded ledger (via a nullifier), without revealing &lt;em&gt;which&lt;/em&gt; UTXO it is. This proof is what actually authorizes the spending of the user's tokens.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; that you receive in your circuit as a parameter is essentially the user's declaration: &lt;em&gt;"I have a coin of this color and this value that I'm sending you."&lt;/em&gt; The wallet is the one that provides the actual cryptographic evidence backing that claim. The proof server then bundles your circuit's proof together with the wallet's Zswap proof into a single transaction that satisfies all the balance constraints.&lt;/p&gt;

&lt;p&gt;This is why &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; has to arrive as a circuit parameter it has to exist &lt;em&gt;before&lt;/em&gt; the proof is constructed so the wallet knows what to balance against. You cannot dynamically request coins inside a circuit in a way the wallet didn't know about from the start.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The coin comes in as a circuit parameter - the wallet uses this
// to find the matching UTXO and generate its Zswap ownership proof
export circuit deposit(incomingCoin_: ShieldedCoinInfo): [] {
    const incomingCoin = disclose(incomingCoin_);
    // At this point, the wallet has already committed to providing
    // a UTXO matching incomingCoin.color and incomingCoin.value
    receiveShielded(incomingCoin);
    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that foundation in place, let's get into the errors.&lt;/p&gt;




&lt;h2&gt;
  
  
  Error 1: Multiple Separate Shielded Balance Mappings
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The mistake:&lt;/strong&gt; Creating separate ledger fields for each type of shielded asset the contract holds, or trying to manage shielded balances across multiple independent &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; ledger fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// DON'T do this
export ledger assetABalance: QualifiedShieldedCoinInfo;
export ledger assetBBalance: QualifiedShieldedCoinInfo;
export ledger assetCBalance: QualifiedShieldedCoinInfo;
// ...and growing as you add more assets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why it fails:&lt;/strong&gt; The moment you try to receive or send shielded tokens from multiple separate ledger fields in anything but the most trivial scenarios, you'll start hitting proof construction issues and ledger state inconsistencies. It also completely kills your extensibility you can't add a new supported asset without changing the contract's ledger schema.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The correct pattern:&lt;/strong&gt; One &lt;code&gt;Map&amp;lt;Bytes&amp;lt;32&amp;gt;, QualifiedShieldedCoinInfo&amp;gt;&lt;/code&gt; keyed by coin color. That's your single source of truth for everything the contract holds in shielded form. Separate tracking for accounting, positions, pool states all of that can live in whatever structure you want (Maps, commitments, Merkle trees). But the actual &lt;em&gt;shielded custody&lt;/em&gt; is one map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ONE mapping to manage all shielded holdings
export ledger contractShieldedBalance: Map&amp;lt;Bytes&amp;lt;32&amp;gt;, QualifiedShieldedCoinInfo&amp;gt;;

// Separate ledgers for your protocol accounting - no shielded tokens here
export ledger userPositions: Map&amp;lt;Bytes&amp;lt;32&amp;gt;, UserPosition&amp;gt;;
export ledger assetConfigs: Map&amp;lt;Bytes&amp;lt;32&amp;gt;, AssetConfig&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; returned by operations like &lt;code&gt;insertCoin&lt;/code&gt; or &lt;code&gt;mergeCoinImmediate&lt;/code&gt; already encapsulates the UTXO pointer the contract needs to spend from. You want all of that living in one place, keyed by color, so any circuit that needs to receive or send a particular asset knows exactly where to look.&lt;/p&gt;




&lt;h2&gt;
  
  
  Error 2: Receiving and Sending from the Same Contract Balance in One Circuit
&lt;/h2&gt;

&lt;p&gt;This one caused the most confusing errors. The symptom is a &lt;strong&gt;public mismatch error from the proof server&lt;/strong&gt; which is cryptic enough that you might chase it for a while before finding the root cause.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The mistake:&lt;/strong&gt; Writing a single circuit that both receives shielded tokens &lt;em&gt;into&lt;/em&gt; the contract balance (&lt;code&gt;receiveShielded&lt;/code&gt; then update your balance map) &lt;em&gt;and&lt;/em&gt; sends shielded tokens &lt;em&gt;from&lt;/em&gt; the same balance map (&lt;code&gt;sendShielded&lt;/code&gt;) for the same coin type.&lt;/p&gt;

&lt;p&gt;The intuition that leads you here is natural in a two-sided operation like a collateral deposit paired with a loan payout, you want to take in one token and release another in the same transaction. In a liquidity rebalance, you take in one asset and send another. Seems like one atomic operation. It isn't, at least not when both sides touch the same &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it fails:&lt;/strong&gt; When your circuit calls &lt;code&gt;receiveShielded&lt;/code&gt;, it marks that UTXO slot as modified in one direction. When it calls &lt;code&gt;sendShielded&lt;/code&gt; from the same balance map entry, the proof server tries to reconcile the public inputs/outputs for that UTXO across both operations in the same proof and the balance equations don't hold cleanly. This manifests as the public input mismatch error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The correct pattern:&lt;/strong&gt; Split the operation into two circuits one for receiving, one for sending.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Phase 1: receive the incoming token, update contract balance, mark state as pending
export circuit actionReceive(incomingCoin_: ShieldedCoinInfo): [] {
    const incomingCoin = disclose(incomingCoin_);
    // ... validate, update accounting state ...

    receiveShielded(incomingCoin);

    const existing = contractShieldedBalance.member(incomingCoin.color)
        ? mergeCoinImmediate(contractShieldedBalance.lookup(incomingCoin.color), incomingCoin)
        : incomingCoin;

    contractShieldedBalance.insertCoin(
        incomingCoin.color,
        existing,
        right&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(kernel.self())
    );

    // Flag that the send side is pending
    userState.insert(userKey, { ...currentState, status: Status.sendPending });
}

// Phase 2: send from contract balance, clear pending state
export circuit actionSend(outgoingCoinColor_: Bytes&amp;lt;32&amp;gt;): [] {
    const outgoingCoinColor = disclose(outgoingCoinColor_);
    const state = userState.lookup(userKey);
    assert(state.status == Status.sendPending, "Send is not pending");

    // Only sending here - no receiveShielded in this circuit
    const balance = contractShieldedBalance.lookup(outgoingCoinColor);
    const result = sendShielded(balance, left&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(ownPublicKey()), state.pendingAmount);

    if (result.change.is_some) {
        contractShieldedBalance.insertCoin(outgoingCoinColor, result.change.value, right&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(kernel.self()));
    } else {
        contractShieldedBalance.remove(outgoingCoinColor);
    }

    userState.insert(userKey, { ...state, status: Status.settled, pendingAmount: 0 });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The two-phase pattern also gives you a cleaner state machine for your protocol. The pending status flags aren't just organizational they're the safety check that ensures the second circuit can only run after the first one completed successfully on-chain.&lt;/p&gt;

&lt;p&gt;There are two specific cases where you &lt;em&gt;can&lt;/em&gt; mix shielded operations in one circuit, and they work fine because they don't touch the same &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; on both sides:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case A: &lt;code&gt;receiveShielded&lt;/code&gt; + &lt;code&gt;mintShieldedToken&lt;/code&gt;&lt;/strong&gt; You're receiving one type of token (into your balance map) and minting a &lt;em&gt;different&lt;/em&gt; token (LP tokens, receipt tokens, etc.) directly to the user. No conflict because mint creates a new UTXO that wasn't in the contract's balance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Works fine: receive principal token, mint a receipt token to user
export circuit depositAndMintReceipt(incomingCoin_: ShieldedCoinInfo): [] {
    const incomingCoin = disclose(incomingCoin_);

    receiveShielded(incomingCoin);

    const existing = contractShieldedBalance.member(incomingCoin.color)
        ? mergeCoinImmediate(contractShieldedBalance.lookup(incomingCoin.color), incomingCoin)
        : incomingCoin;

    contractShieldedBalance.insertCoin(
        incomingCoin.color,
        existing,
        right&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(kernel.self())
    );

    // Mint a receipt token of a completely different color to the user
    mintShieldedToken(
        receiptTokenDomainSeparator,
        incomingCoin.value,
        mintNonce(),
        left&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(ownPublicKey())
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Case B: &lt;code&gt;receiveShielded&lt;/code&gt; + &lt;code&gt;sendImmediateShielded&lt;/code&gt;&lt;/strong&gt; You receive a token and immediately send it to the burn address (or anywhere else) &lt;em&gt;without&lt;/em&gt; going through the contract's managed balance map. The &lt;code&gt;sendImmediate&lt;/code&gt; family of operations routes tokens directly without touching the contract's stored &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt;, so there's no conflict. This is the correct pattern for burning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Works fine: receive a receipt token from user, burn it immediately
export circuit burnReceiptAndWithdraw(receiptCoin_: ShieldedCoinInfo, underlyingCoinColor_: Bytes&amp;lt;32&amp;gt;): [] {
    const receiptCoin = disclose(receiptCoin_);
    const underlyingColor = disclose(underlyingCoinColor_);
    const burnAddr = shieldedBurnAddress();

    receiveShielded(receiptCoin);
    sendImmediateShielded(receiptCoin, burnAddr, receiptCoin.value); // burn receipt token directly

    // Now send the underlying asset FROM the contract balance (different coin type = no conflict)
    const balance = contractShieldedBalance.lookup(underlyingColor);
    const withdrawAmount = calculateWithdrawable(receiptCoin.value);
    const result = sendShielded(balance, left&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(ownPublicKey()), withdrawAmount);

    if (result.change.is_some) {
        contractShieldedBalance.insertCoin(underlyingColor, result.change.value, right&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(kernel.self()));
    } else {
        contractShieldedBalance.remove(underlyingColor);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Error 3: Treating Shielded Token Balance Like an Unshielded Balance
&lt;/h2&gt;

&lt;p&gt;If you're coming from building unshielded token contracts on Midnight, you might expect the contract runtime to track your shielded holdings automatically. It doesn't.&lt;/p&gt;

&lt;p&gt;With unshielded tokens, the runtime handles the accounting your contract just calls the right transfer functions and the ledger reflects what's there. Shielded tokens work completely differently. The contract has &lt;strong&gt;no automatic awareness&lt;/strong&gt; of what shielded UTXOs it holds. If you receive a shielded deposit and don't explicitly track it in a ledger field, that UTXO is effectively lost you can't spend it because you have no reference to it.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; type is what gives you that reference. It's the pointer from your contract's ledger into the shielded UTXO set. Without it, you cannot call &lt;code&gt;sendShielded&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export ledger contractShieldedBalance: Map&amp;lt;Bytes&amp;lt;32&amp;gt;, QualifiedShieldedCoinInfo&amp;gt;;

export circuit receiveDeposit(incomingCoin_: ShieldedCoinInfo): [] {
    const incomingCoin = disclose(incomingCoin_);

    receiveShielded(incomingCoin);

    // Without this insertCoin call, the tokens are received but permanently unspendable
    const coinToStore = contractShieldedBalance.member(incomingCoin.color)
        ? mergeCoinImmediate(contractShieldedBalance.lookup(incomingCoin.color), incomingCoin)
        : incomingCoin;

    contractShieldedBalance.insertCoin(
        incomingCoin.color,
        coinToStore,
        right&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(kernel.self())
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;insertCoin&lt;/code&gt; call is what binds the received UTXO to your contract's ledger state. Skip it and you've essentially sent tokens into a black hole.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practice 4: Always Receive ShieldedCoinInfo as a Circuit Parameter
&lt;/h2&gt;

&lt;p&gt;This one is less a confirmed error and more a pattern I've settled on firmly: always accept &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; as a &lt;strong&gt;circuit parameter&lt;/strong&gt; before calling &lt;code&gt;receiveShielded&lt;/code&gt;. Do not attempt to derive or build the coin info from other inputs inside the circuit body.&lt;/p&gt;

&lt;p&gt;The reason this matters comes down to how the proof system works. The wallet needs to see the &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; at the point of transaction construction so it can identify the matching UTXO from the user's private coin set and generate its Zswap ownership proof. The circuit parameters are what the wallet reads to know what to balance against. If the coin information isn't surfaced as a parameter, the wallet has no clean way to participate in the proof at the right time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The correct pattern:&lt;/strong&gt; Accept &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; as a parameter, disclose it inside the circuit, then pass it to &lt;code&gt;receiveShielded&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The coin arrives as a parameter - the wallet can read it and balance accordingly
export circuit deposit(incomingCoin_: ShieldedCoinInfo): [] {
    const incomingCoin = disclose(incomingCoin_);
    receiveShielded(incomingCoin);
    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frontend or CLI is responsible for constructing the &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; from the user's wallet (the wallet API exposes the user's available coins), then passing it as a parameter to the circuit call. The wallet then handles the UTXO selection and ownership proof on its side.&lt;/p&gt;




&lt;h2&gt;
  
  
  Error 5: Storing Each Deposit as an Independent UTXO
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The mistake:&lt;/strong&gt; Instead of merging new deposits into a single &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; balance, someone might reach for a &lt;code&gt;Set&amp;lt;QualifiedShieldedCoinInfo&amp;gt;&lt;/code&gt; to accumulate UTXOs, thinking they can collect them all and deal with selection later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// DON'T do this - accumulating independent UTXOs in a set
export ledger assetUTXOs: Set&amp;lt;QualifiedShieldedCoinInfo&amp;gt;;

export circuit deposit(incomingCoin_: ShieldedCoinInfo): [] {
    const incomingCoin = disclose(incomingCoin_);

    receiveShielded(incomingCoin);

    // Each deposit added as its own independent UTXO entry
    assetUTXOs.insertCoin(incomingCoin);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is a problem:&lt;/strong&gt; When the contract needs to send, &lt;code&gt;sendShielded&lt;/code&gt; takes a single &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; as input. If your liquidity is spread across a set of independent UTXOs, you now have to filter and sort through them to find entries that cover the amount you need, handle partial coverage across multiple entries, and manually merge before you can send anything. That logic is complex, brittle, and entirely avoidable. You end up writing significant off-chain UTXO selection code for a problem that doesn't need to exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The correct pattern:&lt;/strong&gt; Always merge deposits into a single aggregated balance per coin color using &lt;code&gt;mergeCoinImmediate&lt;/code&gt;. One coin color = one &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; entry in your map. When you deposit, merge. When you send, the single entry has everything you need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export circuit deposit(incomingCoin_: ShieldedCoinInfo): [] {
    const incomingCoin = disclose(incomingCoin_);

    receiveShielded(incomingCoin);

    // Merge into existing balance, or initialize if first deposit of this type
    const aggregated = contractShieldedBalance.member(incomingCoin.color)
        ? mergeCoinImmediate(contractShieldedBalance.lookup(incomingCoin.color), incomingCoin)
        : incomingCoin;

    contractShieldedBalance.insertCoin(
        incomingCoin.color,
        aggregated,
        right&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(kernel.self())
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mergeCoinImmediate&lt;/code&gt; is provided by the Compact standard library exactly for this purpose combining multiple UTXOs of the same coin type into a single &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt;. Use it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Error 6: Not Handling Change After Sending
&lt;/h2&gt;

&lt;p&gt;This one is subtle. When your contract calls &lt;code&gt;sendShielded&lt;/code&gt; to send tokens to a user, the operation doesn't automatically return the change to the contract. You have to handle it explicitly.&lt;/p&gt;

&lt;p&gt;Think of it like spending cash if you hand over a 100-unit note to pay for something that costs 70, you need to receive 30 back. If you don't explicitly handle that change, it evaporates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The mistake:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export circuit withdraw(coinColor_: Bytes&amp;lt;32&amp;gt;, amount: Uint&amp;lt;128&amp;gt;, receiver: ZswapCoinPublicKey): [] {
    const coinColor = disclose(coinColor_);
    const balance = contractShieldedBalance.lookup(coinColor);
    sendShielded(balance, left&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(receiver), amount);
    // BUG: change is lost. contractShieldedBalance still points to a spent UTXO.
    // The next attempt to send from this color will fail.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The correct pattern:&lt;/strong&gt; &lt;code&gt;sendShielded&lt;/code&gt; returns a result that contains the change (if any). Check it. If there's change, put it back into your balance map. If the balance is fully spent, remove the entry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export circuit withdraw(coinColor_: Bytes&amp;lt;32&amp;gt;, amount: Uint&amp;lt;128&amp;gt;, receiver: ZswapCoinPublicKey): [] {
    const coinColor = disclose(coinColor_);
    assert(contractShieldedBalance.member(coinColor), "No balance for this coin type");
    assert(amount &amp;gt; 0, "Invalid send amount");

    const balance = contractShieldedBalance.lookup(coinColor);
    const result = sendShielded(
        balance,
        left&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(receiver),
        amount
    );

    // Always handle the change
    if (result.change.is_some) {
        contractShieldedBalance.insertCoin(
            coinColor,
            result.change.value,
            right&amp;lt;ZswapCoinPublicKey, ContractAddress&amp;gt;(kernel.self())
        );
    } else {
        contractShieldedBalance.remove(coinColor); // fully spent, clean up
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you skip this and your contract later tries to call &lt;code&gt;sendShielded&lt;/code&gt; on a coin color whose UTXO was already spent in a previous transaction, the transaction will fail because the UTXO no longer exists in the ledger.&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting It All Together: The Pattern That Works
&lt;/h2&gt;

&lt;p&gt;Combining all of the above, here's the mental model for shielded token management that I now use in every contract:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State design:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One &lt;code&gt;Map&amp;lt;Bytes&amp;lt;32&amp;gt;, QualifiedShieldedCoinInfo&amp;gt;&lt;/code&gt; as the single source of truth for all shielded holdings&lt;/li&gt;
&lt;li&gt;All other accounting (user positions, asset configs, protocol state) in separate non-shielded ledger structures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Receive pattern:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accept &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; as a circuit parameter&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;receiveShielded&lt;/code&gt; on the disclosed coin&lt;/li&gt;
&lt;li&gt;Merge into the balance map with &lt;code&gt;mergeCoinImmediate&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Send pattern:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lookup from the balance map&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;sendShielded&lt;/code&gt;, capture the result&lt;/li&gt;
&lt;li&gt;Handle change or remove the entry&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Split circuits wherever both sides touch the same &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receive in one circuit, update state to pending&lt;/li&gt;
&lt;li&gt;Send in a separate circuit, verify pending state, execute, finalize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Safe exceptions where you can combine in one circuit:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receiving a token + minting a different token (LP tokens, receipt tokens)&lt;/li&gt;
&lt;li&gt;Receiving a token + &lt;code&gt;sendImmediate&lt;/code&gt; to burn it (doesn't touch the balance map)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Compact Standard Library &lt;code&gt;receiveShielded&lt;/code&gt;, &lt;code&gt;sendShielded&lt;/code&gt;, &lt;code&gt;sendImmediateShielded&lt;/code&gt;, &lt;code&gt;mintShieldedToken&lt;/code&gt;, &lt;code&gt;mergeCoinImmediate&lt;/code&gt;: &lt;a href="https://docs.midnight.network/develop/reference/compact" rel="noopener noreferrer"&gt;docs.midnight.network/develop/reference/compact&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Shielded token concepts and &lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt;: &lt;a href="https://docs.midnight.network/develop/guides/shielded-tokens" rel="noopener noreferrer"&gt;docs.midnight.network/develop/guides/shielded-tokens&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Zswap protocol overview: &lt;a href="https://docs.midnight.network/learn/zswap" rel="noopener noreferrer"&gt;docs.midnight.network/learn/zswap&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Transaction balancing and proof construction: &lt;a href="https://docs.midnight.network/develop/guides/transactions" rel="noopener noreferrer"&gt;docs.midnight.network/develop/guides/transactions&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;These patterns took real trial and error to arrive at. The proof server errors in particular are notoriously unhelpful when you're deep in the wrong abstraction a "public mismatch" message doesn't tell you that you're trying to receive and send from the same balance in one circuit. Hopefully this saves someone else those hours.&lt;/p&gt;

&lt;p&gt;If you're building shielded DeFi on Midnight and running into something I didn't cover here, feel free to reach out.&lt;/p&gt;

&lt;p&gt;Follow Me on X: &lt;a href="https://x.com/codebigint_01" rel="noopener noreferrer"&gt;https://x.com/codebigint_01&lt;/a&gt;&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>blockchain</category>
      <category>defi</category>
      <category>web3</category>
    </item>
    <item>
      <title>I Built a Vault Before Anyone Finished the Lock</title>
      <dc:creator>Tushar Pamnani</dc:creator>
      <pubDate>Wed, 24 Jun 2026 13:51:38 +0000</pubDate>
      <link>https://dev.to/midnight-aliit/i-built-a-vault-before-anyone-finished-the-lock-49ik</link>
      <guid>https://dev.to/midnight-aliit/i-built-a-vault-before-anyone-finished-the-lock-49ik</guid>
      <description>&lt;p&gt;Over the last few weeks, I've been building &lt;a href="https://obscura.sevryn.xyz" rel="noopener noreferrer"&gt;Obscura&lt;/a&gt;, a token launchpad for Midnight.&lt;/p&gt;

&lt;p&gt;There's just one problem.&lt;/p&gt;

&lt;p&gt;Obscura's entire pitch is that your position disappears. Right now, on Midnight mainnet, nothing disappears.&lt;/p&gt;

&lt;p&gt;Here's the actual situation: tokens already exist, the base account-based standard is live, and that's what Obscura mints against. What isn't live yet is the piece that lets those tokens vanish into Midnight's UTXO model, which is the actual mechanism the privacy pitch runs on. It's defined in &lt;a href="https://github.com/midnightntwrk/midnight-improvement-proposals" rel="noopener noreferrer"&gt;MIP-0004&lt;/a&gt;, still sitting in Draft status, already proven out on preprod, but not deployed to mainnet.&lt;/p&gt;

&lt;p&gt;So: a vault, built before anyone finished the lock.&lt;/p&gt;

&lt;p&gt;Because I wasn't building for today's Midnight. I was building for the Midnight that exists six months from now.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Idea Started With a Different Problem
&lt;/h2&gt;

&lt;p&gt;Over the last six months, I've spent most of my time building on Midnight.&lt;/p&gt;

&lt;p&gt;I joined Allit Fellowship and Build Club Cohort 1. Built DeFi protocols. Shipped &lt;code&gt;mn-scaffold&lt;/code&gt; and &lt;code&gt;midnight-wallet-kit&lt;/code&gt;. Ran prediction markets, lending protocols, identity systems, voting systems, escrow, most of the primitives an ecosystem eventually needs.&lt;/p&gt;

&lt;p&gt;Somewhere in there, I also wrote the &lt;a href="https://compact-book.sevryn.xyz" rel="noopener noreferrer"&gt;Compact Book&lt;/a&gt;. When I started on Midnight, there wasn't a complete resource that explained Compact end-to-end easily, so I wrote one. Twenty-five chapters later, it became one of the most widely referenced learning resources. Not bragging, just context: it's the reason I've come to trust "build the thing the ecosystem hasn't gotten around to yet" as a strategy, not just an excuse.&lt;/p&gt;

&lt;p&gt;I also founded &lt;a href="https://midnight-skills.sevryn.xyz" rel="noopener noreferrer"&gt;Midnight Skills&lt;/a&gt; and took it everywhere I could get a screen share - Midnight's own Fireside Dev Hangout, my own Twitter livestream, and MLH's hackathon for Midnight. That alone brought roughly 250 developers into the ecosystem.&lt;/p&gt;

&lt;p&gt;The more time I spent building, the more I noticed a pattern.&lt;/p&gt;

&lt;p&gt;Every ecosystem eventually reaches the same inflection point. Developers stop asking &lt;em&gt;"can I build here?"&lt;/em&gt; and start asking &lt;em&gt;"what can I launch here?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That transition changes everything. Ethereum had it. Solana had it. Base had it. Each time, a creator economy formed around tokens, and launchpads became the infrastructure layer that made experimentation cheap.&lt;/p&gt;

&lt;p&gt;The question was never whether Midnight would need this. The question was what a launchpad looks like when the underlying chain is privacy-first instead of fully transparent.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Traditional Launchpad Model
&lt;/h2&gt;

&lt;p&gt;Most launchpads converge on the same flow: a creator launches a token, early buyers transact against a bonding curve, price rises as supply is bought up, and once the curve hits a liquidity threshold, the token "graduates" to open trading.&lt;/p&gt;

&lt;p&gt;Pump(.)fun popularized it. Moonshot and a dozen EVM clones refined it. The mechanics are solved.&lt;/p&gt;

&lt;p&gt;But that whole model assumes total transparency - every buy, every wallet, every position is public the instant it happens. That's &lt;em&gt;the point&lt;/em&gt; on Solana and Ethereum. It's also the reason every bonding-curve launch turns into a sniping contest: bots watch the chain, front-run obvious buys, and the loudest wallets win before retail even sees the chart move.&lt;/p&gt;

&lt;p&gt;Midnight doesn't replicate that. It's built to bring privacy to programmable assets. So the real design question wasn't "how do I copy pump.fun"; it was "what does a bonding curve look like when participant positions aren't broadcast to every bot watching the chain?"&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Obscura
&lt;/h2&gt;

&lt;p&gt;The name took three tries. It started as Nox, became Veil once I realized Nox was already in use elsewhere in the ecosystem, and finally settled as Obscura; after the &lt;em&gt;camera obscura&lt;/em&gt;, the original device that lets you observe an image without the subject knowing it's being observed. That's the actual thesis: a market you can see clearly without every other participant seeing you.&lt;/p&gt;

&lt;p&gt;The pitch, stripped down: &lt;strong&gt;fair market infrastructure&lt;/strong&gt;, a token launch and bonding-curve market where information asymmetry isn't just discouraged, it's structurally impossible, because the chain underneath doesn't leak positions the way Solana or Ethereum do.&lt;/p&gt;

&lt;p&gt;Mechanically, the flow looks familiar; creators deploy a token, early buyers transact against an automated curve, price discovery happens on-chain, the token eventually graduates to broader liquidity. What's different is what's hidden along the way: balances and position sizes stay shielded, so the usual whale-signals-the-move-and-everyone-front-runs-it dynamic doesn't have anywhere to attach itself.&lt;/p&gt;

&lt;p&gt;Visually, the brand follows the metaphor all the way through; near-black background, a single copper accent, IBM Plex Mono for anything functional, Cormorant Garamond for display type, and a three-ring aperture that breathes open and closed like a shutter. Small detail, but it's the kind of thing that makes a product feel designed rather than assembled.&lt;/p&gt;

&lt;p&gt;Simple in theory. Much harder in practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  The First Reality Check
&lt;/h2&gt;

&lt;p&gt;The first real challenge wasn't launch mechanics or tokenomics. It was infrastructure that didn't exist yet.&lt;/p&gt;

&lt;p&gt;Earlier this year I redesigned a lending protocol twice after running into Midnight's block limits. Building Obscura felt familiar. Once again, the hard problems weren't in the whiteboard design. They appeared when the design met the actual constraints of the chain.&lt;/p&gt;

&lt;p&gt;The piece that's missing isn't the token standard itself; it's the extension to it. &lt;a href="https://github.com/midnightntwrk/midnight-improvement-proposals" rel="noopener noreferrer"&gt;MIP-0004&lt;/a&gt; adds four circuits; &lt;code&gt;shield&lt;/code&gt;, &lt;code&gt;toUtxo&lt;/code&gt;, &lt;code&gt;unshield&lt;/code&gt;, &lt;code&gt;fromUtxo&lt;/code&gt;, that let a token built on the existing account-based standard convert into shielded or unshielded UTXOs and back. That conversion is what makes the shielded-position story real: without it, a token can mint and transfer, but it can't disappear into Zswap the way Obscura's pitch depends on.&lt;/p&gt;

&lt;p&gt;It's further along than "still being debated" makes it sound; there's a working reference implementation, deployed and exercised on preprod with real transactions across the full round trip, including atomic swaps between two tokens in both shielded and unshielded form. What it doesn't have yet is Draft status promoted to Active, or a mainnet deployment. So the actual decision wasn't "wait for a standard to exist or build my own", it was whether to launch the bonding-curve mechanics now on the standard that's already live, and bring in the shielding layer once MIP-0004 clears mainnet, or hold the whole product until a still-Draft extension is fully rolled out.&lt;/p&gt;

&lt;p&gt;I chose to ship. Obscura's v1 launches on the existing account-based standard; the curve, the mechanics, the launch flow all work today. The deeper privacy guarantees that MIP-0004 unlocks; full shielded position-hiding through UTXO conversion, are the v2 layer, adopted once the extension is stable and live on mainnet rather than sitting on preprod. That's not a shortcut I'm hiding; it's the actual trade-off. Waiting on a Draft proposal to formally graduate means losing the only thing an early mover actually has: time.&lt;/p&gt;

&lt;p&gt;That's the real lesson buried in this project, and it had nothing to do with code: &lt;strong&gt;a working product and a launchable product are not the same thing.&lt;/strong&gt; Obscura works. Whether it's launchable at full strength depends on pieces outside the application entirely; token standards, liquidity rails, the parts of the ecosystem that aren't mine to control.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Build It Anyway
&lt;/h2&gt;

&lt;p&gt;Because ecosystems don't produce infrastructure on their own. Someone has to build it first, before it's obviously needed.&lt;/p&gt;

&lt;p&gt;I've already run this play once. The Compact Book shipped before the Foundation's own technical DevRel team finished a comparable resource. It became the canonical reference anyway, not because it was the most polished thing that could've existed, but because it existed when nothing else did.&lt;/p&gt;

&lt;p&gt;Every mature ecosystem has a graveyard of things built too early. It also has a handful of things that became foundational &lt;em&gt;because&lt;/em&gt; they were early. I don't know which pile Obscura ends up in. That's not really the point.&lt;/p&gt;

&lt;p&gt;The point is that building before the rails exist teaches you things documentation can't. You find the bottlenecks nobody's written about. You find the missing standards. You find which of your assumptions don't survive contact with a chain that's still being built under you.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Biggest Lesson
&lt;/h2&gt;

&lt;p&gt;Before building Obscura, I thought launchpads were a token problem, get the contract and the curve right, and the rest follows.&lt;/p&gt;

&lt;p&gt;After building Obscura, I think launchpads are a distribution problem. The contracts matter. The curve matters. The standard matters. None of it matters if nobody launches, and nobody launches without distribution, liquidity, builders, and a reason to show up on day one.&lt;/p&gt;

&lt;p&gt;Technology creates the possibility. A community creates the adoption. The best launchpad in the world is just an interesting demo without an ecosystem willing to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building Ahead of the Ecosystem
&lt;/h2&gt;

&lt;p&gt;"Build for where the ecosystem is going, not where it is today" is the kind of line that sounds like a poster until you're actually doing it, and then it's just a description of your Tuesday.&lt;/p&gt;

&lt;p&gt;Obscura isn't a bet on current activity; there isn't much yet. It's a bet that Midnight's token economy shows up eventually, that creators will want launch infrastructure when it does, and that privacy-preserving assets open up mechanics that don't exist on fully transparent chains at all. Shielded position sizes aren't just a privacy feature, they remove an entire category of extractive behavior that every transparent-chain launchpad has had to design around after the fact.&lt;/p&gt;

&lt;p&gt;Maybe the bet is right. Maybe it isn't. The only way to find out is to build it before it's obvious.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;Obscura is still an early look at what token launches on Midnight could be; not a finished product, and not pretending to be one. The waitlist is open now at &lt;a href="https://obscura.sevryn.xyz/" rel="noopener noreferrer"&gt;obscura.sevryn.xyz&lt;/a&gt;, and as MIP-0004 clears Draft status and lands on mainnet, a lot of what's true about this version will change. That's expected. Building early was never about getting everything right on the first pass, it's about learning faster than everyone else gets the chance to.&lt;/p&gt;

&lt;p&gt;Maybe Obscura launches six months too early. Maybe it becomes one of the first launchpads people use when Midnight's token economy arrives. I genuinely don't know.&lt;/p&gt;

&lt;p&gt;What I do know is that by the time everyone starts asking how to launch a token on Midnight, I won't be asking the question anymore. I'll already know the answer.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If you want the deeper technical context on how Compact and Midnight's ZK model work under the hood, I wrote the &lt;a href="https://compact-book.sevryn.xyz" rel="noopener noreferrer"&gt;Compact Book&lt;/a&gt;, it's the resource I wish existed when I started.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>compact</category>
      <category>web3</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Learning Midnight: nothing goes public by accident</title>
      <dc:creator>Tminus1sec</dc:creator>
      <pubDate>Mon, 22 Jun 2026 13:26:58 +0000</pubDate>
      <link>https://dev.to/tminus1s/learning-midnight-nothing-goes-public-by-accident-2odm</link>
      <guid>https://dev.to/tminus1s/learning-midnight-nothing-goes-public-by-accident-2odm</guid>
      <description>&lt;p&gt;I'm a self-taught enthusiast poking around the Midnight Network, and I wanted to write down one thing that finally clicked for me this week. Maybe it helps someone else who's just starting out.&lt;br&gt;
Midnight is a privacy blockchain, and you write its smart contracts in a language called Compact. Coming in, I assumed "privacy blockchain" meant everything is hidden by default and you flip a switch when you want to share something.&lt;br&gt;
It's actually the other way around, and that surprised me.&lt;br&gt;
In Compact, anything you want to put into the public, on-chain state you have to mark on purpose, with a keyword called disclose(). If you forget, the compiler stops you. It won't let you publish a value unless you've clearly said "yes, I mean to make this public."&lt;br&gt;
The first time I hit that, it felt annoying, like extra typing for no reason. Then it clicked: that IS the privacy model. Nothing leaks by accident. You go line by line and decide exactly what crosses from the private side to the public side. Everything you don't disclose just stays private.&lt;br&gt;
That made Midnight's whole "choose what you share, nothing more" idea real for me in a way the tagline never did.&lt;br&gt;
To play with it, I built a tiny app: a proof-of-age gate. You prove you're over 18 without ever showing your birth year. The birth year stays private the whole time; the only thing that becomes public is a simple "yes, this person qualifies." Same idea, share the one fact that matters and keep the rest to yourself.&lt;br&gt;
Still very early in my journey, but this one stuck with me. Most of what I picked up came straight from the docs at docs.midnight.network.&lt;br&gt;
Repo, if you want to peek: &lt;a href="https://github.com/tomiin/midnight-proof-of-age" rel="noopener noreferrer"&gt;https://github.com/tomiin/midnight-proof-of-age&lt;/a&gt;&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>zeroknowledge</category>
      <category>blockchain</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building a Shielded Token dApp on Midnight: From Compact Contract to React UI</title>
      <dc:creator>Ragrin Armstrong</dc:creator>
      <pubDate>Wed, 17 Jun 2026 10:24:38 +0000</pubDate>
      <link>https://dev.to/ragrin_armstrong_a78a0748/building-a-shielded-token-dapp-on-midnight-from-compact-contract-to-react-ui-2ja0</link>
      <guid>https://dev.to/ragrin_armstrong_a78a0748/building-a-shielded-token-dapp-on-midnight-from-compact-contract-to-react-ui-2ja0</guid>
      <description>&lt;p&gt;When I first looked at Midnight's privacy model, I kept tripping over the same question. If the chain hides balances inside zero-knowledge proofs, how do you actually write a token contract? Where does the contract end and the wallet begin?&lt;/p&gt;

&lt;p&gt;This tutorial answers that by building one. We write a Compact contract that mints, transfers, and burns shielded tokens, wire it through TypeScript, and drive it from a React frontend. By the end you will have a working dApp you can run locally, plus the mental model to take it to testnet.&lt;/p&gt;

&lt;p&gt;The full source lives at &lt;a href="https://github.com/kwanter/midnight-shielded-dapp" rel="noopener noreferrer"&gt;the companion repo&lt;/a&gt;. Clone it, follow along, or just read the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Midnight actually gives you
&lt;/h2&gt;

&lt;p&gt;Midnight is a privacy first blockchain from the team behind Cardano. The important part for us is how it splits responsibility.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ledger layer&lt;/strong&gt; holds the shielded coins as UTXO commitments. The wallet SDK (&lt;code&gt;@midnight-ntwrk/wallet-sdk-shielded&lt;/code&gt;) spends them. This is where the privacy actually lives. Coins are committed on chain, but their value, recipient, and linkability are hidden behind ZK proofs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contract layer&lt;/strong&gt; is your Compact code. It runs as a circuit. The contract's job is authorization and bookkeeping, not hiding values. Privacy is the ledger's problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This split feels strange if you come from Solidity. In Ethereum the contract is the whole story. On Midnight the contract authorizes a value movement and the ledger hides it.&lt;/p&gt;

&lt;p&gt;That means our shielded token contract does two things. It tracks a public total supply so anyone can audit the cap, and it gates who can mint or burn. The actual private coins are out of scope for the circuit. That is the wallet's job.&lt;/p&gt;

&lt;h2&gt;
  
  
  The keypair admin pattern
&lt;/h2&gt;

&lt;p&gt;Before any tokens exist we need someone authorized to mint them. We could store the deployer's address on chain, but that ties the contract to a specific wallet identity and leaks who deployed it.&lt;/p&gt;

&lt;p&gt;Midnight's official NFT example uses a better pattern. The deployer generates a private key locally, derives a public key from it, and stores only the public key on chain. Every mint call re-derives the public key from the local private key and checks it matches the committed value. The deployer's wallet identity never appears on chain.&lt;/p&gt;

&lt;p&gt;Here is the contract entrypoint that sets this up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma language_version &amp;gt;= 0.22.0;

import CompactStandardLibrary;
import "./modules/ShieldedToken";

export { totalSupply, balanceOf, mint, burn };

struct AdminSecretKey { bytes: Bytes&amp;lt;32&amp;gt;; }
struct AdminPublicKey { bytes: Bytes&amp;lt;32&amp;gt;; }

export ledger contractAdmin: AdminPublicKey;

witness localSecretKey(): AdminSecretKey;

constructor() {
  contractAdmin = disclose(deriveAdminPublicKey(localSecretKey()));
}

export circuit deriveAdminPublicKey(sk: AdminSecretKey): AdminPublicKey {
  return AdminPublicKey {
    bytes: persistentHash&amp;lt;Vector&amp;lt;2, Bytes&amp;lt;32&amp;gt;&amp;gt;&amp;gt;([pad(32, "shielded:admin:pk:v1"), sk.bytes])
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to notice if this is your first Compact code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ledger&lt;/code&gt; declares on-chain state. It is public and readable by anyone querying the contract.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;witness&lt;/code&gt; declares a function the wallet provides. The contract calls it, but the wallet decides what it returns. This is how private data enters a circuit without leaking.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;disclose&lt;/code&gt; marks a value as safe to reveal on chain. The constructor stores the derived public key, not the private key.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;persistentHash&lt;/code&gt; is a domain-separated hash. We pad a namespace string (&lt;code&gt;shielded:admin:pk:v1&lt;/code&gt;) so this key cannot collide with one derived for some other purpose. Domain separation is a small habit that prevents a whole class of cross-protocol attacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Minting and burning
&lt;/h2&gt;

&lt;p&gt;With admin authority in place, the mint and burn circuits are short.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export circuit mintShieldedToken(to: ZswapCoinPublicKey, amount: Uint&amp;lt;64&amp;gt;): [] {
  assert(contractAdmin == deriveAdminPublicKey(localSecretKey()), "Not authorized to mint.");
  assert(disclose(amount) &amp;gt; 0, "Amount must be positive.");
  mint(to, amount);
}

export circuit burnShieldedToken(amount: Uint&amp;lt;64&amp;gt;): [] {
  assert(disclose(amount) &amp;gt; 0, "Amount must be positive.");
  const holder = ownPublicKey();
  burn(holder, amount);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ZswapCoinPublicKey&lt;/code&gt; is the 32-byte public key a wallet derives for receiving shielded coins. It is not the same as an account address. When you ask a wallet for its shielded address, you get back this coin public key plus an encryption key.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ownPublicKey()&lt;/code&gt; is a built-in that returns the coin public key of whoever signed the current transaction. For burn, that is the holder proving they own the tokens being destroyed.&lt;/p&gt;

&lt;p&gt;The actual balance arithmetic lives in a module, which keeps the entrypoint readable and matches how the official examples are structured.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module ShieldedToken {

  import CompactStandardLibrary;

  export ledger totalSupply: Uint&amp;lt;64&amp;gt;;
  export ledger balances: Map&amp;lt;ZswapCoinPublicKey, Uint&amp;lt;64&amp;gt;&amp;gt;;

  export circuit balanceOf(holder: ZswapCoinPublicKey): Uint&amp;lt;64&amp;gt; {
    assert(holder != default&amp;lt;ZswapCoinPublicKey&amp;gt;, "Holder cannot be empty.");
    if (balances.member(disclose(holder))) {
      return balances.lookup(disclose(holder));
    } else {
      return 0;
    }
  }

  export circuit mint(to: ZswapCoinPublicKey, amount: Uint&amp;lt;64&amp;gt;): [] {
    assert(to != default&amp;lt;ZswapCoinPublicKey&amp;gt;, "Recipient cannot be empty.");
    assert(disclose(amount) &amp;gt; 0, "Amount must be positive.");
    if (!balances.member(disclose(to))) {
      balances.insert(disclose(to), 0);
    }
    const prev: Uint&amp;lt;64&amp;gt; = balances.lookup(disclose(to));
    const next: Uint&amp;lt;64&amp;gt; = (prev + disclose(amount)) as Uint&amp;lt;64&amp;gt;;
    balances.insert(disclose(to), next);
    totalSupply = (totalSupply + disclose(amount)) as Uint&amp;lt;64&amp;gt;;
  }

  export circuit burn(holder: ZswapCoinPublicKey, amount: Uint&amp;lt;64&amp;gt;): [] {
    assert(holder != default&amp;lt;ZswapCoinPublicKey&amp;gt;, "Holder cannot be empty.");
    assert(balances.member(disclose(holder)), "Holder has no balance.");
    assert(balances.lookup(disclose(holder)) &amp;gt;= disclose(amount), "Insufficient balance.");
    const prev: Uint&amp;lt;64&amp;gt; = balances.lookup(disclose(holder));
    const next: Uint&amp;lt;64&amp;gt; = (prev - disclose(amount)) as Uint&amp;lt;64&amp;gt;;
    balances.insert(disclose(holder), next);
    totalSupply = (totalSupply - disclose(amount)) as Uint&amp;lt;64&amp;gt;;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the part where Compact differs from Solidity the most, so it is worth slowing down.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why all the &lt;code&gt;disclose()&lt;/code&gt; calls
&lt;/h3&gt;

&lt;p&gt;In a ZK circuit, values that depend on private inputs stay private by default. The compiler refuses to write them to the public ledger unless you explicitly opt in. When you write &lt;code&gt;amount &amp;gt; 0&lt;/code&gt;, the result of that comparison is fine to leak (it is just a boolean), but &lt;code&gt;amount&lt;/code&gt; itself is a private input until you disclose it.&lt;/p&gt;

&lt;p&gt;When you then do &lt;code&gt;prev + amount&lt;/code&gt; and want to store the result on chain, the compiler says wait, that arithmetic result depends on a private value, and writing it to the ledger would disclose information derived from it. So you call &lt;code&gt;disclose(amount)&lt;/code&gt; to tell the compiler yes, I know this reveals the amount, and that is the point. Mint amounts are public in this design.&lt;/p&gt;

&lt;p&gt;If you wanted the amounts to stay private, you would keep the bookkeeping off chain entirely and let the wallet's shielded coin layer handle it. That is a more advanced pattern and out of scope here, but it is worth knowing the trade-off exists.&lt;/p&gt;

&lt;p&gt;This default-private behavior is the single biggest difference between Compact and Solidity. Solidity has no concept of private inputs to a function. Every parameter is visible to anyone reading the transaction calldata. You can hash values before storing them, but the inputs themselves are public by construction. Compact inverts this. Privacy is the default, and disclosure is an explicit, auditable decision you make at each ledger write. When you read a Compact contract, the &lt;code&gt;disclose()&lt;/code&gt; calls are the points where data crosses from private to public. Scan for them and you have a map of exactly what the contract leaks.&lt;/p&gt;

&lt;p&gt;This also changes how you reason about a contract audit. In Solidity you ask, what can an attacker learn by watching the chain? In Compact you ask, did the author disclose more than they needed to? The answer for this tutorial contract is yes, intentionally, because we want the supply and balances to be auditable. A production privacy coin would disclose almost nothing from the contract layer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the explicit &lt;code&gt;as Uint&amp;lt;64&amp;gt;&lt;/code&gt; casts
&lt;/h3&gt;

&lt;p&gt;Compact's type system widens arithmetic results to the full field range, roughly 254 bits. Adding two &lt;code&gt;Uint&amp;lt;64&amp;gt;&lt;/code&gt; values does not give you another &lt;code&gt;Uint&amp;lt;64&amp;gt;&lt;/code&gt;, it gives you a field element that could be larger. The compiler will not silently narrow it back, because doing so could hide an overflow.&lt;/p&gt;

&lt;p&gt;So after every arithmetic expression we cast explicitly. &lt;code&gt;(prev + disclose(amount)) as Uint&amp;lt;64&amp;gt;&lt;/code&gt; tells the compiler we have considered overflow and accept the narrowing. For a tutorial contract this is fine. For production you would add an explicit overflow check first.&lt;/p&gt;

&lt;p&gt;I hit this the hard way while building this. My first version used &lt;code&gt;Counter&lt;/code&gt;, the standard library type from the official counter example. Counter has &lt;code&gt;.increment()&lt;/code&gt; and &lt;code&gt;.decrement()&lt;/code&gt; methods that take &lt;code&gt;Uint&amp;lt;16&amp;gt;&lt;/code&gt; deltas, capped at 65,535. Fine for a click counter, useless for token amounts. The lesson is that Counter is for counting, Uint is for amounts, and the two do not mix.&lt;/p&gt;

&lt;h2&gt;
  
  
  The witness provider
&lt;/h2&gt;

&lt;p&gt;The contract declares &lt;code&gt;witness localSecretKey(): AdminSecretKey&lt;/code&gt; but does not implement it. The implementation lives in TypeScript, because the witness runs in the wallet, not on chain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Ledger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../managed/shielded-token/contract/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@midnight-ntwrk/compact-runtime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="na"&gt;adminSecretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createShieldedTokenPrivateState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;adminSecretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;adminSecretKey&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;generateAdminSecretKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRandomValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adminSecretKey&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The witness receives the current private state and returns a tuple. The first element is the updated private state (unchanged here, we just read the key). The second is the value the contract sees as the witness result.&lt;/p&gt;

&lt;p&gt;The private state is a plain object the wallet holds locally. It never goes on chain. The admin key lives in it, generated once at deploy time with &lt;code&gt;crypto.getRandomValues&lt;/code&gt; and persisted in the wallet from then on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiling and testing
&lt;/h2&gt;

&lt;p&gt;The Compact compiler is not an npm package. Midnight ships it through their own installer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;--proto&lt;/span&gt; &lt;span class="s1"&gt;'=https'&lt;/span&gt; &lt;span class="nt"&gt;--tlsv1&lt;/span&gt;.2 &lt;span class="nt"&gt;-LsSf&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  https://github.com/midnightntwrk/compact/releases/latest/download/compact-installer.sh | sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That puts a &lt;code&gt;compact&lt;/code&gt; binary at &lt;code&gt;~/.local/bin&lt;/code&gt;. The binary is a version manager. To get the specific compiler version the official examples target, run &lt;code&gt;compact update 0.30.0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Compiling the contract generates TypeScript under &lt;code&gt;src/managed/&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;contract
npm &lt;span class="nb"&gt;install
&lt;/span&gt;npm run compact
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated &lt;code&gt;index.js&lt;/code&gt; exports a &lt;code&gt;Contract&lt;/code&gt; class, a &lt;code&gt;ledger&lt;/code&gt; function for reading state, and typed wrappers for every circuit. This is what the rest of your TypeScript code imports.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing without a node
&lt;/h3&gt;

&lt;p&gt;You do not need a running Midnight node to test contract logic. The &lt;code&gt;compact-runtime&lt;/code&gt; package ships an in-memory simulator that runs your circuits directly.&lt;/p&gt;

&lt;p&gt;The pattern, lifted from the official counter example, is a small wrapper class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CircuitContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;sampleContractAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;createConstructorContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;createCircuitContext&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@midnight-ntwrk/compact-runtime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../managed/shielded-token/contract/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../witnesses.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Recipient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShieldedTokenSimulator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Contract&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;circuitContext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CircuitContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adminSecretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Contract&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;witnesses&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;currentPrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;currentContractState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;currentZswapLocalState&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;createConstructorContext&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;adminSecretKey&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circuitContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCircuitContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;sampleContractAddress&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="nx"&gt;currentZswapLocalState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;currentContractState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;currentPrivateState&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;mintShieldedToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Recipient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circuitContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;impureCircuits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mintShieldedToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circuitContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;
    &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;holder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Recipient&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;impureCircuits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circuitContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;holder&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;circuitContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each circuit call returns a new context. You thread that context into the next call. The simulator is pure in the sense that it does not touch the network, but it mutates its own context as circuits run, so you treat it as stateful.&lt;/p&gt;

&lt;p&gt;With that wrapper, tests read like specs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rejects mint from a caller without the admin key&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sim&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ShieldedTokenSimulator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;freshAdminKey&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrongKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;freshAdminKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;sim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replacePrivateState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;createShieldedTokenPrivateState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wrongKey&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mintShieldedToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sampleRecipient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toThrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Not authorized/&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rejects burn larger than the holder's balance&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sim&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ShieldedTokenSimulator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;freshAdminKey&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="nx"&gt;sim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mintShieldedToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sampleRecipient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;sim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setCaller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sampleRecipient&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;burnShieldedToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toThrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Insufficient balance/&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run them with &lt;code&gt;npm test&lt;/code&gt;. Ten tests cover the happy path plus the authorization and bounds checks. If a test fails, you see the exact assert message from the contract.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;setCaller&lt;/code&gt; method on the simulator is worth a note. The contract calls &lt;code&gt;ownPublicKey()&lt;/code&gt; to find out who is transacting. In the simulator, that reads from the circuit context's Zswap local state. &lt;code&gt;setCaller&lt;/code&gt; swaps the coin public key there, which is how a test pretends to be a different wallet signing the transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wiring a React frontend
&lt;/h2&gt;

&lt;p&gt;The contract is the hard part. The frontend is plumbing. We use Vite with React and TypeScript, and a thin API layer that talks to the simulator. On a real deploy you would swap the simulator for the wallet SDK, but the function signatures stay the same.&lt;/p&gt;

&lt;p&gt;The API layer is one file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;deployContract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adminKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;DappState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;contract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Contract&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ShieldedTokenPrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;witnesses&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createShieldedTokenPrivateState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adminKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;constructorCtx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createConstructorContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;constructorCtx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCircuitContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;sampleContractAddress&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentZswapLocalState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentContractState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentPrivateState&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;makeState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ready&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;simulator&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[deployContract]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;makeState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mintTokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;DappState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;requireContract&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;contract&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;impureCircuits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mintShieldedToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;recipient&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;makeState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ready&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;totalSupply&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;readTotalSupply&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[mintTokens]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;makeState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;totalSupply&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;safeTotalSupply&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things here matter more than they look.&lt;/p&gt;

&lt;p&gt;First, errors get logged before they get caught. A common mistake in this kind of wrapper is to swallow every exception and return a default. I did that in the first draft, and &lt;code&gt;balanceOfHolder&lt;/code&gt; silently returned &lt;code&gt;0n&lt;/code&gt; on any failure. A corrupted context looked identical to an empty wallet. Now errors throw, callers catch at the boundary, and the console shows what actually broke.&lt;/p&gt;

&lt;p&gt;Second, &lt;code&gt;mintTokens&lt;/code&gt; preserves the known supply on error instead of zeroing it. If a mint fails after the contract already holds tokens, the UI should still show the real supply, not flash to zero. The &lt;code&gt;safeTotalSupply&lt;/code&gt; helper reads the supply if it can and falls back to zero only as a last resort.&lt;/p&gt;

&lt;p&gt;The React side is straightforward. State holds the contract status, total supply, and the recipient balance. Buttons call the API layer. One detail worth flagging is input handling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleMint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mintAmount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid amount. Enter a whole number.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mintAmount&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;BigInt("")&lt;/code&gt; throws. So does &lt;code&gt;BigInt("1.5")&lt;/code&gt;. An unhandled throw inside a React event handler leaves the UI in a stale state with no feedback. Wrapping the conversion and showing a friendly error is the fix. It is a small thing, but it is the kind of thing a reviewer notices.&lt;/p&gt;

&lt;h2&gt;
  
  
  One build wrinkle: WebAssembly
&lt;/h2&gt;

&lt;p&gt;Midnight's runtime ships a WebAssembly binary. Vite's production bundler cannot import &lt;code&gt;.wasm&lt;/code&gt; files directly without a plugin. The dev server handles it natively, but &lt;code&gt;vite build&lt;/code&gt; fails with a confusing error about ESM integration for Wasm.&lt;/p&gt;

&lt;p&gt;The fix is two plugins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; vite-plugin-wasm vite-plugin-top-level-await
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;wasm&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vite-plugin-wasm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;topLevelAwait&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vite-plugin-top-level-await&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;wasm&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;topLevelAwait&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;esnext&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, &lt;code&gt;vite build&lt;/code&gt; bundles the runtime into a single output. The WASM file lands at around 1.4 MB uncompressed, 412 KB gzipped, which is the bulk of the bundle. For a tutorial that is fine. For production you would lazy-load it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting a real wallet
&lt;/h2&gt;

&lt;p&gt;The frontend above runs against the simulator. To connect a real wallet, Midnight defines a DApp connector API. A wallet extension injects itself at &lt;code&gt;window.midnight&lt;/code&gt; keyed by a UUID. Your code detects it, calls &lt;code&gt;connect&lt;/code&gt;, and gets back an API for balances, addresses, and transaction signing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Window&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;midnight&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;InitialAPI&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;detectWallets&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;DetectedWallet&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;midnight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;midnight&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;icon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;icon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;rdns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rdns&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apiVersion&lt;/span&gt;
  &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;connectWallet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;networkId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;midnight&lt;/span&gt;&lt;span class="p"&gt;?.[&lt;/span&gt;&lt;span class="nx"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wallet not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;midnight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;networkId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The connected API gives you shielded and unshielded balances, transaction history, and addresses. To submit a transaction that calls your contract, you build the circuit invocation, hand it to the wallet's &lt;code&gt;balanceTransaction&lt;/code&gt; method, and the wallet handles coin selection, fee payment, and proof generation. Your code stays focused on what the transaction should do, not how it gets balanced.&lt;/p&gt;

&lt;p&gt;This is the piece I left as future work in the companion repo. The simulator proves the contract is correct. The wallet integration is mechanical once you have a deployed contract address on testnet.&lt;/p&gt;

&lt;p&gt;The reason the integration is mechanical is the API contract between your code and the wallet is narrow. Your TypeScript calls the same &lt;code&gt;impureCircuits&lt;/code&gt; methods on the same &lt;code&gt;Contract&lt;/code&gt; object. The difference is where the circuit context comes from. In the simulator you build it yourself with &lt;code&gt;createCircuitContext&lt;/code&gt;. On a real deploy, the wallet SDK hands you a context backed by the actual ledger state, real coin selection, and a live proof server. The &lt;code&gt;Contract&lt;/code&gt; class does not know or care which one it is running against.&lt;/p&gt;

&lt;p&gt;That means the work of going from simulator to testnet is mostly infrastructure. You need a wallet connected, a funded account for fees, and a proof server endpoint. None of that changes your circuit logic or your witness implementations. If the contract is correct in the simulator, it is correct on chain. The simulator is not a toy, it runs the same compiled bytecode the chain would run. It just skips the consensus and networking layers.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model, restated
&lt;/h2&gt;

&lt;p&gt;If you take one thing from this, take the split.&lt;/p&gt;

&lt;p&gt;The contract authorizes. The ledger hides.&lt;/p&gt;

&lt;p&gt;When you write a shielded token contract on Midnight, you are not reimplementing privacy. You are writing the rules for who can move value, and the ledger enforces those rules behind a ZK proof. Your circuit runs, the wallet generates a proof that it ran correctly, and the chain verifies the proof without ever seeing the private inputs.&lt;/p&gt;

&lt;p&gt;That is why the contract is short. There is no Merkle tree management, no nullifier set, no commitment scheme. Those exist, but they live in the ledger, maintained by the wallet SDK. Your job is the policy layer on top.&lt;/p&gt;

&lt;p&gt;This is also why the testing story is so clean. Because the contract does not own the privacy primitives, you can test every circuit path in memory with no node, no wallet, and no network. The simulator catches logic bugs. Privacy correctness is guaranteed by the ledger, which is battle-tested and shared across every contract on the chain. You inherit its security for free.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to go next
&lt;/h2&gt;

&lt;p&gt;A few directions worth exploring once you have this working.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deploy to testnet.&lt;/strong&gt; Get test NIGHT tokens from the faucet, deploy the contract through the wallet SDK, and mint real shielded coins. The simulator code path and the deploy code path share the same contract and witnesses, so the only new code is the wallet connection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add a transfer circuit.&lt;/strong&gt; The current contract tracks balances but does not move them between holders. A transfer circuit would debit the sender and credit the recipient in one atomic call, which is the pattern the bounty description calls &lt;code&gt;mint_and_send&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep amounts private.&lt;/strong&gt; Right now mint amounts are public because we &lt;code&gt;disclose()&lt;/code&gt; them. To hide them, drop the on-chain balance tracking entirely and let the wallet's shielded coin layer own the accounting. The contract becomes a pure authorization gate. This is closer to how Midnight's own native tokens work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Compact docs at &lt;a href="https://docs.midnight.network" rel="noopener noreferrer"&gt;docs.midnight.network&lt;/a&gt; go deeper on the ledger primitives. The &lt;code&gt;example-nft-contracts&lt;/code&gt; and &lt;code&gt;example-counter&lt;/code&gt; repos on GitHub are the best reference for real patterns, since the generated TypeScript types they produce are what you actually import.&lt;/p&gt;

&lt;p&gt;Thanks for following along. If you build something with this, tag it &lt;code&gt;#MidnightforDevs&lt;/code&gt; so the team sees it.&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>web3</category>
      <category>blockchain</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Witnesses in Depth: Patterns, Types, and Real Use Cases in Compact</title>
      <dc:creator>Ademola Thompson</dc:creator>
      <pubDate>Wed, 20 May 2026 10:42:49 +0000</pubDate>
      <link>https://dev.to/superherojt/witnesses-in-depth-patterns-types-and-real-use-cases-in-compact-3ec8</link>
      <guid>https://dev.to/superherojt/witnesses-in-depth-patterns-types-and-real-use-cases-in-compact-3ec8</guid>
      <description>&lt;p&gt;ZK circuits require every computation inside them to be expressible as arithmetic over a finite field. This means that ZK circuits can only prove computations that reduce to addition and multiplication.&lt;br&gt;
Division, conditionals on private values, and anything involving external data cannot be expressed that way. You can't read from local storage or fetch a user's private key from their device by using a circuit.&lt;/p&gt;

&lt;p&gt;But any useful DApp needs to do these things. For example, a game needs to divide a player's score by the number of rounds, and an access-controlled contract needs to check the caller's identity against a stored secret.&lt;/p&gt;

&lt;p&gt;Midnight handles this by using witnesses. Witnesses let you run arbitrary TypeScript off-chain, feed the result into a circuit as an input, and then have the circuit verify that result using ZK constraints, without the off-chain computation itself being part of the proof. So you can perform your divisions off-chain and verify their correctness on-chain&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before continuing, you should have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Compact toolchain installed and working&lt;/li&gt;
&lt;li&gt;A basic understanding of Compact syntax — circuits, ledger declarations, and types&lt;/li&gt;
&lt;li&gt;Familiarity with TypeScript, since witness implementations live there&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What is a witness?
&lt;/h2&gt;

&lt;p&gt;A witness is a function you declare in Compact but implement in TypeScript. Your Compact circuit calls it, but it does not see inside of it. It only gets to see the output. Therefore, what gets proven is not what the witness computed, but that its output (return value) satisfies the constraints the circuit put on it.&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You declare a witness in Compact with a name and type signature, and no body&lt;/li&gt;
&lt;li&gt;You implement it in TypeScript in the witness object you pass to the contract&lt;/li&gt;
&lt;li&gt;At runtime, when the circuit calls the witness, the TypeScript implementation executes off-chain, and the return value flows back into the circuit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is what the most basic witness declaration in Compact will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that simple declaration, the compiler knows the function exists and knows its return type. Any circuit in the contract can call it. What it actually does at runtime is determined entirely by the TypeScript implementation.&lt;/p&gt;

&lt;p&gt;This is what a TypeScript implementation of the &lt;code&gt;secretKey()&lt;/code&gt; witness might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// witnesses.ts file&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@midnight-ntwrk/compact-runtime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Ledger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./managed/&amp;lt;myApp&amp;gt;/contract/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// update your path&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the snippet above, the function receives a &lt;code&gt;WitnessContext&lt;/code&gt; object typed over two generic parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Ledger&lt;/code&gt;: the shape of the projected on-chain state&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PrivateState&lt;/code&gt;: the shape of the user's local private data for this contract&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It destructures &lt;code&gt;privateState&lt;/code&gt; from it and returns a tuple containing the updated private state first, then the actual value the circuit receives.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: &lt;code&gt;WitnessContext&lt;/code&gt; exposes three fields — &lt;code&gt;ledger&lt;/code&gt;, &lt;code&gt;privateState&lt;/code&gt;, and &lt;code&gt;contractAddress&lt;/code&gt;. This witness only destructures &lt;code&gt;privateState&lt;/code&gt; because it does not need to read the on-chain state or the contract address to do its job.&lt;/p&gt;

&lt;p&gt;The private state threading matters. Witnesses are the only mechanism for reading and mutating the private state during circuit execution. The private state itself never touches the ledger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Your &lt;code&gt;witnesses.ts&lt;/code&gt; file should live in the same directory as your &lt;code&gt;.compact&lt;/code&gt; file. The &lt;code&gt;managed/&lt;/code&gt; folder you see in the import path is generated by the compiler into that same directory. Do not edit it manually. Your &lt;code&gt;witnesses.ts&lt;/code&gt; sits alongside your &lt;code&gt;.compact&lt;/code&gt; file, and imports compiled types from the &lt;code&gt;managed/&lt;/code&gt; subfolder next to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How witnesses are different from circuit logic in Compact
&lt;/h2&gt;

&lt;p&gt;When you write a circuit, every operation in it becomes part of the ZK proof. The prover has to demonstrate that each step happened correctly, without revealing private inputs. That is what makes circuits trustworthy. It is also what makes them expensive. You cannot do arbitrary computation inside a circuit. You cannot perform operations like reading files, calling APIs, or division.&lt;/p&gt;

&lt;p&gt;Witnesses, on the other hand, have no such constraint. They run before the proof is generated, outside it entirely. Their output enters the circuit as an input, and the circuit's job is to verify that input, not to reproduce the computation that generated it. If a computation is cheap and arbitrary, it belongs in a witness. If it needs to be proven correct, it belongs in a circuit.&lt;/p&gt;

&lt;p&gt;Here is what this looks like in practice:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compact code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;getAge&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;verifyAdult&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getAge&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Must be 18 or older&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;TypeScript implementation&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;getAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;getAge()&lt;/code&gt; runs off-chain and returns the user's age from private state. The circuit never proves how that value was obtained. It only proves that the value satisfies the condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Must be 18 or older&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the snippet above, &lt;code&gt;disclose(age &amp;gt;= 18)&lt;/code&gt; wraps the comparison rather than the age because&lt;br&gt;
&lt;code&gt;disclose()&lt;/code&gt; controls what leaves the private domain. Wrapping &lt;code&gt;age&lt;/code&gt; directly would put the raw number on-chain. However, wrapping the comparison puts only the boolean result on-chain. The circuit can prove the condition is true without the actual age ever leaving the private state.&lt;/p&gt;

&lt;p&gt;One consequence of using witnesses is that the compiler treats every value that comes from a witness as potentially private. Any value derived from a witness return — through arithmetic, type conversion, or logic — inherits the private status. If such data reaches a public ledger field or an exported circuit's return value without an explicit &lt;code&gt;disclose()&lt;/code&gt; call, the compiler will refuse to build.&lt;/p&gt;
&lt;h2&gt;
  
  
  Common ways to use witnesses in Compact
&lt;/h2&gt;

&lt;p&gt;You will come across multiple scenarios where you will need to use witnesses in Compact. This section lists some of the popular ways to use witnesses when writing code for the Midnight chain.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Private identities such as secret keys
&lt;/h3&gt;

&lt;p&gt;The most common witness in Midnight contracts retrieves a user's private key from local storage. The secret key never touches the public ledger. Only its hash is public.&lt;/p&gt;

&lt;p&gt;Here's an example Compact contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;CompactStandardLibrary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//witness declaration&lt;/span&gt;

&lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myapp:auth:pk:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;_sk&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the snippet above, the constructor runs once at deployment. It calls the &lt;code&gt;secretKey()&lt;/code&gt; witness to retrieve the deployer's private key off-chain, derives a public key from it using &lt;code&gt;persistentHash&lt;/code&gt;, and stores only the hash in the &lt;code&gt;owner&lt;/code&gt; ledger field.&lt;/p&gt;

&lt;p&gt;After deployment, the secret key is gone from the execution context. What remains on-chain is only its cryptographic commitment.&lt;/p&gt;

&lt;p&gt;In TypeScript, you will implement your witness like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@midnight-ntwrk/compact-runtime&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Ledger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./managed/&amp;lt;myApp&amp;gt;/contract/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// update your path&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// private state is unchanged&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// this is what the circuit receives&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The TypeScript implementation simply pulls the secret key out of private state and returns it. What the circuit does with it — hashing, storing — is none of the witness's concern.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Witness-verified division
&lt;/h3&gt;

&lt;p&gt;Circuits have no native division operator. The field arithmetic that underpins ZK proofs does not support it directly. If you need to divide two numbers inside a circuit, you must use a witness to compute the result off-chain and then prove that the result is correct inside the circuit.&lt;/p&gt;

&lt;p&gt;This is an example from a real &lt;a href="https://github.com/effectstream/dust-to-dust" rel="noopener noreferrer"&gt;Midnight game contract&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;_divMod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;div&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;_divMod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;quotient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;quotient&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid divMod witness impl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;_divMod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;quotient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;quotient&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid divMod witness impl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the circuits do not blindly trust the witness. They verify the fundamental identity of division: &lt;code&gt;x == y * quotient + remainder&lt;/code&gt;, and check that the remainder is strictly less than the divisor. If an attacker provides a malicious witness implementation that returns incorrect values, the assert fails, and the transaction reverts.&lt;/p&gt;

&lt;p&gt;In TypeScript, this is what the implementation of the &lt;code&gt;_divMod&lt;/code&gt; witness looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;_divMod&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Game2PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Game2PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;xn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;yn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;xn&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;yn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;quotient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xn&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;yn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`dyn witness _divMod(&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;) = [&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;]`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;BigInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;)]];&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Overall, TypeScript performs the cheap arithmetic, and the circuit proves that the result is correct.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Managing private state with witnesses
&lt;/h3&gt;

&lt;p&gt;Some witnesses do not return a single computed value. Instead, they manage a local state machine that mirrors the contract's on-chain state. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;CompactStandardLibrary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;LocalState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localAdvanceState&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localRecordVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vote&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localVoteCast&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Maybe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;LocalState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;committed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;revealed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;committedVotes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MerkleTree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;committed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;commitmentNullifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nullifier:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;commitWithSk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ballot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;ballot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;voteCommit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ballot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;LocalState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Already committed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;localRecordVote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ballot&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nullifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;commitmentNullifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;committed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nullifier&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Already used this identity&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;commitWithSk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ballot&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;committedVotes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cm&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;committed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nullifier&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nf"&gt;localAdvanceState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;localAdvanceState()&lt;/code&gt; and &lt;code&gt;localRecordVote()&lt;/code&gt; return &lt;code&gt;[]&lt;/code&gt;. They have no return value to the circuit. Their entire job is to update private state as a side effect. The Compact side declares that these functions exist; the TypeScript side is where the actual state transition happens.&lt;/p&gt;

&lt;p&gt;The TypeScript implementation will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;initial&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;committed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;revealed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;localVote&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;localAdvanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;initial&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;committed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;committed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;revealed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;localState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]];&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="na"&gt;localRecordVote&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;vote&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;localVote&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;vote&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]],&lt;/span&gt;

  &lt;span class="na"&gt;localVoteCast&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;is_some&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localVote&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;is_some&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localVote&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;is_some&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each witness returns the full updated private state as its first element. This is how Midnight threads private state through the circuit execution functionally. If the circuit fails at any point, none of the private state mutations are committed either.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Merkle path witness
&lt;/h3&gt;

&lt;p&gt;When a contract needs to verify that a value belongs to a set without revealing which member, it uses a Merkle tree. The path computation is too expensive and arbitrary to happen inside a circuit. A witness does the traversal off-chain and hands the path to the circuit, which verifies the root.&lt;/p&gt;

&lt;p&gt;Here's an example Compact contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;CompactStandardLibrary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localPathOfPk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Maybe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MerkleTreePath&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;eligibleVoters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MerkleTree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;voterPublicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;election:voter:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;proveEligibility&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;voterPublicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localPathOfPk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;is_some&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;eligibleVoters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;checkRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;merkleTreePathRoot&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nx"&gt;pk&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;leaf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not an eligible voter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The circuit receives a path from the witness and checks for three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;that a path exists&lt;/li&gt;
&lt;li&gt;that the path root matches the on-chain tree root&lt;/li&gt;
&lt;li&gt;that the leaf is the caller's public key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If any check fails, the assert reverts everything. The voter's identity is never disclosed. Instead, the proof only establishes that a valid path exists.&lt;/p&gt;

&lt;p&gt;Here is the TypeScript implementation of the witnesses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;localVoterTree&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// local Merkle tree structure&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;localSecretKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;localPathOfPk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;is_some&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Traverse the local Merkle tree to find the path for this public key. Implement your version computeMerklePath&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computeMerklePath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localVoterTree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;is_some&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;is_some&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript holds a local copy of the Merkle tree and computes the path. The circuit validates it. The tree traversal never needs to be expressed in field arithmetic.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Feeding external data into circuits
&lt;/h3&gt;

&lt;p&gt;Witnesses can pull in any data that exists outside the contract (e.g., game state, board positions, API responses). The key requirement is that the circuit must constrain and validate whatever the witness provides. A witness that returns unvalidated external data and a circuit that trusts it blindly is a security hole.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/ErickRomeroDev/midnight-examples-0.1.16_v2/blob/main/examples/naval-battle-game/contract/src/naval-battle-game.compact" rel="noopener noreferrer"&gt;naval battle game by ErickRomeroDev&lt;/a&gt; demonstrates this pattern cleanly. Before the game starts, each player commits their board setup privately. The witness receives the full board, stores it locally, and returns only a hash to the circuit. The circuit writes that hash to the ledger, and the actual board positions never go on-chain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;playerOnePk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;playerOneCommit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;playerOneHasCommitted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;local_sk&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Receives the full board setup, stores it in private state,&lt;/span&gt;
&lt;span class="c1"&gt;// and returns only a hash to the circuit&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;set_local_gameplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;commitGrid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;player&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Verify the caller is who they claim to be&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;playerOnePk&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;public_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;local_sk&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PlayerOne confirmation failed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// The witness stores the board locally and hands back only the hash&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set_local_gameplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Only the hash goes on-chain — the board positions stay private&lt;/span&gt;
  &lt;span class="nx"&gt;playerOneCommit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;playerOneHasCommitted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;vectorHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;persistent_hash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later, when a player makes a move, the circuit uses the &lt;code&gt;vectorHash&lt;/code&gt; circuit to verify that the locally stored board matches the committed hash. This will spot any attempt to tamper with the board after the game has started.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Verify the player has not tampered with their board since committing&lt;/span&gt;
&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vectorHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;local_gameplay&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;playerOneCommit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Player one has tampered with the grid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the TypeScript implementation of the two witnesses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;witnesses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;local_sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NavalBattlePrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;NavalBattlePrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;set_local_gameplay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WitnessContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ledger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NavalBattlePrivateState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NavalBattlePrivateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updatedGameplay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;localGameplay&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Store the full board setup locally, keyed by contract address&lt;/span&gt;
    &lt;span class="nx"&gt;updatedGameplay&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;privateState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;localGameplay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;updatedGameplay&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;

      &lt;span class="c1"&gt;// Return only the hash to the circuit — the board itself stays private&lt;/span&gt;
      &lt;span class="nx"&gt;pureCircuits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;vectorHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;local_sk&lt;/code&gt; returns the player's secret key from the private state. This pattern was explained in an earlier section.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set_local_gameplay&lt;/code&gt; is where the external data ingestion happens. It receives the full board setup as a &lt;code&gt;bigint[]&lt;/code&gt;, stores it in the local gameplay map keyed by &lt;code&gt;contractAddress&lt;/code&gt;, and returns only its hash via &lt;code&gt;pureCircuits.vectorHash&lt;/code&gt;. Using &lt;code&gt;contractAddress&lt;/code&gt; as the key means a player can&lt;br&gt;
participate in multiple game instances simultaneously without their board from one game bleeding into another.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: &lt;code&gt;pureCircuits&lt;/code&gt; is a generated object from your compiled contract that exposes circuits with no ledger side effects so they can be called directly from TypeScript without submitting a transaction. You can learn more about how this works on &lt;a href="https://docs.midnight.network/guides/compact-javascript-runtime#pure-circuits-implementation" rel="noopener noreferrer"&gt;Midnight's Documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Witness-based access control
&lt;/h2&gt;

&lt;p&gt;Access control in Midnight does not work the way it does in Ethereum. There is no native version of &lt;code&gt;msg.sender&lt;/code&gt;, and circuits cannot observe who submitted a transaction.&lt;/p&gt;

&lt;p&gt;In Compact, circuits can enforce access control by asking the caller to prove knowledge of a secret, and verify that proof against a commitment stored on-chain. It follows this pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store a hash of the secret at setup time&lt;/li&gt;
&lt;li&gt;Re-derive the same hash at call time&lt;/li&gt;
&lt;li&gt;Assert that the above two match each other. If they do not, the circuit reverts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example of what it looks like to enforce access control in Compact:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;CompactStandardLibrary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;authority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;sensitiveValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myapp:auth:pk:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;round&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;_sk&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Runs once at deployment. Stores a hash of the deployer's secret key as the authority.&lt;/span&gt;
&lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;authority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;sensitiveValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Only the holder of the original secret key can call this.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;authority&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not authorized&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;sensitiveValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;authority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;publicKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Anyone can read. Only the authority can write.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sensitiveValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the snippet above, the constructor runs once at deployment. It calls the &lt;code&gt;secretKey()&lt;/code&gt; witness&lt;br&gt;
to retrieve the deployer's private key, derives a public key from it, and stores only the hash as &lt;code&gt;authority&lt;/code&gt;. Every subsequent call to &lt;code&gt;update()&lt;/code&gt; re-derives that same hash from the witness at call time and compares it against &lt;code&gt;authority&lt;/code&gt;. If the caller does not know the original secret key, the &lt;code&gt;assert&lt;/code&gt; fails and the transaction reverts.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;read()&lt;/code&gt; has no access control. Any caller can execute it. The distinction between these two circuits is the access control model: one requires proof of identity, the other does not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: You will notice the use of a round counter. After &lt;code&gt;update()&lt;/code&gt; succeeds, the function increments &lt;code&gt;round&lt;/code&gt; and re-derives &lt;code&gt;authority&lt;/code&gt; with the new counter value. This means the stored authority hash changes after every authorized action, so an observer watching the chain cannot link multiple &lt;code&gt;update()&lt;/code&gt; calls to the same key.&lt;/p&gt;
&lt;h3&gt;
  
  
  Role-based access control with witnesses
&lt;/h3&gt;

&lt;p&gt;So far, you have seen how to handle access control with a single authority. But what happens when your contract requires multiple roles, such as minter or admin? The solution is simply to extend your contract to store a separate authority hash for each role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;pragma&lt;/span&gt; &lt;span class="nx"&gt;language_version&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;CompactStandardLibrary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;adminAuthority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;ledger&lt;/span&gt; &lt;span class="nx"&gt;minterAuthority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;roleKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myapp:role:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;_sk&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;mint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;roleKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;minter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;minterAuthority&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not a minter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// mint logic&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;pause&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;roleKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;adminAuthority&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not an admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// pause logic&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each role uses a different domain string in the hash, so the same secret key produces a different commitment per role. Therefore, an observer cannot correlate admin actions with minter actions even if the same person holds both roles.&lt;/p&gt;

&lt;h2&gt;
  
  
  How official Midnight DApps use witnesses
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Election Contract
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://docs.midnight.network/examples/contracts/election" rel="noopener noreferrer"&gt;example election contract&lt;/a&gt; uses two witnesses to implement a private commit-reveal voting scheme:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localSk&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;localGetVote&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;VoteChoice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;commitVote&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;votingState&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VotingState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OPEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Voting has not opened yet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localSk&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pubKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getDappPubKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registeredVoters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You are not registered to vote.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hashedVoteMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Attempt to double vote&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_currentVote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localGetVote&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_currentVote&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VoteChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BAD&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;_currentVote&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VoteChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;WORSE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please provide a valid vote&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;commitWithSk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_currentVote&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;hashedVoteMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;totalVoteCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// check privateState for original vote, if it's changed, then error&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;revealVote&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;votingState&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VotingState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CLOSED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Voting is still open&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localSk&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pubKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getDappPubKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// we want to check that they have already voted..&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashedVoteMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You have not voted yet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registeredVoters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;member&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You are not a registered voter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;vote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localGetVote&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vote&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VoteChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BAD&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;vote&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VoteChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;WORSE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please supply a valid vote&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// here is the money&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hashedVote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;commitWithSk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vote&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashedVoteMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;hashedVote&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Attempt to change the vote!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vote&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VoteChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BAD&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;candidate0VoteCounter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vote&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;VoteChoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;WORSE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;candidate1VoteCounter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// hash a random "public key" that is only traceable in this dapp&lt;/span&gt;
&lt;span class="nx"&gt;circuit&lt;/span&gt; &lt;span class="nf"&gt;getDappPubKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;disclose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;persistentHash&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;election:pk:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;localSk&lt;/code&gt; handles identity. Every circuit that needs to verify the caller derives their public key from it. &lt;code&gt;localGetVote&lt;/code&gt; retrieves the voter's choice from the private state.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;commitVote()&lt;/code&gt;, the circuit retrieves the vote from the witness, hashes it together with the caller's secret key, and stores only that hash on-chain without revealing the vote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;_currentVote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;localGetVote&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;commitWithSk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_currentVote&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;hashedVoteMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;revealVote()&lt;/code&gt;, the circuit retrieves the vote again and re-derives the same hash. If the re-derived hash matches what was committed, the vote is counted. If the voter tries to change their vote between commit and reveal, the hashes will not match, and the assert fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hashedVote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;commitWithSk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vote&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Field&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_sk&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;hashedVoteMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;hashedVote&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Attempt to change the vote!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can browse the full code based on &lt;a href="https://docs.midnight.network/examples/contracts/election" rel="noopener noreferrer"&gt;Midnight's official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Naval battle game
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://github.com/ErickRomeroDev/midnight-examples-0.1.16_v2/blob/main/examples/naval-battle-game/contract/src/naval-battle-game.compact" rel="noopener noreferrer"&gt;naval battle contract&lt;/a&gt; uses three witnesses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;local_sk&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;local_gameplay&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;witness&lt;/span&gt; &lt;span class="nf"&gt;set_local_gameplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Uint&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Bytes&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;local_sk&lt;/code&gt; handles player identity. &lt;code&gt;set_local_gameplay&lt;/code&gt; receives the full board setup, stores it locally, and returns only a hash to the circuit. &lt;code&gt;local_gameplay&lt;/code&gt; retrieves the stored board during gameplay.&lt;/p&gt;

&lt;p&gt;The three witnesses work together across two key moments in the game. At commit time, &lt;code&gt;set_local_gameplay&lt;/code&gt; stores the board, and the circuit writes only the hash on-chain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set_local_gameplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;playerSetup&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;playerOneCommit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At move time, &lt;code&gt;local_gameplay&lt;/code&gt; retrieves the stored board, and the circuit verifies it has not been tampered with since the commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vectorHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;local_gameplay&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;playerOneCommit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Player one has tampered with the grid&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The board positions never go on-chain. What the chain holds is a commitment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security Rules for Witnesses
&lt;/h2&gt;

&lt;p&gt;When using witnesses in Compact (and you will use them), you should keep the following in mind so you don't compromise on your privacy:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Verify, never trust.&lt;/strong&gt; Every value a witness returns should be treated as adversarial input. Write circuit assertions that prove the output is correct based on public inputs and mathematical identities. You should never assume that the witness is honest or correct.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Witnesses run outside the ZK proof.&lt;/strong&gt; The user's own DApp provides the witness implementation. If a user modifies their frontend to return malicious values from a witness, the circuit assertions are the only thing standing between that and an invalid state transition. Make those assertions tight.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Never use &lt;code&gt;ownPublicKey()&lt;/code&gt; for access control.&lt;/strong&gt; It is a witness. It can return anything. Use the secret key → hash pattern instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Keep &lt;code&gt;disclose()&lt;/code&gt; as late as possible.&lt;/strong&gt; Once you wrap a value in &lt;code&gt;disclose()&lt;/code&gt;, the compiler stops tracking it for accidental leakage. Disclose immediately before the value enters a ledger field or a circuit return, and not the moment it comes out of the witness.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Witnesses cannot modify the public state.&lt;/strong&gt; They can only update private state and return values to circuits. If you need to change a ledger field, that has to happen in circuit logic, using what the witness returned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Witnesses are where Midnight's privacy architecture does most of its practical work. They handle everything the ZK proof system cannot, such as arbitrary computation, private data access, external data ingestion, and hand the results to circuits that constrain and verify them.&lt;/p&gt;

&lt;p&gt;The patterns in this article cover the cases you will encounter most often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Secret key witnesses&lt;/strong&gt; establish private identity without putting credentials on-chain&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Witness-verified division&lt;/strong&gt; handles arithmetic that the circuit cannot do natively, with circuit assertions proving correctness&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private state management witnesses&lt;/strong&gt; implement local state machines that mirror the on-chain state&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Merkle path witnesses&lt;/strong&gt; compute membership proofs off-chain and hand them to circuits for verification&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;External data witnesses&lt;/strong&gt; ingest arbitrary off-chain data that circuits then constrain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In every pattern, the structure is the same: the witness computes, the circuit verifies. That boundary is not a limitation. It is what makes the whole system trustworthy.&lt;/p&gt;

</description>
      <category>midnight</category>
      <category>zkproofs</category>
      <category>privacy</category>
      <category>witnesses</category>
    </item>
  </channel>
</rss>
