@rabby-wallet/hyperliquid-sdk 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { ExchangeType } from '../types/constants';
2
- import type { OrderResponse, CancelResponse, ExchangeClientConfig, PlaceOrderParams, MultiOrderParams, CancelOrderParams, ModifyOrderParams, WithdrawParams, SendAssetParams, ApproveBuilderFeeParams, PrepareApproveBuilderFeeResult, SendApproveParams, MarketOrderParams, UpdateLeverageParams, BindTpslByOrderIdParams, UpdateIsolatedMarginParams, TwapOrderParams, TwapCancelParams, TwapOrderResponse, PlaceTPSlMarketOrderParams, PlaceTPSlLimitOrderParams, ClearinghouseState, UpdateTpslByOrderIdParams, LimitOrderParams, Abstraction, UserSetAbstractionParams, Tif } from '../types';
2
+ import type { OrderResponse, CancelResponse, ExchangeClientConfig, PlaceOrderParams, MultiOrderParams, CancelOrderParams, ModifyOrderParams, WithdrawParams, SendAssetParams, ApproveBuilderFeeParams, PrepareApproveBuilderFeeResult, SendApproveParams, MarketOrderParams, UpdateLeverageParams, BindTpslByOrderIdParams, UpdateIsolatedMarginParams, TwapOrderParams, TwapCancelParams, TwapOrderResponse, PlaceTPSlMarketOrderParams, PlaceTPSlLimitOrderParams, ClearinghouseState, UpdateTpslByOrderIdParams, LimitOrderParams, Abstraction, UserSetAbstractionParams, Tif, ExternalSign } from '../types';
3
3
  /**
4
4
  * Client for executing trades on Hyperliquid (perpetuals only)
5
5
  * Only includes essential trading APIs as specified
@@ -9,6 +9,7 @@ export declare class ExchangeClient {
9
9
  private agentPrivateKey?;
10
10
  private agentPublicKey?;
11
11
  private agentName?;
12
+ private externalSign?;
12
13
  private readonly isTestnet;
13
14
  slippage: number;
14
15
  private masterAddress;
@@ -24,6 +25,11 @@ export declare class ExchangeClient {
24
25
  */
25
26
  setSlippage(slippage?: number): void;
26
27
  initOrUpdateAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
28
+ /**
29
+ * Install (or clear with undefined) an external async signer. When present,
30
+ * L1 actions are signed through it instead of the built-in agent key.
31
+ */
32
+ setExternalSign(externalSign?: ExternalSign): void;
27
33
  /**
28
34
  * Check if agent is set
29
35
  */
