miden_standards/account/policies/transfer/
basic_allowlist.rs1use alloc::collections::BTreeSet;
2
3use miden_protocol::account::component::{
4 AccountComponentCode,
5 AccountComponentMetadata,
6 StorageSchema,
7};
8use miden_protocol::account::{AccountComponent, AccountId, AccountProcedureRoot};
9
10use crate::account::account_component_code;
11use crate::account::policies::transfer::allowlist::AllowlistStorage;
12use crate::procedure_root;
13
14account_component_code!(
18 BASIC_ALLOWLIST_TRANSFER_POLICY_CODE,
19 "miden-standards-faucets-policies-transfer-basic-allowlist.masp"
20);
21
22const BASIC_ALLOWLIST_LIBRARY_PATH: &str =
28 "miden::standards::components::faucets::policies::transfer::basic_allowlist";
29
30procedure_root!(
31 BASIC_ALLOWLIST_TRANSFER_POLICY_ROOT,
32 BASIC_ALLOWLIST_LIBRARY_PATH,
33 BasicAllowlist::PROC_NAME,
34 BasicAllowlist::code()
35);
36
37#[derive(Debug, Clone, Default)]
51pub struct BasicAllowlist(AllowlistStorage);
52
53impl BasicAllowlist {
54 pub const NAME: &'static str = "miden::standards::faucets::policies::transfer::basic_allowlist";
56
57 pub(crate) const PROC_NAME: &str = "check_policy";
58
59 pub fn with_allowed_accounts<I>(allowed_accounts: I) -> Self
61 where
62 I: IntoIterator<Item = AccountId>,
63 {
64 Self(AllowlistStorage::with_allowed_accounts(allowed_accounts))
65 }
66
67 pub fn allowed_accounts(&self) -> &BTreeSet<AccountId> {
69 self.0.allowed_accounts()
70 }
71
72 pub fn code() -> &'static AccountComponentCode {
74 &BASIC_ALLOWLIST_TRANSFER_POLICY_CODE
75 }
76
77 pub fn root() -> AccountProcedureRoot {
79 *BASIC_ALLOWLIST_TRANSFER_POLICY_ROOT
80 }
81}
82
83impl From<AllowlistStorage> for BasicAllowlist {
84 fn from(storage: AllowlistStorage) -> Self {
85 Self(storage)
86 }
87}
88
89impl From<BasicAllowlist> for AccountComponent {
90 fn from(allowlist: BasicAllowlist) -> Self {
91 let storage_schema = StorageSchema::new([AllowlistStorage::allowed_accounts_slot_schema()])
92 .expect("storage schema should be valid");
93
94 let metadata = AccountComponentMetadata::new(BasicAllowlist::NAME)
95 .with_description(
96 "Basic allowlist transfer policy: predicate procedure plus the `allowed_accounts` \
97 storage map it reads",
98 )
99 .with_storage_schema(storage_schema);
100
101 AccountComponent::new(BasicAllowlist::code().clone(), vec![allowlist.0.into_slot()], metadata)
102 .expect(
103 "basic allowlist transfer policy component should satisfy the requirements of a valid account component",
104 )
105 }
106}