Skip to content

Commit d670ab8

Browse files
authored
chore: add cli/tx for create channel. (#7460)
1 parent ef74ad6 commit d670ab8

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

modules/core/04-channel/v2/client/cli/cli.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func NewTxCmd() *cobra.Command {
3636
}
3737

3838
txCmd.AddCommand(
39-
newProvideCounterpartyCmd(),
39+
newCreateChannelTxCmd(),
40+
newProvideCounterpartyTxCmd(),
4041
)
4142

4243
return txCmd

modules/core/04-channel/v2/client/cli/tx.go

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cli
22

33
import (
4+
"encoding/hex"
45
"fmt"
6+
"strings"
57

68
"github.com/spf13/cobra"
79

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

1315
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
16+
commitmenttypesv2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
1417
"github.com/cosmos/ibc-go/v9/modules/core/exported"
1518
)
1619

20+
// newCreateChannelTxCmd defines the command to create an IBC channel/v2.
21+
func newCreateChannelTxCmd() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "create-channel [client-identifier] [merkle-path-prefix]",
24+
Args: cobra.ExactArgs(2),
25+
Short: "create an IBC channel/v2",
26+
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.`,
27+
Example: fmt.Sprintf("%s tx %s %s create-channel 07-tendermint-0 696263,657572656b61", version.AppName, exported.ModuleName, types.SubModuleName),
28+
RunE: func(cmd *cobra.Command, args []string) error {
29+
clientCtx, err := client.GetClientTxContext(cmd)
30+
if err != nil {
31+
return err
32+
}
33+
34+
clientID := args[0]
35+
merklePathPrefix, err := parseMerklePathPrefix(args[2])
36+
if err != nil {
37+
return err
38+
}
39+
40+
msg := types.NewMsgCreateChannel(clientID, merklePathPrefix, clientCtx.GetFromAddress().String())
41+
42+
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
43+
},
44+
}
45+
46+
flags.AddTxFlagsToCmd(cmd)
47+
return cmd
48+
}
49+
1750
// newProvideCounterpartyCmd defines the command to provide the counterparty channel identifier to an IBC channel.
18-
func newProvideCounterpartyCmd() *cobra.Command {
51+
func newProvideCounterpartyTxCmd() *cobra.Command {
1952
cmd := &cobra.Command{
2053
Use: "provide-counterparty [channel-identifier] [counterparty-channel-identifier]",
2154
Args: cobra.ExactArgs(2),
@@ -43,3 +76,18 @@ func newProvideCounterpartyCmd() *cobra.Command {
4376
flags.AddTxFlagsToCmd(cmd)
4477
return cmd
4578
}
79+
80+
// parseMerklePathPrefix parses a comma-separated list of hex-encoded strings into a MerklePath.
81+
func parseMerklePathPrefix(merklePathPrefixString string) (commitmenttypesv2.MerklePath, error) {
82+
var keyPath [][]byte
83+
hexPrefixes := strings.Split(merklePathPrefixString, ",")
84+
for _, hexPrefix := range hexPrefixes {
85+
prefix, err := hex.DecodeString(hexPrefix)
86+
if err != nil {
87+
return commitmenttypesv2.MerklePath{}, fmt.Errorf("invalid hex merkle path prefix: %w", err)
88+
}
89+
keyPath = append(keyPath, prefix)
90+
}
91+
92+
return commitmenttypesv2.MerklePath{KeyPath: keyPath}, nil
93+
}

0 commit comments

Comments
 (0)