Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

roles.gno

1.40 Kb · 48 lines
 1package gnofly
 2
 3import "gno.land/r/g1t2kg2vtr3fukg43eujkn6x53gfdyakhngt4sfd/gnofly/game/v0"
 4
 5// The market keeps no roles of its own: its admins are the game realm's,
 6// asked again at every call, as the planes and skins realms do.
 7
 8// authority is the one question this realm asks about roles.
 9type authority interface {
10	IsAdmin(addr address) bool
11}
12
13// gameRoles answers with the roles of the game realm.
14type gameRoles struct{}
15
16func (gameRoles) IsAdmin(addr address) bool { return game.IsAdmin(addr) }
17
18// roles is the authority of this realm. It is a variable only so that the
19// tests can answer for the game realm, which has no admin under `gno test`.
20var roles authority = gameRoles{}
21
22// callerOf returns the verified caller of a crossing function; rlm must be
23// its live cur.
24func callerOf(_ int, rlm realm) address {
25	if !rlm.IsCurrent() {
26		panic(errSpoofedRealm)
27	}
28	return rlm.Previous().Address()
29}
30
31// userOf returns the caller, who must be a wallet calling directly: never a
32// realm, so that no contract can act in a user's name or wrap a purchase.
33func userOf(_ int, rlm realm) address {
34	caller := callerOf(0, rlm)
35	if !rlm.Previous().IsUserCall() {
36		panic(errNotUserCall)
37	}
38	return caller
39}
40
41// assertAdmin panics unless the caller is an admin of the game realm.
42func assertAdmin(_ int, rlm realm) address {
43	caller := callerOf(0, rlm)
44	if !roles.IsAdmin(caller) {
45		panic(errNotAdmin)
46	}
47	return caller
48}