@nktkas/hyperliquid 0.22.2 → 0.23.1

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.
Files changed (256) hide show
  1. package/README.md +79 -67
  2. package/esm/mod.d.ts +1 -1
  3. package/esm/mod.d.ts.map +1 -1
  4. package/esm/src/base.d.ts +4 -1
  5. package/esm/src/base.d.ts.map +1 -1
  6. package/esm/src/base.js +2 -2
  7. package/esm/src/clients/exchange.d.ts +289 -207
  8. package/esm/src/clients/exchange.d.ts.map +1 -1
  9. package/esm/src/clients/exchange.js +605 -410
  10. package/esm/src/clients/info.d.ts +264 -48
  11. package/esm/src/clients/info.d.ts.map +1 -1
  12. package/esm/src/clients/info.js +273 -31
  13. package/esm/src/clients/multiSign.d.ts +285 -204
  14. package/esm/src/clients/multiSign.d.ts.map +1 -1
  15. package/esm/src/clients/multiSign.js +521 -493
  16. package/esm/src/clients/subscription.d.ts +38 -3
  17. package/esm/src/clients/subscription.d.ts.map +1 -1
  18. package/esm/src/clients/subscription.js +36 -3
  19. package/esm/src/signing/{_ethers.d.ts → _signTypedData/ethers.d.ts} +1 -1
  20. package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -0
  21. package/esm/src/signing/_signTypedData/mod.d.ts +35 -0
  22. package/esm/src/signing/_signTypedData/mod.d.ts.map +1 -0
  23. package/esm/src/signing/_signTypedData/mod.js +59 -0
  24. package/esm/src/signing/_signTypedData/private_key.d.ts +26 -0
  25. package/esm/src/signing/_signTypedData/private_key.d.ts.map +1 -0
  26. package/esm/src/signing/_signTypedData/private_key.js +144 -0
  27. package/esm/src/signing/{_viem.d.ts → _signTypedData/viem.d.ts} +2 -2
  28. package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -0
  29. package/{script/src/signing/_window.d.ts → esm/src/signing/_signTypedData/window.d.ts} +18 -12
  30. package/esm/src/signing/_signTypedData/window.d.ts.map +1 -0
  31. package/esm/src/signing/{_window.js → _signTypedData/window.js} +3 -2
  32. package/esm/src/signing/_sorter.d.ts +56 -82
  33. package/esm/src/signing/_sorter.d.ts.map +1 -1
  34. package/esm/src/signing/_sorter.js +36 -49
  35. package/esm/src/signing/mod.d.ts +151 -130
  36. package/esm/src/signing/mod.d.ts.map +1 -1
  37. package/esm/src/signing/mod.js +182 -228
  38. package/esm/src/transports/base.d.ts +1 -1
  39. package/esm/src/transports/base.d.ts.map +1 -1
  40. package/esm/src/transports/base.js +2 -2
  41. package/esm/src/transports/http/http_transport.d.ts +9 -15
  42. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  43. package/esm/src/transports/http/http_transport.js +64 -59
  44. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
  45. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  46. package/esm/src/transports/websocket/_hyperliquid_event_target.js +1 -1
  47. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  48. package/esm/src/transports/websocket/_reconnecting_websocket.js +17 -4
  49. package/esm/src/transports/websocket/_websocket_async_request.d.ts +0 -9
  50. package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  51. package/esm/src/transports/websocket/_websocket_async_request.js +14 -20
  52. package/esm/src/transports/websocket/websocket_transport.d.ts +8 -4
  53. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  54. package/esm/src/transports/websocket/websocket_transport.js +83 -54
  55. package/esm/src/types/exchange/requests.d.ts +27 -51
  56. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  57. package/esm/src/types/explorer/requests.d.ts +5 -8
  58. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  59. package/esm/src/types/info/accounts.d.ts +5 -5
  60. package/esm/src/types/info/accounts.d.ts.map +1 -1
  61. package/esm/src/types/info/markets.d.ts +6 -0
  62. package/esm/src/types/info/markets.d.ts.map +1 -1
  63. package/esm/src/types/info/orders.d.ts +1 -1
  64. package/esm/src/types/info/orders.d.ts.map +1 -1
  65. package/esm/src/types/info/requests.d.ts +129 -124
  66. package/esm/src/types/info/requests.d.ts.map +1 -1
  67. package/{script/src/types/info/delegations.d.ts → esm/src/types/info/validators.d.ts} +2 -2
  68. package/esm/src/types/info/validators.d.ts.map +1 -0
  69. package/esm/src/types/info/vaults.d.ts +7 -0
  70. package/esm/src/types/info/vaults.d.ts.map +1 -1
  71. package/esm/src/types/mod.d.ts +1 -1
  72. package/esm/src/types/mod.d.ts.map +1 -1
  73. package/esm/src/types/subscriptions/requests.d.ts +72 -18
  74. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  75. package/esm/src/types/subscriptions/responses.d.ts +2 -0
  76. package/esm/src/types/subscriptions/responses.d.ts.map +1 -1
  77. package/package.json +8 -5
  78. package/script/mod.d.ts +1 -1
  79. package/script/mod.d.ts.map +1 -1
  80. package/script/mod.js +13 -23
  81. package/script/src/base.d.ts +4 -1
  82. package/script/src/base.d.ts.map +1 -1
  83. package/script/src/base.js +10 -20
  84. package/script/src/clients/exchange.d.ts +289 -207
  85. package/script/src/clients/exchange.d.ts.map +1 -1
  86. package/script/src/clients/exchange.js +2000 -1815
  87. package/script/src/clients/info.d.ts +264 -48
  88. package/script/src/clients/info.d.ts.map +1 -1
  89. package/script/src/clients/info.js +1442 -1210
  90. package/script/src/clients/multiSign.d.ts +285 -204
  91. package/script/src/clients/multiSign.d.ts.map +1 -1
  92. package/script/src/clients/multiSign.js +2043 -2025
  93. package/script/src/clients/subscription.d.ts +38 -3
  94. package/script/src/clients/subscription.d.ts.map +1 -1
  95. package/script/src/clients/subscription.js +568 -545
  96. package/script/src/signing/{_ethers.d.ts → _signTypedData/ethers.d.ts} +1 -1
  97. package/script/src/signing/_signTypedData/ethers.d.ts.map +1 -0
  98. package/script/src/signing/_signTypedData/ethers.js +16 -0
  99. package/script/src/signing/_signTypedData/mod.d.ts +35 -0
  100. package/script/src/signing/_signTypedData/mod.d.ts.map +1 -0
  101. package/script/src/signing/_signTypedData/mod.js +67 -0
  102. package/script/src/signing/_signTypedData/private_key.d.ts +26 -0
  103. package/script/src/signing/_signTypedData/private_key.d.ts.map +1 -0
  104. package/script/src/signing/_signTypedData/private_key.js +148 -0
  105. package/script/src/signing/{_viem.d.ts → _signTypedData/viem.d.ts} +2 -2
  106. package/script/src/signing/_signTypedData/viem.d.ts.map +1 -0
  107. package/script/src/signing/_signTypedData/viem.js +9 -0
  108. package/{esm/src/signing/_window.d.ts → script/src/signing/_signTypedData/window.d.ts} +18 -12
  109. package/script/src/signing/_signTypedData/window.d.ts.map +1 -0
  110. package/script/src/signing/_signTypedData/window.js +34 -0
  111. package/script/src/signing/_sorter.d.ts +56 -82
  112. package/script/src/signing/_sorter.d.ts.map +1 -1
  113. package/script/src/signing/_sorter.js +655 -678
  114. package/script/src/signing/mod.d.ts +151 -130
  115. package/script/src/signing/mod.d.ts.map +1 -1
  116. package/script/src/signing/mod.js +286 -342
  117. package/script/src/transports/base.d.ts +1 -1
  118. package/script/src/transports/base.d.ts.map +1 -1
  119. package/script/src/transports/base.js +11 -21
  120. package/script/src/transports/http/http_transport.d.ts +9 -15
  121. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  122. package/script/src/transports/http/http_transport.js +97 -102
  123. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
  124. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  125. package/script/src/transports/websocket/_hyperliquid_event_target.js +51 -61
  126. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  127. package/script/src/transports/websocket/_reconnecting_websocket.js +271 -268
  128. package/script/src/transports/websocket/_websocket_async_request.d.ts +0 -9
  129. package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  130. package/script/src/transports/websocket/_websocket_async_request.js +157 -174
  131. package/script/src/transports/websocket/websocket_transport.d.ts +8 -4
  132. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  133. package/script/src/transports/websocket/websocket_transport.js +209 -189
  134. package/script/src/types/exchange/requests.d.ts +27 -51
  135. package/script/src/types/exchange/requests.d.ts.map +1 -1
  136. package/script/src/types/exchange/requests.js +2 -12
  137. package/script/src/types/exchange/responses.js +2 -12
  138. package/script/src/types/explorer/requests.d.ts +5 -8
  139. package/script/src/types/explorer/requests.d.ts.map +1 -1
  140. package/script/src/types/explorer/requests.js +2 -12
  141. package/script/src/types/explorer/responses.js +2 -12
  142. package/script/src/types/info/accounts.d.ts +5 -5
  143. package/script/src/types/info/accounts.d.ts.map +1 -1
  144. package/script/src/types/info/accounts.js +2 -12
  145. package/script/src/types/info/assets.js +2 -12
  146. package/script/src/types/info/markets.d.ts +6 -0
  147. package/script/src/types/info/markets.d.ts.map +1 -1
  148. package/script/src/types/info/markets.js +2 -12
  149. package/script/src/types/info/orders.d.ts +1 -1
  150. package/script/src/types/info/orders.d.ts.map +1 -1
  151. package/script/src/types/info/orders.js +2 -12
  152. package/script/src/types/info/requests.d.ts +129 -124
  153. package/script/src/types/info/requests.d.ts.map +1 -1
  154. package/script/src/types/info/requests.js +2 -12
  155. package/{esm/src/types/info/delegations.d.ts → script/src/types/info/validators.d.ts} +2 -2
  156. package/script/src/types/info/validators.d.ts.map +1 -0
  157. package/script/src/types/info/validators.js +2 -0
  158. package/script/src/types/info/vaults.d.ts +7 -0
  159. package/script/src/types/info/vaults.d.ts.map +1 -1
  160. package/script/src/types/info/vaults.js +2 -12
  161. package/script/src/types/mod.d.ts +1 -1
  162. package/script/src/types/mod.d.ts.map +1 -1
  163. package/script/src/types/mod.js +2 -12
  164. package/script/src/types/subscriptions/requests.d.ts +72 -18
  165. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  166. package/script/src/types/subscriptions/requests.js +2 -12
  167. package/script/src/types/subscriptions/responses.d.ts +2 -0
  168. package/script/src/types/subscriptions/responses.d.ts.map +1 -1
  169. package/script/src/types/subscriptions/responses.js +2 -12
  170. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
  171. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
  172. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -1
  173. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
  174. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
  175. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -10
  176. package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
  177. package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
  178. package/esm/deps/jsr.io/@std/async/1.0.13/delay.js +0 -63
  179. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
  180. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
  181. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -2
  182. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
  183. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
  184. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -32
  185. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  186. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  187. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -51
  188. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  189. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  190. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -13
  191. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  192. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  193. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -2
  194. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  195. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  196. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -87
  197. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
  198. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
  199. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -2
  200. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
  201. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
  202. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -237
  203. package/esm/src/signing/_ethers.d.ts.map +0 -1
  204. package/esm/src/signing/_private_key.d.ts +0 -22
  205. package/esm/src/signing/_private_key.d.ts.map +0 -1
  206. package/esm/src/signing/_private_key.js +0 -124
  207. package/esm/src/signing/_viem.d.ts.map +0 -1
  208. package/esm/src/signing/_window.d.ts.map +0 -1
  209. package/esm/src/types/info/delegations.d.ts.map +0 -1
  210. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
  211. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
  212. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -27
  213. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
  214. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
  215. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -24
  216. package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
  217. package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
  218. package/script/deps/jsr.io/@std/async/1.0.13/delay.js +0 -76
  219. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
  220. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
  221. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -13
  222. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
  223. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
  224. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -45
  225. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  226. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  227. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -67
  228. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  229. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  230. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -26
  231. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  232. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  233. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -13
  234. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  235. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  236. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -101
  237. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
  238. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
  239. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -13
  240. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
  241. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
  242. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -250
  243. package/script/src/signing/_ethers.d.ts.map +0 -1
  244. package/script/src/signing/_ethers.js +0 -26
  245. package/script/src/signing/_private_key.d.ts +0 -22
  246. package/script/src/signing/_private_key.d.ts.map +0 -1
  247. package/script/src/signing/_private_key.js +0 -138
  248. package/script/src/signing/_viem.d.ts.map +0 -1
  249. package/script/src/signing/_viem.js +0 -19
  250. package/script/src/signing/_window.d.ts.map +0 -1
  251. package/script/src/signing/_window.js +0 -43
  252. package/script/src/types/info/delegations.d.ts.map +0 -1
  253. package/script/src/types/info/delegations.js +0 -12
  254. /package/esm/src/signing/{_ethers.js → _signTypedData/ethers.js} +0 -0
  255. /package/esm/src/signing/{_viem.js → _signTypedData/viem.js} +0 -0
  256. /package/esm/src/types/info/{delegations.js → validators.js} +0 -0
