@@ -6,29 +6,26 @@ use std::{
66} ;
77
88use anyhow:: anyhow;
9+ use content:: Content ;
910use coset:: { CborSerializable , CoseSignature } ;
1011
12+ mod content;
1113mod error;
1214mod metadata;
13- mod payload;
1415mod signature;
1516
1617pub use metadata:: { DocumentRef , Metadata , UuidV7 } ;
17- use payload:: JsonContent ;
1818pub use signature:: KidUri ;
1919use signature:: Signatures ;
2020
2121/// Inner type that holds the Catalyst Signed Document with parsing errors.
22- #[ derive( Default ) ]
2322struct InnerCatalystSignedDocument {
2423 /// Document Metadata
2524 metadata : Metadata ,
26- /// Document Payload viewed as JSON Content
27- payload : JsonContent ,
25+ /// Document Content
26+ content : Content ,
2827 /// Signatures
2928 signatures : Signatures ,
30- /// Raw COSE Sign data
31- cose_sign : coset:: CoseSign ,
3229}
3330
3431/// Keep all the contents private.
@@ -43,15 +40,9 @@ pub struct CatalystSignedDocument {
4340impl Display for CatalystSignedDocument {
4441 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
4542 writeln ! ( f, "{}" , self . inner. metadata) ?;
46- writeln ! ( f, "{:#?}\n " , self . inner. payload) ?;
4743 writeln ! ( f, "Signature Information [" ) ?;
48- for signature in & self . inner . cose_sign . signatures {
49- writeln ! (
50- f,
51- " {} 0x{:#}" ,
52- String :: from_utf8_lossy( & signature. protected. header. key_id) ,
53- hex:: encode( signature. signature. as_slice( ) )
54- ) ?;
44+ for kid in & self . inner . signatures . kids ( ) {
45+ writeln ! ( f, " {kid}" ) ?;
5546 }
5647 writeln ! ( f, "]\n " )
5748 }
@@ -67,44 +58,47 @@ impl TryFrom<&[u8]> for CatalystSignedDocument {
6758
6859 let metadata = Metadata :: try_from ( & cose_sign. protected ) ?;
6960
70- let mut content_errors = Vec :: new ( ) ;
71- let mut payload = JsonContent :: default ( ) ;
72-
73- if let Some ( bytes) = & cose_sign. payload {
74- match JsonContent :: try_from ( ( bytes. as_ref ( ) , metadata. content_encoding ( ) ) ) {
75- Ok ( c) => payload = c,
76- Err ( e) => {
77- content_errors. push ( anyhow ! ( "Invalid Payload: {e}" ) ) ;
78- } ,
79- }
80- } else {
81- content_errors. push ( anyhow ! ( "COSE payload is empty" ) ) ;
82- } ;
61+ let mut errors = Vec :: new ( ) ;
8362
8463 let mut signatures = Signatures :: default ( ) ;
8564 match Signatures :: try_from ( & cose_sign. signatures ) {
8665 Ok ( s) => signatures = s,
87- Err ( errors ) => {
88- for e in errors . errors ( ) {
89- content_errors . push ( anyhow ! ( "{e}" ) ) ;
66+ Err ( sign_errors ) => {
67+ for e in sign_errors . errors ( ) {
68+ errors . push ( anyhow ! ( "{e}" ) ) ;
9069 }
9170 } ,
9271 }
9372
94- let inner = InnerCatalystSignedDocument {
95- metadata,
96- payload,
97- signatures,
98- cose_sign,
99- } ;
100-
101- if !content_errors. is_empty ( ) {
102- return Err ( error:: Error ( content_errors) ) ;
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) ) ;
82+ }
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) )
103101 }
104-
105- Ok ( CatalystSignedDocument {
106- inner : Arc :: new ( inner) ,
107- } )
108102 }
109103}
110104
@@ -129,34 +123,10 @@ impl CatalystSignedDocument {
129123 self . inner . metadata . doc_ver ( )
130124 }
131125
132- /// Return Last Document Reference `Option<DocumentRef>`.
133- #[ must_use]
134- pub fn doc_ref ( & self ) -> Option < DocumentRef > {
135- self . inner . metadata . doc_ref ( )
136- }
137-
138- /// Return Document Template `Option<DocumentRef>`.
139- #[ must_use]
140- pub fn doc_template ( & self ) -> Option < DocumentRef > {
141- self . inner . metadata . doc_template ( )
142- }
143-
144- /// Return Document Reply `Option<DocumentRef>`.
145- #[ must_use]
146- pub fn doc_reply ( & self ) -> Option < DocumentRef > {
147- self . inner . metadata . doc_reply ( )
148- }
149-
150- /// Return Document Reply `Option<DocumentRef>`.
151- #[ must_use]
152- pub fn doc_section ( & self ) -> Option < String > {
153- self . inner . metadata . doc_section ( )
154- }
155-
156- /// Return Raw COSE SIGN bytes.
126+ /// Return document `Content`.
157127 #[ must_use]
158- pub fn cose_sign_bytes ( & self ) -> Vec < u8 > {
159- self . inner . cose_sign . clone ( ) . to_vec ( ) . unwrap_or_default ( )
128+ pub fn document_content ( & self ) -> & Content {
129+ & self . inner . content
160130 }
161131
162132 /// Return a list of signature KIDs.
0 commit comments