专注于Pacifica平台的内部凭据管理系统,提供Ed25519签名服务供项目内部其他模块调用。
核心特性:
创建 credentials.json
配置文件:
{
"pacifica": [
{
"accountId": "pac-main-001",
"alias": "Pacifica主账户",
"privateKey": "your_base58_private_key_here",
"environment": "production"
},
{
"accountId": "pac-dev-001",
"alias": "Pacifica开发账户",
"privateKey": "your_development_private_key_here",
"environment": "development"
}
]
}
import { signPacifica, hasPacificaAccount } from './src/services/index.js';
// 检查账户是否存在
if (hasPacificaAccount('pac-main-001')) {
// 执行签名
const result = await signPacifica('pac-main-001', {
instruction: {
type: 'place_order',
market: 'BTC-USD',
side: 'bid',
amount: '0.001',
price: '65000'
},
nonce: Date.now()
});
console.log('签名:', result.signature);
console.log('公钥:', result.publicKey);
}
signPacifica(accountId, data)
执行单次Pacifica签名
import { signPacifica, PacificaSignData } from './src/services/index.js';
const signData: PacificaSignData = {
instruction: {
type: 'place_order',
market: 'BTC-USD',
side: 'bid',
amount: '0.001',
price: '65000'
},
nonce: Date.now()
};
const result = await signPacifica('pac-main-001', signData);
signPacificaBatch(requests)
执行批量Pacifica签名
import { signPacificaBatch } from './src/services/index.js';
const requests = [
{
accountId: 'pac-main-001',
requestId: 'order_1',
data: { instruction: { type: 'place_order' } }
},
{
accountId: 'pac-main-002',
requestId: 'order_2',
data: { instruction: { type: 'cancel_order' } }
}
];
const results = await signPacificaBatch(requests);
hasPacificaAccount(accountId)
检查账户是否存在
import { hasPacificaAccount } from './src/services/index.js';
if (hasPacificaAccount('pac-main-001')) {
console.log('账户存在');
}
getPacificaPublicKey(accountId)
获取账户公钥
import { getPacificaPublicKey } from './src/services/index.js';
const publicKey = getPacificaPublicKey('pac-main-001');
console.log('公钥:', publicKey);
import { getCredentialService } from './src/services/index.js';
const service = getCredentialService();
// 获取服务状态
const status = service.getStatus();
console.log('Pacifica账户数:', status.pacificaAccounts);
// 获取所有账户
const accounts = service.getPacificaAccounts();
// 监听事件
service.on('signature:success', (event) => {
console.log(`签名成功: ${event.accountId}`);
});
service.on('signature:error', (event) => {
console.log(`签名失败: ${event.accountId} - ${event.error}`);
});
import { signPacifica, hasPacificaAccount } from './src/services/index.js';
class TradingModule {
async placeOrder(accountId: string, market: string, side: 'bid' | 'ask', amount: string, price: string) {
// 1. 检查账户
if (!hasPacificaAccount(accountId)) {
throw new Error(`账户 ${accountId} 不可用`);
}
// 2. 构建订单数据
const orderData = {
instruction: {
type: 'place_order',
market,
side,
amount,
price
},
nonce: Date.now()
};
// 3. 执行签名
const signatureResult = await signPacifica(accountId, orderData);
// 4. 提交到Pacifica交易所
return this.submitToPacifica(signatureResult);
}
}
import { signPacificaBatch } from './src/services/index.js';
async function hedgeTrade(account1: string, account2: string, market: string, amount: string) {
const requests = [
{
accountId: account1,
requestId: 'long_position',
data: {
instruction: {
type: 'place_order',
market,
side: 'bid',
amount
}
}
},
{
accountId: account2,
requestId: 'short_position',
data: {
instruction: {
type: 'place_order',
market,
side: 'ask',
amount
}
}
}
];
const results = await signPacificaBatch(requests);
// 处理结果...
}
Pacifica签名适配器支持以下订单类型:
place_order
- 下单cancel_order
- 撤单modify_order
- 修改订单close_position
- 平仓# 运行单元测试
npx jest __tests__/unit/pacifica-signature.test.ts
npx jest __tests__/unit/internal-credential-service.test.ts
# 运行使用示例
npx tsx examples/credential-usage.ts
src/
├── services/
│ ├── index.ts # 统一导出
│ └── credential-service.ts # 内部凭据服务
├── core/
│ ├── config-loader.ts # 配置加载器
│ └── signature-adapters/
│ └── pacifica.ts # Pacifica签名适配器
└── shared/
├── credential-types.ts # 类型定义
├── credential-constants.ts # 常量
└── credential-utils.ts # 工具函数
examples/
└── credential-usage.ts # 使用示例
__tests__/
├── unit/
│ ├── pacifica-signature.test.ts
│ └── internal-credential-service.test.ts
└── fixtures/
└── test-credentials.json # 测试配置
credentials.json
存在且包含有效的Pacifica账户