UNPKG

atp-sdk

Version:

Official TypeScript SDK for Agent Trust Protocolβ„’ - Build secure, verifiable, and trustworthy applications with decentralized identity, verifiable credentials, and robust access control

130 lines (109 loc) β€’ 4.36 kB
/** * Identity Management Example * * This example demonstrates how to: * - Generate a new DID and register an identity * - Set up multi-factor authentication (MFA) * - Manage trust levels and verify identities */ import { ATPClient, createQuickConfig, CryptoUtils, DIDUtils } from '@atp/sdk'; async function identityManagementExample() { console.log('πŸ” ATPβ„’ SDK Identity Management Example\n'); // Setup client const config = createQuickConfig('http://localhost'); const client = new ATPClient(config); try { // Step 1: Generate a new DID and key pair console.log('πŸ”‘ Step 1: Generating new DID and key pair...'); const { did, document, keyPair } = await DIDUtils.generateDID({ network: 'testnet', method: 'atp' }); console.log(`Generated DID: ${did}`); console.log(`Public Key: ${keyPair.publicKey}`); console.log(); // Step 2: Authenticate the client with the new DID console.log('πŸ” Step 2: Authenticating with generated DID...'); client.setAuthentication({ did: did, privateKey: keyPair.privateKey }); // Step 3: Register the identity console.log('πŸ“ Step 3: Registering identity...'); const registrationResult = await client.identity.register({ did: did, document: document, metadata: { name: 'Example User', organization: 'ATP Demo', email: 'demo@example.com' } }); console.log(`βœ… Identity registered successfully!`); console.log(`Trust Level: ${registrationResult.data.trustLevel}`); console.log(); // Step 4: Set up Multi-Factor Authentication console.log('πŸ›‘οΈ Step 4: Setting up Multi-Factor Authentication...'); const mfaSetup = await client.identity.setupMFA({ method: 'totp', label: 'ATP Demo Account' }); console.log(`πŸ“± MFA Secret: ${mfaSetup.data.secret}`); console.log(`πŸ”— QR Code URL: ${mfaSetup.data.qrCodeUrl}`); console.log(); // Simulate TOTP verification (in real scenario, user would provide this) console.log('πŸ” Step 5: Simulating MFA verification...'); // Note: In a real application, you would get this code from the user's authenticator app const simulatedCode = '123456'; // This would be generated by TOTP algorithm try { await client.identity.verifyMFA({ method: 'totp', code: simulatedCode }); console.log('βœ… MFA verification successful!'); } catch (error) { console.log('ℹ️ MFA verification skipped (demo mode)'); } console.log(); // Step 6: Update trust level console.log('πŸ“ˆ Step 6: Updating trust level...'); const trustUpdate = await client.identity.updateTrustLevel(did, { level: 'VERIFIED', evidence: ['email_verified', 'phone_verified'], verifiedBy: did // Self-verification for demo }); console.log(`βœ… Trust level updated to: ${trustUpdate.data.newLevel}`); console.log(); // Step 7: Resolve and verify the identity console.log('πŸ” Step 7: Resolving identity...'); const resolvedIdentity = await client.identity.resolve(did); console.log(`πŸ“„ Resolved Identity:`); console.log(` DID: ${resolvedIdentity.data.did}`); console.log(` Trust Level: ${resolvedIdentity.data.trustLevel}`); console.log(` Status: ${resolvedIdentity.data.status}`); console.log(` Created: ${resolvedIdentity.data.createdAt}`); console.log(); // Step 8: Get trust score console.log('πŸ“Š Step 8: Getting trust score...'); const trustScore = await client.identity.getTrustScore(did, { includeHistory: true, includeFactors: true }); console.log(`πŸ† Trust Score: ${trustScore.data.score}/100`); console.log(`πŸ“ˆ Trend: ${trustScore.data.trend}`); console.log(`πŸ“‹ Factors: ${trustScore.data.factors.map(f => f.name).join(', ')}`); } catch (error) { console.error('❌ Identity management example failed:', error.message); if (error.response) { console.error('Response:', error.response.data); } } finally { client.cleanup(); console.log('\n✨ Identity management example completed!'); } } // Run the example if (import.meta.url === `file://${process.argv[1]}`) { identityManagementExample().catch(console.error); } export { identityManagementExample };