use miden_client::note::{
NoteHeader as NativeNoteHeader,
NoteInclusionProof as NativeNoteInclusionProof,
};
use wasm_bindgen::prelude::wasm_bindgen;
use crate::models::NoteType;
use crate::models::input_note::InputNote;
use crate::models::note::Note;
use crate::models::note_header::NoteHeader;
use crate::models::note_id::NoteId;
use crate::models::note_inclusion_proof::NoteInclusionProof;
use crate::models::note_metadata::NoteMetadata;
#[derive(Clone)]
#[wasm_bindgen]
pub struct FetchedNote {
header: NoteHeader,
inclusion_proof: NoteInclusionProof,
note: Option<Note>,
}
#[wasm_bindgen]
impl FetchedNote {
#[wasm_bindgen(constructor)]
pub fn new(
note_id: NoteId,
metadata: NoteMetadata,
inclusion_proof: NoteInclusionProof,
note: Option<Note>,
) -> FetchedNote {
let native_note_id = note_id.into();
let native_metadata = metadata.into();
let native_header = NativeNoteHeader::new(native_note_id, native_metadata);
let header = native_header.into();
FetchedNote { header, inclusion_proof, note }
}
#[wasm_bindgen(getter)]
#[wasm_bindgen(js_name = "noteId")]
pub fn note_id(&self) -> NoteId {
self.header.id()
}
#[wasm_bindgen(getter)]
pub fn metadata(&self) -> NoteMetadata {
self.header.metadata()
}
#[wasm_bindgen(getter)]
pub fn header(&self) -> NoteHeader {
self.header.clone()
}
#[wasm_bindgen(getter)]
pub fn note(&self) -> Option<Note> {
self.note.clone()
}
#[wasm_bindgen(getter)]
#[wasm_bindgen(js_name = "inclusionProof")]
pub fn inclusion_proof(&self) -> NoteInclusionProof {
self.inclusion_proof.clone()
}
#[wasm_bindgen(getter)]
#[wasm_bindgen(js_name = "noteType")]
pub fn note_type(&self) -> NoteType {
self.header.metadata().note_type()
}
#[wasm_bindgen(js_name = "asInputNote")]
pub fn as_input_note(&self) -> Option<InputNote> {
self.note().map(|note| InputNote::authenticated(¬e, &self.inclusion_proof))
}
}
impl FetchedNote {
pub(super) fn from_header(
header: NativeNoteHeader,
note: Option<Note>,
inclusion_proof: NativeNoteInclusionProof,
) -> Self {
FetchedNote {
header: header.into(),
note,
inclusion_proof: inclusion_proof.into(),
}
}
}