cosmos-sdk/stack/helperware.go

63 lines
1.7 KiB
Go
Raw Normal View History

2017-07-06 20:37:45 -07:00
//nolint
2017-06-29 11:02:38 -07:00
package stack
import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors"
2017-07-06 05:23:38 -07:00
"github.com/tendermint/basecoin/state"
2017-06-29 11:02:38 -07:00
)
const (
2017-07-12 07:53:07 -07:00
NameCheck = "check"
NameGrant = "grant"
2017-06-29 11:02:38 -07:00
)
// CheckMiddleware returns an error if the tx doesn't have auth of this
// Required Actor, otherwise passes along the call untouched
type CheckMiddleware struct {
Required basecoin.Actor
PassOption
2017-06-29 11:02:38 -07:00
}
var _ Middleware = CheckMiddleware{}
func (_ CheckMiddleware) Name() string {
return NameCheck
}
2017-07-06 05:23:38 -07:00
func (p CheckMiddleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
2017-06-29 11:02:38 -07:00
if !ctx.HasPermission(p.Required) {
2017-07-03 05:50:33 -07:00
return res, errors.ErrUnauthorized()
2017-06-29 11:02:38 -07:00
}
return next.CheckTx(ctx, store, tx)
}
2017-07-06 05:23:38 -07:00
func (p CheckMiddleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
2017-06-29 11:02:38 -07:00
if !ctx.HasPermission(p.Required) {
2017-07-03 05:50:33 -07:00
return res, errors.ErrUnauthorized()
2017-06-29 11:02:38 -07:00
}
return next.DeliverTx(ctx, store, tx)
}
// GrantMiddleware tries to set the permission to this Actor, which may be prohibited
type GrantMiddleware struct {
Auth basecoin.Actor
PassOption
2017-06-29 11:02:38 -07:00
}
var _ Middleware = GrantMiddleware{}
func (_ GrantMiddleware) Name() string {
return NameGrant
}
2017-07-06 05:23:38 -07:00
func (g GrantMiddleware) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
2017-06-29 11:02:38 -07:00
ctx = ctx.WithPermissions(g.Auth)
return next.CheckTx(ctx, store, tx)
}
2017-07-06 05:23:38 -07:00
func (g GrantMiddleware) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
2017-06-29 11:02:38 -07:00
ctx = ctx.WithPermissions(g.Auth)
return next.DeliverTx(ctx, store, tx)
}