use alloc::vec::Vec;
use miden_protocol::account::AccountId;
use miden_protocol::assembly::Path;
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 miden_protocol::{Felt, Word};
use crate::StandardsLib;
const OWNER_ACTION_SCRIPT_PATH: &str = "::miden::standards::notes::owner_action::main";
static OWNER_ACTION_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
let standards_lib = StandardsLib::default();
let path = Path::new(OWNER_ACTION_SCRIPT_PATH);
NoteScript::from_library_reference(standards_lib.as_ref(), path)
.expect("Standards library contains OWNER_ACTION note script procedure")
});
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OwnerAction {
TransferOwnership { new_owner: Option<AccountId> },
AcceptOwnership,
RenounceOwnership,
}
impl OwnerAction {
const SELECTOR_TRANSFER_OWNERSHIP: u8 = 0;
const SELECTOR_ACCEPT_OWNERSHIP: u8 = 1;
const SELECTOR_RENOUNCE_OWNERSHIP: u8 = 2;
fn to_storage_values(self) -> Vec<Felt> {
match self {
OwnerAction::TransferOwnership { new_owner } => {
let (suffix, prefix) = match new_owner {
Some(id) => (id.suffix(), id.prefix().as_felt()),
None => (Felt::ZERO, Felt::ZERO),
};
vec![Felt::from(Self::SELECTOR_TRANSFER_OWNERSHIP), suffix, prefix]
},
OwnerAction::AcceptOwnership => {
vec![Felt::from(Self::SELECTOR_ACCEPT_OWNERSHIP)]
},
OwnerAction::RenounceOwnership => {
vec![Felt::from(Self::SELECTOR_RENOUNCE_OWNERSHIP)]
},
}
}
}
impl From<OwnerAction> for NoteStorage {
fn from(action: OwnerAction) -> Self {
NoteStorage::new(action.to_storage_values())
.expect("number of storage items should not exceed max storage items")
}
}
#[derive(Debug, Clone)]
pub struct OwnerActionNote {
sender: AccountId,
account: AccountId,
action: OwnerAction,
serial_number: Word,
attachments: NoteAttachments,
}
#[bon::bon]
impl OwnerActionNote {
#[builder]
pub fn new(
#[builder(field)] attachments: Vec<NoteAttachment>,
sender: AccountId,
account: AccountId,
action: OwnerAction,
serial_number: Word,
) -> Result<Self, NoteError> {
let attachments = NoteAttachments::new(attachments)?;
Ok(Self {
sender,
account,
action,
serial_number,
attachments,
})
}
}
impl OwnerActionNote {
pub const MAX_NUM_STORAGE_ITEMS: usize = 3;
pub fn script() -> NoteScript {
OWNER_ACTION_SCRIPT.clone()
}
pub fn script_root() -> NoteScriptRoot {
OWNER_ACTION_SCRIPT.root()
}
pub fn sender(&self) -> AccountId {
self.sender
}
pub fn account(&self) -> AccountId {
self.account
}
pub fn action(&self) -> OwnerAction {
self.action
}
pub fn serial_number(&self) -> Word {
self.serial_number
}
pub fn attachments(&self) -> &NoteAttachments {
&self.attachments
}
}
impl<S: owner_action_note_builder::State> OwnerActionNoteBuilder<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: owner_action_note_builder::State> OwnerActionNoteBuilder<S>
where
S::SerialNumber: owner_action_note_builder::IsUnset,
{
pub fn generate_serial_number(
self,
rng: &mut impl FeltRng,
) -> OwnerActionNoteBuilder<owner_action_note_builder::SetSerialNumber<S>> {
self.serial_number(rng.draw_word())
}
}
impl From<OwnerActionNote> for Note {
fn from(note: OwnerActionNote) -> Self {
let metadata = PartialNoteMetadata::new(note.sender, NoteType::Public)
.with_tag(NoteTag::with_account_target(note.account));
let recipient = NoteRecipient::new(
note.serial_number,
OwnerActionNote::script(),
NoteStorage::from(note.action),
);
Note::with_attachments(NoteAssets::default(), metadata, recipient, note.attachments)
}
}
#[cfg(test)]
mod tests {
use miden_protocol::account::AccountType;
use miden_protocol::crypto::rand::RandomCoin;
use super::*;
fn account_id(seed: u8) -> AccountId {
AccountId::builder()
.account_type(AccountType::Public)
.build_with_seed([seed; 32])
}
#[test]
fn builder_builds_owner_action_note() {
let mut rng = RandomCoin::new(Word::empty());
let managed = account_id(1);
let owner = account_id(2);
let new_owner = account_id(3);
let note = OwnerActionNote::builder()
.sender(owner)
.account(managed)
.action(OwnerAction::TransferOwnership { new_owner: Some(new_owner) })
.generate_serial_number(&mut rng)
.build()
.unwrap();
assert_eq!(note.sender(), owner);
assert_eq!(note.account(), managed);
let note = Note::from(note);
assert_eq!(note.metadata().note_type(), NoteType::Public);
assert_eq!(note.metadata().tag(), NoteTag::with_account_target(managed));
assert_eq!(note.assets().num_assets(), 0);
}
#[test]
fn transfer_ownership_storage_layout() {
let new_owner = account_id(3);
let storage =
NoteStorage::from(OwnerAction::TransferOwnership { new_owner: Some(new_owner) });
assert_eq!(
storage.items(),
&[
Felt::from(OwnerAction::SELECTOR_TRANSFER_OWNERSHIP),
new_owner.suffix(),
new_owner.prefix().as_felt(),
]
);
}
#[test]
fn cancel_transfer_ownership_storage_layout() {
let storage = NoteStorage::from(OwnerAction::TransferOwnership { new_owner: None });
assert_eq!(
storage.items(),
&[Felt::from(OwnerAction::SELECTOR_TRANSFER_OWNERSHIP), Felt::ZERO, Felt::ZERO]
);
}
#[test]
fn accept_and_renounce_storage_layout() {
let accept = NoteStorage::from(OwnerAction::AcceptOwnership);
assert_eq!(accept.items(), &[Felt::from(OwnerAction::SELECTOR_ACCEPT_OWNERSHIP)]);
let renounce = NoteStorage::from(OwnerAction::RenounceOwnership);
assert_eq!(renounce.items(), &[Felt::from(OwnerAction::SELECTOR_RENOUNCE_OWNERSHIP)]);
}
}