Skip to content

Commit

Permalink
did:jwk WASM bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Aug 30, 2024
1 parent 98d9520 commit ace7120
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
105 changes: 105 additions & 0 deletions bindings/wasm/src/did/did_jwk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity_iota::did::DIDJwk;
use identity_iota::did::DID as _;
use wasm_bindgen::prelude::*;

use super::wasm_core_did::get_core_did_clone;
use super::IToCoreDID;
use super::WasmCoreDID;
use crate::error::Result;
use crate::error::WasmResult;
use crate::jose::WasmJwk;

/// `did:jwk` DID.
#[wasm_bindgen(js_name = DIDJwk)]
pub struct WasmDIDJwk(pub(crate) DIDJwk);

#[wasm_bindgen(js_class = DIDJwk)]
impl WasmDIDJwk {
#[wasm_bindgen(constructor)]
/// Creates a new {@link DIDJwk} from a {@link CoreDID}.
///
/// ### Errors
/// Throws an error if the given did is not a valid `did:jwk` DID.
pub fn new(did: IToCoreDID) -> Result<WasmDIDJwk> {
let did = get_core_did_clone(&did).0;
DIDJwk::try_from(did).wasm_result().map(Self)
}
/// Parses a {@link DIDJwk} from the given `input`.
///
/// ### Errors
///
/// Throws an error if the input is not a valid {@link DIDJwk}.
#[wasm_bindgen]
pub fn parse(input: &str) -> Result<WasmDIDJwk> {
DIDJwk::parse(input).wasm_result().map(Self)
}

/// Returns the JSON WEB KEY (JWK) encoded inside this `did:jwk`.
#[wasm_bindgen]
pub fn jwk(&self) -> WasmJwk {
self.0.jwk().into()
}

// ===========================================================================
// DID trait
// ===========================================================================

/// Returns the {@link CoreDID} scheme.
///
/// E.g.
/// - `"did:example:12345678" -> "did"`
/// - `"did:iota:smr:12345678" -> "did"`
#[wasm_bindgen]
pub fn scheme(&self) -> String {
self.0.scheme().to_owned()
}

/// Returns the {@link CoreDID} authority: the method name and method-id.
///
/// E.g.
/// - `"did:example:12345678" -> "example:12345678"`
/// - `"did:iota:smr:12345678" -> "iota:smr:12345678"`
#[wasm_bindgen]
pub fn authority(&self) -> String {
self.0.authority().to_owned()
}

/// Returns the {@link CoreDID} method name.
///
/// E.g.
/// - `"did:example:12345678" -> "example"`
/// - `"did:iota:smr:12345678" -> "iota"`
#[wasm_bindgen]
pub fn method(&self) -> String {
self.0.method().to_owned()
}

/// Returns the {@link CoreDID} method-specific ID.
///
/// E.g.
/// - `"did:example:12345678" -> "12345678"`
/// - `"did:iota:smr:12345678" -> "smr:12345678"`
#[wasm_bindgen(js_name = methodId)]
pub fn method_id(&self) -> String {
self.0.method_id().to_owned()
}

/// Returns the {@link CoreDID} as a string.
#[allow(clippy::inherent_to_string)]
#[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String {
self.0.to_string()
}

// Only intended to be called internally.
#[wasm_bindgen(js_name = toCoreDid, skip_typescript)]
pub fn to_core_did(&self) -> WasmCoreDID {
WasmCoreDID(self.0.clone().into())
}
}

impl_wasm_json!(WasmDIDJwk, DIDJwk);
impl_wasm_clone!(WasmDIDJwk, DIDJwk);
2 changes: 2 additions & 0 deletions bindings/wasm/src/did/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

mod did_jwk;
mod jws_verification_options;
mod service;
mod wasm_core_did;
Expand All @@ -19,5 +20,6 @@ pub use self::wasm_core_document::PromiseJws;
pub use self::wasm_core_document::PromiseJwt;
pub use self::wasm_core_document::WasmCoreDocument;
pub use self::wasm_did_url::WasmDIDUrl;
pub use did_jwk::*;

pub use self::jws_verification_options::*;
7 changes: 7 additions & 0 deletions bindings/wasm/src/did/wasm_core_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::credential::WasmJwt;
use crate::credential::WasmPresentation;
use crate::did::service::WasmService;
use crate::did::wasm_did_url::WasmDIDUrl;
use crate::did::WasmDIDJwk;
use crate::error::Result;
use crate::error::WasmResult;
use crate::jose::WasmDecodedJws;
Expand Down Expand Up @@ -765,6 +766,12 @@ impl WasmCoreDocument {
});
Ok(promise.unchecked_into())
}

/// Creates a {@link CoreDocument} from the given {@link DIDJwk}.
#[wasm_bindgen(js_name = expandDIDJwk)]
pub fn expand_did_jwk(did: WasmDIDJwk) -> Result<WasmCoreDocument> {
CoreDocument::expand_did_jwk(did.0).wasm_result().map(Self::from)
}
}

#[wasm_bindgen]
Expand Down

0 comments on commit ace7120

Please sign in to comment.