identity_credential/sd_jwt_vc/
presentation.rs

1// Copyright 2020-2024 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use super::Error;
5use super::Result;
6use super::SdJwtVc;
7use super::SdJwtVcClaims;
8
9use sd_jwt_payload_rework::Disclosure;
10use sd_jwt_payload_rework::Hasher;
11use sd_jwt_payload_rework::KeyBindingJwt;
12use sd_jwt_payload_rework::SdJwtPresentationBuilder;
13
14/// Builder structure to create an SD-JWT VC presentation.
15/// It allows users to conceal claims and attach a key binding JWT.
16#[derive(Debug, Clone)]
17pub struct SdJwtVcPresentationBuilder {
18  vc_claims: SdJwtVcClaims,
19  builder: SdJwtPresentationBuilder,
20}
21
22impl SdJwtVcPresentationBuilder {
23  /// Prepare a presentation for a given [`SdJwtVc`].
24  pub fn new(token: SdJwtVc, hasher: &dyn Hasher) -> Result<Self> {
25    let SdJwtVc {
26      sd_jwt,
27      parsed_claims: vc_claims,
28    } = token;
29    let builder = sd_jwt.into_presentation(hasher).map_err(Error::SdJwt)?;
30
31    Ok(Self { vc_claims, builder })
32  }
33  /// Removes the disclosure for the property at `path`, conceiling it.
34  ///
35  /// ## Notes
36  /// - When concealing a claim more than one disclosure may be removed: the disclosure for the claim itself and the
37  ///   disclosures for any concealable sub-claim.
38  pub fn conceal(mut self, path: &str) -> Result<Self> {
39    self.builder = self.builder.conceal(path).map_err(Error::SdJwt)?;
40    Ok(self)
41  }
42
43  /// Adds a [`KeyBindingJwt`] to this [`SdJwtVc`]'s presentation.
44  pub fn attach_key_binding_jwt(mut self, kb_jwt: KeyBindingJwt) -> Self {
45    self.builder = self.builder.attach_key_binding_jwt(kb_jwt);
46    self
47  }
48
49  /// Returns the resulting [`SdJwtVc`] together with all removed disclosures.
50  pub fn finish(self) -> Result<(SdJwtVc, Vec<Disclosure>)> {
51    let (sd_jwt, disclosures) = self.builder.finish()?;
52    Ok((SdJwtVc::new(sd_jwt, self.vc_claims), disclosures))
53  }
54}