PacificaDetector.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Pacifica平台检测器
  3. *
  4. * 基于Ed25519私钥格式检测Pacifica平台
  5. */
  6. import { Platform } from '@/core/types';
  7. import { BaseDetector } from '@/core/PlatformDetector';
  8. export class PacificaDetector extends BaseDetector {
  9. public readonly confidence = 0.9;
  10. public detect(credentials: any): Platform | null {
  11. if (!credentials || typeof credentials !== 'object') {
  12. return null;
  13. }
  14. // 检查是否声明为Pacifica类型
  15. if (this.hasProperty(credentials, 'type') && credentials.type === 'pacifica') {
  16. // 验证privateKey格式
  17. if (this.hasProperty(credentials, 'privateKey')) {
  18. if (this.isValidPacificaPrivateKey(credentials.privateKey)) {
  19. return Platform.PACIFICA;
  20. }
  21. }
  22. }
  23. // 即使没有声明类型,也可以基于privateKey格式推断
  24. if (this.hasProperty(credentials, 'privateKey') && !this.hasProperty(credentials, 'apiKey')) {
  25. if (this.isValidPacificaPrivateKey(credentials.privateKey)) {
  26. // 没有apiKey/secretKey,且privateKey符合Ed25519格式,很可能是Pacifica
  27. return Platform.PACIFICA;
  28. }
  29. }
  30. return null;
  31. }
  32. /**
  33. * 验证是否为有效的Pacifica私钥格式
  34. */
  35. private isValidPacificaPrivateKey(privateKey: any): boolean {
  36. if (!this.isNonEmptyString(privateKey)) {
  37. return false;
  38. }
  39. // Ed25519私钥:64字符十六进制字符串
  40. const ed25519Pattern = /^[0-9a-fA-F]{64}$/;
  41. return this.matchesPattern(privateKey, ed25519Pattern);
  42. }
  43. }