From ed431e3a6d897302f8b4b0cc5128ee66c30a0efa Mon Sep 17 00:00:00 2001 From: Simon Bihel Date: Thu, 27 Jun 2024 11:36:49 +0100 Subject: [PATCH 1/3] Allow both v1 and v2 VCs in JsonCredentialOrJws --- crates/claims/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/claims/src/lib.rs b/crates/claims/src/lib.rs index eca914aa9..6b786dcf5 100644 --- a/crates/claims/src/lib.rs +++ b/crates/claims/src/lib.rs @@ -49,7 +49,7 @@ pub use ssi_data_integrity as data_integrity; #[educe(Debug(bound("S: DebugCryptographicSuite")))] pub enum JsonCredentialOrJws { /// JSON-like verifiable credential. - Credential(DataIntegrity), + Credential(DataIntegrity), /// JSON Web Signature. Jws(jws::CompactJWSString), @@ -68,7 +68,7 @@ pub enum JsonCredentialOrJws { #[educe(Debug(bound("S: DebugCryptographicSuite")))] pub enum JsonPresentationOrJws { /// JSON-like verifiable presentation. - Presentation(DataIntegrity), + Presentation(DataIntegrity), /// JSON Web Signature. Jws(jws::CompactJWSString), From 141bd79aa042c7dea206f4219be17c7a015897d7 Mon Sep 17 00:00:00 2001 From: Simon Bihel Date: Thu, 27 Jun 2024 13:08:36 +0100 Subject: [PATCH 2/3] Make present example compile, and add the scenarios as tests Unsure how to fix the JWT scenarios --- examples/present.rs | 83 +++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/examples/present.rs b/examples/present.rs index 55eba34f5..316198ed5 100644 --- a/examples/present.rs +++ b/examples/present.rs @@ -19,41 +19,24 @@ use ssi::{ }; use ssi_claims::{ data_integrity::AnyDataIntegrity, - vc::v1::{SpecializedJsonCredential, ToJwtClaims}, + vc::{v1::ToJwtClaims, AnyJsonCredential}, }; use ssi_dids::DIDResolver; use static_iref::{iri, uri}; -#[async_std::main] -async fn main() { - let mut args = std::env::args().skip(1); - let proof_format_in = args.next().unwrap(); - let proof_format_out = args.next().unwrap(); - - let key_str = include_str!("../tests/ed25519-2020-10-18.json"); - let key: ssi::jwk::JWK = serde_json::from_str(key_str).unwrap(); - let resolver = ssi::dids::example::ExampleDIDResolver::default().with_default_options(); - let signer = SingleSecretSigner::new(key.clone()).into_local(); - - let mut reader = std::io::BufReader::new(std::io::stdin()); - let vc = match &proof_format_in[..] { +async fn verify(proof_format_in: &str, proof_format_out: &str, input_vc: &str) { + let vc = match proof_format_in { "ldp" => { - let vc_ldp: AnyDataIntegrity = - serde_json::from_reader(reader).unwrap(); + let vc_ldp: AnyDataIntegrity = + serde_json::from_str(input_vc).unwrap(); ssi::claims::JsonCredentialOrJws::Credential(vc_ldp) } - "jwt" => { - use std::io::Read; - let mut buffer = Vec::new(); - reader.read_to_end(&mut buffer).unwrap(); - - match CompactJWSString::new(buffer) { - Ok(vc_jwt) => ssi::claims::JsonCredentialOrJws::Jws(vc_jwt), - Err(_) => { - panic!("Input must be a compact JWT"); - } + "jwt" => match CompactJWSString::from_string(input_vc.to_string()) { + Ok(vc_jwt) => ssi::claims::JsonCredentialOrJws::Jws(vc_jwt), + Err(_) => { + panic!("Input must be a compact JWT"); } - } + }, format => panic!("unknown input proof format: {}", format), }; @@ -63,6 +46,11 @@ async fn main() { vec![vc], ); + let key_str = include_str!("../tests/ed25519-2020-10-18.json"); + let key: ssi::jwk::JWK = serde_json::from_str(key_str).unwrap(); + let resolver = ssi::dids::example::ExampleDIDResolver::default().with_default_options(); + let signer = SingleSecretSigner::new(key.clone()).into_local(); + // let mut proof_options = ssi::vc::LinkedDataProofOptions::default(); // let verification_method = "did:example:foo#key2".to_string(); // proof_options.verification_method = Some(ssi::vc::URI::String(verification_method)); @@ -70,7 +58,7 @@ async fn main() { // proof_options.challenge = Some("example".to_string()); let verification_method = iri!("did:example:foo#key2").into(); - match &proof_format_out[..] { + match proof_format_out { "ldp" => { let mut params = ProofOptions::from_method(verification_method); @@ -81,7 +69,7 @@ async fn main() { let vp = suite.sign(vp, &resolver, &signer, params).await.unwrap(); let result = vp.verify(&resolver).await.expect("verification failed"); - if !result.is_ok() { + if result.is_err() { panic!("verify failed"); } @@ -92,7 +80,7 @@ async fn main() { let jwt = vp.to_jwt_claims().unwrap().sign(&key).await.unwrap(); let result = jwt.verify(&resolver).await.expect("verification failed"); - if !result.is_ok() { + if result.is_err() { panic!("verify failed"); } @@ -101,3 +89,38 @@ async fn main() { format => panic!("unknown output proof format: {}", format), } } + +#[async_std::main] +async fn main() { + let mut args = std::env::args().skip(1); + let proof_format_in = args.next().unwrap(); + let proof_format_out = args.next().unwrap(); + + let input_vc = std::io::read_to_string(std::io::stdin()).unwrap(); + verify(&proof_format_in[..], &proof_format_out[..], &input_vc).await; +} + +#[cfg(test)] +mod test { + use super::*; + + #[async_std::test] + async fn ldp_ldp() { + verify("ldp", "ldp", include_str!("files/vc.jsonld")).await; + } + + #[async_std::test] + async fn ldp_jwt() { + verify("ldp", "jwt", include_str!("files/vc.jsonld")).await; + } + + #[async_std::test] + async fn jwt_ldp() { + verify("jwt", "ldp", include_str!("files/vc.jwt")).await; + } + + #[async_std::test] + async fn jwt_jwt() { + verify("jwt", "jwt", include_str!("files/vc.jwt")).await; + } +} From e6981baf4fbc5c2aad57cf6e0189fffd94e18d2a Mon Sep 17 00:00:00 2001 From: Simon Bihel Date: Thu, 27 Jun 2024 16:55:43 +0100 Subject: [PATCH 3/3] Fix example --- examples/present.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/present.rs b/examples/present.rs index 316198ed5..7869f75f5 100644 --- a/examples/present.rs +++ b/examples/present.rs @@ -47,7 +47,8 @@ async fn verify(proof_format_in: &str, proof_format_out: &str, input_vc: &str) { ); let key_str = include_str!("../tests/ed25519-2020-10-18.json"); - let key: ssi::jwk::JWK = serde_json::from_str(key_str).unwrap(); + let mut key: ssi::jwk::JWK = serde_json::from_str(key_str).unwrap(); + key.key_id = Some("did:example:foo#key2".to_string()); let resolver = ssi::dids::example::ExampleDIDResolver::default().with_default_options(); let signer = SingleSecretSigner::new(key.clone()).into_local();