Playground
doc.gno
// v0 - Unaudited: This is an initial version 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. // // Package ownable provides an ownership pattern for Gno realms, allowing // contracts to restrict access to privileged operations to a designated owner. package ownable
errors.gno
package ownable
import "errors"
var (
ErrUnauthorized = errors.New("ownable: caller is not owner")
ErrInvalidAddress = errors.New("ownable: new owner address is invalid")
)
gnomod.toml
module = "gno.land/p/nt/ownable/v0" gno = "0.9" [addpkg] creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"
ownable.gno
package ownable
import "chain"
const OwnershipTransferEvent = "OwnershipTransfer"
// Ownable is meant to be used as a top-level object to make your contract
// ownable OR being embedded in a Gno object to manage per-object ownership.
// Ownable is safe to export as a top-level object.
//
// Authority-mutating methods (TransferOwnership, DropOwnership) take
// (_ int, rlm realm). The caller threads its own cur; the method
// asserts rlm.IsCurrent() and identifies the principal as
// rlm.Previous().Address() — which must equal the current owner.
//
// o.TransferOwnership(0, cur, newOwner)
// o.DropOwnership(0, cur)
//
// Read methods (OwnedBy, AssertOwnedBy) keep the bare-address shape;
// callers extract the address themselves (e.g. cur.Previous().Address()).
type Ownable struct {
owner address
}
// NewWithAddress creates an Ownable with the given address as owner.
// This is the only constructor — the previous New/NewWithOrigin/
// NewWithAddressByPrevious sugar baked runtime walks and an auth-mode
// flag into the struct; the realm using this package now picks the
// owner address explicitly (e.g. cur.Previous().Address() after
// verifying cur.Previous().IsUserCall() in init).
func NewWithAddress(addr address) *Ownable {
return &Ownable{
owner: addr,
}
}
// OwnedBy reports whether addr is the current owner.
func (o *Ownable) OwnedBy(addr address) bool {
if o == nil {
return false
}
return addr == o.owner
}
// AssertOwnedBy panics with ErrUnauthorized if addr is not the owner.
func (o *Ownable) AssertOwnedBy(addr address) {
if !o.OwnedBy(addr) {
panic(ErrUnauthorized)
}
}
// TransferOwnership transfers ownership of the Ownable to newOwner. rlm
// must be the caller's own captured cur (asserted via rlm.IsCurrent()).
// The principal is rlm.Previous().Address() — the realm that crossed
// into the caller — which must equal the current owner.
//
// IsCurrent + rlm.Previous() makes the principal unforgeable: an
// attacker calling TransferOwnership on a foreign Ownable cannot supply
// an arbitrary caller address; rlm comes from a runtime-validated
// crossing frame.
func (o *Ownable) TransferOwnership(_ int, rlm realm, newOwner address) error {
if !rlm.IsCurrent() {
return ErrUnauthorized
}
caller := rlm.Previous().Address()
if !o.OwnedBy(caller) {
return ErrUnauthorized
}
if !newOwner.IsValid() {
return ErrInvalidAddress
}
prevOwner := o.owner
o.owner = newOwner
chain.Emit(
OwnershipTransferEvent,
"from", prevOwner.String(),
"to", newOwner.String(),
)
return nil
}
// DropOwnership removes the owner, disabling any owner-related actions.
// rlm must be the caller's own captured cur; rlm.Previous().Address()
// must equal the current owner.
func (o *Ownable) DropOwnership(_ int, rlm realm) error {
if !rlm.IsCurrent() {
return ErrUnauthorized
}
caller := rlm.Previous().Address()
if !o.OwnedBy(caller) {
return ErrUnauthorized
}
prevOwner := o.owner
o.owner = ""
chain.Emit(
OwnershipTransferEvent,
"from", prevOwner.String(),
"to", "",
)
return nil
}
// Owner returns the owner address.
func (o *Ownable) Owner() address {
if o == nil {
return address("")
}
return o.owner
}
ownable_test.gno
package ownable
import (
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
"gno.land/p/nt/urequire/v0"
)
var (
alice = testutils.TestAddress("alice")
bob = testutils.TestAddress("bob")
)
func TestNewWithAddress(cur realm, t *testing.T) {
o := NewWithAddress(alice)
got := o.Owner()
uassert.Equal(t, got, alice)
}
func TestTransferOwnership(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
o := NewWithAddress(alice)
func(cur realm) {
err := o.TransferOwnership(0, cur, bob)
urequire.NoError(t, err)
}(cross(cur))
got := o.Owner()
uassert.Equal(t, got, bob)
}
func TestTransferOwnershipUnauthorized(cur realm, t *testing.T) {
o := NewWithAddress(alice)
testing.SetRealm(testing.NewUserRealm(bob))
func(cur realm) {
uassert.ErrorContains(t, o.TransferOwnership(0, cur, bob), ErrUnauthorized.Error())
}(cross(cur))
}
func TestDropOwnership(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
o := NewWithAddress(alice)
func(cur realm) {
err := o.DropOwnership(0, cur)
urequire.NoError(t, err, "DropOwnership failed")
}(cross(cur))
owner := o.Owner()
uassert.Empty(t, owner, "owner should be empty")
}
func TestDropOwnershipUnauthorized(cur realm, t *testing.T) {
o := NewWithAddress(alice)
testing.SetRealm(testing.NewUserRealm(bob))
func(cur realm) {
uassert.ErrorContains(t, o.DropOwnership(0, cur), ErrUnauthorized.Error())
}(cross(cur))
}
func TestOwnedBy(cur realm, t *testing.T) {
o := NewWithAddress(alice)
uassert.True(t, o.OwnedBy(alice))
uassert.False(t, o.OwnedBy(bob))
}
func TestAssertOwnedBy(cur realm, t *testing.T) {
o := NewWithAddress(alice)
// Should not panic.
o.AssertOwnedBy(alice)
uassert.PanicsWithMessage(t, cur, ErrUnauthorized.Error(), func() {
o.AssertOwnedBy(bob)
})
}
func TestErrInvalidAddress(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(alice))
o := NewWithAddress(alice)
func(cur realm) {
err := o.TransferOwnership(0, cur, "")
uassert.ErrorContains(t, err, ErrInvalidAddress.Error())
err = o.TransferOwnership(0, cur, "10000000001000000000100000000010000000001000000000")
uassert.ErrorContains(t, err, ErrInvalidAddress.Error())
}(cross(cur))
}
func TestNilReceiver(cur realm, t *testing.T) {
var o *Ownable
owner := o.Owner()
if owner != address("") {
t.Errorf("expected empty address but got %v", owner)
}
isOwner := o.OwnedBy(alice)
uassert.False(t, isOwner)
defer func() {
r := recover()
if r == nil {
t.Error("expected panic but got none")
}
if r != ErrUnauthorized {
t.Errorf("expected ErrUnauthorized but got %v", r)
}
}()
o.AssertOwnedBy(alice)
}