Skip to main content

df_hyperliquid_core/utils/sign/
breakdown.rs

1//! Everything an L1 signature is over, laid out in the order it is built.
2//!
3//! "User or API Wallet 0x... does not exist" is the venue saying the address it recovered from a
4//! signature is not one it knows, which almost always means what was signed is not what it hashed.
5//! Each stage here is deterministic, so each can be shown, compared with another SDK's, or timed:
6//! the msgpack bytes, the action hash over them with the nonce, the vault and an optional expiry,
7//! the EIP-712 payload around that hash, the digest a key signs, and the address a signature
8//! recovers to. Every stage uses the encoders [`L1Action::digest`] and [`L1Action::body`] use.
9
10#[cfg(feature = "sign")]
11use crate::account::{SignError, Signature};
12use crate::client::network::Network;
13use crate::utils::address::Address;
14use crate::utils::sign::eip712::Domain;
15use crate::utils::sign::l1::{L1Action, connection_id_expiring, phantom_agent};
16
17/// The stages of one L1 signature, in order.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct Breakdown {
20    /// The action as msgpack, keys in wire order: the first part of the hash.
21    pub msgpack: Vec<u8>,
22    /// `keccak256(msgpack || nonce || vault marker || optional expiry)`: the connection id the
23    /// phantom agent carries.
24    pub action_hash: [u8; 32],
25    /// The EIP-712 typed data a wallet is asked to sign (`eth_signTypedData_v4`): the phantom
26    /// agent `Agent{source, connectionId}` under `Exchange/1/1337/0x0`, as JSON.
27    pub payload: String,
28    /// What a key signs: the EIP-712 digest of the payload.
29    pub digest: [u8; 32],
30}
31
32impl Breakdown {
33    /// The stages for `action` at `nonce`, for `vault` when it acts for one, on `network`.
34    #[must_use]
35    pub fn of<A: L1Action + ?Sized>(action: &A, nonce: u64, vault: Option<Address>, network: Network) -> Breakdown {
36        Breakdown::expiring(action, nonce, vault, None, network)
37    }
38
39    /// [`Breakdown::of`] for an action posted with `expiresAfter` (milliseconds); `None` is
40    /// [`Breakdown::of`].
41    #[must_use]
42    pub fn expiring<A: L1Action + ?Sized>(
43        action: &A,
44        nonce: u64,
45        vault: Option<Address>,
46        expires_after: Option<u64>,
47        network: Network,
48    ) -> Breakdown {
49        let msgpack = action.msgpack();
50        let action_hash = connection_id_expiring(&msgpack, nonce, vault, expires_after);
51        let agent = phantom_agent(action_hash, network);
52        Breakdown {
53            payload: agent.wallet_json(&Domain::EXCHANGE),
54            digest: agent.digest(&Domain::EXCHANGE),
55            msgpack,
56            action_hash,
57        }
58    }
59
60    /// The last stage: the address `signature` recovers to over this digest. The venue answers
61    /// "does not exist" for any address but the signer's own.
62    #[cfg(feature = "sign")]
63    pub fn recover(&self, signature: &Signature) -> Result<Address, SignError> {
64        crate::account::recover(&self.digest, signature)
65    }
66}
67
68#[cfg(test)]
69#[path = "tests/breakdown.rs"]
70mod tests;