diff --git a/chain/rust/src/crypto/hash.rs b/chain/rust/src/crypto/hash.rs index b64fe77e..bd5f462c 100644 --- a/chain/rust/src/crypto/hash.rs +++ b/chain/rust/src/crypto/hash.rs @@ -17,7 +17,7 @@ pub fn hash_auxiliary_data(auxiliary_data: &AuxiliaryData) -> AuxiliaryDataHash } pub fn hash_transaction(tx_body: &TransactionBody) -> TransactionHash { - TransactionHash::from(blake2b256(tx_body.to_canonical_cbor_bytes().as_ref())) + TransactionHash::from(blake2b256(tx_body.to_cbor_bytes().as_ref())) } pub fn hash_plutus_data(plutus_data: &PlutusData) -> DatumHash { diff --git a/chain/rust/src/transaction/mod.rs b/chain/rust/src/transaction/mod.rs index 19f6240d..1030d1c2 100644 --- a/chain/rust/src/transaction/mod.rs +++ b/chain/rust/src/transaction/mod.rs @@ -24,8 +24,7 @@ use cbor_encodings::{ TransactionWitnessSetEncoding, }; use cml_core::ordered_hash_map::OrderedHashMap; -use cml_core::serialization::{LenEncoding, Serialize, StringEncoding}; -use cml_crypto::blake2b256; +use cml_core::serialization::{LenEncoding, StringEncoding}; use std::collections::BTreeMap; #[derive( @@ -376,10 +375,6 @@ impl TransactionBody { encodings: None, } } - - pub fn hash(&self) -> [u8; 32] { - blake2b256(&self.to_cbor_bytes()) - } } #[derive( diff --git a/chain/rust/src/transaction/utils.rs b/chain/rust/src/transaction/utils.rs index 751614ec..5d302de4 100644 --- a/chain/rust/src/transaction/utils.rs +++ b/chain/rust/src/transaction/utils.rs @@ -6,9 +6,17 @@ use crate::{ transaction::{DatumOption, ScriptRef, TransactionOutput}, Value, }; -use cml_crypto::{DatumHash, Ed25519KeyHash}; +use cml_crypto::{DatumHash, Ed25519KeyHash, TransactionHash}; -use super::{AlonzoFormatTxOut, ConwayFormatTxOut, NativeScript, TransactionWitnessSet}; +use super::{ + AlonzoFormatTxOut, ConwayFormatTxOut, NativeScript, TransactionBody, TransactionWitnessSet, +}; + +impl TransactionBody { + pub fn hash(&self) -> TransactionHash { + crate::crypto::hash::hash_transaction(self) + } +} impl TransactionOutput { pub fn new( diff --git a/multi-era/rust/src/allegra/mod.rs b/multi-era/rust/src/allegra/mod.rs index 067a7662..0cfe0b01 100644 --- a/multi-era/rust/src/allegra/mod.rs +++ b/multi-era/rust/src/allegra/mod.rs @@ -23,9 +23,8 @@ use cml_chain::transaction::{NativeScript, TransactionInput}; use cml_chain::Withdrawals; use cml_chain::{DeltaCoin, LenEncoding, TransactionIndex}; use cml_core::ordered_hash_map::OrderedHashMap; -use cml_core::serialization::Serialize; use cml_core::Epoch; -use cml_crypto::{blake2b256, Ed25519KeyHash, GenesisDelegateHash, GenesisHash, VRFKeyHash}; +use cml_crypto::{Ed25519KeyHash, GenesisDelegateHash, GenesisHash, VRFKeyHash}; use std::collections::BTreeMap; use self::cbor_encodings::{MoveInstantaneousRewardEncoding, MoveInstantaneousRewardsCertEncoding}; @@ -188,10 +187,6 @@ impl AllegraTransactionBody { encodings: None, } } - - pub fn hash(&self) -> [u8; 32] { - blake2b256(&self.to_cbor_bytes()) - } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] diff --git a/multi-era/rust/src/allegra/utils.rs b/multi-era/rust/src/allegra/utils.rs index 1ca78594..cae7a14e 100644 --- a/multi-era/rust/src/allegra/utils.rs +++ b/multi-era/rust/src/allegra/utils.rs @@ -1,6 +1,15 @@ use cml_chain::{auxdata::AuxiliaryData, transaction::TransactionWitnessSet}; -use super::{AllegraAuxiliaryData, AllegraTransactionWitnessSet}; +use super::{AllegraAuxiliaryData, AllegraTransactionBody, AllegraTransactionWitnessSet}; + +use cml_core::serialization::Serialize; +use cml_crypto::{blake2b256, TransactionHash}; + +impl AllegraTransactionBody { + pub fn hash(&self) -> TransactionHash { + blake2b256(&self.to_cbor_bytes()).into() + } +} impl From for AuxiliaryData { fn from(aux: AllegraAuxiliaryData) -> Self { diff --git a/multi-era/rust/src/alonzo/mod.rs b/multi-era/rust/src/alonzo/mod.rs index 51936be2..e6315069 100644 --- a/multi-era/rust/src/alonzo/mod.rs +++ b/multi-era/rust/src/alonzo/mod.rs @@ -24,8 +24,6 @@ use cml_chain::transaction::{AlonzoFormatTxOut, NativeScript, RequiredSigners, T use cml_chain::TransactionIndex; use cml_chain::{Epoch, NetworkId, Rational, UnitInterval, Withdrawals}; use cml_core::ordered_hash_map::OrderedHashMap; -use cml_core::serialization::Serialize; -use cml_crypto::blake2b256; use std::collections::BTreeMap; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] @@ -259,10 +257,6 @@ impl AlonzoTransactionBody { encodings: None, } } - - pub fn hash(&self) -> [u8; 32] { - blake2b256(&self.to_cbor_bytes()) - } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] diff --git a/multi-era/rust/src/alonzo/utils.rs b/multi-era/rust/src/alonzo/utils.rs index ab2f213f..7dd8714f 100644 --- a/multi-era/rust/src/alonzo/utils.rs +++ b/multi-era/rust/src/alonzo/utils.rs @@ -3,7 +3,16 @@ use cml_chain::{ transaction::TransactionWitnessSet, }; -use super::{AlonzoAuxiliaryData, AlonzoTransactionWitnessSet}; +use super::{AlonzoAuxiliaryData, AlonzoTransactionBody, AlonzoTransactionWitnessSet}; + +use cml_core::serialization::Serialize; +use cml_crypto::{blake2b256, TransactionHash}; + +impl AlonzoTransactionBody { + pub fn hash(&self) -> TransactionHash { + blake2b256(&self.to_cbor_bytes()).into() + } +} impl From for AuxiliaryData { fn from(aux: AlonzoAuxiliaryData) -> Self { diff --git a/multi-era/rust/src/babbage/mod.rs b/multi-era/rust/src/babbage/mod.rs index 0a80bf7e..9e193d50 100644 --- a/multi-era/rust/src/babbage/mod.rs +++ b/multi-era/rust/src/babbage/mod.rs @@ -28,10 +28,9 @@ use cml_chain::transaction::{ use cml_chain::{Epoch, NetworkId, Rational, UnitInterval, Withdrawals}; use cml_core::ordered_hash_map::OrderedHashMap; -use cml_core::serialization::{LenEncoding, Serialize}; +use cml_core::serialization::LenEncoding; use cml_core::{Int, TransactionIndex}; -use cml_crypto::blake2b256; use std::collections::BTreeMap; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] @@ -356,10 +355,6 @@ impl BabbageTransactionBody { encodings: None, } } - - pub fn hash(&self) -> [u8; 32] { - blake2b256(&self.to_cbor_bytes()) - } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] diff --git a/multi-era/rust/src/babbage/utils.rs b/multi-era/rust/src/babbage/utils.rs index 7a1fd6fc..b7cc38ff 100644 --- a/multi-era/rust/src/babbage/utils.rs +++ b/multi-era/rust/src/babbage/utils.rs @@ -4,7 +4,18 @@ use cml_chain::{ Script, }; -use super::{BabbageAuxiliaryData, BabbageScript, BabbageTransactionWitnessSet}; +use super::{ + BabbageAuxiliaryData, BabbageScript, BabbageTransactionBody, BabbageTransactionWitnessSet, +}; + +use cml_core::serialization::Serialize; +use cml_crypto::{blake2b256, TransactionHash}; + +impl BabbageTransactionBody { + pub fn hash(&self) -> TransactionHash { + blake2b256(&self.to_cbor_bytes()).into() + } +} impl From for Script { fn from(script: BabbageScript) -> Script { diff --git a/multi-era/rust/src/byron/transaction/mod.rs b/multi-era/rust/src/byron/transaction/mod.rs index 1c744a54..84463d43 100644 --- a/multi-era/rust/src/byron/transaction/mod.rs +++ b/multi-era/rust/src/byron/transaction/mod.rs @@ -2,12 +2,11 @@ // https://github.com/dcSpark/cddl-codegen pub mod serialization; +pub mod utils; use crate::byron::{Blake2b256, ByronPubKey, ByronSignature, ByronTxId}; use cml_chain::byron::ByronTxOut; -use cml_core::serialization::ToBytes; -use cml_crypto::blake2b256; use std::collections::BTreeMap; use super::ByronAny; @@ -122,10 +121,6 @@ impl ByronTx { attrs, } } - - pub fn hash(&self) -> [u8; 32] { - blake2b256(&self.to_bytes()) - } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] diff --git a/multi-era/rust/src/byron/transaction/utils.rs b/multi-era/rust/src/byron/transaction/utils.rs new file mode 100644 index 00000000..f6545c9c --- /dev/null +++ b/multi-era/rust/src/byron/transaction/utils.rs @@ -0,0 +1,9 @@ +use super::ByronTx; +use cml_core::serialization::ToBytes; +use cml_crypto::{blake2b256, TransactionHash}; + +impl ByronTx { + pub fn hash(&self) -> TransactionHash { + blake2b256(&self.to_bytes()).into() + } +} diff --git a/multi-era/rust/src/mary/mod.rs b/multi-era/rust/src/mary/mod.rs index 67dc5f03..efaea9e9 100644 --- a/multi-era/rust/src/mary/mod.rs +++ b/multi-era/rust/src/mary/mod.rs @@ -3,6 +3,7 @@ pub mod cbor_encodings; pub mod serialization; +pub mod utils; use crate::allegra::{AllegraAuxiliaryData, AllegraCertificate, AllegraTransactionWitnessSet}; use crate::shelley::{ShelleyHeader, ShelleyUpdate}; @@ -15,8 +16,6 @@ use cml_chain::transaction::TransactionInput; use cml_chain::TransactionIndex; use cml_chain::Withdrawals; use cml_core::ordered_hash_map::OrderedHashMap; -use cml_core::serialization::Serialize; -use cml_crypto::blake2b256; use std::collections::BTreeMap; use self::cbor_encodings::MaryTransactionOutputEncoding; @@ -108,10 +107,6 @@ impl MaryTransactionBody { encodings: None, } } - - pub fn hash(&self) -> [u8; 32] { - blake2b256(&self.to_cbor_bytes()) - } } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)] diff --git a/multi-era/rust/src/mary/utils.rs b/multi-era/rust/src/mary/utils.rs new file mode 100644 index 00000000..64199079 --- /dev/null +++ b/multi-era/rust/src/mary/utils.rs @@ -0,0 +1,9 @@ +use super::MaryTransactionBody; +use cml_core::serialization::Serialize; +use cml_crypto::{blake2b256, TransactionHash}; + +impl MaryTransactionBody { + pub fn hash(&self) -> TransactionHash { + blake2b256(&self.to_cbor_bytes()).into() + } +} diff --git a/multi-era/rust/src/shelley/mod.rs b/multi-era/rust/src/shelley/mod.rs index 4645f2c2..23985e7c 100644 --- a/multi-era/rust/src/shelley/mod.rs +++ b/multi-era/rust/src/shelley/mod.rs @@ -26,9 +26,8 @@ use cml_chain::crypto::{ use cml_chain::transaction::TransactionInput; use cml_chain::{Epoch, LenEncoding, Rational, UnitInterval, Withdrawals}; use cml_core::ordered_hash_map::OrderedHashMap; -use cml_core::serialization::Serialize; use cml_core::TransactionIndex; -use cml_crypto::{blake2b256, GenesisDelegateHash, VRFKeyHash}; +use cml_crypto::{GenesisDelegateHash, VRFKeyHash}; use std::collections::BTreeMap; use crate::allegra::MIRPot; @@ -472,10 +471,6 @@ impl ShelleyTransactionBody { encodings: None, } } - - pub fn hash(&self) -> [u8; 32] { - blake2b256(&self.to_cbor_bytes()) - } } pub type ShelleyTransactionIndex = u16; diff --git a/multi-era/rust/src/shelley/utils.rs b/multi-era/rust/src/shelley/utils.rs index f8ee4dfe..e16d0789 100644 --- a/multi-era/rust/src/shelley/utils.rs +++ b/multi-era/rust/src/shelley/utils.rs @@ -1,6 +1,15 @@ use cml_chain::transaction::{NativeScript, TransactionWitnessSet}; -use super::{MultisigScript, ShelleyTransactionWitnessSet}; +use super::{MultisigScript, ShelleyTransactionBody, ShelleyTransactionWitnessSet}; + +use cml_core::serialization::Serialize; +use cml_crypto::{blake2b256, TransactionHash}; + +impl ShelleyTransactionBody { + pub fn hash(&self) -> TransactionHash { + blake2b256(&self.to_cbor_bytes()).into() + } +} impl From for TransactionWitnessSet { fn from(wits: ShelleyTransactionWitnessSet) -> Self { diff --git a/multi-era/rust/src/utils.rs b/multi-era/rust/src/utils.rs index eb6e1f77..17a750d8 100644 --- a/multi-era/rust/src/utils.rs +++ b/multi-era/rust/src/utils.rs @@ -790,7 +790,7 @@ impl MultiEraTransactionBody { } } - pub fn hash(&self) -> [u8; 32] { + pub fn hash(&self) -> TransactionHash { match self { MultiEraTransactionBody::Byron(tx) => tx.hash(), MultiEraTransactionBody::Shelley(tx) => tx.hash(),