use js_export_macro::js_export;
use miden_client::crypto::MerklePath as NativeMerklePath;
use super::word::Word;
use crate::platform::{JsErr, from_str_err, js_u64_to_u64};
#[derive(Clone)]
#[js_export]
pub struct MerklePath(NativeMerklePath);
#[js_export]
impl MerklePath {
#[js_export(js_name = "computeRoot")]
pub fn compute_root(&self, index: JsU64, node: &Word) -> Result<Word, JsErr> {
self.0
.compute_root(js_u64_to_u64(index), node.clone().into())
.map(Into::into)
.map_err(|err| from_str_err(&format!("Invalid Merkle path index: {err}")))
}
pub fn verify(&self, index: JsU64, node: &Word, root: &Word) -> bool {
self.0
.verify(js_u64_to_u64(index), node.clone().into(), &root.clone().into())
.is_ok()
}
pub fn depth(&self) -> u8 {
self.0.depth()
}
pub fn nodes(&self) -> Vec<Word> {
self.0.nodes().iter().map(Into::into).collect()
}
}
impl From<NativeMerklePath> for MerklePath {
fn from(native_path: NativeMerklePath) -> Self {
MerklePath(native_path)
}
}
impl From<&NativeMerklePath> for MerklePath {
fn from(native_path: &NativeMerklePath) -> Self {
MerklePath(native_path.clone())
}
}