Skip to content

Commit

Permalink
refactor: set provided id (optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mader committed Nov 6, 2024
1 parent 787867b commit adcac7e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions agent_issuance/src/credential/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use types_ob_v3::prelude::{
AchievementCredential, AchievementCredentialBuilder, AchievementCredentialType, AchievementSubject, Profile,
ProfileBuilder,
};
use url::Url;

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub enum Status {
Expand Down Expand Up @@ -144,7 +145,13 @@ impl Aggregate for Credential {
serde_json::from_value::<AchievementSubject>(credential_subject_json.clone())
.map_err(|e| InvalidVerifiableCredentialError(e.to_string()))?;

let credential: AchievementCredential = AchievementCredentialBuilder::default()
let id = data
.raw
.get("id")
.and_then(|id| id.as_str())
.and_then(|id| Url::parse(id).ok());

let builder = AchievementCredentialBuilder::default()
.context(vec![
"https://www.w3.org/2018/credentials/v1",
"https://purl.imsglobal.org/spec/ob/v3p0/context-3.0.2.json",
Expand All @@ -153,14 +160,15 @@ impl Aggregate for Credential {
"VerifiableCredential",
&credential_format,
]))
// TODO: Come up with a way to get the credential id.
.id("http://example.com/credentials/3527")
.name(name)
.issuer(issuer)
.credential_subject(credential_subject)
.issuance_date(issuance_date)
.try_into()
.map_err(InvalidVerifiableCredentialError)?;
.issuance_date(issuance_date);

let builder = if let Some(id) = id { builder.id(id) } else { builder };

let credential: AchievementCredential =
builder.try_into().map_err(InvalidVerifiableCredentialError)?;

return Ok(vec![UnsignedCredentialCreated {
data: Data { raw: json!(credential) },
Expand All @@ -181,6 +189,13 @@ impl Aggregate for Credential {
return Ok(vec![]);
}

let id: Option<Url> = self
.data
.as_ref()
.and_then(|data| data.raw.get("id"))
.and_then(|id| id.as_str())
.and_then(|id| Url::parse(id).ok());

let default_did_method = get_preferred_did_method();

let issuer_did = services
Expand All @@ -191,6 +206,10 @@ impl Aggregate for Credential {
let signed_credential = {
let mut credential = self.data.as_ref().ok_or(MissingCredentialDataError)?.clone();

if let Some(ref id) = id {
credential.raw["id"] = json!(id);
};

credential.raw["issuer"] = json!(issuer_did);

let credential_subject = credential.raw["credentialSubject"].as_object().unwrap().clone();
Expand Down

0 comments on commit adcac7e

Please sign in to comment.