Skip to content

Commit

Permalink
adding presentation result
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal committed Sep 9, 2024
1 parent 28d229a commit 99dd95d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
45 changes: 27 additions & 18 deletions crates/web5/src/credentials/presentation_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ pub struct InputDescriptorMapping {
pub path_nested: Option<Box<InputDescriptorMapping>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PresentationResult {
pub presentation_submission: PresentationSubmission,
pub matched_vc_jwts: Vec<String>,
}

fn generate_token() -> String {
Uuid::new_v4().to_string()
}
Expand Down Expand Up @@ -178,13 +184,13 @@ impl PresentationDefinition {
/// * `presentation_definition` - The Presentation Definition V2 object against which VCs are validated.
///
/// # Returns
///
/// A `PresentationResult` which holds the `PresentationSubmission` and a Vec<String> which has the vc_jwts that were used
/// A `PresentationSubmission` object.
/// A `Vec<String>` which contains the chosen vc_jwts
pub fn create_presentation_from_credentials(
&self,
vc_jwts: &Vec<String>,
) -> Result<(PresentationSubmission, Vec<String>)> {
) -> Result<PresentationResult> {
// Check if there are submission requirements (not supported in this implementation)
if self.submission_requirements.is_some() {
return Err(PexError::IllegalState(
Expand Down Expand Up @@ -252,8 +258,10 @@ impl PresentationDefinition {
descriptor_map,
};

// Return both the presentation submission and the list of used vc_jwts
Ok((presentation_submission, used_vc_jwts))
Ok(PresentationResult {
presentation_submission,
matched_vc_jwts: used_vc_jwts,
})
}
}

Expand Down Expand Up @@ -449,13 +457,14 @@ mod tests {

assert!(result.is_ok(), "Failed to create presentation submission");

let presentation_submission = result.unwrap();
let presentation_result = result.unwrap();

assert_eq!(presentation_result.presentation_submission.definition_id, "test_pd_id");
assert_eq!(presentation_result.presentation_submission.descriptor_map.len(), 1);
assert_eq!(presentation_result.presentation_submission.descriptor_map[0].id, "test_input_1");
assert_eq!(presentation_result.presentation_submission.descriptor_map[0].format, "jwt_vc");
assert_eq!(presentation_result.presentation_submission.descriptor_map[0].path, "$.verifiableCredential[0]");

assert_eq!(presentation_submission.0.definition_id, "test_pd_id");
assert_eq!(presentation_submission.0.descriptor_map.len(), 1);
assert_eq!(presentation_submission.0.descriptor_map[0].id, "test_input_1");
assert_eq!(presentation_submission.0.descriptor_map[0].format, "jwt_vc");
assert_eq!(presentation_submission.0.descriptor_map[0].path, "$.verifiableCredential[0]");
}

#[test]
Expand Down Expand Up @@ -502,7 +511,7 @@ mod tests {

let vc_jwts = vec![signed_vcjwt_1.clone()];

let (presentation_submission, selected_vcs) = presentation_definition
let presentation_result = presentation_definition
.create_presentation_from_credentials(&vc_jwts)
.unwrap();

Expand All @@ -512,15 +521,15 @@ mod tests {
let mut additional_data = HashMap::new();
additional_data.insert(
"presentation_submission".to_string(),
json!(presentation_submission),
json!(presentation_result.presentation_submission),
);

let vp_create_options = VerifiablePresentationCreateOptions {
additional_data: Some(additional_data),
..Default::default()
};

let vp = VerifiablePresentation::create(holder_uri.clone(), selected_vcs, Some(vp_create_options))
let vp = VerifiablePresentation::create(holder_uri.clone(), presentation_result.matched_vc_jwts, Some(vp_create_options))
.expect("Failed to create Verifiable Presentation");

let vp_jwt = vp.sign(&holder.clone(), None).unwrap();
Expand All @@ -544,7 +553,7 @@ mod tests {
// Check if the decoded presentation_submission is equal to the original one
assert_eq!(
decoded_presentation_submission,
Some(&json!(presentation_submission)),
Some(&json!(presentation_result.presentation_submission)),
"The presentation_submission in additional_data does not match the expected value"
);
}
Expand Down Expand Up @@ -655,7 +664,7 @@ mod tests {
];

// Unwrap the result of `create_presentation_from_credentials`
let (presentation_submission, selected_vcs) = presentation_definition
let presentation_result= presentation_definition
.create_presentation_from_credentials(&vc_jwts)
.unwrap();

Expand All @@ -665,7 +674,7 @@ mod tests {
let mut additional_data = HashMap::new();
additional_data.insert(
"presentation_submission".to_string(),
json!(presentation_submission),
json!(presentation_result.presentation_submission),
);

let vp_create_options = VerifiablePresentationCreateOptions {
Expand All @@ -674,7 +683,7 @@ mod tests {
};

// Use the `selected_vcs` directly
let vp = VerifiablePresentation::create(holder_uri.clone(), selected_vcs, Some(vp_create_options))
let vp = VerifiablePresentation::create(holder_uri.clone(), presentation_result.matched_vc_jwts, Some(vp_create_options))
.expect("Failed to create Verifiable Presentation");

let vp_jwt = vp.sign(&holder.clone(), None).unwrap();
Expand All @@ -701,7 +710,7 @@ mod tests {
// Check if the decoded presentation_submission is equal to the original one
assert_eq!(
decoded_presentation_submission,
Some(&json!(presentation_submission)),
Some(&json!(presentation_result.presentation_submission)),
"The presentation_submission in additional_data does not match the expected value"
);
}
Expand Down
10 changes: 9 additions & 1 deletion docs/API_DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ CLASS PresentationDefinition
PUBLIC DATA input_descriptors: []InputDescriptor
METHOD select_credentials(vc_jwts: []string): []string
METHOD create_presentation_from_credentials(vc_jwts: []string): (PresentationSubmission, []string)
METHOD create_presentation_from_credentials(vc_jwts: []string): PresentationResult
```

### `InputDescriptor`
Expand Down Expand Up @@ -249,6 +249,14 @@ CLASS Filter
PUBLIC DATA contains: Filter?
```

### `PresentationResult`

```pseudocode!
CLASS PresentationResult
PUBLIC DATA presentation_submission: PresentationSubmission
PUBLIC DATA matched_vc_jwts: []string
```

### `PresentationSubmission`

```pseudocode!
Expand Down

0 comments on commit 99dd95d

Please sign in to comment.