Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Jan 17, 2024
1 parent 311bbb5 commit 77f56fe
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 167 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2023 IOTA Stiftung
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity_iota::core::Object;
Expand Down Expand Up @@ -170,7 +170,8 @@ impl WasmJwtCredentialValidator {
let status_check: StatusCheck = statusCheck.into();
JwtCredentialValidatorUtils::check_status(&credential.0, &trusted_issuers, status_check).wasm_result()
}
/// Checks wheter the credential status has been revoked using `StatusList2021`

/// Checks wheter the credential status has been revoked using `StatusList2021`.
#[wasm_bindgen(js_name = checkStatusWithStatusList2021)]
pub fn check_status_with_status_list_2021(
credential: &WasmCredential,
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/credential/revocation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2023 IOTA Stiftung
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

pub mod status_list_2021;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2023 IOTA Stiftung
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::ops::Deref;
Expand All @@ -21,7 +21,7 @@ use crate::error::WasmResult;
use super::WasmStatusList2021;
use super::WasmStatusList2021Entry;

/// Purpose of a {@link StatusList2021}
/// Purpose of a {@link StatusList2021}.
#[wasm_bindgen(js_name = StatusPurpose)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum WasmStatusPurpose {
Expand All @@ -47,7 +47,7 @@ impl From<WasmStatusPurpose> for StatusPurpose {
}
}

