@nktkas/hyperliquid 0.22.1 → 0.23.0
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 +125 -70
- 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 +420 -368
- package/esm/src/clients/exchange.d.ts.map +1 -1
- package/esm/src/clients/exchange.js +721 -551
- package/esm/src/clients/info.d.ts +137 -41
- package/esm/src/clients/info.d.ts.map +1 -1
- package/esm/src/clients/info.js +124 -28
- package/esm/src/clients/multiSign.d.ts +406 -484
- package/esm/src/clients/multiSign.d.ts.map +1 -1
- package/esm/src/clients/multiSign.js +639 -734
- 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/_signTypedData/ethers.d.ts +33 -0
- package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/ethers.js +12 -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/_signTypedData/viem.d.ts +23 -0
- package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/viem.js +6 -0
- package/esm/src/signing/_signTypedData/window.d.ts +29 -0
- package/esm/src/signing/_signTypedData/window.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/window.js +30 -0
- package/esm/src/signing/_sorter.d.ts +128 -0
- package/esm/src/signing/_sorter.d.ts.map +1 -0
- package/esm/src/{signing.js → signing/_sorter.js} +36 -449
- package/esm/src/signing/mod.d.ts +272 -0
- package/esm/src/signing/mod.d.ts.map +1 -0
- package/esm/src/signing/mod.js +306 -0
- 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/delegations.d.ts +1 -1
- package/esm/src/types/info/delegations.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 +49 -96
- package/esm/src/types/info/requests.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 +12 -8
- 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 +420 -368
- package/script/src/clients/exchange.d.ts.map +1 -1
- package/script/src/clients/exchange.js +2000 -1840
- package/script/src/clients/info.d.ts +137 -41
- package/script/src/clients/info.d.ts.map +1 -1
- package/script/src/clients/info.js +1296 -1210
- package/script/src/clients/multiSign.d.ts +406 -484
- package/script/src/clients/multiSign.d.ts.map +1 -1
- package/script/src/clients/multiSign.js +2043 -2148
- 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/_signTypedData/ethers.d.ts +33 -0
- 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/_signTypedData/viem.d.ts +23 -0
- package/script/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/viem.js +9 -0
- package/script/src/signing/_signTypedData/window.d.ts +29 -0
- 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 +128 -0
- package/script/src/signing/_sorter.d.ts.map +1 -0
- package/script/src/signing/_sorter.js +698 -0
- package/script/src/signing/mod.d.ts +272 -0
- package/script/src/signing/mod.d.ts.map +1 -0
- package/script/src/signing/mod.js +331 -0
- 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/delegations.d.ts +1 -1
- package/script/src/types/info/delegations.d.ts.map +1 -1
- package/script/src/types/info/delegations.js +2 -12
- 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 +49 -96
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/requests.js +2 -12
- package/script/src/types/info/vaults.js +2 -12
- 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.d.ts +0 -463
- package/esm/src/signing.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.d.ts +0 -463
- package/script/src/signing.d.ts.map +0 -1
- package/script/src/signing.js +0 -1129
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module contains functions for generating Hyperliquid transaction signatures
|
|
3
|
+
* and interfaces to various wallet implementations.
|
|
4
|
+
*
|
|
5
|
+
* @example Signing an L1 action
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
|
|
8
|
+
*
|
|
9
|
+
* const privateKey = "0x..."; // or `viem`, `ethers`
|
|
10
|
+
*
|
|
11
|
+
* const nonce = Date.now();
|
|
12
|
+
* const action = {
|
|
13
|
+
* type: "cancel",
|
|
14
|
+
* cancels: [
|
|
15
|
+
* { a: 0, o: 12345 },
|
|
16
|
+
* ],
|
|
17
|
+
* } as const;
|
|
18
|
+
*
|
|
19
|
+
* const signature = await signL1Action({
|
|
20
|
+
* wallet: privateKey,
|
|
21
|
+
* action: actionSorter[action.type](action),
|
|
22
|
+
* nonce,
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @example Signing a user-signed action
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
|
|
29
|
+
*
|
|
30
|
+
* const privateKey = "0x..."; // or `viem`, `ethers`
|
|
31
|
+
*
|
|
32
|
+
* const action = {
|
|
33
|
+
* type: "approveAgent",
|
|
34
|
+
* signatureChainId: "0x66eee",
|
|
35
|
+
* hyperliquidChain: "Mainnet",
|
|
36
|
+
* agentAddress: "0x...",
|
|
37
|
+
* agentName: "Agent",
|
|
38
|
+
* nonce: Date.now(),
|
|
39
|
+
* } as const;
|
|
40
|
+
*
|
|
41
|
+
* const signature = await signUserSignedAction({
|
|
42
|
+
* wallet: privateKey,
|
|
43
|
+
* action,
|
|
44
|
+
* types: userSignedActionEip712Types[action.type],
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example Signing a multi-signature action
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { actionSorter, signL1Action, signMultiSigAction } from "@nktkas/hyperliquid/signing";
|
|
51
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
52
|
+
*
|
|
53
|
+
* const wallet = privateKeyToAccount("0x..."); // or `ethers`, private key with address
|
|
54
|
+
* const multiSigUser = "0x...";
|
|
55
|
+
*
|
|
56
|
+
* const nonce = Date.now();
|
|
57
|
+
* const action = {
|
|
58
|
+
* type: "scheduleCancel",
|
|
59
|
+
* time: Date.now() + 10000,
|
|
60
|
+
* } as const;
|
|
61
|
+
*
|
|
62
|
+
* // First, create the required number of signatures
|
|
63
|
+
* const signature = await signL1Action({
|
|
64
|
+
* wallet,
|
|
65
|
+
* action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), actionSorter[action.type](action)],
|
|
66
|
+
* nonce,
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* // Then use signatures in the multi-sig action
|
|
70
|
+
* const multiSigSignature = await signMultiSigAction({
|
|
71
|
+
* wallet,
|
|
72
|
+
* action: {
|
|
73
|
+
* signatureChainId: "0x66eee",
|
|
74
|
+
* signatures: [signature],
|
|
75
|
+
* payload: {
|
|
76
|
+
* multiSigUser,
|
|
77
|
+
* outerSigner: wallet.address,
|
|
78
|
+
* action,
|
|
79
|
+
* },
|
|
80
|
+
* },
|
|
81
|
+
* nonce,
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @module
|
|
86
|
+
*/
|
|
87
|
+
import type { Hex } from "../base.js";
|
|
88
|
+
import { type AbstractEthersSigner, type AbstractEthersV5Signer, type AbstractViemWalletClient, type AbstractWallet, type AbstractWindowEthereum, isAbstractEthersSigner, isAbstractEthersV5Signer, isAbstractViemWalletClient, isAbstractWindowEthereum, isValidPrivateKey, type Signature } from "./_signTypedData/mod.js";
|
|
89
|
+
export { type AbstractEthersSigner, type AbstractEthersV5Signer, type AbstractViemWalletClient, type AbstractWallet, type AbstractWindowEthereum, type Hex, isAbstractEthersSigner, isAbstractEthersV5Signer, isAbstractViemWalletClient, isAbstractWindowEthereum, isValidPrivateKey, type Signature, };
|
|
90
|
+
export * from "./_sorter.js";
|
|
91
|
+
/**
|
|
92
|
+
* Create a hash of the L1 action.
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { actionSorter, createL1ActionHash } from "@nktkas/hyperliquid/signing";
|
|
96
|
+
*
|
|
97
|
+
* const action = {
|
|
98
|
+
* type: "cancel",
|
|
99
|
+
* cancels: [
|
|
100
|
+
* { a: 0, o: 12345 },
|
|
101
|
+
* ],
|
|
102
|
+
* } as const;
|
|
103
|
+
* const nonce = Date.now();
|
|
104
|
+
*
|
|
105
|
+
* const actionHash = createL1ActionHash({
|
|
106
|
+
* action: actionSorter[action.type](action),
|
|
107
|
+
* nonce,
|
|
108
|
+
* vaultAddress: "0x...", // optional
|
|
109
|
+
* expiresAfter: Date.now() + 10_000, // optional
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export declare function createL1ActionHash(args: {
|
|
114
|
+
/** The action to be hashed (hash depends on key order). */
|
|
115
|
+
action: Record<string, unknown> | unknown[];
|
|
116
|
+
/** The current timestamp in ms. */
|
|
117
|
+
nonce: number;
|
|
118
|
+
/** Optional vault address used in the action. */
|
|
119
|
+
vaultAddress?: Hex;
|
|
120
|
+
/** Optional expiration time of the action in ms since the epoch. */
|
|
121
|
+
expiresAfter?: number;
|
|
122
|
+
}): Hex;
|
|
123
|
+
/**
|
|
124
|
+
* Sign an L1 action.
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
|
|
128
|
+
*
|
|
129
|
+
* const privateKey = "0x..."; // or `viem`, `ethers`
|
|
130
|
+
*
|
|
131
|
+
* const nonce = Date.now();
|
|
132
|
+
* const action = {
|
|
133
|
+
* type: "cancel",
|
|
134
|
+
* cancels: [
|
|
135
|
+
* { a: 0, o: 12345 },
|
|
136
|
+
* ],
|
|
137
|
+
* } as const;
|
|
138
|
+
*
|
|
139
|
+
* const signature = await signL1Action({
|
|
140
|
+
* wallet: privateKey,
|
|
141
|
+
* action: actionSorter[action.type](action),
|
|
142
|
+
* nonce,
|
|
143
|
+
* });
|
|
144
|
+
*
|
|
145
|
+
* // Send the signed action to the Hyperliquid API
|
|
146
|
+
* const response = await fetch("https://api.hyperliquid.xyz/exchange", {
|
|
147
|
+
* method: "POST",
|
|
148
|
+
* headers: { "Content-Type": "application/json" },
|
|
149
|
+
* body: JSON.stringify({ action, signature, nonce }),
|
|
150
|
+
* });
|
|
151
|
+
* const body = await response.json();
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export declare function signL1Action(args: {
|
|
155
|
+
/** Wallet to sign the action. */
|
|
156
|
+
wallet: AbstractWallet;
|
|
157
|
+
/** The action to be signed (hash depends on key order). */
|
|
158
|
+
action: Record<string, unknown> | unknown[];
|
|
159
|
+
/** The current timestamp in ms. */
|
|
160
|
+
nonce: number;
|
|
161
|
+
/** Indicates if the action is for the testnet. (default: false) */
|
|
162
|
+
isTestnet?: boolean;
|
|
163
|
+
/** Optional vault address used in the action. */
|
|
164
|
+
vaultAddress?: Hex;
|
|
165
|
+
/** Optional expiration time of the action in ms since the epoch. */
|
|
166
|
+
expiresAfter?: number;
|
|
167
|
+
}): Promise<Signature>;
|
|
168
|
+
/**
|
|
169
|
+
* Sign a user-signed action.
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
|
|
173
|
+
*
|
|
174
|
+
* const privateKey = "0x..."; // or `viem`, `ethers`
|
|
175
|
+
*
|
|
176
|
+
* const action = {
|
|
177
|
+
* type: "approveAgent",
|
|
178
|
+
* signatureChainId: "0x66eee",
|
|
179
|
+
* hyperliquidChain: "Mainnet",
|
|
180
|
+
* agentAddress: "0x...",
|
|
181
|
+
* agentName: "Agent",
|
|
182
|
+
* nonce: Date.now(),
|
|
183
|
+
* } as const;
|
|
184
|
+
*
|
|
185
|
+
* const signature = await signUserSignedAction({
|
|
186
|
+
* wallet: privateKey,
|
|
187
|
+
* action,
|
|
188
|
+
* types: userSignedActionEip712Types[action.type],
|
|
189
|
+
* });
|
|
190
|
+
*
|
|
191
|
+
* // Send the signed action to the Hyperliquid API
|
|
192
|
+
* const response = await fetch("https://api.hyperliquid.xyz/exchange", {
|
|
193
|
+
* method: "POST",
|
|
194
|
+
* headers: { "Content-Type": "application/json" },
|
|
195
|
+
* body: JSON.stringify({ action, signature, nonce: action.nonce }),
|
|
196
|
+
* });
|
|
197
|
+
* const body = await response.json();
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export declare function signUserSignedAction(args: {
|
|
201
|
+
/** Wallet to sign the action. */
|
|
202
|
+
wallet: AbstractWallet;
|
|
203
|
+
/** The action to be signed. */
|
|
204
|
+
action: {
|
|
205
|
+
signatureChainId: Hex;
|
|
206
|
+
[key: string]: unknown;
|
|
207
|
+
};
|
|
208
|
+
/** The types of the action (hash depends on key order). */
|
|
209
|
+
types: {
|
|
210
|
+
[key: string]: {
|
|
211
|
+
name: string;
|
|
212
|
+
type: string;
|
|
213
|
+
}[];
|
|
214
|
+
};
|
|
215
|
+
}): Promise<Signature>;
|
|
216
|
+
/**
|
|
217
|
+
* Sign a multi-signature action.
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* import { actionSorter, signL1Action, signMultiSigAction } from "@nktkas/hyperliquid/signing";
|
|
221
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
222
|
+
*
|
|
223
|
+
* const wallet = privateKeyToAccount("0x..."); // or `ethers`, private key with address
|
|
224
|
+
* const multiSigUser = "0x...";
|
|
225
|
+
*
|
|
226
|
+
* const nonce = Date.now();
|
|
227
|
+
* const action = {
|
|
228
|
+
* type: "scheduleCancel",
|
|
229
|
+
* time: Date.now() + 10000,
|
|
230
|
+
* } as const;
|
|
231
|
+
*
|
|
232
|
+
* // First, create the required number of signatures
|
|
233
|
+
* const signature = await signL1Action({
|
|
234
|
+
* wallet,
|
|
235
|
+
* action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), actionSorter[action.type](action)],
|
|
236
|
+
* nonce,
|
|
237
|
+
* });
|
|
238
|
+
*
|
|
239
|
+
* // Then use signatures in the multi-sig action
|
|
240
|
+
* const multiSigSignature = await signMultiSigAction({
|
|
241
|
+
* wallet,
|
|
242
|
+
* action: {
|
|
243
|
+
* signatureChainId: "0x66eee",
|
|
244
|
+
* signatures: [signature],
|
|
245
|
+
* payload: {
|
|
246
|
+
* multiSigUser,
|
|
247
|
+
* outerSigner: wallet.address,
|
|
248
|
+
* action,
|
|
249
|
+
* },
|
|
250
|
+
* },
|
|
251
|
+
* nonce,
|
|
252
|
+
* });
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
export declare function signMultiSigAction(args: {
|
|
256
|
+
/** Wallet to sign the action. */
|
|
257
|
+
wallet: AbstractWallet;
|
|
258
|
+
/** The action to be signed (hash depends on key order). */
|
|
259
|
+
action: {
|
|
260
|
+
signatureChainId: Hex;
|
|
261
|
+
[key: string]: unknown;
|
|
262
|
+
};
|
|
263
|
+
/** The current timestamp in ms. */
|
|
264
|
+
nonce: number;
|
|
265
|
+
/** Indicates if the action is for the testnet. (default: false) */
|
|
266
|
+
isTestnet?: boolean;
|
|
267
|
+
/** Optional vault address used in the action. */
|
|
268
|
+
vaultAddress?: Hex;
|
|
269
|
+
/** Optional expiration time of the action in ms since the epoch. */
|
|
270
|
+
expiresAfter?: number;
|
|
271
|
+
}): Promise<Signature>;
|
|
272
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","source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CAAC,CAoCrB"}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This module contains functions for generating Hyperliquid transaction signatures
|
|
4
|
+
* and interfaces to various wallet implementations.
|
|
5
|
+
*
|
|
6
|
+
* @example Signing an L1 action
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
|
|
9
|
+
*
|
|
10
|
+
* const privateKey = "0x..."; // or `viem`, `ethers`
|
|
11
|
+
*
|
|
12
|
+
* const nonce = Date.now();
|
|
13
|
+
* const action = {
|
|
14
|
+
* type: "cancel",
|
|
15
|
+
* cancels: [
|
|
16
|
+
* { a: 0, o: 12345 },
|
|
17
|
+
* ],
|
|
18
|
+
* } as const;
|
|
19
|
+
*
|
|
20
|
+
* const signature = await signL1Action({
|
|
21
|
+
* wallet: privateKey,
|
|
22
|
+
* action: actionSorter[action.type](action),
|
|
23
|
+
* nonce,
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example Signing a user-signed action
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
|
|
30
|
+
*
|
|
31
|
+
* const privateKey = "0x..."; // or `viem`, `ethers`
|
|
32
|
+
*
|
|
33
|
+
* const action = {
|
|
34
|
+
* type: "approveAgent",
|
|
35
|
+
* signatureChainId: "0x66eee",
|
|
36
|
+
* hyperliquidChain: "Mainnet",
|
|
37
|
+
* agentAddress: "0x...",
|
|
38
|
+
* agentName: "Agent",
|
|
39
|
+
* nonce: Date.now(),
|
|
40
|
+
* } as const;
|
|
41
|
+
*
|
|
42
|
+
* const signature = await signUserSignedAction({
|
|
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,
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @module
|
|
87
|
+
*/
|
|
88
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
89
|
+
if (k2 === undefined) k2 = k;
|
|
90
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
91
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
92
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
93
|
+
}
|
|
94
|
+
Object.defineProperty(o, k2, desc);
|
|
95
|
+
}) : (function(o, m, k, k2) {
|
|
96
|
+
if (k2 === undefined) k2 = k;
|
|
97
|
+
o[k2] = m[k];
|
|
98
|
+
}));
|
|
99
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
100
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
101
|
+
};
|
|
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: {
|
|
203
|
+
name: "Exchange",
|
|
204
|
+
version: "1",
|
|
205
|
+
chainId: 1337, // hyperliquid requires chainId to be 1337
|
|
206
|
+
verifyingContract: "0x0000000000000000000000000000000000000000",
|
|
207
|
+
},
|
|
208
|
+
types: {
|
|
209
|
+
Agent: [
|
|
210
|
+
{ name: "source", type: "string" },
|
|
211
|
+
{ name: "connectionId", type: "bytes32" },
|
|
212
|
+
],
|
|
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: {
|
|
255
|
+
name: "HyperliquidSignTransaction",
|
|
256
|
+
version: "1",
|
|
257
|
+
chainId: parseInt(action.signatureChainId),
|
|
258
|
+
verifyingContract: "0x0000000000000000000000000000000000000000",
|
|
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
|
+
}
|
|
@@ -42,6 +42,6 @@ export interface Subscription {
|
|
|
42
42
|
}
|
|
43
43
|
/** Base class for all transport-related errors. */
|
|
44
44
|
export declare class TransportError extends HyperliquidError {
|
|
45
|
-
constructor(message?: string);
|
|
45
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
46
46
|
}
|
|
47
47
|
//# sourceMappingURL=base.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/src/transports/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,WAAW,iBAA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,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CACpH;AAED,6CAA6C;AAC7C,MAAM,WAAW,YAAY;IACzB,kFAAkF;IAClF,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,wEAAwE;IACxE,iBAAiB,CAAC,EAAE,WAAW,CAAC;CACnC;AAED,mDAAmD;AACnD,qBAAa,cAAe,SAAQ,gBAAgB;gBACpC,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIvD"}
|
|
@@ -1,22 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TransportError = void 0;
|
|
4
|
+
const base_js_1 = require("../base.js");
|
|
5
|
+
/** Base class for all transport-related errors. */
|
|
6
|
+
class TransportError extends base_js_1.HyperliquidError {
|
|
7
|
+
constructor(message, options) {
|
|
8
|
+
super(message, options);
|
|
9
|
+
this.name = "TransportError";
|
|
5
10
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.TransportError = void 0;
|
|
13
|
-
const base_js_1 = require("../base.js");
|
|
14
|
-
/** Base class for all transport-related errors. */
|
|
15
|
-
class TransportError extends base_js_1.HyperliquidError {
|
|
16
|
-
constructor(message) {
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = "TransportError";
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
exports.TransportError = TransportError;
|
|
22
|
-
});
|
|
11
|
+
}
|
|
12
|
+
exports.TransportError = TransportError;
|