Skip to content

Commit d60579c

Browse files
committed
add utils mod
1 parent 20929de commit d60579c

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

rust/signed_doc/src/metadata/mod.rs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ mod content_type;
77
mod document_ref;
88
mod extra_fields;
99
mod section;
10+
pub(crate) mod utils;
1011

1112
use algorithm::Algorithm;
1213
use catalyst_types::{
1314
problem_report::ProblemReport,
14-
uuid::{CborContext, UuidV4, UuidV7},
15+
uuid::{UuidV4, UuidV7},
1516
};
1617
pub use content_encoding::ContentEncoding;
1718
pub use content_type::ContentType;
18-
use coset::{iana::CoapContentFormat, CborSerializable};
19+
use coset::iana::CoapContentFormat;
1920
pub use document_ref::DocumentRef;
2021
pub use extra_fields::ExtraFields;
2122
pub use section::Section;
23+
use utils::{cose_protected_header_find, decode_cbor_uuid, encode_cbor_uuid};
2224

2325
/// `content_encoding` field COSE key value
2426
const CONTENT_ENCODING_KEY: &str = "Content-Encoding";
@@ -298,43 +300,3 @@ impl TryFrom<&Metadata> for coset::Header {
298300
Ok(builder.build())
299301
}
300302
}
301-
302-
/// Find a value for a predicate in the protected header.
303-
fn cose_protected_header_find(
304-
protected: &coset::ProtectedHeader, mut predicate: impl FnMut(&coset::Label) -> bool,
305-
) -> Option<&coset::cbor::Value> {
306-
protected
307-
.header
308-
.rest
309-
.iter()
310-
.find(|(key, _)| predicate(key))
311-
.map(|(_, value)| value)
312-
}
313-
314-
/// Encode `uuid::Uuid` type into `coset::cbor::Value`.
315-
///
316-
/// This is used to encode `UuidV4` and `UuidV7` types.
317-
pub(crate) fn encode_cbor_uuid<T: minicbor::encode::Encode<CborContext>>(
318-
value: T,
319-
) -> anyhow::Result<coset::cbor::Value> {
320-
let mut cbor_bytes = Vec::new();
321-
minicbor::encode_with(value, &mut cbor_bytes, &mut CborContext::Tagged)
322-
.map_err(|e| anyhow::anyhow!("Unable to encode CBOR value, err: {e}"))?;
323-
coset::cbor::Value::from_slice(&cbor_bytes)
324-
.map_err(|e| anyhow::anyhow!("Invalid CBOR value, err: {e}"))
325-
}
326-
327-
/// Decode `From<uuid::Uuid>` type from `coset::cbor::Value`.
328-
///
329-
/// This is used to decode `UuidV4` and `UuidV7` types.
330-
pub(crate) fn decode_cbor_uuid<T: for<'a> minicbor::decode::Decode<'a, CborContext>>(
331-
value: coset::cbor::Value,
332-
) -> anyhow::Result<T> {
333-
match value.to_vec() {
334-
Ok(cbor_value) => {
335-
minicbor::decode_with(&cbor_value, &mut CborContext::Tagged)
336-
.map_err(|e| anyhow::anyhow!("Invalid UUID, err: {e}"))
337-
},
338-
Err(e) => anyhow::bail!("Invalid CBOR value, err: {e}"),
339-
}
340-
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Utitlity functions for metadata decoding fields
2+
3+
use catalyst_types::uuid::CborContext;
4+
use coset::CborSerializable;
5+
6+
/// Find a value for a predicate in the protected header.
7+
pub(crate) fn cose_protected_header_find(
8+
protected: &coset::ProtectedHeader, mut predicate: impl FnMut(&coset::Label) -> bool,
9+
) -> Option<&coset::cbor::Value> {
10+
protected
11+
.header
12+
.rest
13+
.iter()
14+
.find(|(key, _)| predicate(key))
15+
.map(|(_, value)| value)
16+
}
17+
18+
/// Encode `uuid::Uuid` type into `coset::cbor::Value`.
19+
///
20+
/// This is used to encode `UuidV4` and `UuidV7` types.
21+
pub(crate) fn encode_cbor_uuid<T: minicbor::encode::Encode<CborContext>>(
22+
value: T,
23+
) -> anyhow::Result<coset::cbor::Value> {
24+
let mut cbor_bytes = Vec::new();
25+
minicbor::encode_with(value, &mut cbor_bytes, &mut CborContext::Tagged)
26+
.map_err(|e| anyhow::anyhow!("Unable to encode CBOR value, err: {e}"))?;
27+
coset::cbor::Value::from_slice(&cbor_bytes)
28+
.map_err(|e| anyhow::anyhow!("Invalid CBOR value, err: {e}"))
29+
}
30+
31+
/// Decode `From<uuid::Uuid>` type from `coset::cbor::Value`.
32+
///
33+
/// This is used to decode `UuidV4` and `UuidV7` types.
34+
pub(crate) fn decode_cbor_uuid<T: for<'a> minicbor::decode::Decode<'a, CborContext>>(
35+
value: coset::cbor::Value,
36+
) -> anyhow::Result<T> {
37+
match value.to_vec() {
38+
Ok(cbor_value) => {
39+
minicbor::decode_with(&cbor_value, &mut CborContext::Tagged)
40+
.map_err(|e| anyhow::anyhow!("Invalid UUID, err: {e}"))
41+
},
42+
Err(e) => anyhow::bail!("Invalid CBOR value, err: {e}"),
43+
}
44+
}

rust/signed_doc/src/validator/rules/reply.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ where Provider: 'static + CatalystSignedDocumentProvider
5050
return Ok(false);
5151
}
5252
}
53-
5453
Ok(true)
5554
};
5655
return validate_provided_doc(&reply, provider, doc.report(), reply_validator).await;

0 commit comments

Comments
 (0)