2020-10-06 18:57:55 +00:00
|
|
|
package hd
|
2020-08-11 15:01:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-09-29 20:10:56 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
cryptocodec "github.com/tharsis/ethermint/crypto/codec"
|
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
2020-08-11 15:01:15 +00:00
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
func init() {
|
|
|
|
amino := codec.NewLegacyAmino()
|
|
|
|
cryptocodec.RegisterCrypto(amino)
|
2020-08-11 15:01:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 08:20:27 +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"
|
|
|
|
|
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("")
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
kr, err := keyring.New("ethermint", keyring.BackendTest, dir, mockIn, 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)
|
|
|
|
require.Equal(t, "foo", info.GetName())
|
|
|
|
require.Equal(t, "local", info.GetType().String())
|
2021-04-17 10:00:07 +00:00
|
|
|
require.Equal(t, EthSecp256k1Type, info.GetAlgo())
|
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())
|
|
|
|
|
|
|
|
wallet, err := hdwallet.NewFromMnemonic(mnemonic)
|
|
|
|
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())
|
|
|
|
}
|