Skip to content

Commit

Permalink
[ioctl] fix -P flag ignoring password input (#4341)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Aug 14, 2024
1 parent 8dcbccf commit 48b9de3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
11 changes: 11 additions & 0 deletions ioctl/cmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ioctl/cmd/account/accountexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion ioctl/cmd/account/accountexportpublic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 2 additions & 5 deletions ioctl/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ioctl/cmd/jwt/jwtsign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 48b9de3

Please sign in to comment.