Skip to content

Commit 40acd23

Browse files
committed
wip
1 parent f8f4235 commit 40acd23

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

rust/signed_doc/src/lib.rs

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use anyhow::anyhow;
99
use content::Content;
10-
use coset::{CborSerializable, CoseSignature};
10+
use coset::CborSerializable;
1111

1212
mod content;
1313
mod error;
@@ -24,6 +24,8 @@ struct InnerCatalystSignedDocument {
2424
metadata: Metadata,
2525
/// Document Content
2626
content: Content,
27+
/// Document Author
28+
author: KidUri,
2729
/// Signatures
2830
signatures: Signatures,
2931
}
@@ -56,48 +58,54 @@ impl TryFrom<&[u8]> for CatalystSignedDocument {
5658
let cose_sign = coset::CoseSign::from_slice(cose_bytes)
5759
.map_err(|e| vec![anyhow::anyhow!("Invalid COSE Sign document: {e}")])?;
5860

59-
let metadata = Metadata::try_from(&cose_sign.protected)?;
60-
6161
let mut errors = Vec::new();
6262

63-
let mut signatures = Signatures::default();
64-
match Signatures::try_from(&cose_sign.signatures) {
65-
Ok(s) => signatures = s,
66-
Err(sign_errors) => {
67-
for e in sign_errors.errors() {
68-
errors.push(anyhow!("{e}"));
69-
}
63+
let metadata = Metadata::try_from(&cose_sign.protected).map_or_else(
64+
|e| {
65+
errors.extend(e.0);
66+
None
67+
},
68+
Some,
69+
);
70+
let signatures = Signatures::try_from(&cose_sign.signatures).map_or_else(
71+
|e| {
72+
errors.extend(e.0);
73+
None
7074
},
75+
Some,
76+
);
77+
let author = signatures.as_ref().and_then(|s| s.kids().first().cloned());
78+
79+
if cose_sign.payload.is_none() {
80+
errors.push(anyhow!("Document Content is missing"));
81+
}
82+
if author.is_none() {
83+
errors.push(anyhow!("Document Author is missing"));
7184
}
7285

73-
if let Some(payload) = cose_sign.payload {
74-
match Content::new(
75-
payload,
76-
metadata.content_type(),
77-
metadata.content_encoding(),
78-
) {
79-
Ok(content) => {
80-
if !errors.is_empty() {
81-
return Err(error::Error(errors));
86+
match (cose_sign.payload, author, metadata, signatures) {
87+
(Some(payload), Some(author), Some(metadata), Some(signatures)) => {
88+
let content = Content::new(
89+
payload,
90+
metadata.content_type(),
91+
metadata.content_encoding(),
92+
)
93+
.map_err(|e| {
94+
errors.push(anyhow!("Invalid Document Content: {e}"));
95+
errors
96+
})?;
97+
98+
Ok(CatalystSignedDocument {
99+
inner: InnerCatalystSignedDocument {
100+
metadata,
101+
content,
102+
author,
103+
signatures,
82104
}
83-
84-
Ok(CatalystSignedDocument {
85-
inner: InnerCatalystSignedDocument {
86-
metadata,
87-
content,
88-
signatures,
89-
}
90-
.into(),
91-
})
92-
},
93-
Err(e) => {
94-
errors.push(anyhow::anyhow!("Invalid Document Content: {e}"));
95-
Err(error::Error(errors))
96-
},
97-
}
98-
} else {
99-
errors.push(anyhow!("Document content is missing"));
100-
Err(error::Error(errors))
105+
.into(),
106+
})
107+
},
108+
_ => Err(error::Error(errors)),
101109
}
102110
}
103111
}
@@ -129,15 +137,9 @@ impl CatalystSignedDocument {
129137
&self.inner.content
130138
}
131139

132-
/// Return a list of signature KIDs.
133-
#[must_use]
134-
pub fn signature_kids(&self) -> Vec<KidUri> {
135-
self.inner.signatures.kids()
136-
}
137-
138-
/// Return a list of signatures.
140+
/// Return a Document's author
139141
#[must_use]
140-
pub fn signatures(&self) -> Vec<CoseSignature> {
141-
self.inner.signatures.signatures()
142+
pub fn author(&self) -> KidUri {
143+
self.inner.author.clone()
142144
}
143145
}

rust/signed_doc/src/signature/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct Signature {
1313
}
1414

1515
/// List of Signatures.
16-
#[derive(Default)]
1716
pub struct Signatures(Vec<Signature>);
1817

1918
impl Signatures {
@@ -23,6 +22,7 @@ impl Signatures {
2322
}
2423

2524
/// List of signatures.
25+
#[allow(dead_code)]
2626
pub fn signatures(&self) -> Vec<CoseSignature> {
2727
self.0.iter().map(|sig| sig.signature.clone()).collect()
2828
}

0 commit comments

Comments
 (0)