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