From 4e01da905ad1d6e098c964264bd14a8d77906953 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:57:55 +0200 Subject: [PATCH] crypto: refactor for stargate (#559) * changelog * update changelog * crypto: refactor for stargate * fixes * fix keys * changelog --- CHANGELOG.md | 8 ++- app/ante/ante.go | 6 +- app/ante/utils_test.go | 6 +- client/export.go | 8 +-- client/keys.go | 10 +-- client/testnet.go | 6 +- cmd/ethermintcli/main.go | 6 +- cmd/ethermintd/genaccounts.go | 4 +- cmd/ethermintd/main.go | 6 +- codec/codec.go | 4 +- crypto/{ => ethsecp256k1}/codec.go | 12 +--- .../ethsecp256k1.go} | 63 ++++++++++++------- .../ethsecp256k1_test.go} | 8 +-- crypto/{ => hd}/algorithm.go | 12 ++-- crypto/{ => hd}/algorithm_test.go | 7 ++- importer/importer_test.go | 4 +- rpc/apis.go | 8 ++- rpc/config.go | 17 ++--- rpc/eth_api.go | 13 ++-- rpc/personal_api.go | 15 ++--- tests/personal_test.go | 4 +- types/account_test.go | 8 +-- x/evm/genesis_test.go | 4 +- x/evm/handler_test.go | 12 ++-- x/evm/keeper/statedb_test.go | 8 +-- x/evm/types/genesis_test.go | 5 +- x/evm/types/journal_test.go | 6 +- x/evm/types/logs_test.go | 7 +-- x/evm/types/msg_test.go | 7 +-- x/evm/types/state_transition_test.go | 4 +- x/evm/types/statedb_test.go | 10 +-- x/evm/types/utils.go | 13 ++-- 32 files changed, 165 insertions(+), 146 deletions(-) rename crypto/{ => ethsecp256k1}/codec.go (70%) rename crypto/{secp256k1.go => ethsecp256k1/ethsecp256k1.go} (60%) rename crypto/{secp256k1_test.go => ethsecp256k1/ethsecp256k1_test.go} (90%) rename crypto/{ => hd}/algorithm.go (89%) rename crypto/{ => hd}/algorithm_test.go (95%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 579d097e..d5853d12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,13 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog -## Unreleased +### API Breaking + +* (crypto) [\#559](https://github.com/ChainSafe/ethermint/pull/559) Refactored crypto package in preparation for the SDK's Stargate release: + * `crypto.PubKeySecp256k1` and `crypto.PrivKeySecp256k1` are now `ethsecp256k1.PubKey` and `ethsecp256k1.PrivKey`, respectively + * Moved SDK `SigningAlgo` implementation for Ethermint's Secp256k1 key to `crypto/hd` package. + +## [v0.2.1] - 2020-09-30 ### Features diff --git a/app/ante/ante.go b/app/ante/ante.go index 340f5b61..6ec99066 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -8,14 +8,14 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" evmtypes "github.com/cosmos/ethermint/x/evm/types" tmcrypto "github.com/tendermint/tendermint/crypto" ) func init() { - crypto.RegisterCodec(types.ModuleCdc) + ethsecp256k1.RegisterCodec(types.ModuleCdc) } const ( @@ -76,7 +76,7 @@ func sigGasConsumer( meter sdk.GasMeter, _ []byte, pubkey tmcrypto.PubKey, _ types.Params, ) error { switch pubkey.(type) { - case crypto.PubKeySecp256k1: + case ethsecp256k1.PubKey: meter.ConsumeGas(secp256k1VerifyCost, "ante verify: secp256k1") return nil case tmcrypto.PubKey: diff --git a/app/ante/utils_test.go b/app/ante/utils_test.go index 87043750..d0478ccb 100644 --- a/app/ante/utils_test.go +++ b/app/ante/utils_test.go @@ -12,7 +12,7 @@ import ( "github.com/cosmos/ethermint/app" ante "github.com/cosmos/ethermint/app/ante" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" evmtypes "github.com/cosmos/ethermint/x/evm/types" @@ -60,7 +60,7 @@ func newTestStdFee() auth.StdFee { // GenerateAddress generates an Ethereum address. func newTestAddrKey() (sdk.AccAddress, tmcrypto.PrivKey) { - privkey, _ := crypto.GenerateKey() + privkey, _ := ethsecp256k1.GenerateKey() addr := ethcrypto.PubkeyToAddress(privkey.ToECDSA().PublicKey) return sdk.AccAddress(addr.Bytes()), privkey @@ -95,7 +95,7 @@ func newTestEthTx(ctx sdk.Context, msg evmtypes.MsgEthereumTx, priv tmcrypto.Pri return nil, err } - privkey, ok := priv.(crypto.PrivKeySecp256k1) + privkey, ok := priv.(ethsecp256k1.PrivKey) if !ok { return nil, fmt.Errorf("invalid private key type: %T", priv) } diff --git a/client/export.go b/client/export.go index a90dcead..befe0421 100644 --- a/client/export.go +++ b/client/export.go @@ -15,8 +15,8 @@ import ( "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/crypto/keys" sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" + "github.com/cosmos/ethermint/crypto/hd" ) // UnsafeExportEthKeyCommand exports a key with the given name as a private key in hex format. @@ -34,7 +34,7 @@ func UnsafeExportEthKeyCommand() *cobra.Command { viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), inBuf, - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) if err != nil { return err @@ -64,7 +64,7 @@ func UnsafeExportEthKeyCommand() *cobra.Command { } // Converts key to Ethermint secp256 implementation - emintKey, ok := privKey.(crypto.PrivKeySecp256k1) + emintKey, ok := privKey.(ethsecp256k1.PrivKey) if !ok { return fmt.Errorf("invalid private key type, must be Ethereum key: %T", privKey) } diff --git a/client/keys.go b/client/keys.go index 22810e67..8d0984b7 100644 --- a/client/keys.go +++ b/client/keys.go @@ -12,7 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/hd" ) const ( @@ -37,8 +37,8 @@ func KeyCommands() *cobra.Command { // update the default signing algorithm value to "eth_secp256k1" algoFlag := addCmd.Flag("algo") - algoFlag.DefValue = string(crypto.EthSecp256k1) - err := algoFlag.Value.Set(string(crypto.EthSecp256k1)) + algoFlag.DefValue = string(hd.EthSecp256k1) + err := algoFlag.Value.Set(string(hd.EthSecp256k1)) if err != nil { panic(err) } @@ -74,7 +74,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error { func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) { if transient { return keys.NewInMemory( - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ), nil } @@ -83,6 +83,6 @@ func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) { viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf, - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) } diff --git a/client/testnet.go b/client/testnet.go index 3874ab7d..c6589b88 100644 --- a/client/testnet.go +++ b/client/testnet.go @@ -36,7 +36,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/mint" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/hd" ethermint "github.com/cosmos/ethermint/types" evmtypes "github.com/cosmos/ethermint/x/evm/types" ) @@ -102,7 +102,7 @@ Note, strict routability for addresses is turned off in the config file.`, cmd.Flags().String(flagCoinDenom, ethermint.AttoPhoton, "Coin denomination used for staking, governance, mint, crisis and evm parameters") cmd.Flags().String(server.FlagMinGasPrices, fmt.Sprintf("0.000006%s", ethermint.AttoPhoton), "Minimum gas prices to accept for transactions; All fees in a tx must meet this minimum (e.g. 0.01aphoton,0.001stake)") cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)") - cmd.Flags().String(flagKeyAlgo, string(crypto.EthSecp256k1), "Key signing algorithm to generate keys for") + cmd.Flags().String(flagKeyAlgo, string(hd.EthSecp256k1), "Key signing algorithm to generate keys for") return cmd } @@ -203,7 +203,7 @@ func InitTestnet( keyringBackend, clientDir, inBuf, - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) if err != nil { return err diff --git a/cmd/ethermintcli/main.go b/cmd/ethermintcli/main.go index 6fe3505b..cded7573 100644 --- a/cmd/ethermintcli/main.go +++ b/cmd/ethermintcli/main.go @@ -24,7 +24,7 @@ import ( "github.com/cosmos/ethermint/app" "github.com/cosmos/ethermint/client" "github.com/cosmos/ethermint/codec" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" "github.com/cosmos/ethermint/rpc" ethermint "github.com/cosmos/ethermint/types" ) @@ -37,8 +37,8 @@ func main() { // Configure cobra to sort commands cobra.EnableCommandSorting = false - tmamino.RegisterKeyType(crypto.PubKeySecp256k1{}, crypto.PubKeyAminoName) - tmamino.RegisterKeyType(crypto.PrivKeySecp256k1{}, crypto.PrivKeyAminoName) + tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName) + tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName) keys.CryptoCdc = cdc clientkeys.KeysCdc = cdc diff --git a/cmd/ethermintd/genaccounts.go b/cmd/ethermintd/genaccounts.go index cd6cda74..bd7efe17 100644 --- a/cmd/ethermintd/genaccounts.go +++ b/cmd/ethermintd/genaccounts.go @@ -20,7 +20,7 @@ import ( authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting" "github.com/cosmos/cosmos-sdk/x/genutil" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/hd" ethermint "github.com/cosmos/ethermint/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" @@ -60,7 +60,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa viper.GetString(flags.FlagKeyringBackend), viper.GetString(flagClientHome), inBuf, - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) if err != nil { return err diff --git a/cmd/ethermintd/main.go b/cmd/ethermintd/main.go index 9965287e..b94a5781 100644 --- a/cmd/ethermintd/main.go +++ b/cmd/ethermintd/main.go @@ -30,7 +30,7 @@ import ( "github.com/cosmos/ethermint/app" "github.com/cosmos/ethermint/client" "github.com/cosmos/ethermint/codec" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" ) @@ -43,8 +43,8 @@ func main() { cdc := codec.MakeCodec(app.ModuleBasics) - tmamino.RegisterKeyType(crypto.PubKeySecp256k1{}, crypto.PubKeyAminoName) - tmamino.RegisterKeyType(crypto.PrivKeySecp256k1{}, crypto.PrivKeyAminoName) + tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName) + tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName) keys.CryptoCdc = cdc genutil.ModuleCdc = cdc diff --git a/codec/codec.go b/codec/codec.go index b8bf06ff..9696578e 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -7,7 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth/vesting" - emintcrypto "github.com/cosmos/ethermint/crypto" + cryptocodec "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" ) @@ -22,7 +22,7 @@ func MakeCodec(bm module.BasicManager) *codec.Codec { bm.RegisterCodec(cdc) vesting.RegisterCodec(cdc) sdk.RegisterCodec(cdc) - emintcrypto.RegisterCodec(cdc) + cryptocodec.RegisterCodec(cdc) codec.RegisterCrypto(cdc) ethermint.RegisterCodec(cdc) keys.RegisterCodec(cdc) // temporary. Used to register keyring.Info diff --git a/crypto/codec.go b/crypto/ethsecp256k1/codec.go similarity index 70% rename from crypto/codec.go rename to crypto/ethsecp256k1/codec.go index 02a07220..735d5301 100644 --- a/crypto/codec.go +++ b/crypto/ethsecp256k1/codec.go @@ -1,4 +1,4 @@ -package crypto +package ethsecp256k1 import ( cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino" @@ -10,12 +10,6 @@ import ( // CryptoCodec is the default amino codec used by ethermint var CryptoCodec = codec.New() -// Amino encoding names -const ( - PrivKeyAminoName = "ethermint/PrivKeySecp256k1" - PubKeyAminoName = "ethermint/PubKeySecp256k1" -) - func init() { // replace the keyring codec with the ethermint crypto codec to prevent // amino panics because of unregistered Priv/PubKey @@ -28,6 +22,6 @@ func init() { // RegisterCodec registers all the necessary types with amino for the given // codec. func RegisterCodec(cdc *codec.Codec) { - cdc.RegisterConcrete(PubKeySecp256k1{}, PubKeyAminoName, nil) - cdc.RegisterConcrete(PrivKeySecp256k1{}, PrivKeyAminoName, nil) + cdc.RegisterConcrete(PubKey{}, PubKeyName, nil) + cdc.RegisterConcrete(PrivKey{}, PrivKeyName, nil) } diff --git a/crypto/secp256k1.go b/crypto/ethsecp256k1/ethsecp256k1.go similarity index 60% rename from crypto/secp256k1.go rename to crypto/ethsecp256k1/ethsecp256k1.go index 099e9197..569246b5 100644 --- a/crypto/secp256k1.go +++ b/crypto/ethsecp256k1/ethsecp256k1.go @@ -1,56 +1,71 @@ -package crypto +package ethsecp256k1 import ( "bytes" "crypto/ecdsa" ethcrypto "github.com/ethereum/go-ethereum/crypto" - ethsecp256k1 "github.com/ethereum/go-ethereum/crypto/secp256k1" + "github.com/ethereum/go-ethereum/crypto/secp256k1" tmcrypto "github.com/tendermint/tendermint/crypto" ) +const ( + // PrivKeySize defines the size of the PrivKey bytes + PrivKeySize = 32 + // KeyType is the string constant for the EthSecp256k1 algorithm + KeyType = "eth_secp256k1" +) + +// Amino encoding names +const ( + // PrivKeyName defines the amino encoding name for the EthSecp256k1 private key + PrivKeyName = "ethermint/PrivKeyEthSecp256k1" + // PubKeyName defines the amino encoding name for the EthSecp256k1 public key + PubKeyName = "ethermint/PubKeyEthSecp256k1" +) + // ---------------------------------------------------------------------------- // secp256k1 Private Key -var _ tmcrypto.PrivKey = PrivKeySecp256k1{} +var _ tmcrypto.PrivKey = PrivKey{} -// PrivKeySecp256k1 defines a type alias for an ecdsa.PrivateKey that implements +// PrivKey defines a type alias for an ecdsa.PrivateKey that implements // Tendermint's PrivateKey interface. -type PrivKeySecp256k1 []byte +type PrivKey []byte // GenerateKey generates a new random private key. It returns an error upon // failure. -func GenerateKey() (PrivKeySecp256k1, error) { +func GenerateKey() (PrivKey, error) { priv, err := ethcrypto.GenerateKey() if err != nil { - return PrivKeySecp256k1{}, err + return PrivKey{}, err } - return PrivKeySecp256k1(ethcrypto.FromECDSA(priv)), nil + return PrivKey(ethcrypto.FromECDSA(priv)), nil } // PubKey returns the ECDSA private key's public key. -func (privkey PrivKeySecp256k1) PubKey() tmcrypto.PubKey { +func (privkey PrivKey) PubKey() tmcrypto.PubKey { ecdsaPKey := privkey.ToECDSA() - return PubKeySecp256k1(ethcrypto.CompressPubkey(&ecdsaPKey.PublicKey)) + return PubKey(ethcrypto.CompressPubkey(&ecdsaPKey.PublicKey)) } // Bytes returns the raw ECDSA private key bytes. -func (privkey PrivKeySecp256k1) Bytes() []byte { +func (privkey PrivKey) Bytes() []byte { return CryptoCodec.MustMarshalBinaryBare(privkey) } // Sign creates a recoverable ECDSA signature on the secp256k1 curve over the // Keccak256 hash of the provided message. The produced signature is 65 bytes // where the last byte contains the recovery ID. -func (privkey PrivKeySecp256k1) Sign(msg []byte) ([]byte, error) { +func (privkey PrivKey) Sign(msg []byte) ([]byte, error) { return ethcrypto.Sign(ethcrypto.Keccak256Hash(msg).Bytes(), privkey.ToECDSA()) } // Equals returns true if two ECDSA private keys are equal and false otherwise. -func (privkey PrivKeySecp256k1) Equals(other tmcrypto.PrivKey) bool { - if other, ok := other.(PrivKeySecp256k1); ok { +func (privkey PrivKey) Equals(other tmcrypto.PrivKey) bool { + if other, ok := other.(PrivKey); ok { return bytes.Equal(privkey.Bytes(), other.Bytes()) } @@ -59,7 +74,7 @@ func (privkey PrivKeySecp256k1) Equals(other tmcrypto.PrivKey) bool { // ToECDSA returns the ECDSA private key as a reference to ecdsa.PrivateKey type. // The function will panic if the private key is invalid. -func (privkey PrivKeySecp256k1) ToECDSA() *ecdsa.PrivateKey { +func (privkey PrivKey) ToECDSA() *ecdsa.PrivateKey { key, err := ethcrypto.ToECDSA(privkey) if err != nil { panic(err) @@ -70,15 +85,15 @@ func (privkey PrivKeySecp256k1) ToECDSA() *ecdsa.PrivateKey { // ---------------------------------------------------------------------------- // secp256k1 Public Key -var _ tmcrypto.PubKey = (*PubKeySecp256k1)(nil) +var _ tmcrypto.PubKey = (*PubKey)(nil) -// PubKeySecp256k1 defines a type alias for an ecdsa.PublicKey that implements Tendermint's PubKey +// PubKey defines a type alias for an ecdsa.PublicKey that implements Tendermint's PubKey // interface. It represents the 33-byte compressed public key format. -type PubKeySecp256k1 []byte +type PubKey []byte // Address returns the address of the ECDSA public key. // The function will panic if the public key is invalid. -func (key PubKeySecp256k1) Address() tmcrypto.Address { +func (key PubKey) Address() tmcrypto.Address { pubk, err := ethcrypto.DecompressPubkey(key) if err != nil { panic(err) @@ -89,7 +104,7 @@ func (key PubKeySecp256k1) Address() tmcrypto.Address { // Bytes returns the raw bytes of the ECDSA public key. // The function panics if the key cannot be marshaled to bytes. -func (key PubKeySecp256k1) Bytes() []byte { +func (key PubKey) Bytes() []byte { bz, err := CryptoCodec.MarshalBinaryBare(key) if err != nil { panic(err) @@ -100,19 +115,19 @@ func (key PubKeySecp256k1) Bytes() []byte { // VerifyBytes verifies that the ECDSA public key created a given signature over // the provided message. It will calculate the Keccak256 hash of the message // prior to verification. -func (key PubKeySecp256k1) VerifyBytes(msg []byte, sig []byte) bool { +func (key PubKey) VerifyBytes(msg []byte, sig []byte) bool { if len(sig) == 65 { // remove recovery ID if contained in the signature sig = sig[:len(sig)-1] } // the signature needs to be in [R || S] format when provided to VerifySignature - return ethsecp256k1.VerifySignature(key, ethcrypto.Keccak256Hash(msg).Bytes(), sig) + return secp256k1.VerifySignature(key, ethcrypto.Keccak256Hash(msg).Bytes(), sig) } // Equals returns true if two ECDSA public keys are equal and false otherwise. -func (key PubKeySecp256k1) Equals(other tmcrypto.PubKey) bool { - if other, ok := other.(PubKeySecp256k1); ok { +func (key PubKey) Equals(other tmcrypto.PubKey) bool { + if other, ok := other.(PubKey); ok { return bytes.Equal(key.Bytes(), other.Bytes()) } diff --git a/crypto/secp256k1_test.go b/crypto/ethsecp256k1/ethsecp256k1_test.go similarity index 90% rename from crypto/secp256k1_test.go rename to crypto/ethsecp256k1/ethsecp256k1_test.go index 0da4a5a0..78f4b839 100644 --- a/crypto/secp256k1_test.go +++ b/crypto/ethsecp256k1/ethsecp256k1_test.go @@ -1,4 +1,4 @@ -package crypto +package ethsecp256k1 import ( "testing" @@ -11,7 +11,7 @@ import ( tmcrypto "github.com/tendermint/tendermint/crypto" ) -func TestPrivKeySecp256k1PrivKey(t *testing.T) { +func TestPrivKeyPrivKey(t *testing.T) { // validate type and equality privKey, err := GenerateKey() require.NoError(t, err) @@ -38,12 +38,12 @@ func TestPrivKeySecp256k1PrivKey(t *testing.T) { require.Equal(t, expectedSig, sig) } -func TestPrivKeySecp256k1PubKey(t *testing.T) { +func TestPrivKeyPubKey(t *testing.T) { privKey, err := GenerateKey() require.NoError(t, err) // validate type and equality - pubKey := privKey.PubKey().(PubKeySecp256k1) + pubKey := privKey.PubKey().(PubKey) require.Implements(t, (*tmcrypto.PubKey)(nil), pubKey) // validate inequality diff --git a/crypto/algorithm.go b/crypto/hd/algorithm.go similarity index 89% rename from crypto/algorithm.go rename to crypto/hd/algorithm.go index 901ed46e..1ac60679 100644 --- a/crypto/algorithm.go +++ b/crypto/hd/algorithm.go @@ -1,4 +1,4 @@ -package crypto +package hd import ( "fmt" @@ -15,13 +15,13 @@ import ( tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/crypto/keys" + + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ) const ( - // EthSecp256k1Type string constant for the EthSecp256k1 algorithm - EthSecp256k1Type = "eth_secp256k1" // EthSecp256k1 defines the ECDSA secp256k1 used on Ethereum - EthSecp256k1 = keys.SigningAlgo(EthSecp256k1Type) + EthSecp256k1 = keys.SigningAlgo(ethsecp256k1.KeyType) ) // SupportedAlgorithms defines the list of signing algorithms used on Ethermint: @@ -57,7 +57,7 @@ func EthermintKeygenFunc(bz []byte, algo keys.SigningAlgo) (tmcrypto.PrivKey, er return nil, fmt.Errorf("signing algorithm must be %s, got %s", EthSecp256k1, algo) } - return PrivKeySecp256k1(bz), nil + return ethsecp256k1.PrivKey(bz), nil } // DeriveSecp256k1 derives and returns the eth_secp256k1 private key for the given mnemonic and HD path. @@ -91,6 +91,6 @@ func DeriveSecp256k1(mnemonic, bip39Passphrase, path string) ([]byte, error) { } privateKeyECDSA := privateKey.ToECDSA() - derivedKey := PrivKeySecp256k1(ethcrypto.FromECDSA(privateKeyECDSA)) + derivedKey := ethsecp256k1.PrivKey(ethcrypto.FromECDSA(privateKeyECDSA)) return derivedKey, nil } diff --git a/crypto/algorithm_test.go b/crypto/hd/algorithm_test.go similarity index 95% rename from crypto/algorithm_test.go rename to crypto/hd/algorithm_test.go index be911ead..90a0fb25 100644 --- a/crypto/algorithm_test.go +++ b/crypto/hd/algorithm_test.go @@ -1,4 +1,4 @@ -package crypto +package hd import ( "strings" @@ -15,11 +15,12 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/hd" "github.com/cosmos/cosmos-sdk/tests" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" ) func TestEthermintKeygenFunc(t *testing.T) { - privkey, err := GenerateKey() + privkey, err := ethsecp256k1.GenerateKey() require.NoError(t, err) testCases := []struct { @@ -109,7 +110,7 @@ func TestKeyring(t *testing.T) { require.NoError(t, err) require.NotEmpty(t, bz) - privkey := PrivKeySecp256k1(bz) + privkey := ethsecp256k1.PrivKey(bz) addr := common.BytesToAddress(privkey.PubKey().Address().Bytes()) wallet, err := hdwallet.NewFromMnemonic(mnemonic) diff --git a/importer/importer_test.go b/importer/importer_test.go index 756a7e05..3e33cd06 100644 --- a/importer/importer_test.go +++ b/importer/importer_test.go @@ -24,7 +24,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/ethermint/core" - emintcrypto "github.com/cosmos/ethermint/crypto" + cryptocodec "github.com/cosmos/ethermint/crypto/ethsecp256k1" "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm" evmtypes "github.com/cosmos/ethermint/x/evm/types" @@ -72,7 +72,7 @@ func newTestCodec() *sdkcodec.Codec { auth.RegisterCodec(cdc) bank.RegisterCodec(cdc) sdk.RegisterCodec(cdc) - emintcrypto.RegisterCodec(cdc) + cryptocodec.RegisterCodec(cdc) sdkcodec.RegisterCrypto(cdc) return cdc diff --git a/rpc/apis.go b/rpc/apis.go index eeeaf0d4..37884428 100644 --- a/rpc/apis.go +++ b/rpc/apis.go @@ -3,9 +3,11 @@ package rpc import ( - "github.com/cosmos/cosmos-sdk/client/context" - emintcrypto "github.com/cosmos/ethermint/crypto" "github.com/ethereum/go-ethereum/rpc" + + "github.com/cosmos/cosmos-sdk/client/context" + + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ) // RPC namespaces and API version @@ -19,7 +21,7 @@ const ( ) // GetRPCAPIs returns the list of all APIs -func GetRPCAPIs(cliCtx context.CLIContext, keys []emintcrypto.PrivKeySecp256k1) []rpc.API { +func GetRPCAPIs(cliCtx context.CLIContext, keys []ethsecp256k1.PrivKey) []rpc.API { nonceLock := new(AddrLocker) backend := NewEthermintBackend(cliCtx) ethAPI := NewPublicEthAPI(cliCtx, backend, nonceLock, keys) diff --git a/rpc/config.go b/rpc/config.go index 6142a2ab..88f510e4 100644 --- a/rpc/config.go +++ b/rpc/config.go @@ -19,7 +19,8 @@ import ( authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" "github.com/cosmos/ethermint/app" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" + "github.com/cosmos/ethermint/crypto/hd" "github.com/ethereum/go-ethereum/rpc" ) @@ -45,7 +46,7 @@ func registerRoutes(rs *lcd.RestServer) { accountName := viper.GetString(flagUnlockKey) accountNames := strings.Split(accountName, ",") - var privkeys []crypto.PrivKeySecp256k1 + var privkeys []ethsecp256k1.PrivKey if len(accountName) > 0 { var err error inBuf := bufio.NewReader(os.Stdin) @@ -102,31 +103,31 @@ func registerRoutes(rs *lcd.RestServer) { ws.start() } -func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]crypto.PrivKeySecp256k1, error) { +func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]ethsecp256k1.PrivKey, error) { keybase, err := keys.NewKeyring( sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), os.Stdin, - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) if err != nil { - return []crypto.PrivKeySecp256k1{}, err + return []ethsecp256k1.PrivKey{}, err } // try the for loop with array []string accountNames // run through the bottom code inside the for loop - keys := make([]crypto.PrivKeySecp256k1, len(accountNames)) + keys := make([]ethsecp256k1.PrivKey, len(accountNames)) for i, acc := range accountNames { // With keyring keybase, password is not required as it is pulled from the OS prompt privKey, err := keybase.ExportPrivateKeyObject(acc, passphrase) if err != nil { - return []crypto.PrivKeySecp256k1{}, err + return []ethsecp256k1.PrivKey{}, err } var ok bool - keys[i], ok = privKey.(crypto.PrivKeySecp256k1) + keys[i], ok = privKey.(ethsecp256k1.PrivKey) if !ok { panic(fmt.Sprintf("invalid private key type %T at index %d", privKey, i)) } diff --git a/rpc/eth_api.go b/rpc/eth_api.go index 059dfa84..6c602df8 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -10,7 +10,8 @@ import ( "github.com/spf13/viper" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" + "github.com/cosmos/ethermint/crypto/hd" params "github.com/cosmos/ethermint/rpc/args" ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/utils" @@ -46,14 +47,14 @@ type PublicEthAPI struct { chainIDEpoch *big.Int logger log.Logger backend Backend - keys []crypto.PrivKeySecp256k1 // unlocked keys + keys []ethsecp256k1.PrivKey // unlocked keys nonceLock *AddrLocker keybaseLock sync.Mutex } // NewPublicEthAPI creates an instance of the public ETH Web3 API. func NewPublicEthAPI(cliCtx context.CLIContext, backend Backend, nonceLock *AddrLocker, - key []crypto.PrivKeySecp256k1) *PublicEthAPI { + key []ethsecp256k1.PrivKey) *PublicEthAPI { epoch, err := ethermint.ParseChainID(cliCtx.ChainID) if err != nil { @@ -86,7 +87,7 @@ func (e *PublicEthAPI) getKeybaseInfo() error { viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), e.cliCtx.Input, - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) if err != nil { return err @@ -181,7 +182,7 @@ func (e *PublicEthAPI) Accounts() ([]common.Address, error) { viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), e.cliCtx.Input, - crypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) if err != nil { return addresses, err @@ -342,7 +343,7 @@ func (e *PublicEthAPI) ExportAccount(address common.Address, blockNumber BlockNu return string(res), nil } -func checkKeyInKeyring(keys []crypto.PrivKeySecp256k1, address common.Address) (key crypto.PrivKeySecp256k1, exist bool) { +func checkKeyInKeyring(keys []ethsecp256k1.PrivKey, address common.Address) (key ethsecp256k1.PrivKey, exist bool) { if len(keys) > 0 { for _, key := range keys { if bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) { diff --git a/rpc/personal_api.go b/rpc/personal_api.go index 439d1703..d3bcfcb3 100644 --- a/rpc/personal_api.go +++ b/rpc/personal_api.go @@ -19,7 +19,8 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" - emintcrypto "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" + "github.com/cosmos/ethermint/crypto/hd" params "github.com/cosmos/ethermint/rpc/args" ) @@ -54,7 +55,7 @@ func (e *PersonalEthAPI) getKeybaseInfo() ([]keys.Info, error) { viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), e.ethAPI.cliCtx.Input, - emintcrypto.EthSecp256k1Options()..., + hd.EthSecp256k1Options()..., ) if err != nil { return nil, err @@ -77,9 +78,9 @@ func (e *PersonalEthAPI) ImportRawKey(privkey, password string) (common.Address, return common.Address{}, err } - privKey := emintcrypto.PrivKeySecp256k1(crypto.FromECDSA(priv)) + privKey := ethsecp256k1.PrivKey(crypto.FromECDSA(priv)) - armor := mintkey.EncryptArmorPrivKey(privKey, password, emintcrypto.EthSecp256k1Type) + armor := mintkey.EncryptArmorPrivKey(privKey, password, ethsecp256k1.KeyType) // ignore error as we only care about the length of the list list, _ := e.ethAPI.cliCtx.Keybase.List() @@ -126,7 +127,7 @@ func (e *PersonalEthAPI) LockAccount(address common.Address) bool { continue } - tmp := make([]emintcrypto.PrivKeySecp256k1, len(e.ethAPI.keys)-1) + tmp := make([]ethsecp256k1.PrivKey, len(e.ethAPI.keys)-1) copy(tmp[:i], e.ethAPI.keys[:i]) copy(tmp[i:], e.ethAPI.keys[i+1:]) e.ethAPI.keys = tmp @@ -147,7 +148,7 @@ func (e *PersonalEthAPI) NewAccount(password string) (common.Address, error) { } name := "key_" + time.Now().UTC().Format(time.RFC3339) - info, _, err := e.ethAPI.cliCtx.Keybase.CreateMnemonic(name, keys.English, password, emintcrypto.EthSecp256k1) + info, _, err := e.ethAPI.cliCtx.Keybase.CreateMnemonic(name, keys.English, password, hd.EthSecp256k1) if err != nil { return common.Address{}, err } @@ -193,7 +194,7 @@ func (e *PersonalEthAPI) UnlockAccount(_ context.Context, addr common.Address, p return false, err } - emintKey, ok := privKey.(emintcrypto.PrivKeySecp256k1) + emintKey, ok := privKey.(ethsecp256k1.PrivKey) if !ok { return false, fmt.Errorf("invalid private key type: %T", privKey) } diff --git a/tests/personal_test.go b/tests/personal_test.go index 94a881eb..8f4cf923 100644 --- a/tests/personal_test.go +++ b/tests/personal_test.go @@ -4,11 +4,11 @@ import ( "encoding/json" "testing" + "github.com/stretchr/testify/require" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethcrypto "github.com/ethereum/go-ethereum/crypto" - - "github.com/stretchr/testify/require" ) func TestPersonal_ListAccounts(t *testing.T) { diff --git a/types/account_test.go b/types/account_test.go index ff28bcb3..d8524b88 100644 --- a/types/account_test.go +++ b/types/account_test.go @@ -13,13 +13,13 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" "github.com/cosmos/ethermint/types" ) func init() { - tmamino.RegisterKeyType(crypto.PubKeySecp256k1{}, crypto.PubKeyAminoName) - tmamino.RegisterKeyType(crypto.PrivKeySecp256k1{}, crypto.PrivKeyAminoName) + tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName) + tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName) } type AccountTestSuite struct { @@ -84,7 +84,7 @@ func (suite *AccountTestSuite) TestEthermintAccountJSON() { } func (suite *AccountTestSuite) TestEthermintPubKeyJSON() { - privkey, err := crypto.GenerateKey() + privkey, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) bz := privkey.PubKey().Bytes() diff --git a/x/evm/genesis_test.go b/x/evm/genesis_test.go index 0bc5b094..40194b31 100644 --- a/x/evm/genesis_test.go +++ b/x/evm/genesis_test.go @@ -4,7 +4,7 @@ import ( "crypto/ecdsa" "math/big" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" "github.com/cosmos/ethermint/x/evm" "github.com/cosmos/ethermint/x/evm/types" @@ -24,7 +24,7 @@ func (suite *EvmTestSuite) TestContractExportImport() { gasLimit := uint64(5000000) gasPrice := big.NewInt(1) - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err, "failed to create key") ensFactoryCode := common.FromHex("0x608060405234801561001057600080fd5b50612033806100206000396000f3006080604052600436106100405763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663e9358b018114610045575b600080fd5b34801561005157600080fd5b5061007373ffffffffffffffffffffffffffffffffffffffff6004351661009c565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60008060006100a961057f565b604051809103906000f0801580156100c5573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081526000600482018190527f4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f06024830152306044830152915192945073ffffffffffffffffffffffffffffffffffffffff8516926306ab59239260648084019391929182900301818387803b15801561016357600080fd5b505af1158015610177573d6000803e3d6000fd5b505050508161018461058f565b73ffffffffffffffffffffffffffffffffffffffff909116815260405190819003602001906000f0801580156101be573d6000803e3d6000fd5b50604080517f06ab59230000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae60048201527f329539a1d23af1810c48a07fe7fc66a3b34fbc8b37e9b3cdb97bb88ceab7e4bf6024820152306044820152905191925073ffffffffffffffffffffffffffffffffffffffff8416916306ab59239160648082019260009290919082900301818387803b15801561027c57600080fd5b505af1158015610290573d6000803e3d6000fd5b5050604080517f1896f70a0000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff858116602483015291519186169350631896f70a925060448082019260009290919082900301818387803b15801561032b57600080fd5b505af115801561033f573d6000803e3d6000fd5b5050604080517fd5fa2b000000000000000000000000000000000000000000000000000000000081527ffdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5600482015273ffffffffffffffffffffffffffffffffffffffff851660248201819052915191935063d5fa2b00925060448082019260009290919082900301818387803b1580156103d957600080fd5b505af11580156103ed573d6000803e3d6000fd5b5050604080517f5b0fc9c30000000000000000000000000000000000000000000000000000000081527f93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae600482015273ffffffffffffffffffffffffffffffffffffffff888116602483015291519186169350635b0fc9c3925060448082019260009290919082900301818387803b15801561048857600080fd5b505af115801561049c573d6000803e3d6000fd5b5050604080517f5b0fc9c300000000000000000000000000000000000000000000000000000000815260006004820181905273ffffffffffffffffffffffffffffffffffffffff898116602484015292519287169450635b0fc9c39350604480830193919282900301818387803b15801561051657600080fd5b505af115801561052a573d6000803e3d6000fd5b50506040805173ffffffffffffffffffffffffffffffffffffffff8616815290517fdbfb5ababf63f86424e8df6053dfb90f8b63ea26d7e1e8f68407af4fb2d2c4f29350908190036020019150a15092915050565b60405161064a806105a083390190565b60405161141e80610bea833901905600608060405234801561001057600080fd5b5060008080526020527fad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb58054600160a060020a031916331790556105f1806100596000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630178b8bf811461008757806302571be3146100c857806306ab5923146100e057806314ab90381461011657806316a25cbd1461013b5780631896f70a146101705780635b0fc9c3146101a1575b600080fd5b34801561009357600080fd5b5061009f6004356101d2565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156100d457600080fd5b5061009f6004356101fd565b3480156100ec57600080fd5b5061011460043560243573ffffffffffffffffffffffffffffffffffffffff60443516610225565b005b34801561012257600080fd5b5061011460043567ffffffffffffffff60243516610311565b34801561014757600080fd5b506101536004356103e7565b6040805167ffffffffffffffff9092168252519081900360200190f35b34801561017c57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff6024351661041e565b3480156101ad57600080fd5b5061011460043573ffffffffffffffffffffffffffffffffffffffff602435166104f3565b60009081526020819052604090206001015473ffffffffffffffffffffffffffffffffffffffff1690565b60009081526020819052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600083815260208190526040812054849073ffffffffffffffffffffffffffffffffffffffff16331461025757600080fd5b6040805186815260208082018790528251918290038301822073ffffffffffffffffffffffffffffffffffffffff871683529251929450869288927fce0457fe73731f824cc272376169235128c118b49d344817417c6d108d155e8292908290030190a350600090815260208190526040902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff929092169190911790555050565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461034357600080fd5b6040805167ffffffffffffffff84168152905184917f1d4f9bbfc9cab89d66e1a1562f2233ccbf1308cb4f63de2ead5787adddb8fa68919081900360200190a250600091825260208290526040909120600101805467ffffffffffffffff90921674010000000000000000000000000000000000000000027fffffffff0000000000000000ffffffffffffffffffffffffffffffffffffffff909216919091179055565b60009081526020819052604090206001015474010000000000000000000000000000000000000000900467ffffffffffffffff1690565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461045057600080fd5b6040805173ffffffffffffffffffffffffffffffffffffffff84168152905184917f335721b01866dc23fbee8b6b2c7b1e14d6f05c28cd35a2c934239f94095602a0919081900360200190a25060009182526020829052604090912060010180547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff909216919091179055565b600082815260208190526040902054829073ffffffffffffffffffffffffffffffffffffffff16331461052557600080fd5b6040805173ffffffffffffffffffffffffffffffffffffffff84168152905184917fd4735d920b0f87494915f556dd9b54c8f309026070caea5c737245152564d266919081900360200190a25060009182526020829052604090912080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff9092169190911790555600a165627a7a723058203213a96a5c5e630e44a93f7fa415f3c625e46c7a560debc4dcf02cff9018ee6e0029608060405234801561001057600080fd5b5060405160208061141e833981016040525160008054600160a060020a03909216600160a060020a03199092169190911790556113cc806100526000396000f3006080604052600436106100c45763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166301ffc9a781146100c957806310f13a8c146101175780632203ab56146101b557806329cd62ea1461024f5780632dff69411461026d5780633b3b57de1461029757806359d1d43c146102d8578063623195b0146103ab578063691f34311461040b5780637737221314610423578063c3d014d614610481578063c86902331461049c578063d5fa2b00146104cd575b600080fd5b3480156100d557600080fd5b506101037fffffffff00000000000000000000000000000000000000000000000000000000600435166104fe565b604080519115158252519081900360200190f35b34801561012357600080fd5b5060408051602060046024803582810135601f81018590048502860185019096528585526101b395833595369560449491939091019190819084018382808284375050604080516020601f89358b018035918201839004830284018301909452808352979a9998810197919650918201945092508291508401838280828437509497506107139650505050505050565b005b3480156101c157600080fd5b506101d060043560243561098f565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156102135781810151838201526020016101fb565b50505050905090810190601f1680156102405780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561025b57600080fd5b506101b3600435602435604435610a9b565b34801561027957600080fd5b50610285600435610bcb565b60408051918252519081900360200190f35b3480156102a357600080fd5b506102af600435610be1565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b3480156102e457600080fd5b5060408051602060046024803582810135601f8101859004850286018501909652858552610336958335953695604494919390910191908190840183828082843750949750610c099650505050505050565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610370578181015183820152602001610358565b50505050905090810190601f16801561039d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103b757600080fd5b50604080516020600460443581810135601f81018490048402850184019095528484526101b3948235946024803595369594606494920191908190840183828082843750949750610d309650505050505050565b34801561041757600080fd5b50610336600435610e61565b34801561042f57600080fd5b5060408051602060046024803582810135601f81018590048502860185019096528585526101b3958335953695604494919390910191908190840183828082843750949750610f059650505050505050565b34801561048d57600080fd5b506101b360043560243561108b565b3480156104a857600080fd5b506104b460043561119c565b6040805192835260208301919091528051918290030190f35b3480156104d957600080fd5b506101b360043573ffffffffffffffffffffffffffffffffffffffff602435166111b9565b60007fffffffff0000000000000000000000000000000000000000000000000000000082167f3b3b57de00000000000000000000000000000000000000000000000000000000148061059157507fffffffff0000000000000000000000000000000000000000000000000000000082167fd8389dc500000000000000000000000000000000000000000000000000000000145b806105dd57507fffffffff0000000000000000000000000000000000000000000000000000000082167f691f343100000000000000000000000000000000000000000000000000000000145b8061062957507fffffffff0000000000000000000000000000000000000000000000000000000082167f2203ab5600000000000000000000000000000000000000000000000000000000145b8061067557507fffffffff0000000000000000000000000000000000000000000000000000000082167fc869023300000000000000000000000000000000000000000000000000000000145b806106c157507fffffffff0000000000000000000000000000000000000000000000000000000082167f59d1d43c00000000000000000000000000000000000000000000000000000000145b8061070d57507fffffffff0000000000000000000000000000000000000000000000000000000082167f01ffc9a700000000000000000000000000000000000000000000000000000000145b92915050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561078957600080fd5b505af115801561079d573d6000803e3d6000fd5b505050506040513d60208110156107b357600080fd5b505173ffffffffffffffffffffffffffffffffffffffff16146107d557600080fd5b6000848152600160209081526040918290209151855185936005019287929182918401908083835b6020831061083a57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016107fd565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845161087b9591949190910192509050611305565b50826040518082805190602001908083835b602083106108ca57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0909201916020918201910161088d565b51815160209384036101000a60001901801990921691161790526040805192909401829003822081835289518383015289519096508a95507fd8c9334b1a9c2f9da342a0a2b32629c1a229b6445dad78947f674b44444a7550948a94508392908301919085019080838360005b8381101561094f578181015183820152602001610937565b50505050905090810190601f16801561097c5780820380516001836020036101000a031916815260200191505b509250505060405180910390a350505050565b60008281526001602081905260409091206060905b838311610a8e57828416158015906109dd5750600083815260068201602052604081205460026000196101006001841615020190911604115b15610a8357600083815260068201602090815260409182902080548351601f600260001961010060018616150201909316929092049182018490048402810184019094528084529091830182828015610a775780601f10610a4c57610100808354040283529160200191610a77565b820191906000526020600020905b815481529060010190602001808311610a5a57829003601f168201915b50505050509150610a93565b6002909202916109a4565b600092505b509250929050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610b1157600080fd5b505af1158015610b25573d6000803e3d6000fd5b505050506040513d6020811015610b3b57600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610b5d57600080fd5b604080518082018252848152602080820185815260008881526001835284902092516003840155516004909201919091558151858152908101849052815186927f1d6f5e03d3f63eb58751986629a5439baee5079ff04f345becb66e23eb154e46928290030190a250505050565b6000908152600160208190526040909120015490565b60009081526001602052604090205473ffffffffffffffffffffffffffffffffffffffff1690565b600082815260016020908152604091829020915183516060936005019285929182918401908083835b60208310610c6f57805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe09092019160209182019101610c32565b518151600019602094850361010090810a820192831692199390931691909117909252949092019687526040805197889003820188208054601f6002600183161590980290950116959095049283018290048202880182019052818752929450925050830182828015610d235780601f10610cf857610100808354040283529160200191610d23565b820191906000526020600020905b815481529060010190602001808311610d0657829003601f168201915b5050505050905092915050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810187905290518693339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610da657600080fd5b505af1158015610dba573d6000803e3d6000fd5b505050506040513d6020811015610dd057600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610df257600080fd5b6000198301831615610e0357600080fd5b600084815260016020908152604080832086845260060182529091208351610e2d92850190611305565b50604051839085907faa121bbeef5f32f5961a2a28966e769023910fc9479059ee3495d4c1a696efe390600090a350505050565b6000818152600160208181526040928390206002908101805485516000199582161561010002959095011691909104601f81018390048302840183019094528383526060939091830182828015610ef95780601f10610ece57610100808354040283529160200191610ef9565b820191906000526020600020905b815481529060010190602001808311610edc57829003601f168201915b50505050509050919050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b158015610f7b57600080fd5b505af1158015610f8f573d6000803e3d6000fd5b505050506040513d6020811015610fa557600080fd5b505173ffffffffffffffffffffffffffffffffffffffff1614610fc757600080fd5b60008381526001602090815260409091208351610fec92600290920191850190611305565b50604080516020808252845181830152845186937fb7d29e911041e8d9b843369e890bcb72c9388692ba48b65ac54e7214c4c348f79387939092839283019185019080838360005b8381101561104c578181015183820152602001611034565b50505050905090810190601f1680156110795780820380516001836020036101000a031916815260200191505b509250505060405180910390a2505050565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561110157600080fd5b505af1158015611115573d6000803e3d6000fd5b505050506040513d602081101561112b57600080fd5b505173ffffffffffffffffffffffffffffffffffffffff161461114d57600080fd5b6000838152600160208181526040928390209091018490558151848152915185927f0424b6fe0d9c3bdbece0e7879dc241bb0c22e900be8b6c168b4ee08bd9bf83bc92908290030190a2505050565b600090815260016020526040902060038101546004909101549091565b60008054604080517f02571be30000000000000000000000000000000000000000000000000000000081526004810186905290518593339373ffffffffffffffffffffffffffffffffffffffff16926302571be39260248083019360209383900390910190829087803b15801561122f57600080fd5b505af1158015611243573d6000803e3d6000fd5b505050506040513d602081101561125957600080fd5b505173ffffffffffffffffffffffffffffffffffffffff161461127b57600080fd5b60008381526001602090815260409182902080547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff86169081179091558251908152915185927f52d7d861f09ab3d26239d492e8968629f95e9e318cf0b73bfddc441522a15fd292908290030190a2505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061134657805160ff1916838001178555611373565b82800160010185558215611373579182015b82811115611373578251825591602001919060010190611358565b5061137f929150611383565b5090565b61139d91905b8082111561137f5760008155600101611389565b905600a165627a7a72305820494d2089cb484863f6172bb6f0ed3b148d6c8eb2a0dd86d330bf08813f99f65d0029a165627a7a72305820df7d6399d923bc8a4fe3d290869ee8614cd2e7d4ac7481c4de85e2df61d183370029") diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index 70244f25..3aefc98a 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -20,7 +20,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ethermint/app" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm" "github.com/cosmos/ethermint/x/evm/keeper" @@ -55,7 +55,7 @@ func TestEvmTestSuite(t *testing.T) { } func (suite *EvmTestSuite) TestHandleMsgEthereumTx() { - privkey, err := crypto.GenerateKey() + privkey, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) sender := ethcmn.HexToAddress(privkey.PubKey().Address().String()) @@ -219,7 +219,7 @@ func (suite *EvmTestSuite) TestHandlerLogs() { gasLimit := uint64(100000) gasPrice := big.NewInt(1000000) - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err, "failed to create key") bytecode := common.FromHex("0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029") @@ -250,7 +250,7 @@ func (suite *EvmTestSuite) TestQueryTxLogs() { gasLimit := uint64(100000) gasPrice := big.NewInt(1000000) - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err, "failed to create key") // send contract deployment transaction with an event in the constructor @@ -349,7 +349,7 @@ func (suite *EvmTestSuite) TestDeployAndCallContract() { gasLimit := uint64(100000000) gasPrice := big.NewInt(10000) - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err, "failed to create key") bytecode := common.FromHex("0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a36102c4806100dc6000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c010000000000000000000000000000000000000000000000000000000090048063893d20e814610058578063a6f9dae1146100a2575b600080fd5b6100606100e6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100e4600480360360208110156100b857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061010f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea265627a7a72315820f397f2733a89198bc7fed0764083694c5b828791f39ebcbc9e414bccef14b48064736f6c63430005100032") @@ -400,7 +400,7 @@ func (suite *EvmTestSuite) TestSendTransaction() { gasLimit := uint64(21000) gasPrice := big.NewInt(0x55ae82600) - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err, "failed to create key") pub := priv.ToECDSA().Public().(*ecdsa.PublicKey) diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index 01e56637..1096a063 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -10,7 +10,7 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm/types" ) @@ -328,7 +328,7 @@ func (suite *KeeperTestSuite) TestStateDB_CreateAccount() { } func (suite *KeeperTestSuite) TestStateDB_ClearStateObj() { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) @@ -341,7 +341,7 @@ func (suite *KeeperTestSuite) TestStateDB_ClearStateObj() { } func (suite *KeeperTestSuite) TestStateDB_Reset() { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) @@ -444,7 +444,7 @@ func (suite *KeeperTestSuite) TestSuiteDB_Suicide() { suite.Require().True(suite.app.EvmKeeper.HasSuicided(suite.ctx, suite.address), tc.name) } else { //Suicide only works for an account with non-zero balance/nonce - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) diff --git a/x/evm/types/genesis_test.go b/x/evm/types/genesis_test.go index 8f499aab..7ac722da 100644 --- a/x/evm/types/genesis_test.go +++ b/x/evm/types/genesis_test.go @@ -4,12 +4,13 @@ import ( "math/big" "testing" - "github.com/cosmos/ethermint/crypto" "github.com/stretchr/testify/require" ethcmn "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" + + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ) func TestValidateGenesisAccount(t *testing.T) { @@ -77,7 +78,7 @@ func TestValidateGenesisAccount(t *testing.T) { } func TestValidateGenesis(t *testing.T) { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() require.NoError(t, err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) diff --git a/x/evm/types/journal_test.go b/x/evm/types/journal_test.go index ee54fe91..11c4f89a 100644 --- a/x/evm/types/journal_test.go +++ b/x/evm/types/journal_test.go @@ -22,7 +22,7 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" ) @@ -40,7 +40,7 @@ func newTestCodec() *sdkcodec.Codec { RegisterCodec(cdc) sdk.RegisterCodec(cdc) - crypto.RegisterCodec(cdc) + ethsecp256k1.RegisterCodec(cdc) sdkcodec.RegisterCrypto(cdc) auth.RegisterCodec(cdc) ethermint.RegisterCodec(cdc) @@ -51,7 +51,7 @@ func newTestCodec() *sdkcodec.Codec { func (suite *JournalTestSuite) SetupTest() { suite.setup() - privkey, err := crypto.GenerateKey() + privkey, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) suite.address = ethcmn.BytesToAddress(privkey.PubKey().Address().Bytes()) diff --git a/x/evm/types/logs_test.go b/x/evm/types/logs_test.go index 4665d82c..6fd38b24 100644 --- a/x/evm/types/logs_test.go +++ b/x/evm/types/logs_test.go @@ -3,17 +3,16 @@ package types import ( "testing" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" "github.com/stretchr/testify/require" ethcmn "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" - - "github.com/cosmos/ethermint/crypto" ) func TestTransactionLogsValidate(t *testing.T) { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() require.NoError(t, err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) @@ -91,7 +90,7 @@ func TestTransactionLogsValidate(t *testing.T) { } func TestValidateLog(t *testing.T) { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() require.NoError(t, err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) diff --git a/x/evm/types/msg_test.go b/x/evm/types/msg_test.go index f4c8ff5c..5f9d0296 100644 --- a/x/evm/types/msg_test.go +++ b/x/evm/types/msg_test.go @@ -10,8 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethcmn "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -164,8 +163,8 @@ func TestMsgEthereumTxRLPDecode(t *testing.T) { func TestMsgEthereumTxSig(t *testing.T) { chainID := big.NewInt(3) - priv1, _ := crypto.GenerateKey() - priv2, _ := crypto.GenerateKey() + priv1, _ := ethsecp256k1.GenerateKey() + priv2, _ := ethsecp256k1.GenerateKey() addr1 := ethcmn.BytesToAddress(priv1.PubKey().Address().Bytes()) addr2 := ethcmn.BytesToAddress(priv2.PubKey().Address().Bytes()) diff --git a/x/evm/types/state_transition_test.go b/x/evm/types/state_transition_test.go index f096c610..c6577c98 100644 --- a/x/evm/types/state_transition_test.go +++ b/x/evm/types/state_transition_test.go @@ -5,7 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm/types" @@ -22,7 +22,7 @@ func (suite *StateDBTestSuite) TestTransitionDb() { _ = acc.SetCoins(sdk.NewCoins(balance)) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) recipient := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) diff --git a/x/evm/types/statedb_test.go b/x/evm/types/statedb_test.go index 8904e7a2..f4418afe 100644 --- a/x/evm/types/statedb_test.go +++ b/x/evm/types/statedb_test.go @@ -15,7 +15,7 @@ import ( ethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/cosmos/ethermint/app" - "github.com/cosmos/ethermint/crypto" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm/types" @@ -43,7 +43,7 @@ func (suite *StateDBTestSuite) SetupTest() { suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1}) suite.stateDB = suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx) - privkey, err := crypto.GenerateKey() + privkey, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) suite.address = ethcmn.BytesToAddress(privkey.PubKey().Address().Bytes()) @@ -377,7 +377,7 @@ func (suite *StateDBTestSuite) TestStateDB_CreateAccount() { } func (suite *StateDBTestSuite) TestStateDB_ClearStateObj() { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) @@ -390,7 +390,7 @@ func (suite *StateDBTestSuite) TestStateDB_ClearStateObj() { } func (suite *StateDBTestSuite) TestStateDB_Reset() { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) @@ -497,7 +497,7 @@ func (suite *StateDBTestSuite) TestSuiteDB_Suicide() { suite.Require().True(suite.stateDB.HasSuicided(suite.address), tc.name) } else { //Suicide only works for an account with non-zero balance/nonce - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) diff --git a/x/evm/types/utils.go b/x/evm/types/utils.go index 552fd57b..871e68b3 100644 --- a/x/evm/types/utils.go +++ b/x/evm/types/utils.go @@ -5,7 +5,8 @@ import ( "math/big" "strings" - "github.com/cosmos/ethermint/crypto" + "github.com/pkg/errors" + "golang.org/x/crypto/sha3" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,13 +17,12 @@ import ( ethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" - "github.com/pkg/errors" - "golang.org/x/crypto/sha3" + "github.com/cosmos/ethermint/crypto/ethsecp256k1" ) // GenerateEthAddress generates an Ethereum address. func GenerateEthAddress() ethcmn.Address { - priv, err := crypto.GenerateKey() + priv, err := ethsecp256k1.GenerateKey() if err != nil { panic(err) } @@ -47,9 +47,8 @@ func ValidateSigner(signBytes, sig []byte, signer ethcmn.Address) error { func rlpHash(x interface{}) (hash ethcmn.Hash) { hasher := sha3.NewLegacyKeccak256() - //nolint:gosec,errcheck - rlp.Encode(hasher, x) - hasher.Sum(hash[:0]) + _ = rlp.Encode(hasher, x) + _ = hasher.Sum(hash[:0]) return hash }