Skip to content

Commit c8cf12a

Browse files
authored
change Hash<28> to Blake2b224 for public method (#232)
1 parent 0bd19e5 commit c8cf12a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

rust/cardano-blockchain-types/src/stake_address.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::fmt::{Display, Formatter};
66

77
use anyhow::{anyhow, Context};
8+
use catalyst_types::hashes::Blake2b224Hash;
89
use pallas::{
910
crypto::hash::Hash,
1011
ledger::{
@@ -26,8 +27,9 @@ impl StakeAddress {
2627
/// Creates a new instance from the given parameters.
2728
#[allow(clippy::expect_used, clippy::missing_panics_doc)]
2829
#[must_use]
29-
pub fn new(network: Network, is_script: bool, hash: Hash<28>) -> Self {
30+
pub fn new(network: Network, is_script: bool, stake_pk_hash: Blake2b224Hash) -> Self {
3031
let network = network.into();
32+
let hash = stake_pk_hash.into();
3133
// `pallas::StakeAddress` can only be constructed from `ShelleyAddress`, so we are forced
3234
// to create a dummy shelley address. The input hash parameter is used to construct both
3335
// payment and delegation parts, but the payment part isn't used in the stake address
@@ -49,8 +51,8 @@ impl StakeAddress {
4951
#[must_use]
5052
pub fn from_stake_cred(network: Network, cred: &conway::StakeCredential) -> Self {
5153
match cred {
52-
conway::StakeCredential::Scripthash(h) => Self::new(network, true, *h),
53-
conway::StakeCredential::AddrKeyhash(h) => Self::new(network, false, *h),
54+
conway::StakeCredential::Scripthash(h) => Self::new(network, true, (*h).into()),
55+
conway::StakeCredential::AddrKeyhash(h) => Self::new(network, false, (*h).into()),
5456
}
5557
}
5658

@@ -111,7 +113,7 @@ impl TryFrom<&[u8]> for StakeAddress {
111113
v => return Err(anyhow!("Unexpected type value: {v}, header = {header}")),
112114
};
113115

114-
Ok(Self::new(network, is_script, hash))
116+
Ok(Self::new(network, is_script, hash.into()))
115117
}
116118
}
117119

@@ -155,7 +157,7 @@ mod tests {
155157
];
156158

157159
for (network, is_script, hash, expected_header) in test_data {
158-
let stake_address = StakeAddress::new(network, is_script, hash);
160+
let stake_address = StakeAddress::new(network, is_script, hash.into());
159161
assert_eq!(stake_address.is_script(), is_script);
160162

161163
// Check that conversion to bytes includes the expected header value.
@@ -227,7 +229,7 @@ mod tests {
227229
// cSpell:enable
228230

229231
for (network, is_script, hash, expected) in test_data {
230-
let address = StakeAddress::new(network, is_script, hash);
232+
let address = StakeAddress::new(network, is_script, hash.into());
231233
assert_eq!(expected, format!("{address}"));
232234
}
233235
}

rust/catalyst-types/src/hashes.rs

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ impl<const BYTES: usize> From<Hash<BYTES>> for Blake2bHash<BYTES> {
8484
}
8585
}
8686

87+
impl<const BYTES: usize> From<Blake2bHash<BYTES>> for Hash<BYTES> {
88+
#[inline]
89+
fn from(hash: Blake2bHash<BYTES>) -> Self {
90+
hash.0
91+
}
92+
}
93+
8794
impl<const BYTES: usize> From<Blake2bHash<BYTES>> for Vec<u8> {
8895
fn from(val: Blake2bHash<BYTES>) -> Self {
8996
val.0.to_vec()

0 commit comments

Comments
 (0)