templar_curator_primitives/auth/
mod.rs1use templar_vault_kernel::{Address, KernelAction};
10
11#[templar_vault_macros::vault_derive(borsh, serde)]
13#[derive(Clone, Copy, PartialEq, Eq)]
14pub enum AuthPolicyClass {
15 Public,
17 Sentinel,
19 Allocator,
21 AllocatorEmergency,
23 Curator,
25}
26
27#[inline]
29#[must_use]
30pub const fn canonical_policy_class(action: ActionKind) -> AuthPolicyClass {
31 match action {
32 ActionKind::Deposit | ActionKind::RequestWithdraw | ActionKind::AtomicWithdraw => {
33 AuthPolicyClass::Public
34 }
35 ActionKind::ExecuteWithdraw
36 | ActionKind::BeginAllocating
37 | ActionKind::FinishAllocating
38 | ActionKind::SyncExternalAssets
39 | ActionKind::RebalanceWithdraw
40 | ActionKind::BeginRefreshing
41 | ActionKind::FinishRefreshing
42 | ActionKind::SettlePayout
43 | ActionKind::RefreshFees => AuthPolicyClass::Allocator,
44 ActionKind::Pause | ActionKind::SetRestrictions => AuthPolicyClass::Sentinel,
45 ActionKind::AbortAllocating
46 | ActionKind::AbortWithdrawing
47 | ActionKind::AbortRefreshing => AuthPolicyClass::AllocatorEmergency,
48 ActionKind::ManualReconcile | ActionKind::EmergencyReset | ActionKind::PolicyAdmin => {
49 AuthPolicyClass::Curator
50 }
51 }
52}
53
54#[inline]
56#[must_use]
57pub const fn boundary_policy_class(action: ActionKind) -> AuthPolicyClass {
58 match action {
59 ActionKind::Deposit | ActionKind::RequestWithdraw | ActionKind::AtomicWithdraw => {
60 AuthPolicyClass::Public
61 }
62 ActionKind::ExecuteWithdraw
63 | ActionKind::BeginAllocating
64 | ActionKind::FinishAllocating
65 | ActionKind::SyncExternalAssets
66 | ActionKind::RebalanceWithdraw
67 | ActionKind::BeginRefreshing
68 | ActionKind::FinishRefreshing
69 | ActionKind::RefreshFees
70 | ActionKind::SettlePayout => AuthPolicyClass::Allocator,
71 ActionKind::Pause | ActionKind::SetRestrictions => AuthPolicyClass::Sentinel,
72 ActionKind::AbortAllocating
73 | ActionKind::AbortWithdrawing
74 | ActionKind::AbortRefreshing => AuthPolicyClass::AllocatorEmergency,
75 ActionKind::ManualReconcile | ActionKind::EmergencyReset | ActionKind::PolicyAdmin => {
76 AuthPolicyClass::Curator
77 }
78 }
79}
80
81#[templar_vault_macros::vault_derive(borsh, serde)]
83#[derive(Clone, Copy, PartialEq, Eq)]
84pub enum ActionKind {
85 Deposit,
87 RequestWithdraw,
89 ExecuteWithdraw,
91 Pause,
93 SetRestrictions,
95 PolicyAdmin,
97 BeginAllocating,
99 FinishAllocating,
101 SyncExternalAssets,
103 RebalanceWithdraw,
104 BeginRefreshing,
106 FinishRefreshing,
108 AbortAllocating,
110 AbortWithdrawing,
112 AbortRefreshing,
114 SettlePayout,
116 RefreshFees,
118 ManualReconcile,
120 EmergencyReset,
122 AtomicWithdraw,
124}
125
126impl ActionKind {
127 #[inline]
129 #[must_use]
130 pub const fn is_privileged(&self) -> bool {
131 !matches!(canonical_policy_class(*self), AuthPolicyClass::Public)
132 }
133}
134
135impl From<&KernelAction> for ActionKind {
136 #[inline]
137 fn from(action: &KernelAction) -> Self {
138 match action {
139 KernelAction::BeginAllocating { .. } => Self::BeginAllocating,
140 KernelAction::Deposit { .. } => Self::Deposit,
141 KernelAction::AtomicWithdraw { .. } => Self::AtomicWithdraw,
142 KernelAction::RequestWithdraw { .. } => Self::RequestWithdraw,
143 KernelAction::ExecuteWithdraw { .. } => Self::ExecuteWithdraw,
144 KernelAction::BeginRefreshing { .. } => Self::BeginRefreshing,
145 KernelAction::FinishAllocating { .. } => Self::FinishAllocating,
146 KernelAction::SyncExternalAssets { .. } => Self::SyncExternalAssets,
147 KernelAction::RebalanceWithdraw { .. } => Self::RebalanceWithdraw,
148 KernelAction::FinishRefreshing { .. } => Self::FinishRefreshing,
149 KernelAction::AbortRefreshing { .. } => Self::AbortRefreshing,
150 KernelAction::SettlePayout { .. } => Self::SettlePayout,
151 KernelAction::AbortAllocating { .. } => Self::AbortAllocating,
152 KernelAction::AbortWithdrawing { .. } => Self::AbortWithdrawing,
153 KernelAction::RefreshFees { .. } => Self::RefreshFees,
154 KernelAction::Pause { .. } => Self::Pause,
155 KernelAction::EmergencyReset => Self::EmergencyReset,
156 }
157 }
158}
159
160impl From<KernelAction> for ActionKind {
161 #[inline]
162 fn from(action: KernelAction) -> Self {
163 Self::from(&action)
164 }
165}
166
167#[templar_vault_macros::vault_derive]
168#[derive(Clone, Copy, PartialEq, Eq)]
169pub enum Caller {
170 Admin,
171 Curator,
172 Sentinel,
173 Allocator,
174 User,
175}
176
177#[templar_vault_macros::vault_derive]
178#[derive(Clone, PartialEq, Eq)]
179pub enum AuthError {
180 NotAuthorized { caller: Caller, action: ActionKind },
181 InvalidProof,
182 MissingRole,
183 VaultPaused,
184}
185
186pub type AuthResult<T> = Result<T, AuthError>;
188
189pub trait AuthAdapter {
194 fn authorize(
196 &self,
197 action: ActionKind,
198 caller: Address,
199 proof: Option<&[u8]>,
200 ) -> AuthResult<()>;
201
202 fn is_paused(&self) -> bool;
204}