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

access.gno

4.45 Kb · 123 lines
  1package access
  2
  3import "gno.land/p/onbloc/access/manager"
  4
  5var accessState manager.State
  6
  7// RelayerRole mirrors Union deployer's RELAYER role id.
  8// Reference:
  9// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/deployer/src/main.rs#L65
 10const RelayerRole manager.RoleId = 1
 11
 12// GrantRole grants role membership to account through the shared access realm.
 13// Union reference:
 14// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L69-L87
 15func GrantRole(cur realm, role manager.RoleId, account address) bool {
 16	assertCanAdminRole(0, cur, role)
 17
 18	newMember := accessState.GrantRole(role, account)
 19	access := accessState.Roles[role].Members[account]
 20
 21	emitRoleGrantedEvent(role, account, access.Since, newMember)
 22
 23	return newMember
 24}
 25
 26// RevokeRole revokes role membership from account through the shared access realm.
 27// Union reference:
 28// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L89-L100
 29func RevokeRole(cur realm, role manager.RoleId, account address) bool {
 30	assertCanAdminRole(0, cur, role)
 31
 32	revoked := accessState.RevokeRole(role, account)
 33	if revoked {
 34		emitRoleRevokedEvent(role, account)
 35	}
 36
 37	return revoked
 38}
 39
 40// RenounceRole revokes the caller's own role membership after confirmation.
 41// Union reference:
 42// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L102-L115
 43func RenounceRole(cur realm, role manager.RoleId, callerConfirmation address) bool {
 44	account := cur.Previous().Address()
 45
 46	revoked := accessState.RenounceRole(role, account, callerConfirmation)
 47	if revoked {
 48		emitRoleRevokedEvent(role, account)
 49	}
 50
 51	return revoked
 52}
 53
 54// LabelRole emits role label metadata for an unlocked role.
 55// Union reference:
 56// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L48-L67
 57func LabelRole(cur realm, role manager.RoleId, label string) {
 58	assertCanManageTarget(0, cur)
 59
 60	manager.RequireUnlockedConfigRole(role)
 61
 62	emitRoleLabelEvent(role, label)
 63}
 64
 65// SetRoleAdmin sets the admin role that can grant or revoke role.
 66// Union reference:
 67// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L117-L126
 68func SetRoleAdmin(cur realm, role manager.RoleId, admin manager.RoleId) {
 69	assertCanManageTarget(0, cur)
 70
 71	accessState.SetRoleAdmin(role, admin)
 72
 73	emitRoleAdminChangedEvent(role, admin)
 74}
 75
 76// SetGrantDelay sets the delay before future grants of role become active.
 77// Union reference:
 78// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L139-L148
 79func SetGrantDelay(cur realm, role manager.RoleId, delay manager.Delay) {
 80	assertCanManageTarget(0, cur)
 81
 82	accessState.SetGrantDelay(role, delay)
 83
 84	emitRoleGrantDelayChangedEvent(role, delay, manager.CurrentTimePoint())
 85}
 86
 87// SetFunctionRole sets the role for selector on the calling realm's target path.
 88// Union reference:
 89// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L353-L367
 90func SetFunctionRole(cur realm, selector manager.Selector, role manager.RoleId) {
 91	assertCanManageTarget(0, cur)
 92
 93	target := cur.Previous().PkgPath()
 94	accessState.SetTargetFunctionRole(target, selector, role)
 95
 96	emitTargetFunctionRoleUpdatedEvent(target, selector, role)
 97}
 98
 99// SetFunctionRoles sets the same role for selectors on the calling realm's target path.
100// Union reference:
101// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L353-L367
102func SetFunctionRoles(cur realm, selectors []manager.Selector, role manager.RoleId) {
103	assertCanManageTarget(0, cur)
104
105	target := cur.Previous().PkgPath()
106	accessState.SetTargetFunctionRoles(target, selectors, role)
107
108	for i := range selectors {
109		emitTargetFunctionRoleUpdatedEvent(target, selectors[i], role)
110	}
111}
112
113// SetTargetClosed sets whether the calling realm's target path is closed.
114// Union reference:
115// https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L450-L483
116func SetTargetClosed(cur realm, closed bool) {
117	assertCanManageTarget(0, cur)
118
119	target := cur.Previous().PkgPath()
120	accessState.SetTargetClosed(target, closed)
121
122	emitTargetClosedEvent(target, closed)
123}