use js_export_macro::js_export;
use miden_client::rpc::domain::nullifier::NullifierUpdate as NativeNullifierUpdate;
use super::RpcClient;
use crate::js_error_with_context;
use crate::models::word::Word;
use crate::platform::{JsErr, JsU16Array, from_str_err};
#[js_export]
pub struct NullifierUpdate(NativeNullifierUpdate);
#[js_export]
impl NullifierUpdate {
pub fn nullifier(&self) -> Word {
self.0.nullifier.as_word().into()
}
#[js_export(js_name = "blockNum")]
pub fn block_num(&self) -> u32 {
self.0.block_num.as_u32()
}
}
#[js_export]
impl RpcClient {
#[allow(clippy::doc_markdown)]
#[js_export(js_name = "syncNullifiers")]
pub async fn sync_nullifiers(
&self,
prefixes: JsU16Array,
block_from: u32,
block_to: u32,
) -> Result<Vec<NullifierUpdate>, JsErr> {
if block_from > block_to {
return Err(from_str_err("block_from must be less than or equal to block_to"));
}
#[cfg(feature = "nodejs")]
let prefixes = prefixes.0;
let mut prefixes = prefixes;
if prefixes.is_empty() {
return Ok(Vec::new());
}
prefixes.sort_unstable();
prefixes.dedup();
self.inner
.sync_nullifiers(&prefixes, block_from.into(), block_to.into())
.await
.map(|updates| updates.into_iter().map(NullifierUpdate).collect())
.map_err(|err| js_error_with_context(err, "failed to sync nullifiers"))
}
}