Skip to content

A minimal experiment to run a Self-powered backend in Rust, delegating verification to a tiny Node worker using @selfxyz/core for mock passport and OFAC checks.

Notifications You must be signed in to change notification settings

kartikmehta8/self-offchain-rust-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Banner

Minimal full-stack example showing how to:

  • Display a Self QR code in a web app.
  • Verify proofs on a Rust backend (which calls a minimal Node worker using @selfxyz/core) with mock passports and OFAC checking.
  • Inspect the full verification result.

Project Structure

  • client/ – Next.js frontend
    • Renders the QR code using @selfxyz/qrcode.
    • Calls the backend verify endpoint and shows the verification JSON.
  • server/ – Rust backend + Node worker
    • Cargo.toml / src/main.rs – Rust HTTP server exposing:
      • POST /api/verify
      • GET /debug/last-result
    • worker/ – Node worker project:
      • worker/worker.mjs – uses SelfBackendVerifier from @selfxyz/core.
      • worker/package.json – Node deps for the worker only.
      • Configured for staging/mock passports with OFAC enabled and minimum age 18.

Prerequisites

  • Node.js 18+ (Self SDK recommends Node 22; you may see engine warnings on other versions, but it still runs).
  • npm (comes with Node).

Setup

Install dependencies once:

# Node worker deps
cd server/worker
npm install

# Frontend deps
cd ../../client
npm install

# Build Rust backend (from repo root)
cd ..
cargo build --manifest-path server/Cargo.toml

Build the Rust backend once (from the repo root):

cargo build

Environment Configuration

Backend (server/.env)

Create server/.env in the server/ folder:

PORT=3001
SELF_SCOPE=demo1-scope
SELF_ENDPOINT=https://your-ngrok-id.ngrok-free.app/api/verify
  • PORT – local port for the Rust backend.
  • SELF_SCOPE – must match the frontend NEXT_PUBLIC_SELF_SCOPE.
  • SELF_ENDPOINT – public URL for /api/verify (typically an ngrok URL pointing to your local backend).

Frontend (client/.env.local)

Create client/.env.local:

NEXT_PUBLIC_SELF_APP_NAME=Self Verification Demo
NEXT_PUBLIC_SELF_SCOPE=demo1-scope
NEXT_PUBLIC_SELF_ENDPOINT=https://your-ngrok-id.ngrok-free.app/api/verify
NEXT_PUBLIC_SELF_DEBUG_ENDPOINT=http://localhost:3001/debug/last-result
  • NEXT_PUBLIC_SELF_SCOPE must equal SELF_SCOPE.
  • NEXT_PUBLIC_SELF_ENDPOINT must equal SELF_ENDPOINT.
  • NEXT_PUBLIC_SELF_DEBUG_ENDPOINT points directly to the local debug endpoint to read the last verification result.

How it works

  • The frontend (client/) builds a SelfApp via SelfAppBuilder and renders a QR code with SelfQRcodeWrapper. When the Self mobile app scans and completes verification, it calls the backend POST /api/verify endpoint with the proof.
  • The Rust backend (src/main.rs) is the only HTTP server. It exposes:
    • GET / – basic status/config info.
    • POST /api/verify – validates the payload and forwards it to a Node worker.
    • GET /debug/last-result – returns the last VerificationResult (for inspection in the UI).
  • The Node worker (server/worker.mjs) runs as a child process managed by the Rust crate node-workers:
    • It listens on stdin/stdout using a simple line-based protocol (READY, PAYLOAD_CHUNK, RESULT_CHUNK, OK).
    • It constructs SelfBackendVerifier with:
      • scope from SELF_SCOPE
      • endpoint from SELF_ENDPOINT
      • mockPassport = true
      • AllIds and DefaultConfigStore with minimum age 18, OFAC enabled.
    • It exposes a single command, verifyProof, which calls selfBackendVerifier.verify(...) and returns the VerificationResult JSON back to Rust.

On the Rust side, node-workers maintains a pool of long-lived Node worker processes so subsequent verifications don’t pay the cost of booting Node each time.

Running the Stack

  1. Start the backend (Rust)

    cd server
    cargo run
    • Listens on http://localhost:3001 by default (controlled by PORT).
    • Endpoints:
      • POST /api/verify – main verification endpoint.
      • GET /debug/last-result – returns the last verification result (for the frontend).
  2. Expose the backend to Self (optional, for real device testing)

    ngrok http 3001
    • Use the ngrok URL (e.g. https://xxxx.ngrok-free.app/api/verify) in both:
      • SELF_ENDPOINT
      • NEXT_PUBLIC_SELF_ENDPOINT
  3. Start the frontend

    cd client
    npm run dev
    • Visit http://localhost:3000 in your browser.
  4. Verify with the Self app

    • Open the Self app (staging), ensure you’re using a mock passport (see self-docs/using-mock-passports.md).
    • Scan the QR code from the frontend.
    • After verification:
      • The QR disappears.
      • The full VerificationResult is shown as JSON below, including:
        • attestationId
        • isValidDetails (overall, age, OFAC)
        • discloseOutput (nationality, gender, etc.)
        • userData (userIdentifier + userDefinedData).

About

A minimal experiment to run a Self-powered backend in Rust, delegating verification to a tiny Node worker using @selfxyz/core for mock passport and OFAC checks.

Topics

Resources

Stars

Watchers

Forks