diff --git a/examples/basecoin/x/cool/commands/cooltx.go b/examples/basecoin/x/cool/commands/cooltx.go index 8a8452a00..e1f1b866f 100644 --- a/examples/basecoin/x/cool/commands/cooltx.go +++ b/examples/basecoin/x/cool/commands/cooltx.go @@ -12,40 +12,64 @@ import ( "github.com/cosmos/cosmos-sdk/examples/basecoin/x/cool" ) -// SendTxCommand will create a send tx and sign it with the given key +// what cool trasaction func WhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { - cmdr := commander{cdc} return &cobra.Command{ Use: "whatcool [answer]", Short: "What's cooler than being cool?", - RunE: cmdr.whatCoolTxCmd, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) != 1 || len(args[0]) == 0 { + return errors.New("You must provide an answer") + } + + // get the from address from the name flag + from, err := builder.GetFromAddress() + if err != nil { + return err + } + + // create the message + msg := cool.NewWhatCoolMsg(from, args[0]) + + // build and sign the transaction, then broadcast to Tendermint + res, err := builder.SignBuildBroadcast(msg, cdc) + if err != nil { + return err + } + + fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String()) + return nil + }, } } -type commander struct { - cdc *wire.Codec -} - -func (c commander) whatCoolTxCmd(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide an answer") - } - - // get the from address from the name flag - from, err := builder.GetFromAddress() - if err != nil { - return err - } - - // create the message - msg := cool.NewWhatCoolMsg(from, args[0]) - - // build and sign the transaction, then broadcast to Tendermint - res, err := builder.SignBuildBroadcast(msg, c.cdc) - if err != nil { - return err - } - - fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String()) - return nil +// set what cool trasaction +func SetWhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { + return &cobra.Command{ + Use: "setwhatcool [answer]", + Short: "You're so cool, tell us what is cool!", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) != 1 || len(args[0]) == 0 { + return errors.New("You must provide an answer") + } + + // get the from address from the name flag + from, err := builder.GetFromAddress() + if err != nil { + return err + } + + // create the message + msg := cool.NewSetWhatCoolMsg(from, args[0]) + + // build and sign the transaction, then broadcast to Tendermint + res, err := builder.SignBuildBroadcast(msg, cdc) + if err != nil { + return err + } + + fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String()) + return nil + }, + } } diff --git a/x/bank/commands/sendtx.go b/x/bank/commands/sendtx.go index 8d0c0be6e..e74361231 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -4,13 +4,10 @@ import ( "encoding/hex" "fmt" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/builder" - "github.com/cosmos/cosmos-sdk/client/keys" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" @@ -44,12 +41,21 @@ type commander struct { } func (c commander) sendTxCmd(cmd *cobra.Command, args []string) error { - txBytes, err := c.buildTx() + + // get the from address + from, err := builder.GetFromAddress() if err != nil { return err } - res, err := builder.BroadcastTx(txBytes) + // build send msg + msg, err := buildMsg(from) + if err != nil { + return err + } + + // build and sign the transaction, then broadcast to Tendermint + res, err := builder.SignBuildBroadcast(msg, c.cdc) if err != nil { return err } @@ -58,52 +64,6 @@ func (c commander) sendTxCmd(cmd *cobra.Command, args []string) error { return nil } -func (c commander) buildTx() ([]byte, error) { - keybase, err := keys.GetKeyBase() - if err != nil { - return nil, err - } - - name := viper.GetString(client.FlagName) - info, err := keybase.Get(name) - if err != nil { - return nil, errors.Errorf("No key for: %s", name) - } - from := info.PubKey.Address() - - msg, err := buildMsg(from) - if err != nil { - return nil, err - } - - // sign and build - bz := msg.GetSignBytes() - buf := client.BufferStdin() - prompt := fmt.Sprintf("Password to sign with '%s':", name) - passphrase, err := client.GetPassword(prompt, buf) - if err != nil { - return nil, err - } - sig, pubkey, err := keybase.Sign(name, passphrase, bz) - if err != nil { - return nil, err - } - sigs := []sdk.StdSignature{{ - PubKey: pubkey, - Signature: sig, - Sequence: viper.GetInt64(flagSequence), - }} - - // marshal bytes - tx := sdk.NewStdTx(msg, sigs) - - txBytes, err := c.cdc.MarshalBinary(tx) - if err != nil { - return nil, err - } - return txBytes, nil -} - func buildMsg(from sdk.Address) (sdk.Msg, error) { // parse coins