1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use super::*;

/// Starting point for deriving all Morpheus related keys in a BIP32 hierarchy. Morpheus uses Ed25519 cipher and currently there are no
/// WASM wrappers for Bip32 nodes with that cipher. Still, Bip32 paths are returned by each object so compatible wallets can derive the
/// same extended private keys.
#[wasm_bindgen(js_name = Morpheus)]
#[derive(Clone, Debug)]
pub struct JsMorpheus;

#[wasm_bindgen(js_class = Morpheus)]
impl JsMorpheus {
    /// Calculate the root node of the Morpheus subtree in the HD wallet defined by a seed.
    pub fn root(seed: &JsSeed) -> Result<JsMorpheusRoot, JsValue> {
        let inner = Morpheus.root(seed.inner()).map_err_to_js()?;
        Ok(JsMorpheusRoot::from(inner))
    }
}

/// Representation of the root node of the Morpheus subtree in the HD wallet.
#[wasm_bindgen(js_name = MorpheusRoot)]
#[derive(Clone, Debug)]
pub struct JsMorpheusRoot {
    inner: MorpheusRoot,
}

#[wasm_bindgen(js_class = MorpheusRoot)]
impl JsMorpheusRoot {
    /// Accessor for the BIP32 path of the morpheus root.
    #[wasm_bindgen(getter = path)]
    pub fn bip32_path(&self) -> String {
        self.inner.node().path().to_string()
    }

    /// Derive a separate HD wallet subtree of the given DID kind. Use 'persona', 'device', 'group' or 'resource' in
    /// singular as a parameter.
    pub fn kind(&self, did_kind: &str) -> Result<JsMorpheusKind, JsValue> {
        let did_kind = did_kind.parse().map_err_to_js()?;
        self.kind_impl(did_kind)
    }

    /// Alias for kind('persona')
    pub fn personas(&self) -> Result<JsMorpheusKind, JsValue> {
        self.kind_impl(DidKind::Persona)
    }

    /// Alias for kind('device')
    pub fn devices(&self) -> Result<JsMorpheusKind, JsValue> {
        self.kind_impl(DidKind::Device)
    }

    /// Alias for kind('group')
    pub fn groups(&self) -> Result<JsMorpheusKind, JsValue> {
        self.kind_impl(DidKind::Group)
    }

    /// Alias for kind('resource')
    pub fn resources(&self) -> Result<JsMorpheusKind, JsValue> {
        self.kind_impl(DidKind::Resource)
    }

    fn kind_impl(&self, did_kind: DidKind) -> Result<JsMorpheusKind, JsValue> {
        let inner = self.inner.kind(did_kind).map_err_to_js()?;
        Ok(JsMorpheusKind::from(inner))
    }
}

impl From<MorpheusRoot> for JsMorpheusRoot {
    fn from(inner: MorpheusRoot) -> Self {
        Self { inner }
    }
}

impl Wraps<MorpheusRoot> for JsMorpheusRoot {
    fn inner(&self) -> &MorpheusRoot {
        &self.inner
    }
}

/// Root node of a specific kind of DIDs. The kind used to derive a DID is indistiguishable outside the wallet.
#[wasm_bindgen(js_name = MorpheusKind)]
#[derive(Clone, Debug)]
pub struct JsMorpheusKind {
    inner: MorpheusKind,
}

#[wasm_bindgen(js_class = MorpheusKind)]
impl JsMorpheusKind {
    /// Accessor for the BIP32 path of the morpheus subtree for a DID kind.
    #[wasm_bindgen(getter = path)]
    pub fn bip32_path(&self) -> String {
        self.inner.node().path().to_string()
    }

    /// Accessor for the kind of DIDs in this subtree
    #[wasm_bindgen(getter)]
    pub fn kind(&self) -> String {
        format!("{:?}", self.inner.path())
    }

