-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
msg_server.go
160 lines (125 loc) · 4.27 KB
/
msg_server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package keeper
import (
"context"
"errors"
"strings"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/authz"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var _ authz.MsgServer = Keeper{}
// Grant implements the MsgServer.Grant method to create a new grant.
func (k Keeper) Grant(ctx context.Context, msg *authz.MsgGrant) (*authz.MsgGrantResponse, error) {
if strings.EqualFold(msg.Grantee, msg.Granter) {
return nil, authz.ErrGranteeIsGranter
}
grantee, err := k.addrCdc.StringToBytes(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}
granter, err := k.addrCdc.StringToBytes(msg.Granter)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
if err := msg.Grant.ValidateBasic(); err != nil {
return nil, err
}
authorization, err := msg.GetAuthorization()
if err != nil {
return nil, err
}
t := authorization.MsgTypeURL()
if err := k.MsgRouterService.CanInvoke(ctx, t); err != nil {
return nil, sdkerrors.ErrInvalidType.Wrapf("%s doesn't exist", t)
}
// Disable granting other accounts with grant permission.
// Preventing user from accidentally authorizing their entire account to a different account.
if t == sdk.MsgTypeURL(&authz.MsgGrant{}) {
return nil, sdkerrors.ErrInvalidType.Wrap("authz msgGrant is not allowed")
}
err = k.SaveGrant(ctx, grantee, granter, authorization, msg.Grant.Expiration)
if err != nil {
return nil, err
}
return &authz.MsgGrantResponse{}, nil
}
// Revoke implements the MsgServer.Revoke method.
func (k Keeper) Revoke(ctx context.Context, msg *authz.MsgRevoke) (*authz.MsgRevokeResponse, error) {
if strings.EqualFold(msg.Grantee, msg.Granter) {
return nil, authz.ErrGranteeIsGranter
}
grantee, err := k.addrCdc.StringToBytes(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}
granter, err := k.addrCdc.StringToBytes(msg.Granter)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
if msg.MsgTypeUrl == "" {
return nil, sdkerrors.ErrInvalidRequest.Wrap("missing msg method name")
}
if err = k.DeleteGrant(ctx, grantee, granter, msg.MsgTypeUrl); err != nil {
return nil, err
}
return &authz.MsgRevokeResponse{}, nil
}
// RevokeAll implements the MsgServer.RevokeAll method.
func (k Keeper) RevokeAll(ctx context.Context, msg *authz.MsgRevokeAll) (*authz.MsgRevokeAllResponse, error) {
granter, err := k.addrCdc.StringToBytes(msg.Granter)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
if err := k.DeleteAllGrants(ctx, granter); err != nil {
return nil, err
}
return &authz.MsgRevokeAllResponse{}, nil
}
// Exec implements the MsgServer.Exec method.
func (k Keeper) Exec(ctx context.Context, msg *authz.MsgExec) (*authz.MsgExecResponse, error) {
if msg.Grantee == "" {
return nil, errors.New("empty address string is not allowed")
}
grantee, err := k.addrCdc.StringToBytes(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}
if len(msg.Msgs) == 0 {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("messages cannot be empty")
}
msgs, err := msg.GetMessages()
if err != nil {
return nil, err
}
if err := validateMsgs(msgs); err != nil {
return nil, err
}
results, err := k.DispatchActions(ctx, grantee, msgs)
if err != nil {
return nil, err
}
return &authz.MsgExecResponse{Results: results}, nil
}
func (k Keeper) PruneExpiredGrants(ctx context.Context, msg *authz.MsgPruneExpiredGrants) (*authz.MsgPruneExpiredGrantsResponse, error) {
// 75 is an arbitrary value, we can change it later if needed
if err := k.DequeueAndDeleteExpiredGrants(ctx, 75); err != nil {
return nil, err
}
if err := k.EventService.EventManager(ctx).Emit(&authz.EventPruneExpiredGrants{Pruner: msg.Pruner}); err != nil {
return nil, err
}
return &authz.MsgPruneExpiredGrantsResponse{}, nil
}
func validateMsgs(msgs []sdk.Msg) error {
for i, msg := range msgs {
m, ok := msg.(sdk.HasValidateBasic)
if !ok {
continue
}
if err := m.ValidateBasic(); err != nil {
return errorsmod.Wrapf(err, "msg %d", i)
}
}
return nil
}