@@ -15,14 +15,13 @@ use std::{
15
15
sync:: Arc ,
16
16
} ;
17
17
18
- use anyhow:: Context ;
19
18
pub use builder:: Builder ;
20
19
pub use catalyst_types:: {
21
20
problem_report:: ProblemReport ,
22
21
uuid:: { Uuid , UuidV4 , UuidV7 } ,
23
22
} ;
24
23
pub use content:: Content ;
25
- use coset:: { CborSerializable , Header , TaggedCborSerializable } ;
24
+ use coset:: { CborSerializable , TaggedCborSerializable } ;
26
25
use decode_context:: { CompatibilityPolicy , DecodeContext } ;
27
26
pub use metadata:: { ContentEncoding , ContentType , DocType , DocumentRef , Metadata , Section } ;
28
27
use minicbor:: { decode, encode, Decode , Decoder , Encode } ;
@@ -68,7 +67,7 @@ impl Display for CatalystSignedDocument {
68
67
if self . inner . signatures . is_empty ( ) {
69
68
writeln ! ( f, " This document is unsigned." ) ?;
70
69
} else {
71
- for kid in & self . inner . signatures . kids ( ) {
70
+ for kid in & self . kids ( ) {
72
71
writeln ! ( f, " Signature Key ID: {kid}" ) ?;
73
72
}
74
73
}
@@ -147,13 +146,21 @@ impl CatalystSignedDocument {
147
146
/// Return a list of Document's Catalyst IDs.
148
147
#[ must_use]
149
148
pub fn kids ( & self ) -> Vec < CatalystId > {
150
- self . inner . signatures . kids ( )
149
+ self . inner
150
+ . signatures
151
+ . iter ( )
152
+ . map ( |s| s. kid ( ) . clone ( ) )
153
+ . collect ( )
151
154
}
152
155
153
156
/// Return a list of Document's author IDs (short form of Catalyst IDs).
154
157
#[ must_use]
155
158
pub fn authors ( & self ) -> Vec < CatalystId > {
156
- self . inner . signatures . authors ( )
159
+ self . inner
160
+ . signatures
161
+ . iter ( )
162
+ . map ( |s| s. kid ( ) . as_short_id ( ) )
163
+ . collect ( )
157
164
}
158
165
159
166
/// Returns a collected problem report for the document.
@@ -173,14 +180,6 @@ impl CatalystSignedDocument {
173
180
& self . inner . report
174
181
}
175
182
176
- /// Convert Catalyst Signed Document into `coset::CoseSign`
177
- ///
178
- /// # Errors
179
- /// Could fails if the `CatalystSignedDocument` object is not valid.
180
- pub ( crate ) fn as_cose_sign ( & self ) -> anyhow:: Result < coset:: CoseSign > {
181
- self . inner . as_cose_sign ( )
182
- }
183
-
184
183
/// Returns a signed document `Builder` pre-loaded with the current signed document's
185
184
/// data.
186
185
#[ must_use]
@@ -189,39 +188,6 @@ impl CatalystSignedDocument {
189
188
}
190
189
}
191
190
192
- impl InnerCatalystSignedDocument {
193
- /// Convert Catalyst Signed Document into `coset::CoseSign`
194
- ///
195
- /// # Errors
196
- /// Could fails if the `CatalystSignedDocument` object is not valid.
197
- fn as_cose_sign ( & self ) -> anyhow:: Result < coset:: CoseSign > {
198
- if let Some ( raw_bytes) = self . raw_bytes . clone ( ) {
199
- let cose_sign = coset:: CoseSign :: from_tagged_slice ( raw_bytes. as_slice ( ) )
200
- . or_else ( |_| coset:: CoseSign :: from_slice ( raw_bytes. as_slice ( ) ) )
201
- . map_err ( |e| {
202
- minicbor:: decode:: Error :: message ( format ! ( "Invalid COSE Sign document: {e}" ) )
203
- } ) ?;
204
- Ok ( cose_sign)
205
- } else {
206
- let protected_header =
207
- Header :: try_from ( & self . metadata ) . context ( "Failed to encode Document Metadata" ) ?;
208
-
209
- let content = self
210
- . content
211
- . encoded_bytes ( self . metadata . content_encoding ( ) ) ?;
212
-
213
- let mut builder = coset:: CoseSignBuilder :: new ( )
214
- . protected ( protected_header)
215
- . payload ( content) ;
216
-
217
- for signature in self . signatures . cose_signatures ( ) {
218
- builder = builder. add_signature ( signature) ;
219
- }
220
- Ok ( builder. build ( ) )
221
- }
222
- }
223
- }
224
-
225
191
impl Decode < ' _ , ( ) > for CatalystSignedDocument {
226
192
fn decode ( d : & mut Decoder < ' _ > , _ctx : & mut ( ) ) -> Result < Self , decode:: Error > {
227
193
let start = d. position ( ) ;
@@ -264,18 +230,38 @@ impl Decode<'_, ()> for CatalystSignedDocument {
264
230
}
265
231
}
266
232
267
- impl Encode < ( ) > for CatalystSignedDocument {
233
+ impl < C > Encode < C > for CatalystSignedDocument {
268
234
fn encode < W : minicbor:: encode:: Write > (
269
- & self , e : & mut encode:: Encoder < W > , _ctx : & mut ( ) ,
235
+ & self , e : & mut encode:: Encoder < W > , _ctx : & mut C ,
270
236
) -> Result < ( ) , encode:: Error < W :: Error > > {
271
- let cose_sign = self . as_cose_sign ( ) . map_err ( encode:: Error :: message) ?;
272
- let cose_bytes = cose_sign. to_tagged_vec ( ) . map_err ( |e| {
273
- minicbor:: encode:: Error :: message ( format ! ( "Failed to encode COSE Sign document: {e}" ) )
274
- } ) ?;
275
-
276
- e. writer_mut ( )
277
- . write_all ( & cose_bytes)
278
- . map_err ( |_| minicbor:: encode:: Error :: message ( "Failed to encode to CBOR" ) )
237
+ if let Some ( raw_bytes) = & self . inner . raw_bytes {
238
+ e. writer_mut ( )
239
+ . write_all ( raw_bytes)
240
+ . map_err ( minicbor:: encode:: Error :: write) ?;
241
+ } else {
242
+ // COSE_Sign tag
243
+ // <!https://datatracker.ietf.org/doc/html/rfc8152#page-9>
244
+ e. tag ( minicbor:: data:: Tag :: new ( 98 ) ) ?;
245
+ e. array ( 4 ) ?;
246
+ // protected headers (metadata fields)
247
+ e. bytes (
248
+ minicbor:: to_vec ( self . doc_meta ( ) )
249
+ . map_err ( minicbor:: encode:: Error :: message) ?
250
+ . as_slice ( ) ,
251
+ ) ?;
252
+ // empty unprotected headers
253
+ e. map ( 0 ) ?;
254
+ // content
255
+ let content = self
256
+ . doc_content ( )
257
+ . encoded_bytes ( self . doc_content_encoding ( ) )
258
+ . map_err ( minicbor:: encode:: Error :: message) ?;
259
+ e. bytes ( content. as_slice ( ) ) ?;
260
+ // signatures
261
+ e. encode ( self . signatures ( ) ) ?;
262
+ }
263
+
264
+ Ok ( ( ) )
279
265
}
280
266
}
281
267
0 commit comments