assert.gno
5.66 Kb · 180 lines
1package access
2
3import (
4 "chain"
5
6 prbac "gno.land/p/gnoswap/rbac/v1"
7 ufmt "gno.land/p/nt/ufmt/v0"
8)
9
10// rbacPackagePath is the package path of the RBAC contract
11// Used to verify that role management functions are called only by RBAC
12const rbacPackagePath = "gno.land/r/gnoswap/rbac/v1"
13
14// AssertIsRlmCurrent panics if the realm token is not the current crossing frame.
15//
16// Parameters:
17// - _: leading realm-call discriminator; callers pass 0
18// - rlm: realm context token that must represent the current crossing frame
19func AssertIsRlmCurrent(_ int, rlm realm) {
20 if !rlm.IsCurrent() {
21 panic(errSpoofedRealm)
22 }
23}
24
25// AssertIsAdminOrGovernance panics unless caller is the configured admin or governance address.
26//
27// Parameters:
28// - caller: address whose authorization is checked against the admin and governance roles
29func AssertIsAdminOrGovernance(caller address) {
30 if IsAuthorized(prbac.ROLE_ADMIN.String(), caller) || IsAuthorized(prbac.ROLE_GOVERNANCE.String(), caller) {
31 return
32 }
33
34 panic(ufmt.Errorf(errUnauthorizedAdminOrGov, caller))
35}
36
37// AssertIsAdmin panics unless caller is the configured admin address.
38//
39// Parameters:
40// - caller: address whose authorization is checked against the admin role
41func AssertIsAdmin(caller address) {
42 AssertIsAuthorized(prbac.ROLE_ADMIN.String(), caller)
43}
44
45// AssertIsGovernance panics unless caller is the configured governance address.
46//
47// Parameters:
48// - caller: address whose authorization is checked against the governance role
49func AssertIsGovernance(caller address) {
50 AssertIsAuthorized(prbac.ROLE_GOVERNANCE.String(), caller)
51}
52
53// AssertIsGovStaker panics unless caller is the configured governance-staker address.
54//
55// Parameters:
56// - caller: address whose authorization is checked against the governance-staker role
57func AssertIsGovStaker(caller address) {
58 AssertIsAuthorized(prbac.ROLE_GOV_STAKER.String(), caller)
59}
60
61// AssertIsRouter panics unless caller is the configured router address.
62//
63// Parameters:
64// - caller: address whose authorization is checked against the router role
65func AssertIsRouter(caller address) {
66 AssertIsAuthorized(prbac.ROLE_ROUTER.String(), caller)
67}
68
69// AssertIsPool panics unless caller is the configured pool address.
70//
71// Parameters:
72// - caller: address whose authorization is checked against the pool role
73func AssertIsPool(caller address) {
74 AssertIsAuthorized(prbac.ROLE_POOL.String(), caller)
75}
76
77// AssertIsPosition panics unless caller is the configured position address.
78//
79// Parameters:
80// - caller: address whose authorization is checked against the position role
81func AssertIsPosition(caller address) {
82 AssertIsAuthorized(prbac.ROLE_POSITION.String(), caller)
83}
84
85// AssertIsStaker panics unless caller is the configured staker address.
86//
87// Parameters:
88// - caller: address whose authorization is checked against the staker role
89func AssertIsStaker(caller address) {
90 AssertIsAuthorized(prbac.ROLE_STAKER.String(), caller)
91}
92
93// AssertIsLaunchpad panics unless caller is the configured launchpad address.
94//
95// Parameters:
96// - caller: address whose authorization is checked against the launchpad role
97func AssertIsLaunchpad(caller address) {
98 AssertIsAuthorized(prbac.ROLE_LAUNCHPAD.String(), caller)
99}
100
101// AssertIsEmission panics unless caller is the configured emission address.
102//
103// Parameters:
104// - caller: address whose authorization is checked against the emission role
105func AssertIsEmission(caller address) {
106 AssertIsAuthorized(prbac.ROLE_EMISSION.String(), caller)
107}
108
109// AssertIsProtocolFee panics unless caller is the configured protocol-fee address.
110//
111// Parameters:
112// - caller: address whose authorization is checked against the protocol-fee role
113func AssertIsProtocolFee(caller address) {
114 AssertIsAuthorized(prbac.ROLE_PROTOCOL_FEE.String(), caller)
115}
116
117// AssertIsGovXGNS panics unless caller is the configured xGNS governance address.
118//
119// Parameters:
120// - caller: address whose authorization is checked against the xGNS governance role
121func AssertIsGovXGNS(caller address) {
122 AssertIsAuthorized(prbac.ROLE_XGNS.String(), caller)
123}
124
125// AssertIsAuthorized panics if caller does not have the specified role or if the role is absent.
126//
127// Parameters:
128// - roleName: role identifier whose configured address is required
129// - caller: address that must match the configured address for roleName
130func AssertIsAuthorized(roleName string, caller address) {
131 addr, ok := GetAddress(roleName)
132 if !ok {
133 panic(ufmt.Errorf(errRoleNotFound, roleName))
134 }
135
136 if caller != addr {
137 panic(ufmt.Errorf(errUnauthorized, caller, roleName))
138 }
139}
140
141// AssertHasAnyRole checks roleNames in order and panics unless caller matches one.
142// It panics immediately if a checked role is absent, even if a later role might match.
143//
144// Parameters:
145// - caller: address compared against each configured role address
146// - roleNames: ordered role identifiers to check; each missing role causes a panic
147func AssertHasAnyRole(caller address, roleNames ...string) {
148 for _, roleName := range roleNames {
149 addr, ok := GetAddress(roleName)
150 if !ok {
151 panic(ufmt.Errorf(errRoleNotFound, roleName))
152 }
153
154 if caller == addr {
155 return
156 }
157 }
158
159 panic(ufmt.Errorf(errUnauthorizedAnyRole, caller, roleNames))
160}
161
162// AssertIsValidAddress panics if addr is not a valid address.
163//
164// Parameters:
165// - addr: address value to validate
166func AssertIsValidAddress(addr address) {
167 if !addr.IsValid() {
168 panic(ufmt.Errorf(errInvalidAddressShort, addr))
169 }
170}
171
172// assertIsRBAC panics if the caller is not the RBAC contract.
173// Used internally to protect role management functions.
174func assertIsRBAC(caller address) {
175 rbacAddress := chain.PackageAddress(rbacPackagePath)
176
177 if caller != rbacAddress {
178 panic(ufmt.Errorf(errUnauthorizedRBAC, caller))
179 }
180}