|
| 1 | +// Copyright 2020-2024 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use identity_iota::did::DIDJwk; |
| 5 | +use identity_iota::did::DID as _; |
| 6 | +use wasm_bindgen::prelude::*; |
| 7 | + |
| 8 | +use super::wasm_core_did::get_core_did_clone; |
| 9 | +use super::IToCoreDID; |
| 10 | +use super::WasmCoreDID; |
| 11 | +use crate::error::Result; |
| 12 | +use crate::error::WasmResult; |
| 13 | +use crate::jose::WasmJwk; |
| 14 | + |
| 15 | +/// `did:jwk` DID. |
| 16 | +#[wasm_bindgen(js_name = DIDJwk)] |
| 17 | +pub struct WasmDIDJwk(pub(crate) DIDJwk); |
| 18 | + |
| 19 | +#[wasm_bindgen(js_class = DIDJwk)] |
| 20 | +impl WasmDIDJwk { |
| 21 | + #[wasm_bindgen(constructor)] |
| 22 | + /// Creates a new {@link DIDJwk} from a {@link CoreDID}. |
| 23 | + /// |
| 24 | + /// ### Errors |
| 25 | + /// Throws an error if the given did is not a valid `did:jwk` DID. |
| 26 | + pub fn new(did: IToCoreDID) -> Result<WasmDIDJwk> { |
| 27 | + let did = get_core_did_clone(&did).0; |
| 28 | + DIDJwk::try_from(did).wasm_result().map(Self) |
| 29 | + } |
| 30 | + /// Parses a {@link DIDJwk} from the given `input`. |
| 31 | + /// |
| 32 | + /// ### Errors |
| 33 | + /// |
| 34 | + /// Throws an error if the input is not a valid {@link DIDJwk}. |
| 35 | + #[wasm_bindgen] |
| 36 | + pub fn parse(input: &str) -> Result<WasmDIDJwk> { |
| 37 | + DIDJwk::parse(input).wasm_result().map(Self) |
| 38 | + } |
| 39 | + |
| 40 | + /// Returns the JSON WEB KEY (JWK) encoded inside this `did:jwk`. |
| 41 | + #[wasm_bindgen] |
| 42 | + pub fn jwk(&self) -> WasmJwk { |
| 43 | + self.0.jwk().into() |
| 44 | + } |
| 45 | + |
| 46 | + // =========================================================================== |
| 47 | + // DID trait |
| 48 | + // =========================================================================== |
| 49 | + |
| 50 | + /// Returns the {@link CoreDID} scheme. |
| 51 | + /// |
| 52 | + /// E.g. |
| 53 | + /// - `"did:example:12345678" -> "did"` |
| 54 | + /// - `"did:iota:smr:12345678" -> "did"` |
| 55 | + #[wasm_bindgen] |
| 56 | + pub fn scheme(&self) -> String { |
| 57 | + self.0.scheme().to_owned() |
| 58 | + } |
| 59 | + |
| 60 | + /// Returns the {@link CoreDID} authority: the method name and method-id. |
| 61 | + /// |
| 62 | + /// E.g. |
| 63 | + /// - `"did:example:12345678" -> "example:12345678"` |
| 64 | + /// - `"did:iota:smr:12345678" -> "iota:smr:12345678"` |
| 65 | + #[wasm_bindgen] |
| 66 | + pub fn authority(&self) -> String { |
| 67 | + self.0.authority().to_owned() |
| 68 | + } |
| 69 | + |
| 70 | + /// Returns the {@link CoreDID} method name. |
| 71 | + /// |
| 72 | + /// E.g. |
| 73 | + /// - `"did:example:12345678" -> "example"` |
| 74 | + /// - `"did:iota:smr:12345678" -> "iota"` |
| 75 | + #[wasm_bindgen] |
| 76 | + pub fn method(&self) -> String { |
| 77 | + self.0.method().to_owned() |
| 78 | + } |
| 79 | + |
| 80 | + /// Returns the {@link CoreDID} method-specific ID. |
| 81 | + /// |
| 82 | + /// E.g. |
| 83 | + /// - `"did:example:12345678" -> "12345678"` |
| 84 | + /// - `"did:iota:smr:12345678" -> "smr:12345678"` |
| 85 | + #[wasm_bindgen(js_name = methodId)] |
| 86 | + pub fn method_id(&self) -> String { |
| 87 | + self.0.method_id().to_owned() |
| 88 | + } |
| 89 | + |
| 90 | + /// Returns the {@link CoreDID} as a string. |
| 91 | + #[allow(clippy::inherent_to_string)] |
| 92 | + #[wasm_bindgen(js_name = toString)] |
| 93 | + pub fn to_string(&self) -> String { |
| 94 | + self.0.to_string() |
| 95 | + } |
| 96 | + |
| 97 | + // Only intended to be called internally. |
| 98 | + #[wasm_bindgen(js_name = toCoreDid, skip_typescript)] |
| 99 | + pub fn to_core_did(&self) -> WasmCoreDID { |
| 100 | + WasmCoreDID(self.0.clone().into()) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl_wasm_json!(WasmDIDJwk, DIDJwk); |
| 105 | +impl_wasm_clone!(WasmDIDJwk, DIDJwk); |
0 commit comments