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 derive command line into new ioctl #3418

Merged
merged 22 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ca74a4b
[ioctl] build hdwallet derive command line into new ioctl
LuckyPigeon May 28, 2022
6f1fbe8
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 1, 2022
3c51320
inline DeriveKey
LuckyPigeon Jun 1, 2022
899aa8b
adjust order
LuckyPigeon Jun 1, 2022
d2ae909
revert DeriveKey inline
LuckyPigeon Jun 1, 2022
0f77970
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 1, 2022
f3cb430
create ExportHdwallet client interface
LuckyPigeon Jun 1, 2022
0a1fa1b
Merge branch 'master' into dev_hdwallet_derive
huof6829 Jun 23, 2022
a56fa05
[ioctl] create main for ioctl/newcmd (#3296)
huof6829 Jun 23, 2022
0c8f894
change return type of HdwalletMnemonic
LuckyPigeon Jun 23, 2022
c4c4447
refactor unit test to cover the modification
LuckyPigeon Jun 23, 2022
23aca4e
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 23, 2022
c6f16ee
refactor unit test to cover the modification
LuckyPigeon Jun 23, 2022
78c2154
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 24, 2022
941bf73
Merge branch 'master' into dev_hdwallet_derive
CoderZhi Jun 25, 2022
10d72e2
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 25, 2022
8384ca3
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 27, 2022
af4ed34
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 27, 2022
cef0bd5
fix commit
LuckyPigeon Jun 27, 2022
5c61230
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 29, 2022
f2df4c1
Merge branch 'master' into dev_hdwallet_derive
LuckyPigeon Jun 29, 2022
71a0a44
fix commit
LuckyPigeon Jun 29, 2022
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
33 changes: 33 additions & 0 deletions ioctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/ioctl/validator"
"github.com/iotexproject/iotex-core/pkg/util/fileutil"
)

