* WIP: Msg authorization module added * fixing errors * fixed errors * fixed module.go * Add msg_tests * fixes compile issues * fix test * fix test * Add msg types tests * Fix Getmsgs * fixed codec issue * Fix syntax issues * Fix keeper * fixed proto issues * Fix keeper tests * fixed router in keeper * Fix query proto * Fix cli txs * Add grpc query client implementation * Add grpc-keeper test * Add grpc query tests Add revoke and exec authorization cli commands * Fix linting issues * Fix cli query * fix lint errors * Add Genesis state * Fix query authorization * Review changes * Fix grant authorization handler * Add cli tests * Add cli tests * Fix genesis test * Fix issues * update module to use proto msg services * Add simultion tests * Fix lint * fix lint * WIP simulations * WIP simulations * add msg tests * Fix simulation * Fix errors * fix genesis import export * fix sim tests * fix sim * fix test * Register RegisterMsgServer * WIP * WIP * Update keeper test * change msg_authorization module name to authz * changed type conversion for serviceMsg * serviceMsg change to any * Fix issues * fix msg tests * fix errors * proto format * remove LegacyQuerierHandler * Use MsgServiceRouter * fix keeper-test * fix query authorizations * fix NewCmdSendAs * fix simtests * fix error * fix lint * fix lint * add tests for generic authorization * fix imports * format * Update error message * remove println * add query all grants * Add pagination for queries * format * fix lint * review changes * fix grpc tests * add pagination to cli query * review changes * replace panic with error * lint * fix errors * fix tests * remove gogoproto extensions * update function doc * review changes * fix errors * fix query flags * fix grpc query test * init service-msg * remove unsed field * add proto-codec for simulations * fix codec issue * update authz simulations * change msgauth to authz * add check for invalid msg-type * change expiration flag to Unix * doc * update module.go * fix sims * fix grant-authorization sims * fix error * fix error * add build flag * fix codec issue * rename * review changes * format * review changes * go.mod * refactor * proto-gen * Update x/authz/keeper/grpc_query_test.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * Update x/authz/keeper/grpc_query_test.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * Update x/authz/keeper/grpc_query_test.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * Fix review comments * fix protogen * Follow Msg...Request style for msg requests * update comment * Fix error codes * fix review comment * improve msg validations * Handle error in casting msgs * rename actor => grantStoreKey * add godoc * add godoc * Fix simulations * Fix cli, cli_tests * Fix simulations * rename to GetOrRevokeAuthorization * Move events to keeper * Fix fmt * Update x/authz/client/cli/tx.go Co-authored-by: Amaury <amaury.martiny@protonmail.com> * rename actor * fix lint Co-authored-by: atheesh <atheesh@vitwit.com> Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: MD Aleem <72057206+aleem1413@users.noreply.github.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
223 lines
6.6 KiB
Go
223 lines
6.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/msgservice"
|
|
"github.com/cosmos/cosmos-sdk/version"
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
|
"github.com/cosmos/cosmos-sdk/x/authz/types"
|
|
)
|
|
|
|
const FlagSpendLimit = "spend-limit"
|
|
const FlagMsgType = "msg-type"
|
|
const FlagExpiration = "expiration"
|
|
|
|
// GetTxCmd returns the transaction commands for this module
|
|
func GetTxCmd() *cobra.Command {
|
|
AuthorizationTxCmd := &cobra.Command{
|
|
Use: types.ModuleName,
|
|
Short: "Authorization transactions subcommands",
|
|
Long: "Authorize and revoke access to execute transactions on behalf of your address",
|
|
DisableFlagParsing: true,
|
|
SuggestionsMinimumDistance: 2,
|
|
RunE: client.ValidateCmd,
|
|
}
|
|
|
|
AuthorizationTxCmd.AddCommand(
|
|
NewCmdGrantAuthorization(),
|
|
NewCmdRevokeAuthorization(),
|
|
NewCmdExecAuthorization(),
|
|
)
|
|
|
|
return AuthorizationTxCmd
|
|
}
|
|
|
|
func NewCmdGrantAuthorization() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "grant <grantee> <authorization_type=\"send\"|\"generic\"> --from <granter>",
|
|
Short: "Grant authorization to an address",
|
|
Long: strings.TrimSpace(
|
|
fmt.Sprintf(`Grant authorization to an address to execute a transaction on your behalf:
|
|
|
|
Examples:
|
|
$ %s tx %s grant cosmos1skjw.. send %s --spend-limit=1000stake --from=cosmos1skl..
|
|
$ %s tx %s grant cosmos1skjw.. generic --msg-type=/cosmos.gov.v1beta1.Msg/Vote --from=cosmos1sk..
|
|
`, version.AppName, types.ModuleName, types.SendAuthorization{}.MethodName(), version.AppName, types.ModuleName),
|
|
),
|
|
Args: cobra.RangeArgs(2, 3),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantee, err := sdk.AccAddressFromBech32(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var authorization types.Authorization
|
|
switch args[1] {
|
|
case "send":
|
|
limit, err := cmd.Flags().GetString(FlagSpendLimit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
spendLimit, err := sdk.ParseCoinsNormalized(limit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !spendLimit.IsAllPositive() {
|
|
return fmt.Errorf("spend-limit should be greater than zero")
|
|
}
|
|
|
|
authorization = &types.SendAuthorization{
|
|
SpendLimit: spendLimit,
|
|
}
|
|
case "generic":
|
|
msgType, err := cmd.Flags().GetString(FlagMsgType)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
authorization = types.NewGenericAuthorization(msgType)
|
|
default:
|
|
return fmt.Errorf("invalid authorization type, %s", args[1])
|
|
}
|
|
|
|
exp, err := cmd.Flags().GetInt64(FlagExpiration)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
msg, err := types.NewMsgGrantAuthorization(clientCtx.GetFromAddress(), grantee, authorization, time.Unix(exp, 0))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
|
authzMsgClient := types.NewMsgClient(svcMsgClientConn)
|
|
_, err = authzMsgClient.GrantAuthorization(context.Background(), msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), svcMsgClientConn.GetMsgs()...)
|
|
},
|
|
}
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
cmd.Flags().String(FlagMsgType, "", "The Msg method name for which we are creating a GenericAuthorization")
|
|
cmd.Flags().String(FlagSpendLimit, "", "SpendLimit for Send Authorization, an array of Coins allowed spend")
|
|
cmd.Flags().Int64(FlagExpiration, time.Now().AddDate(1, 0, 0).Unix(), "The Unix timestamp. Default is one year.")
|
|
return cmd
|
|
}
|
|
|
|
func NewCmdRevokeAuthorization() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "revoke [grantee_address] [msg_type] --from=[granter_address]",
|
|
Short: "revoke authorization",
|
|
Long: strings.TrimSpace(
|
|
fmt.Sprintf(`revoke authorization from a granter to a grantee:
|
|
Example:
|
|
$ %s tx %s revoke cosmos1skj.. %s --from=cosmos1skj..
|
|
`, version.AppName, types.ModuleName, types.SendAuthorization{}.MethodName()),
|
|
),
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
grantee, err := sdk.AccAddressFromBech32(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
granter := clientCtx.GetFromAddress()
|
|
|
|
msgAuthorized := args[1]
|
|
|
|
msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized)
|
|
|
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
|
authzMsgClient := types.NewMsgClient(svcMsgClientConn)
|
|
_, err = authzMsgClient.RevokeAuthorization(context.Background(), &msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), svcMsgClientConn.GetMsgs()...)
|
|
},
|
|
}
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
return cmd
|
|
}
|
|
|
|
func NewCmdExecAuthorization() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "exec [msg_tx_json_file] --from [grantee]",
|
|
Short: "execute tx on behalf of granter account",
|
|
Long: strings.TrimSpace(
|
|
fmt.Sprintf(`execute tx on behalf of granter account:
|
|
Example:
|
|
$ %s tx %s exec tx.json --from grantee
|
|
$ %s tx bank send <granter> <recipient> --from <granter> --chain-id <chain-id> --generate-only > tx.json && %s tx %s exec tx.json --from grantee
|
|
`, version.AppName, types.ModuleName, version.AppName, version.AppName, types.ModuleName),
|
|
),
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantee := clientCtx.GetFromAddress()
|
|
|
|
if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline {
|
|
return errors.New("cannot broadcast tx during offline mode")
|
|
}
|
|
|
|
theTx, err := authclient.ReadTxFromFile(clientCtx, args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
msgs := theTx.GetMsgs()
|
|
serviceMsgs := make([]sdk.ServiceMsg, len(msgs))
|
|
for i, msg := range msgs {
|
|
srvMsg, ok := msg.(sdk.ServiceMsg)
|
|
if !ok {
|
|
return fmt.Errorf("tx contains %T which is not a sdk.ServiceMsg", msg)
|
|
}
|
|
serviceMsgs[i] = srvMsg
|
|
}
|
|
|
|
msg := types.NewMsgExecAuthorized(grantee, serviceMsgs)
|
|
svcMsgClientConn := &msgservice.ServiceMsgClientConn{}
|
|
authzMsgClient := types.NewMsgClient(svcMsgClientConn)
|
|
_, err = authzMsgClient.ExecAuthorized(context.Background(), &msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), svcMsgClientConn.GetMsgs()...)
|
|
},
|
|
}
|
|
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
|
|
return cmd
|
|
}
|