generate.go 3.6 KB

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