fix cli to sign StdSignDoc. fixes #620

This commit is contained in:
Ethan Buchman 2018-03-14 13:11:19 +01:00
parent ee0b396bad
commit 2336a20f5a
4 changed files with 50 additions and 8 deletions

View File

@ -88,7 +88,7 @@ func GetFromAddress() (from sdk.Address, err error) {
} }
// sign and build the transaction from the msg // sign and build the transaction from the msg
func SignAndBuild(msg sdk.Msg, cdc *wire.Codec) ([]byte, error) { func SignAndBuild(signMsg sdk.StdSignMsg, cdc *wire.Codec) ([]byte, error) {
keybase, err := keys.GetKeyBase() keybase, err := keys.GetKeyBase()
if err != nil { if err != nil {
@ -97,7 +97,7 @@ func SignAndBuild(msg sdk.Msg, cdc *wire.Codec) ([]byte, error) {
name := viper.GetString(client.FlagName) name := viper.GetString(client.FlagName)
// sign and build // sign and build
bz := msg.GetSignBytes() bz := signMsg.Bytes()
buf := client.BufferStdin() buf := client.BufferStdin()
prompt := fmt.Sprintf("Password to sign with '%s':", name) prompt := fmt.Sprintf("Password to sign with '%s':", name)
passphrase, err := client.GetPassword(prompt, buf) passphrase, err := client.GetPassword(prompt, buf)
@ -115,14 +115,14 @@ func SignAndBuild(msg sdk.Msg, cdc *wire.Codec) ([]byte, error) {
}} }}
// marshal bytes // marshal bytes
tx := sdk.NewStdTx(msg, sigs) tx := sdk.NewStdTx(signMsg.Msg, sigs)
return cdc.MarshalBinary(tx) return cdc.MarshalBinary(tx)
} }
// sign and build the transaction from the msg // sign and build the transaction from the msg
func SignBuildBroadcast(msg sdk.Msg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) { func SignBuildBroadcast(signMsg sdk.StdSignMsg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) {
txBytes, err := SignAndBuild(msg, cdc) txBytes, err := SignAndBuild(signMsg, cdc)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -5,8 +5,11 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "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/builder"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/examples/basecoin/x/cool" "github.com/cosmos/cosmos-sdk/examples/basecoin/x/cool"
@ -30,9 +33,17 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
// create the message // create the message
msg := cool.NewQuizMsg(from, args[0]) msg := cool.NewQuizMsg(from, args[0])
chainID := viper.GetString(client.FlagChainID)
sequence := int64(viper.GetInt(client.FlagSequence))
signMsg := sdk.StdSignMsg{
ChainID: chainID,
Sequences: []int64{sequence},
Msg: msg,
}
// build and sign the transaction, then broadcast to Tendermint // build and sign the transaction, then broadcast to Tendermint
res, err := builder.SignBuildBroadcast(msg, cdc) res, err := builder.SignBuildBroadcast(signMsg, cdc)
if err != nil { if err != nil {
return err return err
} }
@ -61,9 +72,17 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
// create the message // create the message
msg := cool.NewSetTrendMsg(from, args[0]) msg := cool.NewSetTrendMsg(from, args[0])
chainID := viper.GetString(client.FlagChainID)
sequence := int64(viper.GetInt(client.FlagSequence))
signMsg := sdk.StdSignMsg{
ChainID: chainID,
Sequences: []int64{sequence},
Msg: msg,
}
// build and sign the transaction, then broadcast to Tendermint // build and sign the transaction, then broadcast to Tendermint
res, err := builder.SignBuildBroadcast(msg, cdc) res, err := builder.SignBuildBroadcast(signMsg, cdc)
if err != nil { if err != nil {
return err return err
} }

View File

@ -80,6 +80,19 @@ type StdSignDoc struct {
AltBytes []byte `json:"alt_bytes"` // TODO: do we really want this ? AltBytes []byte `json:"alt_bytes"` // TODO: do we really want this ?
} }
// StdSignMsg is a convenience structure for passing along
// a Msg with the other requirements for a StdSignDoc before
// it is signed. For use in the CLI
type StdSignMsg struct {
ChainID string
Sequences []int64
Msg Msg
}
func (msg StdSignMsg) Bytes() []byte {
return StdSignBytes(msg.ChainID, msg.Sequences, msg.Msg)
}
func StdSignBytes(chainID string, sequences []int64, msg Msg) []byte { func StdSignBytes(chainID string, sequences []int64, msg Msg) []byte {
bz, err := json.Marshal(StdSignDoc{ bz, err := json.Marshal(StdSignDoc{
ChainID: chainID, ChainID: chainID,

View File

@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/builder" "github.com/cosmos/cosmos-sdk/client/builder"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
@ -50,8 +51,17 @@ func (c commander) sendTxCmd(cmd *cobra.Command, args []string) error {
return err return err
} }
chainID := viper.GetString(client.FlagChainID)
sequence := int64(viper.GetInt(client.FlagSequence))
signMsg := sdk.StdSignMsg{
ChainID: chainID,
Sequences: []int64{sequence},
Msg: msg,
}
// build and sign the transaction, then broadcast to Tendermint // build and sign the transaction, then broadcast to Tendermint
res, err := builder.SignBuildBroadcast(msg, c.cdc) res, err := builder.SignBuildBroadcast(signMsg, c.cdc)
if err != nil { if err != nil {
return err return err
} }