123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /**
- * 账户模型实现
- *
- * 提供账户信息的封装和验证功能
- */
- import { Account, Platform, Credentials, AccountStatus, CredentialManagerError, ErrorType } from './types';
- /**
- * 账户实体类
- */
- export class AccountEntity implements Account {
- public readonly id: string;
- public readonly platform: Platform;
- public readonly credentials: Credentials;
- public status: AccountStatus;
- public readonly metadata?: Record<string, any>;
- constructor(config: {
- id: string;
- platform: Platform;
- credentials: Credentials;
- metadata?: Record<string, any>;
- }) {
- this.validateAccountConfig(config);
- this.id = config.id;
- this.platform = config.platform;
- this.credentials = config.credentials;
- this.metadata = config.metadata!;
- this.status = AccountStatus.LOADING;
- }
- /**
- * 验证账户配置
- */
- private validateAccountConfig(config: {
- id: string;
- platform: Platform;
- credentials: Credentials;
- }): void {
- // 验证账户ID
- if (!config.id || typeof config.id !== 'string' || config.id.trim().length === 0) {
- throw new CredentialManagerError(
- 'Account ID must be a non-empty string',
- ErrorType.VALIDATION_ERROR,
- { field: 'id', value: config.id }
- );
- }
- // 验证平台
- if (!Object.values(Platform).includes(config.platform)) {
- throw new CredentialManagerError(
- `Invalid platform: ${config.platform}`,
- ErrorType.VALIDATION_ERROR,
- { field: 'platform', value: config.platform }
- );
- }
- // 验证凭证
- this.validateCredentials(config.credentials, config.platform);
- }
- /**
- * 验证凭证信息
- */
- private validateCredentials(credentials: Credentials, platform: Platform): void {
- if (!credentials || typeof credentials !== 'object') {
- throw new CredentialManagerError(
- 'Credentials must be a valid object',
- ErrorType.VALIDATION_ERROR,
- { credentials }
- );
- }
- // 验证凭证类型与平台匹配
- const expectedType = platform.toLowerCase();
- if (credentials.type !== expectedType) {
- throw new CredentialManagerError(
- `Credential type '${credentials.type}' does not match platform '${platform}'`,
- ErrorType.VALIDATION_ERROR,
- { credentialType: credentials.type, platform }
- );
- }
- // 平台特定验证
- switch (platform) {
- case Platform.PACIFICA:
- this.validatePacificaCredentials(credentials as any);
- break;
- case Platform.ASTER:
- this.validateAsterCredentials(credentials as any);
- break;
- case Platform.BINANCE:
- this.validateBinanceCredentials(credentials as any);
- break;
- default:
- throw new CredentialManagerError(
- `Unsupported platform: ${platform}`,
- ErrorType.VALIDATION_ERROR,
- { platform }
- );
- }
- }
- /**
- * 验证Pacifica凭证
- */
- private validatePacificaCredentials(credentials: { privateKey: string }): void {
- if (!credentials.privateKey || typeof credentials.privateKey !== 'string') {
- throw new CredentialManagerError(
- 'Pacifica credentials must include a valid privateKey',
- ErrorType.VALIDATION_ERROR,
- { field: 'privateKey' }
- );
- }
- // 验证Ed25519私钥格式(64字符十六进制)
- const privateKeyRegex = /^[0-9a-fA-F]{64}$/;
- if (!privateKeyRegex.test(credentials.privateKey)) {
- throw new CredentialManagerError(
- 'Pacifica privateKey must be a 64-character hexadecimal string',
- ErrorType.VALIDATION_ERROR,
- { privateKey: credentials.privateKey.substring(0, 10) + '...' }
- );
- }
- }
- /**
- * 验证Aster凭证
- */
- private validateAsterCredentials(credentials: { privateKey: string }): void {
- if (!credentials.privateKey || typeof credentials.privateKey !== 'string') {
- throw new CredentialManagerError(
- 'Aster credentials must include a valid privateKey',
- ErrorType.VALIDATION_ERROR,
- { field: 'privateKey' }
- );
- }
- // 验证以太坊私钥格式(0x前缀 + 64字符十六进制)
- const ethPrivateKeyRegex = /^0x[0-9a-fA-F]{64}$/i;
- if (!ethPrivateKeyRegex.test(credentials.privateKey)) {
- throw new CredentialManagerError(
- 'Aster privateKey must be a valid Ethereum private key (0x + 64 hex characters)',
- ErrorType.VALIDATION_ERROR,
- { privateKey: credentials.privateKey.substring(0, 10) + '...' }
- );
- }
- }
- /**
- * 验证Binance凭证
- */
- private validateBinanceCredentials(credentials: { apiKey: string; secretKey: string }): void {
- if (!credentials.apiKey || typeof credentials.apiKey !== 'string') {
- throw new CredentialManagerError(
- 'Binance credentials must include a valid apiKey',
- ErrorType.VALIDATION_ERROR,
- { field: 'apiKey' }
- );
- }
- if (!credentials.secretKey || typeof credentials.secretKey !== 'string') {
- throw new CredentialManagerError(
- 'Binance credentials must include a valid secretKey',
- ErrorType.VALIDATION_ERROR,
- { field: 'secretKey' }
- );
- }
- // 基本长度验证
- if (credentials.apiKey.trim().length === 0) {
- throw new CredentialManagerError(
- 'Binance apiKey cannot be empty',
- ErrorType.VALIDATION_ERROR,
- { field: 'apiKey' }
- );
- }
- if (credentials.secretKey.trim().length === 0) {
- throw new CredentialManagerError(
- 'Binance secretKey cannot be empty',
- ErrorType.VALIDATION_ERROR,
- { field: 'secretKey' }
- );
- }
- }
- /**
- * 设置账户状态
- */
- public setStatus(status: AccountStatus): void {
- this.status = status;
- }
- /**
- * 检查账户是否处于活跃状态
- */
- public isActive(): boolean {
- return this.status === AccountStatus.ACTIVE;
- }
- /**
- * 获取账户的安全信息(隐藏敏感数据)
- */
- public toSafeObject(): Omit<Account, 'credentials'> & { credentialsType: string } {
- return {
- id: this.id,
- platform: this.platform,
- status: this.status,
- metadata: this.metadata,
- credentialsType: this.credentials.type,
- };
- }
- /**
- * 克隆账户(深拷贝)
- */
- public clone(): AccountEntity {
- return new AccountEntity({
- id: this.id,
- platform: this.platform,
- credentials: JSON.parse(JSON.stringify(this.credentials)),
- metadata: this.metadata ? JSON.parse(JSON.stringify(this.metadata)) : undefined,
- });
- }
- /**
- * 比较两个账户是否相等
- */
- public equals(other: Account): boolean {
- return (
- this.id === other.id &&
- this.platform === other.platform &&
- JSON.stringify(this.credentials) === JSON.stringify(other.credentials)
- );
- }
- /**
- * 获取账户的字符串表示
- */
- public toString(): string {
- return `Account(id=${this.id}, platform=${this.platform}, status=${this.status})`;
- }
- }
- /**
- * 账户工厂函数
- */
- export function createAccount(config: {
- id: string;
- platform: Platform;
- credentials: Credentials;
- metadata?: Record<string, any>;
- }): AccountEntity {
- return new AccountEntity(config);
- }
- /**
- * 从配置创建账户
- */
- export function createAccountFromConfig(config: any): AccountEntity {
- if (!config || typeof config !== 'object') {
- throw new CredentialManagerError(
- 'Account config must be a valid object',
- ErrorType.VALIDATION_ERROR,
- { config }
- );
- }
- // 转换平台字符串为枚举
- let platform: Platform;
- if (typeof config.platform === 'string') {
- const platformValue = config.platform.toLowerCase();
- switch (platformValue) {
- case 'pacifica':
- platform = Platform.PACIFICA;
- break;
- case 'aster':
- platform = Platform.ASTER;
- break;
- case 'binance':
- platform = Platform.BINANCE;
- break;
- default:
- throw new CredentialManagerError(
- `Unsupported platform: ${config.platform}`,
- ErrorType.VALIDATION_ERROR,
- { platform: config.platform }
- );
- }
- } else {
- throw new CredentialManagerError(
- 'Platform must be a valid string',
- ErrorType.VALIDATION_ERROR,
- { platform: config.platform }
- );
- }
- return new AccountEntity({
- id: config.id,
- platform,
- credentials: config.credentials,
- metadata: config.metadata,
- });
- }
|