Skip to content

PrivacyClient

The main class for interacting with the privacy pool.

Constructor

typescript
new PrivacyClient(config: PrivacyClientConfig)

PrivacyClientConfig

PropertyTypeRequiredDescription
rpcUrlstringYesEthereum JSON-RPC endpoint
poolAddressAddressYesUTXOPool contract address
chainIdnumberNoChain ID (auto-detected if not provided)
relayerUrlstringNoRelayer URL for gasless transactions
quickSyncUrlstringNoQuickSync service for fast initial sync
proverProverNoProver instance (uses global if not provided)
devModebooleanNoUse Anvil test account for gas
enableCachebooleanNoEnable IndexedDB caching (default: true)
makerUrlstringNoMarket maker URL for swaps

Example

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

const client = new PrivacyClient({
  rpcUrl: DEPLOYMENTS.remote.rpcUrl,
  poolAddress: DEPLOYMENTS.remote.pool,
  relayerUrl: DEPLOYMENTS.remote.relayerUrl,
  quickSyncUrl: DEPLOYMENTS.remote.quickSyncUrl,
});

Methods

connect

Connect to the privacy pool with keys.

typescript
await client.connect(options: ConnectOptions): Promise<void>

ConnectOptions

PropertyTypeDescription
mode'linked' | 'standalone'Connection mode
walletClientWalletClientFor linked mode
spendingKeybigintFor standalone mode
mnemonicstringFor standalone mode
accountIndexnumberAccount index (default: 0)

Examples

typescript
// Linked mode
await client.connect({
  mode: 'linked',
  walletClient,
});

// Standalone with key
await client.connect({
  mode: 'standalone',
  spendingKey: BigInt('0x...'),
});

// Standalone with mnemonic
await client.connect({
  mode: 'standalone',
  mnemonic: 'word1 word2 ...',
  accountIndex: 0,
});

disconnect

Clear keys from memory and disconnect.

typescript
client.disconnect(): void

isConnected

Check if client is connected.

typescript
client.isConnected(): boolean

sync

Synchronize with the blockchain.

typescript
await client.sync(onProgress?: (progress: SyncProgress) => void): Promise<void>

SyncProgress

PropertyTypeDescription
phase'commitments' | 'notes' | 'nullifiers'Current phase
currentnumberItems processed
totalnumberTotal items
messagestringHuman-readable status

Example

typescript
await client.sync((progress) => {
  console.log(`${progress.phase}: ${progress.current}/${progress.total}`);
});

getAddress

Get your ZK address for receiving.

typescript
client.getAddress(): string  // zks1...

getBalance

Get ETH balance in wei.

typescript
client.getBalance(): bigint

getBalanceByTokenId

Get balance for a specific token.

typescript
client.getBalanceByTokenId(tokenId: bigint): bigint

getAllBalances

Get all balances as a map.

typescript
client.getAllBalances(): Map<bigint, bigint>

Example

typescript
const balances = client.getAllBalances();
for (const [tokenId, balance] of balances) {
  console.log(`Token ${tokenId}: ${balance}`);
}

getUnspentNotes

Get spendable notes.

typescript
client.getUnspentNotes(tokenId?: bigint): TrackedNote[]

transfer

Execute a private transfer.

typescript
await client.transfer(request: TransferRequest): Promise<Hash>

TransferRequest

PropertyTypeRequiredDescription
recipientstringYesZK address (zks1...)
amountstring | bigintYesAmount to send
tokenIdbigintNoToken ID (0n for ETH)
inputNotesTrackedNote[]NoSpecific notes to spend

unshield

Withdraw to an EOA.

typescript
await client.unshield(request: UnshieldRequest): Promise<Hash>

UnshieldRequest

PropertyTypeRequiredDescription
recipientAddressYesEOA address (0x...)
amountstring | bigintYesAmount to withdraw
tokenIdbigintNoToken ID (0n for ETH)

consolidate

Merge multiple notes into one.

typescript
await client.consolidate(request: ConsolidateRequest): Promise<Hash>

ConsolidateRequest

PropertyTypeDescription
notesTrackedNote[]Notes to consolidate (max 2)
tokenIdbigintToken ID

clearCache

Clear IndexedDB cache.

typescript
await client.clearCache(): Promise<void>

Types

TrackedNote

typescript
interface TrackedNote {
  note: Note;
  commitment: bigint;
  leafIndex: number;
  nullifier: bigint;
  spent: boolean;
  txHash?: Hash;
  blockNumber?: bigint;
  tokenId: bigint;
  senderAddress?: string;
}

Note

typescript
interface Note {
  npk: bigint;      // Nullifier public key
  amount: bigint;   // Note amount
  random: bigint;   // Random blinding factor
}

Released under the MIT License.