-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ioctl] Build hdwallet import command line into new ioctl (#3419)
* [ioctl] build hdwallet import command line into new ioctl Co-authored-by: huofei <[email protected]>
- Loading branch information
1 parent
6f33246
commit 61d189f
Showing
6 changed files
with
249 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// 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" | ||
) | ||
|
||
// 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 | ||
if client.IsHdWalletConfigFileExist() { | ||
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.Wrap(err, "failed to get password") | ||
} | ||
cmd.Println("Enter password again") | ||
passwordAgain, err := client.ReadSecret() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get password") | ||
} | ||
if password != passwordAgain { | ||
return ErrPasswdNotMatch | ||
} | ||
|
||
if err := client.WriteHdWalletConfigFile(mnemonic, password); err != nil { | ||
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.\n", mnemonic) | ||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 ( | ||
"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" | ||
) | ||
|
||
func TestNewHdwalletImportCmd(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().IsHdWalletConfigFileExist().Return(false).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().WriteHdWalletConfigFile(gomock.Any(), gomock.Any()).Return(nil) | ||
|
||
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().WriteHdWalletConfigFile(gomock.Any(), gomock.Any()).Return(expectErr) | ||
|
||
cmd := NewHdwalletImportCmd(client) | ||
_, err := util.ExecuteCmd(cmd) | ||
require.Contains(err.Error(), expectErr.Error()) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.