Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions rust/signed_doc/src/metadata/extra_fields.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Catalyst Signed Document Extra Fields.

use anyhow::bail;
use catalyst_types::{problem_report::ProblemReport, uuid::UuidV4};
use coset::{cbor::Value, Label, ProtectedHeader};

Expand Down Expand Up @@ -160,12 +159,14 @@ impl ExtraFields {
}

/// Converting COSE Protected Header to `ExtraFields`.
/// Return `None` if it fails during
#[allow(clippy::too_many_lines)]
pub(crate) fn from_protected_header(
protected: &ProtectedHeader, error_report: &ProblemReport,
) -> anyhow::Result<Self> {
) -> Option<Self> {
/// Context for error messages.
const CONTEXT: &str = "COSE ProtectedHeader to ExtraFields";
let mut valid = false;

let mut extra = ExtraFields::default();

Expand All @@ -177,6 +178,7 @@ impl ExtraFields {
extra.doc_ref = Some(doc_ref);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header doc ref",
&format!("{cbor_doc_ref:?}"),
Expand All @@ -195,6 +197,7 @@ impl ExtraFields {
extra.template = Some(doc_template);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header document template",
&format!("{cbor_doc_template:?}"),
Expand All @@ -213,6 +216,7 @@ impl ExtraFields {
extra.reply = Some(doc_reply);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header document reply",
&format!("{cbor_doc_reply:?}"),
Expand All @@ -231,6 +235,7 @@ impl ExtraFields {
extra.section = Some(doc_section);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"COSE protected header document section",
&format!("{cbor_doc_section:?}"),
Expand All @@ -253,6 +258,7 @@ impl ExtraFields {
c.push(collaborator);
},
Err(e) => {
valid = false;
error_report.conversion_error(
&format!("COSE protected header collaborator index {ids}"),
&format!("{collaborator:?}"),
Expand All @@ -265,6 +271,7 @@ impl ExtraFields {
extra.collabs = c;
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header collaborators",
&format!("{cbor_doc_collabs:?}"),
Expand All @@ -283,6 +290,7 @@ impl ExtraFields {
extra.brand_id = Some(brand_id);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header brand ID",
&format!("{cbor_doc_brand_id:?}"),
Expand All @@ -301,6 +309,7 @@ impl ExtraFields {
extra.campaign_id = Some(campaign_id);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header campaign ID",
&format!("{cbor_doc_campaign_id:?}"),
Expand All @@ -319,6 +328,7 @@ impl ExtraFields {
extra.election_id = Some(election_id);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header election ID",
&format!("{cbor_doc_election_id:?}"),
Expand All @@ -337,6 +347,7 @@ impl ExtraFields {
extra.category_id = Some(category_id);
},
Err(e) => {
valid = false;
error_report.conversion_error(
"CBOR COSE protected header category ID",
&format!("{cbor_doc_category_id:?}"),
Expand All @@ -347,10 +358,7 @@ impl ExtraFields {
}
}

if error_report.is_problematic() {
bail!("Failed to convert COSE ProtectedHeader to ExtraFields");
}
Ok(extra)
valid.then_some(extra)
}
}

Expand Down
13 changes: 1 addition & 12 deletions rust/signed_doc/src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,7 @@ impl Metadata {
error_report.missing_field("ver", "Missing ver field in COSE protected header");
}

let extra = ExtraFields::from_protected_header(protected, error_report).map_or_else(
|e| {
error_report.conversion_error(
"COSE protected header",
&format!("{protected:?}"),
&format!("Expected ExtraField: {e}"),
&format!("{CONTEXT}, ExtraFields"),
);
None
},
Some,
);
let extra = ExtraFields::from_protected_header(protected, error_report);

match (content_type, content_encoding, id, doc_type, ver, extra) {
(
Expand Down
Loading