github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/hdwallet/hdwalletimport.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  	"bufio"
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  
    14  	"github.com/spf13/cobra"
    15  
    16  	"github.com/tyler-smith/go-bip39"
    17  
    18  	"github.com/iotexproject/iotex-core/ioctl/config"
    19  	"github.com/iotexproject/iotex-core/ioctl/output"
    20  	"github.com/iotexproject/iotex-core/ioctl/util"
    21  	"github.com/iotexproject/iotex-core/pkg/util/fileutil"
    22  )
    23  
    24  // Multi-language support
    25  var (
    26  	_importCmdShorts = map[config.Language]string{
    27  		config.English: "import hdwallet using mnemonic",
    28  		config.Chinese: "通过助记词导入钱包",
    29  	}
    30  )
    31  
    32  // _hdwalletImportCmd represents the hdwallet import command
    33  var _hdwalletImportCmd = &cobra.Command{
    34  	Use:   "import",
    35  	Short: config.TranslateInLang(_importCmdShorts, config.UILanguage),
    36  	Args:  cobra.ExactArgs(0),
    37  	RunE: func(cmd *cobra.Command, args []string) error {
    38  		cmd.SilenceUsage = true
    39  		err := hdwalletImport()
    40  		return output.PrintError(err)
    41  	},
    42  }
    43  
    44  func hdwalletImport() error {
    45  	if fileutil.FileExists(_hdWalletConfigFile) {
    46  		output.PrintResult("Please run 'ioctl hdwallet delete' before import")
    47  		return nil
    48  	}
    49  
    50  	output.PrintQuery("Enter 12 mnemonic words you saved, separated by space\n")
    51  
    52  	in := bufio.NewReader(os.Stdin)
    53  	line, err := in.ReadString('\n')
    54  	if err != nil {
    55  		return err
    56  	}
    57  	mnemonic := strings.TrimSpace(line)
    58  	if _, err = bip39.MnemonicToByteArray(mnemonic); err != nil {
    59  		return err
    60  	}
    61  
    62  	output.PrintQuery("Set password\n")
    63  	password, err := util.ReadSecretFromStdin()
    64  	if err != nil {
    65  		return output.NewError(output.InputError, "failed to get password", err)
    66  	}
    67  	output.PrintQuery("Enter password again\n")
    68  	passwordAgain, err := util.ReadSecretFromStdin()
    69  	if err != nil {
    70  		return output.NewError(output.InputError, "failed to get password", err)
    71  	}
    72  	if password != passwordAgain {
    73  		return output.NewError(output.ValidationError, ErrPasswdNotMatch.Error(), nil)
    74  	}
    75  
    76  	enctxt := append([]byte(mnemonic), util.HashSHA256([]byte(mnemonic))...)
    77  	enckey := util.HashSHA256([]byte(password))
    78  	out, err := util.Encrypt(enctxt, enckey)
    79  	if err != nil {
    80  		return output.NewError(output.ValidationError, "failed to encrypting mnemonic", nil)
    81  	}
    82  
    83  	if err := os.WriteFile(_hdWalletConfigFile, out, 0600); err != nil {
    84  		return output.NewError(output.WriteFileError,
    85  			fmt.Sprintf("failed to write to config file %s", _hdWalletConfigFile), err)
    86  	}
    87  
    88  	output.PrintResult(fmt.Sprintf("Mnemonic phrase: %s\n"+
    89  		"It is used to recover your wallet in case you forgot the password. Write them down and store it in a safe place.", mnemonic))
    90  
    91  	return err
    92  }