diff --git a/rust/cardano-blockchain-types/Cargo.toml b/rust/cardano-blockchain-types/Cargo.toml index a7fb5f6266..6a24f9191a 100644 --- a/rust/cardano-blockchain-types/Cargo.toml +++ b/rust/cardano-blockchain-types/Cargo.toml @@ -19,7 +19,6 @@ workspace = true [dependencies] pallas = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" } -pallas-crypto = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" } # pallas-hardano = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" } cbork-utils = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.11" } catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250108-00" } diff --git a/rust/cardano-blockchain-types/src/auxdata/metadatum.rs b/rust/cardano-blockchain-types/src/auxdata/metadatum.rs index 4b107d13c2..f85c16082f 100644 --- a/rust/cardano-blockchain-types/src/auxdata/metadatum.rs +++ b/rust/cardano-blockchain-types/src/auxdata/metadatum.rs @@ -2,11 +2,11 @@ use std::sync::Arc; +use catalyst_types::conversion::from_saturating; use dashmap::DashMap; use minicbor::Decode; use super::{metadatum_label::MetadatumLabel, metadatum_value::MetadatumValue}; -use crate::conversion::from_saturating; /// Transaction Metadata /// See: diff --git a/rust/cardano-blockchain-types/src/conversion.rs b/rust/cardano-blockchain-types/src/conversion.rs deleted file mode 100644 index 820a50c672..0000000000 --- a/rust/cardano-blockchain-types/src/conversion.rs +++ /dev/null @@ -1,55 +0,0 @@ -//! Conversion functions - -use anyhow::bail; - -/// Convert an `` to `` (saturate if out of range). -/// Note can convert any int to float, or f32 to f64 as well. -/// can not convert from float to int, or f64 to f32. -pub(crate) fn from_saturating< - R: Copy + num_traits::identities::Zero + num_traits::Bounded, - T: Copy - + TryInto - + std::ops::Sub - + std::cmp::PartialOrd - + num_traits::identities::Zero, ->( - value: T, -) -> R { - match value.try_into() { - Ok(value) => value, - Err(_) => { - // If we couldn't convert, its out of range for the destination type. - if value > T::zero() { - // If the number is positive, its out of range in the positive direction. - R::max_value() - } else { - // Otherwise its out of range in the negative direction. - R::min_value() - } - }, - } -} - -/// Try and convert a byte array into an ed25519 verifying key. -/// -/// # Errors -/// -/// Fails if the bytes are not a valid ED25519 Public Key -pub fn vkey_from_bytes(bytes: &[u8]) -> anyhow::Result { - if bytes.len() != ed25519_dalek::PUBLIC_KEY_LENGTH { - bail!( - "Failed to convert bytes to ED25519 public key. Expected {} bytes, got {}", - ed25519_dalek::PUBLIC_KEY_LENGTH, - bytes.len() - ); - } - - let mut ed25519 = [0u8; ed25519_dalek::PUBLIC_KEY_LENGTH]; - ed25519.copy_from_slice(bytes); // Can't panic because we already validated its size. - - let pubkey = match ed25519_dalek::VerifyingKey::from_bytes(&ed25519) { - Ok(key) => key, - Err(err) => bail!("Failed to convert Ed25519 public key in SimplePublicKeyType {err}"), - }; - Ok(pubkey) -} diff --git a/rust/cardano-blockchain-types/src/fork.rs b/rust/cardano-blockchain-types/src/fork.rs index 9679fb576a..6c1f57c4c4 100644 --- a/rust/cardano-blockchain-types/src/fork.rs +++ b/rust/cardano-blockchain-types/src/fork.rs @@ -8,7 +8,7 @@ use std::fmt; -use crate::conversion::from_saturating; +use catalyst_types::conversion::from_saturating; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd)] /// Counter that is incremented every time there is a roll-back in live-chain. diff --git a/rust/cardano-blockchain-types/src/hashes.rs b/rust/cardano-blockchain-types/src/hashes.rs deleted file mode 100644 index 2baed74ccd..0000000000 --- a/rust/cardano-blockchain-types/src/hashes.rs +++ /dev/null @@ -1,144 +0,0 @@ -//! Cardano hashing functions - -use std::{fmt, str::FromStr}; - -use anyhow::bail; -use blake2b_simd::Params; -use pallas_crypto::hash::Hash; - -/// Number of bytes in a blake2b 224 hash. -pub const BLAKE_2B224_SIZE: usize = 224 / 8; - -/// `Blake2B` 224bit Hash. -pub type Blake2b224Hash = Blake2bHash; - -/// Number of bytes in a blake2b 256 hash. -pub const BLAKE_2B256_SIZE: usize = 256 / 8; - -/// `Blake2B` 256bit Hash -pub type Blake2b256Hash = Blake2bHash; - -/// Number of bytes in a blake2b 128 hash. -pub const BLAKE_2B128_SIZE: usize = 128 / 8; - -/// `Blake2B` 128bit Hash -pub type Blake2b128Hash = Blake2bHash; - -/// data that is a blake2b [`struct@Hash`] of `BYTES` long. -/// -/// Possible values with Cardano are 32 bytes long (block hash or transaction -/// hash). Or 28 bytes long (as used in addresses) -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Blake2bHash(Hash); - -impl Blake2bHash { - /// Create a new `Blake2bHash` from a slice of bytes by hashing them. - #[must_use] - pub fn new(input_bytes: &[u8]) -> Self { - let mut bytes: [u8; BYTES] = [0u8; BYTES]; - - // Generate a unique hash of the data. - let mut hasher = Params::new().hash_length(BYTES).to_state(); - - hasher.update(input_bytes); - let hash = hasher.finalize(); - - // Create a new array containing the first BYTES elements from the original array - bytes.copy_from_slice(hash.as_bytes()); - - bytes.into() - } -} - -impl From<[u8; BYTES]> for Blake2bHash { - #[inline] - fn from(bytes: [u8; BYTES]) -> Self { - let hash: Hash = bytes.into(); - hash.into() - } -} - -impl From> for Blake2bHash { - #[inline] - fn from(bytes: Hash) -> Self { - Self(bytes) - } -} - -impl From> for Vec { - fn from(val: Blake2bHash) -> Self { - val.0.to_vec() - } -} - -/// Convert hash in a form of byte array into the `Blake2bHash` type. -impl TryFrom<&[u8]> for Blake2bHash { - type Error = anyhow::Error; - - fn try_from(value: &[u8]) -> Result { - if value.len() < BYTES { - bail!("Invalid input length"); - } - - let mut hash = [0; BYTES]; - hash.copy_from_slice(value); - let hash: Hash = hash.into(); - Ok(hash.into()) - } -} - -impl TryFrom<&Vec> for Blake2bHash { - type Error = anyhow::Error; - - fn try_from(value: &Vec) -> Result { - value.as_slice().try_into() - } -} - -impl TryFrom> for Blake2bHash { - type Error = anyhow::Error; - - fn try_from(value: Vec) -> Result { - value.as_slice().try_into() - } -} - -impl fmt::Debug for Blake2bHash { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&format!("{:?}", self.0)) - } -} - -impl fmt::Display for Blake2bHash { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&format!("{}", self.0)) - } -} - -impl FromStr for Blake2bHash { - type Err = hex::FromHexError; - - fn from_str(s: &str) -> Result { - let hash: Hash = s.parse()?; - Ok(hash.into()) - } -} - -impl minicbor::Encode for Blake2bHash { - fn encode( - &self, e: &mut minicbor::Encoder, _ctx: &mut C, - ) -> Result<(), minicbor::encode::Error> { - e.bytes(self.0.as_ref())?.ok() - } -} - -impl<'a, C, const BYTES: usize> minicbor::Decode<'a, C> for Blake2bHash { - fn decode( - d: &mut minicbor::Decoder<'a>, _ctx: &mut C, - ) -> Result { - let bytes = d.bytes()?; - bytes.try_into().map_err(|_| { - minicbor::decode::Error::message("Invalid hash size for Blake2bHash cbor decode") - }) - } -} diff --git a/rust/cardano-blockchain-types/src/lib.rs b/rust/cardano-blockchain-types/src/lib.rs index b2ae2a1bf9..e4cbb5f394 100644 --- a/rust/cardano-blockchain-types/src/lib.rs +++ b/rust/cardano-blockchain-types/src/lib.rs @@ -1,9 +1,7 @@ //! Catalyst Enhanced `MultiEraBlock` Structures mod auxdata; -pub mod conversion; mod fork; -pub mod hashes; mod metadata; mod multi_era_block_data; mod network; diff --git a/rust/cardano-blockchain-types/src/network.rs b/rust/cardano-blockchain-types/src/network.rs index 95b9d47cbf..35b687a383 100644 --- a/rust/cardano-blockchain-types/src/network.rs +++ b/rust/cardano-blockchain-types/src/network.rs @@ -2,6 +2,7 @@ use std::{ffi::OsStr, path::PathBuf}; +use catalyst_types::conversion::from_saturating; use chrono::{DateTime, Utc}; use pallas::{ ledger::traverse::wellknown::GenesisValues, @@ -11,7 +12,7 @@ use pallas::{ // use strum_macros; use tracing::debug; -use crate::{conversion::from_saturating, Slot}; +use crate::Slot; /// Default name of the executable if we can't derive it. pub(crate) const DEFAULT_EXE_NAME: &str = "cardano_chain_follower"; diff --git a/rust/cardano-blockchain-types/src/point.rs b/rust/cardano-blockchain-types/src/point.rs index d2d391459c..392189a115 100644 --- a/rust/cardano-blockchain-types/src/point.rs +++ b/rust/cardano-blockchain-types/src/point.rs @@ -8,9 +8,10 @@ use std::{ fmt::{Debug, Display, Formatter}, }; +use catalyst_types::hashes::Blake2b256Hash; use pallas::crypto::hash::Hash; -use crate::{hashes::Blake2b256Hash, Slot}; +use crate::Slot; /// A specific point in the blockchain. It can be used to /// identify a particular location within the blockchain, such as the tip (the @@ -279,7 +280,8 @@ impl Point { /// # Examples /// /// ``` - /// use cardano_blockchain_types::{hashes::Blake2b256Hash, Point}; + /// use cardano_blockchain_types::Point; + /// use catalyst_types::hashes::Blake2b256Hash; /// /// let specific_point = Point::new(42.into(), [0; 32].into()); /// assert_eq!( @@ -489,7 +491,9 @@ fn cmp_point( #[cfg(test)] mod tests { - use crate::{hashes::Blake2bHash, point::*}; + use catalyst_types::hashes::Blake2bHash; + + use crate::point::*; #[test] fn test_cmp_hash_simple() { diff --git a/rust/cardano-blockchain-types/src/slot.rs b/rust/cardano-blockchain-types/src/slot.rs index 80d0512f2a..8cb565d836 100644 --- a/rust/cardano-blockchain-types/src/slot.rs +++ b/rust/cardano-blockchain-types/src/slot.rs @@ -5,11 +5,10 @@ use std::{ ops::{MulAssign, Sub}, }; +use catalyst_types::conversion::from_saturating; use num_bigint::{BigInt, Sign}; use serde::Serialize; -use crate::conversion::from_saturating; - #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default, Serialize)] /// Slot on the blockchain, typically one slot equals one second. However chain diff --git a/rust/cardano-blockchain-types/src/txn_index.rs b/rust/cardano-blockchain-types/src/txn_index.rs index 716117c4b7..1d238af454 100644 --- a/rust/cardano-blockchain-types/src/txn_index.rs +++ b/rust/cardano-blockchain-types/src/txn_index.rs @@ -1,5 +1,5 @@ //! Transaction Index -use crate::conversion::from_saturating; +use catalyst_types::conversion::from_saturating; /// Transaction index within a block /// See: diff --git a/rust/cardano-blockchain-types/src/txn_witness.rs b/rust/cardano-blockchain-types/src/txn_witness.rs index 5d7a7977f9..60fd6707cb 100644 --- a/rust/cardano-blockchain-types/src/txn_witness.rs +++ b/rust/cardano-blockchain-types/src/txn_witness.rs @@ -1,11 +1,12 @@ //! Transaction Witness use std::fmt::{Display, Formatter}; +use catalyst_types::{conversion::vkey_from_bytes, hashes::Blake2b224Hash}; use dashmap::{DashMap, DashSet}; use ed25519_dalek::VerifyingKey; use pallas::ledger::traverse::MultiEraTx; -use crate::{conversion::vkey_from_bytes, hashes::Blake2b224Hash, TxnIndex}; +use crate::TxnIndex; /// Hash of a witness verifying public key pub type VKeyHash = Blake2b224Hash;