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