Skip to main content

radix_native_sdk/modules/role_assignment/
role_assignment.rs

1use radix_common::constants::ROLE_ASSIGNMENT_MODULE_PACKAGE;
2use radix_common::data::scrypto::model::Own;
3use radix_common::data::scrypto::*;
4use radix_engine_interface::api::object_api::ModuleId;
5use radix_engine_interface::api::*;
6use radix_engine_interface::blueprints::resource::{
7    AccessRule, OwnerRoleEntry, RoleAssignmentInit, RoleKey,
8};
9use radix_engine_interface::object_modules::role_assignment::*;
10use radix_engine_interface::types::NodeId;
11use sbor::rust::prelude::*;
12
13pub struct RoleAssignment(pub Own);
14
15impl RoleAssignment {
16    pub fn create<Y: SystemApi<E>, E: SystemApiError, R: Into<OwnerRoleEntry>>(
17        owner_role: R,
18        roles: IndexMap<ModuleId, RoleAssignmentInit>,
19        api: &mut Y,
20    ) -> Result<Self, E> {
21        let rtn = api.call_function(
22            ROLE_ASSIGNMENT_MODULE_PACKAGE,
23            ROLE_ASSIGNMENT_BLUEPRINT,
24            ROLE_ASSIGNMENT_CREATE_IDENT,
25            scrypto_encode(&RoleAssignmentCreateInput {
26                owner_role: owner_role.into(),
27                roles,
28            })
29            .unwrap(),
30        )?;
31
32        let role_assignment: Own = scrypto_decode(&rtn).unwrap();
33
34        Ok(Self(role_assignment))
35    }
36}
37
38impl RoleAssignmentObject for RoleAssignment {
39    fn self_id(&self) -> (&NodeId, Option<AttachedModuleId>) {
40        (&self.0 .0, None)
41    }
42}
43
44pub struct AttachedRoleAssignment(pub NodeId);
45
46impl RoleAssignmentObject for AttachedRoleAssignment {
47    fn self_id(&self) -> (&NodeId, Option<AttachedModuleId>) {
48        (&self.0, Some(AttachedModuleId::RoleAssignment))
49    }
50}
51
52pub trait RoleAssignmentObject {
53    fn self_id(&self) -> (&NodeId, Option<AttachedModuleId>);
54
55    fn set_owner_role<Y: SystemApi<E>, E: SystemApiError, A: Into<AccessRule>>(
56        &self,
57        rule: A,
58        api: &mut Y,
59    ) -> Result<(), E> {
60        let (node_id, module_id) = self.self_id();
61        match module_id {
62            None => {
63                api.call_method(
64                    node_id,
65                    ROLE_ASSIGNMENT_SET_OWNER_IDENT,
66                    scrypto_encode(&RoleAssignmentSetOwnerInput { rule: rule.into() }).unwrap(),
67                )?;
68            }
69            Some(module_id) => {
70                api.call_module_method(
71                    node_id,
72                    module_id,
73                    ROLE_ASSIGNMENT_SET_OWNER_IDENT,
74                    scrypto_encode(&RoleAssignmentSetOwnerInput { rule: rule.into() }).unwrap(),
75                )?;
76            }
77        }
78
79        Ok(())
80    }
81
82    fn lock_owner_role<Y: SystemApi<E>, E: SystemApiError>(&self, api: &mut Y) -> Result<(), E> {
83        let (node_id, module_id) = self.self_id();
84        match module_id {
85            None => {
86                api.call_method(
87                    node_id,
88                    ROLE_ASSIGNMENT_LOCK_OWNER_IDENT,
89                    scrypto_encode(&RoleAssignmentLockOwnerInput {}).unwrap(),
90                )?;
91            }
92            Some(module_id) => {
93                api.call_module_method(
94                    node_id,
95                    module_id,
96                    ROLE_ASSIGNMENT_LOCK_OWNER_IDENT,
97                    scrypto_encode(&RoleAssignmentLockOwnerInput {}).unwrap(),
98                )?;
99            }
100        }
101
102        Ok(())
103    }
104
105    fn set_role<Y: SystemApi<E>, E: SystemApiError, R: Into<RoleKey>, A: Into<AccessRule>>(
106        &self,
107        module: ModuleId,
108        role_key: R,
109        rule: A,
110        api: &mut Y,
111    ) -> Result<(), E> {
112        let (node_id, module_id) = self.self_id();
113        match module_id {
114            None => {
115                api.call_method(
116                    node_id,
117                    ROLE_ASSIGNMENT_SET_IDENT,
118                    scrypto_encode(&RoleAssignmentSetInput {
119                        module,
120                        role_key: role_key.into(),
121                        rule: rule.into(),
122                    })
123                    .unwrap(),
124                )?;
125            }
126            Some(module_id) => {
127                api.call_module_method(
128                    node_id,
129                    module_id,
130                    ROLE_ASSIGNMENT_SET_IDENT,
131                    scrypto_encode(&RoleAssignmentSetInput {
132                        module,
133                        role_key: role_key.into(),
134                        rule: rule.into(),
135                    })
136                    .unwrap(),
137                )?;
138            }
139        }
140
141        Ok(())
142    }
143
144    fn get_role<Y: SystemApi<E>, E: SystemApiError, R: Into<RoleKey>>(
145        &self,
146        module: ModuleId,
147        role_key: R,
148        api: &mut Y,
149    ) -> Result<RoleAssignmentGetOutput, E> {
150        let (node_id, module_id) = self.self_id();
151        match module_id {
152            None => api
153                .call_method(
154                    node_id,
155                    ROLE_ASSIGNMENT_GET_IDENT,
156                    scrypto_encode(&RoleAssignmentGetInput {
157                        module,
158                        role_key: role_key.into(),
159                    })
160                    .unwrap(),
161                )
162                .map(|response| scrypto_decode(&response).unwrap()),
163            Some(module_id) => api
164                .call_module_method(
165                    node_id,
166                    module_id,
167                    ROLE_ASSIGNMENT_GET_IDENT,
168                    scrypto_encode(&RoleAssignmentGetInput {
169                        module,
170                        role_key: role_key.into(),
171                    })
172                    .unwrap(),
173                )
174                .map(|response| scrypto_decode(&response).unwrap()),
175        }
176    }
177
178    fn get_owner_role<Y: SystemApi<E>, E: SystemApiError, R: Into<RoleKey>>(
179        &self,
180        api: &mut Y,
181    ) -> Result<RoleAssignmentGetOwnerRoleOutput, E> {
182        let (node_id, module_id) = self.self_id();
183        match module_id {
184            None => api
185                .call_method(
186                    node_id,
187                    ROLE_ASSIGNMENT_GET_OWNER_ROLE_IDENT,
188                    scrypto_encode(&RoleAssignmentGetOwnerRoleInput).unwrap(),
189                )
190                .map(|response| scrypto_decode(&response).unwrap()),
191            Some(module_id) => api
192                .call_module_method(
193                    node_id,
194                    module_id,
195                    ROLE_ASSIGNMENT_GET_OWNER_ROLE_IDENT,
196                    scrypto_encode(&RoleAssignmentGetOwnerRoleInput).unwrap(),
197                )
198                .map(|response| scrypto_decode(&response).unwrap()),
199        }
200    }
201}