use miden_client::PrettyPrint;
use miden_client::note::NoteScript as NativeNoteScript;
use miden_standards::note::StandardNote;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::js_sys::Uint8Array;
use super::word::Word;
use crate::models::package::Package;
use crate::utils::{deserialize_from_uint8array, serialize_to_uint8array};
#[derive(Clone)]
#[wasm_bindgen]
pub struct NoteScript(NativeNoteScript);
#[wasm_bindgen]
impl NoteScript {
#[wasm_bindgen(js_name = toString)]
#[allow(clippy::inherent_to_string)]
pub fn to_string(&self) -> String {
self.0.to_pretty_string()
}
pub fn serialize(&self) -> Uint8Array {
serialize_to_uint8array(&self.0)
}
pub fn deserialize(bytes: &Uint8Array) -> Result<NoteScript, JsValue> {
deserialize_from_uint8array::<NativeNoteScript>(bytes).map(NoteScript)
}
pub fn p2id() -> Self {
StandardNote::P2ID.script().into()
}
pub fn p2ide() -> Self {
StandardNote::P2IDE.script().into()
}
pub fn swap() -> Self {
StandardNote::SWAP.script().into()
}
pub fn root(&self) -> Word {
self.0.root().into()
}
#[wasm_bindgen(js_name = "fromPackage")]
pub fn from_package(package: &Package) -> Result<NoteScript, JsValue> {
let native_package: miden_client::vm::Package = package.into();
let native_note_script = NativeNoteScript::from_package(&native_package)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(native_note_script.into())
}
}
impl From<NativeNoteScript> for NoteScript {
fn from(native_note_script: NativeNoteScript) -> Self {
NoteScript(native_note_script)
}
}
impl From<&NativeNoteScript> for NoteScript {
fn from(native_note_script: &NativeNoteScript) -> Self {
NoteScript(native_note_script.clone())
}
}
impl From<NoteScript> for NativeNoteScript {
fn from(note_script: NoteScript) -> Self {
note_script.0
}
}
impl From<&NoteScript> for NativeNoteScript {
fn from(note_script: &NoteScript) -> Self {
note_script.0.clone()
}
}