cosmos-sdk/x/bank/commands/sendtx.go

115 lines
2.6 KiB
Go
Raw Normal View History

package commands
import (
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2018-03-14 05:01:55 -07:00
"github.com/cosmos/cosmos-sdk/client"
2018-03-03 11:07:50 -08:00
"github.com/cosmos/cosmos-sdk/client/builder"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-02 01:24:07 -08:00
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank"
2018-03-10 09:33:05 -08:00
cryptokeys "github.com/tendermint/go-crypto/keys"
)
const (
2018-03-03 11:41:43 -08:00
flagTo = "to"
flagAmount = "amount"
)
// SendTxCommand will create a send tx and sign it with the given key
2018-03-10 09:33:05 -08:00
func SendTxCmd(Cdc *wire.Codec) *cobra.Command {
cmdr := Commander{Cdc}
cmd := &cobra.Command{
Use: "send",
Short: "Create and sign a send tx",
2018-02-28 17:57:38 -08:00
RunE: cmdr.sendTxCmd,
}
cmd.Flags().String(flagTo, "", "Address to send coins")
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
return cmd
}
2018-03-10 09:33:05 -08:00
type Commander struct {
Cdc *wire.Codec
2018-02-28 17:57:38 -08:00
}
2018-03-10 09:33:05 -08:00
func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
2018-03-03 11:20:58 -08:00
// get the from address
from, err := builder.GetFromAddress()
if err != nil {
return err
}
2018-03-10 09:33:05 -08:00
// parse coins
amount := viper.GetString(flagAmount)
coins, err := sdk.ParseCoins(amount)
if err != nil {
return err
}
// parse destination address
dest := viper.GetString(flagTo)
bz, err := hex.DecodeString(dest)
if err != nil {
return err
}
to := sdk.Address(bz)
2018-03-14 05:01:55 -07:00
// get account name
name := viper.GetString(client.FlagName)
// get password
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password to sign with '%s':", name)
passphrase, err := client.GetPassword(prompt, buf)
if err != nil {
2018-03-03 11:20:58 -08:00
return err
}
2018-03-14 05:01:55 -07:00
// build message
msg := BuildMsg(from, to, coins)
2018-03-03 11:20:58 -08:00
// build and sign the transaction, then broadcast to Tendermint
2018-03-14 05:01:55 -07:00
res, err := builder.SignBuildBroadcast(name, passphrase, msg, c.Cdc)
2018-02-23 01:40:10 -08:00
if err != nil {
2018-03-03 11:20:58 -08:00
return err
}
2018-03-03 11:20:58 -08:00
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
return nil
}
2018-03-10 09:33:05 -08:00
func BuildMsg(from sdk.Address, to sdk.Address, coins sdk.Coins) sdk.Msg {
input := bank.NewInput(from, coins)
output := bank.NewOutput(to, coins)
msg := bank.NewSendMsg([]bank.Input{input}, []bank.Output{output})
return msg
}
2018-02-28 15:26:39 -08:00
2018-03-10 09:33:05 -08:00
func (c Commander) SignMessage(msg sdk.Msg, kb cryptokeys.Keybase, accountName string, password string) ([]byte, error) {
// sign and build
bz := msg.GetSignBytes()
sig, pubkey, err := kb.Sign(accountName, password, bz)
if err != nil {
return nil, err
}
2018-03-10 09:33:05 -08:00
sigs := []sdk.StdSignature{{
PubKey: pubkey,
Signature: sig,
2018-03-15 03:07:40 -07:00
Sequence: viper.GetInt64(client.FlagName),
2018-03-10 09:33:05 -08:00
}}
2018-03-10 09:33:05 -08:00
// marshal bytes
tx := sdk.NewStdTx(msg, sigs)
txBytes, err := c.Cdc.MarshalBinary(tx)
if err != nil {
return nil, err
}
2018-03-10 09:33:05 -08:00
return txBytes, nil
}