diff --git a/rust/src/protocol_types/fixed_tx.rs b/rust/src/protocol_types/fixed_tx.rs index cad72646..79989a42 100644 --- a/rust/src/protocol_types/fixed_tx.rs +++ b/rust/src/protocol_types/fixed_tx.rs @@ -62,6 +62,21 @@ impl FixedTransaction { }) } + pub fn new_from_body_bytes(raw_body: &[u8]) -> Result { + let body = TransactionBody::from_bytes(raw_body.to_vec())?; + let tx_hash = TransactionHash::from(blake2b256(raw_body)); + + Ok(FixedTransaction { + body, + body_bytes: raw_body.to_vec(), + tx_hash, + witness_set: FixedTxWitnessesSet::new_empty(), + is_valid: true, + auxiliary_data: None, + auxiliary_bytes: None, + }) + } + pub(crate) fn new_with_original_bytes( tx_body: TransactionBody, raw_body: Vec, diff --git a/rust/src/protocol_types/witnesses/fixed_tx_witnesses_set.rs b/rust/src/protocol_types/witnesses/fixed_tx_witnesses_set.rs index d46a079e..1a3efce3 100644 --- a/rust/src/protocol_types/witnesses/fixed_tx_witnesses_set.rs +++ b/rust/src/protocol_types/witnesses/fixed_tx_witnesses_set.rs @@ -30,6 +30,13 @@ impl FixedTxWitnessesSet { } } + pub(crate) fn new_empty() -> Self { + Self { + tx_witnesses_set: TransactionWitnessSet::new(), + raw_parts: TransactionWitnessSetRaw::new(), + } + } + pub fn tx_witnesses_set(&self) -> TransactionWitnessSet { self.tx_witnesses_set.clone() } diff --git a/rust/src/tests/builders/tx_builder.rs b/rust/src/tests/builders/tx_builder.rs index 0aed26d9..6035c607 100644 --- a/rust/src/tests/builders/tx_builder.rs +++ b/rust/src/tests/builders/tx_builder.rs @@ -2785,8 +2785,9 @@ fn build_tx_multisig_1on1_signed() { let mut witness_set = TransactionWitnessSet::new(); let mut vkw = Vkeywitnesses::new(); + let fixed_tx = FixedTransaction::new_from_body_bytes(&body.to_bytes()).unwrap(); vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("c660e50315d76a53d80732efda7630cae8885dfb85c46378684b3c6103e1284a") .unwrap(), diff --git a/rust/src/tests/fees.rs b/rust/src/tests/fees.rs index bc19fb6f..9220a3af 100644 --- a/rust/src/tests/fees.rs +++ b/rust/src/tests/fees.rs @@ -40,8 +40,9 @@ fn tx_simple_utxo() { let mut w = TransactionWitnessSet::new(); let mut vkw = Vkeywitnesses::new(); + let fixed_tx = FixedTransaction::new_from_body_bytes(&body.to_bytes()).unwrap(); vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("c660e50315d76a53d80732efda7630cae8885dfb85c46378684b3c6103e1284a") .unwrap(), @@ -95,8 +96,9 @@ fn tx_simple_byron_utxo() { let mut w = TransactionWitnessSet::new(); let mut bootstrap_wits = BootstrapWitnesses::new(); + let fixed_tx = FixedTransaction::new_from_body_bytes(&body.to_bytes()).unwrap(); bootstrap_wits.add(&make_icarus_bootstrap_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &ByronAddress::from_base58("Ae2tdPwUPEZ6r6zbg4ibhFrNnyKHg7SYuPSfDpjKxgvwFX9LquRep7gj7FQ").unwrap(), &Bip32PrivateKey::from_bytes( &hex::decode("d84c65426109a36edda5375ea67f1b738e1dacf8629f2bb5a2b0b20f3cd5075873bf5cdfa7e533482677219ac7d639e30a38e2e645ea9140855f44ff09e60c52c8b95d0d35fe75a70f9f5633a3e2439b2994b9e2bc851c49e9f91d1a5dcbb1a3").unwrap() @@ -173,8 +175,9 @@ fn tx_multi_utxo() { let mut w = TransactionWitnessSet::new(); let mut vkw = Vkeywitnesses::new(); + let fixed_tx = FixedTransaction::new_from_body_bytes(&body.to_bytes()).unwrap(); vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("c660e50315d76a53d80732efda7630cae8885dfb85c46378684b3c6103e1284a") .unwrap(), @@ -182,7 +185,7 @@ fn tx_multi_utxo() { .unwrap(), )); vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("13fe79205e16c09536acb6f0524d04069f380329d13949698c5f22c65c989eb4") .unwrap(), @@ -283,9 +286,10 @@ fn tx_register_stake() { let mut w = TransactionWitnessSet::new(); let mut vkw = Vkeywitnesses::new(); + let fixed_tx = FixedTransaction::new_from_body_bytes(&body.to_bytes()).unwrap(); // input key witness vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("c660e50315d76a53d80732efda7630cae8885dfb85c46378684b3c6103e1284a") .unwrap(), @@ -294,7 +298,7 @@ fn tx_register_stake() { )); // operator key witness vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("2363f3660b9f3b41685665bf10632272e2d03c258e8a5323436f0f3406293505") .unwrap(), @@ -303,7 +307,7 @@ fn tx_register_stake() { )); // owner key witness vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("5ada7f4d92bce1ee1707c0a0e211eb7941287356e6ed0e76843806e307b07c8d") .unwrap(), @@ -504,9 +508,10 @@ fn tx_withdrawal() { let mut w = TransactionWitnessSet::new(); let mut vkw = Vkeywitnesses::new(); + let fixed_tx = FixedTransaction::new_from_body_bytes(&body.to_bytes()).unwrap(); // input key witness vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("c660e50315d76a53d80732efda7630cae8885dfb85c46378684b3c6103e1284a") .unwrap(), @@ -515,7 +520,7 @@ fn tx_withdrawal() { )); // withdrawal key witness vkw.add(&make_vkey_witness( - &hash_transaction(&body), + &fixed_tx.transaction_hash(), &PrivateKey::from_normal_bytes( &hex::decode("5ada7f4d92bce1ee1707c0a0e211eb7941287356e6ed0e76843806e307b07c8d") .unwrap(), diff --git a/rust/src/utils.rs b/rust/src/utils.rs index 2180a6e4..5c761ad2 100644 --- a/rust/src/utils.rs +++ b/rust/src/utils.rs @@ -579,11 +579,6 @@ pub fn hash_auxiliary_data(auxiliary_data: &AuxiliaryData) -> AuxiliaryDataHash AuxiliaryDataHash::from(blake2b256(&auxiliary_data.to_bytes())) } -#[wasm_bindgen] -pub fn hash_transaction(tx_body: &TransactionBody) -> TransactionHash { - TransactionHash::from(crypto::blake2b256(tx_body.to_bytes().as_ref())) -} - #[wasm_bindgen] pub fn hash_plutus_data(plutus_data: &PlutusData) -> DataHash { DataHash::from(blake2b256(&plutus_data.to_bytes()))