pacifica-signer.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. * Pacifica平台签名器契约
  3. *
  4. * 定义Pacifica平台特定的签名接口和数据结构
  5. * 使用Ed25519算法进行签名操作
  6. */
  7. import { Platform, SignRequest, SignResponse, VerifyRequest, VerifyResponse } from './credential-manager';
  8. // ============================================================================
  9. // Pacifica特定接口
  10. // ============================================================================
  11. /**
  12. * Pacifica平台签名器接口
  13. */
  14. export interface IPacificaSigner {
  15. readonly platform: Platform.PACIFICA;
  16. /**
  17. * 对Pacifica订单进行签名
  18. * @param request Pacifica签名请求
  19. * @returns 签名结果,包含base64编码的Ed25519签名
  20. */
  21. signOrder(request: PacificaSignRequest): Promise<PacificaSignResponse>;
  22. /**
  23. * 验证Pacifica签名
  24. * @param request 验证请求
  25. * @returns 验证结果
  26. */
  27. verifySignature(request: PacificaVerifyRequest): Promise<PacificaVerifyResponse>;
  28. /**
  29. * 获取账户公钥
  30. * @param accountId 账户ID
  31. * @returns base58编码的公钥
  32. */
  33. getPublicKey(accountId: string): Promise<string>;
  34. /**
  35. * 批量签名(提高性能)
  36. * @param requests 批量签名请求
  37. * @returns 批量签名结果
  38. */
  39. signBatch(requests: PacificaSignRequest[]): Promise<PacificaSignResponse[]>;
  40. }
  41. // ============================================================================
  42. // Pacifica请求/响应类型
  43. // ============================================================================
  44. export interface PacificaSignRequest extends Omit<SignRequest, 'options'> {
  45. /**
  46. * 订单类型,影响签名格式
  47. */
  48. orderType: PacificaOrderType;
  49. /**
  50. * Pacifica特定选项
  51. */
  52. options?: PacificaSignOptions;
  53. }
  54. export interface PacificaSignOptions {
  55. /**
  56. * 签名超时时间(毫秒)
  57. * @default 30000
  58. */
  59. timeout?: number;
  60. /**
  61. * 是否包含时间戳
  62. * @default true
  63. */
  64. includeTimestamp?: boolean;
  65. /**
  66. * 签名编码格式
  67. * @default 'base64'
  68. */
  69. encoding?: 'base64' | 'base58' | 'hex';
  70. /**
  71. * 是否启用批量优化
  72. * @default false
  73. */
  74. enableBatchOptimization?: boolean;
  75. }
  76. export interface PacificaSignResponse extends SignResponse {
  77. /**
  78. * Pacifica签名格式,base64编码
  79. */
  80. signature: string;
  81. /**
  82. * 固定为'ed25519'
  83. */
  84. algorithm: 'ed25519';
  85. /**
  86. * 用于Pacifica API的公钥
  87. */
  88. publicKey: string;
  89. /**
  90. * 订单类型
  91. */
  92. orderType: PacificaOrderType;
  93. }
  94. export interface PacificaVerifyRequest extends VerifyRequest {
  95. /**
  96. * Pacifica公钥,base58编码
  97. */
  98. publicKey: string;
  99. /**
  100. * 订单类型
  101. */
  102. orderType?: PacificaOrderType;
  103. }
  104. export interface PacificaVerifyResponse extends VerifyResponse {
  105. /**
  106. * 固定为'ed25519'
  107. */
  108. algorithm: 'ed25519';
  109. /**
  110. * 验证的公钥
  111. */
  112. publicKey: string;
  113. }
  114. // ============================================================================
  115. // Pacifica特定枚举和常量
  116. // ============================================================================
  117. export enum PacificaOrderType {
  118. MARKET = 'market',
  119. LIMIT = 'limit',
  120. STOP_LOSS = 'stop_loss',
  121. TAKE_PROFIT = 'take_profit',
  122. CANCEL = 'cancel',
  123. CANCEL_ALL = 'cancel_all'
  124. }
  125. /**
  126. * Pacifica API常量
  127. */
  128. export const PACIFICA_CONSTANTS = {
  129. /**
  130. * Ed25519私钥长度(字节)
  131. */
  132. PRIVATE_KEY_LENGTH: 32,
  133. /**
  134. * Ed25519公钥长度(字节)
  135. */
  136. PUBLIC_KEY_LENGTH: 32,
  137. /**
  138. * Ed25519签名长度(字节)
  139. */
  140. SIGNATURE_LENGTH: 64,
  141. /**
  142. * 私钥十六进制字符串长度
  143. */
  144. PRIVATE_KEY_HEX_LENGTH: 64,
  145. /**
  146. * 公钥base58字符串典型长度
  147. */
  148. PUBLIC_KEY_BASE58_LENGTH: 44,
  149. /**
  150. * 签名base64字符串长度
  151. */
  152. SIGNATURE_BASE64_LENGTH: 88,
  153. /**
  154. * 最大消息大小(字节)
  155. */
  156. MAX_MESSAGE_SIZE: 1024 * 1024, // 1MB
  157. /**
  158. * 默认签名超时(毫秒)
  159. */
  160. DEFAULT_SIGN_TIMEOUT: 30000,
  161. /**
  162. * 最大批量签名数量
  163. */
  164. MAX_BATCH_SIZE: 100
  165. } as const;
  166. // ============================================================================
  167. // Pacifica消息格式
  168. // ============================================================================
  169. /**
  170. * Pacifica订单消息结构
  171. */
  172. export interface PacificaOrderMessage {
  173. /**
  174. * 订单类型
  175. */
  176. order_type: PacificaOrderType;
  177. /**
  178. * 交易对
  179. */
  180. symbol: string;
  181. /**
  182. * 订单方向
  183. */
  184. side: 'buy' | 'sell';
  185. /**
  186. * 数量
  187. */
  188. size: string;
  189. /**
  190. * 价格(限价单)
  191. */
  192. price?: string;
  193. /**
  194. * 客户端订单ID
  195. */
  196. client_id?: string;
  197. /**
  198. * 时间戳
  199. */
  200. timestamp: number;
  201. /**
  202. * 过期时间
  203. */
  204. expiry?: number;
  205. }
  206. /**
  207. * Pacifica取消订单消息
  208. */
  209. export interface PacificaCancelMessage {
  210. /**
  211. * 订单类型
  212. */
  213. order_type: 'cancel' | 'cancel_all';
  214. /**
  215. * 要取消的订单ID(单个取消)
  216. */
  217. order_id?: string;
  218. /**
  219. * 交易对(取消所有)
  220. */
  221. symbol?: string;
  222. /**
  223. * 时间戳
  224. */
  225. timestamp: number;
  226. }
  227. // ============================================================================
  228. // 辅助函数接口
  229. // ============================================================================
  230. /**
  231. * Pacifica消息序列化器
  232. */
  233. export interface IPacificaMessageSerializer {
  234. /**
  235. * 序列化订单消息为签名用的字节数组
  236. */
  237. serializeOrder(message: PacificaOrderMessage): Uint8Array;
  238. /**
  239. * 序列化取消消息为签名用的字节数组
  240. */
  241. serializeCancel(message: PacificaCancelMessage): Uint8Array;
  242. /**
  243. * 验证消息格式
  244. */
  245. validateMessage(message: PacificaOrderMessage | PacificaCancelMessage): boolean;
  246. }
  247. /**
  248. * Pacifica密钥工具
  249. */
  250. export interface IPacificaKeyUtils {
  251. /**
  252. * 从十六进制私钥生成公钥
  253. */
  254. derivePublicKey(privateKeyHex: string): Promise<string>;
  255. /**
  256. * 验证私钥格式
  257. */
  258. validatePrivateKey(privateKeyHex: string): boolean;
  259. /**
  260. * 验证公钥格式
  261. */
  262. validatePublicKey(publicKeyBase58: string): boolean;
  263. /**
  264. * 转换密钥格式
  265. */
  266. convertKeyFormat(
  267. key: string,
  268. from: 'hex' | 'base58' | 'base64',
  269. to: 'hex' | 'base58' | 'base64'
  270. ): string;
  271. }
  272. // ============================================================================
  273. // 错误类型
  274. // ============================================================================
  275. export class PacificaSignerError extends Error {
  276. constructor(
  277. message: string,
  278. public readonly code: PacificaErrorCode,
  279. public readonly details?: any
  280. ) {
  281. super(message);
  282. this.name = 'PacificaSignerError';
  283. }
  284. }
  285. export enum PacificaErrorCode {
  286. INVALID_PRIVATE_KEY = 'INVALID_PRIVATE_KEY',
  287. INVALID_PUBLIC_KEY = 'INVALID_PUBLIC_KEY',
  288. INVALID_MESSAGE = 'INVALID_MESSAGE',
  289. SIGNATURE_FAILED = 'SIGNATURE_FAILED',
  290. VERIFICATION_FAILED = 'VERIFICATION_FAILED',
  291. ACCOUNT_NOT_FOUND = 'ACCOUNT_NOT_FOUND',
  292. TIMEOUT = 'TIMEOUT',
  293. BATCH_SIZE_EXCEEDED = 'BATCH_SIZE_EXCEEDED',
  294. MESSAGE_TOO_LARGE = 'MESSAGE_TOO_LARGE'
  295. }
  296. // ============================================================================
  297. // 性能监控接口
  298. // ============================================================================
  299. export interface PacificaSignerMetrics {
  300. /**
  301. * 总签名次数
  302. */
  303. totalSignatures: number;
  304. /**
  305. * 成功签名次数
  306. */
  307. successfulSignatures: number;
  308. /**
  309. * 失败签名次数
  310. */
  311. failedSignatures: number;
  312. /**
  313. * 平均签名时间(毫秒)
  314. */
  315. averageSignTime: number;
  316. /**
  317. * 最大签名时间(毫秒)
  318. */
  319. maxSignTime: number;
  320. /**
  321. * 最小签名时间(毫秒)
  322. */
  323. minSignTime: number;
  324. /**
  325. * 批量签名次数
  326. */
  327. batchSignatures: number;
  328. /**
  329. * 平均批量大小
  330. */
  331. averageBatchSize: number;
  332. /**
  333. * 上次重置时间
  334. */
  335. lastResetAt: Date;
  336. }