use crate::account::Signature;
use crate::client::http::body;
use crate::client::network::Network;
use crate::utils::address::Address;
use crate::utils::sign::eip712::{Domain, Field, TypedStruct};
use crate::utils::sign::keccak::Keccak256;
use crate::utils::sign::msgpack::Map;
pub(crate) const EXCHANGE_SEPARATOR: [u8; 32] = [
0xd7, 0x92, 0x97, 0xfc, 0xdf, 0x2f, 0xfc, 0xd4, 0xae, 0x22, 0x3d, 0x01, 0xed, 0xaa, 0x2b, 0xa2, 0x14, 0xff, 0x8f,
0x40, 0x1d, 0x7c, 0x93, 0x00, 0xd9, 0x95, 0xd1, 0x7c, 0x82, 0xaa, 0x40, 0x40,
];
pub(crate) const AGENT_TYPE_HASH: [u8; 32] = [
0x26, 0xf0, 0x5c, 0x2f, 0x72, 0x39, 0xb6, 0x98, 0x30, 0x75, 0xe5, 0x83, 0x21, 0x29, 0x2d, 0x77, 0xb3, 0xaa, 0x17,
0x3d, 0x19, 0xb2, 0x72, 0x57, 0xac, 0x96, 0xab, 0x36, 0x25, 0x70, 0xf5, 0x08,
];
pub(crate) const SOURCE_MAINNET: [u8; 32] = [
0x3a, 0xc2, 0x25, 0x16, 0x8d, 0xf5, 0x42, 0x12, 0xa2, 0x5c, 0x1c, 0x01, 0xfd, 0x35, 0xbe, 0xbf, 0xea, 0x40, 0x8f,
0xda, 0xc2, 0xe3, 0x1d, 0xdd, 0x6f, 0x80, 0xa4, 0xbb, 0xf9, 0xa5, 0xf1, 0xcb,
];
pub(crate) const SOURCE_TESTNET: [u8; 32] = [
0xb5, 0x55, 0x3d, 0xe3, 0x15, 0xe0, 0xed, 0xf5, 0x04, 0xd9, 0x15, 0x0a, 0xf8, 0x2d, 0xaf, 0xa5, 0xc4, 0x66, 0x7f,
0xa6, 0x18, 0xed, 0x0a, 0x6f, 0x19, 0xc6, 0x9b, 0x41, 0x16, 0x6c, 0x55, 0x10,
];
#[must_use]
pub fn connection_id(msgpack: &[u8], nonce: u64, vault: Option<Address>) -> [u8; 32] {
connection_id_expiring(msgpack, nonce, vault, None)
}
#[must_use]
pub fn connection_id_expiring(
msgpack: &[u8],
nonce: u64,
vault: Option<Address>,
expires_after: Option<u64>,
) -> [u8; 32] {
let sponge = Keccak256::new().chain(msgpack).chain(&nonce.to_be_bytes());
let sponge = match vault {
Some(vault) => sponge.chain(&[0x01]).chain(vault.bytes()),
None => sponge.chain(&[0x00]),
};
match expires_after {
Some(expires_after) => sponge.chain(&[0x00]).chain(&expires_after.to_be_bytes()),
None => sponge,
}
.finalize()
}
#[must_use]
pub fn phantom_agent(connection_id: [u8; 32], network: Network) -> TypedStruct<'static> {
TypedStruct::new("Agent")
.field("source", Field::String(network.agent_source()))
.field("connectionId", Field::Bytes32(connection_id))
}
#[must_use]
pub fn l1_digest(msgpack: &[u8], nonce: u64, vault: Option<Address>, network: Network) -> [u8; 32] {
agent_digest(connection_id(msgpack, nonce, vault), network)
}
#[must_use]
pub fn agent_digest(connection_id: [u8; 32], network: Network) -> [u8; 32] {
let source = match network {
Network::Mainnet => &SOURCE_MAINNET,
Network::Testnet => &SOURCE_TESTNET,
};
let agent = Keccak256::new().chain(&AGENT_TYPE_HASH).chain(source).chain(&connection_id).finalize();
Keccak256::new().chain(&[0x19, 0x01]).chain(&EXCHANGE_SEPARATOR).chain(&agent).finalize()
}
pub trait L1Action {
fn action(&self) -> Map;
#[must_use]
fn msgpack(&self) -> Vec<u8> {
self.action().to_msgpack()
}
#[must_use]
fn wire(&self) -> String {
self.action().to_json().compact()
}
#[must_use]
fn connection_id(&self, nonce: u64, vault: Option<Address>) -> [u8; 32] {
connection_id(&self.msgpack(), nonce, vault)
}
#[must_use]
fn typed(&self, nonce: u64, vault: Option<Address>, network: Network) -> TypedStruct<'static> {
phantom_agent(self.connection_id(nonce, vault), network)
}
#[must_use]
fn digest(&self, nonce: u64, vault: Option<Address>, network: Network) -> [u8; 32] {
agent_digest(self.connection_id(nonce, vault), network)
}
#[must_use]
fn wallet_json(&self, nonce: u64, vault: Option<Address>, network: Network) -> String {
self.typed(nonce, vault, network).wallet_json(&Domain::EXCHANGE)
}
#[must_use]
fn body(&self, nonce: u64, signature: &Signature, vault: Option<Address>) -> String {
body::l1(&self.action(), nonce, signature, vault)
}
}
impl L1Action for Map {
fn action(&self) -> Map {
self.clone()
}
}
#[cfg(test)]
#[path = "tests/l1.rs"]
mod tests;