use alloc::vec;
use alloc::vec::Vec;
use miden_core::Felt;
use miden_protocol::account::AccountId;
use miden_protocol::crypto::rand::FeltRng;
use miden_protocol::errors::NoteError;
use miden_protocol::note::{
Note,
NoteAssets,
NoteAttachment,
NoteAttachments,
NoteRecipient,
NoteScript,
NoteScriptRoot,
NoteStorage,
NoteType,
PartialNoteMetadata,
};
use miden_standards::interop::eth::EthAddress;
use miden_standards::note::costs::NoteConsumptionCost;
use miden_standards::note::{BurnNote, NetworkAccountTarget, NoteExecutionHint};
use miden_utils_sync::LazyLock;
use crate::costs::B2AGG_CONSUMPTION_CYCLES;
use crate::note_script;
const B2AGG_SCRIPT_PATH: &str = "::agglayer::notes::b2agg::main";
static B2AGG_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| note_script(B2AGG_SCRIPT_PATH));
pub struct B2AggNote;
impl B2AggNote {
pub const NUM_STORAGE_ITEMS: usize = 6;
pub fn script() -> NoteScript {
B2AGG_SCRIPT.clone()
}
pub fn script_root() -> NoteScriptRoot {
B2AGG_SCRIPT.root()
}
pub fn create<R: FeltRng>(
destination_network: u32,
destination_address: EthAddress,
assets: NoteAssets,
target_account_id: AccountId,
sender_account_id: AccountId,
rng: &mut R,
) -> Result<Note, NoteError> {
let note_storage = build_note_storage(destination_network, destination_address)?;
let attachment = NetworkAccountTarget::new(target_account_id, NoteExecutionHint::Always)
.map_err(|error| {
NoteError::other_with_source("failed to create b2agg network account target", error)
})?;
let attachments = NoteAttachments::from(NoteAttachment::from(attachment));
let metadata = PartialNoteMetadata::new(sender_account_id, NoteType::Public);
let recipient = NoteRecipient::new(rng.draw_word(), Self::script(), note_storage);
Ok(Note::with_attachments(assets, metadata, recipient, attachments))
}
}
fn build_note_storage(
destination_network: u32,
destination_address: EthAddress,
) -> Result<NoteStorage, NoteError> {
let mut elements = Vec::with_capacity(6);
let destination_network = u32::from_le_bytes(destination_network.to_be_bytes());
elements.push(Felt::from(destination_network));
elements.extend(destination_address.to_elements());
NoteStorage::new(elements)
}
impl NoteConsumptionCost for B2AggNote {
fn consumption_cycles() -> u32 {
B2AGG_CONSUMPTION_CYCLES
}
fn created_notes() -> Vec<NoteScriptRoot> {
vec![BurnNote::script_root()]
}
}