Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6eb47b4
Add the purpose getter function to Cip509
stanislav-tkach Jan 27, 2025
b477fd5
Implement SerializeValue and DeserializeValue for UUIDs
stanislav-tkach Jan 27, 2025
9383106
Merge branch 'main' into cip509-purpose-getter
stanislav-tkach Jan 27, 2025
4fa68e7
Export Cip509RbacMetadata type
stanislav-tkach Jan 27, 2025
5fb0602
Merge remote-tracking branch 'origin/cip509-purpose-getter' into cip5…
stanislav-tkach Jan 27, 2025
35b42e5
Implement SerializeValue and DeserializeValue for Blake2bHash
stanislav-tkach Jan 27, 2025
26d8a74
Fix Clippy
stanislav-tkach Jan 27, 2025
84a3e02
Merge branch 'main' into cip509-purpose-getter
stanislav-tkach Jan 27, 2025
328733c
Add TransactionHash and PubKeyHash types
stanislav-tkach Jan 27, 2025
33ba72a
Merge remote-tracking branch 'origin/cip509-purpose-getter' into cip5…
stanislav-tkach Jan 27, 2025
4f7cae2
Merge branch 'main' into cip509-purpose-getter
stanislav-tkach Jan 27, 2025
6c26210
Fix spellcheck
stanislav-tkach Jan 27, 2025
d74e1bb
Merge remote-tracking branch 'origin/cip509-purpose-getter' into cip5…
stanislav-tkach Jan 27, 2025
d0f4924
Fix spellcheck 2
stanislav-tkach Jan 27, 2025
344f3dc
Remove unused dependencies
stanislav-tkach Jan 27, 2025
5ed07ab
Document the macro
stanislav-tkach Jan 27, 2025
b697f71
Use 'meta' instead of 'tt' for docs
stanislav-tkach Jan 27, 2025
eab11ac
Remove cspell exception
stanislav-tkach Jan 28, 2025
c853ce7
Remove -scylla dependency
stanislav-tkach Jan 28, 2025
20c169d
Merge branch 'main' into cip509-purpose-getter
stanislav-tkach Jan 28, 2025
3629032
Move define_hashes to catalyst-types
stanislav-tkach Jan 28, 2025
b6bed0e
Remove import
stanislav-tkach Jan 28, 2025
0b832b3
cleanup
stanislav-tkach Jan 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions rust/catalyst-types/src/hash_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//! A macro for defining a new type wrappers for the given hash types.

