crypto: refactor for stargate (#559)

* changelog

* update changelog

* crypto: refactor for stargate

* fixes

* fix keys

* changelog
This commit is contained in:
Federico Kunze 2020-10-06 20:57:55 +02:00 committed by GitHub
parent e7a19a1c0a
commit 4e01da905a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 165 additions and 146 deletions

View File

@ -35,7 +35,13 @@ Ref: https://keepachangelog.com/en/1.0.0/
# Changelog # 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 ### Features

View File

@ -8,14 +8,14 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/types" "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" evmtypes "github.com/cosmos/ethermint/x/evm/types"
tmcrypto "github.com/tendermint/tendermint/crypto" tmcrypto "github.com/tendermint/tendermint/crypto"
) )
func init() { func init() {
crypto.RegisterCodec(types.ModuleCdc) ethsecp256k1.RegisterCodec(types.ModuleCdc)
} }
const ( const (
@ -76,7 +76,7 @@ func sigGasConsumer(
meter sdk.GasMeter, _ []byte, pubkey tmcrypto.PubKey, _ types.Params, meter sdk.GasMeter, _ []byte, pubkey tmcrypto.PubKey, _ types.Params,
) error { ) error {
switch pubkey.(type) { switch pubkey.(type) {
case crypto.PubKeySecp256k1: case ethsecp256k1.PubKey:
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: secp256k1") meter.ConsumeGas(secp256k1VerifyCost, "ante verify: secp256k1")
return nil return nil
case tmcrypto.PubKey: case tmcrypto.PubKey:

View File

@ -12,7 +12,7 @@ import (
"github.com/cosmos/ethermint/app" "github.com/cosmos/ethermint/app"
ante "github.com/cosmos/ethermint/app/ante" ante "github.com/cosmos/ethermint/app/ante"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
evmtypes "github.com/cosmos/ethermint/x/evm/types" evmtypes "github.com/cosmos/ethermint/x/evm/types"
@ -60,7 +60,7 @@ func newTestStdFee() auth.StdFee {
// GenerateAddress generates an Ethereum address. // GenerateAddress generates an Ethereum address.
func newTestAddrKey() (sdk.AccAddress, tmcrypto.PrivKey) { func newTestAddrKey() (sdk.AccAddress, tmcrypto.PrivKey) {
privkey, _ := crypto.GenerateKey() privkey, _ := ethsecp256k1.GenerateKey()
addr := ethcrypto.PubkeyToAddress(privkey.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(privkey.ToECDSA().PublicKey)
return sdk.AccAddress(addr.Bytes()), privkey return sdk.AccAddress(addr.Bytes()), privkey
@ -95,7 +95,7 @@ func newTestEthTx(ctx sdk.Context, msg evmtypes.MsgEthereumTx, priv tmcrypto.Pri
return nil, err return nil, err
} }
privkey, ok := priv.(crypto.PrivKeySecp256k1) privkey, ok := priv.(ethsecp256k1.PrivKey)
if !ok { if !ok {
return nil, fmt.Errorf("invalid private key type: %T", priv) return nil, fmt.Errorf("invalid private key type: %T", priv)
} }

View File

@ -15,8 +15,8 @@ import (
"github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/hd"
) )
// UnsafeExportEthKeyCommand exports a key with the given name as a private key in hex format. // 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.FlagKeyringBackend),
viper.GetString(flags.FlagHome), viper.GetString(flags.FlagHome),
inBuf, inBuf,
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
if err != nil { if err != nil {
return err return err
@ -64,7 +64,7 @@ func UnsafeExportEthKeyCommand() *cobra.Command {
} }
// Converts key to Ethermint secp256 implementation // Converts key to Ethermint secp256 implementation
emintKey, ok := privKey.(crypto.PrivKeySecp256k1) emintKey, ok := privKey.(ethsecp256k1.PrivKey)
if !ok { if !ok {
return fmt.Errorf("invalid private key type, must be Ethereum key: %T", privKey) return fmt.Errorf("invalid private key type, must be Ethereum key: %T", privKey)
} }

View File

@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/hd"
) )
const ( const (
@ -37,8 +37,8 @@ func KeyCommands() *cobra.Command {
// update the default signing algorithm value to "eth_secp256k1" // update the default signing algorithm value to "eth_secp256k1"
algoFlag := addCmd.Flag("algo") algoFlag := addCmd.Flag("algo")
algoFlag.DefValue = string(crypto.EthSecp256k1) algoFlag.DefValue = string(hd.EthSecp256k1)
err := algoFlag.Value.Set(string(crypto.EthSecp256k1)) err := algoFlag.Value.Set(string(hd.EthSecp256k1))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -74,7 +74,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) { func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) {
if transient { if transient {
return keys.NewInMemory( return keys.NewInMemory(
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
), nil ), nil
} }
@ -83,6 +83,6 @@ func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) {
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome), viper.GetString(flags.FlagHome),
buf, buf,
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
} }

View File

@ -36,7 +36,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/mint" "github.com/cosmos/cosmos-sdk/x/mint"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" 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" ethermint "github.com/cosmos/ethermint/types"
evmtypes "github.com/cosmos/ethermint/x/evm/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(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(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(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 return cmd
} }
@ -203,7 +203,7 @@ func InitTestnet(
keyringBackend, keyringBackend,
clientDir, clientDir,
inBuf, inBuf,
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
if err != nil { if err != nil {
return err return err

View File

@ -24,7 +24,7 @@ import (
"github.com/cosmos/ethermint/app" "github.com/cosmos/ethermint/app"
"github.com/cosmos/ethermint/client" "github.com/cosmos/ethermint/client"
"github.com/cosmos/ethermint/codec" "github.com/cosmos/ethermint/codec"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/rpc" "github.com/cosmos/ethermint/rpc"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
) )
@ -37,8 +37,8 @@ func main() {
// Configure cobra to sort commands // Configure cobra to sort commands
cobra.EnableCommandSorting = false cobra.EnableCommandSorting = false
tmamino.RegisterKeyType(crypto.PubKeySecp256k1{}, crypto.PubKeyAminoName) tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName)
tmamino.RegisterKeyType(crypto.PrivKeySecp256k1{}, crypto.PrivKeyAminoName) tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName)
keys.CryptoCdc = cdc keys.CryptoCdc = cdc
clientkeys.KeysCdc = cdc clientkeys.KeysCdc = cdc

View File

@ -20,7 +20,7 @@ import (
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting" authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/hd"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto" 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(flags.FlagKeyringBackend),
viper.GetString(flagClientHome), viper.GetString(flagClientHome),
inBuf, inBuf,
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
if err != nil { if err != nil {
return err return err

View File

@ -30,7 +30,7 @@ import (
"github.com/cosmos/ethermint/app" "github.com/cosmos/ethermint/app"
"github.com/cosmos/ethermint/client" "github.com/cosmos/ethermint/client"
"github.com/cosmos/ethermint/codec" "github.com/cosmos/ethermint/codec"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
) )
@ -43,8 +43,8 @@ func main() {
cdc := codec.MakeCodec(app.ModuleBasics) cdc := codec.MakeCodec(app.ModuleBasics)
tmamino.RegisterKeyType(crypto.PubKeySecp256k1{}, crypto.PubKeyAminoName) tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName)
tmamino.RegisterKeyType(crypto.PrivKeySecp256k1{}, crypto.PrivKeyAminoName) tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName)
keys.CryptoCdc = cdc keys.CryptoCdc = cdc
genutil.ModuleCdc = cdc genutil.ModuleCdc = cdc

View File

@ -7,7 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/vesting" "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" ethermint "github.com/cosmos/ethermint/types"
) )
@ -22,7 +22,7 @@ func MakeCodec(bm module.BasicManager) *codec.Codec {
bm.RegisterCodec(cdc) bm.RegisterCodec(cdc)
vesting.RegisterCodec(cdc) vesting.RegisterCodec(cdc)
sdk.RegisterCodec(cdc) sdk.RegisterCodec(cdc)
emintcrypto.RegisterCodec(cdc) cryptocodec.RegisterCodec(cdc)
codec.RegisterCrypto(cdc) codec.RegisterCrypto(cdc)
ethermint.RegisterCodec(cdc) ethermint.RegisterCodec(cdc)
keys.RegisterCodec(cdc) // temporary. Used to register keyring.Info keys.RegisterCodec(cdc) // temporary. Used to register keyring.Info

View File

@ -1,4 +1,4 @@
package crypto package ethsecp256k1
import ( import (
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino" cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
@ -10,12 +10,6 @@ import (
// CryptoCodec is the default amino codec used by ethermint // CryptoCodec is the default amino codec used by ethermint
var CryptoCodec = codec.New() var CryptoCodec = codec.New()
// Amino encoding names
const (
PrivKeyAminoName = "ethermint/PrivKeySecp256k1"
PubKeyAminoName = "ethermint/PubKeySecp256k1"
)
func init() { func init() {
// replace the keyring codec with the ethermint crypto codec to prevent // replace the keyring codec with the ethermint crypto codec to prevent
// amino panics because of unregistered Priv/PubKey // amino panics because of unregistered Priv/PubKey
@ -28,6 +22,6 @@ func init() {
// RegisterCodec registers all the necessary types with amino for the given // RegisterCodec registers all the necessary types with amino for the given
// codec. // codec.
func RegisterCodec(cdc *codec.Codec) { func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(PubKeySecp256k1{}, PubKeyAminoName, nil) cdc.RegisterConcrete(PubKey{}, PubKeyName, nil)
cdc.RegisterConcrete(PrivKeySecp256k1{}, PrivKeyAminoName, nil) cdc.RegisterConcrete(PrivKey{}, PrivKeyName, nil)
} }

View File

@ -1,56 +1,71 @@
package crypto package ethsecp256k1
import ( import (
"bytes" "bytes"
"crypto/ecdsa" "crypto/ecdsa"
ethcrypto "github.com/ethereum/go-ethereum/crypto" 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" 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 // 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. // Tendermint's PrivateKey interface.
type PrivKeySecp256k1 []byte type PrivKey []byte
// GenerateKey generates a new random private key. It returns an error upon // GenerateKey generates a new random private key. It returns an error upon
// failure. // failure.
func GenerateKey() (PrivKeySecp256k1, error) { func GenerateKey() (PrivKey, error) {
priv, err := ethcrypto.GenerateKey() priv, err := ethcrypto.GenerateKey()
if err != nil { 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. // PubKey returns the ECDSA private key's public key.
func (privkey PrivKeySecp256k1) PubKey() tmcrypto.PubKey { func (privkey PrivKey) PubKey() tmcrypto.PubKey {
ecdsaPKey := privkey.ToECDSA() ecdsaPKey := privkey.ToECDSA()
return PubKeySecp256k1(ethcrypto.CompressPubkey(&ecdsaPKey.PublicKey)) return PubKey(ethcrypto.CompressPubkey(&ecdsaPKey.PublicKey))
} }
// Bytes returns the raw ECDSA private key bytes. // Bytes returns the raw ECDSA private key bytes.
func (privkey PrivKeySecp256k1) Bytes() []byte { func (privkey PrivKey) Bytes() []byte {
return CryptoCodec.MustMarshalBinaryBare(privkey) return CryptoCodec.MustMarshalBinaryBare(privkey)
} }
// Sign creates a recoverable ECDSA signature on the secp256k1 curve over the // Sign creates a recoverable ECDSA signature on the secp256k1 curve over the
// Keccak256 hash of the provided message. The produced signature is 65 bytes // Keccak256 hash of the provided message. The produced signature is 65 bytes
// where the last byte contains the recovery ID. // 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()) return ethcrypto.Sign(ethcrypto.Keccak256Hash(msg).Bytes(), privkey.ToECDSA())
} }
// Equals returns true if two ECDSA private keys are equal and false otherwise. // Equals returns true if two ECDSA private keys are equal and false otherwise.
func (privkey PrivKeySecp256k1) Equals(other tmcrypto.PrivKey) bool { func (privkey PrivKey) Equals(other tmcrypto.PrivKey) bool {
if other, ok := other.(PrivKeySecp256k1); ok { if other, ok := other.(PrivKey); ok {
return bytes.Equal(privkey.Bytes(), other.Bytes()) 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. // ToECDSA returns the ECDSA private key as a reference to ecdsa.PrivateKey type.
// The function will panic if the private key is invalid. // 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) key, err := ethcrypto.ToECDSA(privkey)
if err != nil { if err != nil {
panic(err) panic(err)
@ -70,15 +85,15 @@ func (privkey PrivKeySecp256k1) ToECDSA() *ecdsa.PrivateKey {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// secp256k1 Public Key // 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. // 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. // Address returns the address of the ECDSA public key.
// The function will panic if the public key is invalid. // 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) pubk, err := ethcrypto.DecompressPubkey(key)
if err != nil { if err != nil {
panic(err) panic(err)
@ -89,7 +104,7 @@ func (key PubKeySecp256k1) Address() tmcrypto.Address {
// Bytes returns the raw bytes of the ECDSA public key. // Bytes returns the raw bytes of the ECDSA public key.
// The function panics if the key cannot be marshaled to bytes. // 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) bz, err := CryptoCodec.MarshalBinaryBare(key)
if err != nil { if err != nil {
panic(err) panic(err)
@ -100,19 +115,19 @@ func (key PubKeySecp256k1) Bytes() []byte {
// VerifyBytes verifies that the ECDSA public key created a given signature over // VerifyBytes verifies that the ECDSA public key created a given signature over
// the provided message. It will calculate the Keccak256 hash of the message // the provided message. It will calculate the Keccak256 hash of the message
// prior to verification. // 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 { if len(sig) == 65 {
// remove recovery ID if contained in the signature // remove recovery ID if contained in the signature
sig = sig[:len(sig)-1] sig = sig[:len(sig)-1]
} }
// the signature needs to be in [R || S] format when provided to VerifySignature // 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. // Equals returns true if two ECDSA public keys are equal and false otherwise.
func (key PubKeySecp256k1) Equals(other tmcrypto.PubKey) bool { func (key PubKey) Equals(other tmcrypto.PubKey) bool {
if other, ok := other.(PubKeySecp256k1); ok { if other, ok := other.(PubKey); ok {
return bytes.Equal(key.Bytes(), other.Bytes()) return bytes.Equal(key.Bytes(), other.Bytes())
} }

View File

@ -1,4 +1,4 @@
package crypto package ethsecp256k1
import ( import (
"testing" "testing"
@ -11,7 +11,7 @@ import (
tmcrypto "github.com/tendermint/tendermint/crypto" tmcrypto "github.com/tendermint/tendermint/crypto"
) )
func TestPrivKeySecp256k1PrivKey(t *testing.T) { func TestPrivKeyPrivKey(t *testing.T) {
// validate type and equality // validate type and equality
privKey, err := GenerateKey() privKey, err := GenerateKey()
require.NoError(t, err) require.NoError(t, err)
@ -38,12 +38,12 @@ func TestPrivKeySecp256k1PrivKey(t *testing.T) {
require.Equal(t, expectedSig, sig) require.Equal(t, expectedSig, sig)
} }
func TestPrivKeySecp256k1PubKey(t *testing.T) { func TestPrivKeyPubKey(t *testing.T) {
privKey, err := GenerateKey() privKey, err := GenerateKey()
require.NoError(t, err) require.NoError(t, err)
// validate type and equality // validate type and equality
pubKey := privKey.PubKey().(PubKeySecp256k1) pubKey := privKey.PubKey().(PubKey)
require.Implements(t, (*tmcrypto.PubKey)(nil), pubKey) require.Implements(t, (*tmcrypto.PubKey)(nil), pubKey)
// validate inequality // validate inequality

View File

@ -1,4 +1,4 @@
package crypto package hd
import ( import (
"fmt" "fmt"
@ -15,13 +15,13 @@ import (
tmcrypto "github.com/tendermint/tendermint/crypto" tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keys" "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
) )
const ( const (
// EthSecp256k1Type string constant for the EthSecp256k1 algorithm
EthSecp256k1Type = "eth_secp256k1"
// EthSecp256k1 defines the ECDSA secp256k1 used on Ethereum // 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: // 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 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. // 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() privateKeyECDSA := privateKey.ToECDSA()
derivedKey := PrivKeySecp256k1(ethcrypto.FromECDSA(privateKeyECDSA)) derivedKey := ethsecp256k1.PrivKey(ethcrypto.FromECDSA(privateKeyECDSA))
return derivedKey, nil return derivedKey, nil
} }

View File

@ -1,4 +1,4 @@
package crypto package hd
import ( import (
"strings" "strings"
@ -15,11 +15,12 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/hd"
"github.com/cosmos/cosmos-sdk/tests" "github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
) )
func TestEthermintKeygenFunc(t *testing.T) { func TestEthermintKeygenFunc(t *testing.T) {
privkey, err := GenerateKey() privkey, err := ethsecp256k1.GenerateKey()
require.NoError(t, err) require.NoError(t, err)
testCases := []struct { testCases := []struct {
@ -109,7 +110,7 @@ func TestKeyring(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, bz) require.NotEmpty(t, bz)
privkey := PrivKeySecp256k1(bz) privkey := ethsecp256k1.PrivKey(bz)
addr := common.BytesToAddress(privkey.PubKey().Address().Bytes()) addr := common.BytesToAddress(privkey.PubKey().Address().Bytes())
wallet, err := hdwallet.NewFromMnemonic(mnemonic) wallet, err := hdwallet.NewFromMnemonic(mnemonic)

View File

@ -24,7 +24,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/ethermint/core" "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/types"
"github.com/cosmos/ethermint/x/evm" "github.com/cosmos/ethermint/x/evm"
evmtypes "github.com/cosmos/ethermint/x/evm/types" evmtypes "github.com/cosmos/ethermint/x/evm/types"
@ -72,7 +72,7 @@ func newTestCodec() *sdkcodec.Codec {
auth.RegisterCodec(cdc) auth.RegisterCodec(cdc)
bank.RegisterCodec(cdc) bank.RegisterCodec(cdc)
sdk.RegisterCodec(cdc) sdk.RegisterCodec(cdc)
emintcrypto.RegisterCodec(cdc) cryptocodec.RegisterCodec(cdc)
sdkcodec.RegisterCrypto(cdc) sdkcodec.RegisterCrypto(cdc)
return cdc return cdc

View File

@ -3,9 +3,11 @@
package rpc package rpc
import ( import (
"github.com/cosmos/cosmos-sdk/client/context"
emintcrypto "github.com/cosmos/ethermint/crypto"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
) )
// RPC namespaces and API version // RPC namespaces and API version
@ -19,7 +21,7 @@ const (
) )
// GetRPCAPIs returns the list of all APIs // 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) nonceLock := new(AddrLocker)
backend := NewEthermintBackend(cliCtx) backend := NewEthermintBackend(cliCtx)
ethAPI := NewPublicEthAPI(cliCtx, backend, nonceLock, keys) ethAPI := NewPublicEthAPI(cliCtx, backend, nonceLock, keys)

View File

@ -19,7 +19,8 @@ import (
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/ethermint/app" "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" "github.com/ethereum/go-ethereum/rpc"
) )
@ -45,7 +46,7 @@ func registerRoutes(rs *lcd.RestServer) {
accountName := viper.GetString(flagUnlockKey) accountName := viper.GetString(flagUnlockKey)
accountNames := strings.Split(accountName, ",") accountNames := strings.Split(accountName, ",")
var privkeys []crypto.PrivKeySecp256k1 var privkeys []ethsecp256k1.PrivKey
if len(accountName) > 0 { if len(accountName) > 0 {
var err error var err error
inBuf := bufio.NewReader(os.Stdin) inBuf := bufio.NewReader(os.Stdin)
@ -102,31 +103,31 @@ func registerRoutes(rs *lcd.RestServer) {
ws.start() ws.start()
} }
func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]crypto.PrivKeySecp256k1, error) { func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]ethsecp256k1.PrivKey, error) {
keybase, err := keys.NewKeyring( keybase, err := keys.NewKeyring(
sdk.KeyringServiceName(), sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome), viper.GetString(flags.FlagHome),
os.Stdin, os.Stdin,
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
if err != nil { if err != nil {
return []crypto.PrivKeySecp256k1{}, err return []ethsecp256k1.PrivKey{}, err
} }
// try the for loop with array []string accountNames // try the for loop with array []string accountNames
// run through the bottom code inside the for loop // 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 { for i, acc := range accountNames {
// With keyring keybase, password is not required as it is pulled from the OS prompt // With keyring keybase, password is not required as it is pulled from the OS prompt
privKey, err := keybase.ExportPrivateKeyObject(acc, passphrase) privKey, err := keybase.ExportPrivateKeyObject(acc, passphrase)
if err != nil { if err != nil {
return []crypto.PrivKeySecp256k1{}, err return []ethsecp256k1.PrivKey{}, err
} }
var ok bool var ok bool
keys[i], ok = privKey.(crypto.PrivKeySecp256k1) keys[i], ok = privKey.(ethsecp256k1.PrivKey)
if !ok { if !ok {
panic(fmt.Sprintf("invalid private key type %T at index %d", privKey, i)) panic(fmt.Sprintf("invalid private key type %T at index %d", privKey, i))
} }

View File

@ -10,7 +10,8 @@ import (
"github.com/spf13/viper" "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" params "github.com/cosmos/ethermint/rpc/args"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/utils" "github.com/cosmos/ethermint/utils"
@ -46,14 +47,14 @@ type PublicEthAPI struct {
chainIDEpoch *big.Int chainIDEpoch *big.Int
logger log.Logger logger log.Logger
backend Backend backend Backend
keys []crypto.PrivKeySecp256k1 // unlocked keys keys []ethsecp256k1.PrivKey // unlocked keys
nonceLock *AddrLocker nonceLock *AddrLocker
keybaseLock sync.Mutex keybaseLock sync.Mutex
} }
// NewPublicEthAPI creates an instance of the public ETH Web3 API. // NewPublicEthAPI creates an instance of the public ETH Web3 API.
func NewPublicEthAPI(cliCtx context.CLIContext, backend Backend, nonceLock *AddrLocker, func NewPublicEthAPI(cliCtx context.CLIContext, backend Backend, nonceLock *AddrLocker,
key []crypto.PrivKeySecp256k1) *PublicEthAPI { key []ethsecp256k1.PrivKey) *PublicEthAPI {
epoch, err := ethermint.ParseChainID(cliCtx.ChainID) epoch, err := ethermint.ParseChainID(cliCtx.ChainID)
if err != nil { if err != nil {
@ -86,7 +87,7 @@ func (e *PublicEthAPI) getKeybaseInfo() error {
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome), viper.GetString(flags.FlagHome),
e.cliCtx.Input, e.cliCtx.Input,
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
if err != nil { if err != nil {
return err return err
@ -181,7 +182,7 @@ func (e *PublicEthAPI) Accounts() ([]common.Address, error) {
viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome), viper.GetString(flags.FlagHome),
e.cliCtx.Input, e.cliCtx.Input,
crypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
if err != nil { if err != nil {
return addresses, err return addresses, err
@ -342,7 +343,7 @@ func (e *PublicEthAPI) ExportAccount(address common.Address, blockNumber BlockNu
return string(res), nil 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 { if len(keys) > 0 {
for _, key := range keys { for _, key := range keys {
if bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) { if bytes.Equal(key.PubKey().Address().Bytes(), address.Bytes()) {

View File

@ -19,7 +19,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto" "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" 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.FlagKeyringBackend),
viper.GetString(flags.FlagHome), viper.GetString(flags.FlagHome),
e.ethAPI.cliCtx.Input, e.ethAPI.cliCtx.Input,
emintcrypto.EthSecp256k1Options()..., hd.EthSecp256k1Options()...,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -77,9 +78,9 @@ func (e *PersonalEthAPI) ImportRawKey(privkey, password string) (common.Address,
return common.Address{}, err 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 // ignore error as we only care about the length of the list
list, _ := e.ethAPI.cliCtx.Keybase.List() list, _ := e.ethAPI.cliCtx.Keybase.List()
@ -126,7 +127,7 @@ func (e *PersonalEthAPI) LockAccount(address common.Address) bool {
continue 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])
copy(tmp[i:], e.ethAPI.keys[i+1:]) copy(tmp[i:], e.ethAPI.keys[i+1:])
e.ethAPI.keys = tmp 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) 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 { if err != nil {
return common.Address{}, err return common.Address{}, err
} }
@ -193,7 +194,7 @@ func (e *PersonalEthAPI) UnlockAccount(_ context.Context, addr common.Address, p
return false, err return false, err
} }
emintKey, ok := privKey.(emintcrypto.PrivKeySecp256k1) emintKey, ok := privKey.(ethsecp256k1.PrivKey)
if !ok { if !ok {
return false, fmt.Errorf("invalid private key type: %T", privKey) return false, fmt.Errorf("invalid private key type: %T", privKey)
} }

View File

@ -4,11 +4,11 @@ import (
"encoding/json" "encoding/json"
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
) )
func TestPersonal_ListAccounts(t *testing.T) { func TestPersonal_ListAccounts(t *testing.T) {

View File

@ -13,13 +13,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/types"
) )
func init() { func init() {
tmamino.RegisterKeyType(crypto.PubKeySecp256k1{}, crypto.PubKeyAminoName) tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName)
tmamino.RegisterKeyType(crypto.PrivKeySecp256k1{}, crypto.PrivKeyAminoName) tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName)
} }
type AccountTestSuite struct { type AccountTestSuite struct {
@ -84,7 +84,7 @@ func (suite *AccountTestSuite) TestEthermintAccountJSON() {
} }
func (suite *AccountTestSuite) TestEthermintPubKeyJSON() { func (suite *AccountTestSuite) TestEthermintPubKeyJSON() {
privkey, err := crypto.GenerateKey() privkey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
bz := privkey.PubKey().Bytes() bz := privkey.PubKey().Bytes()

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/app" "github.com/cosmos/ethermint/app"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm" "github.com/cosmos/ethermint/x/evm"
"github.com/cosmos/ethermint/x/evm/keeper" "github.com/cosmos/ethermint/x/evm/keeper"
@ -55,7 +55,7 @@ func TestEvmTestSuite(t *testing.T) {
} }
func (suite *EvmTestSuite) TestHandleMsgEthereumTx() { func (suite *EvmTestSuite) TestHandleMsgEthereumTx() {
privkey, err := crypto.GenerateKey() privkey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
sender := ethcmn.HexToAddress(privkey.PubKey().Address().String()) sender := ethcmn.HexToAddress(privkey.PubKey().Address().String())
@ -219,7 +219,7 @@ func (suite *EvmTestSuite) TestHandlerLogs() {
gasLimit := uint64(100000) gasLimit := uint64(100000)
gasPrice := big.NewInt(1000000) gasPrice := big.NewInt(1000000)
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err, "failed to create key") suite.Require().NoError(err, "failed to create key")
bytecode := common.FromHex("0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029") bytecode := common.FromHex("0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029")
@ -250,7 +250,7 @@ func (suite *EvmTestSuite) TestQueryTxLogs() {
gasLimit := uint64(100000) gasLimit := uint64(100000)
gasPrice := big.NewInt(1000000) gasPrice := big.NewInt(1000000)
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err, "failed to create key") suite.Require().NoError(err, "failed to create key")
// send contract deployment transaction with an event in the constructor // send contract deployment transaction with an event in the constructor
@ -349,7 +349,7 @@ func (suite *EvmTestSuite) TestDeployAndCallContract() {
gasLimit := uint64(100000000) gasLimit := uint64(100000000)
gasPrice := big.NewInt(10000) gasPrice := big.NewInt(10000)
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err, "failed to create key") suite.Require().NoError(err, "failed to create key")
bytecode := common.FromHex("0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a36102c4806100dc6000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c010000000000000000000000000000000000000000000000000000000090048063893d20e814610058578063a6f9dae1146100a2575b600080fd5b6100606100e6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100e4600480360360208110156100b857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061010f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea265627a7a72315820f397f2733a89198bc7fed0764083694c5b828791f39ebcbc9e414bccef14b48064736f6c63430005100032") bytecode := common.FromHex("0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a36102c4806100dc6000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c010000000000000000000000000000000000000000000000000000000090048063893d20e814610058578063a6f9dae1146100a2575b600080fd5b6100606100e6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100e4600480360360208110156100b857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061010f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea265627a7a72315820f397f2733a89198bc7fed0764083694c5b828791f39ebcbc9e414bccef14b48064736f6c63430005100032")
@ -400,7 +400,7 @@ func (suite *EvmTestSuite) TestSendTransaction() {
gasLimit := uint64(21000) gasLimit := uint64(21000)
gasPrice := big.NewInt(0x55ae82600) gasPrice := big.NewInt(0x55ae82600)
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err, "failed to create key") suite.Require().NoError(err, "failed to create key")
pub := priv.ToECDSA().Public().(*ecdsa.PublicKey) pub := priv.ToECDSA().Public().(*ecdsa.PublicKey)

View File

@ -10,7 +10,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm/types" "github.com/cosmos/ethermint/x/evm/types"
) )
@ -328,7 +328,7 @@ func (suite *KeeperTestSuite) TestStateDB_CreateAccount() {
} }
func (suite *KeeperTestSuite) TestStateDB_ClearStateObj() { func (suite *KeeperTestSuite) TestStateDB_ClearStateObj() {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)
@ -341,7 +341,7 @@ func (suite *KeeperTestSuite) TestStateDB_ClearStateObj() {
} }
func (suite *KeeperTestSuite) TestStateDB_Reset() { func (suite *KeeperTestSuite) TestStateDB_Reset() {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) 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) suite.Require().True(suite.app.EvmKeeper.HasSuicided(suite.ctx, suite.address), tc.name)
} else { } else {
//Suicide only works for an account with non-zero balance/nonce //Suicide only works for an account with non-zero balance/nonce
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)

View File

@ -4,12 +4,13 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/cosmos/ethermint/crypto"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
) )
func TestValidateGenesisAccount(t *testing.T) { func TestValidateGenesisAccount(t *testing.T) {
@ -77,7 +78,7 @@ func TestValidateGenesisAccount(t *testing.T) {
} }
func TestValidateGenesis(t *testing.T) { func TestValidateGenesis(t *testing.T) {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
require.NoError(t, err) require.NoError(t, err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)

View File

@ -22,7 +22,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
) )
@ -40,7 +40,7 @@ func newTestCodec() *sdkcodec.Codec {
RegisterCodec(cdc) RegisterCodec(cdc)
sdk.RegisterCodec(cdc) sdk.RegisterCodec(cdc)
crypto.RegisterCodec(cdc) ethsecp256k1.RegisterCodec(cdc)
sdkcodec.RegisterCrypto(cdc) sdkcodec.RegisterCrypto(cdc)
auth.RegisterCodec(cdc) auth.RegisterCodec(cdc)
ethermint.RegisterCodec(cdc) ethermint.RegisterCodec(cdc)
@ -51,7 +51,7 @@ func newTestCodec() *sdkcodec.Codec {
func (suite *JournalTestSuite) SetupTest() { func (suite *JournalTestSuite) SetupTest() {
suite.setup() suite.setup()
privkey, err := crypto.GenerateKey() privkey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
suite.address = ethcmn.BytesToAddress(privkey.PubKey().Address().Bytes()) suite.address = ethcmn.BytesToAddress(privkey.PubKey().Address().Bytes())

View File

@ -3,17 +3,16 @@ package types
import ( import (
"testing" "testing"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/ethermint/crypto"
) )
func TestTransactionLogsValidate(t *testing.T) { func TestTransactionLogsValidate(t *testing.T) {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
require.NoError(t, err) require.NoError(t, err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)
@ -91,7 +90,7 @@ func TestTransactionLogsValidate(t *testing.T) {
} }
func TestValidateLog(t *testing.T) { func TestValidateLog(t *testing.T) {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
require.NoError(t, err) require.NoError(t, err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)

View File

@ -10,8 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/crypto"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
@ -164,8 +163,8 @@ func TestMsgEthereumTxRLPDecode(t *testing.T) {
func TestMsgEthereumTxSig(t *testing.T) { func TestMsgEthereumTxSig(t *testing.T) {
chainID := big.NewInt(3) chainID := big.NewInt(3)
priv1, _ := crypto.GenerateKey() priv1, _ := ethsecp256k1.GenerateKey()
priv2, _ := crypto.GenerateKey() priv2, _ := ethsecp256k1.GenerateKey()
addr1 := ethcmn.BytesToAddress(priv1.PubKey().Address().Bytes()) addr1 := ethcmn.BytesToAddress(priv1.PubKey().Address().Bytes())
addr2 := ethcmn.BytesToAddress(priv2.PubKey().Address().Bytes()) addr2 := ethcmn.BytesToAddress(priv2.PubKey().Address().Bytes())

View File

@ -5,7 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm/types" "github.com/cosmos/ethermint/x/evm/types"
@ -22,7 +22,7 @@ func (suite *StateDBTestSuite) TestTransitionDb() {
_ = acc.SetCoins(sdk.NewCoins(balance)) _ = acc.SetCoins(sdk.NewCoins(balance))
suite.app.AccountKeeper.SetAccount(suite.ctx, acc) suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
recipient := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) recipient := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)

View File

@ -15,7 +15,7 @@ import (
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/ethermint/app" "github.com/cosmos/ethermint/app"
"github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
ethermint "github.com/cosmos/ethermint/types" ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm/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.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1})
suite.stateDB = suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx) suite.stateDB = suite.app.EvmKeeper.CommitStateDB.WithContext(suite.ctx)
privkey, err := crypto.GenerateKey() privkey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
suite.address = ethcmn.BytesToAddress(privkey.PubKey().Address().Bytes()) suite.address = ethcmn.BytesToAddress(privkey.PubKey().Address().Bytes())
@ -377,7 +377,7 @@ func (suite *StateDBTestSuite) TestStateDB_CreateAccount() {
} }
func (suite *StateDBTestSuite) TestStateDB_ClearStateObj() { func (suite *StateDBTestSuite) TestStateDB_ClearStateObj() {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)
@ -390,7 +390,7 @@ func (suite *StateDBTestSuite) TestStateDB_ClearStateObj() {
} }
func (suite *StateDBTestSuite) TestStateDB_Reset() { func (suite *StateDBTestSuite) TestStateDB_Reset() {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) 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) suite.Require().True(suite.stateDB.HasSuicided(suite.address), tc.name)
} else { } else {
//Suicide only works for an account with non-zero balance/nonce //Suicide only works for an account with non-zero balance/nonce
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) suite.Require().NoError(err)
addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey) addr := ethcrypto.PubkeyToAddress(priv.ToECDSA().PublicKey)

View File

@ -5,7 +5,8 @@ import (
"math/big" "math/big"
"strings" "strings"
"github.com/cosmos/ethermint/crypto" "github.com/pkg/errors"
"golang.org/x/crypto/sha3"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -16,13 +17,12 @@ import (
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/pkg/errors" "github.com/cosmos/ethermint/crypto/ethsecp256k1"
"golang.org/x/crypto/sha3"
) )
// GenerateEthAddress generates an Ethereum address. // GenerateEthAddress generates an Ethereum address.
func GenerateEthAddress() ethcmn.Address { func GenerateEthAddress() ethcmn.Address {
priv, err := crypto.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -47,9 +47,8 @@ func ValidateSigner(signBytes, sig []byte, signer ethcmn.Address) error {
func rlpHash(x interface{}) (hash ethcmn.Hash) { func rlpHash(x interface{}) (hash ethcmn.Hash) {
hasher := sha3.NewLegacyKeccak256() hasher := sha3.NewLegacyKeccak256()
//nolint:gosec,errcheck _ = rlp.Encode(hasher, x)
rlp.Encode(hasher, x) _ = hasher.Sum(hash[:0])
hasher.Sum(hash[:0])
return hash return hash
} }