From de537c34ac1dbbc6fcf0543bd65dede5bf15d389 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 21 Jul 2017 17:45:29 +0200 Subject: [PATCH] Add cli support for register/update ibc --- cmd/basecli/main.go | 3 + modules/ibc/commands/tx.go | 107 +++++++++++++++++++++++++++++++++++ modules/roles/commands/tx.go | 4 +- 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 modules/ibc/commands/tx.go diff --git a/cmd/basecli/main.go b/cmd/basecli/main.go index 3a48febe2..490da2989 100644 --- a/cmd/basecli/main.go +++ b/cmd/basecli/main.go @@ -67,6 +67,9 @@ func main() { coincmd.SendTxCmd, // this enables creating roles rolecmd.CreateRoleTxCmd, + // these are for handling ibc + ibccmd.RegisterChainTxCmd, + ibccmd.UpdateChainTxCmd, ) // Set up the various commands to use diff --git a/modules/ibc/commands/tx.go b/modules/ibc/commands/tx.go new file mode 100644 index 000000000..608082bb1 --- /dev/null +++ b/modules/ibc/commands/tx.go @@ -0,0 +1,107 @@ +package commands + +import ( + "encoding/json" + "os" + + "github.com/pkg/errors" + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/tendermint/basecoin/client/commands" + txcmd "github.com/tendermint/basecoin/client/commands/txs" + "github.com/tendermint/basecoin/modules/ibc" + "github.com/tendermint/light-client/certifiers" +) + +// RegisterChainTxCmd is CLI command to register a new chain for ibc +var RegisterChainTxCmd = &cobra.Command{ + Use: "ibc-register", + Short: "Register a new chain", + RunE: commands.RequireInit(registerChainTxCmd), +} + +// UpdateChainTxCmd is CLI command to update the header for an ibc chain +var UpdateChainTxCmd = &cobra.Command{ + Use: "ibc-update", + Short: "Add new header to an existing chain", + RunE: commands.RequireInit(updateChainTxCmd), +} + +// TODO: post packet (query and all that jazz) + +// TODO: relay! + +//nolint +const ( + FlagSeed = "seed" +) + +func init() { + fs1 := RegisterChainTxCmd.Flags() + fs1.String(FlagSeed, "", "Filename with a seed file") + + fs2 := UpdateChainTxCmd.Flags() + fs2.String(FlagSeed, "", "Filename with a seed file") +} + +func registerChainTxCmd(cmd *cobra.Command, args []string) error { + seed, err := readSeed() + if err != nil { + return err + } + tx := ibc.RegisterChainTx{seed}.Wrap() + return txcmd.DoTx(tx) +} + +func updateChainTxCmd(cmd *cobra.Command, args []string) error { + seed, err := readSeed() + if err != nil { + return err + } + tx := ibc.UpdateChainTx{seed}.Wrap() + return txcmd.DoTx(tx) +} + +func readSeed() (seed certifiers.Seed, err error) { + name := viper.GetString(FlagSeed) + if name == "" { + return seed, errors.New("You must specify a seed file") + } + + var f *os.File + f, err = os.Open(name) + if err != nil { + return seed, errors.Wrap(err, "Cannot read seed file") + } + defer f.Close() + + // read the file as json into a seed + j := json.NewDecoder(f) + err = j.Decode(&seed) + err = errors.Wrap(err, "Invalid seed file") + return +} + +// func readCreateRoleTxFlags() (tx basecoin.Tx, err error) { +// role, err := parseRole(viper.GetString(FlagRole)) +// if err != nil { +// return tx, err +// } + +// sigs := viper.GetInt(FlagMinSigs) +// if sigs < 1 { +// return tx, errors.Errorf("--%s must be at least 1", FlagMinSigs) +// } + +// signers, err := commands.ParseActors(viper.GetString(FlagMembers)) +// if err != nil { +// return tx, err +// } +// if len(signers) == 0 { +// return tx, errors.New("must specify at least one member") +// } + +// tx = roles.NewCreateRoleTx(role, uint32(sigs), signers) +// return tx, nil +// } diff --git a/modules/roles/commands/tx.go b/modules/roles/commands/tx.go index 710413142..d0492445c 100644 --- a/modules/roles/commands/tx.go +++ b/modules/roles/commands/tx.go @@ -11,7 +11,7 @@ import ( "github.com/tendermint/basecoin/modules/roles" ) -// CreateRoleTxCmd is CLI command to send tokens between basecoin accounts +// CreateRoleTxCmd is CLI command to create a new role var CreateRoleTxCmd = &cobra.Command{ Use: "create-role", Short: "Create a new role", @@ -32,7 +32,7 @@ func init() { flags.Int(FlagMinSigs, 0, "Minimum number of signatures needed to assume this role") } -// createRoleTxCmd is an example of how to make a tx +// createRoleTxCmd creates a basic role tx and then wraps, signs, and posts it func createRoleTxCmd(cmd *cobra.Command, args []string) error { tx, err := readCreateRoleTxFlags() if err != nil {