credentials

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Credentials - Complete API Reference

凭证管理 - 完整API参考

Securely store and manage API credentials for trading platforms with AES-256-GCM encryption.

通过AES-256-GCM加密,为交易平台安全存储和管理API凭证。

Chat Commands

聊天命令

Add Credentials

添加凭证

/creds add polymarket                       Interactive setup
/creds add kalshi --key abc --secret xyz    Direct setup
/creds add binance                          Add Binance API
/creds add hyperliquid                      Add wallet key
/creds add polymarket                       交互式设置
/creds add kalshi --key abc --secret xyz    直接设置
/creds add binance                          添加Binance API凭证
/creds add hyperliquid                      添加钱包密钥

View Credentials

查看凭证

/creds list                                 List configured platforms
/creds status                               Encryption system status
/creds test polymarket                      Test API connection
/creds check polymarket                     Verify credentials work
/creds list                                 列出已配置的平台
/creds status                               加密系统状态
/creds test polymarket                      测试API连接
/creds check polymarket                     验证凭证有效性

Remove Credentials

删除凭证

/creds remove polymarket                    Remove platform creds
/creds clear                                Clear all (careful!)
/creds remove polymarket                    删除指定平台的凭证
/creds clear                                清空所有凭证(请谨慎操作!)

Auth Status

认证状态

/auth status                                Overall auth status
/auth refresh kalshi                        Refresh tokens
/auth cooldown                              View cooldown status

/auth status                                整体认证状态
/auth refresh kalshi                        刷新令牌
/auth cooldown                              查看冷却状态

TypeScript API Reference

TypeScript API参考

Create Credentials Manager

创建凭证管理器

typescript
import { createCredentialsManager } from 'clodds/credentials';

const creds = createCredentialsManager({
  // Encryption key (required)
  encryptionKey: process.env.CREDENTIALS_KEY,

  // Storage backend
  storage: 'sqlite',  // 'sqlite' | 'postgres'
  dbPath: './credentials.db',

  // Cooldown settings
  cooldownMinutes: 15,
  maxFailures: 3,
});
typescript
import { createCredentialsManager } from 'clodds/credentials';

const creds = createCredentialsManager({
  // 加密密钥(必填)
  encryptionKey: process.env.CREDENTIALS_KEY,

  // 存储后端
  storage: 'sqlite',  // 'sqlite' | 'postgres'
  dbPath: './credentials.db',

  // 冷却设置
  cooldownMinutes: 15,
  maxFailures: 3,
});

Set Credentials

设置凭证

typescript
// Polymarket (API + signing key)
await creds.setCredentials({
  userId: 'user-123',
  platform: 'polymarket',
  credentials: {
    apiKey: 'pk_...',
    apiSecret: 'sk_...',
    privateKey: '0x...',  // For order signing
    funderAddress: '0x...',
  },
});

// Kalshi (API key)
await creds.setCredentials({
  userId: 'user-123',
  platform: 'kalshi',
  credentials: {
    email: 'user@example.com',
    apiKey: 'key_...',
  },
});

// Binance Futures
await creds.setCredentials({
  userId: 'user-123',
  platform: 'binance',
  credentials: {
    apiKey: 'abc...',
    apiSecret: 'xyz...',
  },
});

// Hyperliquid (wallet)
await creds.setCredentials({
  userId: 'user-123',
  platform: 'hyperliquid',
  credentials: {
    privateKey: '0x...',
    walletAddress: '0x...',
  },
});
typescript
// Polymarket(API密钥 + 签名密钥)
await creds.setCredentials({
  userId: 'user-123',
  platform: 'polymarket',
  credentials: {
    apiKey: 'pk_...',
    apiSecret: 'sk_...',
    privateKey: '0x...',  // 用于订单签名
    funderAddress: '0x...',
  },
});

// Kalshi(API密钥)
await creds.setCredentials({
  userId: 'user-123',
  platform: 'kalshi',
  credentials: {
    email: 'user@example.com',
    apiKey: 'key_...',
  },
});

// Binance期货
await creds.setCredentials({
  userId: 'user-123',
  platform: 'binance',
  credentials: {
    apiKey: 'abc...',
    apiSecret: 'xyz...',
  },
});

// Hyperliquid(钱包)
await creds.setCredentials({
  userId: 'user-123',
  platform: 'hyperliquid',
  credentials: {
    privateKey: '0x...',
    walletAddress: '0x...',
  },
});

Get Credentials

获取凭证

typescript
// Get for specific platform
const polymarketCreds = await creds.getCredentials({
  userId: 'user-123',
  platform: 'polymarket',
});

if (polymarketCreds) {
  console.log(`API Key: ${polymarketCreds.apiKey}`);
  // Credentials are decrypted on retrieval
}

// List user's configured platforms
const platforms = await creds.listUserPlatforms('user-123');
console.log(`Configured: ${platforms.join(', ')}`);
typescript
// 获取指定平台的凭证
const polymarketCreds = await creds.getCredentials({
  userId: 'user-123',
  platform: 'polymarket',
});

if (polymarketCreds) {
  console.log(`API密钥: ${polymarketCreds.apiKey}`);
  // 凭证在获取时自动解密
}

// 列出用户已配置的平台
const platforms = await creds.listUserPlatforms('user-123');
console.log(`已配置平台: ${platforms.join(', ')}`);

Delete Credentials

删除凭证

typescript
// Remove single platform
await creds.deleteCredentials({
  userId: 'user-123',
  platform: 'kalshi',
});

