特性:001-credential-manager 日期:2025-09-28 状态:Phase 1 - 数据模型设计
描述:代表不同的交易平台,定义了特定的签名和认证规则。
属性:
id
: string - 平台唯一标识符name
: string - 平台显示名称signatureType
: SignatureType - 签名算法类型枚举值:
enum Platform {
PACIFICA = 'pacifica',
ASTER = 'aster',
BINANCE = 'binance'
}
enum SignatureType {
ED25519 = 'ed25519',
ECDSA_ETHEREUM = 'ecdsa-ethereum',
HMAC_SHA256 = 'hmac-sha256'
}
验证规则:
描述:包含账户ID、平台类型、凭证信息的基本数据结构。
属性:
interface Account {
id: string; // 账户唯一ID
platform: Platform; // 所属平台
credentials: Credentials; // 凭证信息
metadata?: { // 可选元数据
alias?: string; // 账户别名
description?: string; // 账户描述
[key: string]: any; // 其他自定义字段
};
}
凭证结构:
type Credentials = PacificaCredentials | AsterCredentials | BinanceCredentials;
interface PacificaCredentials {
type: 'pacifica';
privateKey: string; // 64字符十六进制Ed25519私钥
}
interface AsterCredentials {
type: 'aster';
privateKey: string; // 以太坊格式私钥
}
interface BinanceCredentials {
type: 'binance';
apiKey: string; // API密钥
secretKey: string; // 密钥对
}
验证规则:
描述:提供统一签名接口的组件,内部根据平台类型调用对应算法。
接口定义:
interface Signer {
// 统一签名接口
sign(accountId: string, message: Uint8Array): Promise<SignResult>;
// 验证签名
verify(accountId: string, message: Uint8Array, signature: string): Promise<boolean>;
// 获取账户信息
getAccount(accountId: string): Account | null;
// 列出所有账户
listAccounts(): Account[];
}
interface SignResult {
success: boolean;
signature?: string; // Base64编码的签名
algorithm: string; // 使用的签名算法
timestamp: Date; // 签名时间戳
error?: string; // 错误信息
}
平台特定签名策略:
interface SignerStrategy {
platform: Platform;
sign(message: Uint8Array, credentials: Credentials): Promise<string>;
verify(message: Uint8Array, signature: string, publicKey: string): Promise<boolean>;
}
描述:负责从配置文件加载账户信息并监听文件变更。
接口定义:
interface ConfigLoader {
// 加载配置文件
loadConfig(filePath: string): Promise<LoadResult>;
// 开始监听文件变更
watchConfig(filePath: string, callback: (accounts: Account[]) => void): void;
// 停止监听
stopWatching(): void;
}
interface LoadResult {
success: boolean;
accounts: Account[];
errors?: string[];
loadTime: number; // 加载耗时(毫秒)
}
配置文件格式:
interface ConfigFile {
version: string; // 配置文件版本
accounts: AccountConfig[];
}
interface AccountConfig {
id: string;
platform: Platform;
credentials: Credentials;
metadata?: Record<string, any>;
}
ConfigLoader (1) ←→ (N) Account
↓
Account (N) ←→ (1) Signer
↓
Signer (1) ←→ (N) SignerStrategy
↓
SignerStrategy (N) ←→ (1) Platform
关系说明:
[文件创建] → [首次加载] → [监听变更] → [热重载] → [验证] → [更新内存]
↓ ↓ ↓ ↓ ↓ ↓
CREATED → LOADING → WATCHING → RELOADING → VALIDATED → UPDATED
enum AccountStatus {
LOADING = 'loading', // 正在加载
ACTIVE = 'active', // 活跃可用
ERROR = 'error', // 错误状态
REMOVED = 'removed' // 已移除
}
状态转换规则:
{
"version": "1.0",
"accounts": [
{
"id": "pacifica-main-001",
"platform": "pacifica",
"credentials": {
"type": "pacifica",
"privateKey": "abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab"
},
"metadata": {
"alias": "Pacifica主账户",
"description": "用于Pacifica交易的主要账户",
"environment": "production"
}
},
{
"id": "binance-api-001",
"platform": "binance",
"credentials": {
"type": "binance",
"apiKey": "your_binance_api_key",
"secretKey": "your_binance_secret_key"
},
"metadata": {
"alias": "Binance API账户",
"testnet": false
}
}
]
}
const accountConfigSchema = {
type: "object",
properties: {
version: { type: "string", pattern: "^\\d+\\.\\d+$" },
accounts: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string", minLength: 1 },
platform: { enum: ["pacifica", "aster", "binance"] },
credentials: {
oneOf: [
{
properties: {
type: { const: "pacifica" },
privateKey: {
type: "string",
pattern: "^[0-9a-fA-F]{64}$"
}
},
required: ["type", "privateKey"]
},
{
properties: {
type: { const: "binance" },
apiKey: { type: "string", minLength: 1 },
secretKey: { type: "string", minLength: 1 }
},
required: ["type", "apiKey", "secretKey"]
}
// 其他平台schema...
]
}
},
required: ["id", "platform", "credentials"]
}
}
},
required: ["version", "accounts"]
};
实体 | 性能要求 | 实现策略 |
---|---|---|
ConfigLoader | 加载<100ms | 异步文件读取 + JSON解析优化 |
Signer | 签名<50ms | 内存缓存凭证 + 高性能签名库 |
Account | 查询<10ms | 内存索引 + HashMap查找 |
Platform识别 | 识别<1ms | 正则表达式预编译 + 优先级队列 |
enum ErrorType {
CONFIG_LOAD_ERROR = 'config_load_error',
VALIDATION_ERROR = 'validation_error',
SIGNATURE_ERROR = 'signature_error',
PLATFORM_DETECTION_ERROR = 'platform_detection_error'
}
interface CredentialError {
type: ErrorType;
message: string;
accountId?: string;
timestamp: Date;
details?: any;
}
Phase 1.1 状态:✅ 数据模型设计完成 下一步:生成API契约定义