@@ -7,7 +7,7 @@ use std::{
77
88use anyhow:: anyhow;
99use content:: Content ;
10- use coset:: { CborSerializable , CoseSignature } ;
10+ use coset:: CborSerializable ;
1111
1212mod content;
1313mod 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}
0 commit comments