github.com/isti4github/eth-ecc@v0.0.0-20201227085832-c337f2d99319/cmd/ethkey/changepassword.go (about)

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