-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
authorization_grant.go
61 lines (53 loc) · 1.92 KB
/
authorization_grant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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"
)
// NewGrant returns new Grant. Expiration is optional and noop if null.
// It returns an error if the expiration is before the current block time,
// which is passed into the `blockTime` arg.
func NewGrant(blockTime time.Time, a Authorization, expiration *time.Time) (Grant, error) {
if expiration != nil && !expiration.After(blockTime) {
return Grant{}, sdkerrors.Wrapf(ErrInvalidExpirationTime, "expiration must be after the current block time (%v), got %v", blockTime.Format(time.RFC3339), expiration.Format(time.RFC3339))
}
msg, ok := a.(proto.Message)
if !ok {
return Grant{}, sdkerrors.ErrPackAny.Wrapf("cannot proto marshal %T", a)
}
any, err := cdctypes.NewAnyWithValue(msg)
if err != nil {
return Grant{}, err
}
return Grant{
Expiration: expiration,
Authorization: any,
}, nil
}
var _ cdctypes.UnpackInterfacesMessage = &Grant{}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (g Grant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
var authorization Authorization
return unpacker.UnpackAny(g.Authorization, &authorization)
}
// GetAuthorization returns the cached value from the Grant.Authorization if present.
func (g Grant) GetAuthorization() (Authorization, error) {
if g.Authorization == nil {
return nil, sdkerrors.ErrInvalidType.Wrap("authorization is nil")
}
av := g.Authorization.GetCachedValue()
a, ok := av.(Authorization)
if !ok {
return nil, sdkerrors.ErrInvalidType.Wrapf("expected %T, got %T", (Authorization)(nil), av)
}
return a, nil
}
func (g Grant) ValidateBasic() error {
av := g.Authorization.GetCachedValue()
a, ok := av.(Authorization)
if !ok {
return sdkerrors.ErrInvalidType.Wrapf("expected %T, got %T", (Authorization)(nil), av)
}
return a.ValidateBasic()
}