|
1 | 1 | //! A module for placing common structs, functions, and variables across the `document` |
2 | 2 | //! endpoint module not specified to a specific endpoint. |
3 | 3 |
|
4 | | -use std::collections::HashMap; |
5 | | - |
6 | 4 | use catalyst_signed_doc::CatalystSignedDocument; |
7 | 5 |
|
8 | 6 | use crate::{ |
@@ -157,16 +155,23 @@ impl catalyst_signed_doc_v1::providers::CatalystSignedDocumentProvider for DocPr |
157 | 155 | // TODO: make the struct to support multi sigs validation |
158 | 156 | /// A struct which implements a |
159 | 157 | /// `catalyst_signed_doc::providers::CatalystSignedDocumentProvider` trait |
160 | | -pub(crate) struct VerifyingKeyProvider( |
161 | | - HashMap<catalyst_signed_doc::CatalystId, ed25519_dalek::VerifyingKey>, |
162 | | -); |
| 158 | +pub(crate) struct VerifyingKeyProvider { |
| 159 | + /// A user's `CatalystId` from the corresponding `CatalystRBACTokenV1` |
| 160 | + kid: catalyst_signed_doc::CatalystId, |
| 161 | + /// A corresponding `VerifyingKey` derived from the `CatalystRBACTokenV1` |
| 162 | + pk: ed25519_dalek::VerifyingKey, |
| 163 | +} |
163 | 164 |
|
164 | 165 | impl catalyst_signed_doc::providers::CatalystIdProvider for VerifyingKeyProvider { |
165 | 166 | async fn try_get_registered_key( |
166 | 167 | &self, |
167 | 168 | kid: &catalyst_signed_doc::CatalystId, |
168 | 169 | ) -> anyhow::Result<Option<ed25519_dalek::VerifyingKey>> { |
169 | | - Ok(self.0.get(kid).copied()) |
| 170 | + if &self.kid == kid { |
| 171 | + Ok(Some(self.pk)) |
| 172 | + } else { |
| 173 | + Ok(None) |
| 174 | + } |
170 | 175 | } |
171 | 176 | } |
172 | 177 |
|
@@ -203,39 +208,38 @@ impl VerifyingKeyProvider { |
203 | 208 | anyhow::bail!("Multi-signature document is currently unsupported"); |
204 | 209 | } |
205 | 210 |
|
206 | | - if kids |
207 | | - .iter() |
208 | | - .any(|kid| kid.as_short_id() != token.catalyst_id().as_short_id()) |
209 | | - { |
| 211 | + let [kid] = kids else { |
| 212 | + anyhow::bail!("Multi-signature document is currently unsupported"); |
| 213 | + }; |
| 214 | + |
| 215 | + if kid != token.catalyst_id() { |
210 | 216 | anyhow::bail!("RBAC Token CatID does not match with the document KIDs"); |
211 | 217 | } |
212 | 218 |
|
213 | 219 | let Some(reg_chain) = token.reg_chain().await? else { |
214 | 220 | anyhow::bail!("Failed to retrieve a registration from corresponding Catalyst ID"); |
215 | 221 | }; |
216 | 222 |
|
217 | | - let result = kids.iter().map(|kid| { |
218 | | - if !kid.is_signature_key() { |
219 | | - anyhow::bail!("Invalid KID {kid}: KID must be a signing key not an encryption key"); |
220 | | - } |
| 223 | + if !kid.is_signature_key() { |
| 224 | + anyhow::bail!("Invalid KID {kid}: KID must be a signing key not an encryption key"); |
| 225 | + } |
221 | 226 |
|
222 | | - let (kid_role_index, kid_rotation) = kid.role_and_rotation(); |
223 | | - let (latest_pk, rotation) = reg_chain |
224 | | - .get_latest_signing_pk_for_role(&kid_role_index) |
225 | | - .ok_or_else(|| { |
| 227 | + let (kid_role_index, kid_rotation) = kid.role_and_rotation(); |
| 228 | + let (latest_pk, rotation) = reg_chain |
| 229 | + .get_latest_signing_pk_for_role(&kid_role_index) |
| 230 | + .ok_or_else(|| { |
226 | 231 | anyhow::anyhow!( |
227 | 232 | "Failed to get last signing key for the proposer role for {kid} Catalyst ID" |
228 | 233 | ) |
229 | 234 | })?; |
230 | 235 |
|
231 | | - if rotation != kid_rotation { |
232 | | - anyhow::bail!("Invalid KID {kid}: KID's rotation ({kid_rotation}) is not the latest rotation ({rotation})"); |
233 | | - } |
| 236 | + if rotation != kid_rotation { |
| 237 | + anyhow::bail!("Invalid KID {kid}: KID's rotation ({kid_rotation}) is not the latest rotation ({rotation})"); |
| 238 | + } |
234 | 239 |
|
235 | | - Ok((kid.clone(), latest_pk)) |
| 240 | + Ok(Self { |
| 241 | + kid: kid.clone(), |
| 242 | + pk: latest_pk, |
236 | 243 | }) |
237 | | - .collect::<Result<_, _>>()?; |
238 | | - |
239 | | - Ok(Self(result)) |
240 | 244 | } |
241 | 245 | } |
0 commit comments