Skip to content

Commit

Permalink
basic cli and first draft command for ct module (#1929)
Browse files Browse the repository at this point in the history
* basic cli and first draft command for ct module

* add second command draft

* draft of init account

* close account update

* lint errors
  • Loading branch information
dssei authored Nov 14, 2024
1 parent 27b032b commit 3c23484
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 19 deletions.
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
175 changes: 175 additions & 0 deletions x/confidentialtransfers/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
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(
*privKey,
clientCtx.GetFromAddress().String(),
args[0],
account.PendingBalanceLo,
account.PendingBalanceHi,
account.AvailableBalance)

if err != nil {
return err
}

msg := types.NewMsgCloseAccountProto(closeAccount)

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

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
}
1 change: 1 addition & 0 deletions x/confidentialtransfers/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down
1 change: 1 addition & 0 deletions x/confidentialtransfers/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
Expand Down
8 changes: 6 additions & 2 deletions x/confidentialtransfers/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package keeper

import (
"context"
"math"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/utils"
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
"github.com/sei-protocol/sei-cryptography/pkg/zkproofs"
"math"
)

type msgServer struct {
Expand Down Expand Up @@ -282,7 +283,10 @@ func (m msgServer) ApplyPendingBalance(goCtx context.Context, req *types.MsgAppl
account.PendingBalanceCreditCounter = 0

// Save the changes to the account state
m.Keeper.SetAccount(ctx, req.Address, req.Denom, account)
err = m.Keeper.SetAccount(ctx, req.Address, req.Denom, account)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error setting account")
}

// Emit any required events
ctx.EventManager().EmitEvents(sdk.Events{
Expand Down
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
3 changes: 2 additions & 1 deletion x/confidentialtransfers/types/account.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types

import (
"math/big"

"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
"math/big"
)

type Account struct {
Expand Down
1 change: 1 addition & 0 deletions x/confidentialtransfers/types/close_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"crypto/ecdsa"

"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
"github.com/sei-protocol/sei-cryptography/pkg/zkproofs"
)
Expand Down
1 change: 1 addition & 0 deletions x/confidentialtransfers/types/initialize_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"crypto/ecdsa"

"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/sei-protocol/sei-cryptography/pkg/encryption"
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
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
3 changes: 2 additions & 1 deletion x/confidentialtransfers/types/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package types
import (
"crypto/ecdsa"
"errors"
"math/big"

"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/utils"
"github.com/sei-protocol/sei-cryptography/pkg/encryption"
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
"github.com/sei-protocol/sei-cryptography/pkg/zkproofs"
"math/big"
)

type Transfer struct {
Expand Down
3 changes: 2 additions & 1 deletion x/confidentialtransfers/types/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package types
import (
"crypto/ecdsa"
"errors"
"math/big"

"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/sei-protocol/sei-cryptography/pkg/encryption"
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
"github.com/sei-protocol/sei-cryptography/pkg/zkproofs"
"math/big"
)

type Withdraw struct {
Expand Down

0 comments on commit 3c23484

Please sign in to comment.