Skip to content

Commit 7c730cc

Browse files
committed
Refactor: Replace KeyHash::try_from with .try_into() for consistent hash conversion across modules
1 parent 87b2ea6 commit 7c730cc

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
lines changed

common/src/snapshot/streaming_snapshot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'b, C> minicbor::decode::Decode<'b, C> for StakeCredential {
6666
0 => {
6767
// ScriptHash variant (first in enum) - decode bytes directly
6868
let bytes = d.bytes()?;
69-
let key_hash = KeyHash::try_from(bytes).map_err(|_| {
69+
let key_hash = bytes.try_into().map_err(|_| {
7070
minicbor::decode::Error::message(
7171
"invalid length for ScriptHash in StakeCredential",
7272
)
@@ -76,7 +76,7 @@ impl<'b, C> minicbor::decode::Decode<'b, C> for StakeCredential {
7676
1 => {
7777
// AddrKeyHash variant (second in enum) - decodes bytes directly
7878
let bytes = d.bytes()?;
79-
let key_hash = KeyHash::try_from(bytes).map_err(|_| {
79+
let key_hash = bytes.try_into().map_err(|_| {
8080
minicbor::decode::Error::message(
8181
"invalid length for AddrKeyHash in StakeCredential",
8282
)
@@ -309,7 +309,7 @@ impl<'b, C> minicbor::Decode<'b, C> for Account {
309309

310310
pub use crate::types::AddrKeyhash;
311311
pub use crate::types::ScriptHash;
312-
use crate::{KeyHash, PoolId};
312+
use crate::{PoolId};
313313
/// Alias minicbor as cbor for pool_params module
314314
pub use minicbor as cbor;
315315

common/src/types.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,10 @@ impl Credential {
653653
hex_str
654654
))
655655
} else {
656-
KeyHash::try_from(key_hash.as_slice())
657-
.map_err(|_| anyhow!("Failed to convert to KeyHash"))
656+
key_hash
657+
.as_slice()
658+
.try_into()
659+
.map_err(|e| anyhow!("Failed to convert to KeyHash {}", e))
658660
}
659661
}
660662

@@ -695,12 +697,7 @@ impl Credential {
695697
));
696698
}
697699

698-
let hash = KeyHash::try_from(data).map_err(|v| {
699-
anyhow!(
700-
"Failed to convert to KeyHash: expected 28 bytes, got {}",
701-
v.len()
702-
)
703-
})?;
700+
let hash = data.try_into().expect("failed to convert to fixed-size array");
704701

705702
match hrp.as_str() {
706703
"drep" => Ok(Credential::AddrKeyHash(hash)),

modules/accounts_state/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ mod tests {
10091009
full_hash[..hash.len().min(28)].copy_from_slice(&hash[..hash.len().min(28)]);
10101010
StakeAddress {
10111011
network: NetworkId::Mainnet,
1012-
credential: StakeCredential::AddrKeyHash(KeyHash::try_from(full_hash).unwrap()),
1012+
credential: StakeCredential::AddrKeyHash(full_hash.try_into().unwrap()),
10131013
}
10141014
}
10151015

modules/accounts_state/src/verifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl Verifier {
166166
continue;
167167
};
168168

169-
expected_rewards.entry(KeyHash::try_from(spo).unwrap()).or_default().push(
169+
expected_rewards.entry(spo.try_into().unwrap()).or_default().push(
170170
RewardDetail {
171171
account: stake_address,
172172
rtype,

modules/chain_store/src/chain_store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use acropolis_common::{
1212
},
1313
queries::misc::Order,
1414
state_history::{StateHistory, StateHistoryStore},
15-
BechOrdAddress, BlockHash, GenesisDelegate, HeavyDelegate, TxHash, VrfKeyHash,
15+
BechOrdAddress, BlockHash, GenesisDelegate, HeavyDelegate, TxHash,
1616
};
1717
use anyhow::{bail, Result};
1818
use caryatid_sdk::{module, Context, Module};
@@ -413,7 +413,7 @@ impl ChainStore {
413413
tx_count: decoded.tx_count() as u64,
414414
output,
415415
fees,
416-
block_vrf: header.vrf_vkey().map(|key| VrfKeyHash::try_from(key).ok().unwrap()),
416+
block_vrf: header.vrf_vkey().map(|key| key.try_into().ok().unwrap()),
417417
op_cert,
418418
op_cert_counter,
419419
previous_block: header.previous_hash().map(|h| BlockHash::from(*h)),

0 commit comments

Comments
 (0)