laconicd-deprecated/crypto/hd/algorithm_test.go

114 lines
4.0 KiB
Go
Raw Normal View History

package hd
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common"
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"
cryptocodec "github.com/tharsis/ethermint/crypto/codec"
ethermint "github.com/tharsis/ethermint/types"
)
2021-04-17 10:00:07 +00:00
func init() {
amino := codec.NewLegacyAmino()
cryptocodec.RegisterCrypto(amino)
}
func TestKeyring(t *testing.T) {
2021-04-17 10:00:07 +00:00
dir := t.TempDir()
mockIn := strings.NewReader("")
2021-04-17 10:00:07 +00:00
kr, err := keyring.New("ethermint", keyring.BackendTest, dir, mockIn, EthSecp256k1Option())
require.NoError(t, err)
// fail in retrieving key
2021-04-17 10:00:07 +00:00
info, err := kr.Key("foo")
require.Error(t, err)
require.Nil(t, info)
mockIn.Reset("password\npassword\n")
info, mnemonic, err := kr.NewMnemonic("foo", keyring.English, ethermint.BIP44HDPath, keyring.DefaultBIP39Passphrase, EthSecp256k1)
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())
2021-04-17 10:00:07 +00:00
hdPath := ethermint.BIP44HDPath
2021-04-17 10:00:07 +00:00
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, hdPath)
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")
require.Error(t, err)
2021-04-17 10:00:07 +00:00
require.Empty(t, wrongBz)
2021-04-17 10:00:07 +00:00
privkey := EthSecp256k1.Generate()(bz)
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())
}
func TestDerivation(t *testing.T) {
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"
2021-04-17 10:00:07 +00:00
bz, err := EthSecp256k1.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, ethermint.BIP44HDPath)
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")
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)
2021-04-17 10:00:07 +00:00
require.False(t, privkey.Equals(badPrivKey))
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())
}