Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for eth address standard #36

Merged
merged 19 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
require.Equal(t, []byte("ok"), okValue)
}
// check block gas is always consumed
baseGas := uint64(70184) // baseGas is the gas consumed before tx msg
baseGas := uint64(74343) // baseGas is the gas consumed before tx msg
expGasConsumed := addUint64Saturating(tc.gasToConsume, baseGas)
if expGasConsumed > txtypes.MaxGasWanted {
// capped by gasLimit
Expand Down
39 changes: 20 additions & 19 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
payer, _ := flagSet.GetString(flags.FlagFeePayer)

if payer != "" {
payerAcc, err := sdk.AccAddressFromBech32(payer)
payerAcc, err := sdk.AccAddressFromHexUnsafe(payer)
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
return clientCtx, err
}
Expand All @@ -237,7 +237,7 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
granter, _ := flagSet.GetString(flags.FlagFeeGranter)

if granter != "" {
granterAcc, err := sdk.AccAddressFromBech32(granter)
granterAcc, err := sdk.AccAddressFromHexUnsafe(granter)
if err != nil {
return clientCtx, err
}
Expand All @@ -264,23 +264,24 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
}
}

if !clientCtx.IsAux || flagSet.Changed(flags.FlagAux) {
isAux, _ := flagSet.GetBool(flags.FlagAux)
clientCtx = clientCtx.WithAux(isAux)
if isAux {
// If the user didn't explicitly set an --output flag, use JSON by
// default.
if clientCtx.OutputFormat == "" || !flagSet.Changed(cli.OutputFlag) {
clientCtx = clientCtx.WithOutputFormat("json")
}

// If the user didn't explicitly set a --sign-mode flag, use
// DIRECT_AUX by default.
if clientCtx.SignModeStr == "" || !flagSet.Changed(flags.FlagSignMode) {
clientCtx = clientCtx.WithSignModeStr(flags.SignModeDirectAux)
}
}
}
// Aux mode is disabled
// if !clientCtx.IsAux || flagSet.Changed(flags.FlagAux) {
// isAux, _ := flagSet.GetBool(flags.FlagAux)
// clientCtx = clientCtx.WithAux(isAux)
// if isAux {
// // If the user didn't explicitly set an --output flag, use JSON by
// // default.
// if clientCtx.OutputFormat == "" || !flagSet.Changed(cli.OutputFlag) {
// clientCtx = clientCtx.WithOutputFormat("json")
// }
//
// // If the user didn't explicitly set a --sign-mode flag, use
// // DIRECT_AUX by default.
// if clientCtx.SignModeStr == "" || !flagSet.Changed(flags.FlagSignMode) {
// clientCtx = clientCtx.WithSignModeStr(flags.SignModeDirectAux)
// }
// }
// }

return clientCtx, nil
}
Expand Down
4 changes: 2 additions & 2 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ func GetFromFields(clientCtx Context, kr keyring.Keyring, from string) (sdk.AccA
return nil, "", 0, nil
}

