github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/internal/cli/account_import.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 6 "github.com/ethereum/go-ethereum/cmd/utils" 7 "github.com/ethereum/go-ethereum/crypto" 8 "github.com/ethereum/go-ethereum/internal/cli/flagset" 9 ) 10 11 type AccountImportCommand struct { 12 *Meta 13 } 14 15 // Help implements the cli.Command interface 16 func (a *AccountImportCommand) Help() string { 17 return `Usage: bor account import 18 19 Import a private key into a new account. 20 21 Import an account: 22 23 $ bor account import key.json 24 25 ` + a.Flags().Help() 26 } 27 28 func (a *AccountImportCommand) Flags() *flagset.Flagset { 29 return a.NewFlagSet("account import") 30 } 31 32 // Synopsis implements the cli.Command interface 33 func (a *AccountImportCommand) Synopsis() string { 34 return "Import a private key into a new account" 35 } 36 37 // Run implements the cli.Command interface 38 func (a *AccountImportCommand) Run(args []string) int { 39 flags := a.Flags() 40 if err := flags.Parse(args); err != nil { 41 a.UI.Error(err.Error()) 42 return 1 43 } 44 45 args = flags.Args() 46 if len(args) != 1 { 47 a.UI.Error("Expected one argument") 48 return 1 49 } 50 key, err := crypto.LoadECDSA(args[0]) 51 if err != nil { 52 a.UI.Error(fmt.Sprintf("Failed to load the private key '%s': %v", args[0], err)) 53 return 1 54 } 55 56 keystore, err := a.GetKeystore() 57 if err != nil { 58 a.UI.Error(fmt.Sprintf("Failed to get keystore: %v", err)) 59 return 1 60 } 61 62 password, err := a.AskPassword() 63 if err != nil { 64 a.UI.Error(err.Error()) 65 return 1 66 } 67 68 acct, err := keystore.ImportECDSA(key, password) 69 if err != nil { 70 utils.Fatalf("Could not create the account: %v", err) 71 } 72 a.UI.Output(fmt.Sprintf("Account created: %s", acct.Address.String())) 73 return 0 74 }