DEV Community

BossChaos
BossChaos

Posted on

Building Privacy-First Apps with Midnight Easy SDK

๐ŸŒ™ Midnight Easy SDK: Privacy-First Development Made Simple

This is a submission for the Midnight Network "Privacy First" Challenge

๐Ÿ“Œ Project Overview

I built @midnight/easy-sdk to lower the barrier for developers who want to add privacy features to their apps but don't want to become zero-knowledge cryptography experts.

The SDK wraps Midnight's cryptographic primitives in a clean TypeScript API. You get seal, verify, and decrypt operations, plus React hooks that handle loading states and error recovery out of the box.

๐Ÿ› ๏ธ Tech Stack

Layer Technology
Language TypeScript
UI Framework React + Hooks
Cryptography Midnight ZK circuits (Noir)
Testing Vitest
License Apache 2.0

๐Ÿ’ก Core Value

  • No ZK expertise required โ€” Seal data, verify proofs, and decrypt results with three function calls
  • React-first design โ€” useSeal, useVerify, useDecrypt hooks handle all the async complexity
  • Selective disclosure โ€” Reveal only the metadata you choose, keeping the rest private
  • Drop-in installation โ€” npm install @midnight/easy-sdk and you're ready to go

๐Ÿš€ Quick Start

npm install @midnight/easy-sdk
Enter fullscreen mode Exit fullscreen mode
import { MidnightEasy } from '@midnight/easy-sdk';

const midnight = new MidnightEasy({ network: 'testnet' });
await midnight.init();
Enter fullscreen mode Exit fullscreen mode

Core API

1. Seal Data

seal() creates a confidential commitment from your data. Under the hood, it generates a zero-knowledge note that hides the input while allowing later selective disclosure.

const sealed = await midnight.seal({
  value: 1_000_000n,
  asset: 'USDC',
  owner: 'did:mad:test...',
  metadata: { purpose: 'escrow' }
});

console.log(sealed.commitment);   // '0xabc123...'
console.log(sealed.nullifier);    // '0xdef456...'
Enter fullscreen mode Exit fullscreen mode

The commitment is posted to the Midnight ledger. Observers see a hash โ€” not the underlying value.

2. Verify a Proof

verify() checks a zero-knowledge proof against a commitment without revealing the sealed data.

const result = await midnight.verify({
  commitment: sealed.commitment,
  conditions: { minValue: 500_000n, allowedAssets: ['USDC'] },
  proof: userProvidedProof,
});

console.log(result.valid);    // true
console.log(result.metadata); // { purpose: 'escrow' }
Enter fullscreen mode Exit fullscreen mode

3. Decrypt Results

decrypt() reveals the plaintext to authorized recipients only, using Midnight's threshold decryption.

const plaintext = await midnight.decrypt({
  ciphertext: transfer.encryptedResult,
  recipientKey: recipient.publicKey,
  threshold: 2,
});
Enter fullscreen mode Exit fullscreen mode

React Hooks

For UI-driven privacy flows, the SDK provides React hooks:

import { MidnightProvider, useSeal } from '@midnight/easy-sdk/react';

function App() {
  return (
    <MidnightProvider network="testnet" autoConnect>
      <EscrowPanel />
    </MidnightProvider>
  );
}

function EscrowPanel() {
  const { seal, isLoading, error } = useSeal();
  const handleSeal = async () => {
    const result = await seal({ value: 500_000n, asset: 'USDC', owner: myDID });
    console.log('Committed:', result.commitment);
  };
  return <button onClick={handleSeal}>Seal</button>;
}
Enter fullscreen mode Exit fullscreen mode

Real-World Example: Private Escrow

// Alice seals her deposit
const deposit = await midnight.seal({
  value: 500_000n, asset: 'USDC', owner: aliceDID,
  metadata: { role: 'depositor' }
});

// Bob seals his acceptance
const acceptance = await midnight.seal({
  value: 0n, asset: 'USDC', owner: bobDID,
  metadata: { role: 'acceptor' }
});

// Contract verifies both
const valid = await midnight.verify({
  commitment: deposit.commitment,
  conditions: { allowedAssets: ['USDC'] }
});
Enter fullscreen mode Exit fullscreen mode

Contributing

Open source under Apache 2.0. Contributions welcome:

  • ๐Ÿ“– Improve documentation
  • ๐Ÿงช Expand test coverage
  • ๐Ÿ› ๏ธ Add new primitives and hooks

GitHub Repository


Built for the Midnight Network "Privacy First" Challenge โ€” Enhance the Ecosystem & Best Tutorial tracks.

Top comments (0)