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 4 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
15 changes: 14 additions & 1 deletion ioctl/newcmd/hdwallet/hdwallet.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
// 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 (
"errors"
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved

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

// 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 = config.ReadConfig.Wallet + "/hdwallet"
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
96 changes: 96 additions & 0 deletions ioctl/newcmd/hdwallet/hdwalletimport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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 (
"errors"
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
"fmt"
"os"
"strings"

"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/output"
"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)

cmd := &cobra.Command{
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
Use: use,
Short: short,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
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.ReadSecret()
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
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 output.NewError(output.ValidationError, "failed to encrypting mnemonic", nil)
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

if err := os.WriteFile(_hdWalletConfigFile, out, 0600); err != nil {
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
return output.NewError(output.WriteFileError,
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
fmt.Sprintf("failed to write to config file %s", _hdWalletConfigFile), err)
}

cmd.Println(fmt.Sprintf("Mnemonic phrase: %s\n"+
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
"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
},
}
return cmd
}
57 changes: 57 additions & 0 deletions ioctl/newcmd/hdwallet/hdwalletimport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 (
"os"
"testing"

"github.com/golang/mock/gomock"
"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"
)

const (
_testPath = "testNewAccount"
)

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(4)

t.Run("import hdwallet", func(t *testing.T) {
client.EXPECT().ReadSecret().Return(mnemonic, nil)
client.EXPECT().ReadSecret().Return(password, nil)
client.EXPECT().ReadSecret().Return(password, nil)
client.EXPECT().WriteConfig()
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved

cmd := NewHdwalletImportCmd(client)
_, err := util.ExecuteCmd(cmd)
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(err)
})

t.Run("hdwalletConfigFile exist", func(t *testing.T) {
expectedValue := "Please run 'ioctl hdwallet delete' before import"
err := os.WriteFile(_hdWalletConfigFile, []byte("123"), 0600)
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(err)
defer os.RemoveAll(_hdWalletConfigFile)

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