    /// Creates a {@link MorpheusPrivateKey} with the given index under this subtree.
    /// E.g. 5th persona, 3rd device, or 0th group, etc.
    pub fn key(&self, idx: i32) -> Result<JsMorpheusPrivateKey, JsValue> {
        let inner = self.inner.key(idx).map_err_to_js()?;
        Ok(JsMorpheusPrivateKey::from(inner))
    }
}

impl From<MorpheusKind> for JsMorpheusKind {
    fn from(inner: MorpheusKind) -> Self {
        Self { inner }
    }
}

impl Wraps<MorpheusKind> for JsMorpheusKind {
    fn inner(&self) -> &MorpheusKind {
        &self.inner
    }
}

/// The operations on an identifier that require the private key to be available in memory.
#[wasm_bindgen(js_name = MorpheusPrivateKey)]
#[derive(Clone, Debug)]
pub struct JsMorpheusPrivateKey {
    inner: MorpheusPrivateKey,
}

#[wasm_bindgen(js_class = MorpheusPrivateKey)]
impl JsMorpheusPrivateKey {
    /// Accessor for the BIP32 path of the morpheus key.
    #[wasm_bindgen(getter = path)]
    pub fn bip32_path(&self) -> String {
        self.inner.node().path().to_string()
    }

    /// Accessor for the kind of DIDs in this subtree
    #[wasm_bindgen(getter)]
    pub fn kind(&self) -> String {
        format!("{:?}", self.inner.path().kind())
    }

    /// Index of the key in its subtree.
    #[wasm_bindgen(getter)]
    pub fn idx(&self) -> i32 {
        self.inner().path().idx()
    }

    /// Creates the public interface of the node that does not need the private key in memory.
    pub fn neuter(&self) -> JsMorpheusPublicKey {
        let inner = self.inner.neuter();
        JsMorpheusPublicKey::from(inner)
    }

    /// Returns the multicipher {@link PrivateKey} that belongs to this key.
    #[wasm_bindgen(js_name = privateKey)]
    pub fn private_key(&self) -> JsMPrivateKey {
        let inner = self.inner.private_key();
        JsMPrivateKey::from(inner)
    }
}

impl From<MorpheusPrivateKey> for JsMorpheusPrivateKey {
    fn from(inner: MorpheusPrivateKey) -> Self {
        Self { inner }
    }
}

impl Wraps<MorpheusPrivateKey> for JsMorpheusPrivateKey {
    fn inner(&self) -> &MorpheusPrivateKey {
        &self.inner
    }
}

/// The operations on an identifier that do not require the private key to be available in memory.
#[wasm_bindgen(js_name = MorpheusPublicKey)]
#[derive(Clone, Debug)]
pub struct JsMorpheusPublicKey {
    inner: MorpheusPublicKey,
}

#[wasm_bindgen(js_class = MorpheusPublicKey)]
impl JsMorpheusPublicKey {
    /// Accessor for the BIP32 path of the morpheus key.
    #[wasm_bindgen(getter = path)]
    pub fn bip32_path(&self) -> String {
        self.inner.node().path().to_string()
    }

    /// Accessor for the kind of DIDs in this subtree
    #[wasm_bindgen(getter)]
    pub fn kind(&self) -> String {
        format!("{:?}", self.inner.path().kind())
    }

    /// Index of the key in its subtree.
    #[wasm_bindgen(getter)]
    pub fn idx(&self) -> i32 {
        self.inner().path().idx()
    }

    /// Returns the multicipher {@link PublicKey} that belongs to this key.
    #[wasm_bindgen(js_name = publicKey)]
    pub fn public_key(&self) -> JsMPublicKey {
        let inner = self.inner.public_key();
        JsMPublicKey::from(inner)
    }
}

impl From<MorpheusPublicKey> for JsMorpheusPublicKey {
    fn from(inner: MorpheusPublicKey) -> Self {
        Self { inner }
    }
}

impl Wraps<MorpheusPublicKey> for JsMorpheusPublicKey {
    fn inner(&self) -> &MorpheusPublicKey {
        &self.inner
    }
}