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

config.gno

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