一个高性能的多平台加密货币交易账户凭证管理库,支持热加载、智能平台识别和安全签名功能。
npm install @binance-api/credential-manager
import { CredentialManagerFactory, Platform } from '@binance-api/credential-manager';
// 创建管理器
const factory = new CredentialManagerFactory();
const manager = await factory.create();
// 添加账户
await manager.addAccount({
id: 'my-pacifica-account',
platform: Platform.PACIFICA,
credentials: {
type: 'pacifica',
privateKey: 'your-private-key-here'
},
metadata: {
alias: '我的Pacifica账户'
}
});
// 执行签名
const message = new TextEncoder().encode('Hello, Pacifica!');
const result = await manager.sign('my-pacifica-account', message);
if (result.success) {
console.log('签名成功:', result.signature);
console.log('执行时间:', result.executionTime, 'ms');
}
// 清理资源
await manager.destroy();
// 加载JSON配置文件
const loadResult = await manager.loadConfig('./accounts.json');
if (loadResult.success) {
console.log(`成功加载 ${loadResult.accounts.length} 个账户`);
console.log(`加载时间: ${loadResult.loadTime}ms`);
}
// 监听配置文件变化
manager.watchConfig('./accounts.json', (accounts) => {
console.log('配置文件已更新,重新加载账户');
});
{
"version": "1.0.0",
"accounts": [
{
"id": "pacifica-main",
"platform": "pacifica",
"credentials": {
"type": "pacifica",
"privateKey": "your-64-char-hex-private-key"
},
"metadata": {
"alias": "主交易账户",
"description": "用于主要交易的Pacifica账户"
}
}
]
}
凭证管理器经过性能优化,满足高频交易需求:
# 运行所有测试
npm test
# 性能测试
npm run test:performance
# 覆盖率测试
npm run test:coverage
interface ICredentialManager {
// 配置管理
loadConfig(configPath: string): Promise<LoadResult>;
watchConfig(configPath: string, callback?: (accounts: Account[]) => void): void;
stopWatching(): void;
// 账户管理
addAccount(accountConfig: AccountConfig): Promise<boolean>;
removeAccount(accountId: string): Promise<boolean>;
getAccount(accountId: string): Account | null;
listAccounts(): Account[];
// 签名操作
sign(accountId: string, message: Uint8Array): Promise<SignResult>;
verify(accountId: string, message: Uint8Array, signature: string): Promise<boolean>;
// 统计和管理
getStats(): Promise<ManagerStats>;
destroy(): Promise<void>;
}
interface SignResult {
success: boolean;
signature?: string;
algorithm: string;
timestamp: Date;
executionTime?: number;
error?: string;
}
src/
├── core/ # 核心组件
│ ├── types.ts # 类型定义
│ ├── CredentialManager.ts
│ ├── Signer.ts
│ └── PlatformDetector.ts
├── loaders/ # 配置加载器
│ └── ConfigLoader.ts
├── platforms/ # 平台特定实现
│ ├── pacifica/
│ ├── aster/
│ └── binance/
└── index.ts # 主入口
# 开发模式
npm run dev
# 类型检查
npm run typecheck
# 代码格式化
npm run format
# 代码检查
npm run lint
MIT License
欢迎提交 Issue 和 Pull Request!
如有问题,请通过以下方式联系: