use alloc::string::String;
use miden_crypto_derive::WordWrapper;
use crate::Word;
use crate::account::AccountId;
use crate::transaction::{OrderedTransactionHeaders, ProvenTransaction, TransactionId};
use crate::utils::serde::{
ByteReader,
ByteWriter,
Deserializable,
DeserializationError,
Serializable,
};
#[derive(Debug, Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash, WordWrapper)]
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 {
Self(OrderedTransactionHeaders::compute_commitment(iter))
}
}
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)?))
}
}