2021-05-06 11:23:48 -07:00
|
|
|
package authz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
proto "github.com/gogo/protobuf/proto"
|
|
|
|
|
|
|
|
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
)
|
|
|
|
|
2021-05-10 02:28:08 -07:00
|
|
|
// NewGrant returns new Grant
|
|
|
|
func NewGrant(a Authorization, expiration time.Time) (Grant, error) {
|
|
|
|
g := Grant{
|
2021-05-06 11:23:48 -07:00
|
|
|
Expiration: expiration,
|
|
|
|
}
|
2021-05-10 02:28:08 -07:00
|
|
|
msg, ok := a.(proto.Message)
|
2021-05-06 11:23:48 -07:00
|
|
|
if !ok {
|
2021-05-10 02:28:08 -07:00
|
|
|
return Grant{}, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", a)
|
2021-05-06 11:23:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
any, err := cdctypes.NewAnyWithValue(msg)
|
|
|
|
if err != nil {
|
|
|
|
return Grant{}, err
|
|
|
|
}
|
2021-05-10 02:28:08 -07:00
|
|
|
g.Authorization = any
|
2021-05-06 11:23:48 -07:00
|
|
|
|
2021-05-10 02:28:08 -07:00
|
|
|
return g, nil
|
2021-05-06 11:23:48 -07:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:01:33 -08:00
|
|
|
var (
|
|
|
|
_ cdctypes.UnpackInterfacesMessage = &Grant{}
|
|
|
|
)
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
2021-05-10 02:28:08 -07:00
|
|
|
func (g Grant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
|
2021-05-06 11:23:48 -07:00
|
|
|
var authorization Authorization
|
2021-05-10 02:28:08 -07:00
|
|
|
return unpacker.UnpackAny(g.Authorization, &authorization)
|
2021-05-06 11:23:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuthorization returns the cached value from the Grant.Authorization if present.
|
2021-05-10 02:28:08 -07:00
|
|
|
func (g Grant) GetAuthorization() Authorization {
|
|
|
|
if g.Authorization == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
a, ok := g.Authorization.GetCachedValue().(Authorization)
|
2021-05-06 11:23:48 -07:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-10 02:28:08 -07:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Grant) ValidateBasic() error {
|
2022-02-03 08:01:33 -08:00
|
|
|
if g.Expiration.Unix() < time.Now().Unix() {
|
|
|
|
return sdkerrors.Wrap(ErrInvalidExpirationTime, "Time can't be in the past")
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:28:08 -07:00
|
|
|
av := g.Authorization.GetCachedValue()
|
|
|
|
a, ok := av.(Authorization)
|
|
|
|
if !ok {
|
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expected %T, got %T", (Authorization)(nil), av)
|
|
|
|
}
|
|
|
|
return a.ValidateBasic()
|
2021-05-06 11:23:48 -07:00
|
|
|
}
|