iop_sdk_wasm/hydra/
public.rs

1use super::*;
2
3/// Public keys of a Hydra account in a vault.
4///
5/// @see HydraPlugin.pub
6#[wasm_bindgen(js_name = HydraPublic)]
7pub struct JsHydraPublic {
8    inner: HydraPublic,
9}
10
11#[wasm_bindgen(js_class = HydraPublic)]
12impl JsHydraPublic {
13    /// Name of the network this account belongs to.
14    #[wasm_bindgen(getter)]
15    pub fn network(&self) -> String {
16        self.inner.network().subtree().name().to_owned()
17    }
18
19    /// Calculates the receiving address having the given index and takes note that the address was already generated in the account.
20    ///
21    /// @see Bip44Account.key, Bip44Account.chain
22    pub fn key(&mut self, idx: i32) -> Result<JsBip44PublicKey, JsValue> {
23        let inner = self.inner.key_mut(idx).map_err_to_js()?;
24        Ok(JsBip44PublicKey::from(inner))
25    }
26
27    /// The extended public key for auditing the whole Bip44 account or deriving new public keys outside the vault.
28    #[wasm_bindgen(getter)]
29    pub fn xpub(&self) -> Result<String, JsValue> {
30        let res = self.inner().xpub().map_err_to_js()?;
31        Ok(res)
32    }
33
34    /// How many receive addresses have been used in this {@link Bip44Account}
35    #[wasm_bindgen(getter = receiveKeys)]
36    pub fn receive_keys(&self) -> Result<u32, JsValue> {
37        let res = self.inner().receive_keys().map_err_to_js()?;
38        Ok(res)
39    }
40
41    /// How many change addresses have been used in this {@link Bip44Account}
42    #[wasm_bindgen(getter = changeKeys)]
43    pub fn change_keys(&self) -> Result<u32, JsValue> {
44        let res = self.inner().change_keys().map_err_to_js()?;
45        Ok(res)
46    }
47
48    /// Finds the {@link Bip44PublicKey} public api that belongs to the given P2PKH address. You can check the index of the key or
49    /// get the actual {@link SecpPublicKey} from the returned object.
50    ///
51    /// Throws an error if the address is not in this account, which can also happen when the key was derived outside the vault and
52    /// therefore the vault does not know it was already used. In that case, make sure to "touch" the last key index used by calling
53    /// {@link key} before calling this method.
54    #[wasm_bindgen(js_name = keyByAddress)]
55    pub fn key_by_p2pkh_addr(&self, addr: &str) -> Result<JsBip44PublicKey, JsValue> {
56        let inner = self.inner.key_by_p2pkh_addr(addr).map_err_to_js()?;
57        Ok(JsBip44PublicKey::from(inner))
58    }
59}
60
61impl From<HydraPublic> for JsHydraPublic {
62    fn from(inner: HydraPublic) -> Self {
63        Self { inner }
64    }
65}
66
67impl Wraps<HydraPublic> for JsHydraPublic {
68    fn inner(&self) -> &HydraPublic {
69        &self.inner
70    }
71}