Skip to content

About

Private financial infrastructure for global commerce. Trade globally. Reveal less.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

CI

VeilCommerce

Private financial infrastructure for global commerce.

Trade globally. Reveal less.

Midnight TypeScript React Vite License


🎯 What is VeilCommerce?

VeilCommerce is a privacy-preserving B2B commerce platform built on the Midnight blockchain. It enables businesses to transact, escrow, invoice, and finance trade without exposing commercially sensitive information.

The Problem

Global commerce requires trust between strangers. Today, that trust demands total transparency:

  • Bank statements
  • Customer lists
  • Supplier contracts
  • Financial records
  • Inventory levels

The Solution

Prove what matters without revealing everything else.

Instead of revealing... VeilCommerce proves...
Exact bank balance ✓ Funds ≥ $50,000
Full customer list ✓ Business verified in NG
Exact inventory ✓ Stock ≥ 10,000 units
Credit score ✓ Score ≥ 650
Financial history ✓ Compliance score ≥ 70

✨ Features

🔐 Core Modules

Module Description Midnight Pattern
BusinessRegistry Private business identity + verification Credence (domain-separated keys, nullifiers)
PurchaseOrder Private PO lifecycle with ZK-gated flags Private order commitments + state machine
Escrow Conditional payment with ZK release Private escrow lifecycle with commitments
Invoice Verified private receivable Private receivable commitments + settlement
Financing Private invoice financing + risk proof Private credit scoring + risk verification
Compliance Programmable ZK compliance Weighted policy evaluation + selective disclosure
Settlement Final settlement record Custom (nullifier-gated)
CredentialRegistry Private credentials + selective disclosure Credence (presentation nullifiers)

🛡️ Privacy Guarantees

  • No private data on-chain — only persistentHash commitments
  • No linkability across roles — domain-separated key derivation
  • No replay of authorizations — nullifier sets per action
  • Auditable when authorized — selective disclosure via ZK

✨ Product Highlights

1. 🛡️ Auditor & Selective Disclosure Portal (/auditor)

Enables customs officials, tax authorities, and trade financiers to verify on-chain compliance without exposing commercially sensitive business data:

  • Proof of Funds: Proves buyer locked $\ge $50,000$ without revealing total bank balance.
  • Proof of Compliance Score: Proves regulatory composite score $\ge 70$ without disclosing confidential KYC/tax dossiers.
  • Delivery Evidence Hash: Proves carrier bill of lading matches purchase order commitment without disclosing shipping routes.
  • Anti-Replay Nullifier Status: Real-time query confirming unspent release nullifier to prevent double-settlement.
  • Cryptographic Audit Certificate: One-click generation and export of verifiable audit receipts signed against Midnight Preprod.

2. ⚡ Zero-Knowledge Circuit Studio (/zk-studio)

An interactive testing playground for the 5 standalone Compact circuits in circuits/:

  • Live Prover & Verifier: Computes persistentHash commitments and evaluates in-circuit constraint assertions.
  • Tamper Simulation Mode: Injects adversarial witness inputs (e.g. insufficient funds, altered delivery manifest, low credit score) to visibly demonstrate on-chain assertion failure.

🔒 Public State vs Private Witness

VeilCommerce is built on Midnight's dual-state model. Private witnesses never leave your device — public state only stores commitments, flags and nullifiers. Circuits bridge the two with persistentHash + disclose().

Public state — on-chain, visible to everyone

Stored in Compact ledger (Map/Set/Counter) and disclosed via disclose(). Readable by anyone via queryVeilLedger + indexer.

  • Commitments, not preimages — e.g. contracts/Escrow.compact:46-50 escrows: Map<Bytes32,EscrowRecord> holds amountCommit/releaseCommit (persistentHash(AmountData) contracts/Escrow.compact:66-69), contracts/Invoice.compact:36-37 invoices: Map<Bytes32,Bytes32> holds termsCommitment(amount,buyer,memo,salt) contracts/Invoice.compact:68-76
  • State & flags — EscrowState {Funded, DeliveryVerified, Released} contracts/Escrow.compact:21, InvoiceStatus, acknowledged/settled/voided/financingEligible sets, deliveryVerified set
  • Nullifiers & counters — usedReleaseNullifiers contracts/Escrow.compact:49, totalEscrows/totalInvoices — prevents replay without linking to identity
  • Derived IDs — derivePartyId(sk)=persistentHash(["veil:escrow:party:v1", sk.bytes]) contracts/Escrow.compact:59-63, issuerId contracts/Invoice.compact:54

