basic-usage.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * 凭证管理器基本使用示例
  3. *
  4. * 演示如何使用凭证管理器进行账户加载、签名等基本操作
  5. */
  6. import { CredentialManagerFactory, Platform, PacificaOrderType } from '../src/index';
  7. async function basicUsageExample() {
  8. console.log('🚀 凭证管理器基本使用示例');
  9. // 1. 创建凭证管理器
  10. const factory = new CredentialManagerFactory();
  11. const manager = await factory.create({
  12. enableFileWatching: false, // 示例中禁用文件监听
  13. enableLogging: true,
  14. logLevel: 'info'
  15. });
  16. try {
  17. // 2. 添加Pacifica账户
  18. console.log('\n📝 添加Pacifica账户...');
  19. const success = await manager.addAccount({
  20. id: 'pacifica-demo-001',
  21. platform: Platform.PACIFICA,
  22. credentials: {
  23. type: 'pacifica',
  24. privateKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' // 示例私钥
  25. },
  26. metadata: {
  27. alias: 'Pacifica演示账户',
  28. description: '用于演示的Pacifica账户'
  29. }
  30. });
  31. if (success) {
  32. console.log('✅ 账户添加成功');
  33. }
  34. // 3. 列出所有账户
  35. console.log('\n📋 当前账户列表:');
  36. const accounts = manager.listAccounts();
  37. accounts.forEach(account => {
  38. console.log(` - ${account.id} (${account.platform}) - ${account.metadata?.alias}`);
  39. });
  40. // 4. 签名测试
  41. console.log('\n🔐 执行签名测试...');
  42. const testMessage = new TextEncoder().encode('Hello, Pacifica!');
  43. const startTime = Date.now();
  44. const signResult = await manager.sign('pacifica-demo-001', testMessage);
  45. const signTime = Date.now() - startTime;
  46. if (signResult.success) {
  47. console.log(`✅ 签名成功! 耗时: ${signTime}ms`);
  48. console.log(` 算法: ${signResult.algorithm}`);
  49. console.log(` 签名: ${signResult.signature?.substring(0, 20)}...`);
  50. console.log(` 执行时间: ${signResult.executionTime}ms`);
  51. // 验证性能要求
  52. if (signResult.executionTime && signResult.executionTime < 50) {
  53. console.log('🎯 满足 <50ms 性能要求');
  54. } else {
  55. console.log('⚠️ 未满足 <50ms 性能要求');
  56. }
  57. } else {
  58. console.log(`❌ 签名失败: ${signResult.error}`);
  59. }
  60. // 5. 获取统计信息
  61. console.log('\n📊 管理器统计信息:');
  62. const stats = await manager.getStats();
  63. console.log(` 总账户数: ${stats.totalAccounts}`);
  64. console.log(` Pacifica账户: ${stats.accountsByPlatform[Platform.PACIFICA]}`);
  65. console.log(` 总签名次数: ${stats.totalSignatures}`);
  66. console.log(` 成功率: ${(stats.successRate * 100).toFixed(1)}%`);
  67. console.log(` 平均签名时间: ${stats.averageSignatureTime.toFixed(2)}ms`);
  68. console.log(` 运行时间: ${(stats.uptime / 1000).toFixed(1)}s`);
  69. // 6. 移除账户
  70. console.log('\n🗑️ 移除账户...');
  71. const removed = await manager.removeAccount('pacifica-demo-001');
  72. if (removed) {
  73. console.log('✅ 账户移除成功');
  74. }
  75. } finally {
  76. // 7. 清理资源
  77. console.log('\n🧹 清理资源...');
  78. await manager.destroy();
  79. console.log('✅ 示例完成');
  80. }
  81. }
  82. // 执行示例
  83. if (require.main === module) {
  84. basicUsageExample().catch(console.error);
  85. }
  86. export { basicUsageExample };