Skip to content

Commit b9f8882

Browse files
authored
feat(rust/signed-doc): Split into two ContentRule and TemplateRule (#540)
* distinguish ContentRule with the TemplateRule * cleanup * fix spelling * fix clippy * fix spelling * change Content internal representation to Option<Vec<u8>> * wip * fix clippy, add decoding test case
1 parent 90c3a50 commit b9f8882

File tree

11 files changed

+754
-467
lines changed

11 files changed

+754
-467
lines changed

rust/catalyst-signed-doc-macro/src/rules/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ pub(crate) fn catalyst_signed_documents_rules_impl() -> anyhow::Result<TokenStre
2626
exp: ContentEncoding::Brotli,
2727
optional: false,
2828
},
29-
content: crate::validator::rules::ContentRule::NotSpecified,
29+
template: crate::validator::rules::TemplateRule::NotSpecified,
3030
parameters: crate::validator::rules::ParametersRule::NotSpecified,
3131
doc_ref: #ref_rule,
3232
reply: crate::validator::rules::ReplyRule::NotSpecified,
3333
section: crate::validator::rules::SectionRule::NotSpecified,
34+
content: crate::validator::rules::ContentRule::Nil,
3435
kid: crate::validator::rules::SignatureKidRule {
3536
exp: &[],
3637
},

rust/signed_doc/src/builder.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct SignaturesBuilder {
3434
/// metadata
3535
metadata: WithCborBytes<Metadata>,
3636
/// content
37-
content: WithCborBytes<Content>,
37+
content: Content,
3838
/// signatures
3939
signatures: Signatures,
4040
}
@@ -71,7 +71,7 @@ impl ContentBuilder {
7171
fn into_signatures_builder(self) -> anyhow::Result<SignaturesBuilder> {
7272
Ok(SignaturesBuilder {
7373
metadata: WithCborBytes::new(self.metadata, &mut ())?,
74-
content: WithCborBytes::new(self.content, &mut ())?,
74+
content: self.content,
7575
signatures: Signatures::default(),
7676
})
7777
}
@@ -176,7 +176,7 @@ fn build_signature(
176176
sign_fn: impl FnOnce(Vec<u8>) -> Vec<u8>,
177177
kid: CatalystId,
178178
metadata: &WithCborBytes<Metadata>,
179-
content: &WithCborBytes<Content>,
179+
content: &Content,
180180
) -> anyhow::Result<Signature> {
181181
let data_to_sign = tbs_data(&kid, metadata, content)?;
182182
let sign_bytes = sign_fn(data_to_sign);
@@ -246,11 +246,13 @@ pub(crate) mod tests {
246246
kid: super::CatalystId,
247247
) -> anyhow::Result<Self> {
248248
let metadata = WithCborBytes::new(self.metadata, &mut ())?;
249-
let content = WithCborBytes::new(self.content, &mut ())?;
250-
self.signatures
251-
.push(super::build_signature(sign_fn, kid, &metadata, &content)?);
249+
self.signatures.push(super::build_signature(
250+
sign_fn,
251+
kid,
252+
&metadata,
253+
&self.content,
254+
)?);
252255
self.metadata = metadata.inner();
253-
self.content = content.inner();
254256
Ok(self)
255257
}
256258

rust/signed_doc/src/content.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
22
33
/// Document Content bytes (COSE payload).
44
#[derive(Debug, Clone, PartialEq, Default)]
5-
pub struct Content(Vec<u8>);
5+
pub struct Content(Option<Vec<u8>>);
66

77
impl Content {
88
/// Return content bytes.
99
#[must_use]
1010
pub fn bytes(&self) -> &[u8] {
11-
self.0.as_slice()
12-
}
13-
14-
/// Return content byte size.
15-
#[must_use]
16-
pub fn size(&self) -> usize {
17-
self.0.len()
11+
self.0.as_deref().unwrap_or(&[])
1812
}
1913
}
2014

2115
impl From<Vec<u8>> for Content {
2216
fn from(value: Vec<u8>) -> Self {
23-
Self(value)
17+
Self(Some(value))
2418
}
2519
}
2620

@@ -30,13 +24,16 @@ impl minicbor::Encode<()> for Content {
3024
e: &mut minicbor::Encoder<W>,
3125
_ctx: &mut (),
3226
) -> Result<(), minicbor::encode::Error<W::Error>> {
33-
if self.0.is_empty() {
34-
e.null()?;
35-
} else {
36-
e.bytes(self.0.as_slice())?;
37-
}
27+
match &self.0 {
28+
Some(bytes) => e.bytes(bytes)?,
29+
None => e.null()?,
30+
};
3831
Ok(())
3932
}
33+
34+
fn is_nil(&self) -> bool {
35+
self.0.is_none()
36+
}
4037
}
4138

4239
impl minicbor::Decode<'_, ()> for Content {
@@ -46,11 +43,11 @@ impl minicbor::Decode<'_, ()> for Content {
4643
) -> Result<Self, minicbor::decode::Error> {
4744
let p = d.position();
4845
d.null()
49-
.map(|()| Self(Vec::new()))
46+
.map(|()| Self(None))
5047
// important to use `or_else` so it will lazy evaluated at the time when it is needed
5148
.or_else(|_| {
5249
d.set_position(p);
53-
d.bytes().map(Vec::from).map(Self)
50+
d.bytes().map(Vec::from).map(Some).map(Self)
5451
})
5552
}
5653
}

