Skip to content

Commit 08ec6a4

Browse files
committed
fix(rust/catalyst-types): encode UUID types with context
1 parent fc732df commit 08ec6a4

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

rust/catalyst-types/src/uuid/mod.rs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,46 @@ pub const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]);
1313
#[allow(dead_code)]
1414
const UUID_CBOR_TAG: u64 = 37;
1515

16+
/// Context for `CBOR` encoding and decoding
17+
pub enum CborContext {
18+
/// Untagged bytes
19+
Untagged,
20+
/// IANA CBOR tag and bytes
21+
Tagged,
22+
/// Optional tag
23+
Optional,
24+
}
25+
1626
/// Decode from `CBOR` into `UUID`
17-
fn decode_cbor_uuid<C>(
18-
d: &mut minicbor::Decoder<'_>, _ctx: &mut C,
27+
fn decode_cbor_uuid(
28+
d: &mut minicbor::Decoder<'_>, ctx: &mut CborContext,
1929
) -> Result<uuid::Uuid, minicbor::decode::Error> {
20-
let decoded: [u8; 16] = d.bytes()?.try_into().map_err(|_| {
30+
let bytes = match ctx {
31+
CborContext::Untagged => d.bytes()?,
32+
CborContext::Tagged => {
33+
let tag = d.tag()?.as_u64();
34+
if UUID_CBOR_TAG == tag {
35+
return Err(minicbor::decode::Error::message(format!(
36+
"tag value must be: {UUID_CBOR_TAG}, provided: {tag}"
37+
)));
38+
}
39+
d.bytes()?
40+
},
41+
CborContext::Optional => {
42+
if let Ok(tag) = d.tag() {
43+
let tag = tag.as_u64();
44+
if UUID_CBOR_TAG == tag {
45+
return Err(minicbor::decode::Error::message(format!(
46+
"tag value must be: {UUID_CBOR_TAG}, provided: {tag}"
47+
)));
48+
}
49+
d.bytes()?
50+
} else {
51+
d.bytes()?
52+
}
53+
},
54+
};
55+
let decoded: [u8; 16] = bytes.try_into().map_err(|_| {
2156
minicbor::decode::Error::message("Invalid CBOR encoded UUID type: invalid bytes size")
2257
})?;
2358
let uuid = uuid::Uuid::from_bytes(decoded);
@@ -34,9 +69,10 @@ fn encode_cbor_uuid<C, W: minicbor::encode::Write>(
3469

3570
#[cfg(test)]
3671
mod tests {
37-
use minicbor::data::Tagged;
72+
use minicbor::data::{Tag, Tagged};
3873

3974
use super::{V4, V7};
75+
use crate::uuid::CborContext;
4076

4177
const UUID_CBOR_TAG: u64 = 37;
4278

@@ -45,7 +81,7 @@ mod tests {
4581
let uuid: V4 = uuid::Uuid::new_v4().into();
4682
let mut bytes = Vec::new();
4783
minicbor::encode(uuid, &mut bytes).unwrap();
48-
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
84+
let decoded = minicbor::decode_with(bytes.as_slice(), &mut CborContext::Untagged).unwrap();
4985
assert_eq!(uuid, decoded);
5086
}
5187

@@ -54,7 +90,7 @@ mod tests {
5490
let uuid: V7 = uuid::Uuid::now_v7().into();
5591
let mut bytes = Vec::new();
5692
minicbor::encode(uuid, &mut bytes).unwrap();
57-
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
93+
let decoded = minicbor::decode_with(bytes.as_slice(), &mut CborContext::Untagged).unwrap();
5894
assert_eq!(uuid, decoded);
5995
}
6096

@@ -64,7 +100,7 @@ mod tests {
64100
let tagged: Tagged<UUID_CBOR_TAG, V4> = uuid.into();
65101
let mut bytes = Vec::new();
66102
minicbor::encode(tagged, &mut bytes).unwrap();
67-
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
103+
let decoded = minicbor::decode_with(bytes.as_slice(), &mut CborContext::Untagged).unwrap();
68104
assert_eq!(tagged, decoded);
69105
}
70106

@@ -74,7 +110,8 @@ mod tests {
74110
let tagged: Tagged<UUID_CBOR_TAG, V7> = uuid.into();
75111
let mut bytes = Vec::new();
76112
minicbor::encode(tagged, &mut bytes).unwrap();
77-
let decoded = minicbor::decode(bytes.as_slice()).unwrap();
113+
let decoded = minicbor::decode_with(bytes.as_slice(), &mut CborContext::Untagged).unwrap();
78114
assert_eq!(tagged, decoded);
115+
assert_eq!(tagged.tag(), Tag::new(UUID_CBOR_TAG));
79116
}
80117
}

rust/catalyst-types/src/uuid/uuid_v4.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter};
33

44
use minicbor::{Decode, Decoder, Encode};
55

6-
use super::{decode_cbor_uuid, encode_cbor_uuid, INVALID_UUID};
6+
use super::{decode_cbor_uuid, encode_cbor_uuid, CborContext, INVALID_UUID};
77

88
/// Type representing a `UUIDv4`.
99
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
@@ -43,8 +43,8 @@ impl Display for UuidV4 {
4343
}
4444
}
4545

46-
impl<C> Decode<'_, C> for UuidV4 {
47-
fn decode(d: &mut Decoder<'_>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
46+
impl Decode<'_, CborContext> for UuidV4 {
47+
fn decode(d: &mut Decoder<'_>, ctx: &mut CborContext) -> Result<Self, minicbor::decode::Error> {
4848
let uuid = decode_cbor_uuid(d, ctx)?;
4949
Ok(Self { uuid })
5050
}

rust/catalyst-types/src/uuid/uuid_v7.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter};
33

44
use minicbor::{Decode, Decoder, Encode};
55

6-
use super::{decode_cbor_uuid, encode_cbor_uuid, INVALID_UUID};
6+
use super::{decode_cbor_uuid, encode_cbor_uuid, CborContext, INVALID_UUID};
77

88
/// Type representing a `UUIDv7`.
99
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
@@ -43,8 +43,8 @@ impl Display for UuidV7 {
4343
}
4444
}
4545

46-
impl<C> Decode<'_, C> for UuidV7 {
47-
fn decode(d: &mut Decoder<'_>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
46+
impl Decode<'_, CborContext> for UuidV7 {
47+
fn decode(d: &mut Decoder<'_>, ctx: &mut CborContext) -> Result<Self, minicbor::decode::Error> {
4848
let uuid = decode_cbor_uuid(d, ctx)?;
4949
Ok(Self { uuid })
5050
}

0 commit comments

Comments
 (0)