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)]
18pub enum NonceKey {
19 Nonce,
21 Time,
23}
24
25impl NonceKey {
26 #[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#[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 #[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 #[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 #[must_use]
63 pub const fn kind(&self) -> &'static str {
64 self.kind
65 }
66
67 #[must_use]
69 pub const fn nonce(&self) -> u64 {
70 self.nonce
71 }
72
73 #[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 #[must_use]
88 pub fn wire(&self, network: Network) -> String {
89 self.action(network).to_json().compact()
90 }
91
92 #[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 #[must_use]
105 pub fn digest(&self, network: Network) -> [u8; 32] {
106 self.typed(network).digest(&Domain::user_signed(network))
107 }
108
109 #[must_use]
112 pub fn wallet_json(&self, network: Network) -> String {
113 self.typed(network).wallet_json(&Domain::user_signed(network))
114 }
115
116 #[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
123pub trait UserAction {
126 fn user_signed(&self) -> UserSigned<'_>;
128
129 #[must_use]
131 fn nonce(&self) -> u64 {
132 self.user_signed().nonce()
133 }
134
135 #[must_use]
137 fn action(&self, network: Network) -> Map {
138 self.user_signed().action(network)
139 }
140
141 #[must_use]
143 fn wire(&self, network: Network) -> String {
144 self.user_signed().wire(network)
145 }
146
147 #[must_use]
149 fn typed(&self, network: Network) -> TypedStruct<'_> {
150 self.user_signed().typed(network)
151 }
152
153 #[must_use]
155 fn digest(&self, network: Network) -> [u8; 32] {
156 self.user_signed().digest(network)
157 }
158
159 #[must_use]
161 fn wallet_json(&self, network: Network) -> String {
162 self.user_signed().wallet_json(network)
163 }
164
165 #[must_use]
167 fn body(&self, signature: &Signature, network: Network) -> String {
168 self.user_signed().body(signature, network)
169 }
170}