Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
crypto: refactor for stargate (#559)
Browse files Browse the repository at this point in the history
* changelog

* update changelog

* crypto: refactor for stargate

* fixes

* fix keys

* changelog
  • Loading branch information
fedekunze committed Oct 6, 2020
1 parent e7a19a1 commit 4e01da9
Show file tree
Hide file tree
Showing 32 changed files with 165 additions and 146 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions client/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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()...,
)
}
6 changes: 3 additions & 3 deletions client/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func InitTestnet(
keyringBackend,
clientDir,
inBuf,
crypto.EthSecp256k1Options()...,
hd.EthSecp256k1Options()...,
)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/ethermintcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/ethermintd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/ethermintd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
12 changes: 3 additions & 9 deletions crypto/codec.go → crypto/ethsecp256k1/codec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package crypto
package ethsecp256k1

import (
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
Expand All @@ -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
Expand All @@ -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)
}
Loading

0 comments on commit 4e01da9

Please sign in to comment.