Skip to content

Commit

Permalink
remove vp builder, use AnyJsonPresentation
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Sep 19, 2024
1 parent ee67e6b commit ae2c5c9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 224 deletions.
47 changes: 11 additions & 36 deletions src/core/response/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::core::presentation_submission::PresentationSubmission as Presentation
use anyhow::Error;
use base64::prelude::*;
use serde_json::{Map, Value as Json};
use ssi::claims::jwt::VerifiablePresentation;
use ssi::prelude::AnyJsonPresentation;

#[derive(Debug, Clone)]
pub struct IdToken(pub String);
Expand Down Expand Up @@ -59,7 +59,7 @@ impl TryFrom<Json> for VpToken {
fn try_from(value: Json) -> Result<Self, Self::Error> {
match value {
// NOTE: When parsing a Json string object, it must be base64Url encoded.
Json::String(s) => Ok(Self::Single(BASE64_URL_SAFE_NO_PAD.encode(s.as_bytes()))),
Json::String(s) => Ok(Self::Single(s)),
// NOTE: When the Json is an object, it must be a map.
Json::Object(map) => Ok(Self::SingleAsMap(map)),
Json::Array(arr) => {
Expand All @@ -79,12 +79,7 @@ impl TryFrom<VpToken> for Json {

fn try_from(value: VpToken) -> Result<Self, Self::Error> {
match value {
VpToken::Single(s) => {
let bytes = BASE64_URL_SAFE_NO_PAD.decode(s.as_bytes())?;
let s = String::from_utf8(bytes)?;

Ok(s.into())
}
VpToken::Single(s) => Ok(serde_json::Value::String(s)),
VpToken::SingleAsMap(map) => Ok(serde_json::Value::Object(map)),
VpToken::Many(tokens) => {
let mut arr: Vec<Json> = Vec::new();
Expand All @@ -97,39 +92,19 @@ impl TryFrom<VpToken> for Json {
}
}

impl TryFrom<VerifiablePresentation> for VpToken {
impl TryFrom<AnyJsonPresentation> for VpToken {
type Error = Error;

fn try_from(vp: VerifiablePresentation) -> Result<Self, Self::Error> {
Self::try_from(vp.0.into_serde_json())
fn try_from(vp: AnyJsonPresentation) -> Result<Self, Self::Error> {
Self::try_from(serde_json::to_value(vp)?)
}
}

impl TryFrom<VpToken> for Vec<VerifiablePresentation> {
type Error = Error;

fn try_from(token: VpToken) -> Result<Self, Self::Error> {
let mut vps = Vec::new();

match token {
VpToken::Single(s) => {
let bytes = BASE64_URL_SAFE_NO_PAD.decode(s.as_bytes())?;
let s = String::from_utf8(bytes)?;
let value = json_syntax::Value::from_serde_json(s.into());
vps.push(VerifiablePresentation(value))
}
VpToken::SingleAsMap(map) => {
let value = json_syntax::Value::from_serde_json(serde_json::Value::Object(map));
vps.push(VerifiablePresentation(value))
}
VpToken::Many(tokens) => {
for token in tokens {
vps.extend(Self::try_from(token)?);
}
}
}

Ok(vps)
impl VpToken {
pub fn as_base64_url_encoded(self) -> Result<String, Error> {
let json: Json = self.try_into()?;
let string = serde_json::to_string(&json)?;
Ok(BASE64_URL_SAFE_NO_PAD.encode(string.as_bytes()))
}
}

Expand Down
1 change: 0 additions & 1 deletion src/holder/mod.rs

This file was deleted.

164 changes: 0 additions & 164 deletions src/holder/verifiable_presentation_builder.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod core;
pub mod holder;
#[cfg(test)]
pub(crate) mod tests;
mod utils;
Expand Down
41 changes: 19 additions & 22 deletions tests/jwt_vp.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use std::str::FromStr;

use anyhow::Result;
use oid4vp::holder::verifiable_presentation_builder::{
VerifiablePresentationBuilder, VerifiablePresentationBuilderOptions,
};
use oid4vp::verifier::request_signer::P256Signer;
use ssi::claims::jwt::{self, VerifiablePresentation};
use ssi::claims::vc::v1::{JsonCredential, JsonPresentation};
use ssi::dids::DIDKey;
use ssi::jwk::JWK;

pub async fn create_test_verifiable_presentation() -> Result<VerifiablePresentation> {
let verifier = JWK::from_str(include_str!("examples/verifier.jwk"))?;
use ssi::json_ld::iref::UriBuf;
use ssi::prelude::AnyJsonPresentation;
use uuid::Uuid;

pub async fn create_test_verifiable_presentation() -> Result<AnyJsonPresentation> {
let signer = P256Signer::new(
p256::SecretKey::from_jwk_str(include_str!("examples/subject.jwk"))
.unwrap()
Expand All @@ -20,22 +15,24 @@ pub async fn create_test_verifiable_presentation() -> Result<VerifiablePresentat
.unwrap();

let holder_did = DIDKey::generate_url(signer.jwk())?;
let verifier_did = DIDKey::generate_url(&verifier)?;

// Create a verifiable presentation using the `examples/vc.jwt` file
// The signer information is the holder's key, also found in the `examples/subject.jwk` file.
let verifiable_credential: jwt::VerifiableCredential =
let verifiable_credential: JsonCredential =
ssi::claims::jwt::decode_unverified(include_str!("examples/vc.jwt"))?;

let verifiable_presentation =
VerifiablePresentationBuilder::from_options(VerifiablePresentationBuilderOptions {
issuer: holder_did.clone(),
subject: holder_did.clone(),
audience: verifier_did.clone(),
expiration_secs: 3600,
credentials: vec![verifiable_credential],
nonce: "random_nonce".into(),
});
let mut vp = JsonPresentation::default();

vp.verifiable_credentials.push(verifiable_credential);
vp.holder = Some(holder_did.into());

// NOTE: Should this be a `DID` or other value?
vp.id = UriBuf::new(
format!("urn:uuid:{}", Uuid::new_v4().to_string())
.as_bytes()
.to_vec(),
)
.ok();

Ok(verifiable_presentation)
Ok(AnyJsonPresentation::V1(vp))
}

0 comments on commit ae2c5c9

Please sign in to comment.