Skip to main content

df_hyperliquid_core/utils/sign/
user_signed.rs

1//! User-signed actions: EIP-712 typed data signed by the account's own wallet
2//! (never an agent) under `HyperliquidSignTransaction/1/<chain>/0x0`, the chain
3//! being the network's signature chain (42161 on mainnet, 421614 on testnet).
4//!
5//! One field list drives both the posted action and the typed struct: `type`,
6//! `signatureChainId`, `hyperliquidChain`, the action's own keys, then the
7//! nonce under its own name, as the `@nktkas/hyperliquid` SDK writes them.
8
9use crate::account::Signature;
10use crate::client::http::body;
11use crate::client::network::Network;
12use crate::utils::sign::eip712::{Domain, Field, TypedStruct};
13use crate::utils::sign::msgpack::Map;
14
15/// What an action calls its nonce. The older actions (`usdSend`, `spotSend`,
16/// `withdraw3`) say `time`; the number is the same.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum NonceKey {
19    /// `nonce`, used by the newer actions.
20    Nonce,
21    /// `time`, used by `usdSend`, `spotSend` and `withdraw3`.
22    Time,
23}
24
25impl NonceKey {
26    /// The key as written: `nonce` or `time`.
27    #[must_use]
28    pub const fn as_str(self) -> &'static str {
29        match self {
30            NonceKey::Nonce => "nonce",
31            NonceKey::Time => "time",
32        }
33    }
34}
35
36/// A user-signed action, everything that goes into its signature.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct UserSigned<'a> {
39    kind: &'static str,
40    primary_type: &'static str,
41    fields: Vec<(&'static str, Field<'a>)>,
42    nonce_key: NonceKey,
43    nonce: u64,
44}
45
46impl<'a> UserSigned<'a> {
47    /// `kind` is the posted `type` (`withdraw3`), `primary_type` the EIP-712
48    /// name (`HyperliquidTransaction:Withdraw`).
49    #[must_use]
50    pub const fn new(kind: &'static str, primary_type: &'static str, nonce_key: NonceKey, nonce: u64) -> Self {
51        UserSigned { kind, primary_type, fields: Vec::new(), nonce_key, nonce }
52    }
53
54    /// Appends one of the action's own keys; call order is wire and type order.
55    #[must_use]
56    pub fn field(mut self, name: &'static str, value: Field<'a>) -> Self {
57        self.fields.push((name, value));
58        self
59    }
60
61    /// The posted `type`, such as `withdraw3`.
62    #[must_use]
63    pub const fn kind(&self) -> &'static str {
64        self.kind
65    }
66
67    /// The nonce, in milliseconds, as given to [`UserSigned::new`].
68    #[must_use]
69    pub const fn nonce(&self) -> u64 {
70        self.nonce
71    }
72
73    /// The action as posted on `network`, in wire order.
74    #[must_use]
75    pub fn action(&self, network: Network) -> Map {
76        let mut map = Map::new()
77            .put("type", self.kind)
78            .put("signatureChainId", network.signature_chain_id_hex())
79            .put("hyperliquidChain", network.hyperliquid_chain());
80        for (name, value) in &self.fields {
81            map = map.put(*name, value.to_wire());
82        }
83        map.put(self.nonce_key.as_str(), self.nonce)
84    }
85
86    /// [`UserSigned::action`] as JSON.
87    #[must_use]
88    pub fn wire(&self, network: Network) -> String {
89        self.action(network).to_json().compact()
90    }
91
92    /// `hyperliquidChain`, the action's own keys, then the nonce as `uint64`.
93    #[must_use]
94    pub fn typed(&self, network: Network) -> TypedStruct<'a> {
95        let mut data =
96            TypedStruct::new(self.primary_type).field("hyperliquidChain", Field::String(network.hyperliquid_chain()));
97        for &(name, value) in &self.fields {
98            data = data.field(name, value);
99        }
100        data.field(self.nonce_key.as_str(), Field::Uint64(self.nonce))
101    }
102
103    /// What the key signs: [`UserSigned::typed`] under the network's user-signed domain.
104    #[must_use]
105    pub fn digest(&self, network: Network) -> [u8; 32] {
106        self.typed(network).digest(&Domain::user_signed(network))
107    }
108
109    /// For the wallet. It wants to be on the network's signature chain to
110    /// sign this: Arbitrum One, or Arbitrum Sepolia on testnet.
111    #[must_use]
112    pub fn wallet_json(&self, network: Network) -> String {
113        self.typed(network).wallet_json(&Domain::user_signed(network))
114    }
115
116    /// The `/exchange` body, with no `vaultAddress`.
117    #[must_use]
118    pub fn body(&self, signature: &Signature, network: Network) -> String {
119        body::user_signed(&self.action(network), self.nonce, signature)
120    }
121}
122
123/// An action defined by its [`UserSigned`] form, from which come the posted
124/// JSON, typed data, digest, wallet JSON and body.
125pub trait UserAction {
126    /// The action's type, EIP-712 name, fields and nonce.
127    fn user_signed(&self) -> UserSigned<'_>;
128
129    /// [`UserSigned::nonce`].
130    #[must_use]
131    fn nonce(&self) -> u64 {
132        self.user_signed().nonce()
133    }
134
135    /// [`UserSigned::action`]: the action as posted on `network`.
136    #[must_use]
137    fn action(&self, network: Network) -> Map {
138        self.user_signed().action(network)
139    }
140
141    /// [`UserSigned::wire`]: the action as posted, as JSON.
142    #[must_use]
143    fn wire(&self, network: Network) -> String {
144        self.user_signed().wire(network)
145    }
146
147    /// [`UserSigned::typed`]: the struct the key signs.
148    #[must_use]
149    fn typed(&self, network: Network) -> TypedStruct<'_> {
150        self.user_signed().typed(network)
151    }
152
153    /// [`UserSigned::digest`]: what the key signs.
154    #[must_use]
155    fn digest(&self, network: Network) -> [u8; 32] {
156        self.user_signed().digest(network)
157    }
158
159    /// [`UserSigned::wallet_json`]: the typed data for a browser wallet.
160    #[must_use]
161    fn wallet_json(&self, network: Network) -> String {
162        self.user_signed().wallet_json(network)
163    }
164
165    /// [`UserSigned::body`]: the `/exchange` body, with no `vaultAddress`.
166    #[must_use]
167    fn body(&self, signature: &Signature, network: Network) -> String {
168        self.user_signed().body(signature, network)
169    }
170}