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