use derive_more::FromStr;
use ergo_lib::wallet::derivation_path::ChildIndexError;
use ergo_lib::wallet::derivation_path::ChildIndexHardened;
use ergo_lib::wallet::derivation_path::ChildIndexNormal;
use ergo_lib::wallet::derivation_path::DerivationPath as InnerDerivationPath;
use wasm_bindgen::prelude::*;
use crate::error_conversion::to_js;
extern crate derive_more;
use derive_more::{From, Into};
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone, From, Into, FromStr)]
pub struct DerivationPath(InnerDerivationPath);
#[wasm_bindgen]
impl DerivationPath {
pub fn new(acc: u32, address_indices: &[u32]) -> Result<DerivationPath, JsValue> {
let acc = ChildIndexHardened::from_31_bit(acc).map_err(to_js)?;
let address_indices = address_indices
.iter()
.map(|i| ChildIndexNormal::normal(*i))
.collect::<Result<Vec<ChildIndexNormal>, ChildIndexError>>()
.map_err(to_js)?;
Ok(DerivationPath(InnerDerivationPath::new(
acc,
address_indices,
)))
}
pub fn master_path() -> Self {
DerivationPath(InnerDerivationPath::master_path())
}
pub fn depth(&self) -> usize {
self.0.depth()
}
pub fn next(&self) -> Result<DerivationPath, JsValue> {
Ok(self.0.next().map_err(to_js)?.into())
}
#[wasm_bindgen(js_name = toString)]
#[allow(clippy::inherent_to_string)]
pub fn to_string(&self) -> String {
self.0.to_string()
}
pub fn from_string(path: &str) -> Result<DerivationPath, JsValue> {
Ok(path.parse::<InnerDerivationPath>().map_err(to_js)?.into())
}
pub fn ledger_bytes(&self) -> Vec<u8> {
self.0.ledger_bytes()
}
}