Skip to content

Commit 3629032

Browse files
Move define_hashes to catalyst-types
1 parent 20c169d commit 3629032

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
mod auxdata;
44
mod cip134_uri;
55
mod fork;
6-
mod hashes;
76
mod metadata;
87
mod multi_era_block_data;
98
mod network;
@@ -22,7 +21,6 @@ pub use auxdata::{
2221
};
2322
pub use cip134_uri::Cip0134Uri;
2423
pub use fork::Fork;
25-
pub use hashes::{PubKeyHash, TransactionHash};
2624
pub use metadata::cip36::{voting_pk::VotingPubKey, Cip36};
2725
pub use multi_era_block_data::MultiEraBlock;
2826
pub use network::Network;
Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
//! A bunch of specific hashes.
2-
3-
use std::str::FromStr;
4-
5-
use catalyst_types::hashes::{Blake2b224Hash, Blake2b256Hash, Blake2bHashError};
6-
use pallas_crypto::hash::Hash;
7-
8-
/// Defines a new type wrapper for the given hash type.
1+
//! A macro for defining a new type wrappers for the given hash types.
2+
3+
/// Defines a new type wrapper for the given hash types.
4+
///
5+
/// # Examples
6+
///
7+
/// ```
8+
/// use catalyst_types::{define_hashes, hashes::Blake2b128Hash};
9+
///
10+
/// define_hashes!(
11+
/// /// You can document the declared types...
12+
/// (SomeHash, Blake2b128Hash),
13+
/// // ...or not.
14+
/// (AnotherHash, Blake2b128Hash),
15+
/// );
16+
///
17+
/// let hash = SomeHash::new(&[1, 2, 3]);
18+
/// println!("{hash:?}");
19+
/// ```
20+
#[macro_export]
921
macro_rules! define_hashes {
10-
($($(#[$docs:meta])* ($name:ident, $inner:ty)),+) => {
22+
($($(#[$docs:meta])* ($name:ident, $inner:ty)),+ $(,)?) => {
1123
$(
1224
$(#[$docs])*
1325
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -34,26 +46,26 @@ macro_rules! define_hashes {
3446
}
3547

3648
impl TryFrom<&[u8]> for $name {
37-
type Error = Blake2bHashError;
49+
type Error = $crate::hashes::Blake2bHashError;
3850

3951
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
4052
Ok(Self(<$inner>::try_from(value)?))
4153
}
4254
}
4355

4456
impl TryFrom<Vec<u8>> for $name {
45-
type Error = Blake2bHashError;
57+
type Error = $crate::hashes::Blake2bHashError;
4658

4759
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
4860
value.as_slice().try_into()
4961
}
5062
}
5163

52-
impl FromStr for $name {
53-
type Err = Blake2bHashError;
64+
impl std::str::FromStr for $name {
65+
type Err = $crate::hashes::Blake2bHashError;
5466

5567
fn from_str(s: &str) -> Result<Self, Self::Err> {
56-
let hash: $inner = s.parse().map_err(Blake2bHashError::from)?;
68+
let hash: $inner = s.parse().map_err($crate::hashes::Blake2bHashError::from)?;
5769
Ok(Self(hash))
5870
}
5971
}
@@ -78,40 +90,38 @@ macro_rules! define_hashes {
7890
};
7991
}
8092

81-
define_hashes!(
82-
/// A transaction hash - Blake2b-256 hash of a transaction.
83-
(TransactionHash, Blake2b256Hash),
84-
/// A public key hash - raw Blake2b-224 hash of an Ed25519 public key (has no discriminator, just the hash).
85-
(PubKeyHash, Blake2b224Hash)
86-
);
87-
88-
impl From<Hash<32>> for TransactionHash {
89-
fn from(hash: Hash<32>) -> Self {
90-
Self(Blake2b256Hash::from(hash))
91-
}
92-
}
93-
94-
impl From<Hash<28>> for PubKeyHash {
95-
fn from(hash: Hash<28>) -> Self {
96-
Self(Blake2b224Hash::from(hash))
97-
}
98-
}
99-
10093
#[cfg(test)]
10194
mod tests {
102-
use super::*;
95+
use crate::hashes::Blake2b128Hash;
96+
97+
// Define one type without a trailing comma.
98+
define_hashes!((H1, Blake2b128Hash));
99+
// Define one type with a trailing comma and a doc-comment.
100+
define_hashes!(
101+
/// Some documentation.
102+
(H2, Blake2b128Hash),
103+
);
104+
// Define multiple types at once.
105+
define_hashes!(
106+
/// Documentation.
107+
(H3, Blake2b128Hash),
108+
// No documentation.
109+
(H4, Blake2b128Hash),
110+
/// More documentation.
111+
(H5, Blake2b128Hash),
112+
);
103113

104114
// There is little reason to check the conversion itself, it is mostly a demonstration
105115
// that the methods defined by the macro are working.
106116
#[test]
107-
fn roundtrip() {
108-
let hash = TransactionHash::new(&[]);
117+
fn hash_wrapper() {
118+
let hash = H1::new(&[1, 2, 3, 4, 5]);
109119

110120
let v = Vec::from(hash);
111-
let from_slice = TransactionHash::try_from(v.as_slice()).unwrap();
121+
let from_slice = H1::try_from(v.as_slice()).unwrap();
112122
assert_eq!(hash, from_slice);
113123

114-
let from_vec = TransactionHash::try_from(v).unwrap();
124+
let from_vec = H1::try_from(v).unwrap();
115125
assert_eq!(hash, from_vec);
116126
}
117127
}

rust/catalyst-types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
pub mod cbor_utils;
44
pub mod conversion;
5+
pub mod hash_wrapper;
56
pub mod hashes;
67
pub mod id_uri;
78
pub mod mmap_file;

0 commit comments

Comments
 (0)