iop_keyvault_wasm/
morpheus.rs

1use super::*;
2
3/// Starting point for deriving all Morpheus related keys in a BIP32 hierarchy. Morpheus uses Ed25519 cipher and currently there are no
4/// WASM wrappers for Bip32 nodes with that cipher. Still, Bip32 paths are returned by each object so compatible wallets can derive the
5/// same extended private keys.
6#[wasm_bindgen(js_name = Morpheus)]
7#[derive(Clone, Debug)]
8pub struct JsMorpheus;
9
10#[wasm_bindgen(js_class = Morpheus)]
11impl JsMorpheus {
12    /// Calculate the root node of the Morpheus subtree in the HD wallet defined by a seed.
13    pub fn root(seed: &JsSeed) -> Result<JsMorpheusRoot, JsValue> {
14        let inner = Morpheus.root(seed.inner()).map_err_to_js()?;
15        Ok(JsMorpheusRoot::from(inner))
16    }
17}
18
19/// Representation of the root node of the Morpheus subtree in the HD wallet.
20#[wasm_bindgen(js_name = MorpheusRoot)]
21#[derive(Clone, Debug)]
22pub struct JsMorpheusRoot {
23    inner: MorpheusRoot,
24}
25
26#[wasm_bindgen(js_class = MorpheusRoot)]
27impl JsMorpheusRoot {
28    /// Accessor for the BIP32 path of the morpheus root.
29    #[wasm_bindgen(getter = path)]
30    pub fn bip32_path(&self) -> String {
31        self.inner.node().path().to_string()
32    }
33
34    /// Derive a separate HD wallet subtree of the given DID kind. Use 'persona', 'device', 'group' or 'resource' in
35    /// singular as a parameter.
36    pub fn kind(&self, did_kind: &str) -> Result<JsMorpheusKind, JsValue> {
37        let did_kind = did_kind.parse().map_err_to_js()?;
38        self.kind_impl(did_kind)
39    }
40
41    /// Alias for kind('persona')
42    pub fn personas(&self) -> Result<JsMorpheusKind, JsValue> {
43        self.kind_impl(DidKind::Persona)
44    }
45
46    /// Alias for kind('device')
47    pub fn devices(&self) -> Result<JsMorpheusKind, JsValue> {
48        self.kind_impl(DidKind::Device)
49    }
50
51    /// Alias for kind('group')
52    pub fn groups(&self) -> Result<JsMorpheusKind, JsValue> {
53        self.kind_impl(DidKind::Group)
54    }
55
56    /// Alias for kind('resource')
57    pub fn resources(&self) -> Result<JsMorpheusKind, JsValue> {
58        self.kind_impl(DidKind::Resource)
59    }
60
61    fn kind_impl(&self, did_kind: DidKind) -> Result<JsMorpheusKind, JsValue> {
62        let inner = self.inner.kind(did_kind).map_err_to_js()?;
63        Ok(JsMorpheusKind::from(inner))
64    }
65}
66
67impl From<MorpheusRoot> for JsMorpheusRoot {
68    fn from(inner: MorpheusRoot) -> Self {
69        Self { inner }
70    }
71}
72
73impl Wraps<MorpheusRoot> for JsMorpheusRoot {
74    fn inner(&self) -> &MorpheusRoot {
75        &self.inner
76    }
77}
78
79/// Root node of a specific kind of DIDs. The kind used to derive a DID is indistiguishable outside the wallet.
80#[wasm_bindgen(js_name = MorpheusKind)]
81#[derive(Clone, Debug)]
82pub struct JsMorpheusKind {
83    inner: MorpheusKind,
84}
85
86#[wasm_bindgen(js_class = MorpheusKind)]
87impl JsMorpheusKind {
88    /// Accessor for the BIP32 path of the morpheus subtree for a DID kind.
89    #[wasm_bindgen(getter = path)]
90    pub fn bip32_path(&self) -> String {
91        self.inner.node().path().to_string()
92    }
93
94    /// Accessor for the kind of DIDs in this subtree
95    #[wasm_bindgen(getter)]
96    pub fn kind(&self) -> String {
97        format!("{:?}", self.inner.path())
98    }
99
100    /// Creates a {@link MorpheusPrivateKey} with the given index under this subtree.
101    /// E.g. 5th persona, 3rd device, or 0th group, etc.
102    pub fn key(&self, idx: i32) -> Result<JsMorpheusPrivateKey, JsValue> {
103        let inner = self.inner.key(idx).map_err_to_js()?;
104        Ok(JsMorpheusPrivateKey::from(inner))
105    }
106}
107
108impl From<MorpheusKind> for JsMorpheusKind {
109    fn from(inner: MorpheusKind) -> Self {
110        Self { inner }
111    }
112}
113
114impl Wraps<MorpheusKind> for JsMorpheusKind {
115    fn inner(&self) -> &MorpheusKind {
116        &self.inner
117    }
118}
119
120/// The operations on an identifier that require the private key to be available in memory.
121#[wasm_bindgen(js_name = MorpheusPrivateKey)]
122#[derive(Clone, Debug)]
123pub struct JsMorpheusPrivateKey {
124    inner: MorpheusPrivateKey,
125}
126
127#[wasm_bindgen(js_class = MorpheusPrivateKey)]
128impl JsMorpheusPrivateKey {
129    /// Accessor for the BIP32 path of the morpheus key.
130    #[wasm_bindgen(getter = path)]
131    pub fn bip32_path(&self) -> String {
132        self.inner.node().path().to_string()
133    }
134
135    /// Accessor for the kind of DIDs in this subtree
136    #[wasm_bindgen(getter)]
137    pub fn kind(&self) -> String {
138        format!("{:?}", self.inner.path().kind())
139    }
140
141    /// Index of the key in its subtree.
142    #[wasm_bindgen(getter)]
143    pub fn idx(&self) -> i32 {
144        self.inner().path().idx()
145    }
146
147    /// Creates the public interface of the node that does not need the private key in memory.
148    pub fn neuter(&self) -> JsMorpheusPublicKey {
149        let inner = self.inner.neuter();
150        JsMorpheusPublicKey::from(inner)
151    }
152
153    /// Returns the multicipher {@link PrivateKey} that belongs to this key.
154    #[wasm_bindgen(js_name = privateKey)]
155    pub fn private_key(&self) -> JsMPrivateKey {
156        let inner = self.inner.private_key();
157        JsMPrivateKey::from(inner)
158    }
159}
160
161impl From<MorpheusPrivateKey> for JsMorpheusPrivateKey {
162    fn from(inner: MorpheusPrivateKey) -> Self {
163        Self { inner }
164    }
165}
166
167impl Wraps<MorpheusPrivateKey> for JsMorpheusPrivateKey {
168    fn inner(&self) -> &MorpheusPrivateKey {
169        &self.inner
170    }
171}
172
173/// The operations on an identifier that do not require the private key to be available in memory.
174#[wasm_bindgen(js_name = MorpheusPublicKey)]
175#[derive(Clone, Debug)]
176pub struct JsMorpheusPublicKey {
177    inner: MorpheusPublicKey,
178}
179
180#[wasm_bindgen(js_class = MorpheusPublicKey)]
181impl JsMorpheusPublicKey {
182    /// Accessor for the BIP32 path of the morpheus key.
183    #[wasm_bindgen(getter = path)]
184    pub fn bip32_path(&self) -> String {
185        self.inner.node().path().to_string()
186    }
187
188    /// Accessor for the kind of DIDs in this subtree
189    #[wasm_bindgen(getter)]
190    pub fn kind(&self) -> String {
191        format!("{:?}", self.inner.path().kind())
192    }
193
194    /// Index of the key in its subtree.
195    #[wasm_bindgen(getter)]
196    pub fn idx(&self) -> i32 {
197        self.inner().path().idx()
198    }
199
200    /// Returns the multicipher {@link PublicKey} that belongs to this key.
201    #[wasm_bindgen(js_name = publicKey)]
202    pub fn public_key(&self) -> JsMPublicKey {
203        let inner = self.inner.public_key();
204        JsMPublicKey::from(inner)
205    }
206}
207
208impl From<MorpheusPublicKey> for JsMorpheusPublicKey {
209    fn from(inner: MorpheusPublicKey) -> Self {
210        Self { inner }
211    }
212}
213
214impl Wraps<MorpheusPublicKey> for JsMorpheusPublicKey {
215    fn inner(&self) -> &MorpheusPublicKey {
216        &self.inner
217    }
218}