generate.go 3.7 KB

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