The Identity Plugin provides comprehensive identity management for ICP applications, handling seed phrases, anonymous identities, and identity switching. It’s the foundation for all authenticated operations in the ICP Agent Kit.
import { ICPAgent } from 'icp-agent-kit';// Initialize agentconst agent = new ICPAgent({ network: 'mainnet' });await agent.initialize();const identity = agent.identityPlugin;// Generate a seed phrase and create identityconst seedPhrase = identity.generateSeedPhrase(12);await identity.createFromSeedPhrase(seedPhrase, 'my-wallet');// Switch to the new identityawait identity.switch('my-wallet');// Get principal and account IDconst principal = identity.getPrincipal();const accountId = identity.getAccountId();
// The Identity Plugin handles all ICP network authenticationconst agent = new ICPAgent({ network: 'mainnet' });await agent.initialize();// Identity state affects all network operationsawait identity.switch('trading-wallet');// Now all canister calls, token transfers use 'trading-wallet' identityawait identity.switch('governance-wallet');// Now all operations use 'governance-wallet' identity
Critical Security Guidelines: - Store seed phrases offline in secure locations - Never log
seed phrases in production - Use 24-word phrases for high-value accounts - Implement proper key
rotation policies - Use anonymous identities only for testing
// With natural language processingconst agent = new ICPAgent({ network: 'mainnet', openai: { apiKey: process.env.OPENAI_API_KEY }});await agent.initialize();// Check current identityawait agent.processNaturalLanguage("Show me my current identity");// Output: "Current identity: anonymous, Principal: 2vxsx-fae..."// Create new identityawait agent.processNaturalLanguage("Create a new identity called trading");// Output: "Created identity 'trading' with principal: tcf25-..."// Switch identitiesawait agent.processNaturalLanguage("Switch to my trading identity");// Output: "Switched to identity 'trading'"