Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #4470 #4472

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 52 additions & 20 deletions ioctl/cmd/account/accountcreateadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/v2/ioctl/config"
"github.com/iotexproject/iotex-core/v2/ioctl/output"
"github.com/iotexproject/iotex-core/v2/ioctl/validator"
Expand Down Expand Up @@ -43,50 +44,81 @@ var _accountCreateAddCmd = &cobra.Command{
}

func accountCreateAdd(args []string) error {
// Validate inputs
// Validate alias
if err := validator.ValidateAlias(args[0]); err != nil {
return output.NewError(output.ValidationError, "invalid alias", err)
}
alias := args[0]
if addr, ok := config.ReadConfig.Aliases[alias]; ok {
var confirm string
info := fmt.Sprintf("** Alias \"%s\" has already used for %s\n"+
"Overwriting the account will keep the previous keystore file stay, "+
"but bind the alias to the new one.\nWould you like to continue?\n", alias, addr)
message := output.ConfirmationMessage{Info: info, Options: []string{"yes"}}
fmt.Println(message.String())
if _, err := fmt.Scanf("%s", &confirm); err != nil {
return output.NewError(output.InputError, "failed to input yes", err)

// Check for existing alias and confirm overwrite if necessary
if io1Addr, ok := config.ReadConfig.Aliases[alias]; ok {
// Convert io1 to 0x format for display
iotexAddr, err := address.FromString(io1Addr)
if err != nil {
return output.NewError(output.AddressError, "failed to parse existing alias address", err)
}
ethAddr := iotexAddr.Hex()

// Show confirmation prompt in "0x (io1)" format
proceed, err := confirmOverwrite(alias, fmt.Sprintf("%s (%s)", ethAddr, io1Addr))
if err != nil {
return err
}
if !strings.EqualFold(confirm, "yes") {
if !proceed {
output.PrintResult("quit")
return nil
}
}

var addr string
// Create new account based on crypto type
var io1Addr string
var err error
if CryptoSm2 {
addr, err = newAccountSm2(alias)
io1Addr, err = newAccountSm2(alias)
if err != nil {
return output.NewError(0, "", err)
return output.NewError(output.CryptoError, "failed to create SM2 account", err)
}
} else {
addr, err = newAccount(alias)
io1Addr, err = newAccount(alias)
if err != nil {
return output.NewError(0, "", err)
return output.NewError(output.CryptoError, "failed to create account", err)
}
}
config.ReadConfig.Aliases[alias] = addr

// Convert io1 address to 0x format for display
iotexAddr, err := address.FromString(io1Addr)
if err != nil {
return output.NewError(output.AddressError, "failed to convert to address struct", err)
}
ethAddr := iotexAddr.Hex()

// Update alias configuration and write changes to the config file
config.ReadConfig.Aliases[alias] = io1Addr
out, err := yaml.Marshal(&config.ReadConfig)
if err != nil {
return output.NewError(output.SerializationError, "failed to marshal config", err)
}
if err := os.WriteFile(config.DefaultConfigFile, out, 0600); err != nil {
return output.NewError(output.WriteFileError,
fmt.Sprintf("failed to write to config file %s", config.DefaultConfigFile), err)
return output.NewError(output.WriteFileError, fmt.Sprintf("failed to write to config file %s", config.DefaultConfigFile), err)
}

// Display both formats in "0x (io1)" format on a single line
output.PrintResult(fmt.Sprintf("New account \"%s\" is created.\n"+
"Please Keep your password, or you will lose your private key.", alias))
"Please keep your password safe, or you will lose access to your private key.\n\n"+
"Account address:\n%s (%s)", alias, ethAddr, io1Addr))

return nil
}

func confirmOverwrite(alias, formattedAddress string) (bool, error) {
info := fmt.Sprintf("** Alias \"%s\" is already used for:\n\n%s\n\n"+
"Overwriting the account will keep the previous keystore file, "+
"but bind the alias to the new one.\nWould you like to continue? [yes/no]", alias, formattedAddress)
fmt.Println(info)

var confirm string
if _, err := fmt.Scanf("%s", &confirm); err != nil {
return false, output.NewError(output.InputError, "failed to read confirmation input", err)
}
return strings.EqualFold(confirm, "yes"), nil
}
Loading