From 48b9de39d2413c23efc7eada2dffa6cf124d8928 Mon Sep 17 00:00:00 2001 From: dustinxie Date: Tue, 13 Aug 2024 19:00:15 -0700 Subject: [PATCH] [ioctl] fix -P flag ignoring password input (#4341) --- ioctl/cmd/account/account.go | 11 +++++++++++ ioctl/cmd/account/accountexport.go | 2 +- ioctl/cmd/account/accountexportpublic.go | 2 +- ioctl/cmd/action/action.go | 7 ++----- ioctl/cmd/jwt/jwtsign.go | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/ioctl/cmd/account/account.go b/ioctl/cmd/account/account.go index baf0492d68..1abab90da6 100644 --- a/ioctl/cmd/account/account.go +++ b/ioctl/cmd/account/account.go @@ -30,6 +30,7 @@ import ( "github.com/iotexproject/iotex-core/ioctl/cmd/hdwallet" "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/flag" "github.com/iotexproject/iotex-core/ioctl/output" "github.com/iotexproject/iotex-core/ioctl/util" "github.com/iotexproject/iotex-core/ioctl/validator" @@ -59,6 +60,8 @@ var ( // CryptoSm2 is a flag for sm2 cryptographic algorithm var CryptoSm2 bool +var _passwordFlag = flag.NewStringVarP("password", "P", "", "input password for account") + // AccountCmd represents the account command var AccountCmd = &cobra.Command{ Use: "account", @@ -86,6 +89,14 @@ func init() { AccountCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure, config.TranslateInLang(_flagInsecure, config.UILanguage)) } +func RegisterPasswordFlag(cmd *cobra.Command) { + _passwordFlag.RegisterCommand(cmd) +} + +func PasswordByFlag() string { + return _passwordFlag.Value().(string) +} + // Sign sign message with signer func Sign(signer, password, message string) (string, error) { pri, err := PrivateKeyFromSigner(signer, password) diff --git a/ioctl/cmd/account/accountexport.go b/ioctl/cmd/account/accountexport.go index 5e4d871756..7915bad90a 100644 --- a/ioctl/cmd/account/accountexport.go +++ b/ioctl/cmd/account/accountexport.go @@ -50,7 +50,7 @@ func accountExport(arg string) error { return output.NewError(output.AddressError, "failed to get address", err) } } - prvKey, err := PrivateKeyFromSigner(addr, "") + prvKey, err := PrivateKeyFromSigner(addr, PasswordByFlag()) if err != nil { return output.NewError(output.KeystoreError, "failed to get private key from keystore", err) } diff --git a/ioctl/cmd/account/accountexportpublic.go b/ioctl/cmd/account/accountexportpublic.go index 4f6b0c0b3e..ebb53ab751 100644 --- a/ioctl/cmd/account/accountexportpublic.go +++ b/ioctl/cmd/account/accountexportpublic.go @@ -50,7 +50,7 @@ func exportPublic(arg string) error { return output.NewError(output.AddressError, "failed to get address", err) } } - prvKey, err := PrivateKeyFromSigner(addr, "") + prvKey, err := PrivateKeyFromSigner(addr, PasswordByFlag()) if err != nil { return output.NewError(output.KeystoreError, "failed to get private key from keystore", err) } diff --git a/ioctl/cmd/action/action.go b/ioctl/cmd/action/action.go index 86508b3d20..5ade6cb58b 100644 --- a/ioctl/cmd/action/action.go +++ b/ioctl/cmd/action/action.go @@ -49,8 +49,6 @@ var ( const _defaultGasLimit = uint64(20000000) -// var defaultGasPrice = big.NewInt(unit.Qev) - // Flags var ( _gasLimitFlag = flag.NewUint64VarP("gas-limit", "l", _defaultGasLimit, "set gas limit") @@ -59,7 +57,6 @@ var ( _signerFlag = flag.NewStringVarP("signer", "s", "", "choose a signing account") _bytecodeFlag = flag.NewStringVarP("bytecode", "b", "", "set the byte code") _yesFlag = flag.BoolVarP("assume-yes", "y", false, "answer yes for all confirmations") - _passwordFlag = flag.NewStringVarP("password", "P", "", "input password for account") ) // ActionCmd represents the action command @@ -140,7 +137,7 @@ func RegisterWriteCommand(cmd *cobra.Command) { _signerFlag.RegisterCommand(cmd) _nonceFlag.RegisterCommand(cmd) _yesFlag.RegisterCommand(cmd) - _passwordFlag.RegisterCommand(cmd) + account.RegisterPasswordFlag(cmd) } // gasPriceInRau returns the suggest gas price @@ -260,7 +257,7 @@ func SendAction(elp action.Envelope, signer string) error { // SendActionAndResponse sends signed action to blockchain with response and error return func SendActionAndResponse(elp action.Envelope, signer string) (*iotexapi.SendActionResponse, error) { - prvKey, err := account.PrivateKeyFromSigner(signer, _passwordFlag.Value().(string)) + prvKey, err := account.PrivateKeyFromSigner(signer, account.PasswordByFlag()) if err != nil { return nil, err } diff --git a/ioctl/cmd/jwt/jwtsign.go b/ioctl/cmd/jwt/jwtsign.go index 8934f2c58c..fa96c97740 100644 --- a/ioctl/cmd/jwt/jwtsign.go +++ b/ioctl/cmd/jwt/jwtsign.go @@ -70,7 +70,7 @@ func jwtSign() error { if err != nil { return output.NewError(output.AddressError, "failed to get signer address", err) } - prvKey, err := account.PrivateKeyFromSigner(signer, "") + prvKey, err := account.PrivateKeyFromSigner(signer, account.PasswordByFlag()) if err != nil { return err }