github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/set/passphrase/passphrase.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package passphrase
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  
    15  	"github.com/ethereum/go-ethereum/accounts/keystore"
    16  	"github.com/spf13/cobra"
    17  
    18  	"github.com/vchain-us/vcn/internal/assert"
    19  	"github.com/vchain-us/vcn/pkg/api"
    20  	"github.com/vchain-us/vcn/pkg/cmd/internal/cli"
    21  	"github.com/vchain-us/vcn/pkg/store"
    22  )
    23  
    24  // NewCommand returns the cobra command for `vcn set notarization-password`
    25  func NewCommand() *cobra.Command {
    26  	cmd := &cobra.Command{
    27  		Use:     "notarization-password",
    28  		Aliases: []string{"passphrase"},
    29  		Short:   "Change the notarization password for the current user",
    30  		Long:    `This command allows you to set a custom notarization password.`,
    31  		RunE:    runPasshphrase,
    32  		Args:    cobra.NoArgs,
    33  	}
    34  
    35  	return cmd
    36  }
    37  
    38  func runPasshphrase(cmd *cobra.Command, args []string) error {
    39  
    40  	cmd.SilenceUsage = true
    41  
    42  	// User
    43  	if err := assert.UserLogin(); err != nil {
    44  		return err
    45  	}
    46  	u := api.NewUser(store.Config().CurrentContext.Email)
    47  	fmt.Printf("User:	%s\n", u.Email())
    48  
    49  	secret, id, offline, err := u.Secret()
    50  	if err != nil {
    51  		return err
    52  	}
    53  	if offline {
    54  		return fmt.Errorf("offline secret is not supported by the current vcn version")
    55  	}
    56  	fmt.Printf("SignerID:	%s\n", id)
    57  
    58  	pass, err := cli.ProvidePasswordWithMessage("Enter your current notarization password: ")
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	buf := new(bytes.Buffer)
    64  	buf.ReadFrom(bytes.NewReader([]byte(secret)))
    65  	key, err := keystore.DecryptKey(buf.Bytes(), pass)
    66  	if err != nil {
    67  		if err.Error() == "could not decrypt key with given passphrase" {
    68  			return fmt.Errorf("incorrect password")
    69  		}
    70  		return err
    71  	}
    72  
    73  	pass, err = cli.PromptPassphrase()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	b, err := keystore.EncryptKey(key, pass, keystore.StandardScryptN, keystore.StandardScryptP)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	err = u.UploadSecret(bytes.NewReader(b), pass)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	fmt.Println("You have successfully updated the password.")
    88  	return nil
    89  }