Skip to content

Commit aac3103

Browse files
Remove duplicated hash helpers
1 parent 36d7c45 commit aac3103

File tree

5 files changed

+42
-79
lines changed

5 files changed

+42
-79
lines changed

rust/rbac-registration/src/cardano/cip509/cip509.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ fn decode_input_hash(
470470
};
471471

472472
let len = bytes.len();
473-
if let Ok(v) = TxInputHash::try_from(bytes) {
473+
if let Ok(v) = TxInputHash::try_from(bytes.as_slice()) {
474474
Some(v)
475475
} else {
476476
report.invalid_value(
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
//! Transaction input hash type
22
3+
use anyhow::Context;
4+
use cardano_blockchain_types::hashes::Blake2b128Hash;
5+
36
/// A 16-byte hash of the transaction inputs field.
47
///
58
/// This type is described [here].
69
///
710
/// [here]: https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/README.md#key-1-txn-inputs-hash
8-
#[derive(Debug, PartialEq, Clone, Default)]
9-
pub struct TxInputHash([u8; 16]);
11+
#[derive(Debug, PartialEq, Clone)]
12+
pub struct TxInputHash(Blake2b128Hash);
1013

1114
impl From<[u8; 16]> for TxInputHash {
1215
fn from(bytes: [u8; 16]) -> Self {
13-
TxInputHash(bytes)
16+
Self(Blake2b128Hash::from(bytes))
1417
}
1518
}
1619

17-
impl TryFrom<Vec<u8>> for TxInputHash {
18-
type Error = &'static str;
19-
20-
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
21-
if vec.len() == 16 {
22-
let mut array = [0u8; 16];
23-
array.copy_from_slice(&vec);
24-
Ok(TxInputHash(array))
25-
} else {
26-
Err("Input Vec must be exactly 16 bytes")
27-
}
28-
}
29-
}
20+
impl TryFrom<&[u8]> for TxInputHash {
21+
type Error = anyhow::Error;
3022

31-
impl From<TxInputHash> for Vec<u8> {
32-
fn from(val: TxInputHash) -> Self {
33-
val.0.to_vec()
23+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
24+
let hash = Blake2b128Hash::try_from(value).context("Invalid transaction input hash")?;
25+
Ok(Self(hash))
3426
}
3527
}
3628

37-
impl From<TxInputHash> for [u8; 16] {
38-
fn from(val: TxInputHash) -> Self {
39-
val.0
29+
impl From<Blake2b128Hash> for TxInputHash {
30+
fn from(value: Blake2b128Hash) -> Self {
31+
Self(value)
4032
}
4133
}

rust/rbac-registration/src/cardano/cip509/validation.rs

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
2424
use std::borrow::Cow;
2525

26-
use cardano_blockchain_types::{TxnWitness, VKeyHash};
26+
use cardano_blockchain_types::{
27+
hashes::{Blake2b128Hash, Blake2b256Hash},
28+
TxnWitness, VKeyHash,
29+
};
2730
use catalyst_types::problem_report::ProblemReport;
2831
use pallas::{
2932
codec::{
@@ -34,10 +37,7 @@ use pallas::{
3437
};
3538

3639
use super::utils::cip19::compare_key_hash;
37-
use crate::{
38-
cardano::cip509::{types::TxInputHash, Cip0134UriSet, LocalRefInt, RoleData},
39-
utils::hashing::{blake2b_128, blake2b_256},
40-
};
40+
use crate::cardano::cip509::{types::TxInputHash, Cip0134UriSet, LocalRefInt, RoleData};
4141

4242
/// Context-specific primitive type with tag number 6 (`raw_tag` 134) for
4343
/// uniform resource identifier (URI) in the subject alternative name extension.
@@ -77,24 +77,14 @@ pub fn validate_txn_inputs_hash(
7777
}
7878
}
7979

80-
match blake2b_128(&buffer).map(TxInputHash::from) {
81-
Ok(h) if h == *hash => {
82-
// All good - the calculated hash is the same as in Cip509.
83-
},
84-
Ok(h) => {
85-
report.invalid_value(
86-
"txn_inputs_hash",
87-
&format!("{h:?}"),
88-
&format!("Must be equal to the value in Cip509 ({hash:?})"),
89-
context,
90-
);
91-
},
92-
Err(e) => {
93-
report.other(
94-
&format!("Failed to hash transaction inputs: {e:?}"),
95-
context,
96-
);
97-
},
80+
let calculated_hash = TxInputHash::from(Blake2b128Hash::new(&buffer));
81+
if &calculated_hash != hash {
82+
report.invalid_value(
83+
"txn_inputs_hash",
84+
&format!("{hash:?}"),
85+
&format!("Must be equal to the value in Cip509 ({hash:?})"),
86+
context,
87+
);
9888
}
9989
}
10090

@@ -108,23 +98,23 @@ pub fn validate_aux(
10898
report.other("Auxiliary data hash not found in transaction", context);
10999
return;
110100
};
111-
112-
match blake2b_256(auxiliary_data) {
113-
Ok(h) if h == ***auxiliary_data_hash => {
114-
// The hash is correct.
115-
},
116-
Ok(h) => {
117-
report.other(
118-
&format!("Incorrect transaction auxiliary data hash = '{h:?}', expected = '{auxiliary_data_hash:?}'"),
119-
context,
120-
);
121-
},
101+
let auxiliary_data_hash = match Blake2b256Hash::try_from(auxiliary_data_hash.as_slice()) {
102+
Ok(v) => v,
122103
Err(e) => {
123104
report.other(
124-
&format!("Failed to hash transaction auxiliary data: {e:?}"),
105+
&format!("Invalid transaction auxiliary data hash: {e:?}"),
125106
context,
126107
);
108+
return;
127109
},
110+
};
111+
112+
let hash = Blake2b256Hash::new(auxiliary_data);
113+
if hash != auxiliary_data_hash {
114+
report.other(
115+
&format!("Incorrect transaction auxiliary data hash = '{hash:?}', expected = '{auxiliary_data_hash:?}'"),
116+
context,
117+
);
128118
}
129119
}
130120

rust/rbac-registration/src/utils/hashing.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Utility functions for the RBAC registration module.
22
3-
pub(crate) mod decode_helper;
3+
pub mod decode_helper;
4+
// TODO: FIXME: Remove module or pub crate?
45
pub(crate) mod general;
5-
pub(crate) mod hashing;
66

77
#[cfg(test)]
88
pub mod test;

0 commit comments

Comments
 (0)