github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/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  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/google/uuid"
    26  	"github.com/tirogen/go-ethereum/accounts/keystore"
    27  	"github.com/tirogen/go-ethereum/cmd/utils"
    28  	"github.com/tirogen/go-ethereum/crypto"
    29  	"github.com/urfave/cli/v2"
    30  )
    31  
    32  type outputGenerate struct {
    33  	Address      string
    34  	AddressEIP55 string
    35  }
    36  
    37  var (
    38  	privateKeyFlag = &cli.StringFlag{
    39  		Name:  "privatekey",
    40  		Usage: "file containing a raw private key to encrypt",
    41  	}
    42  	lightKDFFlag = &cli.BoolFlag{
    43  		Name:  "lightkdf",
    44  		Usage: "use less secure scrypt parameters",
    45  	}
    46  )
    47  
    48  var commandGenerate = &cli.Command{
    49  	Name:      "generate",
    50  	Usage:     "generate new keyfile",
    51  	ArgsUsage: "[ <keyfile> ]",
    52  	Description: `
    53  Generate a new keyfile.
    54  
    55  If you want to encrypt an existing private key, it can be specified by setting
    56  --privatekey with the location of the file containing the private key.
    57  `,
    58  	Flags: []cli.Flag{
    59  		passphraseFlag,
    60  		jsonFlag,
    61  		privateKeyFlag,
    62  		lightKDFFlag,
    63  	},
    64  	Action: func(ctx *cli.Context) error {
    65  		// Check if keyfile path given and make sure it doesn't already exist.
    66  		keyfilepath := ctx.Args().First()
    67  		if keyfilepath == "" {
    68  			keyfilepath = defaultKeyfileName
    69  		}
    70  		if _, err := os.Stat(keyfilepath); err == nil {
    71  			utils.Fatalf("Keyfile already exists at %s.", keyfilepath)
    72  		} else if !os.IsNotExist(err) {
    73  			utils.Fatalf("Error checking if keyfile exists: %v", err)
    74  		}
    75  
    76  		var privateKey *ecdsa.PrivateKey
    77  		var err error
    78  		if file := ctx.String(privateKeyFlag.Name); file != "" {
    79  			// Load private key from file.
    80  			privateKey, err = crypto.LoadECDSA(file)
    81  			if err != nil {
    82  				utils.Fatalf("Can't load private key: %v", err)
    83  			}
    84  		} else {
    85  			// If not loaded, generate random.
    86  			privateKey, err = crypto.GenerateKey()
    87  			if err != nil {
    88  				utils.Fatalf("Failed to generate random private key: %v", err)
    89  			}
    90  		}
    91  
    92  		// Create the keyfile object with a random UUID.
    93  		UUID, err := uuid.NewRandom()
    94  		if err != nil {
    95  			utils.Fatalf("Failed to generate random uuid: %v", err)
    96  		}
    97  		key := &keystore.Key{
    98  			Id:         UUID,
    99  			Address:    crypto.PubkeyToAddress(privateKey.PublicKey),
   100  			PrivateKey: privateKey,
   101  		}
   102  
   103  		// Encrypt key with passphrase.
   104  		passphrase := getPassphrase(ctx, true)
   105  		scryptN, scryptP := keystore.StandardScryptN, keystore.StandardScryptP
   106  		if ctx.Bool(lightKDFFlag.Name) {
   107  			scryptN, scryptP = keystore.LightScryptN, keystore.LightScryptP
   108  		}
   109  		keyjson, err := keystore.EncryptKey(key, passphrase, scryptN, scryptP)
   110  		if err != nil {
   111  			utils.Fatalf("Error encrypting key: %v", err)
   112  		}
   113  
   114  		// Store the file to disk.
   115  		if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
   116  			utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
   117  		}
   118  		if err := os.WriteFile(keyfilepath, keyjson, 0600); err != nil {
   119  			utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
   120  		}
   121  
   122  		// Output some information.
   123  		out := outputGenerate{
   124  			Address: key.Address.Hex(),
   125  		}
   126  		if ctx.Bool(jsonFlag.Name) {
   127  			mustPrintJSON(out)
   128  		} else {
   129  			fmt.Println("Address:", out.Address)
   130  		}
   131  		return nil
   132  	},
   133  }