aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/ethkey/generate.go
blob: fe9a0c15192e688a05276a4a89504b19fc618194 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2017 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "crypto/ecdsa"
    "fmt"
    "io/ioutil"
    "os"
    "path/filepath"

    "github.com/ethereum/go-ethereum/accounts/keystore"
    "github.com/ethereum/go-ethereum/cmd/utils"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/pborman/uuid"
    "gopkg.in/urfave/cli.v1"
)

type outputGenerate struct {
    Address      string
    AddressEIP55 string
}

var commandGenerate = cli.Command{
    Name:      "generate",
    Usage:     "generate new keyfile",
    ArgsUsage: "[ <keyfile> ]",
    Description: `
Generate a new keyfile.

If you want to encrypt an existing private key, it can be specified by setting
--privatekey with the location of the file containing the private key.
`,
    Flags: []cli.Flag{
        passphraseFlag,
        jsonFlag,
        cli.StringFlag{
            Name:  "privatekey",
            Usage: "file containing a raw private key to encrypt",
        },
    },
    Action: func(ctx *cli.Context) error {
        // Check if keyfile path given and make sure it doesn't already exist.
        keyfilepath := ctx.Args().First()
        if keyfilepath == "" {
            keyfilepath = defaultKeyfileName
        }
        if _, err := os.Stat(keyfilepath); err == nil {
            utils.Fatalf("Keyfile already exists at %s.", keyfilepath)
        } else if !os.IsNotExist(err) {
            utils.Fatalf("Error checking if keyfile exists: %v", err)
        }

        var privateKey *ecdsa.PrivateKey
        var err error
        if file := ctx.String("privatekey"); file != "" {
            // Load private key from file.
            privateKey, err = crypto.LoadECDSA(file)
            if err != nil {
                utils.Fatalf("Can't load private key: %v", err)
            }
        } else {
            // If not loaded, generate random.
            privateKey, err = crypto.GenerateKey()
            if err != nil {
                utils.Fatalf("Failed to generate random private key: %v", err)
            }
        }

        // Create the keyfile object with a random UUID.
        id := uuid.NewRandom()
        key := &keystore.Key{
            Id:         id,
            Address:    crypto.PubkeyToAddress(privateKey.PublicKey),
            PrivateKey: privateKey,
        }

        // Encrypt key with passphrase.
        passphrase := promptPassphrase(true)
        keyjson, err := keystore.EncryptKey(key, passphrase, keystore.StandardScryptN, keystore.StandardScryptP)
        if err != nil {
            utils.Fatalf("Error encrypting key: %v", err)
        }

        // Store the file to disk.
        if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
            utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
        }
        if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil {
            utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
        }

        // Output some information.
        out := outputGenerate{
            Address: key.Address.Hex(),
        }
        if ctx.Bool(jsonFlag.Name) {
            mustPrintJSON(out)
        } else {
            fmt.Println("Address:", out.Address)
        }
        return nil
    },
}