diff --git a/rust/cardano-blockchain-types/Cargo.toml b/rust/cardano-blockchain-types/Cargo.toml index 6a24f9191a..e69cc10377 100644 --- a/rust/cardano-blockchain-types/Cargo.toml +++ b/rust/cardano-blockchain-types/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cardano-blockchain-types" description = "Common Cardano Blockchain data types for use in both applications and crates" -keywords = ["cardano", "catalyst",] +keywords = ["cardano", "catalyst", ] version = "0.0.1" authors = [ "Steven Johnson " @@ -20,8 +20,8 @@ workspace = true [dependencies] pallas = { 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" } +cbork-utils = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250127-00" } +catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250127-00" } ouroboros = "0.18.4" tracing = "0.1.41" diff --git a/rust/cardano-blockchain-types/src/metadata/cip36/key_registration.rs b/rust/cardano-blockchain-types/src/metadata/cip36/key_registration.rs index 31dd52fc06..483a71dad3 100644 --- a/rust/cardano-blockchain-types/src/metadata/cip36/key_registration.rs +++ b/rust/cardano-blockchain-types/src/metadata/cip36/key_registration.rs @@ -5,7 +5,10 @@ //! //! -use catalyst_types::problem_report::ProblemReport; +use catalyst_types::{ + cbor_utils::{report_duplicated_key, report_missing_keys}, + problem_report::ProblemReport, +}; use cbork_utils::decode_helper::{decode_array_len, decode_bytes, decode_helper, decode_map_len}; use ed25519_dalek::VerifyingKey; use minicbor::{decode, Decode, Decoder}; @@ -88,7 +91,13 @@ impl Decode<'_, ProblemReport> for Cip36KeyRegistration { let key: u16 = decode_helper(d, "key in CIP36 Key Registration", err_report)?; if let Some(key) = Cip36KeyRegistrationKeys::from_repr(key) { - if check_is_key_exist(&found_keys, &key, index, err_report) { + if report_duplicated_key( + &found_keys, + &key, + index, + "CIP36 Key Registration", + err_report, + ) { continue; } match key { @@ -127,37 +136,17 @@ impl Decode<'_, ProblemReport> for Cip36KeyRegistration { Cip36KeyRegistrationKeys::PaymentAddr, Cip36KeyRegistrationKeys::Nonce, ]; - - for key in &required_keys { - if !found_keys.contains(key) { - err_report.missing_field( - &format!("{key:?}"), - "Missing required key in CIP36 Key Registration", - ); - } - } + report_missing_keys( + &found_keys, + &required_keys, + "CIP36 Key Registration", + err_report, + ); Ok(cip36_key_registration) } } -/// Helper function for checking whether the key is already in the `found_keys` or not. -/// True if exist, false if not. -fn check_is_key_exist( - found_keys: &[Cip36KeyRegistrationKeys], key: &Cip36KeyRegistrationKeys, index: u64, - err_report: &ProblemReport, -) -> bool { - if found_keys.contains(key) { - err_report.duplicate_field( - format!("{key:?}").as_str(), - format!("Redundant key found in item {} in RBAC map", index + 1).as_str(), - format!("CIP36 Key Registration {key:?}").as_str(), - ); - return true; - } - false -} - /// Helper function for decoding the voting key. /// /// # Returns diff --git a/rust/cardano-blockchain-types/src/metadata/cip36/mod.rs b/rust/cardano-blockchain-types/src/metadata/cip36/mod.rs index 068cd5b0ff..e64c73d04f 100644 --- a/rust/cardano-blockchain-types/src/metadata/cip36/mod.rs +++ b/rust/cardano-blockchain-types/src/metadata/cip36/mod.rs @@ -227,71 +227,85 @@ impl Cip36 { /// Get the `is_cip36` flag from the registration. /// True if it is CIP-36 format, false if CIP-15 format. + #[must_use] pub fn is_cip36(&self) -> Option { self.key_registration.is_cip36 } /// Get the voting public keys from the registration. + #[must_use] pub fn voting_pks(&self) -> &Vec { &self.key_registration.voting_pks } /// Get the stake public key from the registration. + #[must_use] pub fn stake_pk(&self) -> Option<&VerifyingKey> { self.key_registration.stake_pk.as_ref() } /// Get the payment address from the registration. + #[must_use] pub fn payment_address(&self) -> Option<&ShelleyAddress> { self.key_registration.payment_addr.as_ref() } /// Get the nonce from the registration. + #[must_use] pub fn nonce(&self) -> Option { self.key_registration.nonce } /// Get the purpose from the registration. + #[must_use] pub fn purpose(&self) -> u64 { self.key_registration.purpose } /// Get the raw nonce from the registration. + #[must_use] pub fn raw_nonce(&self) -> Option { self.key_registration.raw_nonce } /// Is the payment address in the registration payable? + #[must_use] pub fn is_payable(&self) -> Option { self.key_registration.is_payable } /// Get the signature from the registration witness. + #[must_use] pub fn signature(&self) -> Option { self.registration_witness.signature } /// Get the slot number of this CIP-36 registration. + #[must_use] pub fn slot(&self) -> Slot { self.slot } /// Get the network of this CIP-36 registration. + #[must_use] pub fn network(&self) -> Network { self.network } /// Get the transaction index of this CIP-36 registration. + #[must_use] pub fn txn_idx(&self) -> TxnIndex { self.txn_idx } /// Get the Catalyst strict flag. + #[must_use] pub fn is_strict_catalyst(&self) -> bool { self.is_catalyst_strict } /// Is the CIP-36 registration valid? + #[must_use] pub fn is_valid(&self) -> bool { // Check everything self.is_valid_signature @@ -302,26 +316,31 @@ impl Cip36 { } /// Is the signature valid? + #[must_use] pub fn is_valid_signature(&self) -> bool { self.is_valid_signature } /// Is the payment address network tag match the provided network? + #[must_use] pub fn is_valid_payment_address_network(&self) -> bool { self.is_valid_payment_address_network } /// Is the voting keys valid? + #[must_use] pub fn is_valid_voting_keys(&self) -> bool { self.is_valid_voting_keys } /// Is the purpose valid? + #[must_use] pub fn is_valid_purpose(&self) -> bool { self.is_valid_purpose } /// Get the error report. + #[must_use] pub fn err_report(&self) -> &ProblemReport { &self.err_report } diff --git a/rust/rbac-registration/Cargo.toml b/rust/rbac-registration/Cargo.toml index 7b7bcb3fd2..7140aa3e2a 100644 --- a/rust/rbac-registration/Cargo.toml +++ b/rust/rbac-registration/Cargo.toml @@ -32,6 +32,6 @@ uuid = "1.11.0" c509-certificate = { version = "0.0.3", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.3" } pallas = { 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 = "r20250124-00" } -cardano-blockchain-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250124-00" } -catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250124-00" } +cbork-utils = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250127-00" } +cardano-blockchain-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250127-00" } +catalyst-types = { version = "0.0.1", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "r20250127-00" } diff --git a/rust/rbac-registration/src/cardano/cip509/cip509.rs b/rust/rbac-registration/src/cardano/cip509/cip509.rs index cdef22577b..3dd41cf09c 100644 --- a/rust/rbac-registration/src/cardano/cip509/cip509.rs +++ b/rust/rbac-registration/src/cardano/cip509/cip509.rs @@ -7,6 +7,7 @@ use std::{borrow::Cow, collections::HashMap}; use anyhow::{anyhow, Context}; use cardano_blockchain_types::{MetadatumLabel, MultiEraBlock, TxnIndex}; use catalyst_types::{ + cbor_utils::{report_duplicated_key, report_missing_keys}, hashes::{Blake2b256Hash, BLAKE_2B256_SIZE}, problem_report::ProblemReport, uuid::UuidV4, @@ -28,19 +29,16 @@ use strum_macros::FromRepr; use tracing::warn; use uuid::Uuid; -use crate::{ - cardano::cip509::{ - decode_context::DecodeContext, - rbac::Cip509RbacMetadata, - types::{PaymentHistory, RoleNumber, TxInputHash, ValidationSignature}, - utils::Cip0134UriSet, - validation::{ - validate_aux, validate_role_data, validate_stake_public_key, validate_txn_inputs_hash, - }, - x509_chunks::X509Chunks, - Payment, PointTxnIdx, RoleData, +use crate::cardano::cip509::{ + decode_context::DecodeContext, + rbac::Cip509RbacMetadata, + types::{PaymentHistory, RoleNumber, TxInputHash, ValidationSignature}, + utils::Cip0134UriSet, + validation::{ + validate_aux, validate_role_data, validate_stake_public_key, validate_txn_inputs_hash, }, - utils::decode_helper::{report_duplicated_key, report_missing_keys}, + x509_chunks::X509Chunks, + Payment, PointTxnIdx, RoleData, }; /// A x509 metadata envelope. @@ -294,7 +292,8 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509 { // Consume the key. This should never fail because we used `probe` above. let _: u8 = decode_helper(d, context, &mut ())?; - if report_duplicated_key(&found_keys, &key, index, context, decode_context.report) { + if report_duplicated_key(&found_keys, &key, index, "Cip509", decode_context.report) + { continue; } found_keys.push(key); diff --git a/rust/rbac-registration/src/cardano/cip509/rbac/metadata.rs b/rust/rbac-registration/src/cardano/cip509/rbac/metadata.rs index 687cd50620..df1aac5df6 100644 --- a/rust/rbac-registration/src/cardano/cip509/rbac/metadata.rs +++ b/rust/rbac-registration/src/cardano/cip509/rbac/metadata.rs @@ -2,21 +2,18 @@ use std::collections::{HashMap, HashSet}; -use catalyst_types::problem_report::ProblemReport; +use catalyst_types::{cbor_utils::report_duplicated_key, problem_report::ProblemReport}; use cbork_utils::decode_helper::{ decode_any, decode_array_len, decode_bytes, decode_helper, decode_map_len, }; use minicbor::{decode, Decode, Decoder}; use strum_macros::FromRepr; -use crate::{ - cardano::cip509::{ - decode_context::DecodeContext, - rbac::{role_data::CborRoleData, C509Cert, SimplePublicKeyType, X509DerCert}, - utils::Cip0134UriSet, - CertKeyHash, RoleData, RoleNumber, - }, - utils::decode_helper::report_duplicated_key, +use crate::cardano::cip509::{ + decode_context::DecodeContext, + rbac::{role_data::CborRoleData, C509Cert, SimplePublicKeyType, X509DerCert}, + utils::Cip0134UriSet, + CertKeyHash, RoleData, RoleNumber, }; /// Cip509 RBAC metadata. @@ -94,7 +91,13 @@ impl Decode<'_, DecodeContext<'_, '_>> for Cip509RbacMetadata { for index in 0..map_len { let key: u16 = decode_helper(d, "key in Cip509RbacMetadata", &mut ())?; if let Some(key) = Cip509RbacMetadataInt::from_repr(key) { - if report_duplicated_key(&found_keys, &key, index, context, decode_context.report) { + if report_duplicated_key( + &found_keys, + &key, + index, + "Cip509RbacMetadata", + decode_context.report, + ) { continue; } found_keys.push(key); diff --git a/rust/rbac-registration/src/cardano/cip509/rbac/role_data.rs b/rust/rbac-registration/src/cardano/cip509/rbac/role_data.rs index 6d25680448..7a46a4259b 100644 --- a/rust/rbac-registration/src/cardano/cip509/rbac/role_data.rs +++ b/rust/rbac-registration/src/cardano/cip509/rbac/role_data.rs @@ -2,15 +2,15 @@ use std::collections::HashMap; -use catalyst_types::problem_report::ProblemReport; +use catalyst_types::{ + cbor_utils::{report_duplicated_key, report_missing_keys}, + problem_report::ProblemReport, +}; use cbork_utils::decode_helper::{decode_any, decode_array_len, decode_helper, decode_map_len}; use minicbor::{decode, Decode, Decoder}; use strum_macros::FromRepr; -use crate::{ - cardano::cip509::{KeyLocalRef, RoleNumber}, - utils::decode_helper::{report_duplicated_key, report_missing_keys}, -}; +use crate::cardano::cip509::{KeyLocalRef, RoleNumber}; /// Role data as encoded in CBOR. #[allow(clippy::module_name_repetitions)] @@ -61,7 +61,7 @@ impl Decode<'_, ProblemReport> for CborRoleData { for index in 0..map_len { let key: u8 = decode_helper(d, "key in RoleData", &mut ())?; if let Some(key) = RoleDataInt::from_repr(key) { - if report_duplicated_key(&found_keys, &key, index, context, report) { + if report_duplicated_key(&found_keys, &key, index, "RoleData", report) { continue; } found_keys.push(key); diff --git a/rust/rbac-registration/src/utils/decode_helper.rs b/rust/rbac-registration/src/utils/decode_helper.rs deleted file mode 100644 index 9f0632ae74..0000000000 --- a/rust/rbac-registration/src/utils/decode_helper.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! CBOR decoding helper functions. - -use std::fmt::Debug; - -use catalyst_types::problem_report::ProblemReport; - -/// Adds a "duplicated field" entry to the report and returns true if the field is already -/// present in the given found keys list. -pub fn report_duplicated_key( - found_keys: &[T], key: &T, index: u64, context: &str, report: &ProblemReport, -) -> bool { - if found_keys.contains(key) { - report.duplicate_field( - format!("{key:?}").as_str(), - format!("Redundant key found in item {} in RBAC map", index + 1).as_str(), - context, - ); - return true; - } - false -} - -/// Adds a "missing field" entry to the report for every required key that isn't present -/// in the found keys list. -pub fn report_missing_keys( - found_keys: &[T], required_keys: &[T], context: &str, report: &ProblemReport, -) { - for key in required_keys { - if !found_keys.contains(key) { - report.missing_field(&format!("{key:?}"), context); - } - } -} diff --git a/rust/rbac-registration/src/utils/mod.rs b/rust/rbac-registration/src/utils/mod.rs index 9a5e61bfa5..0723ec5c50 100644 --- a/rust/rbac-registration/src/utils/mod.rs +++ b/rust/rbac-registration/src/utils/mod.rs @@ -1,6 +1,4 @@ //! Utility functions for the RBAC registration module. -pub mod decode_helper; - #[cfg(test)] pub mod test;