Skip to main content

Privacy Testing

Write tests that verify privacy requirements.

Setup

npm install --save-dev solana-privacy-scanner-ci-tools

Create tests/setup.ts:

import 'solana-privacy-scanner-ci-tools/matchers';

Add to vitest.config.ts:

export default {
setupFilesAfterEnv: ['./tests/setup.ts'],
};

Matchers

MatcherDescription
toHavePrivacyRisk(level)Assert risk level
toNotLeakUserRelationships()No linkage
toHaveNoHighRiskSignals()No HIGH signals
toNotHaveSignal(type)Signal not present
toHavePrivacyScore(min)Min score (0-100)

Examples

Basic Test

import { simulateTransactionPrivacy } from 'solana-privacy-scanner-ci-tools/simulator';

test('transfer maintains privacy', async () => {
const tx = await buildTransaction();
const report = await simulateTransactionPrivacy(tx, connection);

expect(report).toHavePrivacyRisk('LOW');
expect(report).toNotLeakUserRelationships();
});

DeFi Protocol

test('deposit maintains privacy', async () => {
const tx = await program.methods.deposit(amount).transaction();
const report = await simulateTransactionPrivacy(tx, connection);

expect(report).toHavePrivacyRisk('LOW');
expect(report).toNotHaveSignal('fee-payer-reuse');
});

Flow Analysis

test('user journey maintains privacy', async () => {
const flow = [loginTx, actionTx, logoutTx];
const flowReport = await simulateTransactionFlow(flow, connection);

expect(flowReport.cumulativeRisk).not.toBe('HIGH');
});

NFT Marketplace

test('purchase is private', async () => {
const tx = await marketplace.buy(nftMint);
const report = await simulateTransactionPrivacy(tx, connection);

expect(report).toHavePrivacyScore(70);
expect(report).toNotInteractWith('exchange');
});

Config-Based Testing

import { loadConfig } from 'solana-privacy-scanner-ci-tools/config';

const config = loadConfig();

test('meets privacy standards', async () => {
const report = await simulateTransactionPrivacy(tx, connection);
const highSignals = report.signals.filter(s => s.severity === 'HIGH').length;

expect(highSignals).toBeLessThanOrEqual(config.thresholds.maxHighSeverity);
});

Simulator Functions

// Single transaction
const report = await simulateTransactionPrivacy(tx, connection);

// Transaction flow
const flowReport = await simulateTransactionFlow(txArray, connection);

// Compare implementations
const comparison = await compareImplementations(txA, txB, connection);

Next Steps