Module 0x1::acl
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.
- Struct
ACL - Constants
- Function
empty - Function
add - Function
remove - Function
contains - Function
assert_contains - Specification
use 0x1::error;
use 0x1::vector;
Struct ACL
struct ACL has copy, drop, store
Fields
-
list: vector<address>
Constants
The ACL already contains the address.
const ECONTAIN: u64 = 0;
The ACL does not contain the address.
const ENOT_CONTAIN: u64 = 1;
Function empty
Return an empty ACL.
public fun empty(): acl::ACL
Implementation
public fun empty(): ACL {
ACL{ list: vector::empty<address>() }
}
Function add
Add the address to the ACL.
public fun add(self: &mut acl::ACL, addr: address)
Implementation
public fun add(self: &mut ACL, addr: address) {
assert!(!self.list.contains(&addr), error::invalid_argument(ECONTAIN));
self.list.push_back(addr);
}
Function remove
Remove the address from the ACL.
public fun remove(self: &mut acl::ACL, addr: address)
Implementation
public fun remove(self: &mut ACL, addr: address) {
let (found, index) = self.list.index_of(&addr);
assert!(found, error::invalid_argument(ENOT_CONTAIN));
self.list.remove(index);
}
Function contains
Return true iff the ACL contains the address.
public fun contains(self: &acl::ACL, addr: address): bool
Function assert_contains
assert! that the ACL has the address.
public fun assert_contains(self: &acl::ACL, addr: address)
Implementation
public fun assert_contains(self: &ACL, addr: address) {
assert!(self.contains(addr), error::invalid_argument(ENOT_CONTAIN));
}
Specification
Struct ACL
struct ACL has copy, drop, store
-
list: vector<address>
invariant forall i in 0..len(list), j in 0..len(list): list[i] == list[j] ==> i == j;
fun spec_contains(self: ACL, addr: address): bool {
exists a in self.list: a == addr
}
Function add
public fun add(self: &mut acl::ACL, addr: address)
aborts_if spec_contains(self, addr) with error::INVALID_ARGUMENT;
ensures spec_contains(self, addr);
Function remove
public fun remove(self: &mut acl::ACL, addr: address)
aborts_if !spec_contains(self, addr) with error::INVALID_ARGUMENT;
ensures !spec_contains(self, addr);
Function contains
public fun contains(self: &acl::ACL, addr: address): bool
ensures result == spec_contains(self, addr);
Function assert_contains
public fun assert_contains(self: &acl::ACL, addr: address)
aborts_if !spec_contains(self, addr) with error::INVALID_ARGUMENT;