File size: 3,065 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
/**
 * Authenticated Usage Example - Audit Proof Circuit
 * 
 * This example demonstrates how to use the Audit Proof circuit
 * with the AffixIO SDK in production mode with API authentication.
 * 
 * ⚠️ IMPORTANT: This example requires an API key from AffixIO.
 * Get your API key at: https://dashboard.affix-io.com
 * 
 * Prerequisites:
 * - npm install @affixio/sdk
 * - npm install typescript @types/node ts-node
 * - Get an API key from https://dashboard.affix-io.com
 * - Set AFFIXIO_API_KEY environment variable or replace the placeholder
 * 
 * Run with: 
 *   AFFIXIO_API_KEY=affix_xxxxx npx ts-node examples/authenticated-usage.ts
 */

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

async function main() {
  // Get API key from environment variable or use placeholder
  const apiKey = process.env.AFFIXIO_API_KEY || 'affix_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  
  if (apiKey === 'affix_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') {
    console.error('❌ Error: API key not provided!');
    console.error('   Please set AFFIXIO_API_KEY environment variable or update the code.');
    console.error('   Get your API key at: https://dashboard.affix-io.com');
    process.exit(1);
  }

  // Initialize SDK with API key for production use
  const sdk = new AffixIO({
    apiKey: apiKey,
    baseURL: 'https://api.affix-io.com'
  });

  console.log('🔐 Generating audit proof with authentication...\n');

  // Example: Audit a payment authorization decision
  const paymentDecision = {
    decisionValue: 1, // 1 = approved, 0 = denied
    pseudonymisedId: '0x' + Buffer.from('user123').toString('hex').padStart(64, '0'),
    rulesHash: '0x' + Buffer.from('balance >= amount && kyc_verified').toString('hex').padStart(64, '0'),
    timestamp: new Date().toISOString()
  };

  try {
    const result = await sdk.circuits.auditProof(paymentDecision);

    console.log('✅ Audit Proof Generated:');
    console.log('   Decision:', result.decision ? 'Approved' : 'Denied');
    console.log('   Verified:', result.verified);
    console.log('   Proof ID:', result.proofId);
    console.log('   Timestamp:', result.timestamp);
    console.log('   Sector:', result.sector || 'N/A');
    console.log('\n✨ Audit proof stored and verified!');

    // Verify the proof
    console.log('\n🔍 Verifying proof...');
    const verification = await sdk.client.verifyProof({
      proofId: result.proofId,
      proof: result.proof,
      publicInputs: result.publicInputs
    });

    console.log('   Verification Result:', verification.verified ? '✅ Valid' : '❌ Invalid');
    console.log('   Decision:', verification.decision);

  } catch (error: any) {
    console.error('❌ Error generating audit proof:', error.message);
    if (error.statusCode) {
      console.error('   Status Code:', error.statusCode);
    }
    if (error.statusCode === 401) {
      console.error('   ⚠️  Authentication failed. Please check your API key.');
      console.error('   Get your API key at: https://dashboard.affix-io.com');
    }
  }
}

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