-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_server.go
90 lines (73 loc) · 2.24 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
package keeper
import (
"context"
sdk "github.com/blockgenx/blockgen-sdk/types"
sdkerrors "github.com/blockgenx/blockgen-sdk/types/errors"
"github.com/blockgenx/blockgen-sdk/x/authz"
)
var _ authz.MsgServer = Keeper{}
// GrantAuthorization implements the MsgServer.Grant method to create a new grant.
func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGrantResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
if err != nil {
return nil, err
}
// create the account if it is not in account state
granteeAcc := k.authKeeper.GetAccount(ctx, grantee)
if granteeAcc == nil {
granteeAcc = k.authKeeper.NewAccountWithAddress(ctx, grantee)
k.authKeeper.SetAccount(ctx, granteeAcc)
}
granter, err := sdk.AccAddressFromBech32(msg.Granter)
if err != nil {
return nil, err
}
authorization, err := msg.GetAuthorization()
if err != nil {
return nil, err
}
t := authorization.MsgTypeURL()
if k.router.HandlerByTypeURL(t) == nil {
return nil, sdkerrors.ErrInvalidType.Wrapf("%s doesn't exist.", t)
}
err = k.SaveGrant(ctx, grantee, granter, authorization, msg.Grant.Expiration)
if err != nil {
return nil, err
}
return &authz.MsgGrantResponse{}, nil
}
// RevokeAuthorization implements the MsgServer.Revoke method.
func (k Keeper) Revoke(goCtx context.Context, msg *authz.MsgRevoke) (*authz.MsgRevokeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
if err != nil {
return nil, err
}
granter, err := sdk.AccAddressFromBech32(msg.Granter)
if err != nil {
return nil, err
}
err = k.DeleteGrant(ctx, grantee, granter, msg.MsgTypeUrl)
if err != nil {
return nil, err
}
return &authz.MsgRevokeResponse{}, nil
}
// Exec implements the MsgServer.Exec method.
func (k Keeper) Exec(goCtx context.Context, msg *authz.MsgExec) (*authz.MsgExecResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
if err != nil {
return nil, err
}
msgs, err := msg.GetMessages()
if err != nil {
return nil, err
}
results, err := k.DispatchActions(ctx, grantee, msgs)
if err != nil {
return nil, err
}
return &authz.MsgExecResponse{Results: results}, nil
}