forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess.rs
More file actions
203 lines (176 loc) · 6.18 KB
/
Copy pathaccess.rs
File metadata and controls
203 lines (176 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use soroban_sdk::{contracterror, panic_with_error, symbol_short, vec, Address, BytesN, Env, Symbol, Vec};
use crate::event_struct::{MOD_CORE, ACT_INIT};
use crate::event_utils::publish_event;
const KEY_INIT: Symbol = symbol_short!("C_INIT");
const KEY_ADMIN: Symbol = symbol_short!("C_ADMIN");
const KEY_OPERATRS: Symbol = symbol_short!("C_OPERS");
const KEY_AUDITORS: Symbol = symbol_short!("C_AUDIT");
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum AccessError {
Unauthorized = 1,
AlreadyInit = 2,
NotInitialized = 3,
InvalidAdmin = 4,
InvalidOperator = 5,
InvalidAuditor = 6,
}
pub const ROLE_ADMIN: u32 = 0;
pub const ROLE_OPERATOR: u32 = 1;
pub const ROLE_AUDITOR: u32 = 2;
pub fn initialize(env: &Env, admin: &Address, operators: Vec<Address>, auditors: Vec<Address>) {
if is_initialized(env) {
panic_with_error!(env, AccessError::AlreadyInit);
}
admin.require_auth();
env.storage().instance().set(&KEY_INIT, &true);
env.storage().instance().set(&KEY_ADMIN, admin);
let mut op_vec: Vec<Address> = vec![env];
for o in operators.iter() {
op_vec.push_back(o);
}
env.storage().instance().set(&KEY_OPERATRS, &op_vec);
let mut au_vec: Vec<Address> = vec![env];
for a in auditors.iter() {
au_vec.push_back(a);
}
env.storage().instance().set(&KEY_AUDITORS, &au_vec);
publish_event(
env,
MOD_CORE | ACT_INIT,
0,
BytesN::from_array(env, &[0u8; 32]),
);
}
pub fn is_initialized(env: &Env) -> bool {
env.storage().instance().has(&KEY_INIT)
}
pub fn require_initialized(env: &Env) {
if !is_initialized(env) {
panic_with_error!(env, AccessError::NotInitialized);
}
}
pub fn require_role(env: &Env, caller: &Address, role: u32) {
caller.require_auth();
require_initialized(env);
let ok = match role {
ROLE_ADMIN => {
let admin: Address = env.storage().instance().get(&KEY_ADMIN)
.unwrap_or_else(|| panic_with_error!(env, AccessError::NotInitialized));
admin == caller.clone()
}
ROLE_OPERATOR => {
let operators: Vec<Address> = env.storage().instance().get(&KEY_OPERATRS)
.unwrap_or_else(|| vec![env]);
operators.contains(caller)
}
ROLE_AUDITOR => {
let auditors: Vec<Address> = env.storage().instance().get(&KEY_AUDITORS)
.unwrap_or_else(|| vec![env]);
auditors.contains(caller)
}
_ => false,
};
if !ok {
panic_with_error!(env, AccessError::Unauthorized);
}
}
pub fn get_admin(env: &Env) -> Option<Address> {
env.storage().instance().get(&KEY_ADMIN)
}
pub fn get_operators(env: &Env) -> Vec<Address> {
env.storage().instance().get(&KEY_OPERATRS).unwrap_or(vec![env])
}
pub fn get_auditors(env: &Env) -> Vec<Address> {
env.storage().instance().get(&KEY_AUDITORS).unwrap_or(vec![env])
}
#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::{testutils::Address as _, vec, Env};
#[soroban_sdk::contract]
pub struct TestContract;
#[soroban_sdk::contractimpl]
impl TestContract {}
fn setup(env: &Env) -> (Address, Address, Vec<Address>, Vec<Address>) {
let admin = Address::generate(env);
let op1 = Address::generate(env);
let au1 = Address::generate(env);
let operators = vec![env, op1.clone()];
let auditors = vec![env, au1.clone()];
(admin, op1, operators, auditors)
}
#[test]
fn initialize_sets_admin() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, TestContract);
let (admin, _, operators, auditors) = setup(&env);
env.as_contract(&contract_id, || {
initialize(&env, &admin, operators, auditors);
assert!(is_initialized(&env));
assert_eq!(get_admin(&env).unwrap(), admin);
});
}
#[test]
#[should_panic]
fn double_init_rejected() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, TestContract);
let (admin, _, operators, auditors) = setup(&env);
env.as_contract(&contract_id, || {
initialize(&env, &admin, operators.clone(), auditors.clone());
initialize(&env, &admin, operators, auditors);
});
}
#[test]
fn admin_role_authorized() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, TestContract);
let (admin, _, operators, auditors) = setup(&env);
env.as_contract(&contract_id, || {
initialize(&env, &admin, operators, auditors);
});
env.as_contract(&contract_id, || {
require_role(&env, &admin, ROLE_ADMIN);
});
}
#[test]
fn operator_role_authorized() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, TestContract);
let (admin, op1, operators, auditors) = setup(&env);
env.as_contract(&contract_id, || {
initialize(&env, &admin, operators, auditors);
require_role(&env, &op1, ROLE_OPERATOR);
});
}
#[test]
#[should_panic]
fn operator_not_admin() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, TestContract);
let (admin, _op1, operators, auditors) = setup(&env);
let rogue = Address::generate(&env);
env.as_contract(&contract_id, || {
initialize(&env, &admin, operators, auditors);
require_role(&env, &rogue, ROLE_ADMIN);
});
}
#[test]
fn auditor_role_authorized() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, TestContract);
let (admin, _, operators, auditors) = setup(&env);
let au1 = auditors.get(0).unwrap();
env.as_contract(&contract_id, || {
initialize(&env, &admin, operators, auditors);
require_role(&env, &au1, ROLE_AUDITOR);
});
}
}