1
1
//! Catalyst Signed Document Extra Fields.
2
2
3
- use catalyst_types:: { problem_report:: ProblemReport , uuid :: UuidV4 } ;
3
+ use catalyst_types:: problem_report:: ProblemReport ;
4
4
use coset:: { cbor:: Value , Label , ProtectedHeader } ;
5
5
6
6
use super :: {
7
- cose_protected_header_find,
8
- utils:: { decode_document_field_from_protected_header, CborUuidV4 } ,
9
- DocumentRef , Section ,
7
+ cose_protected_header_find, utils:: decode_document_field_from_protected_header, DocumentRef ,
8
+ Section ,
10
9
} ;
11
10
12
11
/// `ref` field COSE key value
@@ -19,13 +18,13 @@ const REPLY_KEY: &str = "reply";
19
18
const SECTION_KEY : & str = "section" ;
20
19
/// `collabs` field COSE key value
21
20
const COLLABS_KEY : & str = "collabs" ;
22
- /// `brand_id` field COSE key value
21
+ /// `parameters` field COSE key value
22
+ const PARAMETERS_KEY : & str = "parameters" ;
23
+ /// `brand_id` field COSE key value (alias of the `parameters` field)
23
24
const BRAND_ID_KEY : & str = "brand_id" ;
24
- /// `campaign_id` field COSE key value
25
+ /// `campaign_id` field COSE key value (alias of the `parameters` field)
25
26
const CAMPAIGN_ID_KEY : & str = "campaign_id" ;
26
- /// `election_id` field COSE key value
27
- const ELECTION_ID_KEY : & str = "election_id" ;
28
- /// `category_id` field COSE key value
27
+ /// `category_id` field COSE key value (alias of the `parameters` field)
29
28
const CATEGORY_ID_KEY : & str = "category_id" ;
30
29
31
30
/// Extra Metadata Fields.
@@ -48,18 +47,9 @@ pub struct ExtraFields {
48
47
/// Reference to the document collaborators. Collaborator type is TBD.
49
48
#[ serde( default = "Vec::new" , skip_serializing_if = "Vec::is_empty" ) ]
50
49
collabs : Vec < String > ,
51
- /// Unique identifier for the brand that is running the voting .
50
+ /// Reference to the parameters document .
52
51
#[ serde( skip_serializing_if = "Option::is_none" ) ]
53
- brand_id : Option < DocumentRef > ,
54
- /// Unique identifier for the campaign of voting.
55
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
56
- campaign_id : Option < DocumentRef > ,
57
- /// Unique identifier for the election.
58
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
59
- election_id : Option < UuidV4 > ,
60
- /// Unique identifier for the voting category as a collection of proposals.
61
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
62
- category_id : Option < DocumentRef > ,
52
+ parameters : Option < DocumentRef > ,
63
53
}
64
54
65
55
impl ExtraFields {
@@ -93,28 +83,10 @@ impl ExtraFields {
93
83
& self . collabs
94
84
}
95
85
96
- /// Return `brand_id` field.
97
- #[ must_use]
98
- pub fn brand_id ( & self ) -> Option < DocumentRef > {
99
- self . brand_id
100
- }
101
-
102
- /// Return `campaign_id` field.
103
- #[ must_use]
104
- pub fn campaign_id ( & self ) -> Option < DocumentRef > {
105
- self . campaign_id
106
- }
107
-
108
- /// Return `election_id` field.
86
+ /// Return `parameters` field.
109
87
#[ must_use]
110
- pub fn election_id ( & self ) -> Option < UuidV4 > {
111
- self . election_id
112
- }
113
-
114
- /// Return `category_id` field.
115
- #[ must_use]
116
- pub fn category_id ( & self ) -> Option < DocumentRef > {
117
- self . category_id
88
+ pub fn parameters ( & self ) -> Option < DocumentRef > {
89
+ self . parameters
118
90
}
119
91
120
92
/// Fill the COSE header `ExtraFields` data into the header builder.
@@ -141,26 +113,11 @@ impl ExtraFields {
141
113
Value :: Array ( self . collabs . iter ( ) . cloned ( ) . map ( Value :: Text ) . collect ( ) ) ,
142
114
) ;
143
115
}
144
- if let Some ( brand_id) = & self . brand_id {
145
- builder = builder. text_value ( BRAND_ID_KEY . to_string ( ) , Value :: try_from ( * brand_id) ?) ;
146
- }
147
116
148
- if let Some ( campaign_id) = & self . campaign_id {
149
- builder =
150
- builder. text_value ( CAMPAIGN_ID_KEY . to_string ( ) , Value :: try_from ( * campaign_id) ?) ;
117
+ if let Some ( parameters) = & self . parameters {
118
+ builder = builder. text_value ( PARAMETERS_KEY . to_string ( ) , Value :: try_from ( * parameters) ?) ;
151
119
}
152
120
153
- if let Some ( election_id) = & self . election_id {
154
- builder = builder. text_value (
155
- ELECTION_ID_KEY . to_string ( ) ,
156
- Value :: try_from ( CborUuidV4 ( * election_id) ) ?,
157
- ) ;
158
- }
159
-
160
- if let Some ( category_id) = & self . category_id {
161
- builder =
162
- builder. text_value ( CATEGORY_ID_KEY . to_string ( ) , Value :: try_from ( * category_id) ?) ;
163
- }
164
121
Ok ( builder)
165
122
}
166
123
@@ -196,41 +153,38 @@ impl ExtraFields {
196
153
COSE_DECODING_CONTEXT ,
197
154
error_report,
198
155
) ;
199
- let brand_id = decode_document_field_from_protected_header (
200
- protected,
156
+
157
+ // process `parameters` field and all its aliases
158
+ let ( parameters, has_multiple_fields) = [
159
+ PARAMETERS_KEY ,
201
160
BRAND_ID_KEY ,
202
- COSE_DECODING_CONTEXT ,
203
- error_report,
204
- ) ;
205
- let campaign_id = decode_document_field_from_protected_header (
206
- protected,
207
161
CAMPAIGN_ID_KEY ,
208
- COSE_DECODING_CONTEXT ,
209
- error_report,
210
- ) ;
211
- let election_id = decode_document_field_from_protected_header :: < CborUuidV4 > (
212
- protected,
213
- ELECTION_ID_KEY ,
214
- COSE_DECODING_CONTEXT ,
215
- error_report,
216
- )
217
- . map ( |v| v. 0 ) ;
218
- let category_id = decode_document_field_from_protected_header (
219
- protected,
220
162
CATEGORY_ID_KEY ,
221
- COSE_DECODING_CONTEXT ,
222
- error_report,
223
- ) ;
163
+ ]
164
+ . iter ( )
165
+ . filter_map ( |field_name| -> Option < DocumentRef > {
166
+ decode_document_field_from_protected_header (
167
+ protected,
168
+ field_name,
169
+ COSE_DECODING_CONTEXT ,
170
+ error_report,
171
+ )
172
+ } )
173
+ . fold ( ( None , false ) , |( res, _) , v| ( Some ( v) , res. is_some ( ) ) ) ;
174
+ if has_multiple_fields {
175
+ error_report. duplicate_field (
176
+ "brand_id, campaign_id, category_id" ,
177
+ "Only value at the same time is allowed parameters, brand_id, campaign_id, category_id" ,
178
+ "Validation of parameters field aliases"
179
+ ) ;
180
+ }
224
181
225
182
let mut extra = ExtraFields {
226
183
doc_ref,
227
184
template,
228
185
reply,
229
186
section,
230
- brand_id,
231
- campaign_id,
232
- election_id,
233
- category_id,
187
+ parameters,
234
188
..Default :: default ( )
235
189
} ;
236
190
0 commit comments