PacificaSignerStrategy.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Pacifica签名策略适配器
  3. *
  4. * 将PacificaSigner适配为ISignerStrategy接口
  5. */
  6. import { ISignerStrategy } from '@/core/Signer';
  7. import { Platform, Credentials, PacificaCredentials } from '@/core/types';
  8. import { PacificaSigner, PacificaOrderType } from './PacificaSigner';
  9. export class PacificaSignerStrategy implements ISignerStrategy {
  10. public readonly platform = Platform.PACIFICA;
  11. private pacificaSigner = new PacificaSigner();
  12. public async sign(message: Uint8Array, credentials: Credentials): Promise<string> {
  13. if (credentials.type !== 'pacifica') {
  14. throw new Error('Invalid credentials type for Pacifica signer');
  15. }
  16. const pacificaCreds = credentials as PacificaCredentials;
  17. // 为PacificaSigner添加账户
  18. const tempAccountId = `temp_${Date.now()}`;
  19. this.pacificaSigner.addAccount(tempAccountId, pacificaCreds.privateKey);
  20. try {
  21. // 执行签名
  22. const result = await this.pacificaSigner.signOrder({
  23. accountId: tempAccountId,
  24. message,
  25. orderType: PacificaOrderType.LIMIT, // 默认订单类型
  26. });
  27. if (!result.success) {
  28. throw new Error(result.error || 'Signing failed');
  29. }
  30. return result.signature;
  31. } finally {
  32. // 清理临时账户
  33. this.pacificaSigner.removeAccount(tempAccountId);
  34. }
  35. }
  36. public async verify(message: Uint8Array, signature: string, publicKey: string): Promise<boolean> {
  37. const result = await this.pacificaSigner.verifySignature({
  38. accountId: 'verify',
  39. message,
  40. signature,
  41. publicKey,
  42. });
  43. return result.success && (result.isValid ?? false);
  44. }
  45. }