github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/authorization_grant.go (about) 1 package authz 2 3 import ( 4 "time" 5 6 proto "github.com/cosmos/gogoproto/proto" 7 8 errorsmod "cosmossdk.io/errors" 9 10 cdctypes "github.com/cosmos/cosmos-sdk/codec/types" 11 sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" 12 ) 13 14 // NewGrant returns new Grant. Expiration is optional and noop if null. 15 // It returns an error if the expiration is before the current block time, 16 // which is passed into the `blockTime` arg. 17 func NewGrant(blockTime time.Time, a Authorization, expiration *time.Time) (Grant, error) { 18 if expiration != nil && !expiration.After(blockTime) { 19 return Grant{}, errorsmod.Wrapf(ErrInvalidExpirationTime, "expiration must be after the current block time (%v), got %v", blockTime.Format(time.RFC3339), expiration.Format(time.RFC3339)) 20 } 21 msg, ok := a.(proto.Message) 22 if !ok { 23 return Grant{}, sdkerrors.ErrPackAny.Wrapf("cannot proto marshal %T", a) 24 } 25 any, err := cdctypes.NewAnyWithValue(msg) 26 if err != nil { 27 return Grant{}, err 28 } 29 return Grant{ 30 Expiration: expiration, 31 Authorization: any, 32 }, nil 33 } 34 35 var _ cdctypes.UnpackInterfacesMessage = &Grant{} 36 37 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 38 func (g Grant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error { 39 var authorization Authorization 40 return unpacker.UnpackAny(g.Authorization, &authorization) 41 } 42 43 // GetAuthorization returns the cached value from the Grant.Authorization if present. 44 func (g Grant) GetAuthorization() (Authorization, error) { 45 if g.Authorization == nil { 46 return nil, sdkerrors.ErrInvalidType.Wrap("authorization is nil") 47 } 48 av := g.Authorization.GetCachedValue() 49 a, ok := av.(Authorization) 50 if !ok { 51 return nil, sdkerrors.ErrInvalidType.Wrapf("expected %T, got %T", (Authorization)(nil), av) 52 } 53 return a, nil 54 } 55 56 func (g Grant) ValidateBasic() error { 57 if g.Authorization == nil { 58 return sdkerrors.ErrInvalidType.Wrap("authorization is nil") 59 } 60 61 av := g.Authorization.GetCachedValue() 62 a, ok := av.(Authorization) 63 if !ok { 64 return sdkerrors.ErrInvalidType.Wrapf("expected %T, got %T", (Authorization)(nil), av) 65 } 66 return a.ValidateBasic() 67 }