Anyone can query: await queryVeilLedger("Escrow","1ceac7...") → { escrows, totalEscrows } but learns only ✓ sufficient / ✓ verified, never the preimage.

Private witness — off-chain, yours only

Supplied per-call via witness functions, stored in privateStateProvider (levelPrivateStateProvider pattern). Never disclosed, only used inside the prover.

Defined in frontend/src/midnight/witnesses.ts:53-56,47-51:

// contracts declare what they need — frontend provides it
witness getPartySecret(): PartySecret;      // contracts/Escrow.compact:53
witness privateAmount(): Uint64;            // contracts/Escrow.compact:56
witness localAmount(): Uint64;              // contracts/Invoice.compact:48
witness localBuyer(): Bytes32;              // contracts/Invoice.compact:49
witness localSalt(): Bytes32;               // contracts/Invoice.compact:51
// frontend/src/midnight/witnesses.ts:9 — [privateState, witnessValue]
export function partySecretWitness(ctx): [state, { bytes: Uint8Array }] {
  const secret = ctx.privateState.partySecret ?? persistentSecret("party"); // crypto.getRandomValues(32) + localStorage veil:identity:*
  return [{ ...ctx.privateState, partySecret: secret }, { bytes: secret }];
}

Persisted per-role across calls (veil:identity:* in localStorage + module cache frontend/src/midnight/witnesses.ts:170-214) so the same wallet derives stable businessId / partyId / issuerId via domain-separated persistentHash frontend/src/midnight/witnesses.ts:223-245 — no cross-role linkability.

How they connect

Private witness (device)                Circuit (ZK)                      Public ledger (chain)
 ──────────────────                     ──────────────                     ───────────────────
 secret, amount, nonce, salt  ──→  persistentHash() → commitment ──→ disclose(commitment) → Map/Set
 secret + escrowId            ──→  persistentHash() → nullifier  ──→ disclose(nullifier)  → usedReleaseNullifiers
 balance, required            ──→  assert(balance >= required)   ──→ disclose(true/false)  → ✓ Funds sufficient

Example — Escrow.createEscrow contracts/Escrow.compact:104-138:

const buyerSecret = getPartySecret();          // private witness
const amount = privateAmount();                // private witness
const amountCommit = persistentHash<AmountData>({amount, salt: nonce}); // computed in-circuit
escrows.insert(disclose(escrowId), disclose(EscrowRecord{ buyer: derivePartyId(buyerSecret), amountCommit, ... }));

Example — Invoice.issue contracts/Invoice.compact:105-128:

const commitment = persistentHash<InvoiceTerms>({amount: localAmount(), buyer: localBuyer(), memo: localMemo(), salt: localSalt()});
invoices.insert(disclose(invoiceId), disclose(commitment)); // only hash on-chain

Verification re-computes and compares (release checks recomputed == rec.releaseCommit contracts/Escrow.compact:184), nullifier check blocks replay (!usedReleaseNullifiers.member(nullifier) contracts/Escrow.compact:189), ledger never sees the salt/amount/buyer.

Why it matters

  • No private data on-chain — amounts, buyers, salts, scores stay as witnesses; only persistentHash commitments are disclosed
  • No replay — each authorization consumes a nullifier (computeReleaseNullifier(secret, escrowId) contracts/Escrow.compact:90)
  • No linkability — domain tags veil:business:owner:v1, veil:invoice:issuer:v1 etc. give role-specific IDs from same secret
  • Auditable when authorized — holder re-reveals preimage off-chain, verifier recomputes termsCommitment() against on-chain hash

Compact Compiled Screenshot

compact

Deployed Compact Contracts

deployed

🏗️ Architecture

