|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
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() { |
|
|
|
|
|
|
|
|
const sdk = new AffixIO({ |
|
|
sandbox: true |
|
|
}); |
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
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'); |
|
|
} |
|
|
|
|
|
|
|
|
main().catch(console.error); |
|
|
|
|
|
|