From 96d9c55172ed0ceec959cd0c1ddacd3905c863da Mon Sep 17 00:00:00 2001 From: Yukai Tu Date: Sat, 24 Mar 2018 23:08:27 -0700 Subject: [PATCH] Encaptulate Get Passphrase Logic in Builder --- client/builder/builder.go | 14 +++++++++++++- examples/basecoin/x/cool/commands/tx.go | 20 ++------------------ x/bank/commands/sendtx.go | 10 +--------- x/ibc/commands/ibctx.go | 8 +------- x/ibc/commands/relay.go | 8 +++----- x/staking/commands/commands.go | 9 +-------- 6 files changed, 21 insertions(+), 48 deletions(-) diff --git a/client/builder/builder.go b/client/builder/builder.go index 2fb1c824e8..ce8ad04958 100644 --- a/client/builder/builder.go +++ b/client/builder/builder.go @@ -124,7 +124,12 @@ func SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *wire.Codec) ([]byte } // sign and build the transaction from the msg -func SignBuildBroadcast(name string, passphrase string, msg sdk.Msg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) { +func SignBuildBroadcast(name string, msg sdk.Msg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) { + passphrase, err := GetPassphraseFromStdin(name) + if err != nil { + return nil, err + } + txBytes, err := SignAndBuild(name, passphrase, msg, cdc) if err != nil { return nil, err @@ -132,3 +137,10 @@ func SignBuildBroadcast(name string, passphrase string, msg sdk.Msg, cdc *wire.C return BroadcastTx(txBytes) } + +// get passphrase from std input +func GetPassphraseFromStdin(name string) (pass string, err error) { + buf := client.BufferStdin() + prompt := fmt.Sprintf("Password to sign with '%s':", name) + return client.GetPassword(prompt, buf) +} diff --git a/examples/basecoin/x/cool/commands/tx.go b/examples/basecoin/x/cool/commands/tx.go index f06eb8af42..c2d643a905 100644 --- a/examples/basecoin/x/cool/commands/tx.go +++ b/examples/basecoin/x/cool/commands/tx.go @@ -36,16 +36,8 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command { // 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 { - return err - } - // build and sign the transaction, then broadcast to Tendermint - res, err := builder.SignBuildBroadcast(name, passphrase, msg, cdc) + res, err := builder.SignBuildBroadcast(name, msg, cdc) if err != nil { return err } @@ -75,19 +67,11 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command { // 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 { - return err - } - // create the message msg := cool.NewSetTrendMsg(from, args[0]) // build and sign the transaction, then broadcast to Tendermint - res, err := builder.SignBuildBroadcast(name, passphrase, msg, cdc) + res, err := builder.SignBuildBroadcast(name, msg, cdc) if err != nil { return err } diff --git a/x/bank/commands/sendtx.go b/x/bank/commands/sendtx.go index 5d1a6e05cf..5619b4d0f3 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -62,19 +62,11 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error { // 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 { - return err - } - // build message msg := BuildMsg(from, to, coins) // build and sign the transaction, then broadcast to Tendermint - res, err := builder.SignBuildBroadcast(name, passphrase, msg, c.Cdc) + res, err := builder.SignBuildBroadcast(name, msg, c.Cdc) if err != nil { return err } diff --git a/x/ibc/commands/ibctx.go b/x/ibc/commands/ibctx.go index 17d1e00483..e0186b7175 100644 --- a/x/ibc/commands/ibctx.go +++ b/x/ibc/commands/ibctx.go @@ -53,14 +53,8 @@ func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error // get password name := viper.GetString(client.FlagName) - buf := client.BufferStdin() - prompt := fmt.Sprintf("Password to sign with '%s':", name) - passphrase, err := client.GetPassword(prompt, buf) - if err != nil { - return err - } - res, err := builder.SignBuildBroadcast(name, passphrase, msg, c.cdc) + res, err := builder.SignBuildBroadcast(name, msg, c.cdc) if err != nil { return err } diff --git a/x/ibc/commands/relay.go b/x/ibc/commands/relay.go index 091b0e9206..9f6647ba5b 100644 --- a/x/ibc/commands/relay.go +++ b/x/ibc/commands/relay.go @@ -27,7 +27,7 @@ const ( type relayCommander struct { cdc *wire.Codec address sdk.Address - decoder sdk.AccountDecoder + decoder sdk.AccountDecoder mainStore string ibcStore string } @@ -35,7 +35,7 @@ type relayCommander struct { func IBCRelayCmd(cdc *wire.Codec) *cobra.Command { cmdr := relayCommander{ cdc: cdc, - decoder: authcmd.GetAccountDecoder(cdc), + decoder: authcmd.GetAccountDecoder(cdc), ibcStore: "ibc", mainStore: "main", } @@ -80,9 +80,7 @@ func (c relayCommander) runIBCRelay(cmd *cobra.Command, args []string) { func (c relayCommander) loop(fromChainID, fromChainNode, toChainID, toChainNode string) { // get password name := viper.GetString(client.FlagName) - buf := client.BufferStdin() - prompt := fmt.Sprintf("Password to sign with '%s':", name) - passphrase, err := client.GetPassword(prompt, buf) + passphrase, err := builder.GetPassphraseFromStdin(name) if err != nil { panic(err) } diff --git a/x/staking/commands/commands.go b/x/staking/commands/commands.go index c2830d3951..79520ebeff 100644 --- a/x/staking/commands/commands.go +++ b/x/staking/commands/commands.go @@ -94,14 +94,7 @@ func (co commander) unbondTxCmd(cmd *cobra.Command, args []string) error { func (co commander) sendMsg(msg sdk.Msg) error { name := viper.GetString(client.FlagName) - buf := client.BufferStdin() - prompt := fmt.Sprintf("Password to sign with '%s':", name) - passphrase, err := client.GetPassword(prompt, buf) - if err != nil { - return err - } - - res, err := builder.SignBuildBroadcast(name, passphrase, msg, co.cdc) + res, err := builder.SignBuildBroadcast(name, msg, co.cdc) if err != nil { return err }