Skip to content

Commit

Permalink
Merge pull request #22 from regen-network/msg_auth-test
Browse files Browse the repository at this point in the history
Msg auth test
  • Loading branch information
anilcse authored Feb 11, 2020
2 parents 904d6d5 + 5ed3e84 commit 0c78c72
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
5 changes: 4 additions & 1 deletion x/msg_authorization/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command
}

var grant types.AuthorizationGrant
cdc.MustUnmarshalJSON(res, grant)
err = cdc.UnmarshalBinaryBare(res, &grant)
if err != nil {
return err
}

return cliCtx.PrintOutput(grant)
},
Expand Down
18 changes: 6 additions & 12 deletions x/msg_authorization/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command {

func GetCmdSendAs(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send-as [granter] [msg_tx_json] --from [grantee]",
Use: "send-as [grantee] [msg_tx_json] --from [grantee]",
Short: "execute tx on behalf of granter account",
Long: "execute tx on behalf of granter account",
Args: cobra.ExactArgs(2),
Expand All @@ -130,24 +130,18 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command {

grantee := cliCtx.FromAddress

// TODO cleanup this
//granter, err := sdk.AccAddressFromBech32(args[0])
//if err != nil {
// return err
//}

// TODO interactive should look good, consider second arg as optional?
//generatedTx, err := input.GetString("Enter generated tx json string:", inBuf)

var stdTx auth.StdTx
bz, err := ioutil.ReadFile(args[1])
if err != nil {
return err
}

err := cdc.UnmarshalJSON([]byte(args[1]), &stdTx)
err = cdc.UnmarshalJSON(bz, &stdTx)
if err != nil {
return err
}

msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs)
// TODO include the granter as delegated signer in the encoded JSON

if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
19 changes: 13 additions & 6 deletions x/msg_authorization/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package msg_authorization
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types"
)

func NewHandler(k Keeper) sdk.Handler {
Expand All @@ -27,23 +27,30 @@ func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k K

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
types.EventGrantAuthorization,
sdk.NewAttribute(types.AttributeKeyGrantType, msg.Authorization.MsgType()),
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()),
sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()),
sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()),
),
)

return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}

func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) (*sdk.Result, error) {
k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType)
err := k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType)
if err != nil {
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
types.EventRevokeAuthorization,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()),
sdk.NewAttribute(types.AttributeKeyGrantType, msg.AuthorizationMsgType),
sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()),
sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()),
),
)

Expand Down
7 changes: 6 additions & 1 deletion x/msg_authorization/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,14 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd
}

// Revoke method revokes any authorization for the provided message type granted to the grantee by the granter.
func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) {
func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) error {
store := ctx.KVStore(k.storeKey)
authorization, _ := k.GetAuthorization(ctx, grantee, granter, msgType)
if authorization == nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found")
}
store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType))
return nil
}

// GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time,
Expand Down
1 change: 1 addition & 0 deletions x/msg_authorization/internal/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
EventRevokeAuthorization = "revoke-authorization"
EventExecuteAuthorization = "execute-authorization"

AttributeKeyGrantType = "grant-type"
AttributeKeyGranteeAddress = "grantee"
AttributeKeyGranterAddress = "granter"

Expand Down
6 changes: 3 additions & 3 deletions x/msg_authorization/internal/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ func (msg MsgRevokeAuthorization) ValidateBasic() error {
// one signer corresponding to the granter of the authorization.
type MsgExecDelegated struct {
Grantee sdk.AccAddress `json:"grantee"`
Msgs []sdk.Msg `json:"msg"`
Msgs []sdk.Msg `json:"msgs"`
}

func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated {
func NewMsgExecDelegated(grantee sdk.AccAddress, msg []sdk.Msg) MsgExecDelegated {
return MsgExecDelegated{
Grantee: grantee,
Msgs: msgs,
Msgs: msg,
}
}

Expand Down

0 comments on commit 0c78c72

Please sign in to comment.