@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.
- package/README.md +79 -67
- package/esm/mod.d.ts +1 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/src/base.d.ts +4 -1
- package/esm/src/base.d.ts.map +1 -1
- package/esm/src/base.js +2 -2
- package/esm/src/clients/exchange.d.ts +289 -207
- package/esm/src/clients/exchange.d.ts.map +1 -1
- package/esm/src/clients/exchange.js +605 -410
- package/esm/src/clients/info.d.ts +264 -48
- package/esm/src/clients/info.d.ts.map +1 -1
- package/esm/src/clients/info.js +273 -31
- package/esm/src/clients/multiSign.d.ts +285 -204
- package/esm/src/clients/multiSign.d.ts.map +1 -1
- package/esm/src/clients/multiSign.js +521 -493
- package/esm/src/clients/subscription.d.ts +38 -3
- package/esm/src/clients/subscription.d.ts.map +1 -1
- package/esm/src/clients/subscription.js +36 -3
- package/esm/src/signing/{_ethers.d.ts → _signTypedData/ethers.d.ts} +1 -1
- package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/mod.d.ts +35 -0
- package/esm/src/signing/_signTypedData/mod.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/mod.js +59 -0
- package/esm/src/signing/_signTypedData/private_key.d.ts +26 -0
- package/esm/src/signing/_signTypedData/private_key.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/private_key.js +144 -0
- package/esm/src/signing/{_viem.d.ts → _signTypedData/viem.d.ts} +2 -2
- package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/{script/src/signing/_window.d.ts → esm/src/signing/_signTypedData/window.d.ts} +18 -12
- package/esm/src/signing/_signTypedData/window.d.ts.map +1 -0
- package/esm/src/signing/{_window.js → _signTypedData/window.js} +3 -2
- package/esm/src/signing/_sorter.d.ts +56 -82
- package/esm/src/signing/_sorter.d.ts.map +1 -1
- package/esm/src/signing/_sorter.js +36 -49
- package/esm/src/signing/mod.d.ts +151 -130
- package/esm/src/signing/mod.d.ts.map +1 -1
- package/esm/src/signing/mod.js +182 -228
- package/esm/src/transports/base.d.ts +1 -1
- package/esm/src/transports/base.d.ts.map +1 -1
- package/esm/src/transports/base.js +2 -2
- package/esm/src/transports/http/http_transport.d.ts +9 -15
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +64 -59
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/esm/src/transports/websocket/_hyperliquid_event_target.js +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.js +17 -4
- package/esm/src/transports/websocket/_websocket_async_request.d.ts +0 -9
- package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
- package/esm/src/transports/websocket/_websocket_async_request.js +14 -20
- package/esm/src/transports/websocket/websocket_transport.d.ts +8 -4
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +83 -54
- package/esm/src/types/exchange/requests.d.ts +27 -51
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/explorer/requests.d.ts +5 -8
- package/esm/src/types/explorer/requests.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +5 -5
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/markets.d.ts +6 -0
- package/esm/src/types/info/markets.d.ts.map +1 -1
- package/esm/src/types/info/orders.d.ts +1 -1
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +129 -124
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/{script/src/types/info/delegations.d.ts → esm/src/types/info/validators.d.ts} +2 -2
- package/esm/src/types/info/validators.d.ts.map +1 -0
- package/esm/src/types/info/vaults.d.ts +7 -0
- package/esm/src/types/info/vaults.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +1 -1
- package/esm/src/types/mod.d.ts.map +1 -1
- package/esm/src/types/subscriptions/requests.d.ts +72 -18
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/esm/src/types/subscriptions/responses.d.ts +2 -0
- package/esm/src/types/subscriptions/responses.d.ts.map +1 -1
- package/package.json +8 -5
- package/script/mod.d.ts +1 -1
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +13 -23
- package/script/src/base.d.ts +4 -1
- package/script/src/base.d.ts.map +1 -1
- package/script/src/base.js +10 -20
- package/script/src/clients/exchange.d.ts +289 -207
- package/script/src/clients/exchange.d.ts.map +1 -1
- package/script/src/clients/exchange.js +2000 -1815
- package/script/src/clients/info.d.ts +264 -48
- package/script/src/clients/info.d.ts.map +1 -1
- package/script/src/clients/info.js +1442 -1210
- package/script/src/clients/multiSign.d.ts +285 -204
- package/script/src/clients/multiSign.d.ts.map +1 -1
- package/script/src/clients/multiSign.js +2043 -2025
- package/script/src/clients/subscription.d.ts +38 -3
- package/script/src/clients/subscription.d.ts.map +1 -1
- package/script/src/clients/subscription.js +568 -545
- package/script/src/signing/{_ethers.d.ts → _signTypedData/ethers.d.ts} +1 -1
- package/script/src/signing/_signTypedData/ethers.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/ethers.js +16 -0
- package/script/src/signing/_signTypedData/mod.d.ts +35 -0
- package/script/src/signing/_signTypedData/mod.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/mod.js +67 -0
- package/script/src/signing/_signTypedData/private_key.d.ts +26 -0
- package/script/src/signing/_signTypedData/private_key.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/private_key.js +148 -0
- package/script/src/signing/{_viem.d.ts → _signTypedData/viem.d.ts} +2 -2
- package/script/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/viem.js +9 -0
- package/{esm/src/signing/_window.d.ts → script/src/signing/_signTypedData/window.d.ts} +18 -12
- package/script/src/signing/_signTypedData/window.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/window.js +34 -0
- package/script/src/signing/_sorter.d.ts +56 -82
- package/script/src/signing/_sorter.d.ts.map +1 -1
- package/script/src/signing/_sorter.js +655 -678
- package/script/src/signing/mod.d.ts +151 -130
- package/script/src/signing/mod.d.ts.map +1 -1
- package/script/src/signing/mod.js +286 -342
- package/script/src/transports/base.d.ts +1 -1
- package/script/src/transports/base.d.ts.map +1 -1
- package/script/src/transports/base.js +11 -21
- package/script/src/transports/http/http_transport.d.ts +9 -15
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +97 -102
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/script/src/transports/websocket/_hyperliquid_event_target.js +51 -61
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.js +271 -268
- package/script/src/transports/websocket/_websocket_async_request.d.ts +0 -9
- package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
- package/script/src/transports/websocket/_websocket_async_request.js +157 -174
- package/script/src/transports/websocket/websocket_transport.d.ts +8 -4
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +209 -189
- package/script/src/types/exchange/requests.d.ts +27 -51
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/exchange/requests.js +2 -12
- package/script/src/types/exchange/responses.js +2 -12
- package/script/src/types/explorer/requests.d.ts +5 -8
- package/script/src/types/explorer/requests.d.ts.map +1 -1
- package/script/src/types/explorer/requests.js +2 -12
- package/script/src/types/explorer/responses.js +2 -12
- package/script/src/types/info/accounts.d.ts +5 -5
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/accounts.js +2 -12
- package/script/src/types/info/assets.js +2 -12
- package/script/src/types/info/markets.d.ts +6 -0
- package/script/src/types/info/markets.d.ts.map +1 -1
- package/script/src/types/info/markets.js +2 -12
- package/script/src/types/info/orders.d.ts +1 -1
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/orders.js +2 -12
- package/script/src/types/info/requests.d.ts +129 -124
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/requests.js +2 -12
- package/{esm/src/types/info/delegations.d.ts → script/src/types/info/validators.d.ts} +2 -2
- package/script/src/types/info/validators.d.ts.map +1 -0
- package/script/src/types/info/validators.js +2 -0
- package/script/src/types/info/vaults.d.ts +7 -0
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/info/vaults.js +2 -12
- package/script/src/types/mod.d.ts +1 -1
- package/script/src/types/mod.d.ts.map +1 -1
- package/script/src/types/mod.js +2 -12
- package/script/src/types/subscriptions/requests.d.ts +72 -18
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/script/src/types/subscriptions/requests.js +2 -12
- package/script/src/types/subscriptions/responses.d.ts +2 -0
- package/script/src/types/subscriptions/responses.d.ts.map +1 -1
- package/script/src/types/subscriptions/responses.js +2 -12
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -1
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -10
- package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
- package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.0.13/delay.js +0 -63
- package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
- package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -2
- package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
- package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -32
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -51
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -13
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -2
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -87
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -2
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -237
- package/esm/src/signing/_ethers.d.ts.map +0 -1
- package/esm/src/signing/_private_key.d.ts +0 -22
- package/esm/src/signing/_private_key.d.ts.map +0 -1
- package/esm/src/signing/_private_key.js +0 -124
- package/esm/src/signing/_viem.d.ts.map +0 -1
- package/esm/src/signing/_window.d.ts.map +0 -1
- package/esm/src/types/info/delegations.d.ts.map +0 -1
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -27
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -24
- package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
- package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.0.13/delay.js +0 -76
- package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
- package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -13
- package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
- package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -45
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -67
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -26
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -13
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -101
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -13
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -250
- package/script/src/signing/_ethers.d.ts.map +0 -1
- package/script/src/signing/_ethers.js +0 -26
- package/script/src/signing/_private_key.d.ts +0 -22
- package/script/src/signing/_private_key.d.ts.map +0 -1
- package/script/src/signing/_private_key.js +0 -138
- package/script/src/signing/_viem.d.ts.map +0 -1
- package/script/src/signing/_viem.js +0 -19
- package/script/src/signing/_window.d.ts.map +0 -1
- package/script/src/signing/_window.js +0 -43
- package/script/src/types/info/delegations.d.ts.map +0 -1
- package/script/src/types/info/delegations.js +0 -12
- /package/esm/src/signing/{_ethers.js → _signTypedData/ethers.js} +0 -0
- /package/esm/src/signing/{_viem.js → _signTypedData/viem.js} +0 -0
- /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
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* isTestnet: true, // Change to false for mainnet
|
|
21
|
+
* wallet: privateKey,
|
|
22
|
+
* action: actionSorter[action.type](action),
|
|
23
|
+
* nonce,
|
|
20
24
|
* });
|
|
21
25
|
* ```
|
|
22
|
-
*
|
|
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
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
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
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
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
|
-
(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
|
205
|
+
chainId: 1337, // hyperliquid requires chainId to be 1337
|
|
198
206
|
verifyingContract: "0x0000000000000000000000000000000000000000",
|
|
199
|
-
}
|
|
200
|
-
|
|
207
|
+
},
|
|
208
|
+
types: {
|
|
201
209
|
Agent: [
|
|
202
210
|
{ name: "source", type: "string" },
|
|
203
211
|
{ name: "connectionId", type: "bytes32" },
|
|
204
212
|
],
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
+
}
|