Skip to content

Commit

Permalink
Rename JwtPresentation to Presentation (#1200)
Browse files Browse the repository at this point in the history
* rename `JwtPresentation` in Rust

* rename in wasm

* fix docs
  • Loading branch information
abdulmth authored Jul 7, 2023
1 parent 09aaa80 commit c1f6171
Show file tree
Hide file tree
Showing 27 changed files with 431 additions and 337 deletions.
396 changes: 226 additions & 170 deletions bindings/wasm/docs/api-reference.md

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions bindings/wasm/examples/src/0_basic/6_create_vp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import {
Jwt,
JwtCredentialValidationOptions,
JwtCredentialValidator,
JwtPresentation,
JwtPresentationOptions,
JwtPresentationValidationOptions,
JwtPresentationValidator,
KeyIdMemStore,
Presentation,
Resolver,
Storage,
SubjectHolderRelationship,
Expand Down Expand Up @@ -130,7 +130,7 @@ export async function createVP() {
// ===========================================================================

// Create a Verifiable Presentation from the Credential
const unsignedVp = new JwtPresentation({
const unsignedVp = new Presentation({
holder: aliceDocument.id(),
verifiableCredential: [credentialJwt],
});
Expand Down Expand Up @@ -188,13 +188,16 @@ export async function createVP() {
// Validate the credentials in the presentation.
let credentialValidator = new JwtCredentialValidator();
let validationOptions = new JwtCredentialValidationOptions({
subjectHolderRelationship: [presentationHolderDID.toString(), SubjectHolderRelationship.AlwaysSubject],
subjectHolderRelationship: [
presentationHolderDID.toString(),
SubjectHolderRelationship.AlwaysSubject,
],
});

let jwtCredentials: Jwt[] = decodedPresentation
.presentation()
.verifiableCredential()
.map(credential => {
.map((credential) => {
const jwt = credential.tryIntoJwt();
if (!jwt) {
throw new Error("expected a JWT credential");
Expand Down
54 changes: 42 additions & 12 deletions bindings/wasm/examples/src/0_basic/7_revoke_vc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,22 @@ export async function revokeVC() {
};

// Create an identity for the issuer with one verification method `key-1`.
const issuerStorage: Storage = new Storage(new JwkMemStore(), new KeyIdMemStore());
const issuerStorage: Storage = new Storage(
new JwkMemStore(),
new KeyIdMemStore(),
);
let { document: issuerDocument, fragment: issuerFragment } = await createDid(
client,
issuerSecretManager,
issuerStorage,
);

// Create an identity for the holder, in this case also the subject.
const aliceStorage: Storage = new Storage(new JwkMemStore(), new KeyIdMemStore());
let { document: aliceDocument, fragment: aliceFragment } = await createDid(
const aliceStorage: Storage = new Storage(
new JwkMemStore(),
new KeyIdMemStore(),
);
let { document: aliceDocument } = await createDid(
client,
issuerSecretManager,
aliceStorage,
Expand All @@ -74,15 +80,23 @@ export async function revokeVC() {
issuerDocument.insertService(service);

// Resolve the latest output and update it with the given document.
let aliasOutput: IAliasOutput = await didClient.updateDidOutput(issuerDocument);
let aliasOutput: IAliasOutput = await didClient.updateDidOutput(
issuerDocument,
);

// Because the size of the DID document increased, we have to increase the allocated storage deposit.
// This increases the deposit amount to the new minimum.
let rentStructure: IRent = await didClient.getRentStructure();
aliasOutput.amount = TransactionHelper.getStorageDeposit(aliasOutput, rentStructure).toString();
aliasOutput.amount = TransactionHelper.getStorageDeposit(
aliasOutput,
rentStructure,
).toString();

// Publish the document.
issuerDocument = await didClient.publishDidOutput(issuerSecretManager, aliasOutput);
issuerDocument = await didClient.publishDidOutput(
issuerSecretManager,
aliasOutput,
);

// Create a credential subject indicating the degree earned by Alice, linked to their DID.
const subject = {
Expand Down Expand Up @@ -137,8 +151,14 @@ export async function revokeVC() {
// Publish the changes.
aliasOutput = await didClient.updateDidOutput(issuerDocument);
rentStructure = await didClient.getRentStructure();
aliasOutput.amount = TransactionHelper.getStorageDeposit(aliasOutput, rentStructure).toString();
const update2: IotaDocument = await didClient.publishDidOutput(issuerSecretManager, aliasOutput);
aliasOutput.amount = TransactionHelper.getStorageDeposit(
aliasOutput,
rentStructure,
).toString();
const update2: IotaDocument = await didClient.publishDidOutput(
issuerSecretManager,
aliasOutput,
);

// Credential verification now fails.
try {
Expand All @@ -159,20 +179,30 @@ export async function revokeVC() {

// By removing the verification method, that signed the credential, from the issuer's DID document,
// we effectively revoke the credential, as it will no longer be possible to validate the signature.
let originalMethod = issuerDocument.resolveMethod(`#${issuerFragment}`) as VerificationMethod;
let originalMethod = issuerDocument.resolveMethod(
`#${issuerFragment}`,
) as VerificationMethod;
await issuerDocument.purgeMethod(issuerStorage, originalMethod.id());

// Publish the changes.
aliasOutput = await didClient.updateDidOutput(issuerDocument);
rentStructure = await didClient.getRentStructure();
aliasOutput.amount = TransactionHelper.getStorageDeposit(aliasOutput, rentStructure).toString();
issuerDocument = await didClient.publishDidOutput(issuerSecretManager, aliasOutput);
aliasOutput.amount = TransactionHelper.getStorageDeposit(
aliasOutput,
rentStructure,
).toString();
issuerDocument = await didClient.publishDidOutput(
issuerSecretManager,
aliasOutput,
);

// We expect the verifiable credential to be revoked.
const resolver = new Resolver({ client: didClient });
try {
// Resolve the issuer's updated DID Document to ensure the key was revoked successfully.
const resolvedIssuerDoc = await resolver.resolve(issuerDocument.id().toString());
const resolvedIssuerDoc = await resolver.resolve(
issuerDocument.id().toString(),
);
jwtCredentialValidator.validate(
credentialJwt,
resolvedIssuerDoc,
Expand Down
7 changes: 4 additions & 3 deletions bindings/wasm/src/credential/jwt_presentation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

mod jwt_presentation;
mod jwt_presentation_builder;
mod presentation;
mod presentation_builder;

pub use self::jwt_presentation::*;
pub use self::presentation::*;
pub use self::presentation_builder::*;
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,48 @@

use identity_iota::core::Context;
use identity_iota::core::Object;
use identity_iota::credential::JwtPresentation;
use identity_iota::credential::JwtPresentationBuilder;
use identity_iota::credential::Presentation;
use identity_iota::credential::PresentationBuilder;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;

use crate::common::ArrayString;
use crate::common::MapStringAny;
use crate::credential::jwt_presentation::jwt_presentation_builder::IJwtPresentation;
use crate::credential::ArrayContext;
use crate::credential::ArrayPolicy;
use crate::credential::ArrayRefreshService;
use crate::credential::ArrayUnknownCredential;
use crate::credential::IPresentation;
use crate::credential::UnknownCredential;
use crate::credential::WasmUnknownCredentialContainer;
use crate::error::Result;
use crate::error::WasmResult;

#[wasm_bindgen(js_name = JwtPresentation, inspectable)]
pub struct WasmJwtPresentation(pub(crate) JwtPresentation<UnknownCredential>);
#[wasm_bindgen(js_name = Presentation, inspectable)]
pub struct WasmPresentation(pub(crate) Presentation<UnknownCredential>);

#[wasm_bindgen(js_class = JwtPresentation)]
impl WasmJwtPresentation {
#[wasm_bindgen(js_class = Presentation)]
impl WasmPresentation {
/// Returns the base JSON-LD context.
#[wasm_bindgen(js_name = "BaseContext")]
pub fn base_context() -> Result<String> {
match JwtPresentation::<Object>::base_context() {
match Presentation::<Object>::base_context() {
Context::Url(url) => Ok(url.to_string()),
Context::Obj(_) => Err(JsError::new("JwtPresentation.BaseContext should be a single URL").into()),
Context::Obj(_) => Err(JsError::new("Presentation.BaseContext should be a single URL").into()),
}
}

/// Returns the base type.
#[wasm_bindgen(js_name = "BaseType")]
pub fn base_type() -> String {
JwtPresentation::<Object>::base_type().to_owned()
Presentation::<Object>::base_type().to_owned()
}

/// Constructs a new presentation.
#[wasm_bindgen(constructor)]
pub fn new(values: IJwtPresentation) -> Result<WasmJwtPresentation> {
let builder: JwtPresentationBuilder<UnknownCredential, Object> =
JwtPresentationBuilder::<UnknownCredential, Object>::try_from(values)?;
pub fn new(values: IPresentation) -> Result<WasmPresentation> {
let builder: PresentationBuilder<UnknownCredential, Object> =
PresentationBuilder::<UnknownCredential, Object>::try_from(values)?;
builder.build().map(Self).wasm_result()
}

Expand Down Expand Up @@ -139,11 +139,11 @@ impl WasmJwtPresentation {
}
}

impl_wasm_json!(WasmJwtPresentation, JwtPresentation);
impl_wasm_clone!(WasmJwtPresentation, JwtPresentation);
impl_wasm_json!(WasmPresentation, Presentation);
impl_wasm_clone!(WasmPresentation, Presentation);

impl From<JwtPresentation<UnknownCredential>> for WasmJwtPresentation {
fn from(presentation: JwtPresentation<UnknownCredential>) -> WasmJwtPresentation {
impl From<Presentation<UnknownCredential>> for WasmPresentation {
fn from(presentation: Presentation<UnknownCredential>) -> WasmPresentation {
Self(presentation)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ use identity_iota::core::Context;
use identity_iota::core::Object;
use identity_iota::core::OneOrMany;
use identity_iota::core::Url;
use identity_iota::credential::JwtPresentationBuilder;
use identity_iota::credential::Policy;
use identity_iota::credential::PresentationBuilder;
use identity_iota::credential::RefreshService;
use proc_typescript::typescript;
use wasm_bindgen::prelude::*;

use crate::credential::UnknownCredential;
use crate::error::WasmResult;

impl TryFrom<IJwtPresentation> for JwtPresentationBuilder<UnknownCredential> {
impl TryFrom<IPresentation> for PresentationBuilder<UnknownCredential> {
type Error = JsValue;

fn try_from(values: IJwtPresentation) -> std::result::Result<Self, Self::Error> {
let IJwtPresentationHelper {
fn try_from(values: IPresentation) -> std::result::Result<Self, Self::Error> {
let IPresentationHelper {
context,
id,
r#type,
Expand All @@ -27,10 +27,10 @@ impl TryFrom<IJwtPresentation> for JwtPresentationBuilder<UnknownCredential> {
refresh_service,
terms_of_use,
properties,
} = values.into_serde::<IJwtPresentationHelper>().wasm_result()?;
} = values.into_serde::<IPresentationHelper>().wasm_result()?;

let mut builder: JwtPresentationBuilder<UnknownCredential> =
JwtPresentationBuilder::new(Url::parse(holder).wasm_result()?, properties);
let mut builder: PresentationBuilder<UnknownCredential> =
PresentationBuilder::new(Url::parse(holder).wasm_result()?, properties);

if let Some(context) = context {
for value in context.into_vec() {
Expand Down Expand Up @@ -65,15 +65,15 @@ impl TryFrom<IJwtPresentation> for JwtPresentationBuilder<UnknownCredential> {

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "IJwtPresentation")]
pub type IJwtPresentation;
#[wasm_bindgen(typescript_type = "IPresentation")]
pub type IPresentation;
}

/// Fields for constructing a new {@link JwtPresentation}.
/// Fields for constructing a new {@link Presentation}.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
#[typescript(name = "IJwtPresentation", readonly, optional)]
struct IJwtPresentationHelper {
#[typescript(name = "IPresentation", readonly, optional)]
struct IPresentationHelper {
/// The JSON-LD context(s) applicable to the presentation.
#[typescript(type = "string | Record<string, any> | Array<string | Record<string, any>>")]
context: Option<OneOrMany<Context>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use identity_iota::credential::DecodedJwtPresentation;
use wasm_bindgen::prelude::*;

use crate::common::WasmTimestamp;
use crate::credential::jwt_presentation::WasmJwtPresentation;
use crate::credential::jwt_presentation::WasmPresentation;
use crate::credential::UnknownCredential;
use crate::jose::WasmJwsHeader;

Expand All @@ -19,8 +19,8 @@ pub struct WasmDecodedJwtPresentation(pub(crate) DecodedJwtPresentation<UnknownC
#[wasm_bindgen(js_class = DecodedJwtPresentation)]
impl WasmDecodedJwtPresentation {
#[wasm_bindgen]
pub fn presentation(&self) -> WasmJwtPresentation {
WasmJwtPresentation(self.0.presentation.clone())
pub fn presentation(&self) -> WasmPresentation {
WasmPresentation(self.0.presentation.clone())
}

/// Returns a copy of the protected header parsed from the decoded JWS.
Expand All @@ -34,8 +34,8 @@ impl WasmDecodedJwtPresentation {
/// ### Warning
/// This destroys the `DecodedJwtPresentation` object.
#[wasm_bindgen(js_name = intoPresentation)]
pub fn into_presentation(self) -> WasmJwtPresentation {
WasmJwtPresentation(self.0.presentation)
pub fn into_presentation(self) -> WasmPresentation {
WasmPresentation(self.0.presentation)
}

/// The expiration date parsed from the JWT claims.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use super::decoded_jwt_presentation::WasmDecodedJwtPresentation;
use super::options::WasmJwtPresentationValidationOptions;
use crate::common::ImportedDocumentLock;
use crate::credential::jwt_presentation::WasmJwtPresentation;
use crate::credential::jwt_presentation::WasmPresentation;
use crate::credential::WasmJwt;
use crate::did::IToCoreDocument;
use crate::did::WasmCoreDID;
Expand Down Expand Up @@ -73,7 +73,7 @@ impl WasmJwtPresentationValidator {

/// Validates the semantic structure of the `JwtPresentation`.
#[wasm_bindgen(js_name = checkStructure)]
pub fn check_structure(presentation: &WasmJwtPresentation) -> Result<()> {
pub fn check_structure(presentation: &WasmPresentation) -> Result<()> {
JwtPresentationValidator::check_structure(&presentation.0).wasm_result()?;
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions bindings/wasm/src/did/wasm_core_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::credential::UnknownCredential;
use crate::credential::WasmCredential;
use crate::credential::WasmJws;
use crate::credential::WasmJwt;
use crate::credential::WasmJwtPresentation;
use crate::credential::WasmPresentation;
use crate::did::service::WasmService;
use crate::did::wasm_did_url::WasmDIDUrl;
use crate::error::Result;
Expand All @@ -43,8 +43,8 @@ use identity_iota::core::OneOrSet;
use identity_iota::core::OrderedSet;
use identity_iota::core::Url;
use identity_iota::credential::Credential;
use identity_iota::credential::JwtPresentation;
use identity_iota::credential::JwtPresentationOptions;
use identity_iota::credential::Presentation;
use identity_iota::credential::RevocationDocumentExt;
use identity_iota::did::CoreDID;
use identity_iota::did::DIDUrl;
Expand Down Expand Up @@ -706,14 +706,14 @@ impl WasmCoreDocument {
&self,
storage: &WasmStorage,
fragment: String,
presentation: &WasmJwtPresentation,
presentation: &WasmPresentation,
signature_options: &WasmJwsSignatureOptions,
presentation_options: &WasmJwtPresentationOptions,
) -> Result<PromiseJwt> {
let storage_clone: Rc<WasmStorageInner> = storage.0.clone();
let options_clone: JwsSignatureOptions = signature_options.0.clone();
let document_lock_clone: Rc<CoreDocumentLock> = self.0.clone();
let presentation_clone: JwtPresentation<UnknownCredential> = presentation.0.clone();
let presentation_clone: Presentation<UnknownCredential> = presentation.0.clone();
let presentation_options_clone: JwtPresentationOptions = presentation_options.0.clone();
let promise: Promise = future_to_promise(async move {
document_lock_clone
Expand Down
Loading

0 comments on commit c1f6171

Please sign in to comment.