Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ioctl] Build hdwallet import command line into new ioctl #3419

Merged
merged 19 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ioctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package ioctl

import (
"bufio"
"bytes"
"context"
"crypto/ecdsa"
Expand Down Expand Up @@ -71,6 +72,10 @@ type (
IsCryptoSm2() bool
// QueryAnalyser sends request to Analyser endpoint
QueryAnalyser(interface{}) (*http.Response, error)
// ReadInput reads the input from stdin
ReadInput() (string, error)
// WriteFile write
WriteFile(path string, in []byte) error
}

// APIServiceConfig defines a config of APIServiceClient
Expand All @@ -94,6 +99,9 @@ type (
Info string `json:"info"`
Options []string `json:"options"`
}

// StringMessage is the Message for string
StringMessage string
)

// EnableCryptoSm2 enables to use sm2 cryptographic algorithm
Expand Down Expand Up @@ -293,6 +301,22 @@ func (c *client) QueryAnalyser(reqData interface{}) (*http.Response, error) {
return resp, nil
}

func (c *client) ReadInput() (string, error) {
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if err != nil {
return "", err
}
return line, nil
}

func (c *client) WriteFile(path string, in []byte) error {
if err := os.WriteFile(path, in, 0600); err != nil {
return errors.Wrap(err, "failed to write to config file")
}
return nil
}

func (m *ConfirmationMessage) String() string {
line := fmt.Sprintf("%s\nOptions:", m.Info)
for _, option := range m.Options {
Expand Down
13 changes: 12 additions & 1 deletion ioctl/newcmd/hdwallet/hdwallet.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// Copyright (c) 2019 IoTeX Foundation
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 (
"github.com/pkg/errors"
)

// Errors
var (
ErrPasswdNotMatch = errors.New("password doesn't match")
)

// DefaultRootDerivationPath for iotex
// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
const DefaultRootDerivationPath = "m/44'/304'"

var _hdWalletConfigFile string
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
92 changes: 92 additions & 0 deletions ioctl/newcmd/hdwallet/hdwalletimport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 (
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tyler-smith/go-bip39"

"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/pkg/util/fileutil"
)

// Multi-language support
var (
_importCmdShorts = map[config.Language]string{
config.English: "import hdwallet using mnemonic",
config.Chinese: "通过助记词导入钱包",
}
_importCmdUses = map[config.Language]string{
config.English: "import",
config.Chinese: "import 导入",
}
)

// NewHdwalletImportCmd represents the hdwallet import command
func NewHdwalletImportCmd(client ioctl.Client) *cobra.Command {
use, _ := client.SelectTranslation(_importCmdUses)
short, _ := client.SelectTranslation(_importCmdShorts)

return &cobra.Command{
Use: use,
Short: short,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_hdWalletConfigFile = client.Config().Wallet + "/hdwallet"
if fileutil.FileExists(_hdWalletConfigFile) {
cmd.Println("Please run 'ioctl hdwallet delete' before import")
return nil
}

cmd.Println("Enter 12 mnemonic words you saved, separated by space")

line, err := client.ReadInput()
if err != nil {
return err
}
mnemonic := strings.TrimSpace(line)
if _, err = bip39.MnemonicToByteArray(mnemonic); err != nil {
return err
}

cmd.Println("Set password")
password, err := client.ReadSecret()
if err != nil {
return errors.New("failed to get password")
}
cmd.Println("Enter password again")
passwordAgain, err := client.ReadSecret()
if err != nil {
return errors.New("failed to get password")
}
if password != passwordAgain {
return ErrPasswdNotMatch
}

enctxt := append([]byte(mnemonic), util.HashSHA256([]byte(mnemonic))...)
enckey := util.HashSHA256([]byte(password))
out, err := util.Encrypt(enctxt, enckey)
if err != nil {
return errors.Wrap(err, "failed to encrypting mnemonic")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

if err := client.WriteFile(_hdWalletConfigFile, out); err != nil {
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
return errors.Wrap(err, "failed to write to config file")
}

cmd.Printf("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
},
}
}
83 changes: 83 additions & 0 deletions ioctl/newcmd/hdwallet/hdwalletimport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2022 IoTeX
// This is an alpha (internal) release and is not suitable for production. 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 (
"log"
"os"
"path/filepath"
"testing"

"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
"github.com/iotexproject/iotex-core/testutil"
)

func TestNewNodeDelegateCmd(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)

mnemonic := "lake stove quarter shove dry matrix hire split wide attract argue core"
password := "123"

client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(6)
client.EXPECT().Config().Return(config.Config{
Wallet: config.ReadConfig.Wallet,
}).Times(2)

t.Run("import hdwallet", func(t *testing.T) {
client.EXPECT().ReadInput().Return(mnemonic, nil)
client.EXPECT().ReadSecret().Return(password, nil)
client.EXPECT().ReadSecret().Return(password, nil)
client.EXPECT().WriteFile(gomock.Any(), gomock.Any()).Return(nil)
defer os.RemoveAll(_hdWalletConfigFile)

cmd := NewHdwalletImportCmd(client)
result, err := util.ExecuteCmd(cmd)
require.NoError(err)
require.Contains(result, mnemonic)
})

t.Run("failed to write to config file", func(t *testing.T) {
expectErr := errors.New("failed to write to config file")
client.EXPECT().ReadInput().Return(mnemonic, nil)
client.EXPECT().ReadSecret().Return(password, nil)
client.EXPECT().ReadSecret().Return(password, nil)
client.EXPECT().WriteFile(gomock.Any(), gomock.Any()).Return(expectErr)
defer os.RemoveAll(_hdWalletConfigFile)

cmd := NewHdwalletImportCmd(client)
_, err := util.ExecuteCmd(cmd)
require.Contains(err.Error(), expectErr.Error())
})

t.Run("hdwalletConfigFile exist", func(t *testing.T) {
expectedValue := "Please run 'ioctl hdwallet delete' before import"
testAccountFolder, err := os.MkdirTemp(os.TempDir(), "default")
require.NoError(err)
defer testutil.CleanupPath(testAccountFolder)
file := filepath.Join(testAccountFolder, "hdwallet")
if err := os.WriteFile(file, []byte("content"), 0666); err != nil {
log.Fatal(err)
}

client.EXPECT().Config().Return(config.Config{
Wallet: testAccountFolder,
})

cmd := NewHdwalletImportCmd(client)
result, err := util.ExecuteCmd(cmd)
require.NoError(err)
require.Contains(result, expectedValue)
})
}
29 changes: 29 additions & 0 deletions test/mock/mock_ioctlclient/mock_ioctlclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.