// Remove all for user
await creds.deleteAllCredentials('user-123');
typescript
// 删除单个平台的凭证
await creds.deleteCredentials({
  userId: 'user-123',
  platform: 'kalshi',
});

// 删除用户的所有凭证
await creds.deleteAllCredentials('user-123');

Test Credentials

测试凭证

typescript
// Test API connection
const result = await creds.testCredentials({
  userId: 'user-123',
  platform: 'polymarket',
});

if (result.success) {
  console.log(`✓ Connected to ${result.platform}`);
  console.log(`  Balance: $${result.balance}`);
} else {
  console.log(`✗ Failed: ${result.error}`);
}
typescript
// 测试API连接
const result = await creds.testCredentials({
  userId: 'user-123',
  platform: 'polymarket',
});

if (result.success) {
  console.log(`✓ 已连接到${result.platform}`);
  console.log(`  账户余额: $${result.balance}`);
} else {
  console.log(`✗ 连接失败: ${result.error}`);
}

Cooldown Management

冷却管理

typescript
// Mark failed auth attempt
await creds.markFailure({
  userId: 'user-123',
  platform: 'kalshi',
  error: 'Invalid API key',
});

// Check if in cooldown
const inCooldown = await creds.isInCooldown({
  userId: 'user-123',
  platform: 'kalshi',
});

if (inCooldown) {
  const remaining = await creds.getCooldownRemaining({
    userId: 'user-123',
    platform: 'kalshi',
  });
  console.log(`Cooldown: ${remaining} minutes remaining`);
}

// Mark successful auth (resets failures)
await creds.markSuccess({
  userId: 'user-123',
  platform: 'kalshi',
});
typescript
// 标记认证失败尝试
await creds.markFailure({
  userId: 'user-123',
  platform: 'kalshi',
  error: '无效API密钥',
});

// 检查是否处于冷却状态
const inCooldown = await creds.isInCooldown({
  userId: 'user-123',
  platform: 'kalshi',
});

if (inCooldown) {
  const remaining = await creds.getCooldownRemaining({
    userId: 'user-123',
    platform: 'kalshi',
  });
  console.log(`冷却中: 剩余${remaining}分钟`);
}

// 标记认证成功(重置失败计数)
await creds.markSuccess({
  userId: 'user-123',
  platform: 'kalshi',
});

Build Trading Context

构建交易上下文

typescript
// Get ready-to-use trading context
const context = await creds.buildTradingContext({
  userId: 'user-123',
  platform: 'polymarket',
});

// Context includes authenticated client
await context.client.getBalance();
await context.client.placeOrder({ ... });

typescript
// 获取可直接使用的交易上下文
const context = await creds.buildTradingContext({
  userId: 'user-123',
  platform: 'polymarket',
});

// 上下文包含已认证的客户端
await context.client.getBalance();
await context.client.placeOrder({ ... });

Supported Platforms

支持的平台

PlatformCredentials Required
PolymarketAPI key, secret, private key, funder address
KalshiEmail, API key
BetfairApp key, session token
SmarketsAPI key
BinanceAPI key, secret
BybitAPI key, secret
HyperliquidPrivate key, wallet address
MEXCAPI key, secret

平台所需凭证
PolymarketAPI密钥、密钥、私钥、出资方地址
Kalshi邮箱、API密钥
Betfair应用密钥、会话令牌
SmarketsAPI密钥
BinanceAPI密钥、密钥
BybitAPI密钥、密钥
Hyperliquid私钥、钱包地址
MEXCAPI密钥、密钥

Security Features

安全特性

FeatureDescription
AES-256-GCMMilitary-grade encryption at rest
Per-user keysIsolated credential storage
CooldownRate limits on failed attempts
No loggingSecrets never logged
Memory wipeCredentials cleared from memory after use

特性说明
AES-256-GCM军用级静态加密
按用户隔离密钥凭证存储相互隔离
冷却机制对失败尝试进行速率限制
无日志记录敏感信息永不记录
内存清除凭证使用后从内存中清除

Environment Variables

环境变量

bash
undefined
bash
undefined

Required encryption key (generate with: openssl rand -hex 32)

必填加密密钥(生成方式: openssl rand -hex 32)

CREDENTIALS_KEY=your-64-char-hex-key
CREDENTIALS_KEY=your-64-char-hex-key
// 可选:各平台独立密钥 POLYMARKET_API_KEY=pk_... POLYMARKET_API_SECRET=sk_... POLYMARKET_PRIVATE_KEY=0x... KALSHI_EMAIL=user@example.com KALSHI_API_KEY=key_...

---

Optional: per-platform keys

最佳实践

POLYMARKET_API_KEY=pk_... POLYMARKET_API_SECRET=sk_... POLYMARKET_PRIVATE_KEY=0x... KALSHI_EMAIL=user@example.com KALSHI_API_KEY=key_...

---
  1. 使用强加密密钥 — 使用
    openssl rand -hex 32
    生成
  2. 定期轮换密钥 — 定期更新API密钥
  3. 添加后测试 — 始终验证凭证有效性
  4. 最小权限原则 — 尽可能使用只读密钥
  5. 安全备份 — 离线保存加密备份

Best Practices

  1. Strong encryption key — Use
    openssl rand -hex 32
  2. Rotate keys regularly — Update API keys periodically
  3. Test after adding — Always verify credentials work
  4. Minimal permissions — Use read-only keys when possible
  5. Backup securely — Keep encrypted backups offline