2021-01-25 08:41:30 -08:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-05-06 11:23:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
2021-01-25 08:41:30 -08:00
|
|
|
)
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
var _ authz.MsgServer = Keeper{}
|
2021-01-25 08:41:30 -08:00
|
|
|
|
2022-02-07 06:38:45 -08:00
|
|
|
// GrantAuthorization implements the MsgServer.Grant method to create a new grant.
|
2021-05-06 11:23:48 -07:00
|
|
|
func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGrantResponse, error) {
|
2021-01-25 08:41:30 -08:00
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-09 20:49:39 -08:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-01-25 08:41:30 -08:00
|
|
|
granter, err := sdk.AccAddressFromBech32(msg.Granter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:36:50 -08:00
|
|
|
authorization, err := msg.GetAuthorization()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-06 11:23:48 -07:00
|
|
|
}
|
|
|
|
t := authorization.MsgTypeURL()
|
2021-05-07 03:37:55 -07:00
|
|
|
if k.router.HandlerByTypeURL(t) == nil {
|
2022-04-19 12:08:35 -07:00
|
|
|
return nil, sdkerrors.ErrInvalidType.Wrapf("%s doesn't exist.", t)
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|
|
|
|
|
2021-05-10 02:28:08 -07:00
|
|
|
err = k.SaveGrant(ctx, grantee, granter, authorization, msg.Grant.Expiration)
|
2021-01-25 08:41:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
return &authz.MsgGrantResponse{}, nil
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
// RevokeAuthorization implements the MsgServer.Revoke method.
|
|
|
|
func (k Keeper) Revoke(goCtx context.Context, msg *authz.MsgRevoke) (*authz.MsgRevokeResponse, error) {
|
2021-01-25 08:41:30 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
err = k.DeleteGrant(ctx, grantee, granter, msg.MsgTypeUrl)
|
2021-01-25 08:41:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
return &authz.MsgRevokeResponse{}, nil
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
// Exec implements the MsgServer.Exec method.
|
|
|
|
func (k Keeper) Exec(goCtx context.Context, msg *authz.MsgExec) (*authz.MsgExecResponse, error) {
|
2021-01-25 08:41:30 -08:00
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-30 04:00:47 -07:00
|
|
|
msgs, err := msg.GetMessages()
|
2021-01-25 08:41:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-18 12:06:10 -07:00
|
|
|
results, err := k.DispatchActions(ctx, grantee, msgs)
|
2021-01-25 08:41:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-18 12:06:10 -07:00
|
|
|
return &authz.MsgExecResponse{Results: results}, nil
|
2021-01-25 08:41:30 -08:00
|
|
|
}
|