iop_sdk_wasm/morpheus/
private_kind.rs

1use super::*;
2
3/// Private keys for a given DID kind in the Morpheus subtree in a vault.
4///
5/// @see MorpheusPrivate.kind
6#[wasm_bindgen(js_name = MorpheusPrivateKind)]
7pub struct JsMorpheusPrivateKind {
8    inner: MorpheusPrivateKind,
9}
10
11#[wasm_bindgen(js_class = MorpheusPrivateKind)]
12impl JsMorpheusPrivateKind {
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    /// Accessor for the BIP32 path of the subtree with the given DID kinds.
20    #[wasm_bindgen(getter = path)]
21    pub fn bip32_path(&self) -> String {
22        self.inner.node().path().to_string()
23    }
24
25    /// Retrieves how many DIDs have already be generated of this kind.
26    #[wasm_bindgen(getter)]
27    pub fn count(&self) -> Result<u32, JsValue> {
28        self.inner.len().map_err_to_js()
29    }
30
31    /// Accessor for the public keys that belong to this subtree in a vault.
32    ///
33    /// @see MorpheusPlugin.pub, MorpheusPublic.kind
34    #[wasm_bindgen(getter = pub)]
35    pub fn neuter(&self) -> JsMorpheusPublicKind {
36        let inner = self.inner.neuter();
37        JsMorpheusPublicKind::from(inner)
38    }
39
40    /// Generates the {@link MorpheusPrivateKey} with the given index.
41    ///
42    /// The result has methods to be converted into a {@link Did} or a
43    /// {@link KeyId} or for authenticating actions signed by the
44    /// {@link PrivateKey} that belongs to that key.
45    pub fn key(&mut self, idx: i32) -> Result<JsMorpheusPrivateKey, JsValue> {
46        let inner = self.inner.key_mut(idx).map_err_to_js()?;
47        Ok(JsMorpheusPrivateKey::from(inner))
48    }
49
50    /// Generates the {@link Did} with the given index.
51    pub fn did(&mut self, idx: i32) -> Result<JsDid, JsValue> {
52        let sk = self.inner.key_mut(idx).map_err_to_js()?;
53        let inner = Did::from(sk.neuter().public_key().key_id());
54        Ok(JsDid::from(inner))
55    }
56
57    /// Finds the {@link MorpheusPrivateKey} that belongs to the given multicipher {@link PublicKey}. You can check the DID kind or
58    /// index of the key or get the actual {@link PrivateKey} from the returned object.
59    ///
60    /// An error will be thrown if the public key has never been used yet in this vault or is not this kind of DID.
61    #[wasm_bindgen(js_name = keyByPublicKey)]
62    pub fn key_by_pk(&self, id: &JsMPublicKey) -> Result<JsMorpheusPrivateKey, JsValue> {
63        let inner = self.inner.key_by_pk(id.inner()).map_err_to_js()?;
64        Ok(JsMorpheusPrivateKey::from(inner))
65    }
66}
67
68impl From<MorpheusPrivateKind> for JsMorpheusPrivateKind {
69    fn from(inner: MorpheusPrivateKind) -> Self {
70        Self { inner }
71    }
72}
73
74impl Wraps<MorpheusPrivateKind> for JsMorpheusPrivateKind {
75    fn inner(&self) -> &MorpheusPrivateKind {
76        &self.inner
77    }
78}
79
80impl WrapsMut<MorpheusPrivateKind> for JsMorpheusPrivateKind {
81    fn inner_mut(&mut self) -> &mut MorpheusPrivateKind {
82        &mut self.inner
83    }
84}