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

README.md

3.24 Kb · 79 lines

v0 - Unaudited This is an initial version of this package that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.

authorizable - Second authorization tier over ownable

Extension of gno.land/p/nt/ownable/v0 that adds a second permission level on top of single-owner ownership: one superuser (the ownable owner) plus a list of authorized addresses. Use it for a moderator tier, an allowlist, or any "owner, plus a set of trusted others" pattern.

Usage

 1package myrealm
 2
 3import (
 4    "chain/runtime"
 5
 6    "gno.land/p/nt/ownable/v0"
 7    "gno.land/p/nt/ownable/v0/exts/authorizable"
 8)
 9
10// The superuser (and first entry on the auth list) is chosen explicitly.
11// Here: the deployer, captured in init.
12var auth *authorizable.Authorizable
13
14func init() {
15    caller := runtime.PreviousRealm()
16    if !caller.IsUserCall() {
17        panic("must be deployed by a user")
18    }
19    auth = authorizable.New(ownable.NewWithAddress(caller.Address()))
20}
21
22// Superuser-only: add a moderator.
23func AddModerator(cur realm, addr address) error {
24    return auth.AddToAuthList(0, cur, addr)
25}
26
27// Gate an action to anyone on the auth list.
28func Moderate(cur realm) {
29    auth.AssertPreviousOnAuthList(0, cur)
30    // ... privileged work ...
31}

API

 1type Authorizable struct {
 2    *ownable.Ownable // the owner is the superuser; all Ownable methods are inherited
 3    // unexported auth list
 4}
 5
 6// New builds an Authorizable from an existing *ownable.Ownable.
 7// The owner is automatically added to the auth list.
 8func New(o *ownable.Ownable) *Authorizable
 9
10// Superuser-only (previous caller must be the owner).
11func (a *Authorizable) AddToAuthList(_ int, rlm realm, addr address) error
12func (a *Authorizable) DeleteFromAuthList(_ int, rlm realm, addr address) error
13
14// Membership checks (return an error; nil means on the list).
15func (a *Authorizable) OnAuthList(_ int, rlm realm) error         // is the caller realm itself on the list
16func (a *Authorizable) PreviousOnAuthList(_ int, rlm realm) error // is the realm/user that crossed in on the list
17
18// Assert variants panic instead of returning an error.
19func (a Authorizable) AssertOnAuthList(_ int, rlm realm)
20func (a Authorizable) AssertPreviousOnAuthList(_ int, rlm realm)
21
22// Errors: ErrNotSuperuser, ErrNotInAuthList, ErrAlreadyInList

Notes

  • Every method takes the caller's own captured cur as rlm and asserts rlm.IsCurrent(), blocking the designation-forgery read where a non-crossing wrapper makes the realm walk return the wrong address. The first _ int argument is an unused placeholder: pass 0.
  • The superuser is authenticated by rlm.Previous().Address() matching the underlying Ownable owner, so AddToAuthList / DeleteFromAuthList succeed only when the owner is the crossing caller. Ownership transfer, renouncing, etc. come from the embedded Ownable.
  • PreviousOnAuthList / AssertPreviousOnAuthList are the user-facing gate: they check the address that crossed into your realm. OnAuthList checks the calling realm itself; use it only when a realm-to-realm caller should be listed directly.
  • The auth list is backed by a bptree, keyed by address string.