Skip to content

Commit

Permalink
chore: add cli/tx for create channel. (#7460)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim authored Oct 15, 2024
1 parent ef74ad6 commit d670ab8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion modules/core/04-channel/v2/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func NewTxCmd() *cobra.Command {
}

txCmd.AddCommand(
newProvideCounterpartyCmd(),
newCreateChannelTxCmd(),
newProvideCounterpartyTxCmd(),
)

return txCmd
Expand Down
50 changes: 49 additions & 1 deletion modules/core/04-channel/v2/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli

import (
"encoding/hex"
"fmt"
"strings"

"github.com/spf13/cobra"

Expand All @@ -11,11 +13,42 @@ import (
"github.com/cosmos/cosmos-sdk/version"

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
commitmenttypesv2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

// newCreateChannelTxCmd defines the command to create an IBC channel/v2.
func newCreateChannelTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-channel [client-identifier] [merkle-path-prefix]",
Args: cobra.ExactArgs(2),
Short: "create an IBC channel/v2",
Long: `Creates an IBC channel/v2 using the client identifier representing the counterparty chain and the hex-encoded merkle path prefix under which the counterparty stores packet flow information.`,
Example: fmt.Sprintf("%s tx %s %s create-channel 07-tendermint-0 696263,657572656b61", version.AppName, exported.ModuleName, types.SubModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

clientID := args[0]
merklePathPrefix, err := parseMerklePathPrefix(args[2])
if err != nil {
return err
}

msg := types.NewMsgCreateChannel(clientID, merklePathPrefix, clientCtx.GetFromAddress().String())

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

flags.AddTxFlagsToCmd(cmd)
return cmd
}

// newProvideCounterpartyCmd defines the command to provide the counterparty channel identifier to an IBC channel.
func newProvideCounterpartyCmd() *cobra.Command {
func newProvideCounterpartyTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "provide-counterparty [channel-identifier] [counterparty-channel-identifier]",
Args: cobra.ExactArgs(2),
Expand Down Expand Up @@ -43,3 +76,18 @@ func newProvideCounterpartyCmd() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

// parseMerklePathPrefix parses a comma-separated list of hex-encoded strings into a MerklePath.
func parseMerklePathPrefix(merklePathPrefixString string) (commitmenttypesv2.MerklePath, error) {
var keyPath [][]byte
hexPrefixes := strings.Split(merklePathPrefixString, ",")
for _, hexPrefix := range hexPrefixes {
prefix, err := hex.DecodeString(hexPrefix)
if err != nil {
return commitmenttypesv2.MerklePath{}, fmt.Errorf("invalid hex merkle path prefix: %w", err)
}
keyPath = append(keyPath, prefix)
}

return commitmenttypesv2.MerklePath{KeyPath: keyPath}, nil
}

0 comments on commit d670ab8

Please sign in to comment.