github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/internal/cli/account_new.go (about)

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