addr, err := sdk.AccAddressFromBech32(from)
addr, err := sdk.AccAddressFromHexUnsafe(from)
switch {
case clientCtx.Simulate:
if err != nil {
return nil, "", 0, fmt.Errorf("a valid bech32 address must be provided in simulation mode: %w", err)
return nil, "", 0, fmt.Errorf("a valid address must be provided in simulation mode: %w", err)
}

return addr, "", 0, nil
Expand Down
10 changes: 5 additions & 5 deletions client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func TestGetFromFields(t *testing.T) {
keyring: func() keyring.Keyring {
return keyring.NewInMemory(cfg.Codec)
},
from: "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5",
expectedErr: "key with address cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5 not found: key not found",
from: "0x8953eb4f1b47c7982a698deb7c557d6e4f4cd923",
expectedErr: "key with address 0x8953eb4F1B47c7982A698DeB7c557D6e4F4CD923 not found",
},
{
keyring: func() keyring.Keyring {
Expand All @@ -211,7 +211,7 @@ func TestGetFromFields(t *testing.T) {
keyring: func() keyring.Keyring {
return keyring.NewInMemory(cfg.Codec)
},
from: "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5",
from: "0x8953eb4f1b47c7982a698deb7c557d6e4f4cd923",
clientCtx: client.Context{}.WithSimulation(true),
},
{
Expand All @@ -220,13 +220,13 @@ func TestGetFromFields(t *testing.T) {
},
from: "alice",
clientCtx: client.Context{}.WithSimulation(true),
expectedErr: "a valid bech32 address must be provided in simulation mode",
expectedErr: "a valid address must be provided in simulation mode",
},
{
keyring: func() keyring.Keyring {
return keyring.NewInMemory(cfg.Codec)
},
from: "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5",
from: "0x8953eb4f1b47c7982a698deb7c557d6e4f4cd923",
clientCtx: client.Context{}.WithGenerateOnly(true),
},
{
Expand Down
20 changes: 10 additions & 10 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"strconv"
"strings"

"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -75,8 +75,8 @@ func bytesToPubkey(bz []byte, keytype string) (cryptotypes.PubKey, bool) {
}
}

if len(bz) == secp256k1.PubKeySize {
return &secp256k1.PubKey{Key: bz}, true
if len(bz) == ethsecp256k1.PubKeySize {
return &ethsecp256k1.PubKey{Key: bz}, true
}
return nil, false
}
Expand Down Expand Up @@ -123,11 +123,11 @@ func getPubKeyFromRawString(pkstr string, keytype string) (cryptotypes.PubKey, e
func PubkeyRawCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pubkey-raw [pubkey] -t [{ed25519, secp256k1}]",
Short: "Decode a ED25519 or secp256k1 pubkey from hex, base64, or bech32",
Long: fmt.Sprintf(`Decode a pubkey from hex, base64, or bech32.
Short: "Decode a ED25519 or eth_secp256k1 pubkey from hex, or base64",
Long: fmt.Sprintf(`Decode a pubkey from hex, or base64.
Example:
$ %s debug pubkey-raw TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
$ %s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
$ %s debug pubkey-raw 0x9f86D081884C7d659A2fEaA0C55AD015A3bf4F1B
`, version.AppName, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -138,8 +138,8 @@ $ %s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
return err
}
pubkeyType = strings.ToLower(pubkeyType)
if pubkeyType != "secp256k1" && pubkeyType != "ed25519" {
return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type, expected oneof ed25519 or secp256k1")
if pubkeyType != "eth_secp256k1" && pubkeyType != "ed25519" {
return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type, expected oneof ed25519 or eth_secp256k1")
}

pk, err := getPubKeyFromRawString(args[0], pubkeyType)
Expand Down Expand Up @@ -205,10 +205,10 @@ $ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
addr, err = hex.DecodeString(addrString)
if err != nil {
var err2 error
addr, err2 = sdk.AccAddressFromBech32(addrString)
addr, err2 = sdk.AccAddressFromHexUnsafe(addrString)
if err2 != nil {
var err3 error
addr, err3 = sdk.ValAddressFromBech32(addrString)
addr, err3 = sdk.ValAddressFromHex(addrString)

if err3 != nil {
return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
Expand Down
10 changes: 6 additions & 4 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
// failures due to state changes that might occur between the tx simulation
// and the actual run.
DefaultGasAdjustment = 1.0
DefaultGasLimit = 200000
DefaultGasLimit = 210000
GasFlagAuto = "auto"

// DefaultKeyringBackend
Expand All @@ -39,6 +39,8 @@ const (
SignModeDirectAux = "direct-aux"
// SignModeEIP191 is the value of the --sign-mode flag for SIGN_MODE_EIP_191
SignModeEIP191 = "eip-191"
// SignModeEIP712 is the value of the --sign-mode flag for SIGN_MODE_EIP_712
SignModeEIP712 = "eip-712"
)

// List of CLI flags
Expand Down Expand Up @@ -78,7 +80,7 @@ const (
FlagFeeGranter = "fee-granter"
FlagReverse = "reverse"
FlagTip = "tip"
FlagAux = "aux"
// FlagAux = "aux"

// Tendermint logging flags
FlagLogLevel = "log_level"
Expand Down Expand Up @@ -119,12 +121,12 @@ func AddTxFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality)")
cmd.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation")
cmd.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test|memory)")
cmd.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json|direct-aux), this is an advanced feature")
cmd.Flags().String(FlagSignMode, "", "We disabled this flag in inscription")
cmd.Flags().Uint64(FlagTimeoutHeight, 0, "Set a block timeout height to prevent the tx from being committed past a certain height")
cmd.Flags().String(FlagFeePayer, "", "Fee payer pays fees for the transaction instead of deducting from the signer")
cmd.Flags().String(FlagFeeGranter, "", "Fee granter grants fees for the transaction")
cmd.Flags().String(FlagTip, "", "Tip is the amount that is going to be transferred to the fee payer on the target chain. This flag is only valid when used with --aux, and is ignored if the target chain didn't enable the TipDecorator")
cmd.Flags().Bool(FlagAux, false, "Generate aux signer data instead of sending a tx")
// cmd.Flags().Bool(FlagAux, false, "Generate aux signer data instead of sending a tx")

// --gas can accept integers and "auto"
cmd.Flags().String(FlagGas, "", fmt.Sprintf("gas limit to set per-transaction; set to %q to calculate sufficient gas automatically. Note: %q option doesn't always report accurate results. Set a valid coin value to adjust the result. Can be used instead of %q. (default %d)",
Expand Down
4 changes: 2 additions & 2 deletions client/grpc/tmservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *IntegrationTestSuite) TestQueryLatestBlock() {
var blockInfoRes tmservice.GetLatestBlockResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &blockInfoRes))
s.Require().Equal(types.ValAddress(blockInfoRes.Block.Header.ProposerAddress).String(), blockInfoRes.SdkBlock.Header.ProposerAddress)
s.Require().Contains(blockInfoRes.SdkBlock.Header.ProposerAddress, "cosmosvaloper")
// s.Require().Contains(blockInfoRes.SdkBlock.Header.ProposerAddress, "cosmosvaloper")
}

func (s *IntegrationTestSuite) TestQueryBlockByHeight() {
Expand All @@ -101,7 +101,7 @@ func (s *IntegrationTestSuite) TestQueryBlockByHeight() {
s.Require().NoError(err)
var blockInfoRes tmservice.GetBlockByHeightResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &blockInfoRes))
s.Require().Contains(blockInfoRes.SdkBlock.Header.ProposerAddress, "cosmosvaloper")
// s.Require().Contains(blockInfoRes.SdkBlock.Header.ProposerAddress, "cosmosvaloper")
}

func (s *IntegrationTestSuite) TestQueryLatestValidatorSet() {
Expand Down
5 changes: 3 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ethHd "github.com/evmos/ethermint/crypto/hd"
)

const (
Expand Down Expand Up @@ -76,7 +77,7 @@ Example:
f.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation")
f.Uint32(flagAccount, 0, "Account number for HD derivation (less than equal 2147483647)")
f.Uint32(flagIndex, 0, "Address index number for HD derivation (less than equal 2147483647)")
f.String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")
f.String(flags.FlagKeyAlgorithm, string(ethHd.EthSecp256k1Type), "Key signing algorithm to generate keys for")

return cmd
}
Expand Down Expand Up @@ -120,7 +121,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf

if dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun); dryRun {
// use in memory keybase
kb = keyring.NewInMemory(ctx.Codec)
kb = keyring.NewInMemory(ctx.Codec, ethHd.EthSecp256k1Option())
} else {
_, err = kb.Key(name)
if err == nil {
Expand Down
5 changes: 3 additions & 2 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"testing"

ethHd "github.com/evmos/ethermint/crypto/hd"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/cli"

Expand Down Expand Up @@ -64,7 +65,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
require.NoError(t, cmd.ExecuteContext(ctx))

// Now check that it has been stored properly
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc, ethHd.EthSecp256k1Option())
require.NoError(t, err)
require.NotNil(t, kb)
t.Cleanup(func() {
Expand Down Expand Up @@ -171,7 +172,7 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {

kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc, ethHd.EthSecp256k1Option())
require.NoError(t, err)

clientCtx := client.Context{}.
Expand Down
5 changes: 3 additions & 2 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
ethHd "github.com/evmos/ethermint/crypto/hd"
)

func Test_runAddCmdBasic(t *testing.T) {
Expand All @@ -30,7 +31,7 @@ func Test_runAddCmdBasic(t *testing.T) {

cdc := simapp.MakeTestEncodingConfig().Codec

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc, ethHd.EthSecp256k1Option())
require.NoError(t, err)

clientCtx := client.Context{}.WithKeyringDir(kbHome).WithInput(mockIn).WithCodec(cdc)
Expand Down Expand Up @@ -193,7 +194,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc, ethHd.EthSecp256k1Option())
require.NoError(t, err)

clientCtx := client.Context{}.
Expand Down
3 changes: 2 additions & 1 deletion client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
ethHd "github.com/evmos/ethermint/crypto/hd"
)

func Test_runDeleteCmd(t *testing.T) {
Expand All @@ -37,7 +38,7 @@ func Test_runDeleteCmd(t *testing.T) {
cdc := simapp.MakeTestEncodingConfig().Codec

cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome)})
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc, ethHd.EthSecp256k1Option())
require.NoError(t, err)

_, err = kb.NewAccount(fakeKeyName1, testdata.TestMnemonic, "", path, hd.Secp256k1)
Expand Down
3 changes: 2 additions & 1 deletion client/keys/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
ethHd "github.com/evmos/ethermint/crypto/hd"
)

func Test_runRenameCmd(t *testing.T) {
Expand All @@ -33,7 +34,7 @@ func Test_runRenameCmd(t *testing.T) {
path := sdk.GetConfig().GetFullBIP44Path()

cdc := simapp.MakeTestEncodingConfig().Codec
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc, ethHd.EthSecp256k1Option())
require.NoError(t, err)

// put fakeKeyName1 in keyring
Expand Down
Loading