From a2783b9532ec3d0ac8d8a52dbf5b1ed72143d530 Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Mon, 24 Mar 2025 11:35:18 +0100 Subject: [PATCH] Use usize for KeyLocalRef::key_offset instead of u64 --- .../src/cardano/cip509/rbac/role_data.rs | 8 +++--- .../src/cardano/cip509/types/key_local_ref.rs | 25 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) 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 7a46a4259b1..448a608e3f0 100644 --- a/rust/rbac-registration/src/cardano/cip509/rbac/role_data.rs +++ b/rust/rbac-registration/src/cardano/cip509/rbac/role_data.rs @@ -137,14 +137,14 @@ impl Decode<'_, ProblemReport> for CborRoleData { /// Decodes a signing key. fn decode_signing_key( - d: &mut Decoder, context: &str, report: &ProblemReport, + d: &mut Decoder, context: &str, report: &mut ProblemReport, ) -> Result, ()> { if let Err(e) = decode_array_len(d, "RoleSigningKey") { report.other(&format!("{e:?}"), context); return Err(()); } - match KeyLocalRef::decode(d, &mut ()) { + match KeyLocalRef::decode(d, report) { Ok(v) => Ok(Some(v)), Err(e) => { report.other( @@ -158,14 +158,14 @@ fn decode_signing_key( /// Decodes an encryption key. fn decode_encryption_key( - d: &mut Decoder, context: &str, report: &ProblemReport, + d: &mut Decoder, context: &str, report: &mut ProblemReport, ) -> Result, ()> { if let Err(e) = decode_array_len(d, "RoleEncryptionKey") { report.other(&format!("{e:?}"), context); return Err(()); } - match KeyLocalRef::decode(d, &mut ()) { + match KeyLocalRef::decode(d, report) { Ok(v) => Ok(Some(v)), Err(e) => { report.other( diff --git a/rust/rbac-registration/src/cardano/cip509/types/key_local_ref.rs b/rust/rbac-registration/src/cardano/cip509/types/key_local_ref.rs index ee7b3cc03be..a05b0b10c1b 100644 --- a/rust/rbac-registration/src/cardano/cip509/types/key_local_ref.rs +++ b/rust/rbac-registration/src/cardano/cip509/types/key_local_ref.rs @@ -1,5 +1,6 @@ //! A local key reference. +use catalyst_types::problem_report::ProblemReport; use cbork_utils::decode_helper::decode_helper; use minicbor::{decode, Decode, Decoder}; use strum_macros::FromRepr; @@ -12,7 +13,7 @@ pub struct KeyLocalRef { /// Local reference. pub local_ref: LocalRefInt, /// Key offset. - pub key_offset: u64, + pub key_offset: usize, } /// Enum of local reference with its associated unsigned integer value. @@ -27,11 +28,23 @@ pub enum LocalRefInt { PubKeys = Cip509RbacMetadataInt::PubKeys as u8, // 30 } -impl Decode<'_, ()> for KeyLocalRef { - fn decode(d: &mut Decoder, ctx: &mut ()) -> Result { - let local_ref = LocalRefInt::from_repr(decode_helper(d, "LocalRef in KeyLocalRef", ctx)?) - .ok_or(decode::Error::message("Invalid local reference"))?; - let key_offset: u64 = decode_helper(d, "KeyOffset in KeyLocalRef", ctx)?; +impl Decode<'_, ProblemReport> for KeyLocalRef { + fn decode(d: &mut Decoder, report: &mut ProblemReport) -> Result { + let local_ref = + LocalRefInt::from_repr(decode_helper(d, "LocalRef in KeyLocalRef", &mut ())?) + .ok_or(decode::Error::message("Invalid local reference"))?; + let key_offset: u64 = decode_helper(d, "KeyOffset in KeyLocalRef", &mut ())?; + let key_offset = if let Ok(v) = usize::try_from(key_offset) { + v + } else { + report.invalid_value( + "key_offset", + &format!("{key_offset}"), + &format!("Value must be less than {}", usize::MAX), + "KeyLocalRef decoding", + ); + 0 + }; Ok(Self { local_ref, key_offset,