api3_common/
whitelist.rs

1#![allow(unused)]
2use crate::abi::{Token, U256};
3use crate::Error;
4use crate::{ensure, keccak_packed, AccessControlRegistryAdminnedWithManager, RoleDeriver};
5use crate::{Bytes32, Zero};
6
7/// Trait that implements temporary and permanent whitelists for
8/// multiple services identified with a hash
9///
10/// This trait implements two kinds of whitelisting:
11///   (1) Temporary, ends when the expiration timestamp is in the past
12///   (2) Indefinite, ends when the indefinite whitelist count is zero
13/// Multiple senders can indefinitely whitelist/unwhitelist independently. The
14/// user will be considered whitelisted as long as there is at least one active
15/// indefinite whitelisting.
16///
17/// The interface of this contract is not implemented. It should be
18/// inherited and its functions should be exposed with a sort of an
19/// authorization scheme.
20pub trait Whitelist {
21    /// The address type for the chain
22    type Address: AsRef<[u8]> + Zero;
23
24    /// Returns if the user is whitelised to use the service
25    ///
26    /// # Argument
27    ///
28    /// * `service_id` Service ID
29    /// * `user` User address
30    fn user_is_whitelisted(&self, service_id: &Bytes32, user: &Self::Address) -> bool;
31
32    /// Extends the expiration of the temporary whitelist of the user
33    /// for the service
34    ///
35    /// # Argument
36    ///
37    /// * `service_id` Service ID
38    /// * `user` User address
39    /// * `expiration_timestamp` Timestamp at which the temporary whitelist will expire
40    fn extend_whitelist_expiration(
41        &mut self,
42        service_id: &Bytes32,
43        user: &Self::Address,
44        expiration_timestamp: u64,
45    );
46
47    /// Sets the expiration of the temporary whitelist of `user` to be
48    /// able to use the service with `serviceId` if the sender has the
49    /// whitelist expiration setter role
50    ///
51    /// # Argument
52    ///
53    /// * `service_id` Service ID
54    /// * `user` User address
55    /// * `expiration_timestamp` Timestamp at which the temporary whitelist will expire
56    fn set_whitelist_expiration(
57        &mut self,
58        service_id: &Bytes32,
59        user: &Self::Address,
60        expiration_timestamp: u64,
61    );
62
63    /// Sets the indefinite whitelist status of `user` to be able to
64    /// use the service with `serviceId` if the sender has the indefinite
65    /// whitelister role
66    ///
67    /// # Argument
68    ///
69    /// `service_id` Service ID
70    /// `user` User address
71    /// `status` Indefinite whitelist status
72    fn set_indefinite_whitelist_status(
73        &mut self,
74        service_id: &Bytes32,
75        user: &Self::Address,
76        status: bool,
77    ) -> U256;
78
79    /// Revokes the indefinite whitelist status granted to the user for
80    /// the service by a specific account
81    ///
82    /// # Argument
83    ///
84    /// `service_id` Service ID
85    /// `user` User address
86    /// `setter` Setter of the indefinite whitelist status
87    fn revoke_indefinite_whitelist_status(
88        &mut self,
89        service_id: &Bytes32,
90        user: &Self::Address,
91        setter: &Self::Address,
92    ) -> (bool, U256);
93}
94
95pub trait WhitelistRoles {
96    fn whitelist_expiration_extender_role_description() -> String {
97        String::from("Whitelist expiration extender")
98    }
99
100    fn whitelist_expiration_setter_role_description() -> String {
101        String::from("Whitelist expiration setter")
102    }
103
104    fn indefinite_whitelister_role_description() -> String {
105        String::from("Indefinite whitelister")
106    }
107}
108
109pub trait WhitelistRolesWithManager:
110    WhitelistRoles + AccessControlRegistryAdminnedWithManager
111{
112    /// Returns if the account has the whitelist expiration extender role
113    /// or is the manager
114    ///
115    /// # Arguments
116    ///
117    /// * `account` Account address
118    fn has_whitelist_expiration_extender_role_or_is_manager(&self, account: &Self::Address) -> bool;
119
120    /// Returns if the account has the indefinite whitelister role or is the
121    /// manager
122    ///
123    /// # Arguments
124    ///
125    /// * `account` Account address
126    fn has_indefinite_whitelister_role_or_is_manager(&self, account: &Self::Address) -> bool;
127
128    /// Returns if the account has the whitelist expriation setter role or
129    /// is the manager
130    ///
131    /// # Arguments
132    ///
133    /// * `account` Account address
134    fn has_whitelist_expiration_setter_role_or_is_manager(&self, account: &Self::Address) -> bool;
135
136    fn whitelist_expiration_extender_role(&self) -> Bytes32 {
137        RoleDeriver::derive_role(
138            self.admin_role(),
139            Self::whitelist_expiration_extender_role_description(),
140        )
141    }
142
143    fn whitelist_expiration_setter_role(&self) -> Bytes32 {
144        RoleDeriver::derive_role(
145            self.admin_role(),
146            Self::whitelist_expiration_setter_role_description(),
147        )
148    }
149
150    fn indefinite_whitelister_role(&self) -> Bytes32 {
151        RoleDeriver::derive_role(
152            self.admin_role(),
153            Self::indefinite_whitelister_role_description(),
154        )
155    }
156}
157
158/// Whitelist contract that is controlled by a manager
159pub trait WhitelistWithManager: Whitelist + WhitelistRolesWithManager {
160    /// Extends the expiration of the temporary whitelist of `user` to
161    /// be able to use the service with `service_id` if the sender has the
162    /// whitelist expiration extender role
163    ///
164    /// # Arguments
165    ///
166    /// * `service_id` Service ID
167    /// * `user` User address
168    /// * `expiration_timestamp` Timestamp at which the temporary whitelist will expire
169    fn extend_whitelist_expiration(
170        &mut self,
171        service_id: &Bytes32,
172        user: &<Self as Whitelist>::Address,
173        expiration_timestamp: u64,
174    );
175
176    /// Sets the expiration of the temporary whitelist of `user` to be
177    /// able to use the service with `service_id` if the sender has the
178    /// whitelist expiration setter role
179    ///
180    /// # Arguments
181    ///
182    /// * `service_id` Service ID
183    /// * `user` User address
184    /// * `expiration_timestamp` Timestamp at which the temporary whitelist will expire
185    fn set_whitelist_expiration(
186        &mut self,
187        service_id: &Bytes32,
188        user: &<Self as Whitelist>::Address,
189        expiration_timestamp: u64,
190    );
191
192    /// Sets the indefinite whitelist status of `user` to be able to
193    /// use the service with `service_id` if the sender has the indefinite whitelister role
194    ///
195    /// # Arguments
196    ///
197    /// * `service_id` Service ID
198    /// * `user` User address
199    /// * `status` Indefinite whitelist status
200    fn set_indefinite_whitelist_status(
201        &mut self,
202        service_id: &Bytes32,
203        user: &<Self as Whitelist>::Address,
204        status: bool,
205    ) -> U256;
206
207    /// Revokes the indefinite whitelist status granted to the user for
208    /// the service by a specific account
209    ///
210    /// # Arguments
211    ///
212    /// * `service_id` Service ID
213    /// * `user` User address
214    /// * `setter` Setter address
215    fn revoke_indefinite_whitelist_status(
216        &mut self,
217        service_id: &Bytes32,
218        user: &<Self as Whitelist>::Address,
219        setter: &<Self as Whitelist>::Address,
220    ) -> (bool, U256);
221}