generate.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/google/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. UUID, err := uuid.NewRandom()
  82. if err != nil {
  83. utils.Fatalf("Failed to generate random uuid: %v", err)
  84. }
  85. key := &keystore.Key{
  86. Id: UUID,
  87. Address: crypto.PubkeyToAddress(privateKey.PublicKey),
  88. PrivateKey: privateKey,
  89. }
  90. // Encrypt key with passphrase.
  91. passphrase := getPassphrase(ctx, true)
  92. scryptN, scryptP := keystore.StandardScryptN, keystore.StandardScryptP
  93. if ctx.Bool("lightkdf") {
  94. scryptN, scryptP = keystore.LightScryptN, keystore.LightScryptP
  95. }
  96. keyjson, err := keystore.EncryptKey(key, passphrase, scryptN, scryptP)
  97. if err != nil {
  98. utils.Fatalf("Error encrypting key: %v", err)
  99. }
  100. // Store the file to disk.
  101. if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
  102. utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
  103. }
  104. if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil {
  105. utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
  106. }
  107. // Output some information.
  108. out := outputGenerate{
  109. Address: key.Address.Hex(),
  110. }
  111. if ctx.Bool(jsonFlag.Name) {
  112. mustPrintJSON(out)
  113. } else {
  114. fmt.Println("Address:", out.Address)
  115. }
  116. return nil
  117. },
  118. }