generate.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. },
  50. Action: func(ctx *cli.Context) error {
  51. // Check if keyfile path given and make sure it doesn't already exist.
  52. keyfilepath := ctx.Args().First()
  53. if keyfilepath == "" {
  54. keyfilepath = defaultKeyfileName
  55. }
  56. if _, err := os.Stat(keyfilepath); err == nil {
  57. utils.Fatalf("Keyfile already exists at %s.", keyfilepath)
  58. } else if !os.IsNotExist(err) {
  59. utils.Fatalf("Error checking if keyfile exists: %v", err)
  60. }
  61. var privateKey *ecdsa.PrivateKey
  62. var err error
  63. if file := ctx.String("privatekey"); file != "" {
  64. // Load private key from file.
  65. privateKey, err = crypto.LoadECDSA(file)
  66. if err != nil {
  67. utils.Fatalf("Can't load private key: %v", err)
  68. }
  69. } else {
  70. // If not loaded, generate random.
  71. privateKey, err = crypto.GenerateKey()
  72. if err != nil {
  73. utils.Fatalf("Failed to generate random private key: %v", err)
  74. }
  75. }
  76. // Create the keyfile object with a random UUID.
  77. id := uuid.NewRandom()
  78. key := &keystore.Key{
  79. Id: id,
  80. Address: crypto.PubkeyToAddress(privateKey.PublicKey),
  81. PrivateKey: privateKey,
  82. }
  83. // Encrypt key with passphrase.
  84. passphrase := getPassPhrase(ctx, true)
  85. keyjson, err := keystore.EncryptKey(key, passphrase, keystore.StandardScryptN, keystore.StandardScryptP)
  86. if err != nil {
  87. utils.Fatalf("Error encrypting key: %v", err)
  88. }
  89. // Store the file to disk.
  90. if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
  91. utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
  92. }
  93. if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil {
  94. utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
  95. }
  96. // Output some information.
  97. out := outputGenerate{
  98. Address: key.Address.Hex(),
  99. }
  100. if ctx.Bool(jsonFlag.Name) {
  101. mustPrintJSON(out)
  102. } else {
  103. fmt.Println("Address:", out.Address)
  104. }
  105. return nil
  106. },
  107. }