github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/internal/cli/account_new.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ethereum/go-ethereum/internal/cli/flagset"
     7  )
     8  
     9  type AccountNewCommand struct {
    10  	*Meta
    11  }
    12  
    13  // Help implements the cli.Command interface
    14  func (a *AccountNewCommand) Help() string {
    15  	return `Usage: bor account new
    16  
    17    Create a new local account.
    18  
    19    ` + a.Flags().Help()
    20  }
    21  
    22  func (a *AccountNewCommand) Flags() *flagset.Flagset {
    23  	return a.NewFlagSet("account new")
    24  }
    25  
    26  // Synopsis implements the cli.Command interface
    27  func (a *AccountNewCommand) Synopsis() string {
    28  	return "Create a new local account"
    29  }
    30  
    31  // Run implements the cli.Command interface
    32  func (a *AccountNewCommand) Run(args []string) int {
    33  	flags := a.Flags()
    34  	if err := flags.Parse(args); err != nil {
    35  		a.UI.Error(err.Error())
    36  		return 1
    37  	}
    38  
    39  	keystore, err := a.GetKeystore()
    40  	if err != nil {
    41  		a.UI.Error(fmt.Sprintf("Failed to get keystore: %v", err))
    42  		return 1
    43  	}
    44  
    45  	password, err := a.AskPassword()
    46  	if err != nil {
    47  		a.UI.Error(err.Error())
    48  		return 1
    49  	}
    50  
    51  	account, err := keystore.NewAccount(password)
    52  	if err != nil {
    53  		a.UI.Error(fmt.Sprintf("Failed to create new account: %v", err))
    54  		return 1
    55  	}
    56  
    57  	a.UI.Output("\nYour new key was generated")
    58  	a.UI.Output(fmt.Sprintf("Public address of the key:   %s", account.Address.Hex()))
    59  	a.UI.Output(fmt.Sprintf("Path of the secret key file: %s", account.URL.Path))
    60  
    61  	return 0
    62  }