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

basic cli and first draft command for ct module #1929

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func New(

app.ConfidentialTransfersKeeper = ctkeeper.NewKeeper(
appCodec,
app.keys[(cttypes.StoreKey)],
app.keys[cttypes.StoreKey],
app.GetSubspace(cttypes.ModuleName),
app.AccountKeeper,
app.BankKeeper)
Expand Down
170 changes: 170 additions & 0 deletions x/confidentialtransfers/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package cli

import (
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/ethereum/go-ethereum/crypto"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
"github.com/spf13/cobra"
)

const (
FlagPrivateKey = "private-key"
)

// NewTxCmd returns a root CLI command handler for all x/confidentialtransfers transaction commands.
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ShortModuleName,
Short: "Confidential transfers transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

txCmd.AddCommand(NewInitializeAccountTxCmd())
txCmd.AddCommand(NewCloseAccountTxCmd())

return txCmd
}

// NewInitializeAccountTxCmd returns a CLI command handler for creating a MsgInitializeAccount transaction.
func NewInitializeAccountTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init-account [denom] [flags]",
Short: "Initialize confidential transfers account",
Long: `Initialize confidential transfers command creates account for the specified denomination and address
passed in --from flag.`,
Args: cobra.ExactArgs(1),
RunE: makeInitializeAccountCmd,
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func makeInitializeAccountCmd(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

privKey, err := getPrivateKey(cmd)
if err != nil {
return err
}
initializeAccount, err := types.NewInitializeAccount(clientCtx.GetFromAddress().String(), args[0], *privKey)
if err != nil {
return err
}

msg := types.NewMsgInitializeAccountProto(initializeAccount)

if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}

func getPrivateKey(cmd *cobra.Command) (*ecdsa.PrivateKey, error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return nil, err
}
txf := tx.NewFactoryCLI(clientCtx, cmd.Flags())
kb := txf.Keybase()
info, err := kb.Key(clientCtx.GetFromName())
if err != nil {
return nil, err
}
localInfo, ok := info.(keyring.LocalInfo)
if !ok {
return nil, errors.New("can only associate address for local keys")
}
if localInfo.GetAlgo() != hd.Secp256k1Type {
return nil, errors.New("can only use addresses using secp256k1")
}
priv, err := legacy.PrivKeyFromBytes([]byte(localInfo.PrivKeyArmor))
if err != nil {
return nil, err
}
privHex := hex.EncodeToString(priv.Bytes())
key, _ := crypto.HexToECDSA(privHex)
return key, nil
}

// NewCloseAccountTxCmd returns a CLI command handler for creating a MsgCloseAccount transaction.
func NewCloseAccountTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "close-account [denom] [flags]",
Short: "Close confidential transfers account",
Long: `Close confidential transfers command closes (deletes) account for the specified denomination and address
passed in --from flag.`,
Args: cobra.ExactArgs(1),
RunE: makeCloseAccountCmd,
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func makeCloseAccountCmd(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

queryClientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(queryClientCtx)

privKey, err := getPrivateKey(cmd)
if err != nil {
return err
}

req := &types.GetCtAccountRequest{
Address: clientCtx.GetFromAddress().String(),
Denom: args[0],
}

ctAccount, err := queryClient.GetCtAccount(context.Background(), req)
if err != nil {
return err
}

account, err := ctAccount.GetAccount().FromProto()
if err != nil {
return err
}

closeAccount, err := types.NewCloseAccount(

Check failure on line 155 in x/confidentialtransfers/client/cli/tx.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
*privKey,
clientCtx.GetFromAddress().String(),
args[0],
account.PendingBalanceLo,
account.PendingBalanceHi,
account.AvailableBalance)

msg := types.NewMsgCloseAccountProto(closeAccount)

if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}
20 changes: 7 additions & 13 deletions x/confidentialtransfers/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@ import (
"fmt"
"math/rand"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/keeper"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/gorilla/mux"
abci "github.com/tendermint/tendermint/abci/types"

//"github.com/sei-protocol/sei-chain/x/tokenfactory/client/cli"
//"github.com/sei-protocol/sei-chain/x/tokenfactory/keeper"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/client/cli"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/keeper"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"

simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
)

var (
Expand Down Expand Up @@ -100,11 +96,9 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) //nolint:errcheck
}

// TODO: Implement this when we add the CLI methods
// GetTxCmd returns the x/confidentialtransfers module's root tx command.
func (am AppModuleBasic) GetTxCmd() *cobra.Command {
//return cli.GetTxCmd()
return nil
return cli.NewTxCmd()
}

// TODO: Implement this when we add the CLI methods
Expand Down
2 changes: 2 additions & 0 deletions x/confidentialtransfers/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const (
// ModuleName defines the module name
ModuleName = "confidentialtransfers"

ShortModuleName = "ct"

// StoreKey defines the primary module store key
StoreKey = ModuleName

Expand Down
Loading