Skip to content

Commit c0417da

Browse files
committed
wip
1 parent d74cc62 commit c0417da

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

rust/catalyst-signed-doc-spec/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{collections::HashMap, fmt::Display};
1313

1414
use build_info as build_info_lib;
1515

16-
use crate::{copyright::Copyright, headers::Headers, metadata::Metadata};
16+
use crate::{copyright::Copyright, headers::Headers, metadata::Metadata, signers::Signers};
1717

1818
build_info_lib::build_info!(pub(crate) fn build_info);
1919

@@ -65,6 +65,7 @@ pub struct DocSpec {
6565
pub doc_type: String,
6666
pub headers: Headers,
6767
pub metadata: Metadata,
68+
pub signers: Signers,
6869
}
6970

7071
impl CatalystSignedDocSpec {

rust/signed_doc/src/validator/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn proposal_rule() -> Rules {
5757
section: SectionRule::NotSpecified,
5858
content: ContentRule::NotNil,
5959
kid: SignatureKidRule {
60-
exp: &[RoleId::Proposer],
60+
allowed_roles: vec![RoleId::Proposer],
6161
},
6262
signature: SignatureRule { mutlisig: false },
6363
original_author: OriginalAuthorRule,
@@ -103,7 +103,7 @@ fn proposal_comment_rule() -> Rules {
103103
},
104104
content: ContentRule::NotNil,
105105
kid: SignatureKidRule {
106-
exp: &[RoleId::Role0],
106+
allowed_roles: vec![RoleId::Role0],
107107
},
108108
signature: SignatureRule { mutlisig: false },
109109
original_author: OriginalAuthorRule,
@@ -155,7 +155,7 @@ fn proposal_submission_action_rule() -> Rules {
155155
section: SectionRule::NotSpecified,
156156
content: ContentRule::StaticSchema(ContentSchema::Json(proposal_action_json_schema)),
157157
kid: SignatureKidRule {
158-
exp: &[RoleId::Proposer],
158+
allowed_roles: vec![RoleId::Proposer],
159159
},
160160
signature: SignatureRule { mutlisig: false },
161161
original_author: OriginalAuthorRule,

rust/signed_doc/src/validator/rules/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Rules {
129129
reply: ReplyRule::NotSpecified,
130130
section: SectionRule::NotSpecified,
131131
content: ContentRule::Nil,
132-
kid: SignatureKidRule { exp: &[] },
132+
kid: SignatureKidRule::new(&doc_spec.signers.roles),
133133
signature: SignatureRule { mutlisig: false },
134134
original_author: OriginalAuthorRule,
135135
};

rust/signed_doc/src/validator/rules/signature_kid.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Catalyst Signed Document COSE signature `kid` (Catalyst Id) role validation
22
3+
use catalyst_signed_doc_spec::signers::roles::{Role, Roles};
34
use catalyst_types::catalyst_id::role_index::RoleId;
45

56
use crate::CatalystSignedDocument;
@@ -8,10 +9,26 @@ use crate::CatalystSignedDocument;
89
#[derive(Debug)]
910
pub(crate) struct SignatureKidRule {
1011
/// expected `RoleId` values for the `kid` field
11-
pub(crate) exp: &'static [RoleId],
12+
pub(crate) allowed_roles: Vec<RoleId>,
1213
}
1314

1415
impl SignatureKidRule {
16+
/// Generating `SignatureKidRule` from specs
17+
pub(crate) fn new(spec: &Roles) -> Self {
18+
let allowed_roles = spec
19+
.user
20+
.iter()
21+
.map(|v| {
22+
match v {
23+
Role::Registered => RoleId::Role0,
24+
Role::Proposer => RoleId::Proposer,
25+
Role::Representative => RoleId::DelegatedRepresentative,
26+
}
27+
})
28+
.collect();
29+
Self { allowed_roles }
30+
}
31+
1532
/// Field validation rule
1633
#[allow(clippy::unused_async)]
1734
pub(crate) async fn check(
@@ -20,12 +37,12 @@ impl SignatureKidRule {
2037
) -> anyhow::Result<bool> {
2138
let contains_exp_role = doc.kids().iter().enumerate().all(|(i, kid)| {
2239
let (role_index, _) = kid.role_and_rotation();
23-
let res = self.exp.contains(&role_index);
40+
let res = self.allowed_roles.contains(&role_index);
2441
if !res {
2542
doc.report().invalid_value(
2643
"kid",
2744
role_index.to_string().as_str(),
28-
format!("{:?}", self.exp).as_str(),
45+
format!("{:?}", self.allowed_roles).as_str(),
2946
format!(
3047
"Invalid Catalyst Signed Document signature at position [{i}] `kid` Catalyst Role value"
3148
)
@@ -56,7 +73,7 @@ mod tests {
5673
#[tokio::test]
5774
async fn signature_kid_rule_test() {
5875
let mut rule = SignatureKidRule {
59-
exp: &[RoleId::Role0, RoleId::DelegatedRepresentative],
76+
allowed_roles: vec![RoleId::Role0, RoleId::DelegatedRepresentative],
6077
};
6178

6279
let sk = ed25519_dalek::SigningKey::generate(&mut rand::rngs::OsRng);
@@ -75,7 +92,7 @@ mod tests {
7592

7693
assert!(rule.check(&doc).await.unwrap());
7794

78-
rule.exp = &[RoleId::Proposer];
95+
rule.allowed_roles = vec![RoleId::Proposer];
7996
assert!(!rule.check(&doc).await.unwrap());
8097
}
8198
}

0 commit comments

Comments
 (0)