@@ -33,6 +39,11 @@ export declare class ExchangeClient {
33
39
  */
34
40
  get address(): string;
35
41
  private getAgentPrivateKey;
42
+ /**
43
+ * Sign an L1 agent action. Prefers the external signer (async, master-wallet
44
+ * keyring) when set; otherwise falls back to the built-in agent private key.
45
+ */
46
+ private signL1Action;
36
47
  /**
37
48
  * @deprecated This method is deprecated. Use the new builder configuration in constructor instead.
38
49
  */
@@ -79,6 +79,9 @@ class ExchangeClient {
79
79
  if (config.agentName) {
80
80
  this.agentName = config.agentName;
81
81
  }
82
+ if (config.externalSign) {
83
+ this.externalSign = config.externalSign;
84
+ }
82
85
  this.isTestnet = config.isTestnet || false;
83
86
  }
84
87
  initAccount(masterAddress, agentPrivateKey, agentPublicKey, agentName) {
@@ -114,11 +117,18 @@ class ExchangeClient {
114
117
  this.agentPublicKey = agentPublicKey.startsWith('0x') ? agentPublicKey : ethUtil.addHexPrefix(agentPublicKey);
115
118
  this.agentName = agentName || '';
116
119
  }
120
+ /**
121
+ * Install (or clear with undefined) an external async signer. When present,
122
+ * L1 actions are signed through it instead of the built-in agent key.
123
+ */
124
+ setExternalSign(externalSign) {
125
+ this.externalSign = externalSign;
126
+ }
117
127
  /**
118
128
  * Check if agent is set
119
129
  */
120
130
  get isHaveAgent() {
121
- return !!this.agentPrivateKey && !!this.agentPublicKey;
131
+ return (!!this.agentPrivateKey && !!this.agentPublicKey) || !!this.externalSign;
122
132
  }
123
133
  /**
124
134
  * Get the wallet address
@@ -132,6 +142,24 @@ class ExchangeClient {
132
142
  }
133
143
  return this.agentPrivateKey;
134
144
  }
145
+ /**
146
+ * Sign an L1 agent action. Prefers the external signer (async, master-wallet
147
+ * keyring) when set; otherwise falls back to the built-in agent private key.
148
+ */
149
+ signL1Action(action, nonce) {
150
+ return __awaiter(this, void 0, void 0, function* () {
151
+ if (this.externalSign) {
152
+ const typedData = (0, signer_1.prepareL1ActionTypedData)({
153
+ action: action,
154
+ isTestnet: this.isTestnet,
155
+ nonce,
156
+ });
157
+ const hexSig = yield this.externalSign(typedData);
158
+ return (0, signer_1.splitSig)(hexSig);
159
+ }
160
+ return (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
161
+ });
162
+ }
135
163
  /**
136
164
  * @deprecated This method is deprecated. Use the new builder configuration in constructor instead.
137
165
  */
@@ -155,7 +183,7 @@ class ExchangeClient {
155
183
  isCross: params.isCross || false,
156
184
  leverage: params.leverage,
157
185
  };
158
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
186
+ const signature = yield this.signL1Action(action, nonce);
159
187
  return this.httpClient.exchange({
160
188
  action,
161
189
  nonce,
@@ -188,7 +216,7 @@ class ExchangeClient {
188
216
  f: params.builder.fee,
189
217
  };
190
218
  }
191
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
219
+ const signature = yield this.signL1Action(action, nonce);
192
220
  return this.httpClient.exchange({
193
221
  action,
194
222
  nonce,
@@ -521,7 +549,7 @@ class ExchangeClient {
521
549
  f: params.builder.fee,
522
550
  };
523
551
  }
524
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
552
+ const signature = yield this.signL1Action(action, nonce);
525
553
  const res = yield this.httpClient.exchange({
526
554
  action,
527
555
  nonce,
@@ -647,7 +675,7 @@ class ExchangeClient {
647
675
  type: constants_1.ExchangeType.CANCEL,
648
676
  cancels: cancels,
649
677
  };
650
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
678
+ const signature = yield this.signL1Action(action, nonce);
651
679
  return this.httpClient.exchange({
652
680
  action,
653
681
  nonce,
@@ -673,7 +701,7 @@ class ExchangeClient {
673
701
  t: params.orderType || { limit: { tif: 'Gtc' } },
674
702
  },
675
703
  };
676
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
704
+ const signature = yield this.signL1Action(action, nonce);
677
705
  return this.httpClient.exchange({
678
706
  action,
679
707
  nonce,
@@ -707,7 +735,7 @@ class ExchangeClient {
707
735
  type: constants_1.ExchangeType.BATCH_MODIFY,
708
736
  modifies,
709
737
  };
710
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
738
+ const signature = yield this.signL1Action(action, nonce);
711
739
  return this.httpClient.exchange({
712
740
  action,
713
741
  nonce,
@@ -725,7 +753,7 @@ class ExchangeClient {
725
753
  isBuy: true,
726
754
  ntli: (0, number_1.floatToInt)(params.value, 6), // 6 decimals
727
755
  };
728
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
756
+ const signature = yield this.signL1Action(action, nonce);
729
757
  return this.httpClient.exchange({
730
758
  action,
731
759
  nonce,
@@ -740,7 +768,7 @@ class ExchangeClient {
740
768
  type: constants_1.ExchangeType.SET_REFERRER,
741
769
  code,
742
770
  };
743
- const typedData = (0, signer_1.prepareSetReferrerTypedData)({ action, isTestnet: this.isTestnet, nonce });
771
+ const typedData = (0, signer_1.prepareL1ActionTypedData)({ action, isTestnet: this.isTestnet, nonce });
744
772
  return { typedData, action, nonce };
745
773
  }
746
774
  catch (error) {
@@ -766,7 +794,7 @@ class ExchangeClient {
766
794
  type: constants_1.ExchangeType.SET_REFERRER,
767
795
  code,
768
796
  };
769
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
797
+ const signature = yield this.signL1Action(action, nonce);
770
798
  return this.httpClient.exchange({
771
799
  action,
772
800
  nonce,
@@ -785,7 +813,7 @@ class ExchangeClient {
785
813
  const action = {
786
814
  type: constants_1.ExchangeType.AGENT_ENABLE_DEX_ABSTRACTION,
787
815
  };
788
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
816
+ const signature = yield this.signL1Action(action, nonce);
789
817
  return this.httpClient.exchange({
790
818
  action,
791
819
  nonce,
@@ -851,7 +879,7 @@ class ExchangeClient {
851
879
  type: constants_1.ExchangeType.AGENT_SET_ABSTRACTION,
852
880
  abstraction: abstraction,
853
881
  };
854
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
882
+ const signature = yield this.signL1Action(action, nonce);
855
883
  return this.httpClient.exchange({
856
884
  action,
857
885
  nonce,
@@ -1075,7 +1103,7 @@ class ExchangeClient {
1075
1103
  f: params.builder.fee,
1076
1104
  };
1077
1105
  }
1078
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
1106
+ const signature = yield this.signL1Action(action, nonce);
1079
1107
  return this.httpClient.exchange({
1080
1108
  action,
1081
1109
  nonce,
@@ -1095,7 +1123,7 @@ class ExchangeClient {
1095
1123
  a,
1096
1124
  t: params.twapId,
1097
1125
  };
1098
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
1126
+ const signature = yield this.signL1Action(action, nonce);
1099
1127
  return this.httpClient.exchange({
1100
1128
  action,
1101
1129
  nonce,
@@ -1138,7 +1166,7 @@ class ExchangeClient {
1138
1166
  orders: [orderRequest],
1139
1167
  grouping: 'na',
1140
1168
  };
1141
- const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
1169
+ const signature = yield this.signL1Action(action, nonce);
1142
1170
  return this.httpClient.exchange({
1143
1171
  action,
1144
1172
  nonce,
@@ -1,6 +1,6 @@
1
1
  import { InfoClient } from './client/info-client';
2
2
  import { ExchangeClient } from './client/exchange-client';
3
- import type { ClearinghouseState } from './types';
3
+ import type { ClearinghouseState, ExternalSign } from './types';
4
4
  import { WebSocketClient, WebSocketClientConfig } from './client/websocket-client';
5
5
  import { SymbolConversion } from './client/symbolConversion';
6
6
  export interface HyperliquidSDKConfig {
@@ -11,6 +11,7 @@ export interface HyperliquidSDKConfig {
11
11
  isTestnet?: boolean;
12
12
  timeout?: number;
13
13
  wsConfig?: WebSocketClientConfig;
14
+ externalSign?: ExternalSign;
14
15
  }
15
16
  /**
16
17
  * Main Hyperliquid SDK class for perpetuals trading
@@ -35,6 +36,11 @@ export declare class HyperliquidSDK {
35
36
  */
36
37
  get isHaveAgent(): boolean;
37
38
  initOrUpdateAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
39
+ /**
40
+ * Install (or clear with undefined) the external async signer on the exchange
41
+ * client. Call after initAccount when using master-wallet self-signing.
42
+ */
43
+ setExternalSign(externalSign?: ExternalSign): void;
38
44
  /**
39
45
  * Connect to WebSocket for real-time data
40
46
  */
@@ -39,6 +39,7 @@ class HyperliquidSDK {
39
39
  agentPrivateKey: config.agentPrivateKey,
40
40
  agentPublicKey: config.agentPublicKey,
41
41
  agentName: config.agentName,
42
+ externalSign: config.externalSign,
42
43
  isTestnet: config.isTestnet,
43
44
  timeout: config.timeout,
44
45
  symbolConversion: this.symbolConversion,
@@ -66,6 +67,7 @@ class HyperliquidSDK {
66
67
  agentPrivateKey,
67
68
  agentPublicKey,
68
69
  agentName,
70
+ externalSign: this.configParams.externalSign,
69
71
  isTestnet: this.configParams.isTestnet,
70
72
  timeout: this.configParams.timeout,
71
73
  });
@@ -86,6 +88,15 @@ class HyperliquidSDK {
86
88
  this.exchange.initOrUpdateAgent(agentPrivateKey, agentPublicKey, agentName);
87
89
  }
88
90
  }
91
+ /**
92
+ * Install (or clear with undefined) the external async signer on the exchange
93
+ * client. Call after initAccount when using master-wallet self-signing.
94
+ */
95
+ setExternalSign(externalSign) {
96
+ if (this.exchange) {
97
+ this.exchange.setExternalSign(externalSign);
98
+ }
99
+ }
89
100
  /**
90
101
  * Connect to WebSocket for real-time data
91
102
  */
@@ -1,4 +1,16 @@
1
1
  import { SymbolConversion } from '../client/symbolConversion';
2
+ import type { TypedMessage, MessageTypes } from '@metamask/eth-sig-util';
3
+ /**
4
+ * EIP-712 typed data handed to an external signer (shape of
5
+ * prepareL1ActionTypedData's return value).
6
+ */
7
+ export type ExternalSignTypedData = TypedMessage<MessageTypes>;
8
+ /**
9
+ * External async signer for L1 actions. Receives EIP-712 typed data and returns
10
+ * a 0x-prefixed 65-byte hex signature. When set, the SDK uses it instead of the
11
+ * built-in agent private key, so the master wallet can sign orders itself.
12
+ */
13
+ export type ExternalSign = (typedData: ExternalSignTypedData) => Promise<string>;
2
14
  export type Tif = 'Alo' | 'Ioc' | 'Gtc';
3
15
  export type TriggerType = 'tp' | 'sl';
4
16
  export type Side = 'A' | 'B';
@@ -526,6 +538,7 @@ export interface ExchangeClientConfig {
526
538
  isTestnet?: boolean;
527
539
  timeout?: number;
528
540
  symbolConversion?: SymbolConversion;
541
+ externalSign?: ExternalSign;
529
542
  }
530
543
  export interface PlaceOrderParams {
531
544
  coin: string;
@@ -23,7 +23,7 @@ export declare const signTypedDataV4: ({ privateKey, domain, primaryType, payloa
23
23
  message: Record<string, unknown>;
24
24
  }) => Signature;
25
25
  export declare const signL1AgentAction: (privateKey: string, action: unknown, isTestnet: boolean, nonce: number) => Signature;
26
- export declare const prepareSetReferrerTypedData: ({ action, isTestnet, nonce, }: {
26
+ export declare const prepareL1ActionTypedData: ({ action, isTestnet, nonce, }: {
27
27
  action: Record<string, unknown>;
28
28
  isTestnet: boolean;
29
29
  nonce: number;
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.prepareMasterSignData = exports.prepareSetReferrerTypedData = exports.signL1AgentAction = exports.signTypedDataV4 = exports.splitSig = exports.EIP712_DOMAIN_TYPES = void 0;
36
+ exports.prepareMasterSignData = exports.prepareL1ActionTypedData = exports.signL1AgentAction = exports.signTypedDataV4 = exports.splitSig = exports.EIP712_DOMAIN_TYPES = void 0;
37
37
  const ethUtil = __importStar(require("@ethereumjs/util"));
38
38
  const sigUtil = __importStar(require("@metamask/eth-sig-util"));
39
39
  const keccak_1 = require("ethereum-cryptography/keccak");
@@ -109,7 +109,7 @@ const signL1AgentAction = (privateKey, action, isTestnet, nonce) => {
109
109
  return (0, exports.signTypedDataV4)({ privateKey, domain, primaryType: 'Agent', payloadTypes, message });
110
110
  };
111
111
  exports.signL1AgentAction = signL1AgentAction;
112
- const prepareSetReferrerTypedData = ({ action, isTestnet, nonce, }) => {
112
+ const prepareL1ActionTypedData = ({ action, isTestnet, nonce, }) => {
113
113
  const domain = {
114
114
  name: 'Exchange',
115
115
  version: '1',
@@ -136,7 +136,7 @@ const prepareSetReferrerTypedData = ({ action, isTestnet, nonce, }) => {
136
136
  };
137
137
  return typedData;
138
138
  };
139
- exports.prepareSetReferrerTypedData = prepareSetReferrerTypedData;
139
+ exports.prepareL1ActionTypedData = prepareL1ActionTypedData;
140
140
  /**
141
141
  * Prepare some action sign must by master wallet so we need to prepare the typed data
142
142
  * Returns typed data that needs to be signed by the main wallet
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/hyperliquid-sdk",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "Simplified Hyperliquid Perpetuals Trading SDK for Frontend Applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",