github.com/bcskill/bcschain/v3@v3.4.9-beta2/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  	"github.com/bcskill/bcschain/v3/accounts/keystore"
    27  	"github.com/bcskill/bcschain/v3/cmd/utils"
    28  	"github.com/bcskill/bcschain/v3/crypto"
    29  	"github.com/pborman/uuid"
    30  	"github.com/urfave/cli"
    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  		id := uuid.NewRandom()
    90  		key := &keystore.Key{
    91  			Id:         id,
    92  			Address:    crypto.PubkeyToAddress(privateKey.PublicKey),
    93  			PrivateKey: privateKey,
    94  		}
    95  
    96  		// Encrypt key with passphrase.
    97  		passphrase := getPassphrase(ctx, true)
    98  		scryptN, scryptP := keystore.StandardScryptN, keystore.StandardScryptP
    99  		if ctx.Bool("lightkdf") {
   100  			scryptN, scryptP = keystore.LightScryptN, keystore.LightScryptP
   101  		}
   102  		keyjson, err := keystore.EncryptKey(key, passphrase, scryptN, scryptP)
   103  		if err != nil {
   104  			utils.Fatalf("Error encrypting key: %v", err)
   105  		}
   106  
   107  		// Store the file to disk.
   108  		if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
   109  			utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
   110  		}
   111  		if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil {
   112  			utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
   113  		}
   114  
   115  		// Output some information.
   116  		out := outputGenerate{
   117  			Address: key.Address.Hex(),
   118  		}
   119  		if ctx.Bool(jsonFlag.Name) {
   120  			mustPrintJSON(out)
   121  		} else {
   122  			fmt.Println("Address:", out.Address)
   123  		}
   124  		return nil
   125  	},
   126  }