use alloc::vec::Vec;
use miden_protocol::Word;
use miden_protocol::account::AccountId;
use miden_protocol::assembly::Path;
use miden_protocol::asset::Asset;
use miden_protocol::crypto::rand::FeltRng;
use miden_protocol::errors::NoteError;
use miden_protocol::note::{
Note,
NoteAssets,
NoteAttachment,
NoteAttachments,
NoteRecipient,
NoteScript,
NoteScriptRoot,
NoteStorage,
NoteTag,
NoteType,
PartialNoteMetadata,
};
use miden_protocol::utils::sync::LazyLock;
use crate::StandardsLib;
const BURN_SCRIPT_PATH: &str = "::miden::standards::notes::burn::main";
static BURN_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let standards_lib = StandardsLib::default();
let path = Path::new(BURN_SCRIPT_PATH);
NoteScript::from_library_reference(standards_lib.as_ref(), path)
.expect("Standards library contains BURN note script procedure")
});
#[derive(Debug, Clone)]
pub struct BurnNote {
sender: AccountId,
serial_number: Word,
asset: Asset,
attachments: NoteAttachments,
}
#[bon::bon]
impl BurnNote {
#[builder]
pub fn new(
#[builder(field)] attachments: Vec<NoteAttachment>,
sender: AccountId,
#[builder(into)] asset: Asset,
serial_number: Word,
) -> Result<Self, NoteError> {
let attachments = NoteAttachments::new(attachments)?;
Ok(Self {
sender,
serial_number,
asset,
attachments,
})
}
}
impl BurnNote {
pub const NUM_STORAGE_ITEMS: usize = 0;
pub fn script() -> NoteScript {
BURN_SCRIPT.clone()
}
pub fn script_root() -> NoteScriptRoot {
BURN_SCRIPT.root()
}
pub fn sender(&self) -> AccountId {
self.sender
}
pub fn faucet_id(&self) -> AccountId {
self.asset.faucet_id()
}
pub fn serial_number(&self) -> Word {
self.serial_number
}
pub fn asset(&self) -> Asset {
self.asset
}
pub fn attachments(&self) -> &NoteAttachments {
&self.attachments
}
}
impl<S: burn_note_builder::State> BurnNoteBuilder<S> {
pub fn attachment(mut self, attachment: impl Into<NoteAttachment>) -> Self {
self.attachments.push(attachment.into());
self
}
pub fn attachments(
mut self,
attachments: impl IntoIterator<Item = impl Into<NoteAttachment>>,
) -> Self {
self.attachments.extend(attachments.into_iter().map(Into::into));
self
}
}
impl<S: burn_note_builder::State> BurnNoteBuilder<S>
where
S::SerialNumber: burn_note_builder::IsUnset,
{
pub fn generate_serial_number(
self,
rng: &mut impl FeltRng,
) -> BurnNoteBuilder<burn_note_builder::SetSerialNumber<S>> {
self.serial_number(rng.draw_word())
}
}
impl From<BurnNote> for Note {
fn from(note: BurnNote) -> Self {
let metadata = PartialNoteMetadata::new(note.sender, NoteType::Public)
.with_tag(NoteTag::with_account_target(note.asset.faucet_id()));
let recipient =
NoteRecipient::new(note.serial_number, BurnNote::script(), NoteStorage::default());
let assets = NoteAssets::new(vec![note.asset])
.expect("a single asset never exceeds the note asset limit");
Note::with_attachments(assets, metadata, recipient, note.attachments)
}
}
#[cfg(test)]
mod tests {
use miden_protocol::account::AccountType;
use miden_protocol::asset::FungibleAsset;
use miden_protocol::crypto::rand::RandomCoin;
use super::*;
fn sender() -> AccountId {
AccountId::builder().account_type(AccountType::Private).build_with_seed([1; 32])
}
fn faucet() -> AccountId {
AccountId::builder().account_type(AccountType::Public).build_with_seed([2; 32])
}
#[test]
fn builder_builds_public_burn_note() {
let mut rng = RandomCoin::new(Word::empty());
let asset = FungibleAsset::new(faucet(), 100).unwrap();
let burn_note = BurnNote::builder()
.sender(sender())
.asset(asset)
.generate_serial_number(&mut rng)
.build()
.unwrap();
assert_eq!(burn_note.sender(), sender());
assert_eq!(burn_note.faucet_id(), faucet());
assert_eq!(burn_note.asset(), asset.into());
assert_ne!(burn_note.serial_number(), Word::empty());
let note = Note::from(burn_note);
assert_eq!(note.metadata().note_type(), NoteType::Public);
assert_eq!(note.metadata().tag(), NoteTag::with_account_target(faucet()));
assert_eq!(note.assets().num_assets(), 1);
}
}