use js_export_macro::js_export;
use miden_client::account::AccountFile as NativeAccountFile;
use crate::js_error_with_context;
use crate::models::account::Account;
use crate::models::account_id::AccountId;
use crate::platform::{JsBytes, JsErr, bytes_to_js, js_to_bytes};
#[derive(Debug, Clone)]
#[js_export]
pub struct AccountFile(NativeAccountFile);
#[js_export]
impl AccountFile {
#[js_export(js_name = "accountId")]
pub fn account_id(&self) -> AccountId {
self.0.account().id().into()
}
pub fn account(&self) -> Account {
self.0.account().clone().into()
}
#[js_export(js_name = "authSecretKeyCount")]
pub fn auth_secret_key_count(&self) -> usize {
self.0.auth_secret_keys().len()
}
pub fn serialize(&self) -> JsBytes {
bytes_to_js(&self.0.to_bytes())
}
pub fn deserialize(bytes: JsBytes) -> Result<AccountFile, JsErr> {
let native_account_file = NativeAccountFile::try_from_bytes(&js_to_bytes(&bytes))
.map_err(|err| js_error_with_context(err, "account file deserialization failed"))?;
Ok(Self(native_account_file))
}
}
impl From<NativeAccountFile> for AccountFile {
fn from(native_account_file: NativeAccountFile) -> Self {
Self(native_account_file)
}
}
impl From<AccountFile> for NativeAccountFile {
fn from(account_file: AccountFile) -> Self {
account_file.0
}
}
impl_napi_from_value!(AccountFile);