/// A parsed [StatusList2021Credential](https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021credential)
/// A parsed [StatusList2021Credential](https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021credential).
#[wasm_bindgen(js_name = "StatusList2021Credential", inspectable)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(from = "StatusList2021Credential", into = "StatusList2021Credential")]
Expand Down Expand Up @@ -80,17 +80,19 @@ impl From<WasmStatusList2021Credential> for StatusList2021Credential {

#[wasm_bindgen(js_class = StatusList2021Credential)]
impl WasmStatusList2021Credential {
/// Creates a new {@link StatusList2021Credential}
/// Creates a new {@link StatusList2021Credential}.
#[wasm_bindgen(constructor)]
pub fn new(credential: WasmCredential) -> Result<WasmStatusList2021Credential> {
StatusList2021Credential::try_from(credential.0)
.map(Into::into)
.map_err(|e| JsValue::from(JsError::new(&e.to_string())))
}

#[wasm_bindgen]
pub fn id(&self) -> String {
self.inner.id.as_deref().map(ToString::to_string).unwrap()
}

/// Sets the given credential's status using the `index`-th entry of this status list.
/// Returns the created `credentialStatus`.
#[wasm_bindgen(js_name = "setCredentialStatus")]
Expand All @@ -106,20 +108,23 @@ impl WasmStatusList2021Credential {

Ok(WasmStatusList2021Entry(entry))
}
/// Returns the {@link StatusPurpose} of this {@link StatusList2021Credential}

/// Returns the {@link StatusPurpose} of this {@link StatusList2021Credential}.
#[wasm_bindgen]
pub fn purpose(&self) -> WasmStatusPurpose {
self.inner.purpose().into()
}
/// Returns the state of the `index`-th entry, if any

/// Returns the state of the `index`-th entry, if any.
#[wasm_bindgen]
pub fn entry(&self, index: usize) -> Result<Option<bool>> {
self
.inner
.entry(index)
.map_err(|e| JsValue::from(JsError::new(&e.to_string())))
}
/// Sets the `index`-th entry to `state`

/// Sets the `index`-th entry to `state`.
#[wasm_bindgen(js_name = "setEntry")]
pub fn set_entry(&mut self, index: usize, state: bool) -> Result<()> {
self
Expand All @@ -130,82 +135,93 @@ impl WasmStatusList2021Credential {
self.wasm_credential = WasmCredential(self.inner.clone().into_inner());
Ok(())
}

#[wasm_bindgen(js_name = "clone")]
pub fn wasm_clone(&self) -> WasmStatusList2021Credential {
self.clone()
}

#[wasm_bindgen(js_name = "fromJSON")]
pub fn from_json(json: JsValue) -> Result<WasmStatusList2021Credential> {
use crate::error::WasmResult;
json.into_serde::<WasmStatusList2021Credential>().wasm_result()
}

#[wasm_bindgen(js_name = "toJSON")]
pub fn to_json(&self) -> Result<JsValue> {
use crate::error::WasmResult;
JsValue::from_serde(self).wasm_result()
}
}

/// Builder type to construct valid {@link StatusList2021Credential} istances
/// Builder type to construct valid {@link StatusList2021Credential} istances.
#[wasm_bindgen(js_name = StatusList2021CredentialBuilder)]
pub struct WasmStatusList2021CredentialBuilder(StatusList2021CredentialBuilder);

#[wasm_bindgen(js_class = StatusList2021CredentialBuilder)]
impl WasmStatusList2021CredentialBuilder {
/// Creates a new {@link StatusList2021CredentialBuilder}
/// Creates a new {@link StatusList2021CredentialBuilder}.
#[wasm_bindgen(constructor)]
pub fn new(status_list: Option<WasmStatusList2021>) -> WasmStatusList2021CredentialBuilder {
Self(StatusList2021CredentialBuilder::new(status_list.unwrap_or_default().0))
}
/// Sets the purpose of the {@link StatusList2021Credential} that is being created

/// Sets the purpose of the {@link StatusList2021Credential} that is being created.
#[wasm_bindgen]
pub fn purpose(mut self, purpose: WasmStatusPurpose) -> WasmStatusList2021CredentialBuilder {
self.0 = self.0.purpose(purpose.into());
self
}
/// Sets `credentialSubject.id`

/// Sets `credentialSubject.id`.
#[wasm_bindgen(js_name = "subjectId")]
pub fn subject_id(mut self, id: String) -> Result<WasmStatusList2021CredentialBuilder> {
let id = Url::parse(id).wasm_result()?;
self.0 = self.0.subject_id(id);

Ok(self)
}
/// Sets the expiration date of the credential

/// Sets the expiration date of the credential.
#[wasm_bindgen(js_name = "expirationDate")]
pub fn expiration_date(mut self, time: WasmTimestamp) -> WasmStatusList2021CredentialBuilder {
self.0 = self.0.expiration_date(time.0);
self
}
/// Sets the issuer of the credential

/// Sets the issuer of the credential.
#[wasm_bindgen]
pub fn issuer(mut self, issuer: String) -> Result<WasmStatusList2021CredentialBuilder> {
let issuer = Url::parse(issuer).wasm_result()?;
self.0 = self.0.issuer(Issuer::Url(issuer));

Ok(self)
}
/// Sets the context of the credential

/// Sets the context of the credential.
#[wasm_bindgen]
pub fn context(mut self, context: String) -> Result<WasmStatusList2021CredentialBuilder> {
let ctx = Context::Url(Url::parse(context).wasm_result()?);
self.0 = self.0.context(ctx);

Ok(self)
}
/// Adds a credential type

/// Adds a credential type.
#[wasm_bindgen(js_name = "type")]
pub fn r#type(mut self, t: String) -> WasmStatusList2021CredentialBuilder {
self.0 = self.0.add_type(t);
self
}
/// Adds a credential's proof

/// Adds a credential's proof.
#[wasm_bindgen]
pub fn proof(mut self, proof: WasmProof) -> WasmStatusList2021CredentialBuilder {
self.0 = self.0.proof(proof.0);
self
}
/// Attempts to build a valid {@link StatusList2021Credential} with the previously provided data

/// Attempts to build a valid {@link StatusList2021Credential} with the previously provided data.
#[wasm_bindgen]
pub fn build(self) -> Result<WasmStatusList2021Credential> {
let credential = self.0.build().wasm_result()?;
Expand Down
18 changes: 11 additions & 7 deletions bindings/wasm/src/credential/revocation/status_list_2021/entry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2023 IOTA Stiftung
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use super::WasmStatusPurpose;
Expand All @@ -7,34 +7,38 @@ use identity_iota::core::Url;
use identity_iota::credential::status_list_2021::StatusList2021Entry;
use wasm_bindgen::prelude::*;

/// [StatusList2021Entry](https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021entry) implementation
/// [StatusList2021Entry](https://www.w3.org/TR/2023/WD-vc-status-list-20230427/#statuslist2021entry) implementation.
#[wasm_bindgen(js_name = StatusList2021Entry, inspectable)]
pub struct WasmStatusList2021Entry(pub(crate) StatusList2021Entry);

#[wasm_bindgen(js_class = StatusList2021Entry)]
impl WasmStatusList2021Entry {
/// Creates a new {@link StatusList2021Entry}
/// Creates a new {@link StatusList2021Entry}.
#[wasm_bindgen(constructor)]
pub fn new(status_list: &str, purpose: WasmStatusPurpose, index: usize) -> Result<WasmStatusList2021Entry> {
let status_list = Url::parse(status_list).map_err(|e| JsValue::from(JsError::new(&e.to_string())))?;
Ok(Self(StatusList2021Entry::new(status_list, purpose.into(), index)))
}
/// Returns this `credentialStatus`'s `id`

/// Returns this `credentialStatus`'s `id`.
#[wasm_bindgen]
pub fn id(&self) -> String {
self.0.id().to_string()
}
/// Returns the purpose of this entry

/// Returns the purpose of this entry.
#[wasm_bindgen]
pub fn purpose(&self) -> WasmStatusPurpose {
self.0.purpose().into()
}
/// Returns the index of this entry

/// Returns the index of this entry.
#[wasm_bindgen]
pub fn index(&self) -> usize {
self.0.index()
}
/// Returns the referenced {@link StatusList2021Credential}'s url

/// Returns the referenced {@link StatusList2021Credential}'s url.
#[wasm_bindgen]
pub fn credential(&self) -> String {
self.0.credential().to_string()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2020-2023 IOTA Stiftung
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::error::Result;
use identity_iota::credential::status_list_2021::StatusList2021;
use wasm_bindgen::prelude::*;

/// StatusList2021 data structure as described in [W3C's VC status list 2021](https://www.w3.org/TR/2023/WD-vc-status-list-20230427/)
/// StatusList2021 data structure as described in [W3C's VC status list 2021](https://www.w3.org/TR/2023/WD-vc-status-list-20230427/).
#[wasm_bindgen(js_name = StatusList2021, inspectable)]
#[derive(Default, Debug)]
pub struct WasmStatusList2021(pub(crate) StatusList2021);
Expand All @@ -14,7 +14,7 @@ impl_wasm_clone!(WasmStatusList2021, StatusList2021);

#[wasm_bindgen(js_class = StatusList2021)]
impl WasmStatusList2021 {
/// Creates a new {@link StatusList2021} of `size` entries
/// Creates a new {@link StatusList2021} of `size` entries.
#[wasm_bindgen(constructor)]
pub fn new(size: Option<usize>) -> Self {
Self(match size {
Expand All @@ -23,33 +23,34 @@ impl WasmStatusList2021 {
})
}

/// Returns the number of entries in this {@link StatusList2021}
/// Returns the number of entries in this {@link StatusList2021}.
#[wasm_bindgen]
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.0.len()
}

/// Returns whether the entry at `index` is set
/// Returns whether the entry at `index` is set.
#[wasm_bindgen]
pub fn get(&self, index: usize) -> Option<bool> {
self.0.get(index)
}

/// Sets the value of the `index`-th entry
/// Sets the value of the `index`-th entry.
#[wasm_bindgen]
pub fn set(&mut self, index: usize, value: bool) {
self.0.set(index, value)
}

/// Encodes this {@link StatusList2021} into its compressed
/// base64 string representation
/// base64 string representation.
#[wasm_bindgen(js_name = "intoEncodedStr")]
pub fn into_encoded_str(self) -> String {
self.0.into_encoded_str()
}

#[wasm_bindgen(js_name = "fromEncodedStr")]
/// Attempts to decode a {@link StatusList2021} from a string.
pub fn from_encoded_str(s: &str) -> Result<WasmStatusList2021> {
StatusList2021::try_from_encoded_str(s)
.map(Self)
Expand Down
12 changes: 1 addition & 11 deletions examples/1_advanced/7_status_list_2021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use identity_iota::core::Timestamp;
use identity_iota::core::Url;
use identity_iota::credential::status_list_2021::StatusList2021;
use identity_iota::credential::status_list_2021::StatusList2021CredentialBuilder;
use identity_iota::credential::status_list_2021::StatusList2021Entry;
use identity_iota::credential::status_list_2021::StatusPurpose;
use identity_iota::credential::Credential;
use identity_iota::credential::Issuer;
use identity_iota::credential::JwtCredentialValidatorUtils;
Expand Down Expand Up @@ -50,15 +48,7 @@ async fn main() -> anyhow::Result<()> {
// "statusListIndex": "420",
// "statusListCredential": "https://example.com/credentials/status"
// }
let revocation_entry = StatusList2021Entry::new(
status_list_credential.id.clone().unwrap(),
StatusPurpose::Revocation,
420,
);
credential.credential_status = Some(revocation_entry.into());

// To revoke this credential we set the status of the 420th entry
status_list_credential.update_status_list(|status_list| status_list.set(420, true))?;
status_list_credential.set_credential_status(&mut credential, 420, true)?;

// The credential has now been revoked, verifying this credential will now fail
let validation = JwtCredentialValidatorUtils::check_status_with_status_list_2021(
Expand Down
5 changes: 3 additions & 2 deletions identity_credential/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ once_cell = { version = "1.18", default-features = false, features = ["std"] }
reqwest = { version = "0.11", default-features = false, features = ["default-tls", "json", "stream"], optional = true }
roaring = { version = "0.10", default-features = false, optional = true }
serde.workspace = true
serde-aux = { version = "4.3.1", default-features = false }
serde-aux = { version = "4.3.1", default-features = false, optional = true }
serde_repr = { version = "0.1", default-features = false, optional = true }
strum.workspace = true
thiserror.workspace = true
Expand All @@ -37,6 +37,7 @@ iota-crypto = { version = "0.23", default-features = false, features = ["ed25519
proptest = { version = "1.0.0", default-features = false, features = ["std"] }
serde_json.workspace = true
tokio = { version = "1.29.0", default-features = false, features = ["rt-multi-thread", "macros"] }
anyhow = "1.0.62"

[package.metadata.docs.rs]
# To build locally:
Expand All @@ -48,7 +49,7 @@ rustdoc-args = ["--cfg", "docsrs"]
default = ["revocation-bitmap", "validator", "credential", "presentation", "domain-linkage-fetch"]
credential = []
presentation = ["credential"]
revocation-bitmap = ["dep:dataurl", "dep:flate2", "dep:roaring"]
revocation-bitmap = ["dep:dataurl", "dep:flate2", "dep:roaring", "dep:serde-aux"]
validator = ["dep:itertools", "dep:serde_repr", "credential", "presentation"]
domain-linkage = ["validator"]
domain-linkage-fetch = ["domain-linkage", "dep:reqwest", "dep:futures"]
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl TryFrom<Status> for RevocationBitmapStatus {
Status::StatusList2021(s) => Err(Self::Error::InvalidStatus(format!(
"expected type '{}', got '{}'",
Self::TYPE,
s.r#type()
s.type_()
))),
Status::Other(s) => s.try_into(),
}
Expand Down
Loading

0 comments on commit 77f56fe

Please sign in to comment.