extern crate alloc;
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use miden_assembly::serde::Deserializable;
use miden_core::{Felt, Word};
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_protocol::vm::Program;
use miden_standards::note::{NetworkAccountTarget, NoteExecutionHint};
use miden_utils_sync::LazyLock;
use crate::EthAddress;
static CONFIG_AGG_BRIDGE_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let bytes =
include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/CONFIG_AGG_BRIDGE.masb"));
let program =
Program::read_from_bytes(bytes).expect("shipped CONFIG_AGG_BRIDGE script is well-formed");
NoteScript::new(program)
});
pub struct ConfigAggBridgeNote;
impl ConfigAggBridgeNote {
pub const NUM_STORAGE_ITEMS: usize = 7;
pub fn script() -> NoteScript {
CONFIG_AGG_BRIDGE_SCRIPT.clone()
}
pub fn script_root() -> Word {
CONFIG_AGG_BRIDGE_SCRIPT.root()
}
pub fn create<R: FeltRng>(
faucet_account_id: AccountId,
origin_token_address: &EthAddress,
sender_account_id: AccountId,
target_account_id: AccountId,
rng: &mut R,
) -> Result<Note, NoteError> {
let addr_elements = origin_token_address.to_elements();
let mut storage_values: Vec<Felt> = addr_elements;
storage_values.push(faucet_account_id.suffix());
storage_values.push(faucet_account_id.prefix().as_felt());
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))
}
}