1use 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
13pub(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];
19pub(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];
24pub(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];
29pub(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#[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#[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#[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#[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#[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
92pub trait L1Action {
96 fn action(&self) -> Map;
98
99 #[must_use]
101 fn msgpack(&self) -> Vec<u8> {
102 self.action().to_msgpack()
103 }
104
105 #[must_use]
107 fn wire(&self) -> String {
108 self.action().to_json().compact()
109 }
110
111 #[must_use]
113 fn connection_id(&self, nonce: u64, vault: Option<Address>) -> [u8; 32] {
114 connection_id(&self.msgpack(), nonce, vault)
115 }
116
117 #[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 #[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 #[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 #[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
143impl L1Action for Map {
145 fn action(&self) -> Map {
146 self.clone()
147 }
148}
149
150#[cfg(test)]
151#[path = "tests/l1.rs"]
152mod tests;