iop_sdk_wasm/morpheus/
public_kind.rs

1use super::*;
2
3/// Public keys for a given DID kind in the Morpheus subtree in a vault.
4///
5/// @see MorpheusPublic.kind
6#[wasm_bindgen(js_name = MorpheusPublicKind)]
7pub struct JsMorpheusPublicKind {
8    inner: MorpheusPublicKind,
9}
10
11#[wasm_bindgen(js_class = MorpheusPublicKind)]
12impl JsMorpheusPublicKind {
13    /// Accessor for the kind of identifiers generated by this subtree.
14    #[wasm_bindgen(getter)]
15    pub fn kind(&self) -> String {
16        format!("{:?}", self.inner.path())
17    }
18
19    /// Retrieves how many DIDs have already be generated of this kind.
20    #[wasm_bindgen(getter)]
21    pub fn count(&self) -> Result<u32, JsValue> {
22        self.inner.len().map_err_to_js().map(|c| c as u32)
23    }
24
25    /// Retrieves the multicipher {@link PublicKey} with the given index in this subtree.
26    ///
27    /// An error is thrown if that index was not generated yet with {@link MorpheusPrivateKind.key} or {@link MorpheusPrivateKind.did}.
28    pub fn key(&self, idx: i32) -> Result<JsMPublicKey, JsValue> {
29        let inner = self.inner.key(idx).map_err_to_js()?;
30        Ok(JsMPublicKey::from(inner))
31    }
32
33    /// Retrieves the {@link Did} with the given index in this subtree.
34    ///
35    /// An error is thrown if that index was not generated yet with {@link MorpheusPrivateKind.key} or {@link MorpheusPrivateKind.did}.
36    pub fn did(&self, idx: i32) -> Result<JsDid, JsValue> {
37        let pk = self.inner.key(idx).map_err_to_js()?;
38        let inner = Did::from(pk.key_id());
39        Ok(JsDid::from(inner))
40    }
41
42    /// Finds the multicipher {@link PublicKey} that belongs to the given multicipher {@link KeyId}.
43    ///
44    /// An error will be thrown if the key has never been used yet in this vault or is not this kind of DID.
45    #[wasm_bindgen(js_name = keyById)]
46    pub fn key_by_id(&self, id: &JsMKeyId) -> Result<JsMPublicKey, JsValue> {
47        let inner = self.inner.key_by_id(id.inner()).map_err_to_js()?;
48        Ok(JsMPublicKey::from(inner))
49    }
50}
51
52impl From<MorpheusPublicKind> for JsMorpheusPublicKind {
53    fn from(inner: MorpheusPublicKind) -> Self {
54        Self { inner }
55    }
56}
57
58impl Wraps<MorpheusPublicKind> for JsMorpheusPublicKind {
59    fn inner(&self) -> &MorpheusPublicKind {
60        &self.inner
61    }
62}