Skip to content

Commit af62ad8

Browse files
committed
Refactor: Consolidate hash type definitions and unify conversions across modules
1 parent a3dd388 commit af62ad8

File tree

7 files changed

+91
-76
lines changed

7 files changed

+91
-76
lines changed

codec/src/map_parameters.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,13 +912,13 @@ pub fn map_governance_proposals_procedures(
912912
fn map_voter(voter: &conway::Voter) -> Voter {
913913
match voter {
914914
conway::Voter::ConstitutionalCommitteeKey(key_hash) => {
915-
Voter::ConstitutionalCommitteeKey(to_hash(key_hash))
915+
Voter::ConstitutionalCommitteeKey(to_hash(key_hash).into())
916916
}
917917
conway::Voter::ConstitutionalCommitteeScript(script_hash) => {
918-
Voter::ConstitutionalCommitteeScript(to_hash(script_hash))
918+
Voter::ConstitutionalCommitteeScript(to_hash(script_hash).into())
919919
}
920-
conway::Voter::DRepKey(addr_key_hash) => Voter::DRepKey(to_hash(addr_key_hash)),
921-
conway::Voter::DRepScript(script_hash) => Voter::DRepScript(to_hash(script_hash)),
920+
conway::Voter::DRepKey(addr_key_hash) => Voter::DRepKey(to_hash(addr_key_hash).into()),
921+
conway::Voter::DRepScript(script_hash) => Voter::DRepScript(to_hash(script_hash).into()),
922922
conway::Voter::StakePoolKey(key_hash) => Voter::StakePoolKey(to_pool_id(key_hash)),
923923
}
924924
}

common/src/address.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(dead_code)]
44

