ownership.gno
2.42 Kb · 93 lines
1package rbac
2
3import (
4 ufmt "gno.land/p/nt/ufmt/v0"
5
6 prbac "gno.land/p/gnoswap/rbac/v1"
7 "gno.land/r/gnoswap/access/v1"
8)
9
10// IsOwner reports whether addr is the current owner address.
11//
12// Parameters:
13// - addr: address to compare with the configured owner
14//
15// Returns:
16// - owner: true when addr equals the current owner; false otherwise
17func IsOwner(addr address) bool {
18 return manager.Owner() == addr
19}
20
21// IsPendingOwner reports whether addr is the address awaiting ownership acceptance.
22//
23// Parameters:
24// - addr: address to compare with the configured pending owner
25//
26// Returns:
27// - pending: true when addr equals the pending owner; false otherwise
28func IsPendingOwner(addr address) bool {
29 return manager.PendingOwner() == addr
30}
31
32// GetOwner returns the currently configured owner address.
33//
34// Returns:
35// - owner: current owner address
36func GetOwner() address {
37 return manager.Owner()
38}
39
40// GetPendingOwner returns the address awaiting ownership acceptance.
41//
42// Returns:
43// - pendingOwner: pending owner address, or the zero address when no transfer is pending
44func GetPendingOwner() address {
45 return manager.PendingOwner()
46}
47
48// AcceptOwnership completes a pending ownership transfer and synchronizes the
49// admin role with the new owner.
50//
51// Parameters:
52// - cur: current realm context; callers use cross(cur) when crossing into this realm
53//
54// Only callable by the pending owner.
55func AcceptOwnership(cur realm) {
56 caller := cur.Previous().Address()
57 assertIsPendingOwner(caller)
58
59 err := manager.AcceptOwnershipBy(caller)
60 if err != nil {
61 panic(err)
62 }
63
64 newOwner := manager.Owner()
65 err = manager.UpdateRoleAddress(prbac.ROLE_ADMIN.String(), newOwner)
66 if err != nil {
67 panic(makeErrorWithDetails(
68 err.Error(),
69 ufmt.Sprintf(
70 "role name: %s, address: %s",
71 prbac.ROLE_ADMIN.String(), newOwner.String()),
72 ))
73 }
74 access.SetRoleAddress(cross(cur), prbac.ROLE_ADMIN.String(), newOwner)
75}
76
77// TransferOwnership starts a two-step ownership transfer to addr.
78//
79// Parameters:
80// - cur: current realm context; callers use cross(cur) when crossing into this realm
81// - addr: valid address that will become pending owner and may later accept ownership
82//
83// Only callable by the current owner.
84func TransferOwnership(cur realm, addr address) {
85 caller := cur.Previous().Address()
86 assertIsOwner(caller)
87 assertIsValidAddress(addr)
88
89 err := manager.TransferOwnershipBy(addr, caller)
90 if err != nil {
91 panic(err)
92 }
93}