df_hyperliquid_core/utils/sign/
user_signed.rs1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18#[non_exhaustive]
19pub enum NonceKey {
20 Nonce,
22 Time,
24}
25
26impl NonceKey {
27 #[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#[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 #[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 #[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 #[must_use]
64 pub const fn kind(&self) -> &'static str {
65 self.kind
66 }
67
68 #[must_use]
70 pub const fn nonce(&self) -> u64 {
71 self.nonce
72 }
73
74 #[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 #[must_use]
89 pub fn wire(&self, network: Network) -> String {
90 self.action(network).to_json().compact()
91 }
92
93 #[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 #[must_use]
106 pub fn digest(&self, network: Network) -> [u8; 32] {
107 self.typed(network).digest(&Domain::user_signed(network))
108 }
109
110 #[must_use]
113 pub fn wallet_json(&self, network: Network) -> String {
114 self.typed(network).wallet_json(&Domain::user_signed(network))
115 }
116
117 #[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
124pub trait UserAction {
127 fn user_signed(&self) -> UserSigned<'_>;
129
130 #[must_use]
132 fn nonce(&self) -> u64 {
133 self.user_signed().nonce()
134 }
135
136 #[must_use]
138 fn action(&self, network: Network) -> Map {
139 self.user_signed().action(network)
140 }
141
142 #[must_use]
144 fn wire(&self, network: Network) -> String {
145 self.user_signed().wire(network)
146 }
147
148 #[must_use]
150 fn typed(&self, network: Network) -> TypedStruct<'_> {
151 self.user_signed().typed(network)
152 }
153
154 #[must_use]
156 fn digest(&self, network: Network) -> [u8; 32] {
157 self.user_signed().digest(network)
158 }
159
160 #[must_use]
162 fn wallet_json(&self, network: Network) -> String {
163 self.user_signed().wallet_json(network)
164 }
165
166 #[must_use]
168 fn body(&self, signature: &Signature, network: Network) -> String {
169 self.user_signed().body(signature, network)
170 }
171}