Skip to main content

df_hyperliquid_core/utils/sign/
multi_sig.rs

1//! Multi-sig: an L1 action sent for an account its signers hold together. Each signer signs the
2//! action nested as `[multiSigUser, outerSigner, action]`, an L1 action like any other; the outer
3//! signer, the one of them that posts it, wraps the action and every signature as `multiSig` and
4//! signs that as `SendMultiSig`, a user-signed action over its hash. Both as the venue's Python SDK
5//! nests and signs them.
6
7use crate::account::Signature;
8use crate::client::http::body;
9use crate::client::network::Network;
10use crate::utils::address::Address;
11use crate::utils::sign::eip712::{Domain, Field, TypedStruct};
12use crate::utils::sign::l1::{L1Action, connection_id, phantom_agent};
13use crate::utils::sign::msgpack::{Map, Value};
14use crate::utils::sign::user_signed::{NonceKey, UserSigned};
15
16/// An L1 action for the multi-sig account `user`, posted by `outer_signer`, one of its signers,
17/// with each signature collected for it.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct MultiSig {
20    /// `multiSigUser`: the multi-sig account the action is for.
21    pub user: Address,
22    /// `outerSigner`: the signer that posts the action and signs the outer `SendMultiSig`.
23    pub outer_signer: Address,
24    /// The action as the account would send it alone, `type` and all.
25    pub action: Map,
26    /// Each signer's signature over [`MultiSig::payload_digest`], in the order collected.
27    pub signatures: Vec<Signature>,
28}
29
30impl MultiSig {
31    /// No signatures yet.
32    #[must_use]
33    pub fn new(user: Address, outer_signer: Address, action: &impl L1Action) -> Self {
34        MultiSig { user, outer_signer, action: action.action(), signatures: Vec::new() }
35    }
36
37    /// With the signatures collected, in the order they are posted.
38    #[must_use]
39    pub fn signed(mut self, signatures: Vec<Signature>) -> Self {
40        self.signatures = signatures;
41        self
42    }
43
44    /// What each signer signs: `[multiSigUser, outerSigner, action]`, the addresses lower case.
45    #[must_use]
46    pub fn payload(&self) -> Value {
47        Value::Array(vec![
48            Value::from(self.user.to_hex()),
49            Value::from(self.outer_signer.to_hex()),
50            Value::Map(self.action.clone()),
51        ])
52    }
53
54    /// The payload's connection id, hashed as an L1 action's is.
55    #[must_use]
56    pub fn payload_connection_id(&self, nonce: u64, vault: Option<Address>) -> [u8; 32] {
57        connection_id(&self.payload().to_msgpack(), nonce, vault)
58    }
59
60    /// What each signer's key signs on `network`: the payload's phantom agent.
61    #[must_use]
62    pub fn payload_digest(&self, nonce: u64, vault: Option<Address>, network: Network) -> [u8; 32] {
63        phantom_agent(self.payload_connection_id(nonce, vault), network).digest(&Domain::EXCHANGE)
64    }
65
66    /// `{"type":"multiSig","signatureChainId","signatures","payload":{"multiSigUser","outerSigner","action"}}`,
67    /// as posted on `network`, each signature `{r,s,v}`.
68    #[must_use]
69    pub fn action(&self, network: Network) -> Map {
70        self.untyped(network)
71            .into_pairs()
72            .fold(Map::new().put("type", "multiSig"), |map, (key, value)| map.put(key, value))
73    }
74
75    /// [`MultiSig::action`] without its `type`, which is what is hashed.
76    fn untyped(&self, network: Network) -> Map {
77        let signatures = self.signatures.iter().map(|signature| {
78            Value::Map(
79                Map::new().put("r", signature.r_hex()).put("s", signature.s_hex()).put("v", u64::from(signature.v())),
80            )
81        });
82        Map::new()
83            .put("signatureChainId", network.signature_chain_id_hex())
84            .put("signatures", Value::Array(signatures.collect()))
85            .put(
86                "payload",
87                Map::new()
88                    .put("multiSigUser", self.user.to_hex())
89                    .put("outerSigner", self.outer_signer.to_hex())
90                    .put("action", self.action.clone()),
91            )
92    }
93
94    /// `multiSigActionHash`: [`MultiSig::action`] without its `type`, hashed as an L1 action's is.
95    #[must_use]
96    pub fn action_hash(&self, nonce: u64, vault: Option<Address>, network: Network) -> [u8; 32] {
97        connection_id(&self.untyped(network).to_msgpack(), nonce, vault)
98    }
99
100    /// `SendMultiSig`, the hash and the nonce, user-signed on `network`.
101    fn send(&self, nonce: u64, vault: Option<Address>, network: Network) -> UserSigned<'static> {
102        UserSigned::new("multiSig", "HyperliquidTransaction:SendMultiSig", NonceKey::Nonce, nonce)
103            .field("multiSigActionHash", Field::Bytes32(self.action_hash(nonce, vault, network)))
104    }
105
106    /// What the outer signer signs: `hyperliquidChain`, `multiSigActionHash`, then the nonce.
107    #[must_use]
108    pub fn typed(&self, nonce: u64, vault: Option<Address>, network: Network) -> TypedStruct<'static> {
109        self.send(nonce, vault, network).typed(network)
110    }
111
112    /// What the outer signer's key signs: [`MultiSig::typed`] under the network's user-signed domain.
113    #[must_use]
114    pub fn digest(&self, nonce: u64, vault: Option<Address>, network: Network) -> [u8; 32] {
115        self.send(nonce, vault, network).digest(network)
116    }
117
118    /// For the outer signer's wallet, on the network's signature chain.
119    #[must_use]
120    pub fn wallet_json(&self, nonce: u64, vault: Option<Address>, network: Network) -> String {
121        self.send(nonce, vault, network).wallet_json(network)
122    }
123
124    /// The `/exchange` body, the outer signer's `signature` beside the action.
125    #[must_use]
126    pub fn body(&self, nonce: u64, signature: &Signature, vault: Option<Address>, network: Network) -> String {
127        body::l1(&self.action(network), nonce, signature, vault)
128    }
129}
130
131#[cfg(test)]
132#[path = "tests/multi_sig.rs"]
133mod tests;