index.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Credential Manager Module - Public API
  3. *
  4. * Main entry point for the multi-platform credential management system.
  5. * Provides unified interface for account management, configuration loading,
  6. * and signing operations across Pacifica, Aster, and Binance platforms.
  7. */
  8. // Main classes
  9. export { CredentialManager } from './CredentialManager'
  10. export { ConfigLoader } from './ConfigLoader'
  11. export { CredentialValidator } from './CredentialValidator'
  12. export { PlatformDetector } from './PlatformDetector'
  13. // Platform-specific signers
  14. export { PacificaSigner } from './signers/PacificaSigner'
  15. // Platform detectors
  16. export { PacificaDetector, AsterDetector, BinanceDetector } from './PlatformDetector'
  17. // Utility classes
  18. export { PacificaKeyUtils, PacificaSignerMetrics } from './signers/PacificaSigner'
  19. // Re-export all types from the types module
  20. export * from '@/types/credential'
  21. // Constants and defaults
  22. export const CREDENTIAL_MANAGER_VERSION = '1.0.0'
  23. export const DEFAULT_CONFIG_PATHS = [
  24. './credentials.json',
  25. './config/credentials.json',
  26. './credentials.yaml',
  27. './config/credentials.yaml',
  28. ]
  29. export const SUPPORTED_PLATFORMS = ['PACIFICA', 'ASTER', 'BINANCE'] as const
  30. export const SUPPORTED_SIGNATURE_TYPES = ['ed25519', 'eip191', 'hmac-sha256'] as const
  31. // Performance constants
  32. export const PERFORMANCE_REQUIREMENTS = {
  33. CONFIG_LOAD_TIME_MS: 100,
  34. SIGNING_TIME_MS: 50,
  35. MEMORY_LIMIT_MB: 50,
  36. MIN_SUPPORTED_ACCOUNTS: 50,
  37. } as const
  38. // Factory function for easy initialization
  39. export function createCredentialManager(): CredentialManager {
  40. return new CredentialManager()
  41. }
  42. // Convenience function to create and load configuration
  43. export async function createCredentialManagerWithConfig(configPath: string): Promise<{
  44. manager: CredentialManager
  45. loadResult: import('@/types/credential').LoadResult
  46. }> {
  47. const manager = new CredentialManager()
  48. const loadResult = await manager.loadConfig(configPath)
  49. return {
  50. manager,
  51. loadResult,
  52. }
  53. }
  54. // Helper function to validate configuration file before loading
  55. export async function validateConfigFile(configPath: string): Promise<{
  56. isValid: boolean
  57. errors: string[]
  58. warnings: string[]
  59. accountCount: number
  60. supportedPlatforms: string[]
  61. }> {
  62. const configLoader = new ConfigLoader()
  63. const validator = new CredentialValidator()
  64. try {
  65. const loadResult = await configLoader.loadConfig(configPath)
  66. if (!loadResult.success) {
  67. return {
  68. isValid: false,
  69. errors: loadResult.errors || ['Configuration load failed'],
  70. warnings: [],
  71. accountCount: 0,
  72. supportedPlatforms: [],
  73. }
  74. }
  75. const validationReport = validator.generateValidationReport(
  76. loadResult.accounts.map(account => ({
  77. id: account.id,
  78. platform: account.platform,
  79. name: account.name,
  80. credentials: account.credentials,
  81. enabled: account.enabled,
  82. metadata: account.metadata,
  83. })),
  84. )
  85. const supportedPlatforms = Object.entries(validationReport.platformCounts)
  86. .filter(([_, count]) => count > 0)
  87. .map(([platform, _]) => platform)
  88. return {
  89. isValid: validationReport.isValid,
  90. errors: validationReport.globalErrors,
  91. warnings: validationReport.globalWarnings,
  92. accountCount: validationReport.validAccounts,
  93. supportedPlatforms,
  94. }
  95. } catch (error) {
  96. return {
  97. isValid: false,
  98. errors: [`Failed to validate configuration: ${error instanceof Error ? error.message : 'Unknown error'}`],
  99. warnings: [],
  100. accountCount: 0,
  101. supportedPlatforms: [],
  102. }
  103. }
  104. }
  105. // Helper function to generate configuration template
  106. export function generateConfigTemplate(): import('@/types/credential').ConfigFile {
  107. const configLoader = new ConfigLoader()
  108. return configLoader.getConfigTemplate()
  109. }
  110. // Helper function to check system requirements
  111. export function checkSystemRequirements(): {
  112. nodejs: { supported: boolean; version?: string }
  113. typescript: { supported: boolean }
  114. memory: { available: boolean; usage?: number }
  115. dependencies: {
  116. tweetnacl: boolean
  117. }
  118. } {
  119. const requirements = {
  120. nodejs: { supported: false },
  121. typescript: { supported: false },
  122. memory: { available: false },
  123. dependencies: {
  124. tweetnacl: false,
  125. },
  126. }
  127. // Check Node.js version
  128. if (process.version) {
  129. const version = process.version
  130. const majorVersion = parseInt(version.slice(1).split('.')[0])
  131. requirements.nodejs = {
  132. supported: majorVersion >= 18,
  133. version,
  134. }
  135. }
  136. // Check TypeScript (basic check)
  137. try {
  138. require.resolve('typescript')
  139. requirements.typescript.supported = true
  140. } catch (error) {
  141. // TypeScript not available
  142. }
  143. // Check memory
  144. if (process.memoryUsage) {
  145. const memUsage = process.memoryUsage()
  146. requirements.memory = {
  147. available: memUsage.heapUsed < PERFORMANCE_REQUIREMENTS.MEMORY_LIMIT_MB * 1024 * 1024,
  148. usage: Math.round(memUsage.heapUsed / (1024 * 1024)),
  149. }
  150. }
  151. // Check dependencies
  152. try {
  153. require.resolve('tweetnacl')
  154. requirements.dependencies.tweetnacl = true
  155. } catch (error) {
  156. // tweetnacl not available
  157. }
  158. return requirements
  159. }
  160. // Main export - default credential manager instance
  161. const defaultManager = new CredentialManager()
  162. export default defaultManager