github.com/core-coin/go-core/v2@v2.1.9/cmd/xcbkey/changepassword.go (about)

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