123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /**
- * 凭证管理器基本使用示例
- *
- * 演示如何使用凭证管理器进行账户加载、签名等基本操作
- */
- import { CredentialManagerFactory, Platform, PacificaOrderType } from '../src/index';
- async function basicUsageExample() {
- console.log('🚀 凭证管理器基本使用示例');
- // 1. 创建凭证管理器
- const factory = new CredentialManagerFactory();
- const manager = await factory.create({
- enableFileWatching: false, // 示例中禁用文件监听
- enableLogging: true,
- logLevel: 'info'
- });
- try {
- // 2. 添加Pacifica账户
- console.log('\n📝 添加Pacifica账户...');
- const success = await manager.addAccount({
- id: 'pacifica-demo-001',
- platform: Platform.PACIFICA,
- credentials: {
- type: 'pacifica',
- privateKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' // 示例私钥
- },
- metadata: {
- alias: 'Pacifica演示账户',
- description: '用于演示的Pacifica账户'
- }
- });
- if (success) {
- console.log('✅ 账户添加成功');
- }
- // 3. 列出所有账户
- console.log('\n📋 当前账户列表:');
- const accounts = manager.listAccounts();
- accounts.forEach(account => {
- console.log(` - ${account.id} (${account.platform}) - ${account.metadata?.alias}`);
- });
- // 4. 签名测试
- console.log('\n🔐 执行签名测试...');
- const testMessage = new TextEncoder().encode('Hello, Pacifica!');
- const startTime = Date.now();
- const signResult = await manager.sign('pacifica-demo-001', testMessage);
- const signTime = Date.now() - startTime;
- if (signResult.success) {
- console.log(`✅ 签名成功! 耗时: ${signTime}ms`);
- console.log(` 算法: ${signResult.algorithm}`);
- console.log(` 签名: ${signResult.signature?.substring(0, 20)}...`);
- console.log(` 执行时间: ${signResult.executionTime}ms`);
- // 验证性能要求
- if (signResult.executionTime && signResult.executionTime < 50) {
- console.log('🎯 满足 <50ms 性能要求');
- } else {
- console.log('⚠️ 未满足 <50ms 性能要求');
- }
- } else {
- console.log(`❌ 签名失败: ${signResult.error}`);
- }
- // 5. 获取统计信息
- console.log('\n📊 管理器统计信息:');
- const stats = await manager.getStats();
- console.log(` 总账户数: ${stats.totalAccounts}`);
- console.log(` Pacifica账户: ${stats.accountsByPlatform[Platform.PACIFICA]}`);
- console.log(` 总签名次数: ${stats.totalSignatures}`);
- console.log(` 成功率: ${(stats.successRate * 100).toFixed(1)}%`);
- console.log(` 平均签名时间: ${stats.averageSignatureTime.toFixed(2)}ms`);
- console.log(` 运行时间: ${(stats.uptime / 1000).toFixed(1)}s`);
- // 6. 移除账户
- console.log('\n🗑️ 移除账户...');
- const removed = await manager.removeAccount('pacifica-demo-001');
- if (removed) {
- console.log('✅ 账户移除成功');
- }
- } finally {
- // 7. 清理资源
- console.log('\n🧹 清理资源...');
- await manager.destroy();
- console.log('✅ 示例完成');
- }
- }
- // 执行示例
- if (require.main === module) {
- basicUsageExample().catch(console.error);
- }
- export { basicUsageExample };
|