changepassword.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2018 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. "fmt"
  19. "io/ioutil"
  20. "strings"
  21. "github.com/ethereum/go-ethereum/accounts/keystore"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "gopkg.in/urfave/cli.v1"
  24. )
  25. var newPassphraseFlag = cli.StringFlag{
  26. Name: "newpasswordfile",
  27. Usage: "the file that contains the new password for the keyfile",
  28. }
  29. var commandChangePassphrase = cli.Command{
  30. Name: "changepassword",
  31. Usage: "change the password on a keyfile",
  32. ArgsUsage: "<keyfile>",
  33. Description: `
  34. Change the password of a keyfile.`,
  35. Flags: []cli.Flag{
  36. passphraseFlag,
  37. newPassphraseFlag,
  38. },
  39. Action: func(ctx *cli.Context) error {
  40. keyfilepath := ctx.Args().First()
  41. // Read key from file.
  42. keyjson, err := ioutil.ReadFile(keyfilepath)
  43. if err != nil {
  44. utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
  45. }
  46. // Decrypt key with passphrase.
  47. passphrase := getPassphrase(ctx, false)
  48. key, err := keystore.DecryptKey(keyjson, passphrase)
  49. if err != nil {
  50. utils.Fatalf("Error decrypting key: %v", err)
  51. }
  52. // Get a new passphrase.
  53. fmt.Println("Please provide a new password")
  54. var newPhrase string
  55. if passFile := ctx.String(newPassphraseFlag.Name); passFile != "" {
  56. content, err := ioutil.ReadFile(passFile)
  57. if err != nil {
  58. utils.Fatalf("Failed to read new password file '%s': %v", passFile, err)
  59. }
  60. newPhrase = strings.TrimRight(string(content), "\r\n")
  61. } else {
  62. newPhrase = utils.GetPassPhrase("", true)
  63. }
  64. // Encrypt the key with the new passphrase.
  65. newJson, err := keystore.EncryptKey(key, newPhrase, keystore.StandardScryptN, keystore.StandardScryptP)
  66. if err != nil {
  67. utils.Fatalf("Error encrypting with new password: %v", err)
  68. }
  69. // Then write the new keyfile in place of the old one.
  70. if err := ioutil.WriteFile(keyfilepath, newJson, 0600); err != nil {
  71. utils.Fatalf("Error writing new keyfile to disk: %v", err)
  72. }
  73. // Don't print anything. Just return successfully,
  74. // producing a positive exit code.
  75. return nil
  76. },
  77. }