rust/signed_doc/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct InnerCatalystSignedDocument {
4040
/// Document Metadata
4141
metadata: WithCborBytes<Metadata>,
4242
/// Document Content
43-
content: WithCborBytes<Content>,
43+
content: Content,
4444
/// Signatures
4545
signatures: Signatures,
4646
/// A comprehensive problem report, which could include a decoding errors along with
@@ -64,7 +64,6 @@ impl Display for CatalystSignedDocument {
6464
f: &mut Formatter<'_>,
6565
) -> Result<(), std::fmt::Error> {
6666
self.inner.metadata.fmt(f)?;
67-
writeln!(f, "Payload Size: {} bytes", self.inner.content.size())?;
6867
writeln!(f, "Signature Information")?;
6968
if self.inner.signatures.is_empty() {
7069
writeln!(f, " This document is unsigned.")?;
@@ -114,7 +113,7 @@ impl CatalystSignedDocument {
114113

115114
/// Return document content object.
116115
#[must_use]
117-
pub(crate) fn content(&self) -> &WithCborBytes<Content> {
116+
pub(crate) fn content(&self) -> &Content {
118117
&self.inner.content
119118
}
120119

@@ -286,7 +285,7 @@ impl Decode<'_, CompatibilityPolicy> for CatalystSignedDocument {
286285
);
287286
}
288287

289-
let content = WithCborBytes::<Content>::decode(
288+
let content = Content::decode(
290289
&mut minicbor::Decoder::new(content_bytes.as_slice()),
291290
&mut (),
292291
)?;

rust/signed_doc/src/signature/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Signatures {
7272
pub(crate) fn tbs_data(
7373
kid: &CatalystId,
7474
metadata: &WithCborBytes<Metadata>,
75-
content: &WithCborBytes<Content>,
75+
content: &Content,
7676
) -> anyhow::Result<Vec<u8>> {
7777
let mut e = minicbor::Encoder::new(Vec::new());
7878

rust/signed_doc/src/validator/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
},
2020
metadata::DocType,
2121
providers::{CatalystSignedDocumentProvider, VerifyingKeyProvider},
22-
validator::rules::SignatureRule,
22+
validator::rules::{SignatureRule, TemplateRule},
2323
CatalystSignedDocument, ContentEncoding, ContentType,
2424
};
2525

@@ -48,8 +48,8 @@ fn proposal_rule() -> Rules {
4848
exp: ContentEncoding::Brotli,
4949
optional: false,
5050
},
51-
content: ContentRule::Templated {
52-
exp_template_type: PROPOSAL_FORM_TEMPLATE.clone(),
51+
template: TemplateRule::Specified {
52+
allowed_type: PROPOSAL_FORM_TEMPLATE.clone(),
5353
},
5454
parameters: ParametersRule::Specified {
5555
exp_parameters_type: parameters.clone(),
@@ -58,6 +58,7 @@ fn proposal_rule() -> Rules {
5858
doc_ref: RefRule::NotSpecified,
5959
reply: ReplyRule::NotSpecified,
6060
section: SectionRule::NotSpecified,
61+
content: ContentRule::NotNil,
6162
kid: SignatureKidRule {
6263
exp: &[RoleId::Proposer],
6364
},
@@ -86,8 +87,8 @@ fn proposal_comment_rule() -> Rules {
8687
exp: ContentEncoding::Brotli,
8788
optional: false,
8889
},
89-
content: ContentRule::Templated {
90-
exp_template_type: PROPOSAL_COMMENT_FORM_TEMPLATE.clone(),
90+
template: TemplateRule::Specified {
91+
allowed_type: PROPOSAL_COMMENT_FORM_TEMPLATE.clone(),
9192
},
9293
doc_ref: RefRule::Specified {
9394
exp_ref_types: vec![PROPOSAL.clone()],
@@ -103,6 +104,7 @@ fn proposal_comment_rule() -> Rules {
103104
exp_parameters_type: parameters.clone(),
104105
optional: false,
105106
},
107+
content: ContentRule::NotNil,
106108
kid: SignatureKidRule {
107109
exp: &[RoleId::Role0],
108110
},
@@ -142,7 +144,7 @@ fn proposal_submission_action_rule() -> Rules {
142144
exp: ContentEncoding::Brotli,
143145
optional: false,
144146
},
145-
content: ContentRule::Static(ContentSchema::Json(proposal_action_json_schema)),
147+
template: TemplateRule::NotSpecified,
146148
parameters: ParametersRule::Specified {
147149
exp_parameters_type: parameters,
148150
optional: false,
@@ -154,6 +156,7 @@ fn proposal_submission_action_rule() -> Rules {
154156
},
155157
reply: ReplyRule::NotSpecified,
156158
section: SectionRule::NotSpecified,
159+
content: ContentRule::StaticSchema(ContentSchema::Json(proposal_action_json_schema)),
157160
kid: SignatureKidRule {
158161
exp: &[RoleId::Proposer],
159162
},

0 commit comments

Comments
 (0)