use alloc::collections::BTreeSet;
use alloc::vec::Vec;
use miden_protocol::account::{Account, AccountCode, AccountId, AccountProcedureRoot};
use crate::AuthMethod;
use crate::account::components::StandardAccountComponent;
use crate::account::interface::{AccountComponentInterface, AccountInterface};
pub trait AccountInterfaceExt {
fn from_code(account_id: AccountId, auth: Vec<AuthMethod>, code: &AccountCode) -> Self;
fn from_account(account: &Account) -> Self;
}
impl AccountInterfaceExt for AccountInterface {
fn from_code(account_id: AccountId, auth: Vec<AuthMethod>, code: &AccountCode) -> Self {
let components = AccountComponentInterface::from_procedures(code.procedures());
Self::new(account_id, auth, components)
}
fn from_account(account: &Account) -> Self {
let components = AccountComponentInterface::from_procedures(account.code().procedures());
let mut auth = Vec::new();
for component in components.iter() {
if component.is_auth_component() {
auth = component.get_auth_methods(account.storage());
break;
}
}
Self::new(account.id(), auth, components)
}
}
pub trait AccountComponentInterfaceExt {
fn from_procedures(procedures: &[AccountProcedureRoot]) -> Vec<AccountComponentInterface>;
}
impl AccountComponentInterfaceExt for AccountComponentInterface {
fn from_procedures(procedures: &[AccountProcedureRoot]) -> Vec<Self> {
let mut component_interface_vec = Vec::new();
let mut procedures = BTreeSet::from_iter(procedures.iter().copied());
StandardAccountComponent::extract_standard_components(
&mut procedures,
&mut component_interface_vec,
);
component_interface_vec
.push(AccountComponentInterface::Custom(procedures.into_iter().collect()));
component_interface_vec
}
}