Skip to content

Examples

Complete working examples for common use cases.

Available Examples

ExampleDescription
React IntegrationFull React app with wallet connection
Node.js ScriptsServer-side scripts and automation
Deposit WidgetMinimal deposit-only widget

Quick Reference

Minimal Browser Setup

typescript
import { PrivacyClient, createProver, setGlobalProver, DEPLOYMENTS } from '@zkprivacy/sdk';
import { createWalletClient, custom } from 'viem';

// 1. Init prover
const prover = await createProver({
  transferCircuit: '/circuits/transfer.json',
  unshieldCircuit: '/circuits/unshield.json',
});
setGlobalProver(prover);

// 2. Create client
const client = new PrivacyClient({
  rpcUrl: DEPLOYMENTS.remote.rpcUrl,
  poolAddress: DEPLOYMENTS.remote.pool,
});

// 3. Connect
const walletClient = createWalletClient({
  transport: custom(window.ethereum),
});
await client.connect({ mode: 'linked', walletClient });

// 4. Use
await client.sync();
console.log(client.getBalance());

Minimal Node.js Setup

typescript
import { PrivacyClient, createProver, setGlobalProver, DEPLOYMENTS } from '@zkprivacy/sdk';
import { readFileSync } from 'fs';
import WebSocket from 'ws';

// WebSocket polyfill
(globalThis as any).WebSocket = WebSocket;

// 1. Init prover
const prover = await createProver({
  transferCircuit: JSON.parse(readFileSync('circuits/transfer.json', 'utf-8')),
  unshieldCircuit: JSON.parse(readFileSync('circuits/unshield.json', 'utf-8')),
});
setGlobalProver(prover);

// 2. Create client
const client = new PrivacyClient({
  rpcUrl: DEPLOYMENTS.remote.rpcUrl,
  poolAddress: DEPLOYMENTS.remote.pool,
});

// 3. Connect with key
await client.connect({
  mode: 'standalone',
  spendingKey: BigInt(process.env.SPENDING_KEY!),
});

// 4. Use
await client.sync();
console.log(client.getBalance());

Minimal Deposit Widget

typescript
import { shieldTo, DEPLOYMENTS } from '@zkprivacy/sdk/shield';

async function deposit(recipient: string, amount: string) {
  await shieldTo(
    {
      rpcUrl: DEPLOYMENTS.remote.rpcUrl,
      poolAddress: DEPLOYMENTS.remote.pool,
    },
    {
      recipient,
      amount,
      tokenAddress: DEPLOYMENTS.remote.tokens.zkUSD,
      walletClient,
    }
  );
}

Released under the MIT License.