This repository was archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 696
/
Copy pathacl.move
46 lines (38 loc) · 1.53 KB
/
acl.move
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
/// Access control list (ACL) module. An ACL is a list of account addresses who
/// have the access permission to a certain object.
/// This module uses a `vector` to represent the list, but can be refactored to
/// use a "set" instead when it's available in the language in the future.
module std::acl {
use std::vector;
use std::errors;
/// The ACL already contains the address.
const ECONTAIN: u64 = 0;
/// The ACL does not contain the address.
const ENOT_CONTAIN: u64 = 1;
struct ACL has store, drop, copy {
list: vector<address>
}
/// Return an empty ACL.
public fun empty(): ACL {
ACL{ list: vector::empty<address>() }
}
/// Add the address to the ACL.
public fun add(acl: &mut ACL, addr: address) {
assert!(!vector::contains(&mut acl.list, &addr), errors::invalid_argument(ECONTAIN));
vector::push_back(&mut acl.list, addr);
}
/// Remove the address from the ACL.
public fun remove(acl: &mut ACL, addr: address) {
let (found, index) = vector::index_of(&mut acl.list, &addr);
assert!(found, errors::invalid_argument(ENOT_CONTAIN));
vector::remove(&mut acl.list, index);
}
/// Return true iff the ACL contains the address.
public fun contains(acl: &ACL, addr: address): bool {
vector::contains(&acl.list, &addr)
}
/// assert! that the ACL has the address.
public fun assert_contains(acl: &ACL, addr: address) {
assert!(contains(acl, addr), errors::invalid_argument(ENOT_CONTAIN));
}
}