type (
Expand Down Expand Up @@ -71,6 +72,8 @@ type (
IsCryptoSm2() bool
// QueryAnalyser sends request to Analyser endpoint
QueryAnalyser(interface{}) (*http.Response, error)
// ExportHdwallet export the mnemonic of hdwallet
ExportHdwallet(string) ([]byte, error)
}

// APIServiceConfig defines a config of APIServiceClient
Expand Down Expand Up @@ -293,6 +296,36 @@ func (c *client) QueryAnalyser(reqData interface{}) (*http.Response, error) {
return resp, nil
}

func (c *client) ExportHdwallet(password string) ([]byte, error) {
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
// derive key as "m/44'/304'/account'/change/index"
hdWalletConfigFile := c.Config().Wallet + "/hdwallet"
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
if !fileutil.FileExists(hdWalletConfigFile) {
return nil, errors.New("run 'ioctl hdwallet create' to create your HDWallet first")
}
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved

enctxt, err := os.ReadFile(hdWalletConfigFile)
if err != nil {
return nil, errors.New("failed to read config")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

enckey := util.HashSHA256([]byte(password))
dectxt, err := util.Decrypt(enctxt, enckey)
if err != nil {
return nil, errors.New("failed to decrypt")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

dectxtLen := len(dectxt)
if dectxtLen <= 32 {
return nil, errors.New("incorrect data")
}

mnemonic, hash := dectxt[:dectxtLen-32], dectxt[dectxtLen-32:]
if !bytes.Equal(hash, util.HashSHA256(mnemonic)) {
return nil, errors.New("password error")
}
return mnemonic, nil
}

func (m *ConfirmationMessage) String() string {
line := fmt.Sprintf("%s\nOptions:", m.Info)
for _, option := range m.Options {
Expand Down
2 changes: 1 addition & 1 deletion ioctl/newcmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func PrivateKeyFromSigner(client ioctl.Client, cmd *cobra.Command, signer, passw
if err != nil {
return nil, errors.Wrap(err, "invalid HDWallet key format")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it changed to client?

Copy link
Contributor Author

@LuckyPigeon LuckyPigeon Jun 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you're talking about HdwalletMnemonic.
The main reason is that we don't want to expose hdwalletConfigFile, so we add it in client struct, which makes all the related function also needs to be inside the client interface.

_, prvKey, err = hdwallet.DeriveKey(account, change, index, password)
_, prvKey, err = hdwallet.DeriveKey(client, account, change, index, password)
if err != nil {
return nil, errors.Wrap(err, "failed to derive key from HDWallet")
}
Expand Down
2 changes: 2 additions & 0 deletions ioctl/newcmd/hdwallet/hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ package hdwallet
// DefaultRootDerivationPath for iotex
// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
const DefaultRootDerivationPath = "m/44'/304'"

var _hdWalletConfigFile string
84 changes: 53 additions & 31 deletions ioctl/newcmd/hdwallet/hdwalletderive.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 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
Expand All @@ -7,49 +7,71 @@
package hdwallet

import (
"bytes"
"fmt"
"os"

ecrypt "github.com/ethereum/go-ethereum/crypto"
"github.com/iotexproject/go-pkgs/crypto"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
"github.com/pkg/errors"
"github.com/spf13/cobra"

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

// DeriveKey derives the key according to path
func DeriveKey(account, change, index uint32, password string) (string, crypto.PrivateKey, error) {
// derive key as "m/44'/304'/account'/change/index"
hdWalletConfigFile := config.ReadConfig.Wallet + "/hdwallet"
if !fileutil.FileExists(hdWalletConfigFile) {
return "", nil, output.NewError(output.InputError, "Run 'ioctl hdwallet create' to create your HDWallet first.", nil)
// Multi-language support
var (
_hdwalletDeriveCmdUses = map[config.Language]string{
config.English: "derive id1/id2/id3",
config.Chinese: "derive id1/id2/id3",
}

enctxt, err := os.ReadFile(hdWalletConfigFile)
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to read config", err)
_hdwalletDeriveCmdShorts = map[config.Language]string{
config.English: "derive key from HDWallet",
config.Chinese: "查询HDWallet钱包的派生key地址",
}
)

enckey := util.HashSHA256([]byte(password))
dectxt, err := util.Decrypt(enctxt, enckey)
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to decrypt", err)
}
// NewHdwalletDeriveCmd represents the hdwallet derive command
func NewHdwalletDeriveCmd(client ioctl.Client) *cobra.Command {
use, _ := client.SelectTranslation(_hdwalletDeriveCmdUses)
short, _ := client.SelectTranslation(_hdwalletDeriveCmdShorts)

dectxtLen := len(dectxt)
if dectxtLen <= 32 {
return "", nil, output.NewError(output.ValidationError, "incorrect data", nil)
}
cmd := &cobra.Command{
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
Use: use,
Short: short,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
signer := "hdw::" + args[0]
account, change, index, err := util.ParseHdwPath(signer)
if err != nil {
return errors.New("invalid hdwallet key format")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

mnemonic, hash := dectxt[:dectxtLen-32], dectxt[dectxtLen-32:]
if !bytes.Equal(hash, util.HashSHA256(mnemonic)) {
return "", nil, output.NewError(output.ValidationError, "password error", nil)
cmd.Println("Enter password\n")
password, err := client.ReadSecret()
if err != nil {
return errors.New("failed to get password")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

addr, _, err := DeriveKey(client, account, change, index, password)
if err != nil {
return err
}
cmd.Println(fmt.Sprintf("address: %s\n", addr))
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
return nil
},
}
return cmd
}

// DeriveKey derives the key according to path
func DeriveKey(client ioctl.Client, account, change, index uint32, password string) (string, crypto.PrivateKey, error) {
mnemonic, err := client.ExportHdwallet(password)
if err != nil {
return "", nil, err
}
wallet, err := hdwallet.NewFromMnemonic(string(mnemonic))
if err != nil {
return "", nil, err
Expand All @@ -59,21 +81,21 @@ func DeriveKey(account, change, index uint32, password string) (string, crypto.P
path := hdwallet.MustParseDerivationPath(derivationPath)
walletAccount, err := wallet.Derive(path, false)
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to get account by derive path", err)
return "", nil, errors.New("failed to get account by derive path")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

private, err := wallet.PrivateKey(walletAccount)
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to get private key", err)
return "", nil, errors.New("failed to get private key")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}
prvKey, err := crypto.BytesToPrivateKey(ecrypt.FromECDSA(private))
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to Bytes private key", err)
return "", nil, errors.New("failed to Bytes private key")
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
}

addr := prvKey.PublicKey().Address()
if addr == nil {
return "", nil, output.NewError(output.ConvertError, "failed to convert public key into address", nil)
return "", nil, errors.New("failed to convert public key into address")
}
return addr.String(), prvKey, nil
}
15 changes: 15 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.