Skip to content

Commit

Permalink
update wasm bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulmth committed Feb 5, 2024
1 parent d4a4a51 commit 7dd5d5a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 66 deletions.
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
79 changes: 26 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,51 +29,33 @@ 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.
#[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();
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.
/// ```
/// {
/// "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`
/// ```
///
/// `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).
/// ## Errors
/// * `InvalidPath` if pointer is invalid.
/// * `DataTypeMismatch` if existing SD format is invalid.
///
/// ## 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()?;
#[wasm_bindgen(js_name = conceal)]
pub fn conceal(&mut self, path: String, salt: Option<String>) -> Result<WasmDisclosure> {
let disclosure = self.0.conceal(&path, salt).wasm_result()?;
Ok(WasmDisclosure(disclosure))
}

Expand All @@ -103,7 +82,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 +91,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
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

0 comments on commit 7dd5d5a

Please sign in to comment.