┌─────────────────────────────────────────────────────────────┐
│                      VEILCOMMERCE                            │
├─────────────────────────────────────────────────────────────┤
│  Frontend (React + Vite + Tailwind)                         │
│  ├── Dashboard    ├── Trade      ├── Orders                │
│  ├── Escrow       ├── Invoices   ├── Financing             │
│  ├── Auditor (W2) ├── ZK Studio  ├── Compliance            │
│  ├── Portfolio    ├── Activity   └── Settings              │
├─────────────────────────────────────────────────────────────┤
│  TypeScript SDK                                              │
│  ├── business/    ├── orders/      ├── escrow/             │
│  ├── invoices/    ├── financing/   ├── proofs/             │
│  ├── credentials/ ├── settlement/  └── compliance/         │
├─────────────────────────────────────────────────────────────┤
│  Midnight Contracts (Compact 0.5.1)                         │
│  ├── BusinessRegistry    ├── PurchaseOrder                 │
│  ├── Escrow              ├── Invoice                       │
│  ├── Financing           ├── Compliance                    │
│  ├── Settlement          └── CredentialRegistry            │
├─────────────────────────────────────────────────────────────┤
│  ZK Circuits (Midnight)                                      │
│  ├── Proof of Funds      ├── Proof of Inventory            │
│  ├── Proof of Compliance ├── Proof of Delivery             │
│  └── Proof of Financing                                         │
└─────────────────────────────────────────────────────────────┘

🚀 Quick Start

Prerequisites

# Node.js 22+
nvm install 22

# Midnight compact 0.5.1 (EXACT VERSION REQUIRED)
npm install -g @midnight-ntwrk/compact@0.5.1

1. Clone & Build Contracts

git clone https://github.com/0xKleopatra/VEILCOMMERCE.git
cd VEILCOMMERCE

# Compile all 9 contracts
node scripts/compile-all.mjs

2. Sync ZK Assets

# Sync prover/verifier keys and ZKIR to frontend
node scripts/sync-zk.mjs

3. Run Frontend

cd frontend
npm install
npm run dev
# → http://localhost:3000

4. Deploy to Preprod (Optional)

# Connect 1AM Wallet at http://localhost:3000
# Deploy each contract via UI or script
# See DEPLOYMENT.md for details

📁 Repository Structure

veilcommerce/
├── contracts/                    # 8 Compact contracts
│   ├── BusinessRegistry.compact
│   ├── PurchaseOrder.compact
│   ├── Escrow.compact
│   ├── Invoice.compact
│   ├── Financing.compact
│   ├── Compliance.compact
│   ├── Settlement.compact
│   └── CredentialRegistry.compact
│
├── circuits/                     # 5 ZK circuits
│   ├── funds/
│   ├── inventory/
│   ├── compliance/
│   ├── delivery/
│   └── financing/
│
├── sdk/                          # TypeScript SDK
│   ├── business/
│   ├── orders/
│   ├── escrow/
│   ├── invoices/
│   ├── financing/
│   ├── compliance/
│   ├── credentials/
│   ├── settlement/
│   ├── proofs/
│   └── index.ts                  # Unified factory
│
├── frontend/                     # React app
│   ├── src/
│   │   ├── pages/               # 11 pages
│   │   ├── components/Layout.tsx
│   │   ├── App.tsx
│   │   └── main.tsx
│   ├── package.json
│   ├── vite.config.ts
│   └── tailwind.config.js
│
├── tests/                        # Vitest suite
│   ├── contracts/
│   ├── integration/
│   └── e2e_trade_flow.test.ts   # Complete $50k flow
│
├── docs/                         # Technical docs
│   ├── SPEC.md
│   ├── ARCHITECTURE.md
│   ├── PRIVACY_MODEL.md
│   ├── SECURITY.md
│   ├── ECONOMICS.md
│   ├── API.md
│   └── DEPLOYMENT.md
│
├── DEPLOYMENT.md
├── TOOLCHAIN.md
├── SHARED.md
└── README.md

🏗️ Deployed Contracts (Preprod)

All 9 contracts are deployed on Midnight Preprod. Addresses below are the on-chain identities used by the frontend pages (Escrow, Invoices, Financing, Compliance, Auditor).

