op_alloy_consensus/transaction/
source.rsuse alloc::string::String;
use alloy_primitives::{keccak256, B256};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum DepositSourceDomainIdentifier {
User = 0,
L1Info = 1,
Upgrade = 2,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DepositSourceDomain {
User(UserDepositSource),
L1Info(L1InfoDepositSource),
Upgrade(UpgradeDepositSource),
}
impl DepositSourceDomain {
pub fn source_hash(&self) -> B256 {
match self {
Self::User(ds) => ds.source_hash(),
Self::L1Info(ds) => ds.source_hash(),
Self::Upgrade(ds) => ds.source_hash(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct L1InfoDepositSource {
pub l1_block_hash: B256,
pub seq_number: u64,
}
impl L1InfoDepositSource {
pub const fn new(l1_block_hash: B256, seq_number: u64) -> Self {
Self { l1_block_hash, seq_number }
}
pub fn source_hash(&self) -> B256 {
let mut input = [0u8; 32 * 2];
input[..32].copy_from_slice(&self.l1_block_hash[..]);
input[32 * 2 - 8..].copy_from_slice(&self.seq_number.to_be_bytes());
let deposit_id_hash = keccak256(input);
let mut domain_input = [0u8; 32 * 2];
let identifier_bytes: [u8; 8] =
(DepositSourceDomainIdentifier::L1Info as u64).to_be_bytes();
domain_input[32 - 8..32].copy_from_slice(&identifier_bytes);
domain_input[32..].copy_from_slice(&deposit_id_hash[..]);
keccak256(domain_input)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UserDepositSource {
pub l1_block_hash: B256,
pub log_index: u64,
}
impl UserDepositSource {
pub const fn new(l1_block_hash: B256, log_index: u64) -> Self {
Self { l1_block_hash, log_index }
}
pub fn source_hash(&self) -> B256 {
let mut input = [0u8; 32 * 2];
input[..32].copy_from_slice(&self.l1_block_hash[..]);
input[32 * 2 - 8..].copy_from_slice(&self.log_index.to_be_bytes());
let deposit_id_hash = keccak256(input);
let mut domain_input = [0u8; 32 * 2];
let identifier_bytes: [u8; 8] = (DepositSourceDomainIdentifier::User as u64).to_be_bytes();
domain_input[32 - 8..32].copy_from_slice(&identifier_bytes);
domain_input[32..].copy_from_slice(&deposit_id_hash[..]);
keccak256(domain_input)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UpgradeDepositSource {
pub intent: String,
}
impl UpgradeDepositSource {
pub const fn new(intent: String) -> Self {
Self { intent }
}
pub fn source_hash(&self) -> B256 {
let intent_hash = keccak256(self.intent.as_bytes());
let mut domain_input = [0u8; 32 * 2];
let identifier_bytes: [u8; 8] =
(DepositSourceDomainIdentifier::Upgrade as u64).to_be_bytes();
domain_input[32 - 8..32].copy_from_slice(&identifier_bytes);
domain_input[32..].copy_from_slice(&intent_hash[..]);
keccak256(domain_input)
}
}