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