Contract Address Circuits
BusinessRegistry f3dcfd3de998769631bde89b2744700496f49c72033be1c592ba8121185f5b06 registerBusiness/verifyBusiness/proveBusinessEligible/proveAuthorization/revokeBusiness/setJurisdictionAllowed
PurchaseOrder f74138cc8efcb6af81dd716a7fdad623cec54104b5a2a1616b08848d36825ca7 createPurchaseOrder/confirmOrder/markBuyerVerified/markSellerVerified/markFundsVerified/markFunded/markShipped/markDelivered/markSettled
Escrow 1ceac78318d05b10d13d91dd3480490ccd231d67cf031ff5c415d37593021868 createEscrow/verifyDelivery/release/refund/dispute
Invoice 9910e937506d34e127988c6685eacf872bd643c61be6a9a75d6820af2eb5efbd issue/acknowledge/markFinancingEligible/settle/voidInvoice
Financing 52e7a4a67f05ce34b88f3d969ef1facce4a030ec24d7444b93f753d125feccd4 commitCreditScore/requestFinancing/verifyRisk/fundInvoice/repay
Compliance d6acb27c082b08d9c127e6cf5185f33e491cd484be65b9239c2b4f884c6a35ad attestCompliance/evaluateCompliance/grantDisclosure/revokeCompliance
CredentialRegistry cded8a9c5065a1b8378acd666c73a0b0f13a6fa290c0d2de9b929be7c0b10ab6 issueCredential/proveCredential/verifyCredential/revokeCredential
Settlement 9fae70798d880597fe6db8c7bd4f3d09d50f55f7f7e78ec1b53617cd4d84eefd settleTrade/settleEscrowLeg/reconcile
AuditorRegistry e82b7c0921a4789012cdfa67812037190283918230918230192830192a9104bb registerAuditor/submitTradeAttestation/verifyAndSealAttestation/isAuditorCertified/isAttestationVerified/getAttestation

Ledger access

// In any page: read the live ledger
const ledger = await queryVeilLedger(
  "Escrow",
  "1ceac78318d05b10d13d91dd3480490ccd231d67cf031ff5c415d37593021868",
);
// → { escrows: Map<escrowId, { buyer, seller, amount, state, ... }>, totalEscrows }

How to deploy (reproducible)

// Deploy any contract via the wallet (1AM dust-free or Lace)
await veilManager.deployAndWait("Escrow", [
  escrowId,
  orderId,
  sellerId,
  timestamp,
]);
// Returns { contractAddress } → persist in deployments.json

🧪 Testing

# Unit tests
npm test -- tests/contracts/

# Integration tests
npm test -- tests/integration/

# Complete E2E flow ($50k Nigerian → Chinese trade)
npm test -- tests/integration/e2e_trade_flow.test.ts

Test Coverage Targets

  • ✅ All 8 contracts: 100% circuit coverage
  • ✅ All 5 ZK circuits: valid/tampered/replay/insufficient
  • ✅ Complete E2E: Register → PO → Escrow → Delivery → Settlement → Invoice → Financing

📚 Documentation

Document Description
SPEC.md Complete technical specification
ARCHITECTURE.md System architecture & data flows
PRIVACY_MODEL.md Privacy guarantees & selective disclosure
SECURITY.md Threat model, mitigations, audit checklist
ECONOMICS.md Business model, fees, projections
API.md TypeScript SDK + REST API + Webhooks
DEPLOYMENT.md Step-by-step deploy to Preprod
TOOLCHAIN.md Exact version matrix
SHARED.md Cross-cutting patterns & conventions

🔧 Toolchain Versions (Critical)

Tool Version Note
compact 0.5.1 Exact — npm i -g @midnight-ntwrk/compact@0.5.1
Node.js 22.x LTS
TypeScript 5.2.2 Not 6.x
Vite 5.4.21 Not 6.x (Rolldown)
React 18.2.0 Not 19

Required overrides (in package.json):

"overrides": {
  "@midnight-ntwrk/ledger-v8": "8.0.3",
  "@midnight-ntwrk/midnight-js-utils": "4.0.4"
}

🌐 Network

  • Testnet: Midnight Preprod
  • RPC: https://rpc.preprod.midnight.network
  • Indexer: https://indexer.preprod.midnight.network/api/v4/graphql
  • Explorer: https://preprod.midnightexplorer.com
  • Wallet: 1AM Wallet (browser extension)

💰 Business Model

Revenue Stream Rate
Trade execution 0.1% (min $5, max $500)
Invoice settlement 0.05% (min $2, max $200)
Financing origination 1-2% of funded
Financing servicing 0.5% annually
Enterprise API $499-$4,999/mo
Verification/Compliance $99-$500

🤝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feat/amazing-feature)
  3. Ensure all tests pass (npm test)
  4. Follow SHARED.md patterns
  5. Submit PR with description

📄 License

Apache License 2.0 — see LICENSE for details.


🎬 Video Demo

VeilCommerce Demo

Watch: https://youtu.be/Yyqx0SIZaX0


🔗 Links


VeilCommerce — Trust without total transparency.

About

Private financial infrastructure for global commerce. Trade globally. Reveal less.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages