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
2021-10-15 11:17:53 -07:00
func NewGrant ( /*blockTime time.Time, */ a Authorization , expiration time . Time ) ( Grant , error ) {
// TODO: add this for 0.45
// if !expiration.After(blockTime) {
// return Grant{}, sdkerrors.ErrInvalidRequest.Wrapf("expiration must be after the current block time (%v), got %v", blockTime.Format(time.RFC3339), expiration.Format(time.RFC3339))
// }
2021-05-10 02:28:08 -07:00
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
}
var (
_ cdctypes . UnpackInterfacesMessage = & Grant { }
)
// 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 {
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
}