github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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/google/uuid"
    27  	"gopkg.in/urfave/cli.v1"
    28  
    29  	"github.com/scroll-tech/go-ethereum/accounts/keystore"
    30  	"github.com/scroll-tech/go-ethereum/cmd/utils"
    31  	"github.com/scroll-tech/go-ethereum/crypto"
    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  		cli.BoolFlag{
    57  			Name:  "lightkdf",
    58  			Usage: "use less secure scrypt parameters",
    59  		},
    60  	},
    61  	Action: func(ctx *cli.Context) error {
    62  		// Check if keyfile path given and make sure it doesn't already exist.
    63  		keyfilepath := ctx.Args().First()
    64  		if keyfilepath == "" {
    65  			keyfilepath = defaultKeyfileName
    66  		}
    67  		if _, err := os.Stat(keyfilepath); err == nil {
    68  			utils.Fatalf("Keyfile already exists at %s.", keyfilepath)
    69  		} else if !os.IsNotExist(err) {
    70  			utils.Fatalf("Error checking if keyfile exists: %v", err)
    71  		}
    72  
    73  		var privateKey *ecdsa.PrivateKey
    74  		var err error
    75  		if file := ctx.String("privatekey"); file != "" {
    76  			// Load private key from file.
    77  			privateKey, err = crypto.LoadECDSA(file)
    78  			if err != nil {
    79  				utils.Fatalf("Can't load private key: %v", err)
    80  			}
    81  		} else {
    82  			// If not loaded, generate random.
    83  			privateKey, err = crypto.GenerateKey()
    84  			if err != nil {
    85  				utils.Fatalf("Failed to generate random private key: %v", err)
    86  			}
    87  		}
    88  
    89  		// Create the keyfile object with a random UUID.
    90  		UUID, err := uuid.NewRandom()
    91  		if err != nil {
    92  			utils.Fatalf("Failed to generate random uuid: %v", err)
    93  		}
    94  		key := &keystore.Key{
    95  			Id:         UUID,
    96  			Address:    crypto.PubkeyToAddress(privateKey.PublicKey),
    97  			PrivateKey: privateKey,
    98  		}
    99  
   100  		// Encrypt key with passphrase.
   101  		passphrase := getPassphrase(ctx, true)
   102  		scryptN, scryptP := keystore.StandardScryptN, keystore.StandardScryptP
   103  		if ctx.Bool("lightkdf") {
   104  			scryptN, scryptP = keystore.LightScryptN, keystore.LightScryptP
   105  		}
   106  		keyjson, err := keystore.EncryptKey(key, passphrase, scryptN, scryptP)
   107  		if err != nil {
   108  			utils.Fatalf("Error encrypting key: %v", err)
   109  		}
   110  
   111  		// Store the file to disk.
   112  		if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
   113  			utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
   114  		}
   115  		if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil {
   116  			utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
   117  		}
   118  
   119  		// Output some information.
   120  		out := outputGenerate{
   121  			Address: key.Address.Hex(),
   122  		}
   123  		if ctx.Bool(jsonFlag.Name) {
   124  			mustPrintJSON(out)
   125  		} else {
   126  			fmt.Println("Address:", out.Address)
   127  		}
   128  		return nil
   129  	},
   130  }