Skip to main content

df_hyperliquid_core/utils/sign/
l1.rs

1//! L1 actions: msgpack, hashed into a connection id, signed as a phantom
2//! agent `Agent{source, connectionId}` under `Exchange/1/1337/0x0`. The
3//! source is `"a"` on mainnet and `"b"` on testnet; the hash has no network.
4
5use crate::account::Signature;
6use crate::client::http::body;
7use crate::client::network::Network;
8use crate::utils::address::Address;
9use crate::utils::sign::eip712::{Domain, Field, TypedStruct};
10use crate::utils::sign::keccak::Keccak256;
11use crate::utils::sign::msgpack::Map;
12
13/// `Domain::EXCHANGE.separator()`: the EIP-712 domain every L1 action is signed under, fixed, so it
14/// is not hashed again for each signature. A test holds it to [`Domain::separator`].
15pub(crate) const EXCHANGE_SEPARATOR: [u8; 32] = [
16    0xd7, 0x92, 0x97, 0xfc, 0xdf, 0x2f, 0xfc, 0xd4, 0xae, 0x22, 0x3d, 0x01, 0xed, 0xaa, 0x2b, 0xa2, 0x14, 0xff, 0x8f,
17    0x40, 0x1d, 0x7c, 0x93, 0x00, 0xd9, 0x95, 0xd1, 0x7c, 0x82, 0xaa, 0x40, 0x40,
18];
19/// `keccak256("Agent(string source,bytes32 connectionId)")`, the phantom agent's type hash.
20pub(crate) const AGENT_TYPE_HASH: [u8; 32] = [
21    0x26, 0xf0, 0x5c, 0x2f, 0x72, 0x39, 0xb6, 0x98, 0x30, 0x75, 0xe5, 0x83, 0x21, 0x29, 0x2d, 0x77, 0xb3, 0xaa, 0x17,
22    0x3d, 0x19, 0xb2, 0x72, 0x57, 0xac, 0x96, 0xab, 0x36, 0x25, 0x70, 0xf5, 0x08,
23];
24/// `keccak256("a")`, the mainnet source as EIP-712 encodes a string field.
25pub(crate) const SOURCE_MAINNET: [u8; 32] = [
26    0x3a, 0xc2, 0x25, 0x16, 0x8d, 0xf5, 0x42, 0x12, 0xa2, 0x5c, 0x1c, 0x01, 0xfd, 0x35, 0xbe, 0xbf, 0xea, 0x40, 0x8f,
27    0xda, 0xc2, 0xe3, 0x1d, 0xdd, 0x6f, 0x80, 0xa4, 0xbb, 0xf9, 0xa5, 0xf1, 0xcb,
28];
29/// `keccak256("b")`, the testnet source.
30pub(crate) const SOURCE_TESTNET: [u8; 32] = [
31    0xb5, 0x55, 0x3d, 0xe3, 0x15, 0xe0, 0xed, 0xf5, 0x04, 0xd9, 0x15, 0x0a, 0xf8, 0x2d, 0xaf, 0xa5, 0xc4, 0x66, 0x7f,
32    0xa6, 0x18, 0xed, 0x0a, 0x6f, 0x19, 0xc6, 0x9b, 0x41, 0x16, 0x6c, 0x55, 0x10,
33];
34
35/// `keccak256(msgpack || nonce as u64 big-endian || vault marker)`: `0x00`,
36/// or `0x01` and the vault's twenty bytes.
37#[must_use]
38pub fn connection_id(msgpack: &[u8], nonce: u64, vault: Option<Address>) -> [u8; 32] {
39    connection_id_expiring(msgpack, nonce, vault, None)
40}
41
42/// [`connection_id`] for an action posted with `expiresAfter`: the expiry follows the vault
43/// marker as `0x00` and the milliseconds as a big-endian u64, as the venue's Python SDK hashes
44/// it. With no expiry it is [`connection_id`].
45#[must_use]
46pub fn connection_id_expiring(
47    msgpack: &[u8],
48    nonce: u64,
49    vault: Option<Address>,
50    expires_after: Option<u64>,
51) -> [u8; 32] {
52    let sponge = Keccak256::new().chain(msgpack).chain(&nonce.to_be_bytes());
53    let sponge = match vault {
54        Some(vault) => sponge.chain(&[0x01]).chain(vault.bytes()),
55        None => sponge.chain(&[0x00]),
56    };
57    match expires_after {
58        Some(expires_after) => sponge.chain(&[0x00]).chain(&expires_after.to_be_bytes()),
59        None => sponge,
60    }
61    .finalize()
62}
63
64/// The struct an L1 signature is really over.
65#[must_use]
66pub fn phantom_agent(connection_id: [u8; 32], network: Network) -> TypedStruct<'static> {
67    TypedStruct::new("Agent")
68        .field("source", Field::String(network.agent_source()))
69        .field("connectionId", Field::Bytes32(connection_id))
70}
71
72/// What the key signs for an L1 action on `network`.
73#[must_use]
74pub fn l1_digest(msgpack: &[u8], nonce: u64, vault: Option<Address>, network: Network) -> [u8; 32] {
75    agent_digest(connection_id(msgpack, nonce, vault), network)
76}
77
78/// The phantom agent's digest under [`Domain::EXCHANGE`] for `connection_id`: the same bytes as
79/// `phantom_agent(connection_id, network).digest(&Domain::EXCHANGE)`, from the fixed domain
80/// separator, type hash and source hash, so a signature hashes two keccak blocks here instead of
81/// eight.
82#[must_use]
83pub fn agent_digest(connection_id: [u8; 32], network: Network) -> [u8; 32] {
84    let source = match network {
85        Network::Mainnet => &SOURCE_MAINNET,
86        Network::Testnet => &SOURCE_TESTNET,
87    };
88    let agent = Keccak256::new().chain(&AGENT_TYPE_HASH).chain(source).chain(&connection_id).finalize();
89    Keccak256::new().chain(&[0x19, 0x01]).chain(&EXCHANGE_SEPARATOR).chain(&agent).finalize()
90}
91
92/// An action signed through the phantom agent. An implementor writes
93/// [`L1Action::action`], keys in the order the venue's clients write them, and
94/// gets everything else from it.
95pub trait L1Action {
96    /// The action's keys and values in wire order, which is the hashed order.
97    fn action(&self) -> Map;
98
99    /// The action as msgpack bytes, the first part of the connection id's hash.
100    #[must_use]
101    fn msgpack(&self) -> Vec<u8> {
102        self.action().to_msgpack()
103    }
104
105    /// The action as posted.
106    #[must_use]
107    fn wire(&self) -> String {
108        self.action().to_json().compact()
109    }
110
111    /// [`connection_id`] of this action at `nonce`, for `vault` when it acts for one.
112    #[must_use]
113    fn connection_id(&self, nonce: u64, vault: Option<Address>) -> [u8; 32] {
114        connection_id(&self.msgpack(), nonce, vault)
115    }
116
117    /// The phantom agent struct the key signs for this action.
118    #[must_use]
119    fn typed(&self, nonce: u64, vault: Option<Address>, network: Network) -> TypedStruct<'static> {
120        phantom_agent(self.connection_id(nonce, vault), network)
121    }
122
123    /// What the key signs for this action: the phantom agent's digest under [`Domain::EXCHANGE`].
124    #[must_use]
125    fn digest(&self, nonce: u64, vault: Option<Address>, network: Network) -> [u8; 32] {
126        agent_digest(self.connection_id(nonce, vault), network)
127    }
128
129    /// For a master wallet signing in the browser. It needs chain 1337.
130    #[must_use]
131    fn wallet_json(&self, nonce: u64, vault: Option<Address>, network: Network) -> String {
132        self.typed(nonce, vault, network).wallet_json(&Domain::EXCHANGE)
133    }
134
135    /// The `/exchange` body: `action`, `nonce`, `signature` and `vaultAddress`, `null` when
136    /// there is no vault.
137    #[must_use]
138    fn body(&self, nonce: u64, signature: &Signature, vault: Option<Address>) -> String {
139        body::l1(&self.action(), nonce, signature, vault)
140    }
141}
142
143/// A map is an action already.
144impl L1Action for Map {
145    fn action(&self) -> Map {
146        self.clone()
147    }
148}
149
150#[cfg(test)]
151#[path = "tests/l1.rs"]
152mod tests;