From 6d35b1f934e01ff140131370de6fbfc79c85eb78 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 14 Jul 2017 12:29:26 +0200 Subject: [PATCH] Clean up nonce wrapper in cli --- cmd/basecli/commands/cmds.go | 41 ++++++++++--------- context.go | 4 ++ .../cmd/countercli/commands/counter.go | 16 ++------ 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/cmd/basecli/commands/cmds.go b/cmd/basecli/commands/cmds.go index 832151ca1d..1a8118f0dd 100644 --- a/cmd/basecli/commands/cmds.go +++ b/cmd/basecli/commands/cmds.go @@ -66,16 +66,6 @@ func doSendTx(cmd *cobra.Command, args []string) error { return err } - //get the nonce accounts - sendTx, ok := tx.Unwrap().(coin.SendTx) - if !ok { - return errors.New("tx not SendTx") - } - var nonceAccount []basecoin.Actor - for _, input := range sendTx.Inputs { - nonceAccount = append(nonceAccount, input.Address) - } - // TODO: make this more flexible for middleware tx, err = WrapFeeTx(tx) if err != nil { @@ -85,13 +75,10 @@ func doSendTx(cmd *cobra.Command, args []string) error { if err != nil { return err } - - //add the nonce tx layer to the tx - seq := viper.GetInt(FlagSequence) - if seq < 0 { - return fmt.Errorf("sequence must be greater than 0") + tx, err = WrapNonceTx(tx) + if err != nil { + return err } - tx = nonce.NewTx(uint32(seq), nonceAccount, tx) // Note: this is single sig (no multi sig yet) stx := auth.NewSig(tx) @@ -106,6 +93,20 @@ func doSendTx(cmd *cobra.Command, args []string) error { return txcmd.OutputTx(bres) } +// WrapNonceTx grabs the sequence number from the flag and wraps +// the tx with this nonce. Grabs the permission from the signer, +// as we still only support single sig on the cli +func WrapNonceTx(tx basecoin.Tx) (res basecoin.Tx, err error) { + //add the nonce tx layer to the tx + seq := viper.GetInt(FlagSequence) + if seq < 0 { + return res, fmt.Errorf("sequence must be greater than 0") + } + signers := []basecoin.Actor{GetSignerAct()} + tx = nonce.NewTx(uint32(seq), signers, tx) + return tx, nil +} + // WrapFeeTx checks for FlagFee and if present wraps the tx with a // FeeTx of the given amount, paid by the signer func WrapFeeTx(tx basecoin.Tx) (res basecoin.Tx, err error) { @@ -118,7 +119,7 @@ func WrapFeeTx(tx basecoin.Tx) (res basecoin.Tx, err error) { if toll.IsZero() { return tx, nil } - return fee.NewFee(tx, toll, getSignerAddr()), nil + return fee.NewFee(tx, toll, GetSignerAct()), nil } // WrapChainTx will wrap the tx with a ChainTx from the standard flags @@ -132,7 +133,9 @@ func WrapChainTx(tx basecoin.Tx) (res basecoin.Tx, err error) { return res, nil } -func getSignerAddr() (res basecoin.Actor) { +// GetSignerAct returns the address of the signer of the tx +// (as we still only support single sig) +func GetSignerAct() (res basecoin.Actor) { // this could be much cooler with multisig... signer := txcmd.GetSigner() if !signer.Empty() { @@ -157,7 +160,7 @@ func readSendTxFlags() (tx basecoin.Tx, err error) { // craft the inputs and outputs ins := []coin.TxInput{{ - Address: getSignerAddr(), + Address: GetSignerAct(), Coins: amountCoins, }} outs := []coin.TxOutput{{ diff --git a/context.go b/context.go index 1308e797c3..65e9bf8abd 100644 --- a/context.go +++ b/context.go @@ -38,6 +38,10 @@ func (a Actor) Equals(b Actor) bool { bytes.Equal(a.Address, b.Address) } +func (a Actor) Empty() bool { + return a.ChainID == "" && a.App == "" && len(a.Address) == 0 +} + // Context is an interface, so we can implement "secure" variants that // rely on private fields to control the actions type Context interface { diff --git a/docs/guide/counter/cmd/countercli/commands/counter.go b/docs/guide/counter/cmd/countercli/commands/counter.go index 3ab9e8e26d..995ec23f30 100644 --- a/docs/guide/counter/cmd/countercli/commands/counter.go +++ b/docs/guide/counter/cmd/countercli/commands/counter.go @@ -1,8 +1,6 @@ package commands import ( - "fmt" - "github.com/spf13/cobra" "github.com/spf13/viper" @@ -13,7 +11,6 @@ import ( "github.com/tendermint/basecoin/docs/guide/counter/plugins/counter" "github.com/tendermint/basecoin/modules/auth" "github.com/tendermint/basecoin/modules/coin" - "github.com/tendermint/basecoin/modules/nonce" ) //CounterTxCmd is the CLI command to execute the counter @@ -58,10 +55,6 @@ func counterTx(cmd *cobra.Command, args []string) error { return err } - //get the nonce accounts - var addr []byte - nonceAccount := []basecoin.Actor{basecoin.NewActor(counter.NameCounter, addr)} - // TODO: make this more flexible for middleware tx, err = bcmd.WrapFeeTx(tx) if err != nil { @@ -71,13 +64,10 @@ func counterTx(cmd *cobra.Command, args []string) error { if err != nil { return err } - - //add the nonce tx layer to the tx - seq := viper.GetInt(FlagSequence) - if seq < 0 { - return fmt.Errorf("sequence must be greater than 0") + tx, err = bcmd.WrapNonceTx(tx) + if err != nil { + return err } - tx = nonce.NewTx(uint32(seq), nonceAccount, tx) stx := auth.NewSig(tx)