iop_sdk_wasm/vault/
mod.rs

1mod serializer;
2
3use serializer::VaultSerializer;
4
5use super::*;
6
7/// This object provides a safe serialization format for an in-rest encoded vault file for the IOP Stack™.
8#[wasm_bindgen(js_name = Vault)]
9pub struct JsVault {
10    inner: Vault,
11}
12
13#[wasm_bindgen(js_class = Vault)]
14impl JsVault {
15    /// Creates a new in-memory vault object from a BIP39 phrase, a seed password (aka 25th word), an unlock password used for
16    /// encryption of the secrets in rest, and optionally a language code (e.g. 'zh-hans' or 'es') for the BIP39 phrase words ('en' by
17    /// default).
18    pub fn create(
19        phrase: &str, bip39_password: &str, unlock_password: &str, language: Option<String>,
20    ) -> Result<JsVault, JsValue> {
21        let inner = Vault::create(language.as_deref(), phrase, bip39_password, unlock_password)
22            .map_err_to_js()?;
23        Ok(Self { inner })
24    }
25
26    /// Loads the vault from its JSON serialization format. Note that no private keys can be calculated without unlocking the loaded
27    /// vault with {@link unlock} or with some plugins like {@link HydraPlugin.private} or {@link MorpheusPlugin.private}. The public
28    /// keys can be enumerated and used without the unlock password.
29    pub fn load(data: &JsValue) -> Result<JsVault, JsValue> {
30        // TODO Consider https://github.com/cloudflare/serde-wasm-bindgen
31        let data_serde: serde_json::Value = data.into_serde().map_err_to_js()?;
32        let hack: VaultSerializer = serde_json::from_value(data_serde).map_err_to_js()?;
33        let inner = Vault::from(hack);
34        Ok(Self { inner })
35    }
36
37    /// Saves the vault into its JSON serialization format. The private keys are encrypted with the unlock password, but the public
38    /// keys can be enumerated from the file, so make sure you understand the privacy aspects of sharing such file with 3rd parties.
39    ///
40    /// Note that calling this method clears the {@link dirty} flag on the vault.
41    pub fn save(&mut self) -> Result<JsValue, JsValue> {
42        let result = JsValue::from_serde(&self.inner).map_err_to_js()?;
43        self.set_dirty(false)?;
44        Ok(result)
45    }
46
47    /// Returns whether the vault has changes since it has been last saved.
48    ///
49    /// @see save
50    #[wasm_bindgen(getter = dirty)]
51    pub fn is_dirty(&self) -> Result<bool, JsValue> {
52        let vault_dirty = self.inner.to_modifiable();
53        let dirty = vault_dirty.try_borrow().map_err_to_js()?;
54        Ok(*dirty)
55    }
56
57    /// Manually sets the dirty flag on the vault.
58    #[wasm_bindgen(js_name = setDirty)]
59    pub fn set_to_dirty(&mut self) -> Result<(), JsValue> {
60        self.set_dirty(true)?;
61        Ok(())
62    }
63
64    /// Unlocks the secrets in the vault with a password. Make sure the password is difficult to guess. Good passwords are a few words
65    /// randomly picked from huge dictionaries, like what the passphrase option of the [Bitwarden password
66    /// generator](https://bitwarden.com/password-generator/) creates (See [correct horse battery staple](https://xkcd.com/936/)).
67    pub fn unlock(&self, password: &str) -> Result<JsSeed, JsValue> {
68        let seed = self.inner.unlock(password).map_err_to_js()?;
69        Ok(JsSeed::from(seed))
70    }
71
72    fn set_dirty(&mut self, value: bool) -> Result<(), JsValue> {
73        let mut vault_dirty = self.inner.to_modifiable();
74        let mut dirty = vault_dirty.try_borrow_mut().map_err_to_js()?;
75        *dirty = value;
76        Ok(())
77    }
78}
79
80impl From<Vault> for JsVault {
81    fn from(inner: Vault) -> Self {
82        Self { inner }
83    }
84}
85
86impl Wraps<Vault> for JsVault {
87    fn inner(&self) -> &Vault {
88        &self.inner
89    }
90}
91
92impl WrapsMut<Vault> for JsVault {
93    fn inner_mut(&mut self) -> &mut Vault {
94        &mut self.inner
95    }
96}