github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/cmd/ethkey/generate.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package main 13 14 import ( 15 "crypto/ecdsa" 16 "fmt" 17 "io/ioutil" 18 "os" 19 "path/filepath" 20 21 "github.com/Sberex/go-sberex/accounts/keystore" 22 "github.com/Sberex/go-sberex/cmd/utils" 23 "github.com/Sberex/go-sberex/crypto" 24 "github.com/pborman/uuid" 25 "gopkg.in/urfave/cli.v1" 26 ) 27 28 type outputGenerate struct { 29 Address string 30 AddressEIP55 string 31 } 32 33 var commandGenerate = cli.Command{ 34 Name: "generate", 35 Usage: "generate new keyfile", 36 ArgsUsage: "[ <keyfile> ]", 37 Description: ` 38 Generate a new keyfile. 39 40 If you want to encrypt an existing private key, it can be specified by setting 41 --privatekey with the location of the file containing the private key. 42 `, 43 Flags: []cli.Flag{ 44 passphraseFlag, 45 jsonFlag, 46 cli.StringFlag{ 47 Name: "privatekey", 48 Usage: "file containing a raw private key to encrypt", 49 }, 50 }, 51 Action: func(ctx *cli.Context) error { 52 // Check if keyfile path given and make sure it doesn't already exist. 53 keyfilepath := ctx.Args().First() 54 if keyfilepath == "" { 55 keyfilepath = defaultKeyfileName 56 } 57 if _, err := os.Stat(keyfilepath); err == nil { 58 utils.Fatalf("Keyfile already exists at %s.", keyfilepath) 59 } else if !os.IsNotExist(err) { 60 utils.Fatalf("Error checking if keyfile exists: %v", err) 61 } 62 63 var privateKey *ecdsa.PrivateKey 64 var err error 65 if file := ctx.String("privatekey"); file != "" { 66 // Load private key from file. 67 privateKey, err = crypto.LoadECDSA(file) 68 if err != nil { 69 utils.Fatalf("Can't load private key: %v", err) 70 } 71 } else { 72 // If not loaded, generate random. 73 privateKey, err = crypto.GenerateKey() 74 if err != nil { 75 utils.Fatalf("Failed to generate random private key: %v", err) 76 } 77 } 78 79 // Create the keyfile object with a random UUID. 80 id := uuid.NewRandom() 81 key := &keystore.Key{ 82 Id: id, 83 Address: crypto.PubkeyToAddress(privateKey.PublicKey), 84 PrivateKey: privateKey, 85 } 86 87 // Encrypt key with passphrase. 88 passphrase := getPassPhrase(ctx, true) 89 keyjson, err := keystore.EncryptKey(key, passphrase, keystore.StandardScryptN, keystore.StandardScryptP) 90 if err != nil { 91 utils.Fatalf("Error encrypting key: %v", err) 92 } 93 94 // Store the file to disk. 95 if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil { 96 utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath)) 97 } 98 if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil { 99 utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err) 100 } 101 102 // Output some information. 103 out := outputGenerate{ 104 Address: key.Address.Hex(), 105 } 106 if ctx.Bool(jsonFlag.Name) { 107 mustPrintJSON(out) 108 } else { 109 fmt.Println("Address:", out.Address) 110 } 111 return nil 112 }, 113 }