data-model.md 4.3 KB

数据模型:多平台账户凭据管理与签名服务

核心实体设计(简化版)

1. PlatformAccount (平台账户)

存储各平台的账户凭据信息,支持不同平台的凭据格式。

interface PlatformAccount {
  accountId: string;           // 账户唯一标识
  platformId: 'pacifica' | 'aster' | 'binance' | 'okx';
  alias?: string;              // 账户别名

  // 加密存储的凭据(根据平台不同)
  credentials: {
    // Pacifica: Ed25519 私钥
    privateKey?: string;       // base58 编码

    // Aster: 以太坊私钥 + 签名者地址
    ethPrivateKey?: string;    // 0x 开头
    signerAddress?: string;    // 0x 开头

    // Binance/OKX: API 凭据
    apiKey?: string;
    secretKey?: string;
    passphrase?: string;       // OKX 需要
  };

  // 状态信息
  status: 'active' | 'inactive';
  environment: 'development' | 'production';

  // 使用统计
  usage: {
    totalSigns: number;
    lastSignAt?: Date;
    errorCount: number;
  };

  createdAt: Date;
  updatedAt: Date;
}

2. SignatureAdapter (签名适配器)

为每个平台实现特定的签名算法。

interface SignatureAdapter {
  platformId: string;         // 平台标识
  algorithm: 'ed25519' | 'eip191' | 'hmac-sha256';

  // 签名方法
  sign(data: any, credentials: any): Promise<SignatureResult>;

  // 验证方法(可选)
  verify?(signature: string, data: any, publicKey: string): Promise<boolean>;
}

interface SignatureResult {
  signature: string;
  algorithm: string;
  encoding: string;           // base58, hex, base64
  publicKey?: string;         // 如果适用
  metadata?: Record<string, any>;
}

3. ConfigManager (配置管理器)

管理配置文件的加载和热重载。

interface ConfigManager {
  configPath: string;
  lastLoaded: Date;

  // 加载的账户数据
  accounts: Map<string, PlatformAccount>; // key: platformId:accountId

  // 配置操作
  loadConfig(): Promise<void>;
  reloadConfig(): Promise<void>;
  watchConfig(): void;        // 监控文件变化

  // 账户查询
  getAccount(platformId: string, accountId: string): PlatformAccount | null;
  getAccountsByPlatform(platformId: string): PlatformAccount[];
}

配置文件格式

JSON 配置结构

{
  "pacifica": [
    {
      "accountId": "pac-main-001",
      "alias": "Pacifica主账户",
      "privateKey": "base58_encoded_private_key",
      "environment": "production"
    }
  ],
  "aster": [
    {
      "accountId": "ast-hedge-001",
      "alias": "Aster对冲账户",
      "ethPrivateKey": "0x1234567890abcdef...",
      "signerAddress": "0x742d35Cc...",
      "environment": "production"
    }
  ],
  "binance": [
    {
      "accountId": "bn-spot-001",
      "alias": "Binance现货账户",
      "apiKey": "your_api_key",
      "secretKey": "your_secret_key",
      "environment": "production"
    }
  ],
  "okx": [
    {
      "accountId": "okx-perp-001",
      "alias": "OKX永续账户",
      "apiKey": "your_api_key",
      "secretKey": "your_secret_key",
      "passphrase": "your_passphrase",
      "environment": "production"
    }
  ]
}

核心服务接口

CredentialManager (主服务)

interface CredentialManager {
  // 签名服务(核心功能)
  sign(platformId: string, accountId: string, data: any): Promise<SignatureResult>;

  // 账户查询
  getAccount(platformId: string, accountId: string): PlatformAccount | null;
  listAccounts(platformId?: string): PlatformAccount[];

  // 配置管理
  reloadConfig(): Promise<void>;
  getStatus(): ConfigStatus;
}

interface ConfigStatus {
  configPath: string;
  lastLoaded: Date;
  accountCount: number;
  platformCount: number;
  isWatching: boolean;
}

验证规则

1. 账户标识验证

  • accountId: 3-50字符,字母数字和连字符
  • platformId: 必须是 'pacifica' | 'aster' | 'binance' | 'okx'

2. 凭据格式验证

  • Pacifica privateKey: base58编码,44字符
  • Aster ethPrivateKey: 0x开头,64位十六进制
  • Binance/OKX apiKey: 非空字符串
  • Binance/OKX secretKey: 非空字符串

3. 环境隔离

  • development 和 production 环境账户分离
  • 同一 platformId + accountId 组合唯一

设计完成时间: 2025-09-28 实体总数: 3个核心实体(简化) 关系复杂度: 简单(单一职责) 下一步: 简化 API 契约文档