File size: 4,393 Bytes
44f0c63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * Batch Audit Example - Audit Proof Circuit
 * 
 * This example demonstrates how to generate multiple audit proofs
 * in batch for compliance reporting or bulk operations.
 * 
 * โš ๏ธ Note: This example uses sandbox mode. For production, use API key authentication.
 * 
 * Prerequisites:
 * - npm install @affixio/sdk
 * - npm install typescript @types/node ts-node
 * 
 * Run with: npx ts-node examples/batch-audit.ts
 */

import { AffixIO } from '@affixio/sdk';

interface AuditEvent {
  decisionValue: number;
  pseudonymisedId: string;
  rulesHash: string;
  timestamp: string;
  description: string;
}

async function generateBatchAuditProofs(sdk: AffixIO, events: AuditEvent[]) {
  console.log(`๐Ÿ“‹ Generating audit proofs for ${events.length} events...\n`);

  const results = [];
  const errors = [];

  for (let i = 0; i < events.length; i++) {
    const event = events[i];
    console.log(`[${i + 1}/${events.length}] Processing: ${event.description}`);

    try {
      const result = await sdk.circuits.auditProof({
        decisionValue: event.decisionValue,
        pseudonymisedId: event.pseudonymisedId,
        rulesHash: event.rulesHash,
        timestamp: event.timestamp
      }, true); // Use sandbox mode

      results.push({
        event: event.description,
        proofId: result.proofId,
        verified: result.verified,
        decision: result.decision
      });

      console.log(`   โœ… Proof generated: ${result.proofId.substring(0, 20)}...`);
    } catch (error: any) {
      errors.push({
        event: event.description,
        error: error.message
      });
      console.log(`   โŒ Error: ${error.message}`);
    }
  }

  console.log('\n๐Ÿ“Š Batch Audit Summary:');
  console.log(`   Total Events: ${events.length}`);
  console.log(`   Successful: ${results.length}`);
  console.log(`   Failed: ${errors.length}`);
  console.log(`   Success Rate: ${((results.length / events.length) * 100).toFixed(1)}%`);

  return { results, errors };
}

async function main() {
  // Initialize SDK
  // For production, use: new AffixIO({ apiKey: process.env.AFFIXIO_API_KEY })
  const sdk = new AffixIO({
    sandbox: true
  });

  // Example: Generate audit proofs for multiple compliance events
  const auditEvents: AuditEvent[] = [
    {
      decisionValue: 1,
      pseudonymisedId: '0x' + Buffer.from('user001').toString('hex').padStart(64, '0'),
      rulesHash: '0x' + Buffer.from('kyc_verified && age >= 18').toString('hex').padStart(64, '0'),
      timestamp: new Date().toISOString(),
      description: 'User registration - KYC verified'
    },
    {
      decisionValue: 1,
      pseudonymisedId: '0x' + Buffer.from('user002').toString('hex').padStart(64, '0'),
      rulesHash: '0x' + Buffer.from('balance >= amount').toString('hex').padStart(64, '0'),
      timestamp: new Date().toISOString(),
      description: 'Payment authorization - Sufficient balance'
    },
    {
      decisionValue: 0,
      pseudonymisedId: '0x' + Buffer.from('user003').toString('hex').padStart(64, '0'),
      rulesHash: '0x' + Buffer.from('fraud_score < threshold').toString('hex').padStart(64, '0'),
      timestamp: new Date().toISOString(),
      description: 'Transaction denied - High fraud score'
    },
    {
      decisionValue: 1,
      pseudonymisedId: '0x' + Buffer.from('user004').toString('hex').padStart(64, '0'),
      rulesHash: '0x' + Buffer.from('consent_given && gdpr_compliant').toString('hex').padStart(64, '0'),
      timestamp: new Date().toISOString(),
      description: 'Data processing - GDPR consent verified'
    }
  ];

  const { results, errors } = await generateBatchAuditProofs(sdk, auditEvents);

  // Generate compliance report
  console.log('\n๐Ÿ“„ Compliance Report:');
  results.forEach((result, index) => {
    console.log(`\n   Event ${index + 1}: ${result.event}`);
    console.log(`   Proof ID: ${result.proofId}`);
    console.log(`   Status: ${result.verified ? 'โœ… Verified' : 'โŒ Failed'}`);
    console.log(`   Decision: ${result.decision ? 'Approved' : 'Denied'}`);
  });

  if (errors.length > 0) {
    console.log('\nโš ๏ธ  Errors:');
    errors.forEach((error, index) => {
      console.log(`   ${index + 1}. ${error.event}: ${error.error}`);
    });
  }

  console.log('\n๐Ÿ“ For production use, get your API key at: https://dashboard.affix-io.com');
}

// Run the example
main().catch(console.error);