Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sd-jwt-payload dependency #1296

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions bindings/wasm/examples/src/1_advanced/6_sd_jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ export async function sdJwt() {
// Make "locality", "postal_code", "street_address" and the first entry of "nationalities"
// selectively disclosable while keeping other properties in plain text.
let disclosures = [
encoder.conceal(["vc", "credentialSubject", "address", "locality"]),
encoder.conceal(["vc", "credentialSubject", "address", "postal_code"]),
encoder.conceal(["vc", "credentialSubject", "address", "street_address"]),
encoder.concealArrayEntry(["vc", "credentialSubject", "nationalities"], 1),
encoder.conceal("/vc/credentialSubject/address/locality"),
encoder.conceal("/vc/credentialSubject/address/postal_code"),
encoder.conceal("/vc/credentialSubject/address/street_address"),
encoder.conceal("/vc/credentialSubject/nationalities/1"),
];

// Add decoys in the credential top level, nationalities array and address object.
encoder.addDecoys(["vc", "credentialSubject", "nationalities"], 3);
encoder.addDecoys(["vc"], 4);
encoder.addDecoys(["vc", "credentialSubject", "address"], 2);
encoder.addDecoys("/vc/credentialSubject/nationalities", 3);
encoder.addDecoys("/vc", 4);
encoder.addDecoys("/vc/credentialSubject/address", 2);

// Add the `_sd_alg` property.
encoder.addSdAlgProperty();
Expand Down
78 changes: 25 additions & 53 deletions bindings/wasm/src/sd_jwt/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use super::disclosure::WasmDisclosure;
use crate::common::ArrayString;
use crate::common::RecordStringAny;
use crate::error::Result;
use crate::error::WasmResult;
use identity_iota::sd_jwt_payload::SdObjectEncoder;
use identity_iota::sd_jwt_payload::Sha256Hasher;
use js_sys::Array;
use js_sys::JsString;
use serde_json::Value;
use wasm_bindgen::prelude::*;

Expand All @@ -32,54 +29,35 @@ impl WasmSdObjectEncoder {
/// Substitutes a value with the digest of its disclosure.
/// If no salt is provided, the disclosure will be created with a random salt value.
///
/// The value of the key specified in `path` will be concealed. E.g. for path
/// `["claim", "subclaim"]` the value of `claim.subclaim` will be concealed.
/// `path` indicates the pointer to the value that will be concealed using the syntax of
/// [JSON pointer](https://datatracker.ietf.org/doc/html/rfc6901).
///
/// ## Error
/// `InvalidPath` if path is invalid or the path slice is empty.
/// `DataTypeMismatch` if existing SD format is invalid.
/// For the following object:
///
/// ## Note
/// Use `concealArrayEntry` for values in arrays.
/// ```
/// {
/// "id": "did:value",
/// "claim1": {
/// "abc": true
/// },
/// "claim2": ["val_1", "val_2"]
/// }
/// ```
///
/// Path "/id" conceals `"id": "did:value"`
/// Path "/claim1/abc" conceals `"abc": true`
/// Path "/claim2/0" conceals `val_1`
/// ```
///
/// ## Errors
/// * `InvalidPath` if pointer is invalid.
/// * `DataTypeMismatch` if existing SD format is invalid.
#[wasm_bindgen(js_name = conceal)]
pub fn conceal(&mut self, path: ArrayString, salt: Option<String>) -> Result<WasmDisclosure> {
let path: Vec<String> = path
.dyn_into::<Array>()?
.iter()
.map(|item| item.dyn_into::<JsString>().map(String::from))
.collect::<Result<Vec<String>>>()?;
let path: Vec<&str> = path.iter().map(|s| &**s).collect();
pub fn conceal(&mut self, path: String, salt: Option<String>) -> Result<WasmDisclosure> {
let disclosure = self.0.conceal(&path, salt).wasm_result()?;
Ok(WasmDisclosure(disclosure))
}

/// Substitutes a value within an array with the digest of its disclosure.
/// If no salt is provided, the disclosure will be created with random salt value.
///
/// `path` is used to specify the array in the object, while `element_index` specifies
/// the index of the element to be concealed (index start at 0).
///
/// ## Error
/// `InvalidPath` if path is invalid or the path slice is empty.
/// `DataTypeMismatch` if existing SD format is invalid.
/// `IndexOutofBounds` if `element_index` is out of bounds.
#[wasm_bindgen(js_name = concealArrayEntry)]
pub fn conceal_array_entry(
&mut self,
path: ArrayString,
element_index: usize,
salt: Option<String>,
) -> Result<WasmDisclosure> {
let path: Vec<String> = path
.dyn_into::<Array>()?
.iter()
.map(|item| item.dyn_into::<JsString>().map(String::from))
.collect::<Result<Vec<String>>>()?;
let path: Vec<&str> = path.iter().map(|s| &**s).collect();
let disclosure = self.0.conceal_array_entry(&path, element_index, salt).wasm_result()?;
Ok(WasmDisclosure(disclosure))
}

/// Adds the `_sd_alg` property to the top level of the object, with
/// its value set to "sha-256".
#[wasm_bindgen(js_name = addSdAlgProperty)]
Expand All @@ -103,7 +81,7 @@ impl WasmSdObjectEncoder {
#[wasm_bindgen(js_name = encodeToObject)]
pub fn encode_to_object(&self) -> Result<RecordStringAny> {
Ok(
JsValue::from_serde(&self.0.object())
JsValue::from_serde(&self.0.object().wasm_result()?)
.wasm_result()?
.unchecked_into::<RecordStringAny>(),
)
Expand All @@ -112,19 +90,13 @@ impl WasmSdObjectEncoder {
/// Returns the modified object.
#[wasm_bindgen(js_name = toJSON)]
pub fn to_json(&self) -> Result<JsValue> {
JsValue::from_serde(&self.0.object()).wasm_result()
JsValue::from_serde(&self.0.object().wasm_result()?).wasm_result()
}

/// Adds a decoy digest to the specified path.
/// If path is an empty slice, decoys will be added to the top level.
#[wasm_bindgen(js_name = addDecoys)]
pub fn add_decoys(&mut self, path: ArrayString, number_of_decoys: usize) -> Result<()> {
let path: Vec<String> = path
.dyn_into::<Array>()?
.iter()
.map(|item| item.dyn_into::<JsString>().map(String::from))
.collect::<Result<Vec<String>>>()?;
let path: Vec<&str> = path.iter().map(|s| &**s).collect();
pub fn add_decoys(&mut self, path: String, number_of_decoys: usize) -> Result<()> {
self.0.add_decoys(&path, number_of_decoys).wasm_result()?;
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions examples/1_advanced/7_sd_jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ async fn main() -> anyhow::Result<()> {
// Make "locality", "postal_code" and "street_address" selectively disclosable while keeping
// other properties in plain text.
let disclosures = vec![
encoder.conceal(&["vc", "credentialSubject", "address", "locality"], None)?,
encoder.conceal(&["vc", "credentialSubject", "address", "postal_code"], None)?,
encoder.conceal(&["vc", "credentialSubject", "address", "street_address"], None)?,
encoder.conceal("/vc/credentialSubject/address/locality", None)?,
encoder.conceal("/vc/credentialSubject/address/postal_code", None)?,
encoder.conceal("/vc/credentialSubject/address/street_address", None)?,
];

// Add the `_sd_alg` property.
Expand Down
4 changes: 0 additions & 4 deletions examples/1_advanced/8_status_list_2021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ use examples::random_stronghold_path;
use examples::MemStorage;
use examples::API_ENDPOINT;
use identity_eddsa_verifier::EdDSAJwsVerifier;

use identity_iota::core::FromJson;
use identity_iota::core::Object;

use identity_iota::core::ToJson;
use identity_iota::core::Url;
use identity_iota::credential::status_list_2021::StatusList2021;
use identity_iota::credential::status_list_2021::StatusList2021Credential;
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::CredentialBuilder;

use identity_iota::credential::FailFast;
use identity_iota::credential::Issuer;
use identity_iota::credential::Jwt;
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ identity_stronghold = { path = "../identity_stronghold", default-features = fals
iota-sdk = { version = "1.0", default-features = false, features = ["tls", "client", "stronghold"] }
primitive-types = "0.12.1"
rand = "0.8.5"
sd-jwt-payload = { version = "0.1.2", default-features = false, features = ["sha"] }
sd-jwt-payload = { version = "0.2.0", default-features = false, features = ["sha"] }
serde_json = { version = "1.0", default-features = false }
tokio = { version = "1.29", default-features = false, features = ["rt"] }

Expand Down
2 changes: 1 addition & 1 deletion identity_credential/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ itertools = { version = "0.11", default-features = false, features = ["use_std"]
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 }
sd-jwt-payload = { version = "0.1.2", default-features = false, features = ["sha"], optional = true }
sd-jwt-payload = { version = "0.2.0", default-features = false, features = ["sha"], optional = true }
serde.workspace = true
serde-aux = { version = "4.3.1", default-features = false, optional = true }
serde_json.workspace = true
Expand Down
8 changes: 2 additions & 6 deletions identity_storage/src/storage/tests/kb_jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ async fn setup_test() -> (Setup<IotaDocument, IotaDocument>, Credential, SdJwt)

let mut encoder = SdObjectEncoder::new(&payload).unwrap();
let disclosures = vec![
encoder
.conceal(&["vc", "credentialSubject", "degree", "type"], None)
.unwrap(),
encoder
.conceal(&["vc", "credentialSubject", "degree", "name"], None)
.unwrap(),
encoder.conceal("/vc/credentialSubject/degree/type", None).unwrap(),
encoder.conceal("/vc/credentialSubject/degree/name", None).unwrap(),
];
encoder.add_sd_alg_property();
let encoded_payload = encoder.try_to_string().unwrap();
Expand Down
Loading