use alloc::string::String;
use alloc::vec::Vec;
use crate::account::AccountId;
use crate::transaction::{ProvenTransaction, TransactionId};
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use crate::{Felt, Hasher, Word, ZERO};
#[derive(Debug, Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct BatchId(Word);
impl BatchId {
pub fn from_transactions<'tx, T>(txs: T) -> Self
where
T: Iterator<Item = &'tx ProvenTransaction>,
{
Self::from_ids(txs.map(|tx| (tx.id(), tx.account_id())))
}
pub fn from_ids(iter: impl IntoIterator<Item = (TransactionId, AccountId)>) -> Self {
let mut elements: Vec<Felt> = Vec::new();
for (tx_id, account_id) in iter {
elements.extend_from_slice(tx_id.as_elements());
let [account_id_prefix, account_id_suffix] = <[Felt; 2]>::from(account_id);
elements.extend_from_slice(&[account_id_prefix, account_id_suffix, ZERO, ZERO]);
}
Self(Hasher::hash_elements(&elements))
}
pub fn as_elements(&self) -> &[Felt] {
self.0.as_elements()
}
pub fn as_bytes(&self) -> [u8; 32] {
self.0.as_bytes()
}
pub fn to_hex(&self) -> String {
self.0.to_hex()
}
}
impl core::fmt::Display for BatchId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_hex())
}
}
impl Serializable for BatchId {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target);
}
}
impl Deserializable for BatchId {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
Ok(Self(Word::read_from(source)?))
}
}