github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/qctkey/generate.go (about)

     1  
     2  package main
     3  
     4  import (
     5  	"crypto/ecdsa"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/quickchainproject/quickchain/accounts/keystore"
    12  	"github.com/quickchainproject/quickchain/cmd/utils"
    13  	"github.com/quickchainproject/quickchain/crypto"
    14  	"github.com/pborman/uuid"
    15  	"gopkg.in/urfave/cli.v1"
    16  )
    17  
    18  type outputGenerate struct {
    19  	Address      string
    20  	AddressEIP55 string
    21  }
    22  
    23  var commandGenerate = cli.Command{
    24  	Name:      "generate",
    25  	Usage:     "generate new keyfile",
    26  	ArgsUsage: "[ <keyfile> ]",
    27  	Description: `
    28  Generate a new keyfile.
    29  
    30  If you want to encrypt an existing private key, it can be specified by setting
    31  --privatekey with the location of the file containing the private key.
    32  `,
    33  	Flags: []cli.Flag{
    34  		passphraseFlag,
    35  		jsonFlag,
    36  		cli.StringFlag{
    37  			Name:  "privatekey",
    38  			Usage: "file containing a raw private key to encrypt",
    39  		},
    40  	},
    41  	Action: func(ctx *cli.Context) error {
    42  		// Check if keyfile path given and make sure it doesn't already exist.
    43  		keyfilepath := ctx.Args().First()
    44  		if keyfilepath == "" {
    45  			keyfilepath = defaultKeyfileName
    46  		}
    47  		if _, err := os.Stat(keyfilepath); err == nil {
    48  			utils.Fatalf("Keyfile already exists at %s.", keyfilepath)
    49  		} else if !os.IsNotExist(err) {
    50  			utils.Fatalf("Error checking if keyfile exists: %v", err)
    51  		}
    52  
    53  		var privateKey *ecdsa.PrivateKey
    54  		var err error
    55  		if file := ctx.String("privatekey"); file != "" {
    56  			// Load private key from file.
    57  			privateKey, err = crypto.LoadECDSA(file)
    58  			if err != nil {
    59  				utils.Fatalf("Can't load private key: %v", err)
    60  			}
    61  		} else {
    62  			// If not loaded, generate random.
    63  			privateKey, err = crypto.GenerateKey()
    64  			if err != nil {
    65  				utils.Fatalf("Failed to generate random private key: %v", err)
    66  			}
    67  		}
    68  
    69  		// Create the keyfile object with a random UUID.
    70  		id := uuid.NewRandom()
    71  		key := &keystore.Key{
    72  			Id:         id,
    73  			Address:    crypto.PubkeyToAddress(privateKey.PublicKey),
    74  			PrivateKey: privateKey,
    75  		}
    76  
    77  		// Encrypt key with passphrase.
    78  		passphrase := getPassPhrase(ctx, true)
    79  		keyjson, err := keystore.EncryptKey(key, passphrase, keystore.StandardScryptN, keystore.StandardScryptP)
    80  		if err != nil {
    81  			utils.Fatalf("Error encrypting key: %v", err)
    82  		}
    83  
    84  		// Store the file to disk.
    85  		if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
    86  			utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
    87  		}
    88  		if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil {
    89  			utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
    90  		}
    91  
    92  		// Output some information.
    93  		out := outputGenerate{
    94  			Address: key.Address.Hex(),
    95  		}
    96  		if ctx.Bool(jsonFlag.Name) {
    97  			mustPrintJSON(out)
    98  		} else {
    99  			fmt.Println("Address:", out.Address)
   100  		}
   101  		return nil
   102  	},
   103  }