extern crate alloc;
use alloc::string::ToString;
use alloc::vec;
use miden_assembly::serde::Deserializable;
use miden_core::Word;
use miden_core::program::Program;
use miden_protocol::account::AccountId;
use miden_protocol::crypto::rand::FeltRng;
use miden_protocol::errors::NoteError;
use miden_protocol::note::{
Note,
NoteAssets,
NoteAttachment,
NoteMetadata,
NoteRecipient,
NoteScript,
NoteStorage,
NoteType,
};
use miden_standards::note::{NetworkAccountTarget, NoteExecutionHint};
use miden_utils_sync::LazyLock;
use crate::ExitRoot;
static UPDATE_GER_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/UPDATE_GER.masb"));
let program =
Program::read_from_bytes(bytes).expect("shipped UPDATE_GER script is well-formed");
NoteScript::new(program)
});
pub struct UpdateGerNote;
impl UpdateGerNote {
pub const NUM_STORAGE_ITEMS: usize = 8;
pub fn script() -> NoteScript {
UPDATE_GER_SCRIPT.clone()
}
pub fn script_root() -> Word {
UPDATE_GER_SCRIPT.root()
}
pub fn create<R: FeltRng>(
ger: ExitRoot,
sender_account_id: AccountId,
target_account_id: AccountId,
rng: &mut R,
) -> Result<Note, NoteError> {
let storage_values = ger.to_elements().to_vec();
let note_storage = NoteStorage::new(storage_values)?;
let serial_num = rng.draw_word();
let recipient = NoteRecipient::new(serial_num, Self::script(), note_storage);
let attachment = NoteAttachment::from(
NetworkAccountTarget::new(target_account_id, NoteExecutionHint::Always)
.map_err(|e| NoteError::other(e.to_string()))?,
);
let metadata =
NoteMetadata::new(sender_account_id, NoteType::Public).with_attachment(attachment);
let assets = NoteAssets::new(vec![])?;
Ok(Note::new(assets, metadata, recipient))
}
}