@@ -1,49 +1,85 @@
1
+ "use strict";
1
2
  /**
2
3
  * This module contains functions for generating Hyperliquid transaction signatures
3
4
  * and interfaces to various wallet implementations.
4
5
  *
5
- * @example
6
+ * @example Signing an L1 action
6
7
  * ```ts
7
- * import { signL1Action } from "@nktkas/hyperliquid/signing";
8
+ * import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
9
+ *
10
+ * const privateKey = "0x..."; // or `viem`, `ethers`
8
11
  *
9
- * const action = {
10
- * type: "cancel",
11
- * cancels: [{ a: 0, o: 12345 }],
12
- * };
13
12
  * const nonce = Date.now();
13
+ * const action = {
14
+ * type: "cancel",
15
+ * cancels: [
16
+ * { a: 0, o: 12345 },
17
+ * ],
18
+ * } as const;
14
19
  *
15
20
  * const signature = await signL1Action({
16
- * wallet,
17
- * action,
18
- * nonce,
19
- * isTestnet: true, // Change to false for mainnet
21
+ * wallet: privateKey,
22
+ * action: actionSorter[action.type](action),
23
+ * nonce,
20
24
  * });
21
25
  * ```
22
- * @example
26
+ *
27
+ * @example Signing a user-signed action
23
28
  * ```ts
24
- * import { signUserSignedAction } from "@nktkas/hyperliquid/signing";
29
+ * import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
30
+ *
31
+ * const privateKey = "0x..."; // or `viem`, `ethers`
25
32
  *
26
33
  * const action = {
27
- * type: "approveAgent",
28
- * hyperliquidChain: "Testnet", // "Mainnet" or "Testnet"
29
- * signatureChainId: "0x66eee",
30
- * nonce: Date.now(),
31
- * agentAddress: "0x...",
32
- * agentName: "Agent",
33
- * };
34
+ * type: "approveAgent",
35
+ * signatureChainId: "0x66eee",
36
+ * hyperliquidChain: "Mainnet",
37
+ * agentAddress: "0x...",
38
+ * agentName: "Agent",
39
+ * nonce: Date.now(),
40
+ * } as const;
34
41
  *
35
42
  * const signature = await signUserSignedAction({
36
- * wallet,
37
- * action,
38
- * types: {
39
- * "HyperliquidTransaction:ApproveAgent": [
40
- * { name: "hyperliquidChain", type: "string" },
41
- * { name: "agentAddress", type: "address" },
42
- * { name: "agentName", type: "string" },
43
- * { name: "nonce", type: "uint64" },
44
- * ],
45
- * },
46
- * chainId: parseInt(action.signatureChainId, 16),
43
+ * wallet: privateKey,
44
+ * action,
45
+ * types: userSignedActionEip712Types[action.type],
46
+ * });
47
+ * ```
48
+ *
49
+ * @example Signing a multi-signature action
50
+ * ```ts
51
+ * import { actionSorter, signL1Action, signMultiSigAction } from "@nktkas/hyperliquid/signing";
52
+ * import { privateKeyToAccount } from "viem/accounts";
53
+ *
54
+ * const wallet = privateKeyToAccount("0x..."); // or `ethers`, private key with address
55
+ * const multiSigUser = "0x...";
56
+ *
57
+ * const nonce = Date.now();
58
+ * const action = {
59
+ * type: "scheduleCancel",
60
+ * time: Date.now() + 10000,
61
+ * } as const;
62
+ *
63
+ * // First, create the required number of signatures
64
+ * const signature = await signL1Action({
65
+ * wallet,
66
+ * action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), actionSorter[action.type](action)],
67
+ * nonce,
68
+ * });
69
+ *
70
+ * // Then use signatures in the multi-sig action
71
+ * const multiSigSignature = await signMultiSigAction({
72
+ * wallet,
73
+ * action: {
74
+ * signatureChainId: "0x66eee",
75
+ * signatures: [signature],
76
+ * payload: {
77
+ * multiSigUser,
78
+ * outerSigner: wallet.address,
79
+ * action,
80
+ * },
81
+ * },
82
+ * nonce,
47
83
  * });
48
84
  * ```
49
85
  *
@@ -63,325 +99,233 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
63
99
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
64
100
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
65
101
  };
66
- (function (factory) {
67
- if (typeof module === "object" && typeof module.exports === "object") {
68
- var v = factory(require, exports);
69
- if (v !== undefined) module.exports = v;
70
- }
71
- else if (typeof define === "function" && define.amd) {
72
- define(["require", "exports", "@noble/hashes/sha3", "../../deps/jsr.io/@std/msgpack/1.0.3/encode.js", "../../deps/jsr.io/@std/encoding/1.0.10/hex.js", "../../deps/jsr.io/@std/bytes/1.0.6/concat.js", "./_ethers.js", "./_private_key.js", "./_viem.js", "./_window.js", "./_sorter.js"], factory);
73
- }
74
- })(function (require, exports) {
75
- "use strict";
76
- Object.defineProperty(exports, "__esModule", { value: true });
77
- exports.isValidPrivateKey = exports.isAbstractWindowEthereum = exports.isAbstractViemWalletClient = exports.isAbstractEthersV5Signer = exports.isAbstractEthersSigner = void 0;
78
- exports.createL1ActionHash = createL1ActionHash;
79
- exports.signL1Action = signL1Action;
80
- exports.signUserSignedAction = signUserSignedAction;
81
- exports.signMultiSigAction = signMultiSigAction;
82
- const sha3_1 = require("@noble/hashes/sha3");
83
- const encode_js_1 = require("../../deps/jsr.io/@std/msgpack/1.0.3/encode.js");
84
- const hex_js_1 = require("../../deps/jsr.io/@std/encoding/1.0.10/hex.js");
85
- const concat_js_1 = require("../../deps/jsr.io/@std/bytes/1.0.6/concat.js");
86
- const _ethers_js_1 = require("./_ethers.js");
87
- Object.defineProperty(exports, "isAbstractEthersSigner", { enumerable: true, get: function () { return _ethers_js_1.isAbstractEthersSigner; } });
88
- Object.defineProperty(exports, "isAbstractEthersV5Signer", { enumerable: true, get: function () { return _ethers_js_1.isAbstractEthersV5Signer; } });
89
- const _private_key_js_1 = require("./_private_key.js");
90
- Object.defineProperty(exports, "isValidPrivateKey", { enumerable: true, get: function () { return _private_key_js_1.isValidPrivateKey; } });
91
- const _viem_js_1 = require("./_viem.js");
92
- Object.defineProperty(exports, "isAbstractViemWalletClient", { enumerable: true, get: function () { return _viem_js_1.isAbstractViemWalletClient; } });
93
- const _window_js_1 = require("./_window.js");
94
- Object.defineProperty(exports, "isAbstractWindowEthereum", { enumerable: true, get: function () { return _window_js_1.isAbstractWindowEthereum; } });
95
- __exportStar(require("./_sorter.js"), exports);
96
- /**
97
- * Create a hash of the L1 action.
98
- *
99
- * Note: Hash generation depends on the order of the action keys.
100
- *
101
- * @param action - The action to be hashed.
102
- * @param nonce - Unique request identifier (recommended current timestamp in ms).
103
- * @param vaultAddress - Optional vault address used in the action.
104
- * @param expiresAfter - Optional expiration time of the action in milliseconds since the epoch.
105
- * @returns The hash of the action.
106
- */
107
- function createL1ActionHash(action, nonce, vaultAddress, expiresAfter) {
108
- // 1. Action
109
- const actionBytes = (0, encode_js_1.encode)(normalizeIntegersForMsgPack(action));
110
- // 2. Nonce
111
- const nonceBytes = new Uint8Array(8);
112
- new DataView(nonceBytes.buffer).setBigUint64(0, BigInt(nonce));
113
- // 3. Vault address
114
- const vaultMarker = vaultAddress ? Uint8Array.of(1) : Uint8Array.of(0);
115
- const vaultBytes = vaultAddress ? (0, hex_js_1.decodeHex)(vaultAddress.slice(2)) : new Uint8Array();
116
- // 4. Expires after
117
- let expiresMarker;
118
- let expiresBytes;
119
- if (expiresAfter !== undefined) {
120
- expiresMarker = Uint8Array.of(0);
121
- expiresBytes = new Uint8Array(8);
122
- new DataView(expiresBytes.buffer).setBigUint64(0, BigInt(expiresAfter));
123
- }
124
- else {
125
- expiresMarker = new Uint8Array();
126
- expiresBytes = new Uint8Array();
127
- }
128
- // Create a keccak256 hash
129
- const bytes = (0, concat_js_1.concat)([
130
- actionBytes,
131
- nonceBytes,
132
- vaultMarker,
133
- vaultBytes,
134
- expiresMarker,
135
- expiresBytes,
136
- ]);
137
- const hash = (0, sha3_1.keccak_256)(bytes);
138
- return `0x${(0, hex_js_1.encodeHex)(hash)}`;
139
- }
140
- /** Layer to make {@link https://jsr.io/@std/msgpack | @std/msgpack} compatible with {@link https://github.com/msgpack/msgpack-javascript | @msgpack/msgpack}. */
141
- function normalizeIntegersForMsgPack(obj) {
142
- const THIRTY_ONE_BITS = 2147483648;
143
- const THIRTY_TWO_BITS = 4294967296;
144
- if (typeof obj === "number" && Number.isInteger(obj) &&
145
- obj <= Number.MAX_SAFE_INTEGER && obj >= Number.MIN_SAFE_INTEGER &&
146
- (obj >= THIRTY_TWO_BITS || obj < -THIRTY_ONE_BITS)) {
147
- return BigInt(obj);
148
- }
149
- if (Array.isArray(obj)) {
150
- return obj.map(normalizeIntegersForMsgPack);
151
- }
152
- if (obj && typeof obj === "object" && obj !== null) {
153
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, normalizeIntegersForMsgPack(value)]));
154
- }
155
- return obj;
156
- }
157
- /**
158
- * Sign an L1 action.
159
- *
160
- * Note: Signature generation depends on the order of the action keys.
161
- * @param args - Arguments for signing the action.
162
- * @returns The signature components r, s, and v.
163
- * @example
164
- * ```ts
165
- * import { signL1Action } from "@nktkas/hyperliquid/signing";
166
- *
167
- * const privateKey = "0x..."; // or `viem`, `ethers`
168
- *
169
- * const action = {
170
- * type: "cancel",
171
- * cancels: [
172
- * { a: 0, o: 12345 }, // Asset index and order ID
173
- * ],
174
- * };
175
- * const nonce = Date.now();
176
- *
177
- * const signature = await signL1Action({
178
- * wallet: privateKey,
179
- * action,
180
- * nonce,
181
- * isTestnet: true, // Change to false for mainnet
182
- * });
183
- *
184
- * const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
185
- * method: "POST",
186
- * headers: { "Content-Type": "application/json" },
187
- * body: JSON.stringify({ action, signature, nonce }),
188
- * });
189
- * const body = await response.json();
190
- * ```
191
- */
192
- async function signL1Action(args) {
193
- const { wallet, action, nonce, isTestnet = false, vaultAddress, expiresAfter, } = args;
194
- const domain = {
102
+ Object.defineProperty(exports, "__esModule", { value: true });
103
+ exports.isValidPrivateKey = exports.isAbstractWindowEthereum = exports.isAbstractViemWalletClient = exports.isAbstractEthersV5Signer = exports.isAbstractEthersSigner = void 0;
104
+ exports.createL1ActionHash = createL1ActionHash;
105
+ exports.signL1Action = signL1Action;
106
+ exports.signUserSignedAction = signUserSignedAction;
107
+ exports.signMultiSigAction = signMultiSigAction;
108
+ const sha3_1 = require("@noble/hashes/sha3");
109
+ const secp256k1_1 = require("@noble/secp256k1");
110
+ const msgpack_1 = require("@msgpack/msgpack");
111
+ const mod_js_1 = require("./_signTypedData/mod.js");
112
+ Object.defineProperty(exports, "isAbstractEthersSigner", { enumerable: true, get: function () { return mod_js_1.isAbstractEthersSigner; } });
113
+ Object.defineProperty(exports, "isAbstractEthersV5Signer", { enumerable: true, get: function () { return mod_js_1.isAbstractEthersV5Signer; } });
114
+ Object.defineProperty(exports, "isAbstractViemWalletClient", { enumerable: true, get: function () { return mod_js_1.isAbstractViemWalletClient; } });
115
+ Object.defineProperty(exports, "isAbstractWindowEthereum", { enumerable: true, get: function () { return mod_js_1.isAbstractWindowEthereum; } });
116
+ Object.defineProperty(exports, "isValidPrivateKey", { enumerable: true, get: function () { return mod_js_1.isValidPrivateKey; } });
117
+ __exportStar(require("./_sorter.js"), exports);
118
+ /**
119
+ * Create a hash of the L1 action.
120
+ * @example
121
+ * ```ts
122
+ * import { actionSorter, createL1ActionHash } from "@nktkas/hyperliquid/signing";
123
+ *
124
+ * const action = {
125
+ * type: "cancel",
126
+ * cancels: [
127
+ * { a: 0, o: 12345 },
128
+ * ],
129
+ * } as const;
130
+ * const nonce = Date.now();
131
+ *
132
+ * const actionHash = createL1ActionHash({
133
+ * action: actionSorter[action.type](action),
134
+ * nonce,
135
+ * vaultAddress: "0x...", // optional
136
+ * expiresAfter: Date.now() + 10_000, // optional
137
+ * });
138
+ * ```
139
+ */
140
+ function createL1ActionHash(args) {
141
+ const { action, nonce, vaultAddress, expiresAfter } = args;
142
+ // 1. Action
143
+ const actionBytes = (0, msgpack_1.encode)(action);
144
+ // 2. Nonce
145
+ const nonceBytes = toUint64Bytes(nonce);
146
+ // 3. Vault address
147
+ const vaultMarker = vaultAddress ? new Uint8Array([1]) : new Uint8Array([0]);
148
+ const vaultBytes = vaultAddress ? secp256k1_1.etc.hexToBytes(vaultAddress.slice(2)) : new Uint8Array();
149
+ // 4. Expires after
150
+ const expiresMarker = expiresAfter !== undefined ? new Uint8Array([0]) : new Uint8Array();
151
+ const expiresBytes = expiresAfter !== undefined ? toUint64Bytes(expiresAfter) : new Uint8Array();
152
+ // Create a hash
153
+ const bytes = secp256k1_1.etc.concatBytes(actionBytes, nonceBytes, vaultMarker, vaultBytes, expiresMarker, expiresBytes);
154
+ const hash = (0, sha3_1.keccak_256)(bytes);
155
+ return `0x${secp256k1_1.etc.bytesToHex(hash)}`;
156
+ }
157
+ function toUint64Bytes(n) {
158
+ const bytes = new Uint8Array(8);
159
+ new DataView(bytes.buffer).setBigUint64(0, BigInt(n));
160
+ return bytes;
161
+ }
162
+ /**
163
+ * Sign an L1 action.
164
+ * @example
165
+ * ```ts
166
+ * import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
167
+ *
168
+ * const privateKey = "0x..."; // or `viem`, `ethers`
169
+ *
170
+ * const nonce = Date.now();
171
+ * const action = {
172
+ * type: "cancel",
173
+ * cancels: [
174
+ * { a: 0, o: 12345 },
175
+ * ],
176
+ * } as const;
177
+ *
178
+ * const signature = await signL1Action({
179
+ * wallet: privateKey,
180
+ * action: actionSorter[action.type](action),
181
+ * nonce,
182
+ * });
183
+ *
184
+ * // Send the signed action to the Hyperliquid API
185
+ * const response = await fetch("https://api.hyperliquid.xyz/exchange", {
186
+ * method: "POST",
187
+ * headers: { "Content-Type": "application/json" },
188
+ * body: JSON.stringify({ action, signature, nonce }),
189
+ * });
190
+ * const body = await response.json();
191
+ * ```
192
+ */
193
+ async function signL1Action(args) {
194
+ const { wallet, action, nonce, isTestnet = false, vaultAddress, expiresAfter, } = args;
195
+ const actionHash = createL1ActionHash({ action, nonce, vaultAddress, expiresAfter });
196
+ const message = {
197
+ source: isTestnet ? "b" : "a",
198
+ connectionId: actionHash,
199
+ };
200
+ return await (0, mod_js_1.signTypedData)({
201
+ wallet,
202
+ domain: {
195
203
  name: "Exchange",
196
204
  version: "1",
197
- chainId: 1337, // hyperliquid requires a fixed chain
205
+ chainId: 1337, // hyperliquid requires chainId to be 1337
198
206
  verifyingContract: "0x0000000000000000000000000000000000000000",
199
- };
200
- const types = {
207
+ },
208
+ types: {
201
209
  Agent: [
202
210
  { name: "source", type: "string" },
203
211
  { name: "connectionId", type: "bytes32" },
204
212
  ],
205
- };
206
- const actionHash = createL1ActionHash(action, nonce, vaultAddress, expiresAfter);
207
- const message = {
208
- source: isTestnet ? "b" : "a",
209
- connectionId: actionHash,
210
- };
211
- const signature = await abstractSignTypedData({ wallet, domain, types, message });
212
- return splitSignature(signature);
213
- }
214
- /**
215
- * Sign a user-signed action.
216
- *
217
- * Note: Signature generation depends on the order of types.
218
- *
219
- * @param args - Arguments for signing the action.
220
- * @returns The signature components r, s, and v.
221
- * @example
222
- * ```ts
223
- * import { signUserSignedAction } from "@nktkas/hyperliquid/signing";
224
- *
225
- * const privateKey = "0x..."; // or `viem`, `ethers`
226
- *
227
- * const action = {
228
- * type: "approveAgent",
229
- * hyperliquidChain: "Testnet", // "Mainnet" or "Testnet"
230
- * signatureChainId: "0x66eee",
231
- * nonce: Date.now(),
232
- * agentAddress: "0x...", // Change to your agent address
233
- * agentName: "Agent",
234
- * };
235
- *
236
- * const signature = await signUserSignedAction({
237
- * wallet: privateKey,
238
- * action,
239
- * types: {
240
- * "HyperliquidTransaction:ApproveAgent": [
241
- * { name: "hyperliquidChain", type: "string" },
242
- * { name: "agentAddress", type: "address" },
243
- * { name: "agentName", type: "string" },
244
- * { name: "nonce", type: "uint64" },
245
- * ],
246
- * },
247
- * chainId: parseInt(action.signatureChainId, 16),
248
- * });
249
- *
250
- * const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
251
- * method: "POST",
252
- * headers: { "Content-Type": "application/json" },
253
- * body: JSON.stringify({ action, signature, nonce: action.nonce }),
254
- * });
255
- * const body = await response.json();
256
- * ```
257
- */
258
- async function signUserSignedAction(args) {
259
- const { wallet, action, types, chainId } = args;
260
- const domain = {
213
+ },
214
+ primaryType: "Agent",
215
+ message,
216
+ });
217
+ }
218
+ /**
219
+ * Sign a user-signed action.
220
+ * @example
221
+ * ```ts
222
+ * import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
223
+ *
224
+ * const privateKey = "0x..."; // or `viem`, `ethers`
225
+ *
226
+ * const action = {
227
+ * type: "approveAgent",
228
+ * signatureChainId: "0x66eee",
229
+ * hyperliquidChain: "Mainnet",
230
+ * agentAddress: "0x...",
231
+ * agentName: "Agent",
232
+ * nonce: Date.now(),
233
+ * } as const;
234
+ *
235
+ * const signature = await signUserSignedAction({
236
+ * wallet: privateKey,
237
+ * action,
238
+ * types: userSignedActionEip712Types[action.type],
239
+ * });
240
+ *
241
+ * // Send the signed action to the Hyperliquid API
242
+ * const response = await fetch("https://api.hyperliquid.xyz/exchange", {
243
+ * method: "POST",
244
+ * headers: { "Content-Type": "application/json" },
245
+ * body: JSON.stringify({ action, signature, nonce: action.nonce }),
246
+ * });
247
+ * const body = await response.json();
248
+ * ```
249
+ */
250
+ async function signUserSignedAction(args) {
251
+ const { wallet, action, types } = args;
252
+ return await (0, mod_js_1.signTypedData)({
253
+ wallet,
254
+ domain: {
261
255
  name: "HyperliquidSignTransaction",
262
256
  version: "1",
263
- chainId,
257
+ chainId: parseInt(action.signatureChainId),
264
258
  verifyingContract: "0x0000000000000000000000000000000000000000",
265
- };
266
- const signature = await abstractSignTypedData({ wallet, domain, types, message: action });
267
- return splitSignature(signature);
268
- }
269
- /**
270
- * Sign a multi-signature action.
271
- *
272
- * Note: Signature generation depends on the order of the action keys.
273
- *
274
- * @param args - Arguments for signing the action.
275
- * @returns The signature components r, s, and v.
276
- * @example
277
- * ```ts
278
- * import { signL1Action, signMultiSigAction } from "@nktkas/hyperliquid/signing";
279
- * import { privateKeyToAccount } from "viem/accounts";
280
- *
281
- * const wallet = privateKeyToAccount("0x...");
282
- * const multiSigUser = "0x..."; // Multi-sig user address
283
- *
284
- * const nonce = Date.now();
285
- * const action = { // Example action
286
- * type: "scheduleCancel",
287
- * time: Date.now() + 10000
288
- * };
289
- *
290
- * // First, create signature from one of the authorized signers
291
- * const signature = await signL1Action({
292
- * wallet,
293
- * action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), action],
294
- * nonce,
295
- * isTestnet: true,
296
- * });
297
- *
298
- * // Then use it in the multi-sig action
299
- * const multiSigSignature = await signMultiSigAction({
300
- * wallet,
301
- * action: {
302
- * type: "multiSig",
303
- * signatureChainId: "0x66eee",
304
- * signatures: [signature],
305
- * payload: {
306
- * multiSigUser,
307
- * outerSigner: wallet.address,
308
- * action,
309
- * }
310
- * },
311
- * nonce,
312
- * hyperliquidChain: "Testnet",
313
- * signatureChainId: "0x66eee",
314
- * });
315
- * ```
316
- */
317
- async function signMultiSigAction(args) {
318
- const { wallet, action, nonce, hyperliquidChain, signatureChainId, vaultAddress, expiresAfter, } = args;
319
- const multiSigActionHash = createL1ActionHash(action, nonce, vaultAddress, expiresAfter);
320
- const message = {
321
- multiSigActionHash,
322
- hyperliquidChain,
323
- signatureChainId,
324
- nonce,
325
- };
326
- return await signUserSignedAction({
327
- wallet,
328
- action: message,
329
- types: {
330
- "HyperliquidTransaction:SendMultiSig": [
331
- { name: "hyperliquidChain", type: "string" },
332
- { name: "multiSigActionHash", type: "bytes32" },
333
- { name: "nonce", type: "uint64" },
334
- ],
335
- },
336
- chainId: parseInt(signatureChainId, 16),
337
- });
338
- }
339
- /** Signs typed data with the provided wallet using EIP-712. */
340
- async function abstractSignTypedData(args) {
341
- const { wallet, domain, types, message } = args;
342
- if ((0, _private_key_js_1.isValidPrivateKey)(wallet)) {
343
- return await (0, _private_key_js_1.signTypedDataWithPrivateKey)({
344
- privateKey: wallet,
345
- domain,
346
- types,
347
- primaryType: Object.keys(types)[0],
348
- message,
349
- });
350
- }
351
- else if ((0, _viem_js_1.isAbstractViemWalletClient)(wallet)) {
352
- return await wallet.signTypedData({
353
- domain,
354
- types: {
355
- EIP712Domain: [
356
- { name: "name", type: "string" },
357
- { name: "version", type: "string" },
358
- { name: "chainId", type: "uint256" },
359
- { name: "verifyingContract", type: "address" },
360
- ],
361
- ...types,
362
- },
363
- primaryType: Object.keys(types)[0],
364
- message,
365
- });
366
- }
367
- else if ((0, _ethers_js_1.isAbstractEthersSigner)(wallet)) {
368
- return await wallet.signTypedData(domain, types, message);
369
- }
370
- else if ((0, _ethers_js_1.isAbstractEthersV5Signer)(wallet)) {
371
- return await wallet._signTypedData(domain, types, message);
372
- }
373
- else if ((0, _window_js_1.isAbstractWindowEthereum)(wallet)) {
374
- return await (0, _window_js_1.signTypedDataWithWindowEthereum)(wallet, domain, types, message);
375
- }
376
- else {
377
- throw new Error("Unsupported wallet for signing typed data");
378
- }
379
- }
380
- /** Splits a signature hexadecimal string into its components. */
381
- function splitSignature(signature) {
382
- const r = `0x${signature.slice(2, 66)}`;
383
- const s = `0x${signature.slice(66, 130)}`;
384
- const v = parseInt(signature.slice(130, 132), 16);
385
- return { r, s, v };
386
- }
387
- });
259
+ },
260
+ types,
261
+ primaryType: Object.keys(types)[0],
262
+ message: action,
263
+ });
264
+ }
265
+ /**
266
+ * Sign a multi-signature action.
267
+ * @example
268
+ * ```ts
269
+ * import { actionSorter, signL1Action, signMultiSigAction } from "@nktkas/hyperliquid/signing";
270
+ * import { privateKeyToAccount } from "viem/accounts";
271
+ *
272
+ * const wallet = privateKeyToAccount("0x..."); // or `ethers`, private key with address
273
+ * const multiSigUser = "0x...";
274
+ *
275
+ * const nonce = Date.now();
276
+ * const action = {
277
+ * type: "scheduleCancel",
278
+ * time: Date.now() + 10000,
279
+ * } as const;
280
+ *
281
+ * // First, create the required number of signatures
282
+ * const signature = await signL1Action({
283
+ * wallet,
284
+ * action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), actionSorter[action.type](action)],
285
+ * nonce,
286
+ * });
287
+ *
288
+ * // Then use signatures in the multi-sig action
289
+ * const multiSigSignature = await signMultiSigAction({
290
+ * wallet,
291
+ * action: {
292
+ * signatureChainId: "0x66eee",
293
+ * signatures: [signature],
294
+ * payload: {
295
+ * multiSigUser,
296
+ * outerSigner: wallet.address,
297
+ * action,
298
+ * },
299
+ * },
300
+ * nonce,
301
+ * });
302
+ * ```
303
+ */
304
+ async function signMultiSigAction(args) {
305
+ const { wallet, action, nonce, isTestnet = false, vaultAddress, expiresAfter, } = args;
306
+ const multiSigActionHash = createL1ActionHash({ action, nonce, vaultAddress, expiresAfter });
307
+ const message = {
308
+ signatureChainId: action.signatureChainId,
309
+ hyperliquidChain: isTestnet ? "Testnet" : "Mainnet",
310
+ multiSigActionHash,
311
+ nonce,
312
+ };
313
+ return await (0, mod_js_1.signTypedData)({
314
+ wallet,
315
+ domain: {
316
+ name: "HyperliquidSignTransaction",
317
+ version: "1",
318
+ chainId: parseInt(message.signatureChainId),
319
+ verifyingContract: "0x0000000000000000000000000000000000000000",
320
+ },
321
+ types: {
322
+ "HyperliquidTransaction:SendMultiSig": [
323
+ { name: "hyperliquidChain", type: "string" },
324
+ { name: "multiSigActionHash", type: "bytes32" },
325
+ { name: "nonce", type: "uint64" },
326
+ ],
327
+ },
328
+ primaryType: "HyperliquidTransaction:SendMultiSig",
329
+ message,
330
+ });
331
+ }