特性:001-credential-manager 日期:2025-09-29 (更新) 状态:Phase 0 完成 用户需求:用于多平台对冲的账号管理模块,智能注入账户分辨不同平台执行对应业务
本次调研更新旨在解决多平台对冲账号管理模块实现中的关键技术选择,重点关注AccountRegistry简化模式、对冲业务集成、智能平台识别、多平台签名算法以及热加载性能优化。基于宪章v1.6.0架构简化要求,避免过度设计。
调研 Ed25519签名库 在 TypeScript 的最佳做法
ed25519
import { ed25519 } from '@noble/ed25519';
// 密钥生成和验证
const privateKey = ed25519.utils.randomPrivateKey();
const publicKey = await ed25519.getPublicKey(privateKey);
// Pacifica订单签名格式
const signMessage = async (message: Uint8Array, privateKey: Uint8Array) => {
const signature = await ed25519.sign(message, privateKey);
return Buffer.from(signature).toString('base64');
};
收集 文件监听 在 Node.js 的最佳实践
import fs from 'fs';
import { debounce } from 'lodash';
class ConfigWatcher {
private watcher?: fs.FSWatcher;
watch(filePath: string, callback: () => void) {
// 防抖动:避免短时间内多次触发
const debouncedCallback = debounce(callback, 100);
this.watcher = fs.watch(filePath, (eventType) => {
if (eventType === 'change') {
debouncedCallback();
}
});
}
close() {
this.watcher?.close();
}
}
调研 智能识别算法 在 多平台系统 的设计模式
interface PlatformDetector {
detect(credential: any): Platform | null;
confidence: number; // 识别置信度
}
class PacificaDetector implements PlatformDetector {
confidence = 0.9;
detect(credential: any): Platform | null {
// 检查Ed25519私钥格式 (64字符十六进制)
if (credential.privateKey &&
typeof credential.privateKey === 'string' &&
/^[0-9a-fA-F]{64}$/.test(credential.privateKey)) {
return Platform.PACIFICA;
}
return null;
}
}
class BinanceDetector implements PlatformDetector {
confidence = 0.95;
detect(credential: any): Platform | null {
// 检查API密钥对格式
if (credential.apiKey && credential.secretKey &&
typeof credential.apiKey === 'string' &&
typeof credential.secretKey === 'string') {
return Platform.BINANCE;
}
return null;
}
}
收集 统一接口设计 在 签名服务 的最佳实践
// 统一签名接口
interface SignerStrategy {
sign(message: Uint8Array, accountId: string): Promise<string>;
verify(message: Uint8Array, signature: string, publicKey: string): Promise<boolean>;
}
// 平台特定实现
class PacificaSignerStrategy implements SignerStrategy {
async sign(message: Uint8Array, accountId: string): Promise<string> {
const credential = this.getCredential(accountId);
return await ed25519.sign(message, credential.privateKey);
}
}
// 统一签名器
class UnifiedSigner {
private strategies = new Map<Platform, SignerStrategy>();
register(platform: Platform, strategy: SignerStrategy) {
this.strategies.set(platform, strategy);
}
async sign(accountId: string, message: Uint8Array): Promise<string> {
const platform = this.detectPlatform(accountId);
const strategy = this.strategies.get(platform);
return await strategy.sign(message, accountId);
}
}
决策:复用现有AccountRegistry模式,避免复杂工厂适配器组合 理由:符合宪章v1.6.0架构简化要求,调用链<3层,直接支持对冲业务需求 备选:复杂工厂模式(违反过度设计防护要求)
决策:credential-manager直接服务于多平台对冲业务,支持账户池管理 理由:专注实际业务集成而非抽象复用,满足Delta中性策略要求 备选:独立npm包(过度抽象,增加集成复杂度)
决策:使用Node.js内置fs.watch + 防抖动 理由:满足性能要求,无额外依赖,跨平台兼容 备选:第三方库(增加依赖)、轮询(性能差)
决策:专注于热加载、智能识别、统一签名 理由:用户明确要求简化,降低初始复杂度 备选:复杂的凭证管理系统(超出需求)
决策:使用tweetnacl(现有codebase已采用)
理由:与现有PacificaSigner兼容,性能满足要求,减少依赖变更
备选:@noble/ed25519
(需要迁移现有代码)、sodium-native(编译复杂)
决策:扩展AccountRegistry支持账户分组和状态跟踪 理由:对冲业务需要账户池概念,支持负载均衡和故障转移 备选:独立账户池服务(增加系统复杂度)
调研多平台对冲业务中账户管理的特殊需求和设计模式
interface HedgingAccountPool {
primaryAccounts: string[]; // 主要对冲账户
backupAccounts: string[]; // 备用账户
testAccounts: string[]; // 测试账户
loadBalanceStrategy: 'round-robin' | 'random' | 'weighted';
}
interface AccountStatus {
accountId: string;
platform: Platform;
isActive: boolean;
lastActivity: Date;
failureCount: number;
balanceInfo?: {
totalValue: number;
positions: Record<string, number>;
};
}
文件监听兼容性
平台识别准确性
性能要求
库兼容性
所有技术调研已完成,关键决策如下:
技术栈确定,架构简化方案明确,对冲业务需求分析完成,无NEEDS CLARIFICATION项,可以进入Phase 1设计阶段。
重点更新:
Phase 0 状态:✅ 完成 (更新支持对冲业务) 下一阶段:Phase 1 - 设计与契约