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