github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/hdwallet/hdwalletcreate.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package hdwallet
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  
    12  	"github.com/spf13/cobra"
    13  	"github.com/tyler-smith/go-bip39"
    14  
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/output"
    17  	"github.com/iotexproject/iotex-core/ioctl/util"
    18  	"github.com/iotexproject/iotex-core/pkg/util/fileutil"
    19  )
    20  
    21  // Multi-language support
    22  var (
    23  	_createByMnemonicCmdShorts = map[config.Language]string{
    24  		config.English: "create hdwallet using mnemonic",
    25  		config.Chinese: "通过助记词创建新钱包",
    26  	}
    27  )
    28  
    29  // _hdwalletCreateCmd represents the hdwallet create command
    30  var _hdwalletCreateCmd = &cobra.Command{
    31  	Use:   "create",
    32  	Short: config.TranslateInLang(_createByMnemonicCmdShorts, config.UILanguage),
    33  	Args:  cobra.ExactArgs(0),
    34  	RunE: func(cmd *cobra.Command, args []string) error {
    35  		cmd.SilenceUsage = true
    36  		err := hdwalletCreate()
    37  		return output.PrintError(err)
    38  	},
    39  }
    40  
    41  func hdwalletCreate() error {
    42  	if fileutil.FileExists(_hdWalletConfigFile) {
    43  		output.PrintResult("already created hdwallet, if you forgot password,use delete/import command.")
    44  		return nil
    45  	}
    46  
    47  	output.PrintQuery("Set password\n")
    48  	password, err := util.ReadSecretFromStdin()
    49  	if err != nil {
    50  		return output.NewError(output.InputError, "failed to get password", err)
    51  	}
    52  	output.PrintQuery("Enter password again\n")
    53  	passwordAgain, err := util.ReadSecretFromStdin()
    54  	if err != nil {
    55  		return output.NewError(output.InputError, "failed to get password", err)
    56  	}
    57  	if password != passwordAgain {
    58  		return output.NewError(output.ValidationError, ErrPasswdNotMatch.Error(), nil)
    59  	}
    60  
    61  	entropy, _ := bip39.NewEntropy(128)
    62  	mnemonic, _ := bip39.NewMnemonic(entropy)
    63  
    64  	enctxt := append([]byte(mnemonic), util.HashSHA256([]byte(mnemonic))...)
    65  	enckey := util.HashSHA256([]byte(password))
    66  	out, err := util.Encrypt(enctxt, enckey)
    67  	if err != nil {
    68  		return output.NewError(output.ValidationError, "failed to encrypting mnemonic", nil)
    69  	}
    70  
    71  	if err := os.WriteFile(_hdWalletConfigFile, out, 0600); err != nil {
    72  		return output.NewError(output.WriteFileError,
    73  			fmt.Sprintf("failed to write to config file %s", _hdWalletConfigFile), err)
    74  	}
    75  
    76  	output.PrintResult(fmt.Sprintf("Mnemonic phrase: %s\n"+
    77  		"It is used to recover your wallet in case you forgot the password. Write them down and store it in a safe place.", mnemonic))
    78  	return nil
    79  }