/// Defines a new type wrapper for the given hash types.
///
/// # Examples
///
/// ```
/// use catalyst_types::{define_hashes, hashes::Blake2b128Hash};
///
/// define_hashes!(
/// /// You can document the declared types...
/// (SomeHash, Blake2b128Hash),
/// // ...or not.
/// (AnotherHash, Blake2b128Hash),
/// );
///
/// let hash = SomeHash::new(&[1, 2, 3]);
/// println!("{hash:?}");
/// ```
#[macro_export]
macro_rules! define_hashes {
($($(#[$docs:meta])* ($name:ident, $inner:ty)),+ $(,)?) => {
$(
$(#[$docs])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name($inner);

impl $name {
/// Creates a new instance from the given bytes by hashing them.
#[must_use]
pub fn new(input_bytes: &[u8]) -> Self {
Self(<$inner>::new(input_bytes))
}
}

impl From<$name> for Vec<u8> {
fn from(value: $name) -> Self {
value.0.into()
}
}

impl From<$inner> for $name {
fn from(value: $inner) -> Self {
Self(value)
}
}

impl TryFrom<&[u8]> for $name {
type Error = $crate::hashes::Blake2bHashError;

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

impl TryFrom<Vec<u8>> for $name {
type Error = $crate::hashes::Blake2bHashError;

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

impl std::str::FromStr for $name {
type Err = $crate::hashes::Blake2bHashError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let hash: $inner = s.parse().map_err($crate::hashes::Blake2bHashError::from)?;
Ok(Self(hash))
}
}

impl<C> minicbor::Encode<C> for $name {
fn encode<W: minicbor::encode::Write>(
&self, e: &mut minicbor::Encoder<W>, ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
self.0.encode(e, ctx)
}
}

impl<'a, C> minicbor::Decode<'a, C> for $name {
fn decode(
d: &mut minicbor::Decoder<'a>, ctx: &mut C,
) -> Result<Self, minicbor::decode::Error> {
let hash = <$inner>::decode(d, ctx)?;
Ok(Self(hash))
}
}
)+
};
}

#[cfg(test)]
mod tests {
use crate::hashes::Blake2b128Hash;

// Define one type without a trailing comma.
define_hashes!((H1, Blake2b128Hash));
// Define one type with a trailing comma and a doc-comment.
define_hashes!(
/// Some documentation.
(H2, Blake2b128Hash),
);
// Define multiple types at once.
define_hashes!(
/// Documentation.
(H3, Blake2b128Hash),
// No documentation.
(H4, Blake2b128Hash),
/// More documentation.
(H5, Blake2b128Hash),
);

// There is little reason to check the conversion itself, it is mostly a demonstration
// that the methods defined by the macro are working.
#[test]
fn hash_wrapper() {
let hash = H1::new(&[1, 2, 3, 4, 5]);

let v = Vec::from(hash);
let from_slice = H1::try_from(v.as_slice()).unwrap();
assert_eq!(hash, from_slice);

let from_vec = H1::try_from(v).unwrap();
assert_eq!(hash, from_vec);
}
}
1 change: 1 addition & 0 deletions rust/catalyst-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod cbor_utils;
pub mod conversion;
pub mod hash_wrapper;
pub mod hashes;
pub mod id_uri;
pub mod mmap_file;
Expand Down
17 changes: 9 additions & 8 deletions rust/catalyst-types/src/uuid/uuid_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
use std::fmt::{Display, Formatter};

use minicbor::{Decode, Decoder, Encode};
use uuid::Uuid;

use super::{decode_cbor_uuid, encode_cbor_uuid, CborContext, UuidError, INVALID_UUID};

/// Type representing a `UUIDv7`.
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize)]
pub struct UuidV7(uuid::Uuid);
pub struct UuidV7(Uuid);

impl UuidV7 {
/// Version for `UUIDv7`.
Expand All @@ -17,7 +18,7 @@ impl UuidV7 {
#[must_use]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self(uuid::Uuid::now_v7())
Self(Uuid::now_v7())
}

/// Generates a zeroed out `UUIDv7` that can never be valid.
Expand All @@ -34,13 +35,13 @@ impl UuidV7 {

/// Returns the `uuid::Uuid` type.
#[must_use]
pub fn uuid(&self) -> uuid::Uuid {
pub fn uuid(&self) -> Uuid {
self.0
}
}

/// Check if this is a valid `UUIDv7`.
fn is_valid(uuid: &uuid::Uuid) -> bool {
fn is_valid(uuid: &Uuid) -> bool {
uuid != &INVALID_UUID && uuid.get_version_num() == UuidV7::UUID_VERSION_NUMBER
}

Expand Down Expand Up @@ -72,10 +73,10 @@ impl Encode<CborContext> for UuidV7 {
}

/// Returns a `UUIDv7` from `uuid::Uuid`.
impl TryFrom<uuid::Uuid> for UuidV7 {
impl TryFrom<Uuid> for UuidV7 {
type Error = UuidError;

fn try_from(uuid: uuid::Uuid) -> Result<Self, Self::Error> {
fn try_from(uuid: Uuid) -> Result<Self, Self::Error> {
if is_valid(&uuid) {
Ok(Self(uuid))
} else {
Expand All @@ -87,7 +88,7 @@ impl TryFrom<uuid::Uuid> for UuidV7 {
/// Returns a `uuid::Uuid` from `UUIDv7`.
///
/// NOTE: This does not guarantee that the `UUID` is valid.
impl From<UuidV7> for uuid::Uuid {
impl From<UuidV7> for Uuid {
fn from(value: UuidV7) -> Self {
value.0
}
Expand All @@ -96,7 +97,7 @@ impl From<UuidV7> for uuid::Uuid {
impl<'de> serde::Deserialize<'de> for UuidV7 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: serde::Deserializer<'de> {
let uuid = uuid::Uuid::deserialize(deserializer)?;
let uuid = Uuid::deserialize(deserializer)?;
if is_valid(&uuid) {
Ok(Self(uuid))
} else {
Expand Down
6 changes: 6 additions & 0 deletions rust/rbac-registration/src/cardano/cip509/cip509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ impl Cip509 {
self.metadata.as_ref().and_then(|m| m.role_data.get(&role))
}

/// Returns a purpose of this registration.
#[must_use]
pub fn purpose(&self) -> Option<UuidV4> {
self.purpose
}

/// Returns a hash of the previous transaction.
#[must_use]
pub fn previous_transaction(&self) -> Option<Blake2b256Hash> {
Expand Down
3 changes: 2 additions & 1 deletion rust/rbac-registration/src/cardano/cip509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//! CDDL Reference: <https://github.com/input-output-hk/catalyst-CIPs/blob/x509-envelope-metadata/CIP-XXXX/x509-envelope.cddl>

pub use cip509::Cip509;
pub use rbac::{C509Cert, SimplePublicKeyType, X509DerCert};
#[allow(clippy::module_name_repetitions)]
pub use rbac::{C509Cert, Cip509RbacMetadata, SimplePublicKeyType, X509DerCert};
pub use types::{
CertKeyHash, KeyLocalRef, LocalRefInt, Payment, PaymentHistory, PointTxnIdx, RoleData,
RoleNumber, TxInputHash, ValidationSignature,
Expand Down
Loading