miden_objects/transaction/
transaction_id.rsuse alloc::string::String;
use core::fmt::{Debug, Display};
use super::{Digest, ExecutedTransaction, Felt, Hasher, ProvenTransaction, Word, WORD_SIZE, ZERO};
use crate::utils::serde::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TransactionId(Digest);
impl TransactionId {
pub fn new(
init_account_hash: Digest,
final_account_hash: Digest,
input_notes_hash: Digest,
output_notes_hash: Digest,
) -> Self {
let mut elements = [ZERO; 4 * WORD_SIZE];
elements[..4].copy_from_slice(init_account_hash.as_elements());
elements[4..8].copy_from_slice(final_account_hash.as_elements());
elements[8..12].copy_from_slice(input_notes_hash.as_elements());
elements[12..].copy_from_slice(output_notes_hash.as_elements());
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()
}
pub fn inner(&self) -> Digest {
self.0
}
}
impl Debug for TransactionId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_hex())
}
}
impl Display for TransactionId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_hex())
}
}
impl From<&ProvenTransaction> for TransactionId {
fn from(tx: &ProvenTransaction) -> Self {
Self::new(
tx.account_update().init_state_hash(),
tx.account_update().final_state_hash(),
tx.input_notes().commitment(),
tx.output_notes().commitment(),
)
}
}
impl From<&ExecutedTransaction> for TransactionId {
fn from(tx: &ExecutedTransaction) -> Self {
let input_notes_hash = tx.input_notes().commitment();
let output_notes_hash = tx.output_notes().commitment();
Self::new(
tx.initial_account().init_hash(),
tx.final_account().hash(),
input_notes_hash,
output_notes_hash,
)
}
}
impl From<Word> for TransactionId {
fn from(value: Word) -> Self {
Self(value.into())
}
}
impl From<Digest> for TransactionId {
fn from(value: Digest) -> Self {
Self(value)
}
}
impl From<TransactionId> for Word {
fn from(id: TransactionId) -> Self {
id.0.into()
}
}
impl From<TransactionId> for [u8; 32] {
fn from(id: TransactionId) -> Self {
id.0.into()
}
}
impl From<&TransactionId> for Word {
fn from(id: &TransactionId) -> Self {
id.0.into()
}
}
impl From<&TransactionId> for [u8; 32] {
fn from(id: &TransactionId) -> Self {
id.0.into()
}
}
impl Serializable for TransactionId {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_bytes(&self.0.to_bytes());
}
}
impl Deserializable for TransactionId {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let id = Digest::read_from(source)?;
Ok(Self(id))
}
}