2020-10-06 18:57:55 +00:00
|
|
|
package hd
|
2020-08-11 15:01:15 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-30 17:00:19 +00:00
|
|
|
"os"
|
2020-08-11 15:01:15 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-08-11 15:01:15 +00:00
|
|
|
|
2020-09-29 20:10:56 +00:00
|
|
|
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
|
|
|
|
|
2022-07-28 13:43:49 +00:00
|
|
|
amino "github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-09-29 20:10:56 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
cryptocodec "github.com/evmos/ethermint/crypto/codec"
|
2022-07-28 13:43:49 +00:00
|
|
|
enccodec "github.com/evmos/ethermint/encoding/codec"
|
2022-06-19 09:43:41 +00:00
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
2020-08-11 15:01:15 +00:00
|
|
|
)
|
|
|
|
|
2022-11-30 17:00:19 +00:00
|
|
|
var TestCodec amino.Codec
|
2022-07-28 13:43:49 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
func init() {
|
2022-11-30 17:00:19 +00:00
|
|
|
cdc := amino.NewLegacyAmino()
|
2022-07-28 13:43:49 +00:00
|
|
|
cryptocodec.RegisterCrypto(cdc)
|
|
|
|
|
|
|
|
interfaceRegistry := types.NewInterfaceRegistry()
|
|
|
|
TestCodec = amino.NewProtoCodec(interfaceRegistry)
|
|
|
|
enccodec.RegisterInterfaces(interfaceRegistry)
|
2020-08-11 15:01:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:00:19 +00:00
|
|
|
const (
|
|
|
|
mnemonic = "picnic rent average infant boat squirrel federal assault mercy purity very motor fossil wheel verify upset box fresh horse vivid copy predict square regret"
|
|
|
|
|
|
|
|
// hdWalletFixEnv defines whether the standard (correct) bip39
|
|
|
|
// derivation path was used, or if derivation was affected by
|
|
|
|
// https://github.com/btcsuite/btcutil/issues/172
|
|
|
|
hdWalletFixEnv = "GO_ETHEREUM_HDWALLET_FIX_ISSUE_179"
|
|
|
|
)
|
2021-08-26 08:20:27 +00:00
|
|
|
|
2020-08-11 15:01:15 +00:00
|
|
|
func TestKeyring(t *testing.T) {
|
2021-04-17 10:00:07 +00:00
|
|
|
dir := t.TempDir()
|
2020-08-11 15:01:15 +00:00
|
|
|
mockIn := strings.NewReader("")
|
2022-07-28 13:43:49 +00:00
|
|
|
kr, err := keyring.New("ethermint", keyring.BackendTest, dir, mockIn, TestCodec, EthSecp256k1Option())
|
2020-08-11 15:01:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// fail in retrieving key
|
2021-04-17 10:00:07 +00:00
|
|
|
info, err := kr.Key("foo")
|
2020-08-11 15:01:15 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, info)
|
|
|
|
|
|
|
|
mockIn.Reset("password\npassword\n")
|
2021-06-29 17:02:21 +00:00
|
|
|
info, mnemonic, err := kr.NewMnemonic("foo", keyring.English, ethermint.BIP44HDPath, keyring.DefaultBIP39Passphrase, EthSecp256k1)
|
2020-08-11 15:01:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, mnemonic)
|
2022-07-28 13:43:49 +00:00
|
|
|
require.Equal(t, "foo", info.Name)
|
2020-08-11 15:01:15 +00:00
|
|
|
require.Equal(t, "local", info.GetType().String())
|
2022-07-28 13:43:49 +00:00
|
|
|
pubKey, err := info.GetPubKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(EthSecp256k1Type), pubKey.Type())
|
2020-08-11 15:01:15 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
hdPath := ethermint.BIP44HDPath
|
2020-08-11 15:01:15 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, hdPath)
|
2020-08-11 15:01:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, bz)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
wrongBz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, "/wrong/hdPath")
|
2020-09-29 20:10:56 +00:00
|
|
|
require.Error(t, err)
|
2021-04-17 10:00:07 +00:00
|
|
|
require.Empty(t, wrongBz)
|
2020-09-29 20:10:56 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
privkey := EthSecp256k1.Generate()(bz)
|
2020-09-29 20:10:56 +00:00
|
|
|
addr := common.BytesToAddress(privkey.PubKey().Address().Bytes())
|
|
|
|
|
2022-11-30 17:00:19 +00:00
|
|
|
os.Setenv(hdWalletFixEnv, "true")
|
2020-09-29 20:10:56 +00:00
|
|
|
wallet, err := hdwallet.NewFromMnemonic(mnemonic)
|
2022-11-30 17:00:19 +00:00
|
|
|
os.Setenv(hdWalletFixEnv, "")
|
2020-09-29 20:10:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
path := hdwallet.MustParseDerivationPath(hdPath)
|
|
|
|
|
|
|
|
account, err := wallet.Derive(path, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, addr.String(), account.Address.String())
|
2020-08-11 15:01:15 +00:00
|
|
|
}
|
2020-10-16 13:51:00 +00:00
|
|
|
|
|
|
|
func TestDerivation(t *testing.T) {
|
2021-04-17 10:00:07 +00:00
|
|
|
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, ethermint.BIP44HDPath)
|
2020-10-16 13:51:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, bz)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
badBz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, "44'/60'/0'/0/0")
|
2020-10-16 13:51:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, badBz)
|
|
|
|
|
|
|
|
require.NotEqual(t, bz, badBz)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
privkey := EthSecp256k1.Generate()(bz)
|
|
|
|
badPrivKey := EthSecp256k1.Generate()(badBz)
|
2020-10-16 13:51:00 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
require.False(t, privkey.Equals(badPrivKey))
|
2020-10-16 13:51:00 +00:00
|
|
|
|
|
|
|
wallet, err := hdwallet.NewFromMnemonic(mnemonic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
path := hdwallet.MustParseDerivationPath(ethermint.BIP44HDPath)
|
|
|
|
account, err := wallet.Derive(path, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
badPath := hdwallet.MustParseDerivationPath("44'/60'/0'/0/0")
|
|
|
|
badAccount, err := wallet.Derive(badPath, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Equality of Address BIP44
|
|
|
|
require.Equal(t, account.Address.String(), "0xA588C66983a81e800Db4dF74564F09f91c026351")
|
|
|
|
require.Equal(t, badAccount.Address.String(), "0xF8D6FDf2B8b488ea37e54903750dcd13F67E71cb")
|
|
|
|
// Inequality of wrong derivation path address
|
|
|
|
require.NotEqual(t, account.Address.String(), badAccount.Address.String())
|
|
|
|
// Equality of Ethermint implementation
|
|
|
|
require.Equal(t, common.BytesToAddress(privkey.PubKey().Address().Bytes()).String(), "0xA588C66983a81e800Db4dF74564F09f91c026351")
|
|
|
|
require.Equal(t, common.BytesToAddress(badPrivKey.PubKey().Address().Bytes()).String(), "0xF8D6FDf2B8b488ea37e54903750dcd13F67E71cb")
|
|
|
|
|
|
|
|
// Equality of Eth and Ethermint implementation
|
|
|
|
require.Equal(t, common.BytesToAddress(privkey.PubKey().Address()).String(), account.Address.String())
|
|
|
|
require.Equal(t, common.BytesToAddress(badPrivKey.PubKey().Address()).String(), badAccount.Address.String())
|
|
|
|
|
|
|
|
// Inequality of wrong derivation path of Eth and Ethermint implementation
|
|
|
|
require.NotEqual(t, common.BytesToAddress(privkey.PubKey().Address()).String(), badAccount.Address.String())
|
|
|
|
require.NotEqual(t, common.BytesToAddress(badPrivKey.PubKey().Address()).String(), account.Address.Hex())
|
|
|
|
}
|