credential-manager.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * 凭证管理模块 - API契约定义
  3. *
  4. * 本文件定义了凭证管理模块的所有公开接口,供其他模块调用。
  5. * 遵循库优先架构原则,所有接口都是类型安全的TypeScript定义。
  6. */
  7. // ============================================================================
  8. // 核心类型定义
  9. // ============================================================================
  10. export enum Platform {
  11. PACIFICA = 'pacifica',
  12. ASTER = 'aster',
  13. BINANCE = 'binance'
  14. }
  15. export enum SignatureType {
  16. ED25519 = 'ed25519',
  17. ECDSA_ETHEREUM = 'ecdsa-ethereum',
  18. HMAC_SHA256 = 'hmac-sha256'
  19. }
  20. export enum AccountStatus {
  21. LOADING = 'loading',
  22. ACTIVE = 'active',
  23. ERROR = 'error',
  24. REMOVED = 'removed'
  25. }
  26. // ============================================================================
  27. // 凭证类型定义
  28. // ============================================================================
  29. export interface PacificaCredentials {
  30. type: 'pacifica';
  31. privateKey: string; // 64字符十六进制Ed25519私钥
  32. }
  33. export interface AsterCredentials {
  34. type: 'aster';
  35. privateKey: string; // 以太坊格式私钥
  36. }
  37. export interface BinanceCredentials {
  38. type: 'binance';
  39. apiKey: string;
  40. secretKey: string;
  41. }
  42. export type Credentials = PacificaCredentials | AsterCredentials | BinanceCredentials;
  43. // ============================================================================
  44. // 核心实体接口
  45. // ============================================================================
  46. export interface Account {
  47. id: string;
  48. platform: Platform;
  49. credentials: Credentials;
  50. status?: AccountStatus;
  51. metadata?: {
  52. alias?: string;
  53. description?: string;
  54. [key: string]: any;
  55. };
  56. }
  57. export interface SignResult {
  58. success: boolean;
  59. signature?: string;
  60. algorithm: string;
  61. timestamp: Date;
  62. error?: string;
  63. }
  64. // ============================================================================
  65. // 主要接口定义
  66. // ============================================================================
  67. /**
  68. * 凭证管理器主接口
  69. * 提供配置加载、账户管理、签名服务等核心功能
  70. */
  71. export interface ICredentialManager {
  72. /**
  73. * 从配置文件加载账户信息
  74. * @param configPath 配置文件路径
  75. * @returns 加载结果
  76. */
  77. loadConfig(configPath: string): Promise<LoadResult>;
  78. /**
  79. * 开始监听配置文件变更
  80. * @param configPath 配置文件路径
  81. * @param callback 变更回调函数
  82. */
  83. watchConfig(configPath: string, callback?: (accounts: Account[]) => void): void;
  84. /**
  85. * 停止监听配置文件变更
  86. */
  87. stopWatching(): void;
  88. /**
  89. * 获取账户信息
  90. * @param accountId 账户ID
  91. * @returns 账户信息,如果不存在返回null
  92. */
  93. getAccount(accountId: string): Account | null;
  94. /**
  95. * 列出所有账户
  96. * @returns 账户列表
  97. */
  98. listAccounts(): Account[];
  99. /**
  100. * 执行签名操作
  101. * @param accountId 账户ID
  102. * @param message 要签名的消息
  103. * @returns 签名结果
  104. */
  105. sign(accountId: string, message: Uint8Array): Promise<SignResult>;
  106. /**
  107. * 验证签名
  108. * @param accountId 账户ID
  109. * @param message 原始消息
  110. * @param signature 签名字符串
  111. * @returns 验证结果
  112. */
  113. verify(accountId: string, message: Uint8Array, signature: string): Promise<boolean>;
  114. }
  115. /**
  116. * 配置加载器接口
  117. * 负责从配置文件加载账户信息并监听文件变更
  118. */
  119. export interface IConfigLoader {
  120. /**
  121. * 加载配置文件
  122. * @param filePath 配置文件路径
  123. * @returns 加载结果
  124. */
  125. loadConfig(filePath: string): Promise<LoadResult>;
  126. /**
  127. * 监听配置文件变更
  128. * @param filePath 配置文件路径
  129. * @param callback 变更回调函数
  130. */
  131. watchConfig(filePath: string, callback: (accounts: Account[]) => void): void;
  132. /**
  133. * 停止监听
  134. */
  135. stopWatching(): void;
  136. }
  137. /**
  138. * 签名器接口
  139. * 提供统一的签名服务,支持多种平台
  140. */
  141. export interface ISigner {
  142. /**
  143. * 注册平台签名策略
  144. * @param platform 平台类型
  145. * @param strategy 签名策略
  146. */
  147. registerStrategy(platform: Platform, strategy: ISignerStrategy): void;
  148. /**
  149. * 执行签名
  150. * @param accountId 账户ID
  151. * @param message 要签名的消息
  152. * @returns 签名结果
  153. */
  154. sign(accountId: string, message: Uint8Array): Promise<SignResult>;
  155. /**
  156. * 验证签名
  157. * @param accountId 账户ID
  158. * @param message 原始消息
  159. * @param signature 签名字符串
  160. * @returns 验证结果
  161. */
  162. verify(accountId: string, message: Uint8Array, signature: string): Promise<boolean>;
  163. }
  164. /**
  165. * 签名策略接口
  166. * 每个平台实现自己的签名策略
  167. */
  168. export interface ISignerStrategy {
  169. platform: Platform;
  170. /**
  171. * 执行签名
  172. * @param message 要签名的消息
  173. * @param credentials 凭证信息
  174. * @returns 签名字符串
  175. */
  176. sign(message: Uint8Array, credentials: Credentials): Promise<string>;
  177. /**
  178. * 验证签名
  179. * @param message 原始消息
  180. * @param signature 签名字符串
  181. * @param publicKey 公钥
  182. * @returns 验证结果
  183. */
  184. verify(message: Uint8Array, signature: string, publicKey: string): Promise<boolean>;
  185. }
  186. /**
  187. * 平台检测器接口
  188. * 用于智能识别账户所属平台
  189. */
  190. export interface IPlatformDetector {
  191. /**
  192. * 检测置信度
  193. */
  194. confidence: number;
  195. /**
  196. * 检测平台类型
  197. * @param credentials 凭证信息
  198. * @returns 平台类型,如果无法识别返回null
  199. */
  200. detect(credentials: any): Platform | null;
  201. }
  202. // ============================================================================
  203. // 请求/响应类型
  204. // ============================================================================
  205. export interface LoadResult {
  206. success: boolean;
  207. accounts: Account[];
  208. errors?: string[];
  209. loadTime: number; // 加载耗时(毫秒)
  210. }
  211. export interface ConfigFile {
  212. version: string;
  213. accounts: AccountConfig[];
  214. }
  215. export interface AccountConfig {
  216. id: string;
  217. platform: Platform;
  218. credentials: Credentials;
  219. metadata?: Record<string, any>;
  220. }
  221. // ============================================================================
  222. // 错误类型
  223. // ============================================================================
  224. export enum ErrorType {
  225. CONFIG_LOAD_ERROR = 'config_load_error',
  226. VALIDATION_ERROR = 'validation_error',
  227. SIGNATURE_ERROR = 'signature_error',
  228. PLATFORM_DETECTION_ERROR = 'platform_detection_error'
  229. }
  230. export interface CredentialError {
  231. type: ErrorType;
  232. message: string;
  233. accountId?: string;
  234. timestamp: Date;
  235. details?: any;
  236. }
  237. export class CredentialManagerError extends Error {
  238. constructor(
  239. message: string,
  240. public readonly type: ErrorType,
  241. public readonly details?: any
  242. ) {
  243. super(message);
  244. this.name = 'CredentialManagerError';
  245. }
  246. }
  247. // ============================================================================
  248. // 工厂函数接口
  249. // ============================================================================
  250. /**
  251. * 凭证管理器工厂接口
  252. */
  253. export interface ICredentialManagerFactory {
  254. /**
  255. * 创建凭证管理器实例
  256. * @param options 配置选项
  257. * @returns 凭证管理器实例
  258. */
  259. create(options?: CredentialManagerOptions): Promise<ICredentialManager>;
  260. }
  261. export interface CredentialManagerOptions {
  262. /**
  263. * 是否启用文件监听
  264. * @default true
  265. */
  266. enableFileWatching?: boolean;
  267. /**
  268. * 签名超时时间(毫秒)
  269. * @default 30000
  270. */
  271. signTimeout?: number;
  272. /**
  273. * 是否启用日志记录
  274. * @default true
  275. */
  276. enableLogging?: boolean;
  277. /**
  278. * 日志级别
  279. * @default 'info'
  280. */
  281. logLevel?: 'debug' | 'info' | 'warn' | 'error';
  282. }
  283. // ============================================================================
  284. // 工具函数类型
  285. // ============================================================================
  286. /**
  287. * 平台检测工具
  288. */
  289. export interface IPlatformDetectionService {
  290. /**
  291. * 注册平台检测器
  292. * @param detector 平台检测器
  293. */
  294. registerDetector(detector: IPlatformDetector): void;
  295. /**
  296. * 检测平台类型
  297. * @param credentials 凭证信息
  298. * @returns 检测结果
  299. */
  300. detectPlatform(credentials: any): DetectionResult;
  301. }
  302. export interface DetectionResult {
  303. platform: Platform | null;
  304. confidence: number;
  305. detectors: Array<{
  306. detector: string;
  307. platform: Platform | null;
  308. confidence: number;
  309. }>;
  310. }
  311. // ============================================================================
  312. // 导出所有类型
  313. // ============================================================================
  314. export {
  315. ICredentialManager as CredentialManager,
  316. IConfigLoader as ConfigLoader,
  317. ISigner as Signer,
  318. ISignerStrategy as SignerStrategy,
  319. IPlatformDetector as PlatformDetector,
  320. ICredentialManagerFactory as CredentialManagerFactory,
  321. IPlatformDetectionService as PlatformDetectionService
  322. };