Skip to content

Commit c287e3a

Browse files
committed
make a separate trait
1 parent 40478ae commit c287e3a

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

rust/signed_doc/src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ mod content;
55
pub mod doc_types;
66
pub mod error;
77
mod metadata;
8+
pub mod providers;
89
mod signature;
910
mod utils;
1011
pub mod validator;
1112

1213
use std::{
1314
convert::TryFrom,
1415
fmt::{Display, Formatter},
15-
future::Future,
1616
sync::Arc,
1717
};
1818

@@ -21,11 +21,11 @@ use catalyst_types::problem_report::ProblemReport;
2121
pub use catalyst_types::uuid::{Uuid, UuidV4, UuidV7};
2222
pub use content::Content;
2323
use coset::{CborSerializable, Header};
24-
use ed25519_dalek::VerifyingKey;
2524
use error::CatalystSignedDocError;
2625
use metadata::{ContentEncoding, ContentType};
2726
pub use metadata::{DocumentRef, ExtraFields, Metadata};
2827
use minicbor::{decode, encode, Decode, Decoder, Encode};
28+
use providers::VerifyingKeyProvider;
2929
pub use signature::{IdUri, Signatures};
3030
use utils::context::DecodeSignDocCtx;
3131

@@ -144,11 +144,9 @@ impl CatalystSignedDocument {
144144
/// Returns a report of verification failures and the source error.
145145
/// If `provider` returns error, fails fast and placed this error into
146146
/// `CatalystSignedDocError::error`.
147-
pub async fn verify<P, PF>(&self, provider: P) -> Result<(), CatalystSignedDocError>
148-
where
149-
P: Fn(&IdUri) -> PF,
150-
PF: Future<Output = anyhow::Result<Option<VerifyingKey>>>,
151-
{
147+
pub async fn verify(
148+
&self, provider: &impl VerifyingKeyProvider,
149+
) -> Result<(), CatalystSignedDocError> {
152150
let report = ProblemReport::new("Catalyst Signed Document Verification");
153151

154152
let cose_sign = match self.as_cose_sign() {
@@ -163,7 +161,7 @@ impl CatalystSignedDocument {
163161
};
164162

165163
for (signature, kid) in self.signatures().cose_signatures_with_kids() {
166-
match provider(kid).await {
164+
match provider.try_get_vk(kid).await {
167165
Ok(Some(pk)) => {
168166
let tbs_data = cose_sign.tbs_data(&[], signature);
169167
match signature.signature.as_slice().try_into() {
@@ -358,7 +356,7 @@ impl Encode<()> for CatalystSignedDocument {
358356
mod tests {
359357
use std::str::FromStr;
360358

361-
use ed25519_dalek::SigningKey;
359+
use ed25519_dalek::{SigningKey, VerifyingKey};
362360
use metadata::{ContentEncoding, ContentType};
363361
use rand::rngs::OsRng;
364362

@@ -413,6 +411,16 @@ mod tests {
413411
assert_eq!(decoded.doc_meta(), metadata.extra());
414412
}
415413

414+
struct Provider(anyhow::Result<Option<VerifyingKey>>);
415+
impl VerifyingKeyProvider for Provider {
416+
async fn try_get_vk(
417+
&self, _kid: &IdUri,
418+
) -> anyhow::Result<Option<ed25519_dalek::VerifyingKey>> {
419+
let res = self.0.as_ref().map_err(|e| anyhow::anyhow!("{e}"))?;
420+
Ok(*res)
421+
}
422+
}
423+
416424
#[tokio::test]
417425
async fn signature_verification_test() {
418426
let mut csprng = OsRng;
@@ -435,10 +443,10 @@ mod tests {
435443
.build()
436444
.unwrap();
437445

438-
assert!(signed_doc.verify(|_| async { Ok(Some(pk)) }).await.is_ok());
439-
assert!(signed_doc.verify(|_| async { Ok(None) }).await.is_err());
446+
assert!(signed_doc.verify(&Provider(Ok(Some(pk)))).await.is_ok());
447+
assert!(signed_doc.verify(&Provider(Ok(None))).await.is_err());
440448
assert!(signed_doc
441-
.verify(|_| async { Err(anyhow::anyhow!("some error")) })
449+
.verify(&Provider(Err(anyhow::anyhow!("some error"))))
442450
.await
443451
.is_err());
444452
}

rust/signed_doc/src/providers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! Providers traits, which are used during different validation procedures.
2+
3+
use std::future::Future;
4+
5+
use catalyst_types::id_uri::IdUri;
6+
use ed25519_dalek::VerifyingKey;
7+
8+
/// `VerifyingKey` Provider trait
9+
pub trait VerifyingKeyProvider {
10+
/// Try to get `VerifyingKey`
11+
fn try_get_vk(&self, kid: &IdUri)
12+
-> impl Future<Output = anyhow::Result<Option<VerifyingKey>>>;
13+
}

0 commit comments

Comments
 (0)