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
Changes from 1 commit
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
70 changes: 59 additions & 11 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 @@ -8,46 +8,94 @@ package hdwallet

import (
"bytes"
"errors"
LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
"fmt"
"os"

ecrypt "github.com/ethereum/go-ethereum/crypto"
"github.com/iotexproject/go-pkgs/crypto"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
"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"
)

// Multi-language support
var (
_hdwalletDeriveCmdUses = map[config.Language]string{
config.English: "derive id1/id2/id3",
config.Chinese: "derive id1/id2/id3",
}
_hdwalletDeriveCmdShorts = map[config.Language]string{
config.English: "derive key from HDWallet",
config.Chinese: "查询HDWallet钱包的派生key地址",
}
)

// NewHdwalletDeriveCmd represents the hdwallet derive command
func NewHdwalletDeriveCmd(client ioctl.Client) *cobra.Command {
use, _ := client.SelectTranslation(_hdwalletDeriveCmdUses)
short, _ := client.SelectTranslation(_hdwalletDeriveCmdShorts)

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
}

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(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(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)
return "", nil, errors.New("run 'ioctl hdwallet create' to create your HDWallet first")
}

enctxt, err := os.ReadFile(hdWalletConfigFile)
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to read config", err)
return "", nil, errors.New("failed to read config")
Copy link
Contributor

Choose a reason for hiding this comment

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

errors.Wrap

}

enckey := util.HashSHA256([]byte(password))
dectxt, err := util.Decrypt(enctxt, enckey)
if err != nil {
return "", nil, output.NewError(output.InputError, "failed to decrypt", err)
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, output.NewError(output.ValidationError, "incorrect data", nil)
return "", nil, errors.New("incorrect data")
}

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

LuckyPigeon marked this conversation as resolved.
Show resolved Hide resolved
wallet, err := hdwallet.NewFromMnemonic(string(mnemonic))
Expand All @@ -59,21 +107,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
}