Skip to content

Commit

Permalink
wip: debug AnyJsonCredential inner type
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Sep 24, 2024
1 parent 49a3287 commit bf8d2a7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ name = "uniffi-bindgen"
path = "uniffi-bindgen.rs"

[dependencies]

async-trait = "0.1"
base64 = "0.22.0"
either = "1.13"
Expand Down
6 changes: 3 additions & 3 deletions src/credential/jwt_vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use base64::prelude::*;
use ssi::{
claims::{
jwt::IntoDecodedJwt,
vc::{v1::Credential as _, v2::Credential as _},
vc::{syntax::NonEmptyObject, v1::Credential as _, v2::Credential as _},
JwsString,
},
prelude::AnyJsonCredential,
Expand All @@ -20,7 +20,7 @@ use super::{Credential, VcdmVersion};
pub struct JwtVc {
id: Uuid,
jws: JwsString,
credential: AnyJsonCredential,
credential: AnyJsonCredential<NonEmptyObject>,
credential_string: String,
header_json_string: String,
payload_json_string: String,
Expand Down Expand Up @@ -153,7 +153,7 @@ impl JwtVc {
}

/// Return the internal `AnyJsonCredential` type
pub fn credential(&self) -> AnyJsonCredential {
pub fn credential(&self) -> AnyJsonCredential<NonEmptyObject> {
self.credential.clone()
}
}
Expand Down
36 changes: 17 additions & 19 deletions src/credential/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use jwt_vc::{JwtVc, JwtVcInitError};
use mdoc::{Mdoc, MdocEncodingError, MdocInitError};
use oid4vp::core::credential_format::ClaimFormatDesignation;
use serde::{Deserialize, Serialize};
use ssi::prelude::AnyJsonCredential;
use ssi::{claims::vc::syntax::NonEmptyObject, prelude::AnyJsonCredential};

/// An unparsed credential, retrieved from storage.
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
Expand Down Expand Up @@ -45,24 +45,22 @@ impl Credential {
}
}

// impl TryFrom<Credential> for AnyJsonCredential {
// type Error = CredentialDecodingError;

// fn try_from(value: Credential) -> Result<Self, Self::Error> {
// match value.format {
// CredentialFormat::JwtVcJson => {
// let jwt_vc: Arc<JwtVc> = value.try_into()?;
// Ok(jwt_vc.credential())
// }
// _ => {
// Err(
// ,
// value.format
// )
// }
// }
// }
// }
impl TryFrom<Credential> for AnyJsonCredential<NonEmptyObject> {
type Error = CredentialDecodingError;

fn try_from(value: Credential) -> Result<Self, Self::Error> {
match value.format {
CredentialFormat::JwtVcJson => {
let jwt_vc: Arc<JwtVc> = value.try_into()?;
Ok(jwt_vc.credential())
}
// TODO: Add more formats here.
_ => Err(Self::Error::UnsupportedCredentialFormat(
value.format.to_string(),
)),
}
}
}

/// A credential that has been parsed as a known variant.
#[derive(Debug, Clone, uniffi::Object)]
Expand Down
71 changes: 50 additions & 21 deletions src/oid4vp/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;

use json_syntax::Object;
use oid4vp::core::authorization_request::parameters::ClientIdScheme;
use oid4vp::core::credential_format::ClaimFormatDesignation;
use oid4vp::{
Expand All @@ -25,13 +26,15 @@ use oid4vp::{
wallet::Wallet as OID4VPWallet,
};
use ssi::claims::jws::JwsSigner;
use ssi::claims::jwt::VerifiableCredential;
use ssi::claims::vc::v1::Context;
use ssi::claims::vc::v1::JsonPresentation;
use ssi::claims::vc;
use ssi::claims::vc::syntax::IdOr;
use ssi::claims::vc::syntax::NonEmptyObject;
use ssi::claims::vc::v2::SpecializedJsonCredential;
use ssi::claims::vc::AnySpecializedJsonCredential;
use ssi::dids::DIDKey;
use ssi::dids::DIDURLBuf;
use ssi::json_ld::iref::UriBuf;
use ssi::prelude::AnyJsonCredential;
use ssi::prelude::AnyJsonPresentation;
use ssi::JWK;
use uniffi::deps::{anyhow, log};

Expand Down Expand Up @@ -319,40 +322,66 @@ impl Holder {
request: &AuthorizationRequestObject,
credential_descriptor_map: Vec<Credential>,
) -> Result<VpToken, Error> {
// NOTE: This assumes the client id scheme is DID URL.
let client_id = DIDURLBuf::from_str(&request.client_id().0)
.map_err(|e| Error::InvalidDIDUrl(e.to_string()))?;

let credentials = credential_descriptor_map
.into_iter()
.map(|credential| credential.try_into().map_err(Error::from))
.collect::<Result<Vec<AnyJsonCredential>, Error>>()?;

let did_key = DIDKey::generate_url(&self.jwk()?)
.map_err(|e| Error::DIDKeyGenerateUrl(e.to_string()))?;

credentials.into_iter().map(|c| ).collect()
let credentials = credential_descriptor_map
.into_iter()
.map(|credential| credential.try_into().map_err(Error::from))
.collect::<Result<Vec<AnyJsonCredential<NonEmptyObject>>, Error>>()?;

// Split into V1 and V2 credentials.
let v1_credentials = credentials
.iter()
.filter_map(|c| match c {
AnyJsonCredential::V1(c) => Some(c),
_ => None,
})
.map(ToOwned::to_owned)
.collect::<Vec<vc::v1::JsonCredential>>();

let mut v1_vp = vc::v1::syntax::JsonPresentation::default();
v1_vp.context = vc::v1::Context::default();
v1_vp.verifiable_credentials = v1_credentials;
v1_vp.holder = Some(client_id.into());
v1_vp.id = UriBuf::new(
format!("urn:uuid:{}", Uuid::new_v4().to_string())
.as_bytes()
.to_vec(),
)
.ok();

let v2_credentials = credentials
.into_iter()
.filter_map(|cred| match cred {
AnySpecializedJsonCredential::<NonEmptyObject>::V2(c) => Some(c),
_ => None,
})
.collect::<Vec<SpecializedJsonCredential<_>>>();

let mut vp = JsonPresentation::default();
vp.context = Context::default();
vp.verifiable_credentials = ;
vp.holder = Some(client_id.into());
vp.id = UriBuf::new(
let mut v2_vp = vc::v2::syntax::JsonPresentation::default();
v2_vp.context = vc::v2::Context::default();
v2_vp.verifiable_credentials = v2_credentials;
v2_vp.holders = vec![IdOr::Id(client_id.into())];
v2_vp.id = UriBuf::new(
format!("urn:uuid:{}", Uuid::new_v4().to_string())
.as_bytes()
.to_vec(),
)
.ok();

Ok(AnyJsonPresentation::V1(vp))
unimplemented!()

// Ok(AnyJsonPresentation::V1(vp))

let token: VpToken = verifiable_presentation
.try_into()
.map_err(|e: anyhow::Error| Error::Token(e.to_string()))?;
// let token: VpToken = verifiable_presentation
// .try_into()
// .map_err(|e: anyhow::Error| Error::Token(e.to_string()))?;

Ok(token)
// Ok(token)
}
}

Expand Down

0 comments on commit bf8d2a7

Please sign in to comment.