From 5ff2b316cb2359c75be62332462cc094c7ec9e73 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Wed, 5 Aug 2020 15:27:48 +0200 Subject: [PATCH] app: change bech32 prefix cosmos -> eth (#429) --- app/ethermint.go | 10 ++++++++-- cmd/emintcli/main.go | 5 ++--- cmd/emintd/main.go | 5 ++--- types/config.go | 30 ++++++++++++++++++++++++++++++ types/config_test.go | 27 +++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 types/config.go create mode 100644 types/config_test.go diff --git a/app/ethermint.go b/app/ethermint.go index 864b6e04..a7352564 100644 --- a/app/ethermint.go +++ b/app/ethermint.go @@ -27,7 +27,7 @@ import ( "github.com/cosmos/ethermint/app/ante" ethermintcodec "github.com/cosmos/ethermint/codec" - eminttypes "github.com/cosmos/ethermint/types" + ethermint "github.com/cosmos/ethermint/types" "github.com/cosmos/ethermint/x/evm" "github.com/cosmos/ethermint/x/faucet" @@ -37,6 +37,12 @@ import ( dbm "github.com/tendermint/tm-db" ) +func init() { + // set the address prefixes + config := sdk.GetConfig() + ethermint.SetBech32Prefixes(config) +} + const appName = "Ethermint" var ( @@ -176,7 +182,7 @@ func NewEthermintApp( // use custom Ethermint account for contracts app.AccountKeeper = auth.NewAccountKeeper( - appCodec, keys[auth.StoreKey], app.subspaces[auth.ModuleName], eminttypes.ProtoAccount, + appCodec, keys[auth.StoreKey], app.subspaces[auth.ModuleName], ethermint.ProtoAccount, ) app.BankKeeper = bank.NewBaseKeeper( appCodec, keys[bank.StoreKey], app.AccountKeeper, app.subspaces[bank.ModuleName], app.BlacklistedAccAddrs(), diff --git a/cmd/emintcli/main.go b/cmd/emintcli/main.go index e8f3acee..107b6ed1 100644 --- a/cmd/emintcli/main.go +++ b/cmd/emintcli/main.go @@ -26,6 +26,7 @@ import ( "github.com/cosmos/ethermint/codec" "github.com/cosmos/ethermint/crypto" "github.com/cosmos/ethermint/rpc" + ethermint "github.com/cosmos/ethermint/types" ) var ( @@ -44,9 +45,7 @@ func main() { // Read in the configuration file for the sdk config := sdk.GetConfig() - config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) - config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) - config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) + ethermint.SetBech32Prefixes(config) config.Seal() rootCmd := &cobra.Command{ diff --git a/cmd/emintd/main.go b/cmd/emintd/main.go index 22cf50ed..6f7ff046 100644 --- a/cmd/emintd/main.go +++ b/cmd/emintd/main.go @@ -31,6 +31,7 @@ import ( "github.com/cosmos/ethermint/client" "github.com/cosmos/ethermint/codec" "github.com/cosmos/ethermint/crypto" + ethermint "github.com/cosmos/ethermint/types" ) const flagInvCheckPeriod = "inv-check-period" @@ -52,9 +53,7 @@ func main() { clientkeys.KeysCdc = cdc config := sdk.GetConfig() - config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub) - config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub) - config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub) + ethermint.SetBech32Prefixes(config) config.Seal() ctx := server.NewDefaultContext() diff --git a/types/config.go b/types/config.go new file mode 100644 index 00000000..ce4f38ea --- /dev/null +++ b/types/config.go @@ -0,0 +1,30 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +const ( + // EthBech32Prefix defines the Bech32 prefix used for EthAccounts + EthBech32Prefix = "eth" + + // Bech32PrefixAccAddr defines the Bech32 prefix of an account's address + Bech32PrefixAccAddr = EthBech32Prefix + // Bech32PrefixAccPub defines the Bech32 prefix of an account's public key + Bech32PrefixAccPub = EthBech32Prefix + sdk.PrefixPublic + // Bech32PrefixValAddr defines the Bech32 prefix of a validator's operator address + Bech32PrefixValAddr = EthBech32Prefix + sdk.PrefixValidator + sdk.PrefixOperator + // Bech32PrefixValPub defines the Bech32 prefix of a validator's operator public key + Bech32PrefixValPub = EthBech32Prefix + sdk.PrefixValidator + sdk.PrefixOperator + sdk.PrefixPublic + // Bech32PrefixConsAddr defines the Bech32 prefix of a consensus node address + Bech32PrefixConsAddr = EthBech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus + // Bech32PrefixConsPub defines the Bech32 prefix of a consensus node public key + Bech32PrefixConsPub = EthBech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus + sdk.PrefixPublic +) + +// SetBech32Prefixes sets the global prefixes to be used when serializing addresses and public keys to Bech32 strings. +func SetBech32Prefixes(config *sdk.Config) { + config.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccPub) + config.SetBech32PrefixForValidator(Bech32PrefixValAddr, Bech32PrefixValPub) + config.SetBech32PrefixForConsensusNode(Bech32PrefixConsAddr, Bech32PrefixConsPub) +} diff --git a/types/config_test.go b/types/config_test.go new file mode 100644 index 00000000..2c6d65c7 --- /dev/null +++ b/types/config_test.go @@ -0,0 +1,27 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/require" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestSetBech32Prefixes(t *testing.T) { + config := sdk.GetConfig() + require.Equal(t, sdk.Bech32PrefixAccAddr, config.GetBech32AccountAddrPrefix()) + require.Equal(t, sdk.Bech32PrefixAccPub, config.GetBech32AccountPubPrefix()) + require.Equal(t, sdk.Bech32PrefixValAddr, config.GetBech32ValidatorAddrPrefix()) + require.Equal(t, sdk.Bech32PrefixValPub, config.GetBech32ValidatorPubPrefix()) + require.Equal(t, sdk.Bech32PrefixConsAddr, config.GetBech32ConsensusAddrPrefix()) + require.Equal(t, sdk.Bech32PrefixConsPub, config.GetBech32ConsensusPubPrefix()) + + SetBech32Prefixes(config) + require.Equal(t, Bech32PrefixAccAddr, config.GetBech32AccountAddrPrefix()) + require.Equal(t, Bech32PrefixAccPub, config.GetBech32AccountPubPrefix()) + require.Equal(t, Bech32PrefixValAddr, config.GetBech32ValidatorAddrPrefix()) + require.Equal(t, Bech32PrefixValPub, config.GetBech32ValidatorPubPrefix()) + require.Equal(t, Bech32PrefixConsAddr, config.GetBech32ConsensusAddrPrefix()) + require.Equal(t, Bech32PrefixConsPub, config.GetBech32ConsensusPubPrefix()) +}