config.gno

2.61 Kb ยท 77 lines
 1package config
 2
 3import (
 4	"errors"
 5	"std"
 6
 7	"gno.land/p/moul/authz"
 8)
 9
10var Authorizer = authz.NewWithOrigin()
11
12// AddManager adds a new address to the list of authorized managers.
13// This only works if the current authority is a MemberAuthority.
14// The caller must be authorized by the current authority.
15func AddManager(cur realm, addr std.Address) error {
16	caller := std.PreviousRealm().Address()
17	memberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)
18	if !ok {
19		return errors.New("current authority is not a MemberAuthority, cannot add manager directly")
20	}
21	// Use the MemberAuthority's specific AddMember method,
22	// which internally performs the authorization check.
23	return memberAuth.AddMember(caller, addr)
24}
25
26// RemoveManager removes an address from the list of authorized managers.
27// This only works if the current authority is a MemberAuthority.
28// The caller must be authorized by the current authority.
29func RemoveManager(cur realm, addr std.Address) error {
30	caller := std.PreviousRealm().Address()
31	memberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)
32	if !ok {
33		return errors.New("current authority is not a MemberAuthority, cannot remove manager directly")
34	}
35	// Use the MemberAuthority's specific RemoveMember method,
36	// which internally performs the authorization check.
37	return memberAuth.RemoveMember(caller, addr)
38}
39
40// TransferManagement transfers the authority to manage keys to a new authority.
41// The caller must be authorized by the current authority.
42func TransferManagement(cur realm, newAuthority authz.Authority) error {
43	caller := std.PreviousRealm().Address()
44	if newAuthority == nil {
45		return errors.New("new authority cannot be nil")
46	}
47	// Use the Authorizer's Transfer method, which handles the authorization check.
48	return Authorizer.Transfer(caller, newAuthority)
49}
50
51// ListManagers returns a slice of all managed keys.
52func ListManagers(cur realm) []std.Address {
53	var keyList []std.Address
54	memberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)
55	if !ok {
56		return keyList
57	}
58	tree := memberAuth.Tree()
59	if !ok || tree == nil {
60		return keyList // Return empty list if tree is not as expected or nil
61	}
62	tree.Iterate("", "", func(key string, _ any) bool {
63		keyList = append(keyList, std.Address(key))
64		return false
65	})
66	return keyList
67}
68
69func HasManager(cur realm, addr std.Address) bool {
70	memberAuth, ok := Authorizer.Authority().(*authz.MemberAuthority)
71	if !ok {
72		return false // Return false if not a MemberAuthority or doesn't exist
73	}
74	// Use the MemberAuthority's specific RemoveMember method,
75	// which internally performs the authorization check.
76	return memberAuth.Has(addr)
77}