github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/ethkey/changepassword.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package main 19 20 import ( 21 "fmt" 22 "io/ioutil" 23 "strings" 24 25 "github.com/AigarNetwork/aigar/accounts/keystore" 26 "github.com/AigarNetwork/aigar/cmd/utils" 27 "gopkg.in/urfave/cli.v1" 28 ) 29 30 var newPassphraseFlag = cli.StringFlag{ 31 Name: "newpasswordfile", 32 Usage: "the file that contains the new password for the keyfile", 33 } 34 35 var commandChangePassphrase = cli.Command{ 36 Name: "changepassword", 37 Usage: "change the password on a keyfile", 38 ArgsUsage: "<keyfile>", 39 Description: ` 40 Change the password of a keyfile.`, 41 Flags: []cli.Flag{ 42 passphraseFlag, 43 newPassphraseFlag, 44 }, 45 Action: func(ctx *cli.Context) error { 46 keyfilepath := ctx.Args().First() 47 48 // Read key from file. 49 keyjson, err := ioutil.ReadFile(keyfilepath) 50 if err != nil { 51 utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err) 52 } 53 54 // Decrypt key with passphrase. 55 passphrase := getPassphrase(ctx) 56 key, err := keystore.DecryptKey(keyjson, passphrase) 57 if err != nil { 58 utils.Fatalf("Error decrypting key: %v", err) 59 } 60 61 // Get a new passphrase. 62 fmt.Println("Please provide a new password") 63 var newPhrase string 64 if passFile := ctx.String(newPassphraseFlag.Name); passFile != "" { 65 content, err := ioutil.ReadFile(passFile) 66 if err != nil { 67 utils.Fatalf("Failed to read new password file '%s': %v", passFile, err) 68 } 69 newPhrase = strings.TrimRight(string(content), "\r\n") 70 } else { 71 newPhrase = promptPassphrase(true) 72 } 73 74 // Encrypt the key with the new passphrase. 75 newJson, err := keystore.EncryptKey(key, newPhrase, keystore.StandardScryptN, keystore.StandardScryptP) 76 if err != nil { 77 utils.Fatalf("Error encrypting with new password: %v", err) 78 } 79 80 // Then write the new keyfile in place of the old one. 81 if err := ioutil.WriteFile(keyfilepath, newJson, 600); err != nil { 82 utils.Fatalf("Error writing new keyfile to disk: %v", err) 83 } 84 85 // Don't print anything. Just return successfully, 86 // producing a positive exit code. 87 return nil 88 }, 89 }