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
JavaScript
/**
* 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 };