Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permissioned keys cli #2644

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions protocol/x/accountplus/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cli

import (
"context"
"fmt"

"github.com/spf13/cast"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
)

// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd() *cobra.Command {
// Group x/accountplus queries under a subcommand.
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(
CmdQueryParam(),
CmdQueryGetAuthenticator(),
CmdQueryGetAllAuthenticators(),
)
return cmd
}

func CmdQueryParam() *cobra.Command {
cmd := &cobra.Command{
Use: "param",
Short: "Get param",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Params(
context.Background(),
&types.QueryParamsRequest{},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdQueryGetAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "get-authenticator [account] [authenticator_id]",
Short: "Get authenticator",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
id, err := cast.ToUint64E(args[1])
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.GetAuthenticator(
context.Background(),
&types.GetAuthenticatorRequest{
Account: args[0],
AuthenticatorId: id,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdQueryGetAllAuthenticators() *cobra.Command {
cmd := &cobra.Command{
Use: "get-all-authenticators [account]",
Short: "Get all authenticators for an account",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.GetAuthenticators(
context.Background(),
&types.GetAuthenticatorsRequest{
Account: args[0],
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
73 changes: 73 additions & 0 deletions protocol/x/accountplus/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cli

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)

// GetTxCmd returns the transaction commands for this module.
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(CmdAddAuthenticator())
cmd.AddCommand(CmdRemoveAuthenticator())
return cmd
}

func CmdAddAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "add-authenticator [account] [authenticator_type] [data]",
Short: "Registers an authenticator",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.MsgAddAuthenticator{
Sender: args[0],
AuthenticatorType: args[1],
Data: []byte(args[2]),
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}

func CmdRemoveAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "remove-authenticator [sender] [authenticator_id]",
Short: "Removes an authenticator",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
id, err := cast.ToUint64E(args[1])
if err != nil {
return err
}
msg := types.MsgRemoveAuthenticator{
Sender: args[0],
Id: id,
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
Comment on lines +50 to +73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add confirmation prompt for authenticator removal

Removing an authenticator is a sensitive operation that should require confirmation to prevent accidental removals.

Add a confirmation prompt using the SDK's input package:

 func CmdRemoveAuthenticator() *cobra.Command {
 	cmd := &cobra.Command{
 		Use:   "remove-authenticator [sender] [authenticator_id]",
-		Short: "Removes an authenticator",
+		Short: "Removes an authenticator (requires confirmation)",
 		Args:  cobra.ExactArgs(2),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			clientCtx, err := client.GetClientTxContext(cmd)
 			if err != nil {
 				return err
 			}
 			id, err := cast.ToUint64E(args[1])
 			if err != nil {
 				return err
 			}
+			buf := bufio.NewReader(cmd.InOrStdin())
+			prompt := fmt.Sprintf("Are you sure you want to remove authenticator %d? (y/N): ", id)
+			if !input.GetConfirmation(prompt, buf) {
+				return fmt.Errorf("operation cancelled")
+			}
 			msg := types.MsgRemoveAuthenticator{

Committable suggestion skipped: line range outside the PR's diff.

7 changes: 3 additions & 4 deletions protocol/x/accountplus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/client/cli"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
)
Expand Down Expand Up @@ -71,15 +72,13 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r
// GetTxCmd returns the root Tx command for the module. The subcommands of this root command are used by end-users
// to generate new transactions containing messages defined in the module
func (a AppModuleBasic) GetTxCmd() *cobra.Command {
// accountplus is currently purely for storage and does not support user-submitted transactions
return nil
return cli.GetTxCmd()
}

// GetQueryCmd returns the root query command for the module. The subcommands of this root command are used by
// end-users to generate new queries to the subset of the state defined by the module
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
// accountplus is currently purely for storage and does not support queries
return nil
return cli.GetQueryCmd()
}

// DefaultGenesis returns a default GenesisState for the module, marshalled to json.RawMessage. The default
Expand Down
Loading