55
use crate::cip19::{VarIntDecoder, VarIntEncoder};
6-
use crate::{Credential, KeyHash, NetworkId, StakeCredential};
6+
use crate::{Credential, KeyHash, NetworkId, ScriptHash, StakeCredential};
77
use anyhow::{anyhow, bail, Result};
88
use crc::{Crc, CRC_32_ISO_HDLC};
99
use minicbor::data::IanaTag;
@@ -100,12 +100,12 @@ pub enum ShelleyAddressPaymentPart {
100100

101101
/// Payment to a script
102102
#[n(1)]
103-
ScriptHash(#[n(0)] KeyHash),
103+
ScriptHash(#[n(0)] ScriptHash),
104104
}
105105

106106
impl Default for ShelleyAddressPaymentPart {
107107
fn default() -> Self {
108-
Self::PaymentKeyHash([0u8; 28].into())
108+
Self::PaymentKeyHash(KeyHash::default())
109109
}
110110
}
111111

@@ -159,7 +159,7 @@ pub enum ShelleyAddressDelegationPart {
159159

160160
/// Delegation to script key hash
161161
#[n(2)]
162-
ScriptHash(#[n(0)] KeyHash),
162+
ScriptHash(#[n(0)] ScriptHash),
163163

164164
/// Delegation to pointer
165165
#[n(3)]
@@ -540,7 +540,7 @@ impl Default for StakeAddress {
540540
fn default() -> Self {
541541
StakeAddress {
542542
network: NetworkId::Mainnet,
543-
credential: StakeCredential::AddrKeyHash([0u8; 28].into()),
543+
credential: StakeCredential::AddrKeyHash(KeyHash::default()),
544544
}
545545
}
546546
}

common/src/types.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(dead_code)]
44

55
use crate::hash::Hash;
6+
use crate::serialization::Bech32Conversion;
67
use crate::{
78
address::{Address, ShelleyAddress, StakeAddress},
89
declare_hash_type, declare_hash_type_with_bech32, protocol_params,
@@ -439,9 +440,13 @@ impl Default for UTXODelta {
439440
}
440441
}
441442

443+
/// Key hash
442444
pub type KeyHash = Hash<28>;
443445

446+
/// Script hash
444447
pub type ScriptHash = KeyHash;
448+
449+
/// Address key hash
445450
pub type AddrKeyhash = KeyHash;
446451

447452
/// Script identifier
@@ -451,8 +456,11 @@ declare_hash_type!(BlockHash, 32);
451456
declare_hash_type!(TxHash, 32);
452457
declare_hash_type_with_bech32!(VrfKeyHash, 32, "vrf_vk");
453458
declare_hash_type_with_bech32!(PoolId, 28, "pool");
454-
// declare_hash_type_with_bech32!(DrepKey, 28, "drep");
455-
// declare_hash_type_with_bech32!(DrepScriptKey, 28, "drep_script");
459+
460+
declare_hash_type_with_bech32!(ConstitutionalCommitteeKeyHash, 28, "cc_hot");
461+
declare_hash_type_with_bech32!(ConstitutionalCommitteeScriptHash, 28, "cc_hot_script");
462+
declare_hash_type_with_bech32!(DrepKeyHash, 28, "drep");
463+
declare_hash_type_with_bech32!(DRepScriptHash, 28, "drep_script");
456464

457465
/// Data hash used for metadata, anchors (SHA256)
458466
pub type DataHash = Vec<u8>;
@@ -641,10 +649,10 @@ pub struct PotDelta {
641649
)]
642650
pub enum Credential {
643651
/// Script hash. NOTE: Order matters when parsing Haskell Node Snapshot data.
644-
ScriptHash(#[serde_as(as = "Hex")] KeyHash),
652+
ScriptHash(#[serde_as(as = "Hex")] ScriptHash),
645653

646654
/// Address key hash
647-
AddrKeyHash(#[serde_as(as = "Hex")] KeyHash),
655+
AddrKeyHash(#[serde_as(as = "Hex")] AddrKeyhash),
648656
}
649657

650658
impl Credential {
@@ -1663,33 +1671,26 @@ impl GovernanceAction {
16631671
serde::Serialize, serde::Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Hash,
16641672
)]
16651673
pub enum Voter {
1666-
ConstitutionalCommitteeKey(AddrKeyhash),
1667-
ConstitutionalCommitteeScript(ScriptHash),
1668-
DRepKey(AddrKeyhash),
1669-
DRepScript(ScriptHash),
1674+
ConstitutionalCommitteeKey(ConstitutionalCommitteeKeyHash),
1675+
ConstitutionalCommitteeScript(ConstitutionalCommitteeScriptHash),
1676+
DRepKey(DrepKeyHash),
1677+
DRepScript(DRepScriptHash),
16701678
StakePoolKey(PoolId),
16711679
}
16721680

1673-
impl Voter {
1674-
pub fn to_bech32(&self, hrp: &str, buf: &[u8]) -> String {
1675-
let voter_hrp: Hrp = Hrp::parse(hrp).unwrap();
1676-
bech32::encode::<Bech32>(voter_hrp, buf)
1677-
.unwrap_or_else(|e| format!("Cannot convert {:?} to bech32: {e}", self))
1678-
}
1679-
}
1680-
16811681
impl Display for Voter {
1682-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1683-
match self {
1684-
Voter::ConstitutionalCommitteeKey(h) => {
1685-
write!(f, "{}", self.to_bech32("cc_hot", h.as_ref()))
1686-
}
1687-
Voter::ConstitutionalCommitteeScript(s) => {
1688-
write!(f, "{}", self.to_bech32("cc_hot_script", s.as_ref()))
1689-
}
1690-
Voter::DRepKey(k) => write!(f, "{}", self.to_bech32("drep", k.as_ref())),
1691-
Voter::DRepScript(s) => write!(f, "{}", self.to_bech32("drep_script", s.as_ref())),
1692-
Voter::StakePoolKey(k) => write!(f, "{}", self.to_bech32("pool", k.as_ref())),
1682+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1683+
let bech32 = match self {
1684+
Voter::ConstitutionalCommitteeKey(h) => h.to_bech32(),
1685+
Voter::ConstitutionalCommitteeScript(s) => s.to_bech32(),
1686+
Voter::DRepKey(k) => k.to_bech32(),
1687+
Voter::DRepScript(s) => s.to_bech32(),
1688+
Voter::StakePoolKey(k) => k.to_bech32(),
1689+
};
1690+
1691+
match bech32 {
1692+
Ok(s) => write!(f, "{}", s),
1693+
Err(e) => write!(f, "<invalid: {}>", e),
16931694
}
16941695
}
16951696
}

modules/drep_state/src/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ impl State {
261261
for (tx_hash, voting_procedures) in voting_procedures {
262262
for (voter, single_votes) in &voting_procedures.votes {
263263
let drep_cred = match voter {
264-
Voter::DRepKey(k) => DRepCredential::AddrKeyHash(*k),
265-
Voter::DRepScript(s) => DRepCredential::ScriptHash(*s),
264+
Voter::DRepKey(k) => DRepCredential::AddrKeyHash(k.into_inner().into()),
265+
Voter::DRepScript(s) => DRepCredential::ScriptHash(s.into_inner().into()),
266266
_ => continue,
267267
};
268268

0 commit comments

Comments
 (0)